blob: fae2703f71b13e33a8fba7ad23c49b56ab2d4fa1 [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) */
2845 long vcol_prev = -1; /* "vcol" of previous character */
2846 char_u *line; /* current line */
2847 char_u *ptr; /* current position in "line" */
2848 int row; /* row in the window, excl w_winrow */
2849 int screen_row; /* row on the screen, incl w_winrow */
2850
2851 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2852 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002853 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002854 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 int c_extra = NUL; /* extra chars, all the same */
2856 int extra_attr = 0; /* attributes when n_extra != 0 */
2857 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2858 displaying lcs_eol at end-of-line */
2859 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2860 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2861
2862 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2863 int saved_n_extra = 0;
2864 char_u *saved_p_extra = NULL;
2865 int saved_c_extra = 0;
2866 int saved_char_attr = 0;
2867
2868 int n_attr = 0; /* chars with special attr */
2869 int saved_attr2 = 0; /* char_attr saved for n_attr */
2870 int n_attr3 = 0; /* chars with overruling special attr */
2871 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2872
2873 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2874
2875 int fromcol, tocol; /* start/end of inverting */
2876 int fromcol_prev = -2; /* start of inverting after cursor */
2877 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002879 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 pos_T pos;
2881 long v;
2882
2883 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002884 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2886 in this line */
2887 int attr = 0; /* attributes for area highlighting */
2888 int area_attr = 0; /* attributes desired by highlighting */
2889 int search_attr = 0; /* attributes desired by 'hlsearch' */
2890#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002891 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 int syntax_attr = 0; /* attributes desired by syntax */
2893 int has_syntax = FALSE; /* this buffer has syntax highl. */
2894 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002895 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002896 int draw_color_col = FALSE; /* highlight colorcolumn */
2897 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002898#endif
2899#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002900 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002901# define SPWORDLEN 150
2902 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002903 int nextlinecol = 0; /* column where nextline[] starts */
2904 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002905 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002906 int spell_attr = 0; /* attributes desired by spelling */
2907 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002908 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2909 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002910 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002911 static int cap_col = -1; /* column to check for Cap word */
2912 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002913 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#endif
2915 int extra_check; /* has syntax or linebreak */
2916#ifdef FEAT_MBYTE
2917 int multi_attr = 0; /* attributes desired by multibyte */
2918 int mb_l = 1; /* multi-byte byte length */
2919 int mb_c = 0; /* decoded multi-byte character */
2920 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002921 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922#endif
2923#ifdef FEAT_DIFF
2924 int filler_lines; /* nr of filler lines to be drawn */
2925 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002926 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 int change_start = MAXCOL; /* first col of changed area */
2928 int change_end = -1; /* last col of changed area */
2929#endif
2930 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2931#ifdef FEAT_LINEBREAK
2932 int need_showbreak = FALSE;
2933#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002934#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2935 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002937 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938#endif
2939#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002940 matchitem_T *cur; /* points to the match list */
2941 match_T *shl; /* points to search_hl or a match */
2942 int shl_flag; /* flag to indicate whether search_hl
2943 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002944 int pos_inprogress; /* marks that position match search is
2945 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002946 int prevcol_hl_flag; /* flag to indicate whether prevcol
2947 equals startcol of search_hl or one
2948 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949#endif
2950#ifdef FEAT_ARABIC
2951 int prev_c = 0; /* previous Arabic character */
2952 int prev_c1 = 0; /* first composing char for prev_c */
2953#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002954#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002955 int did_line_attr = 0;
2956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 /* draw_state: items that are drawn in sequence: */
2959#define WL_START 0 /* nothing done yet */
2960#ifdef FEAT_CMDWIN
2961# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2962#else
2963# define WL_CMDLINE WL_START
2964#endif
2965#ifdef FEAT_FOLDING
2966# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2967#else
2968# define WL_FOLD WL_CMDLINE
2969#endif
2970#ifdef FEAT_SIGNS
2971# define WL_SIGN WL_FOLD + 1 /* column for signs */
2972#else
2973# define WL_SIGN WL_FOLD /* column for signs */
2974#endif
2975#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002976#ifdef FEAT_LINEBREAK
2977# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02002979# define WL_BRI WL_NR
2980#endif
2981#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2982# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
2983#else
2984# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#endif
2986#define WL_LINE WL_SBR + 1 /* text in the line */
2987 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002988#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 int feedback_col = 0;
2990 int feedback_old_attr = -1;
2991#endif
2992
Bram Moolenaar860cae12010-06-05 23:22:07 +02002993#ifdef FEAT_CONCEAL
2994 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002995 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002996 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002997 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002998 int is_concealing = FALSE;
2999 int boguscols = 0; /* nonexistent columns added to force
3000 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003001 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003002 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003003# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003004# define FIX_FOR_BOGUSCOLS \
3005 { \
3006 n_extra += vcol_off; \
3007 vcol -= vcol_off; \
3008 vcol_off = 0; \
3009 col -= boguscols; \
3010 boguscols = 0; \
3011 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003012#else
3013# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
3016 if (startrow > endrow) /* past the end already! */
3017 return startrow;
3018
3019 row = startrow;
3020 screen_row = row + W_WINROW(wp);
3021
3022 /*
3023 * To speed up the loop below, set extra_check when there is linebreak,
3024 * trailing white space and/or syntax processing to be done.
3025 */
3026#ifdef FEAT_LINEBREAK
3027 extra_check = wp->w_p_lbr;
3028#else
3029 extra_check = 0;
3030#endif
3031#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003032 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
3034 /* Prepare for syntax highlighting in this line. When there is an
3035 * error, stop syntax highlighting. */
3036 save_did_emsg = did_emsg;
3037 did_emsg = FALSE;
3038 syntax_start(wp, lnum);
3039 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003040 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else
3042 {
3043 did_emsg = save_did_emsg;
3044 has_syntax = TRUE;
3045 extra_check = TRUE;
3046 }
3047 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003048
3049 /* Check for columns to display for 'colorcolumn'. */
3050 color_cols = wp->w_p_cc_cols;
3051 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003052 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003053#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003054
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003055#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003056 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003057 && *wp->w_s->b_p_spl != NUL
3058 && wp->w_s->b_langp.ga_len > 0
3059 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003060 {
3061 /* Prepare for spell checking. */
3062 has_spell = TRUE;
3063 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003064
3065 /* Get the start of the next line, so that words that wrap to the next
3066 * line are found too: "et<line-break>al.".
3067 * Trick: skip a few chars for C/shell/Vim comments */
3068 nextline[SPWORDLEN] = NUL;
3069 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3070 {
3071 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3072 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3073 }
3074
3075 /* When a word wrapped from the previous line the start of the current
3076 * line is valid. */
3077 if (lnum == checked_lnum)
3078 cur_checked_col = checked_col;
3079 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003080
3081 /* When there was a sentence end in the previous line may require a
3082 * word starting with capital in this line. In line 1 always check
3083 * the first word. */
3084 if (lnum != capcol_lnum)
3085 cap_col = -1;
3086 if (lnum == 1)
3087 cap_col = 0;
3088 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#endif
3091
3092 /*
3093 * handle visual active in this window
3094 */
3095 fromcol = -10;
3096 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3098 {
3099 /* Visual is after curwin->w_cursor */
3100 if (ltoreq(curwin->w_cursor, VIsual))
3101 {
3102 top = &curwin->w_cursor;
3103 bot = &VIsual;
3104 }
3105 else /* Visual is before curwin->w_cursor */
3106 {
3107 top = &VIsual;
3108 bot = &curwin->w_cursor;
3109 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003110 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 if (VIsual_mode == Ctrl_V) /* block mode */
3112 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003113 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
3115 fromcol = wp->w_old_cursor_fcol;
3116 tocol = wp->w_old_cursor_lcol;
3117 }
3118 }
3119 else /* non-block mode */
3120 {
3121 if (lnum > top->lnum && lnum <= bot->lnum)
3122 fromcol = 0;
3123 else if (lnum == top->lnum)
3124 {
3125 if (VIsual_mode == 'V') /* linewise */
3126 fromcol = 0;
3127 else
3128 {
3129 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3130 if (gchar_pos(top) == NUL)
3131 tocol = fromcol + 1;
3132 }
3133 }
3134 if (VIsual_mode != 'V' && lnum == bot->lnum)
3135 {
3136 if (*p_sel == 'e' && bot->col == 0
3137#ifdef FEAT_VIRTUALEDIT
3138 && bot->coladd == 0
3139#endif
3140 )
3141 {
3142 fromcol = -10;
3143 tocol = MAXCOL;
3144 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003145 else if (bot->col == MAXCOL)
3146 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 else
3148 {
3149 pos = *bot;
3150 if (*p_sel == 'e')
3151 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3152 else
3153 {
3154 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3155 ++tocol;
3156 }
3157 }
3158 }
3159 }
3160
3161#ifndef MSDOS
3162 /* Check if the character under the cursor should not be inverted */
3163 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3164# ifdef FEAT_GUI
3165 && !gui.in_use
3166# endif
3167 )
3168 noinvcur = TRUE;
3169#endif
3170
3171 /* if inverting in this line set area_highlighting */
3172 if (fromcol >= 0)
3173 {
3174 area_highlighting = TRUE;
3175 attr = hl_attr(HLF_V);
3176#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003177 if ((clip_star.available && !clip_star.owned
3178 && clip_isautosel_star())
3179 || (clip_plus.available && !clip_plus.owned
3180 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 attr = hl_attr(HLF_VNC);
3182#endif
3183 }
3184 }
3185
3186 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003187 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003189 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 && wp == curwin
3191 && lnum >= curwin->w_cursor.lnum
3192 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3193 {
3194 if (lnum == curwin->w_cursor.lnum)
3195 getvcol(curwin, &(curwin->w_cursor),
3196 (colnr_T *)&fromcol, NULL, NULL);
3197 else
3198 fromcol = 0;
3199 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3200 {
3201 pos.lnum = lnum;
3202 pos.col = search_match_endcol;
3203 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3204 }
3205 else
3206 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003207 /* do at least one character; happens when past end of line */
3208 if (fromcol == tocol)
3209 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 area_highlighting = TRUE;
3211 attr = hl_attr(HLF_I);
3212 }
3213
3214#ifdef FEAT_DIFF
3215 filler_lines = diff_check(wp, lnum);
3216 if (filler_lines < 0)
3217 {
3218 if (filler_lines == -1)
3219 {
3220 if (diff_find_change(wp, lnum, &change_start, &change_end))
3221 diff_hlf = HLF_ADD; /* added line */
3222 else if (change_start == 0)
3223 diff_hlf = HLF_TXD; /* changed text */
3224 else
3225 diff_hlf = HLF_CHD; /* changed line */
3226 }
3227 else
3228 diff_hlf = HLF_ADD; /* added line */
3229 filler_lines = 0;
3230 area_highlighting = TRUE;
3231 }
3232 if (lnum == wp->w_topline)
3233 filler_lines = wp->w_topfill;
3234 filler_todo = filler_lines;
3235#endif
3236
3237#ifdef LINE_ATTR
3238# ifdef FEAT_SIGNS
3239 /* If this line has a sign with line highlighting set line_attr. */
3240 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3241 if (v != 0)
3242 line_attr = sign_get_attr((int)v, TRUE);
3243# endif
3244# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3245 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003246 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 line_attr = hl_attr(HLF_L);
3248# endif
3249 if (line_attr != 0)
3250 area_highlighting = TRUE;
3251#endif
3252
3253 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3254 ptr = line;
3255
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003256#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003257 if (has_spell)
3258 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003259 /* For checking first word with a capital skip white space. */
3260 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003261 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003262
Bram Moolenaar30abd282005-06-22 22:35:10 +00003263 /* To be able to spell-check over line boundaries copy the end of the
3264 * current line into nextline[]. Above the start of the next line was
3265 * copied to nextline[SPWORDLEN]. */
3266 if (nextline[SPWORDLEN] == NUL)
3267 {
3268 /* No next line or it is empty. */
3269 nextlinecol = MAXCOL;
3270 nextline_idx = 0;
3271 }
3272 else
3273 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003274 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003275 if (v < SPWORDLEN)
3276 {
3277 /* Short line, use it completely and append the start of the
3278 * next line. */
3279 nextlinecol = 0;
3280 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003281 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003282 nextline_idx = v + 1;
3283 }
3284 else
3285 {
3286 /* Long line, use only the last SPWORDLEN bytes. */
3287 nextlinecol = v - SPWORDLEN;
3288 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3289 nextline_idx = SPWORDLEN + 1;
3290 }
3291 }
3292 }
3293#endif
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 /* find start of trailing whitespace */
3296 if (wp->w_p_list && lcs_trail)
3297 {
3298 trailcol = (colnr_T)STRLEN(ptr);
3299 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3300 --trailcol;
3301 trailcol += (colnr_T) (ptr - line);
3302 extra_check = TRUE;
3303 }
3304
3305 /*
3306 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3307 * first character to be displayed.
3308 */
3309 if (wp->w_p_wrap)
3310 v = wp->w_skipcol;
3311 else
3312 v = wp->w_leftcol;
3313 if (v > 0)
3314 {
3315#ifdef FEAT_MBYTE
3316 char_u *prev_ptr = ptr;
3317#endif
3318 while (vcol < v && *ptr != NUL)
3319 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003320 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 vcol += c;
3322#ifdef FEAT_MBYTE
3323 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003325 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003328 /* When:
3329 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003330 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003331 * - 'virtualedit' is set, or
3332 * - the visual mode is active,
3333 * the end of the line may be before the start of the displayed part.
3334 */
3335 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003336#ifdef FEAT_SYN_HL
3337 wp->w_p_cuc || draw_color_col ||
3338#endif
3339#ifdef FEAT_VIRTUALEDIT
3340 virtual_active() ||
3341#endif
3342 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
3347 /* Handle a character that's not completely on the screen: Put ptr at
3348 * that character but skip the first few screen characters. */
3349 if (vcol > v)
3350 {
3351 vcol -= c;
3352#ifdef FEAT_MBYTE
3353 ptr = prev_ptr;
3354#else
3355 --ptr;
3356#endif
3357 n_skip = v - vcol;
3358 }
3359
3360 /*
3361 * Adjust for when the inverted text is before the screen,
3362 * and when the start of the inverted text is before the screen.
3363 */
3364 if (tocol <= vcol)
3365 fromcol = 0;
3366 else if (fromcol >= 0 && fromcol < vcol)
3367 fromcol = vcol;
3368
3369#ifdef FEAT_LINEBREAK
3370 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3371 if (wp->w_p_wrap)
3372 need_showbreak = TRUE;
3373#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003374#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003375 /* When spell checking a word we need to figure out the start of the
3376 * word and if it's badly spelled or not. */
3377 if (has_spell)
3378 {
3379 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003380 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003381 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003382
3383 pos = wp->w_cursor;
3384 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003385 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003386 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003387
3388 /* spell_move_to() may call ml_get() and make "line" invalid */
3389 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3390 ptr = line + linecol;
3391
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003392 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003393 {
3394 /* no bad word found at line start, don't check until end of a
3395 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003396 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003397 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003398 }
3399 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003400 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003401 /* bad word found, use attributes until end of word */
3402 word_end = wp->w_cursor.col + len + 1;
3403
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003404 /* Turn index into actual attributes. */
3405 if (spell_hlf != HLF_COUNT)
3406 spell_attr = highlight_attr[spell_hlf];
3407 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003408 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003409
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003410# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003411 /* Need to restart syntax highlighting for this line. */
3412 if (has_syntax)
3413 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003414# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003415 }
3416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418
3419 /*
3420 * Correct highlighting for cursor that can't be disabled.
3421 * Avoids having to check this for each character.
3422 */
3423 if (fromcol >= 0)
3424 {
3425 if (noinvcur)
3426 {
3427 if ((colnr_T)fromcol == wp->w_virtcol)
3428 {
3429 /* highlighting starts at cursor, let it start just after the
3430 * cursor */
3431 fromcol_prev = fromcol;
3432 fromcol = -1;
3433 }
3434 else if ((colnr_T)fromcol < wp->w_virtcol)
3435 /* restart highlighting after the cursor */
3436 fromcol_prev = wp->w_virtcol;
3437 }
3438 if (fromcol >= tocol)
3439 fromcol = -1;
3440 }
3441
3442#ifdef FEAT_SEARCH_EXTRA
3443 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003444 * Handle highlighting the last used search pattern and matches.
3445 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003447 cur = wp->w_match_head;
3448 shl_flag = FALSE;
3449 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003451 if (shl_flag == FALSE)
3452 {
3453 shl = &search_hl;
3454 shl_flag = TRUE;
3455 }
3456 else
3457 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003458 shl->startcol = MAXCOL;
3459 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003461 v = (long)(ptr - line);
3462 if (cur != NULL)
3463 cur->pos.cur = 0;
3464 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3465
3466 /* Need to get the line again, a multi-line regexp may have made it
3467 * invalid. */
3468 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3469 ptr = line + v;
3470
3471 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003473 if (shl->lnum == lnum)
3474 shl->startcol = shl->rm.startpos[0].col;
3475 else
3476 shl->startcol = 0;
3477 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3478 - shl->rm.startpos[0].lnum)
3479 shl->endcol = shl->rm.endpos[0].col;
3480 else
3481 shl->endcol = MAXCOL;
3482 /* Highlight one character for an empty match. */
3483 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003486 if (has_mbyte && line[shl->endcol] != NUL)
3487 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3488 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003490 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003492 if ((long)shl->startcol < v) /* match at leftcol */
3493 {
3494 shl->attr_cur = shl->attr;
3495 search_attr = shl->attr;
3496 }
3497 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003499 if (shl != &search_hl && cur != NULL)
3500 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502#endif
3503
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003504#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003505 /* Cursor line highlighting for 'cursorline' in the current window. Not
3506 * when Visual mode is active, because it's not clear what is selected
3507 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003508 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003509 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003510 {
3511 line_attr = hl_attr(HLF_CUL);
3512 area_highlighting = TRUE;
3513 }
3514#endif
3515
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003516 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 col = 0;
3518#ifdef FEAT_RIGHTLEFT
3519 if (wp->w_p_rl)
3520 {
3521 /* Rightleft window: process the text in the normal direction, but put
3522 * it in current_ScreenLine[] from right to left. Start at the
3523 * rightmost column of the window. */
3524 col = W_WIDTH(wp) - 1;
3525 off += col;
3526 }
3527#endif
3528
3529 /*
3530 * Repeat for the whole displayed line.
3531 */
3532 for (;;)
3533 {
3534 /* Skip this quickly when working on the text. */
3535 if (draw_state != WL_LINE)
3536 {
3537#ifdef FEAT_CMDWIN
3538 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3539 {
3540 draw_state = WL_CMDLINE;
3541 if (cmdwin_type != 0 && wp == curwin)
3542 {
3543 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003545 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 char_attr = hl_attr(HLF_AT);
3547 }
3548 }
3549#endif
3550
3551#ifdef FEAT_FOLDING
3552 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3553 {
3554 draw_state = WL_FOLD;
3555 if (wp->w_p_fdc > 0)
3556 {
3557 /* Draw the 'foldcolumn'. */
3558 fill_foldcolumn(extra, wp, FALSE, lnum);
3559 n_extra = wp->w_p_fdc;
3560 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003561 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 c_extra = NUL;
3563 char_attr = hl_attr(HLF_FC);
3564 }
3565 }
3566#endif
3567
3568#ifdef FEAT_SIGNS
3569 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3570 {
3571 draw_state = WL_SIGN;
3572 /* Show the sign column when there are any signs in this
3573 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003574 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003576 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003578 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579# endif
3580
3581 /* Draw two cells with the sign value or blank. */
3582 c_extra = ' ';
3583 char_attr = hl_attr(HLF_SC);
3584 n_extra = 2;
3585
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003586 if (row == startrow
3587#ifdef FEAT_DIFF
3588 + filler_lines && filler_todo <= 0
3589#endif
3590 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3593 SIGN_TEXT);
3594# ifdef FEAT_SIGN_ICONS
3595 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3596 SIGN_ICON);
3597 if (gui.in_use && icon_sign != 0)
3598 {
3599 /* Use the image in this position. */
3600 c_extra = SIGN_BYTE;
3601# ifdef FEAT_NETBEANS_INTG
3602 if (buf_signcount(wp->w_buffer, lnum) > 1)
3603 c_extra = MULTISIGN_BYTE;
3604# endif
3605 char_attr = icon_sign;
3606 }
3607 else
3608# endif
3609 if (text_sign != 0)
3610 {
3611 p_extra = sign_get_text(text_sign);
3612 if (p_extra != NULL)
3613 {
3614 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003615 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617 char_attr = sign_get_attr(text_sign, FALSE);
3618 }
3619 }
3620 }
3621 }
3622#endif
3623
3624 if (draw_state == WL_NR - 1 && n_extra == 0)
3625 {
3626 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003627 /* Display the absolute or relative line number. After the
3628 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3629 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 && (row == startrow
3631#ifdef FEAT_DIFF
3632 + filler_lines
3633#endif
3634 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3635 {
3636 /* Draw the line number (empty space after wrapping). */
3637 if (row == startrow
3638#ifdef FEAT_DIFF
3639 + filler_lines
3640#endif
3641 )
3642 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003643 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003644 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003645
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003646 if (wp->w_p_nu && !wp->w_p_rnu)
3647 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003648 num = (long)lnum;
3649 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003650 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003651 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003652 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003653 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003654 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003655 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003656 num = lnum;
3657 fmt = "%-*ld ";
3658 }
3659 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003660
Bram Moolenaar700e7342013-01-30 12:31:36 +01003661 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003662 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (wp->w_skipcol > 0)
3664 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3665 *p_extra = '-';
3666#ifdef FEAT_RIGHTLEFT
3667 if (wp->w_p_rl) /* reverse line numbers */
3668 rl_mirror(extra);
3669#endif
3670 p_extra = extra;
3671 c_extra = NUL;
3672 }
3673 else
3674 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003675 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003677#ifdef FEAT_SYN_HL
3678 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003679 * the current line differently.
3680 * TODO: Can we use CursorLine instead of CursorLineNr
3681 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003682 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003683 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003684 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 }
3688
Bram Moolenaar597a4222014-06-25 14:39:50 +02003689#ifdef FEAT_LINEBREAK
3690 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3691 && n_extra == 0 && *p_sbr != NUL)
3692 /* draw indent after showbreak value */
3693 draw_state = WL_BRI;
3694 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3695 /* After the showbreak, draw the breakindent */
3696 draw_state = WL_BRI - 1;
3697
3698 /* draw 'breakindent': indent wrapped text accordingly */
3699 if (draw_state == WL_BRI - 1 && n_extra == 0)
3700 {
3701 draw_state = WL_BRI;
3702# ifdef FEAT_DIFF
3703# endif
3704 if (wp->w_p_bri && n_extra == 0 && row != startrow
3705#ifdef FEAT_DIFF
3706 && filler_lines == 0
3707#endif
3708 )
3709 {
3710 char_attr = 0; /* was: hl_attr(HLF_AT); */
3711#ifdef FEAT_DIFF
3712 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003713 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003714 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003715 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3716 char_attr = hl_combine_attr(char_attr,
3717 hl_attr(HLF_CUL));
3718 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003719#endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003720 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003721 c_extra = ' ';
3722 n_extra = get_breakindent_win(wp,
3723 ml_get_buf(wp->w_buffer, lnum, FALSE));
3724 /* Correct end of highlighted area for 'breakindent',
3725 * required when 'linebreak' is also set. */
3726 if (tocol == vcol)
3727 tocol += n_extra;
3728 }
3729 }
3730#endif
3731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3733 if (draw_state == WL_SBR - 1 && n_extra == 0)
3734 {
3735 draw_state = WL_SBR;
3736# ifdef FEAT_DIFF
3737 if (filler_todo > 0)
3738 {
3739 /* Draw "deleted" diff line(s). */
3740 if (char2cells(fill_diff) > 1)
3741 c_extra = '-';
3742 else
3743 c_extra = fill_diff;
3744# ifdef FEAT_RIGHTLEFT
3745 if (wp->w_p_rl)
3746 n_extra = col + 1;
3747 else
3748# endif
3749 n_extra = W_WIDTH(wp) - col;
3750 char_attr = hl_attr(HLF_DED);
3751 }
3752# endif
3753# ifdef FEAT_LINEBREAK
3754 if (*p_sbr != NUL && need_showbreak)
3755 {
3756 /* Draw 'showbreak' at the start of each broken line. */
3757 p_extra = p_sbr;
3758 c_extra = NUL;
3759 n_extra = (int)STRLEN(p_sbr);
3760 char_attr = hl_attr(HLF_AT);
3761 need_showbreak = FALSE;
3762 /* Correct end of highlighted area for 'showbreak',
3763 * required when 'linebreak' is also set. */
3764 if (tocol == vcol)
3765 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003766#ifdef FEAT_SYN_HL
3767 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003768 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003769 char_attr = hl_combine_attr(char_attr,
3770 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773# endif
3774 }
3775#endif
3776
3777 if (draw_state == WL_LINE - 1 && n_extra == 0)
3778 {
3779 draw_state = WL_LINE;
3780 if (saved_n_extra)
3781 {
3782 /* Continue item from end of wrapped line. */
3783 n_extra = saved_n_extra;
3784 c_extra = saved_c_extra;
3785 p_extra = saved_p_extra;
3786 char_attr = saved_char_attr;
3787 }
3788 else
3789 char_attr = 0;
3790 }
3791 }
3792
3793 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003794 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003795 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#ifdef FEAT_DIFF
3797 && filler_todo <= 0
3798#endif
3799 )
3800 {
3801 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3802 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003803 /* Pretend we have finished updating the window. Except when
3804 * 'cursorcolumn' is set. */
3805#ifdef FEAT_SYN_HL
3806 if (wp->w_p_cuc)
3807 row = wp->w_cline_row + wp->w_cline_height;
3808 else
3809#endif
3810 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 break;
3812 }
3813
3814 if (draw_state == WL_LINE && area_highlighting)
3815 {
3816 /* handle Visual or match highlighting in this line */
3817 if (vcol == fromcol
3818#ifdef FEAT_MBYTE
3819 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3820 && (*mb_ptr2cells)(ptr) > 1)
3821#endif
3822 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003823 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 && vcol < tocol))
3825 area_attr = attr; /* start highlighting */
3826 else if (area_attr != 0
3827 && (vcol == tocol
3828 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831#ifdef FEAT_SEARCH_EXTRA
3832 if (!n_extra)
3833 {
3834 /*
3835 * Check for start/end of search pattern match.
3836 * After end, check for start/end of next match.
3837 * When another match, have to check for start again.
3838 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003839 * Do this for 'search_hl' and the match list (ordered by
3840 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003842 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003843 cur = wp->w_match_head;
3844 shl_flag = FALSE;
3845 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003847 if (shl_flag == FALSE
3848 && ((cur != NULL
3849 && cur->priority > SEARCH_HL_PRIORITY)
3850 || cur == NULL))
3851 {
3852 shl = &search_hl;
3853 shl_flag = TRUE;
3854 }
3855 else
3856 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003857 if (cur != NULL)
3858 cur->pos.cur = 0;
3859 pos_inprogress = TRUE;
3860 while (shl->rm.regprog != NULL
3861 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003863 if (shl->startcol != MAXCOL
3864 && v >= (long)shl->startcol
3865 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867 shl->attr_cur = shl->attr;
3868 }
Bram Moolenaar5307de02014-08-16 16:28:36 +02003869 else if (v >= (long)shl->endcol && shl->lnum == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
3871 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003872 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3873 pos_inprogress = cur == NULL || cur->pos.cur == 0
3874 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
3876 /* Need to get the line again, a multi-line regexp
3877 * may have made it invalid. */
3878 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3879 ptr = line + v;
3880
3881 if (shl->lnum == lnum)
3882 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003883 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003885 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003887 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003889 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
3891 /* highlight empty match, try again after
3892 * it */
3893#ifdef FEAT_MBYTE
3894 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003895 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003896 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 else
3898#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003899 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901
3902 /* Loop to check if the match starts at the
3903 * current position */
3904 continue;
3905 }
3906 }
3907 break;
3908 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003909 if (shl != &search_hl && cur != NULL)
3910 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003912
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003913 /* Use attributes from match with highest priority among
3914 * 'search_hl' and the match list. */
3915 search_attr = search_hl.attr_cur;
3916 cur = wp->w_match_head;
3917 shl_flag = FALSE;
3918 while (cur != NULL || shl_flag == FALSE)
3919 {
3920 if (shl_flag == FALSE
3921 && ((cur != NULL
3922 && cur->priority > SEARCH_HL_PRIORITY)
3923 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003924 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003925 shl = &search_hl;
3926 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003927 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003928 else
3929 shl = &cur->hl;
3930 if (shl->attr_cur != 0)
3931 search_attr = shl->attr_cur;
3932 if (shl != &search_hl && cur != NULL)
3933 cur = cur->next;
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936#endif
3937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003939 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003941 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3942 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003944 if (diff_hlf == HLF_TXD && ptr - line > change_end
3945 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003947 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003948 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3949 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003952
3953 /* Decide which of the highlight attributes to use. */
3954 attr_pri = TRUE;
3955 if (area_attr != 0)
3956 char_attr = area_attr;
3957 else if (search_attr != 0)
3958 char_attr = search_attr;
3959#ifdef LINE_ATTR
3960 /* Use line_attr when not in the Visual or 'incsearch' area
3961 * (area_attr may be 0 when "noinvcur" is set). */
3962 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003963 || vcol < fromcol || vcol_prev < fromcol_prev
3964 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003965 char_attr = line_attr;
3966#endif
3967 else
3968 {
3969 attr_pri = FALSE;
3970#ifdef FEAT_SYN_HL
3971 if (has_syntax)
3972 char_attr = syntax_attr;
3973 else
3974#endif
3975 char_attr = 0;
3976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978
3979 /*
3980 * Get the next character to put on the screen.
3981 */
3982 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003983 * The "p_extra" points to the extra stuff that is inserted to
3984 * represent special characters (non-printable stuff) and other
3985 * things. When all characters are the same, c_extra is used.
3986 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3987 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3989 */
3990 if (n_extra > 0)
3991 {
3992 if (c_extra != NUL)
3993 {
3994 c = c_extra;
3995#ifdef FEAT_MBYTE
3996 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3997 if (enc_utf8 && (*mb_char2len)(c) > 1)
3998 {
3999 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004000 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004001 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003 else
4004 mb_utf8 = FALSE;
4005#endif
4006 }
4007 else
4008 {
4009 c = *p_extra;
4010#ifdef FEAT_MBYTE
4011 if (has_mbyte)
4012 {
4013 mb_c = c;
4014 if (enc_utf8)
4015 {
4016 /* If the UTF-8 character is more than one byte:
4017 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004018 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 mb_utf8 = FALSE;
4020 if (mb_l > n_extra)
4021 mb_l = 1;
4022 else if (mb_l > 1)
4023 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004024 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004026 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028 }
4029 else
4030 {
4031 /* if this is a DBCS character, put it in "mb_c" */
4032 mb_l = MB_BYTE2LEN(c);
4033 if (mb_l >= n_extra)
4034 mb_l = 1;
4035 else if (mb_l > 1)
4036 mb_c = (c << 8) + p_extra[1];
4037 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004038 if (mb_l == 0) /* at the NUL at end-of-line */
4039 mb_l = 1;
4040
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 /* If a double-width char doesn't fit display a '>' in the
4042 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004043 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044# ifdef FEAT_RIGHTLEFT
4045 wp->w_p_rl ? (col <= 0) :
4046# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004047 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 && (*mb_char2cells)(mb_c) == 2)
4049 {
4050 c = '>';
4051 mb_c = c;
4052 mb_l = 1;
4053 mb_utf8 = FALSE;
4054 multi_attr = hl_attr(HLF_AT);
4055 /* put the pointer back to output the double-width
4056 * character at the start of the next line. */
4057 ++n_extra;
4058 --p_extra;
4059 }
4060 else
4061 {
4062 n_extra -= mb_l - 1;
4063 p_extra += mb_l - 1;
4064 }
4065 }
4066#endif
4067 ++p_extra;
4068 }
4069 --n_extra;
4070 }
4071 else
4072 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004073 if (p_extra_free != NULL)
4074 {
4075 vim_free(p_extra_free);
4076 p_extra_free = NULL;
4077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 /*
4079 * Get a character from the line itself.
4080 */
4081 c = *ptr;
4082#ifdef FEAT_MBYTE
4083 if (has_mbyte)
4084 {
4085 mb_c = c;
4086 if (enc_utf8)
4087 {
4088 /* If the UTF-8 character is more than one byte: Decode it
4089 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004090 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 mb_utf8 = FALSE;
4092 if (mb_l > 1)
4093 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004094 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 /* Overlong encoded ASCII or ASCII with composing char
4096 * is displayed normally, except a NUL. */
4097 if (mb_c < 0x80)
4098 c = mb_c;
4099 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004100
4101 /* At start of the line we can have a composing char.
4102 * Draw it as a space with a composing char. */
4103 if (utf_iscomposing(mb_c))
4104 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004105 int i;
4106
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004107 for (i = Screen_mco - 1; i > 0; --i)
4108 u8cc[i] = u8cc[i - 1];
4109 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004110 mb_c = ' ';
4111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 if ((mb_l == 1 && c >= 0x80)
4115 || (mb_l >= 1 && mb_c == 0)
4116 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004117# ifdef UNICODE16
4118 || mb_c >= 0x10000
4119# endif
4120 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
4122 /*
4123 * Illegal UTF-8 byte: display as <xx>.
4124 * Non-BMP character : display as ? or fullwidth ?.
4125 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004126# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004131# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (wp->w_p_rl) /* reverse */
4133 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004136# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else if (utf_char2cells(mb_c) != 2)
4138 STRCPY(extra, "?");
4139 else
4140 /* 0xff1f in UTF-8: full-width '?' */
4141 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 p_extra = extra;
4145 c = *p_extra;
4146 mb_c = mb_ptr2char_adv(&p_extra);
4147 mb_utf8 = (c >= 0x80);
4148 n_extra = (int)STRLEN(p_extra);
4149 c_extra = NUL;
4150 if (area_attr == 0 && search_attr == 0)
4151 {
4152 n_attr = n_extra + 1;
4153 extra_attr = hl_attr(HLF_8);
4154 saved_attr2 = char_attr; /* save current attr */
4155 }
4156 }
4157 else if (mb_l == 0) /* at the NUL at end-of-line */
4158 mb_l = 1;
4159#ifdef FEAT_ARABIC
4160 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4161 {
4162 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004163 int pc, pc1, nc;
4164 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /* The idea of what is the previous and next
4167 * character depends on 'rightleft'. */
4168 if (wp->w_p_rl)
4169 {
4170 pc = prev_c;
4171 pc1 = prev_c1;
4172 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004173 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175 else
4176 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004177 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004179 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 prev_c = mb_c;
4182
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004183 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 else
4186 prev_c = mb_c;
4187#endif
4188 }
4189 else /* enc_dbcs */
4190 {
4191 mb_l = MB_BYTE2LEN(c);
4192 if (mb_l == 0) /* at the NUL at end-of-line */
4193 mb_l = 1;
4194 else if (mb_l > 1)
4195 {
4196 /* We assume a second byte below 32 is illegal.
4197 * Hopefully this is OK for all double-byte encodings!
4198 */
4199 if (ptr[1] >= 32)
4200 mb_c = (c << 8) + ptr[1];
4201 else
4202 {
4203 if (ptr[1] == NUL)
4204 {
4205 /* head byte at end of line */
4206 mb_l = 1;
4207 transchar_nonprint(extra, c);
4208 }
4209 else
4210 {
4211 /* illegal tail byte */
4212 mb_l = 2;
4213 STRCPY(extra, "XX");
4214 }
4215 p_extra = extra;
4216 n_extra = (int)STRLEN(extra) - 1;
4217 c_extra = NUL;
4218 c = *p_extra++;
4219 if (area_attr == 0 && search_attr == 0)
4220 {
4221 n_attr = n_extra + 1;
4222 extra_attr = hl_attr(HLF_8);
4223 saved_attr2 = char_attr; /* save current attr */
4224 }
4225 mb_c = c;
4226 }
4227 }
4228 }
4229 /* If a double-width char doesn't fit display a '>' in the
4230 * last column; the character is displayed at the start of the
4231 * next line. */
4232 if ((
4233# ifdef FEAT_RIGHTLEFT
4234 wp->w_p_rl ? (col <= 0) :
4235# endif
4236 (col >= W_WIDTH(wp) - 1))
4237 && (*mb_char2cells)(mb_c) == 2)
4238 {
4239 c = '>';
4240 mb_c = c;
4241 mb_utf8 = FALSE;
4242 mb_l = 1;
4243 multi_attr = hl_attr(HLF_AT);
4244 /* Put pointer back so that the character will be
4245 * displayed at the start of the next line. */
4246 --ptr;
4247 }
4248 else if (*ptr != NUL)
4249 ptr += mb_l - 1;
4250
4251 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004252 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004253 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004254 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004257 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 c = ' ';
4259 if (area_attr == 0 && search_attr == 0)
4260 {
4261 n_attr = n_extra + 1;
4262 extra_attr = hl_attr(HLF_AT);
4263 saved_attr2 = char_attr; /* save current attr */
4264 }
4265 mb_c = c;
4266 mb_utf8 = FALSE;
4267 mb_l = 1;
4268 }
4269
4270 }
4271#endif
4272 ++ptr;
4273
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004274 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004275 if (wp->w_p_list && (c == 160
4276#ifdef FEAT_MBYTE
4277 || (mb_utf8 && mb_c == 160)
4278#endif
4279 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004280 {
4281 c = lcs_nbsp;
4282 if (area_attr == 0 && search_attr == 0)
4283 {
4284 n_attr = 1;
4285 extra_attr = hl_attr(HLF_8);
4286 saved_attr2 = char_attr; /* save current attr */
4287 }
4288#ifdef FEAT_MBYTE
4289 mb_c = c;
4290 if (enc_utf8 && (*mb_char2len)(c) > 1)
4291 {
4292 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004293 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004294 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004295 }
4296 else
4297 mb_utf8 = FALSE;
4298#endif
4299 }
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 if (extra_check)
4302 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004303#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004304 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004305#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004306
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004307#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 /* Get syntax attribute, unless still at the start of the line
4309 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004310 v = (long)(ptr - line);
4311 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 /* Get the syntax attribute for the character. If there
4314 * is an error, disable syntax highlighting. */
4315 save_did_emsg = did_emsg;
4316 did_emsg = FALSE;
4317
Bram Moolenaar217ad922005-03-20 22:37:15 +00004318 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004319# ifdef FEAT_SPELL
4320 has_spell ? &can_spell :
4321# endif
4322 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004325 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004326 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004327 has_syntax = FALSE;
4328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 else
4330 did_emsg = save_did_emsg;
4331
4332 /* Need to get the line again, a multi-line regexp may
4333 * have made it invalid. */
4334 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4335 ptr = line + v;
4336
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004337 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004339 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004340 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004341# ifdef FEAT_CONCEAL
4342 /* no concealing past the end of the line, it interferes
4343 * with line highlighting */
4344 if (c == NUL)
4345 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004346 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004347 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004350#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004351
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004352#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004353 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004354 * Only do this when there is no syntax highlighting, the
4355 * @Spell cluster is not used or the current syntax item
4356 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004357 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004358 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004359 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004360# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004361 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004362 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004363# endif
4364 if (c != 0 && (
4365# ifdef FEAT_SYN_HL
4366 !has_syntax ||
4367# endif
4368 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004369 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004370 char_u *prev_ptr, *p;
4371 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004372 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004373# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004374 if (has_mbyte)
4375 {
4376 prev_ptr = ptr - mb_l;
4377 v -= mb_l - 1;
4378 }
4379 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004380# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004381 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004382
4383 /* Use nextline[] if possible, it has the start of the
4384 * next line concatenated. */
4385 if ((prev_ptr - line) - nextlinecol >= 0)
4386 p = nextline + (prev_ptr - line) - nextlinecol;
4387 else
4388 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004389 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004390 len = spell_check(wp, p, &spell_hlf, &cap_col,
4391 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004392 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004393
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004394 /* In Insert mode only highlight a word that
4395 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004396 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004397 && (State & INSERT) != 0
4398 && wp->w_cursor.lnum == lnum
4399 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004400 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004401 && wp->w_cursor.col < (colnr_T)word_end)
4402 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004403 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004404 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004405 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004406
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004407 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004408 && (p - nextline) + len > nextline_idx)
4409 {
4410 /* Remember that the good word continues at the
4411 * start of the next line. */
4412 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004413 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004414 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004415
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004416 /* Turn index into actual attributes. */
4417 if (spell_hlf != HLF_COUNT)
4418 spell_attr = highlight_attr[spell_hlf];
4419
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004420 if (cap_col > 0)
4421 {
4422 if (p != prev_ptr
4423 && (p - nextline) + cap_col >= nextline_idx)
4424 {
4425 /* Remember that the word in the next line
4426 * must start with a capital. */
4427 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004428 cap_col = (int)((p - nextline) + cap_col
4429 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004430 }
4431 else
4432 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004433 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004434 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004435 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004436 }
4437 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004438 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004439 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004440 char_attr = hl_combine_attr(char_attr, spell_attr);
4441 else
4442 char_attr = hl_combine_attr(spell_attr, char_attr);
4443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#endif
4445#ifdef FEAT_LINEBREAK
4446 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004447 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004449 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004451 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452# ifdef FEAT_MBYTE
4453 has_mbyte ? mb_l :
4454# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004455 1);
4456 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004457 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004458 NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 c_extra = ' ';
4460 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004461 {
4462#ifdef FEAT_CONCEAL
4463 if (c == TAB)
4464 /* See "Tab alignment" below. */
4465 FIX_FOR_BOGUSCOLS;
4466#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004467 if (!wp->w_p_list)
4468 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471#endif
4472
4473 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4474 {
4475 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004476 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
4478 n_attr = 1;
4479 extra_attr = hl_attr(HLF_8);
4480 saved_attr2 = char_attr; /* save current attr */
4481 }
4482#ifdef FEAT_MBYTE
4483 mb_c = c;
4484 if (enc_utf8 && (*mb_char2len)(c) > 1)
4485 {
4486 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004487 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004488 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 else
4491 mb_utf8 = FALSE;
4492#endif
4493 }
4494 }
4495
4496 /*
4497 * Handling of non-printable characters.
4498 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004499 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
4501 /*
4502 * when getting a character from the file, we may have to
4503 * turn it into something else on the way to putting it
4504 * into "ScreenLines".
4505 */
4506 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4507 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004508 int tab_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004510 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004511 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004512#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004513 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004514#endif
4515 /* tab amount depends on current column */
4516 n_extra = tab_len;
4517#ifdef FEAT_LINEBREAK
4518 else
4519 {
4520 char_u *p;
4521 int len = n_extra;
4522 int i;
4523 int saved_nextra = n_extra;
4524
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004525#ifdef FEAT_CONCEAL
4526 if (is_concealing && vcol_off > 0)
4527 /* there are characters to conceal */
4528 tab_len += vcol_off;
4529#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004530 /* if n_extra > 0, it gives the number of chars, to
4531 * use for a tab, else we need to calculate the width
4532 * for a tab */
4533#ifdef FEAT_MBYTE
4534 len = (tab_len * mb_char2len(lcs_tab2));
4535 if (n_extra > 0)
4536 len += n_extra - tab_len;
4537#endif
4538 c = lcs_tab1;
4539 p = alloc((unsigned)(len + 1));
4540 vim_memset(p, ' ', len);
4541 p[len] = NUL;
4542 p_extra_free = p;
4543 for (i = 0; i < tab_len; i++)
4544 {
4545#ifdef FEAT_MBYTE
4546 mb_char2bytes(lcs_tab2, p);
4547 p += mb_char2len(lcs_tab2);
4548 n_extra += mb_char2len(lcs_tab2)
4549 - (saved_nextra > 0 ? 1 : 0);
4550#else
4551 p[i] = lcs_tab2;
4552#endif
4553 }
4554 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004555#ifdef FEAT_CONCEAL
4556 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4557 * macro below, so need to adjust for that here */
4558 if (is_concealing && vcol_off > 0)
4559 n_extra -= vcol_off;
4560#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004561 }
4562#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004563#ifdef FEAT_CONCEAL
4564 /* Tab alignment should be identical regardless of
4565 * 'conceallevel' value. So tab compensates of all
4566 * previous concealed characters, and thus resets vcol_off
4567 * and boguscols accumulated so far in the line. Note that
4568 * the tab can be longer than 'tabstop' when there
4569 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004570 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572#ifdef FEAT_MBYTE
4573 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4574#endif
4575 if (wp->w_p_list)
4576 {
4577 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004578#ifdef FEAT_LINEBREAK
4579 if (wp->w_p_lbr)
4580 c_extra = NUL; /* using p_extra from above */
4581 else
4582#endif
4583 c_extra = lcs_tab2;
4584 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 extra_attr = hl_attr(HLF_8);
4586 saved_attr2 = char_attr; /* save current attr */
4587#ifdef FEAT_MBYTE
4588 mb_c = c;
4589 if (enc_utf8 && (*mb_char2len)(c) > 1)
4590 {
4591 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004592 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004593 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595#endif
4596 }
4597 else
4598 {
4599 c_extra = ' ';
4600 c = ' ';
4601 }
4602 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004603 else if (c == NUL
4604 && ((wp->w_p_list && lcs_eol > 0)
4605 || ((fromcol >= 0 || fromcol_prev >= 0)
4606 && tocol > vcol
4607 && VIsual_mode != Ctrl_V
4608 && (
4609# ifdef FEAT_RIGHTLEFT
4610 wp->w_p_rl ? (col >= 0) :
4611# endif
4612 (col < W_WIDTH(wp)))
4613 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004614 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004615 && (colnr_T)vcol == wp->w_virtcol)))
4616 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004618 /* Display a '$' after the line or highlight an extra
4619 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4621 /* For a diff line the highlighting continues after the
4622 * "$". */
4623 if (
4624# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004625 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626# ifdef LINE_ATTR
4627 &&
4628# endif
4629# endif
4630# ifdef LINE_ATTR
4631 line_attr == 0
4632# endif
4633 )
4634#endif
4635 {
4636#ifdef FEAT_VIRTUALEDIT
4637 /* In virtualedit, visual selections may extend
4638 * beyond end of line. */
4639 if (area_highlighting && virtual_active()
4640 && tocol != MAXCOL && vcol < tocol)
4641 n_extra = 0;
4642 else
4643#endif
4644 {
4645 p_extra = at_end_str;
4646 n_extra = 1;
4647 c_extra = NUL;
4648 }
4649 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004650 if (wp->w_p_list)
4651 c = lcs_eol;
4652 else
4653 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 lcs_eol_one = -1;
4655 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004656 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
4658 extra_attr = hl_attr(HLF_AT);
4659 n_attr = 1;
4660 }
4661#ifdef FEAT_MBYTE
4662 mb_c = c;
4663 if (enc_utf8 && (*mb_char2len)(c) > 1)
4664 {
4665 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004666 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004667 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669 else
4670 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4671#endif
4672 }
4673 else if (c != NUL)
4674 {
4675 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004676 if (n_extra == 0)
4677 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678#ifdef FEAT_RIGHTLEFT
4679 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4680 rl_mirror(p_extra); /* reverse "<12>" */
4681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004683#ifdef FEAT_LINEBREAK
4684 if (wp->w_p_lbr)
4685 {
4686 char_u *p;
4687
4688 c = *p_extra;
4689 p = alloc((unsigned)n_extra + 1);
4690 vim_memset(p, ' ', n_extra);
4691 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4692 p[n_extra] = NUL;
4693 p_extra_free = p_extra = p;
4694 }
4695 else
4696#endif
4697 {
4698 n_extra = byte2cells(c) - 1;
4699 c = *p_extra++;
4700 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004701 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
4703 n_attr = n_extra + 1;
4704 extra_attr = hl_attr(HLF_8);
4705 saved_attr2 = char_attr; /* save current attr */
4706 }
4707#ifdef FEAT_MBYTE
4708 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4709#endif
4710 }
4711#ifdef FEAT_VIRTUALEDIT
4712 else if (VIsual_active
4713 && (VIsual_mode == Ctrl_V
4714 || VIsual_mode == 'v')
4715 && virtual_active()
4716 && tocol != MAXCOL
4717 && vcol < tocol
4718 && (
4719# ifdef FEAT_RIGHTLEFT
4720 wp->w_p_rl ? (col >= 0) :
4721# endif
4722 (col < W_WIDTH(wp))))
4723 {
4724 c = ' ';
4725 --ptr; /* put it back at the NUL */
4726 }
4727#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004728#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 else if ((
4730# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004731 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 ) && (
4735# ifdef FEAT_RIGHTLEFT
4736 wp->w_p_rl ? (col >= 0) :
4737# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004738 (col
4739# ifdef FEAT_CONCEAL
4740 - boguscols
4741# endif
4742 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
4744 /* Highlight until the right side of the window */
4745 c = ' ';
4746 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004747
4748 /* Remember we do the char for line highlighting. */
4749 ++did_line_attr;
4750
4751 /* don't do search HL for the rest of the line */
4752 if (line_attr != 0 && char_attr == search_attr && col > 0)
4753 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754# ifdef FEAT_DIFF
4755 if (diff_hlf == HLF_TXD)
4756 {
4757 diff_hlf = HLF_CHD;
4758 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004761 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4762 char_attr = hl_combine_attr(char_attr,
4763 hl_attr(HLF_CUL));
4764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766# endif
4767 }
4768#endif
4769 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004770
4771#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004772 if ( wp->w_p_cole > 0
4773 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4774 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004775 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004776 && !(lnum_in_visual_area
4777 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004778 {
4779 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004780 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004781 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4782 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004783 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004784 /* First time at this concealed item: display one
4785 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004786 if (syn_get_sub_char() != NUL)
4787 c = syn_get_sub_char();
4788 else if (lcs_conceal != NUL)
4789 c = lcs_conceal;
4790 else
4791 c = ' ';
4792
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004793 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004794
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004795 if (n_extra > 0)
4796 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004797 vcol += n_extra;
4798 if (wp->w_p_wrap && n_extra > 0)
4799 {
4800# ifdef FEAT_RIGHTLEFT
4801 if (wp->w_p_rl)
4802 {
4803 col -= n_extra;
4804 boguscols -= n_extra;
4805 }
4806 else
4807# endif
4808 {
4809 boguscols += n_extra;
4810 col += n_extra;
4811 }
4812 }
4813 n_extra = 0;
4814 n_attr = 0;
4815 }
4816 else if (n_skip == 0)
4817 {
4818 is_concealing = TRUE;
4819 n_skip = 1;
4820 }
4821# ifdef FEAT_MBYTE
4822 mb_c = c;
4823 if (enc_utf8 && (*mb_char2len)(c) > 1)
4824 {
4825 mb_utf8 = TRUE;
4826 u8cc[0] = 0;
4827 c = 0xc0;
4828 }
4829 else
4830 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4831# endif
4832 }
4833 else
4834 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004835 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004836 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004837 }
4838#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004841#ifdef FEAT_CONCEAL
4842 /* In the cursor line and we may be concealing characters: correct
4843 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004844 if (!did_wcol && draw_state == WL_LINE
4845 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004846 && conceal_cursor_line(wp)
4847 && (int)wp->w_virtcol <= vcol + n_skip)
4848 {
4849 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004850 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004851 did_wcol = TRUE;
4852 }
4853#endif
4854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 /* Don't override visual selection highlighting. */
4856 if (n_attr > 0
4857 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004858 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 char_attr = extra_attr;
4860
Bram Moolenaar81695252004-12-29 20:58:21 +00004861#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 /* XIM don't send preedit_start and preedit_end, but they send
4863 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4864 * im_is_preediting() here. */
4865 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004866 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 && (State & INSERT)
4868 && !p_imdisable
4869 && im_is_preediting()
4870 && draw_state == WL_LINE)
4871 {
4872 colnr_T tcol;
4873
4874 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004875 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 else
4877 tcol = preedit_end_col;
4878 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4879 {
4880 if (feedback_old_attr < 0)
4881 {
4882 feedback_col = 0;
4883 feedback_old_attr = char_attr;
4884 }
4885 char_attr = im_get_feedback_attr(feedback_col);
4886 if (char_attr < 0)
4887 char_attr = feedback_old_attr;
4888 feedback_col++;
4889 }
4890 else if (feedback_old_attr >= 0)
4891 {
4892 char_attr = feedback_old_attr;
4893 feedback_old_attr = -1;
4894 feedback_col = 0;
4895 }
4896 }
4897#endif
4898 /*
4899 * Handle the case where we are in column 0 but not on the first
4900 * character of the line and the user wants us to show us a
4901 * special character (via 'listchars' option "precedes:<char>".
4902 */
4903 if (lcs_prec_todo != NUL
4904 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4905#ifdef FEAT_DIFF
4906 && filler_todo <= 0
4907#endif
4908 && draw_state > WL_NR
4909 && c != NUL)
4910 {
4911 c = lcs_prec;
4912 lcs_prec_todo = NUL;
4913#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004914 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4915 {
4916 /* Double-width character being overwritten by the "precedes"
4917 * character, need to fill up half the character. */
4918 c_extra = MB_FILLER_CHAR;
4919 n_extra = 1;
4920 n_attr = 2;
4921 extra_attr = hl_attr(HLF_AT);
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 mb_c = c;
4924 if (enc_utf8 && (*mb_char2len)(c) > 1)
4925 {
4926 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004927 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004928 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930 else
4931 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4932#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004933 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
4935 saved_attr3 = char_attr; /* save current attr */
4936 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4937 n_attr3 = 1;
4938 }
4939 }
4940
4941 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004942 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004944 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004945#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004946 || did_line_attr == 1
4947#endif
4948 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004950#ifdef FEAT_SEARCH_EXTRA
4951 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004952
4953 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004954 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004955 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004956#endif
4957
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004958 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * highlight match at end of line. If it's beyond the last
4960 * char on the screen, just overwrite that one (tricky!) Not
4961 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004962#ifdef FEAT_SEARCH_EXTRA
4963 prevcol_hl_flag = FALSE;
4964 if (prevcol == (long)search_hl.startcol)
4965 prevcol_hl_flag = TRUE;
4966 else
4967 {
4968 cur = wp->w_match_head;
4969 while (cur != NULL)
4970 {
4971 if (prevcol == (long)cur->hl.startcol)
4972 {
4973 prevcol_hl_flag = TRUE;
4974 break;
4975 }
4976 cur = cur->next;
4977 }
4978 }
4979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004981 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004982 && (VIsual_mode != Ctrl_V
4983 || lnum == VIsual.lnum
4984 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004985 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986#ifdef FEAT_SEARCH_EXTRA
4987 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004988 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004989# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004990 && did_line_attr <= 1
4991# endif
4992 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#endif
4994 ))
4995 {
4996 int n = 0;
4997
4998#ifdef FEAT_RIGHTLEFT
4999 if (wp->w_p_rl)
5000 {
5001 if (col < 0)
5002 n = 1;
5003 }
5004 else
5005#endif
5006 {
5007 if (col >= W_WIDTH(wp))
5008 n = -1;
5009 }
5010 if (n != 0)
5011 {
5012 /* At the window boundary, highlight the last character
5013 * instead (better than nothing). */
5014 off += n;
5015 col += n;
5016 }
5017 else
5018 {
5019 /* Add a blank character to highlight. */
5020 ScreenLines[off] = ' ';
5021#ifdef FEAT_MBYTE
5022 if (enc_utf8)
5023 ScreenLinesUC[off] = 0;
5024#endif
5025 }
5026#ifdef FEAT_SEARCH_EXTRA
5027 if (area_attr == 0)
5028 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005029 /* Use attributes from match with highest priority among
5030 * 'search_hl' and the match list. */
5031 char_attr = search_hl.attr;
5032 cur = wp->w_match_head;
5033 shl_flag = FALSE;
5034 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005035 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005036 if (shl_flag == FALSE
5037 && ((cur != NULL
5038 && cur->priority > SEARCH_HL_PRIORITY)
5039 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005040 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005041 shl = &search_hl;
5042 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005043 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005044 else
5045 shl = &cur->hl;
5046 if ((ptr - line) - 1 == (long)shl->startcol)
5047 char_attr = shl->attr;
5048 if (shl != &search_hl && cur != NULL)
5049 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052#endif
5053 ScreenAttrs[off] = char_attr;
5054#ifdef FEAT_RIGHTLEFT
5055 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005058 --off;
5059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 else
5061#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005064 ++off;
5065 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005066 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005067#ifdef FEAT_SYN_HL
5068 eol_hl_off = 1;
5069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
Bram Moolenaar91170f82006-05-05 21:15:17 +00005073 /*
5074 * At end of the text line.
5075 */
5076 if (c == NUL)
5077 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005078#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005079 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5080 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005081 {
5082 /* highlight last char after line */
5083 --col;
5084 --off;
5085 --vcol;
5086 }
5087
Bram Moolenaar1a384422010-07-14 19:53:30 +02005088 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005089 if (wp->w_p_wrap)
5090 v = wp->w_skipcol;
5091 else
5092 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005093
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005094 /* check if line ends before left margin */
5095 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005096 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005097#ifdef FEAT_CONCEAL
5098 /* Get rid of the boguscols now, we want to draw until the right
5099 * edge for 'cursorcolumn'. */
5100 col -= boguscols;
5101 boguscols = 0;
5102#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005103
5104 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005105 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005106
5107 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005108 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5109 && (int)wp->w_virtcol <
5110 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005111 && lnum != wp->w_cursor.lnum)
5112 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005113# ifdef FEAT_RIGHTLEFT
5114 && !wp->w_p_rl
5115# endif
5116 )
5117 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005118 int rightmost_vcol = 0;
5119 int i;
5120
5121 if (wp->w_p_cuc)
5122 rightmost_vcol = wp->w_virtcol;
5123 if (draw_color_col)
5124 /* determine rightmost colorcolumn to possibly draw */
5125 for (i = 0; color_cols[i] >= 0; ++i)
5126 if (rightmost_vcol < color_cols[i])
5127 rightmost_vcol = color_cols[i];
5128
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005129 while (col < W_WIDTH(wp))
5130 {
5131 ScreenLines[off] = ' ';
5132#ifdef FEAT_MBYTE
5133 if (enc_utf8)
5134 ScreenLinesUC[off] = 0;
5135#endif
5136 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005137 if (draw_color_col)
5138 draw_color_col = advance_color_col(VCOL_HLC,
5139 &color_cols);
5140
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005141 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005142 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005143 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005144 ScreenAttrs[off++] = hl_attr(HLF_MC);
5145 else
5146 ScreenAttrs[off++] = 0;
5147
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005148 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005149 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005150
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005151 ++vcol;
5152 }
5153 }
5154#endif
5155
Bram Moolenaar860cae12010-06-05 23:22:07 +02005156 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5157 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 row++;
5159
5160 /*
5161 * Update w_cline_height and w_cline_folded if the cursor line was
5162 * updated (saves a call to plines() later).
5163 */
5164 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5165 {
5166 curwin->w_cline_row = startrow;
5167 curwin->w_cline_height = row - startrow;
5168#ifdef FEAT_FOLDING
5169 curwin->w_cline_folded = FALSE;
5170#endif
5171 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5172 }
5173
5174 break;
5175 }
5176
5177 /* line continues beyond line end */
5178 if (lcs_ext
5179 && !wp->w_p_wrap
5180#ifdef FEAT_DIFF
5181 && filler_todo <= 0
5182#endif
5183 && (
5184#ifdef FEAT_RIGHTLEFT
5185 wp->w_p_rl ? col == 0 :
5186#endif
5187 col == W_WIDTH(wp) - 1)
5188 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005189 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5191 {
5192 c = lcs_ext;
5193 char_attr = hl_attr(HLF_AT);
5194#ifdef FEAT_MBYTE
5195 mb_c = c;
5196 if (enc_utf8 && (*mb_char2len)(c) > 1)
5197 {
5198 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005199 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005200 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 else
5203 mb_utf8 = FALSE;
5204#endif
5205 }
5206
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005207#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005208 /* advance to the next 'colorcolumn' */
5209 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005210 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005211
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005212 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005213 * highlight the cursor position itself.
5214 * Also highlight the 'colorcolumn' if it is different than
5215 * 'cursorcolumn' */
5216 vcol_save_attr = -1;
5217 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005218 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005219 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005220 && lnum != wp->w_cursor.lnum)
5221 {
5222 vcol_save_attr = char_attr;
5223 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5224 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005225 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005226 {
5227 vcol_save_attr = char_attr;
5228 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5229 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005230 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005231#endif
5232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 /*
5234 * Store character to be displayed.
5235 * Skip characters that are left of the screen for 'nowrap'.
5236 */
5237 vcol_prev = vcol;
5238 if (draw_state < WL_LINE || n_skip <= 0)
5239 {
5240 /*
5241 * Store the character.
5242 */
5243#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5244 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5245 {
5246 /* A double-wide character is: put first halve in left cell. */
5247 --off;
5248 --col;
5249 }
5250#endif
5251 ScreenLines[off] = c;
5252#ifdef FEAT_MBYTE
5253 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005254 {
5255 if ((mb_c & 0xff00) == 0x8e00)
5256 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 else if (enc_utf8)
5260 {
5261 if (mb_utf8)
5262 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005263 int i;
5264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005266 if ((c & 0xff) == 0)
5267 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005268 for (i = 0; i < Screen_mco; ++i)
5269 {
5270 ScreenLinesC[i][off] = u8cc[i];
5271 if (u8cc[i] == 0)
5272 break;
5273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275 else
5276 ScreenLinesUC[off] = 0;
5277 }
5278 if (multi_attr)
5279 {
5280 ScreenAttrs[off] = multi_attr;
5281 multi_attr = 0;
5282 }
5283 else
5284#endif
5285 ScreenAttrs[off] = char_attr;
5286
5287#ifdef FEAT_MBYTE
5288 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5289 {
5290 /* Need to fill two screen columns. */
5291 ++off;
5292 ++col;
5293 if (enc_utf8)
5294 /* UTF-8: Put a 0 in the second screen char. */
5295 ScreenLines[off] = 0;
5296 else
5297 /* DBCS: Put second byte in the second screen char. */
5298 ScreenLines[off] = mb_c & 0xff;
5299 ++vcol;
5300 /* When "tocol" is halfway a character, set it to the end of
5301 * the character, otherwise highlighting won't stop. */
5302 if (tocol == vcol)
5303 ++tocol;
5304#ifdef FEAT_RIGHTLEFT
5305 if (wp->w_p_rl)
5306 {
5307 /* now it's time to backup one cell */
5308 --off;
5309 --col;
5310 }
5311#endif
5312 }
5313#endif
5314#ifdef FEAT_RIGHTLEFT
5315 if (wp->w_p_rl)
5316 {
5317 --off;
5318 --col;
5319 }
5320 else
5321#endif
5322 {
5323 ++off;
5324 ++col;
5325 }
5326 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005327#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005328 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005329 {
5330 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005331 ++vcol_off;
5332 if (n_extra > 0)
5333 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005334 if (wp->w_p_wrap)
5335 {
5336 /*
5337 * Special voodoo required if 'wrap' is on.
5338 *
5339 * Advance the column indicator to force the line
5340 * drawing to wrap early. This will make the line
5341 * take up the same screen space when parts are concealed,
5342 * so that cursor line computations aren't messed up.
5343 *
5344 * To avoid the fictitious advance of 'col' causing
5345 * trailing junk to be written out of the screen line
5346 * we are building, 'boguscols' keeps track of the number
5347 * of bad columns we have advanced.
5348 */
5349 if (n_extra > 0)
5350 {
5351 vcol += n_extra;
5352# ifdef FEAT_RIGHTLEFT
5353 if (wp->w_p_rl)
5354 {
5355 col -= n_extra;
5356 boguscols -= n_extra;
5357 }
5358 else
5359# endif
5360 {
5361 col += n_extra;
5362 boguscols += n_extra;
5363 }
5364 n_extra = 0;
5365 n_attr = 0;
5366 }
5367
5368
5369# ifdef FEAT_MBYTE
5370 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5371 {
5372 /* Need to fill two screen columns. */
5373# ifdef FEAT_RIGHTLEFT
5374 if (wp->w_p_rl)
5375 {
5376 --boguscols;
5377 --col;
5378 }
5379 else
5380# endif
5381 {
5382 ++boguscols;
5383 ++col;
5384 }
5385 }
5386# endif
5387
5388# ifdef FEAT_RIGHTLEFT
5389 if (wp->w_p_rl)
5390 {
5391 --boguscols;
5392 --col;
5393 }
5394 else
5395# endif
5396 {
5397 ++boguscols;
5398 ++col;
5399 }
5400 }
5401 else
5402 {
5403 if (n_extra > 0)
5404 {
5405 vcol += n_extra;
5406 n_extra = 0;
5407 n_attr = 0;
5408 }
5409 }
5410
5411 }
5412#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 else
5414 --n_skip;
5415
Bram Moolenaar64486672010-05-16 15:46:46 +02005416 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5417 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005418 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419#ifdef FEAT_DIFF
5420 && filler_todo <= 0
5421#endif
5422 )
5423 ++vcol;
5424
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005425#ifdef FEAT_SYN_HL
5426 if (vcol_save_attr >= 0)
5427 char_attr = vcol_save_attr;
5428#endif
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 /* restore attributes after "predeces" in 'listchars' */
5431 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5432 char_attr = saved_attr3;
5433
5434 /* restore attributes after last 'listchars' or 'number' char */
5435 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5436 char_attr = saved_attr2;
5437
5438 /*
5439 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005440 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 */
5442 if ((
5443#ifdef FEAT_RIGHTLEFT
5444 wp->w_p_rl ? (col < 0) :
5445#endif
5446 (col >= W_WIDTH(wp)))
5447 && (*ptr != NUL
5448#ifdef FEAT_DIFF
5449 || filler_todo > 0
5450#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005451 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5453 )
5454 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005455#ifdef FEAT_CONCEAL
5456 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5457 (int)W_WIDTH(wp), wp->w_p_rl);
5458 boguscols = 0;
5459#else
5460 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5461 (int)W_WIDTH(wp), wp->w_p_rl);
5462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 ++row;
5464 ++screen_row;
5465
5466 /* When not wrapping and finished diff lines, or when displayed
5467 * '$' and highlighting until last column, break here. */
5468 if ((!wp->w_p_wrap
5469#ifdef FEAT_DIFF
5470 && filler_todo <= 0
5471#endif
5472 ) || lcs_eol_one == -1)
5473 break;
5474
5475 /* When the window is too narrow draw all "@" lines. */
5476 if (draw_state != WL_LINE
5477#ifdef FEAT_DIFF
5478 && filler_todo <= 0
5479#endif
5480 )
5481 {
5482 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5483#ifdef FEAT_VERTSPLIT
5484 draw_vsep_win(wp, row);
5485#endif
5486 row = endrow;
5487 }
5488
5489 /* When line got too long for screen break here. */
5490 if (row == endrow)
5491 {
5492 ++row;
5493 break;
5494 }
5495
5496 if (screen_cur_row == screen_row - 1
5497#ifdef FEAT_DIFF
5498 && filler_todo <= 0
5499#endif
5500 && W_WIDTH(wp) == Columns)
5501 {
5502 /* Remember that the line wraps, used for modeless copy. */
5503 LineWraps[screen_row - 1] = TRUE;
5504
5505 /*
5506 * Special trick to make copy/paste of wrapped lines work with
5507 * xterm/screen: write an extra character beyond the end of
5508 * the line. This will work with all terminal types
5509 * (regardless of the xn,am settings).
5510 * Only do this on a fast tty.
5511 * Only do this if the cursor is on the current line
5512 * (something has been written in it).
5513 * Don't do this for the GUI.
5514 * Don't do this for double-width characters.
5515 * Don't do this for a window not at the right screen border.
5516 */
5517 if (p_tf
5518#ifdef FEAT_GUI
5519 && !gui.in_use
5520#endif
5521#ifdef FEAT_MBYTE
5522 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005523 && ((*mb_off2cells)(LineOffset[screen_row],
5524 LineOffset[screen_row] + screen_Columns)
5525 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005527 + (int)Columns - 2,
5528 LineOffset[screen_row] + screen_Columns)
5529 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530#endif
5531 )
5532 {
5533 /* First make sure we are at the end of the screen line,
5534 * then output the same character again to let the
5535 * terminal know about the wrap. If the terminal doesn't
5536 * auto-wrap, we overwrite the character. */
5537 if (screen_cur_col != W_WIDTH(wp))
5538 screen_char(LineOffset[screen_row - 1]
5539 + (unsigned)Columns - 1,
5540 screen_row - 1, (int)(Columns - 1));
5541
5542#ifdef FEAT_MBYTE
5543 /* When there is a multi-byte character, just output a
5544 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005545 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5546 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 out_char(' ');
5548 else
5549#endif
5550 out_char(ScreenLines[LineOffset[screen_row - 1]
5551 + (Columns - 1)]);
5552 /* force a redraw of the first char on the next line */
5553 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5554 screen_start(); /* don't know where cursor is now */
5555 }
5556 }
5557
5558 col = 0;
5559 off = (unsigned)(current_ScreenLine - ScreenLines);
5560#ifdef FEAT_RIGHTLEFT
5561 if (wp->w_p_rl)
5562 {
5563 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5564 off += col;
5565 }
5566#endif
5567
5568 /* reset the drawing state for the start of a wrapped line */
5569 draw_state = WL_START;
5570 saved_n_extra = n_extra;
5571 saved_p_extra = p_extra;
5572 saved_c_extra = c_extra;
5573 saved_char_attr = char_attr;
5574 n_extra = 0;
5575 lcs_prec_todo = lcs_prec;
5576#ifdef FEAT_LINEBREAK
5577# ifdef FEAT_DIFF
5578 if (filler_todo <= 0)
5579# endif
5580 need_showbreak = TRUE;
5581#endif
5582#ifdef FEAT_DIFF
5583 --filler_todo;
5584 /* When the filler lines are actually below the last line of the
5585 * file, don't draw the line itself, break here. */
5586 if (filler_todo == 0 && wp->w_botfill)
5587 break;
5588#endif
5589 }
5590
5591 } /* for every character in the line */
5592
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005593#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005594 /* After an empty line check first word for capital. */
5595 if (*skipwhite(line) == NUL)
5596 {
5597 capcol_lnum = lnum + 1;
5598 cap_col = 0;
5599 }
5600#endif
5601
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 return row;
5603}
5604
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005605#ifdef FEAT_MBYTE
5606static int comp_char_differs __ARGS((int, int));
5607
5608/*
5609 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005610 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005611 */
5612 static int
5613comp_char_differs(off_from, off_to)
5614 int off_from;
5615 int off_to;
5616{
5617 int i;
5618
5619 for (i = 0; i < Screen_mco; ++i)
5620 {
5621 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5622 return TRUE;
5623 if (ScreenLinesC[i][off_from] == 0)
5624 break;
5625 }
5626 return FALSE;
5627}
5628#endif
5629
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630/*
5631 * Check whether the given character needs redrawing:
5632 * - the (first byte of the) character is different
5633 * - the attributes are different
5634 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005635 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 */
5637 static int
5638char_needs_redraw(off_from, off_to, cols)
5639 int off_from;
5640 int off_to;
5641 int cols;
5642{
5643 if (cols > 0
5644 && ((ScreenLines[off_from] != ScreenLines[off_to]
5645 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5646
5647#ifdef FEAT_MBYTE
5648 || (enc_dbcs != 0
5649 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5650 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5651 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5652 : (cols > 1 && ScreenLines[off_from + 1]
5653 != ScreenLines[off_to + 1])))
5654 || (enc_utf8
5655 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5656 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005657 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005658 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5659 && ScreenLines[off_from + 1]
5660 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661#endif
5662 ))
5663 return TRUE;
5664 return FALSE;
5665}
5666
5667/*
5668 * Move one "cooked" screen line to the screen, but only the characters that
5669 * have actually changed. Handle insert/delete character.
5670 * "coloff" gives the first column on the screen for this line.
5671 * "endcol" gives the columns where valid characters are.
5672 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5673 * needs to be cleared, negative otherwise.
5674 * "rlflag" is TRUE in a rightleft window:
5675 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5676 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5677 */
5678 static void
5679screen_line(row, coloff, endcol, clear_width
5680#ifdef FEAT_RIGHTLEFT
5681 , rlflag
5682#endif
5683 )
5684 int row;
5685 int coloff;
5686 int endcol;
5687 int clear_width;
5688#ifdef FEAT_RIGHTLEFT
5689 int rlflag;
5690#endif
5691{
5692 unsigned off_from;
5693 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005694#ifdef FEAT_MBYTE
5695 unsigned max_off_from;
5696 unsigned max_off_to;
5697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 int col = 0;
5699#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5700 int hl;
5701#endif
5702 int force = FALSE; /* force update rest of the line */
5703 int redraw_this /* bool: does character need redraw? */
5704#ifdef FEAT_GUI
5705 = TRUE /* For GUI when while-loop empty */
5706#endif
5707 ;
5708 int redraw_next; /* redraw_this for next character */
5709#ifdef FEAT_MBYTE
5710 int clear_next = FALSE;
5711 int char_cells; /* 1: normal char */
5712 /* 2: occupies two display cells */
5713# define CHAR_CELLS char_cells
5714#else
5715# define CHAR_CELLS 1
5716#endif
5717
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005718 /* Check for illegal row and col, just in case. */
5719 if (row >= Rows)
5720 row = Rows - 1;
5721 if (endcol > Columns)
5722 endcol = Columns;
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724# ifdef FEAT_CLIPBOARD
5725 clip_may_clear_selection(row, row);
5726# endif
5727
5728 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5729 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005730#ifdef FEAT_MBYTE
5731 max_off_from = off_from + screen_Columns;
5732 max_off_to = LineOffset[row] + screen_Columns;
5733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735#ifdef FEAT_RIGHTLEFT
5736 if (rlflag)
5737 {
5738 /* Clear rest first, because it's left of the text. */
5739 if (clear_width > 0)
5740 {
5741 while (col <= endcol && ScreenLines[off_to] == ' '
5742 && ScreenAttrs[off_to] == 0
5743# ifdef FEAT_MBYTE
5744 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5745# endif
5746 )
5747 {
5748 ++off_to;
5749 ++col;
5750 }
5751 if (col <= endcol)
5752 screen_fill(row, row + 1, col + coloff,
5753 endcol + coloff + 1, ' ', ' ', 0);
5754 }
5755 col = endcol + 1;
5756 off_to = LineOffset[row] + col + coloff;
5757 off_from += col;
5758 endcol = (clear_width > 0 ? clear_width : -clear_width);
5759 }
5760#endif /* FEAT_RIGHTLEFT */
5761
5762 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5763
5764 while (col < endcol)
5765 {
5766#ifdef FEAT_MBYTE
5767 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005768 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 else
5770 char_cells = 1;
5771#endif
5772
5773 redraw_this = redraw_next;
5774 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5775 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5776
5777#ifdef FEAT_GUI
5778 /* If the next character was bold, then redraw the current character to
5779 * remove any pixels that might have spilt over into us. This only
5780 * happens in the GUI.
5781 */
5782 if (redraw_next && gui.in_use)
5783 {
5784 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005785 if (hl > HL_ALL)
5786 hl = syn_attr2attr(hl);
5787 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 redraw_this = TRUE;
5789 }
5790#endif
5791
5792 if (redraw_this)
5793 {
5794 /*
5795 * Special handling when 'xs' termcap flag set (hpterm):
5796 * Attributes for characters are stored at the position where the
5797 * cursor is when writing the highlighting code. The
5798 * start-highlighting code must be written with the cursor on the
5799 * first highlighted character. The stop-highlighting code must
5800 * be written with the cursor just after the last highlighted
5801 * character.
5802 * Overwriting a character doesn't remove it's highlighting. Need
5803 * to clear the rest of the line, and force redrawing it
5804 * completely.
5805 */
5806 if ( p_wiv
5807 && !force
5808#ifdef FEAT_GUI
5809 && !gui.in_use
5810#endif
5811 && ScreenAttrs[off_to] != 0
5812 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5813 {
5814 /*
5815 * Need to remove highlighting attributes here.
5816 */
5817 windgoto(row, col + coloff);
5818 out_str(T_CE); /* clear rest of this screen line */
5819 screen_start(); /* don't know where cursor is now */
5820 force = TRUE; /* force redraw of rest of the line */
5821 redraw_next = TRUE; /* or else next char would miss out */
5822
5823 /*
5824 * If the previous character was highlighted, need to stop
5825 * highlighting at this character.
5826 */
5827 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5828 {
5829 screen_attr = ScreenAttrs[off_to - 1];
5830 term_windgoto(row, col + coloff);
5831 screen_stop_highlight();
5832 }
5833 else
5834 screen_attr = 0; /* highlighting has stopped */
5835 }
5836#ifdef FEAT_MBYTE
5837 if (enc_dbcs != 0)
5838 {
5839 /* Check if overwriting a double-byte with a single-byte or
5840 * the other way around requires another character to be
5841 * redrawn. For UTF-8 this isn't needed, because comparing
5842 * ScreenLinesUC[] is sufficient. */
5843 if (char_cells == 1
5844 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005845 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
5847 /* Writing a single-cell character over a double-cell
5848 * character: need to redraw the next cell. */
5849 ScreenLines[off_to + 1] = 0;
5850 redraw_next = TRUE;
5851 }
5852 else if (char_cells == 2
5853 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005854 && (*mb_off2cells)(off_to, max_off_to) == 1
5855 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 {
5857 /* Writing the second half of a double-cell character over
5858 * a double-cell character: need to redraw the second
5859 * cell. */
5860 ScreenLines[off_to + 2] = 0;
5861 redraw_next = TRUE;
5862 }
5863
5864 if (enc_dbcs == DBCS_JPNU)
5865 ScreenLines2[off_to] = ScreenLines2[off_from];
5866 }
5867 /* When writing a single-width character over a double-width
5868 * character and at the end of the redrawn text, need to clear out
5869 * the right halve of the old character.
5870 * Also required when writing the right halve of a double-width
5871 * char over the left halve of an existing one. */
5872 if (has_mbyte && col + char_cells == endcol
5873 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005874 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005876 && (*mb_off2cells)(off_to, max_off_to) == 1
5877 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 clear_next = TRUE;
5879#endif
5880
5881 ScreenLines[off_to] = ScreenLines[off_from];
5882#ifdef FEAT_MBYTE
5883 if (enc_utf8)
5884 {
5885 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5886 if (ScreenLinesUC[off_from] != 0)
5887 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005888 int i;
5889
5890 for (i = 0; i < Screen_mco; ++i)
5891 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 }
5893 }
5894 if (char_cells == 2)
5895 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5896#endif
5897
5898#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005899 /* The bold trick makes a single column of pixels appear in the
5900 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 * character should be redrawn too. This happens for our own GUI
5902 * and for some xterms. */
5903 if (
5904# ifdef FEAT_GUI
5905 gui.in_use
5906# endif
5907# if defined(FEAT_GUI) && defined(UNIX)
5908 ||
5909# endif
5910# ifdef UNIX
5911 term_is_xterm
5912# endif
5913 )
5914 {
5915 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005916 if (hl > HL_ALL)
5917 hl = syn_attr2attr(hl);
5918 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 redraw_next = TRUE;
5920 }
5921#endif
5922 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5923#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005924 /* For simplicity set the attributes of second half of a
5925 * double-wide character equal to the first half. */
5926 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005928
5929 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 else
5932#endif
5933 screen_char(off_to, row, col + coloff);
5934 }
5935 else if ( p_wiv
5936#ifdef FEAT_GUI
5937 && !gui.in_use
5938#endif
5939 && col + coloff > 0)
5940 {
5941 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5942 {
5943 /*
5944 * Don't output stop-highlight when moving the cursor, it will
5945 * stop the highlighting when it should continue.
5946 */
5947 screen_attr = 0;
5948 }
5949 else if (screen_attr != 0)
5950 screen_stop_highlight();
5951 }
5952
5953 off_to += CHAR_CELLS;
5954 off_from += CHAR_CELLS;
5955 col += CHAR_CELLS;
5956 }
5957
5958#ifdef FEAT_MBYTE
5959 if (clear_next)
5960 {
5961 /* Clear the second half of a double-wide character of which the left
5962 * half was overwritten with a single-wide character. */
5963 ScreenLines[off_to] = ' ';
5964 if (enc_utf8)
5965 ScreenLinesUC[off_to] = 0;
5966 screen_char(off_to, row, col + coloff);
5967 }
5968#endif
5969
5970 if (clear_width > 0
5971#ifdef FEAT_RIGHTLEFT
5972 && !rlflag
5973#endif
5974 )
5975 {
5976#ifdef FEAT_GUI
5977 int startCol = col;
5978#endif
5979
5980 /* blank out the rest of the line */
5981 while (col < clear_width && ScreenLines[off_to] == ' '
5982 && ScreenAttrs[off_to] == 0
5983#ifdef FEAT_MBYTE
5984 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5985#endif
5986 )
5987 {
5988 ++off_to;
5989 ++col;
5990 }
5991 if (col < clear_width)
5992 {
5993#ifdef FEAT_GUI
5994 /*
5995 * In the GUI, clearing the rest of the line may leave pixels
5996 * behind if the first character cleared was bold. Some bold
5997 * fonts spill over the left. In this case we redraw the previous
5998 * character too. If we didn't skip any blanks above, then we
5999 * only redraw if the character wasn't already redrawn anyway.
6000 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006001 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 {
6003 hl = ScreenAttrs[off_to];
6004 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006005 {
6006 int prev_cells = 1;
6007# ifdef FEAT_MBYTE
6008 if (enc_utf8)
6009 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6010 * that its width is 2. */
6011 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6012 else if (enc_dbcs != 0)
6013 {
6014 /* find previous character by counting from first
6015 * column and get its width. */
6016 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006017 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006018
6019 while (off < off_to)
6020 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006021 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006022 off += prev_cells;
6023 }
6024 }
6025
6026 if (enc_dbcs != 0 && prev_cells > 1)
6027 screen_char_2(off_to - prev_cells, row,
6028 col + coloff - prev_cells);
6029 else
6030# endif
6031 screen_char(off_to - prev_cells, row,
6032 col + coloff - prev_cells);
6033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 }
6035#endif
6036 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6037 ' ', ' ', 0);
6038#ifdef FEAT_VERTSPLIT
6039 off_to += clear_width - col;
6040 col = clear_width;
6041#endif
6042 }
6043 }
6044
6045 if (clear_width > 0)
6046 {
6047#ifdef FEAT_VERTSPLIT
6048 /* For a window that's left of another, draw the separator char. */
6049 if (col + coloff < Columns)
6050 {
6051 int c;
6052
6053 c = fillchar_vsep(&hl);
6054 if (ScreenLines[off_to] != c
6055# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006056 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6057 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058# endif
6059 || ScreenAttrs[off_to] != hl)
6060 {
6061 ScreenLines[off_to] = c;
6062 ScreenAttrs[off_to] = hl;
6063# ifdef FEAT_MBYTE
6064 if (enc_utf8)
6065 {
6066 if (c >= 0x80)
6067 {
6068 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006069 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 }
6071 else
6072 ScreenLinesUC[off_to] = 0;
6073 }
6074# endif
6075 screen_char(off_to, row, col + coloff);
6076 }
6077 }
6078 else
6079#endif
6080 LineWraps[row] = FALSE;
6081 }
6082}
6083
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006084#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006086 * Mirror text "str" for right-left displaying.
6087 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006089 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090rl_mirror(str)
6091 char_u *str;
6092{
6093 char_u *p1, *p2;
6094 int t;
6095
6096 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6097 {
6098 t = *p1;
6099 *p1 = *p2;
6100 *p2 = t;
6101 }
6102}
6103#endif
6104
6105#if defined(FEAT_WINDOWS) || defined(PROTO)
6106/*
6107 * mark all status lines for redraw; used after first :cd
6108 */
6109 void
6110status_redraw_all()
6111{
6112 win_T *wp;
6113
6114 for (wp = firstwin; wp; wp = wp->w_next)
6115 if (wp->w_status_height)
6116 {
6117 wp->w_redr_status = TRUE;
6118 redraw_later(VALID);
6119 }
6120}
6121
6122/*
6123 * mark all status lines of the current buffer for redraw
6124 */
6125 void
6126status_redraw_curbuf()
6127{
6128 win_T *wp;
6129
6130 for (wp = firstwin; wp; wp = wp->w_next)
6131 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6132 {
6133 wp->w_redr_status = TRUE;
6134 redraw_later(VALID);
6135 }
6136}
6137
6138/*
6139 * Redraw all status lines that need to be redrawn.
6140 */
6141 void
6142redraw_statuslines()
6143{
6144 win_T *wp;
6145
6146 for (wp = firstwin; wp; wp = wp->w_next)
6147 if (wp->w_redr_status)
6148 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006149 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006150 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151}
6152#endif
6153
6154#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6155/*
6156 * Redraw all status lines at the bottom of frame "frp".
6157 */
6158 void
6159win_redraw_last_status(frp)
6160 frame_T *frp;
6161{
6162 if (frp->fr_layout == FR_LEAF)
6163 frp->fr_win->w_redr_status = TRUE;
6164 else if (frp->fr_layout == FR_ROW)
6165 {
6166 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6167 win_redraw_last_status(frp);
6168 }
6169 else /* frp->fr_layout == FR_COL */
6170 {
6171 frp = frp->fr_child;
6172 while (frp->fr_next != NULL)
6173 frp = frp->fr_next;
6174 win_redraw_last_status(frp);
6175 }
6176}
6177#endif
6178
6179#ifdef FEAT_VERTSPLIT
6180/*
6181 * Draw the verticap separator right of window "wp" starting with line "row".
6182 */
6183 static void
6184draw_vsep_win(wp, row)
6185 win_T *wp;
6186 int row;
6187{
6188 int hl;
6189 int c;
6190
6191 if (wp->w_vsep_width)
6192 {
6193 /* draw the vertical separator right of this window */
6194 c = fillchar_vsep(&hl);
6195 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6196 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6197 c, ' ', hl);
6198 }
6199}
6200#endif
6201
6202#ifdef FEAT_WILDMENU
6203static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006204static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
6206/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006207 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 */
6209 static int
6210status_match_len(xp, s)
6211 expand_T *xp;
6212 char_u *s;
6213{
6214 int len = 0;
6215
6216#ifdef FEAT_MENU
6217 int emenu = (xp->xp_context == EXPAND_MENUS
6218 || xp->xp_context == EXPAND_MENUNAMES);
6219
6220 /* Check for menu separators - replace with '|'. */
6221 if (emenu && menu_is_separator(s))
6222 return 1;
6223#endif
6224
6225 while (*s != NUL)
6226 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006227 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006228 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006229 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
6231
6232 return len;
6233}
6234
6235/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006236 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006237 * These are backslashes used for escaping. Do show backslashes in help tags.
6238 */
6239 static int
6240skip_status_match_char(xp, s)
6241 expand_T *xp;
6242 char_u *s;
6243{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006244 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006245#ifdef FEAT_MENU
6246 || ((xp->xp_context == EXPAND_MENUS
6247 || xp->xp_context == EXPAND_MENUNAMES)
6248 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6249#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006250 )
6251 {
6252#ifndef BACKSLASH_IN_FILENAME
6253 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6254 return 2;
6255#endif
6256 return 1;
6257 }
6258 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006259}
6260
6261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 * Show wildchar matches in the status line.
6263 * Show at least the "match" item.
6264 * We start at item 'first_match' in the list and show all matches that fit.
6265 *
6266 * If inversion is possible we use it. Else '=' characters are used.
6267 */
6268 void
6269win_redr_status_matches(xp, num_matches, matches, match, showtail)
6270 expand_T *xp;
6271 int num_matches;
6272 char_u **matches; /* list of matches */
6273 int match;
6274 int showtail;
6275{
6276#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6277 int row;
6278 char_u *buf;
6279 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006280 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 int fillchar;
6282 int attr;
6283 int i;
6284 int highlight = TRUE;
6285 char_u *selstart = NULL;
6286 int selstart_col = 0;
6287 char_u *selend = NULL;
6288 static int first_match = 0;
6289 int add_left = FALSE;
6290 char_u *s;
6291#ifdef FEAT_MENU
6292 int emenu;
6293#endif
6294#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6295 int l;
6296#endif
6297
6298 if (matches == NULL) /* interrupted completion? */
6299 return;
6300
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006301#ifdef FEAT_MBYTE
6302 if (has_mbyte)
6303 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6304 else
6305#endif
6306 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (buf == NULL)
6308 return;
6309
6310 if (match == -1) /* don't show match but original text */
6311 {
6312 match = 0;
6313 highlight = FALSE;
6314 }
6315 /* count 1 for the ending ">" */
6316 clen = status_match_len(xp, L_MATCH(match)) + 3;
6317 if (match == 0)
6318 first_match = 0;
6319 else if (match < first_match)
6320 {
6321 /* jumping left, as far as we can go */
6322 first_match = match;
6323 add_left = TRUE;
6324 }
6325 else
6326 {
6327 /* check if match fits on the screen */
6328 for (i = first_match; i < match; ++i)
6329 clen += status_match_len(xp, L_MATCH(i)) + 2;
6330 if (first_match > 0)
6331 clen += 2;
6332 /* jumping right, put match at the left */
6333 if ((long)clen > Columns)
6334 {
6335 first_match = match;
6336 /* if showing the last match, we can add some on the left */
6337 clen = 2;
6338 for (i = match; i < num_matches; ++i)
6339 {
6340 clen += status_match_len(xp, L_MATCH(i)) + 2;
6341 if ((long)clen >= Columns)
6342 break;
6343 }
6344 if (i == num_matches)
6345 add_left = TRUE;
6346 }
6347 }
6348 if (add_left)
6349 while (first_match > 0)
6350 {
6351 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6352 if ((long)clen >= Columns)
6353 break;
6354 --first_match;
6355 }
6356
6357 fillchar = fillchar_status(&attr, TRUE);
6358
6359 if (first_match == 0)
6360 {
6361 *buf = NUL;
6362 len = 0;
6363 }
6364 else
6365 {
6366 STRCPY(buf, "< ");
6367 len = 2;
6368 }
6369 clen = len;
6370
6371 i = first_match;
6372 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6373 {
6374 if (i == match)
6375 {
6376 selstart = buf + len;
6377 selstart_col = clen;
6378 }
6379
6380 s = L_MATCH(i);
6381 /* Check for menu separators - replace with '|' */
6382#ifdef FEAT_MENU
6383 emenu = (xp->xp_context == EXPAND_MENUS
6384 || xp->xp_context == EXPAND_MENUNAMES);
6385 if (emenu && menu_is_separator(s))
6386 {
6387 STRCPY(buf + len, transchar('|'));
6388 l = (int)STRLEN(buf + len);
6389 len += l;
6390 clen += l;
6391 }
6392 else
6393#endif
6394 for ( ; *s != NUL; ++s)
6395 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006396 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 clen += ptr2cells(s);
6398#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006399 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 {
6401 STRNCPY(buf + len, s, l);
6402 s += l - 1;
6403 len += l;
6404 }
6405 else
6406#endif
6407 {
6408 STRCPY(buf + len, transchar_byte(*s));
6409 len += (int)STRLEN(buf + len);
6410 }
6411 }
6412 if (i == match)
6413 selend = buf + len;
6414
6415 *(buf + len++) = ' ';
6416 *(buf + len++) = ' ';
6417 clen += 2;
6418 if (++i == num_matches)
6419 break;
6420 }
6421
6422 if (i != num_matches)
6423 {
6424 *(buf + len++) = '>';
6425 ++clen;
6426 }
6427
6428 buf[len] = NUL;
6429
6430 row = cmdline_row - 1;
6431 if (row >= 0)
6432 {
6433 if (wild_menu_showing == 0)
6434 {
6435 if (msg_scrolled > 0)
6436 {
6437 /* Put the wildmenu just above the command line. If there is
6438 * no room, scroll the screen one line up. */
6439 if (cmdline_row == Rows - 1)
6440 {
6441 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6442 ++msg_scrolled;
6443 }
6444 else
6445 {
6446 ++cmdline_row;
6447 ++row;
6448 }
6449 wild_menu_showing = WM_SCROLLED;
6450 }
6451 else
6452 {
6453 /* Create status line if needed by setting 'laststatus' to 2.
6454 * Set 'winminheight' to zero to avoid that the window is
6455 * resized. */
6456 if (lastwin->w_status_height == 0)
6457 {
6458 save_p_ls = p_ls;
6459 save_p_wmh = p_wmh;
6460 p_ls = 2;
6461 p_wmh = 0;
6462 last_status(FALSE);
6463 }
6464 wild_menu_showing = WM_SHOWN;
6465 }
6466 }
6467
6468 screen_puts(buf, row, 0, attr);
6469 if (selstart != NULL && highlight)
6470 {
6471 *selend = NUL;
6472 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6473 }
6474
6475 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6476 }
6477
6478#ifdef FEAT_VERTSPLIT
6479 win_redraw_last_status(topframe);
6480#else
6481 lastwin->w_redr_status = TRUE;
6482#endif
6483 vim_free(buf);
6484}
6485#endif
6486
6487#if defined(FEAT_WINDOWS) || defined(PROTO)
6488/*
6489 * Redraw the status line of window wp.
6490 *
6491 * If inversion is possible we use it. Else '=' characters are used.
6492 */
6493 void
6494win_redr_status(wp)
6495 win_T *wp;
6496{
6497 int row;
6498 char_u *p;
6499 int len;
6500 int fillchar;
6501 int attr;
6502 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006503 static int busy = FALSE;
6504
6505 /* It's possible to get here recursively when 'statusline' (indirectly)
6506 * invokes ":redrawstatus". Simply ignore the call then. */
6507 if (busy)
6508 return;
6509 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511 wp->w_redr_status = FALSE;
6512 if (wp->w_status_height == 0)
6513 {
6514 /* no status line, can only be last window */
6515 redraw_cmdline = TRUE;
6516 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006517 else if (!redrawing()
6518#ifdef FEAT_INS_EXPAND
6519 /* don't update status line when popup menu is visible and may be
6520 * drawn over it */
6521 || pum_visible()
6522#endif
6523 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
6525 /* Don't redraw right now, do it later. */
6526 wp->w_redr_status = TRUE;
6527 }
6528#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006529 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 {
6531 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006532 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
6534#endif
6535 else
6536 {
6537 fillchar = fillchar_status(&attr, wp == curwin);
6538
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006539 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 p = NameBuff;
6541 len = (int)STRLEN(p);
6542
6543 if (wp->w_buffer->b_help
6544#ifdef FEAT_QUICKFIX
6545 || wp->w_p_pvw
6546#endif
6547 || bufIsChanged(wp->w_buffer)
6548 || wp->w_buffer->b_p_ro)
6549 *(p + len++) = ' ';
6550 if (wp->w_buffer->b_help)
6551 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006552 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 len += (int)STRLEN(p + len);
6554 }
6555#ifdef FEAT_QUICKFIX
6556 if (wp->w_p_pvw)
6557 {
6558 STRCPY(p + len, _("[Preview]"));
6559 len += (int)STRLEN(p + len);
6560 }
6561#endif
6562 if (bufIsChanged(wp->w_buffer))
6563 {
6564 STRCPY(p + len, "[+]");
6565 len += 3;
6566 }
6567 if (wp->w_buffer->b_p_ro)
6568 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006569 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 len += 4;
6571 }
6572
6573#ifndef FEAT_VERTSPLIT
6574 this_ru_col = ru_col;
6575 if (this_ru_col < (Columns + 1) / 2)
6576 this_ru_col = (Columns + 1) / 2;
6577#else
6578 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6579 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6580 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6581 if (this_ru_col <= 1)
6582 {
6583 p = (char_u *)"<"; /* No room for file name! */
6584 len = 1;
6585 }
6586 else
6587#endif
6588#ifdef FEAT_MBYTE
6589 if (has_mbyte)
6590 {
6591 int clen = 0, i;
6592
6593 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006594 clen = mb_string2cells(p, -1);
6595
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 /* Find first character that will fit.
6597 * Going from start to end is much faster for DBCS. */
6598 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006599 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 clen -= (*mb_ptr2cells)(p + i);
6601 len = clen;
6602 if (i > 0)
6603 {
6604 p = p + i - 1;
6605 *p = '<';
6606 ++len;
6607 }
6608
6609 }
6610 else
6611#endif
6612 if (len > this_ru_col - 1)
6613 {
6614 p += len - (this_ru_col - 1);
6615 *p = '<';
6616 len = this_ru_col - 1;
6617 }
6618
6619 row = W_WINROW(wp) + wp->w_height;
6620 screen_puts(p, row, W_WINCOL(wp), attr);
6621 screen_fill(row, row + 1, len + W_WINCOL(wp),
6622 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6623
6624 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6625 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6626 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6627 - 1 + W_WINCOL(wp)), attr);
6628
6629#ifdef FEAT_CMDL_INFO
6630 win_redr_ruler(wp, TRUE);
6631#endif
6632 }
6633
6634#ifdef FEAT_VERTSPLIT
6635 /*
6636 * May need to draw the character below the vertical separator.
6637 */
6638 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6639 {
6640 if (stl_connected(wp))
6641 fillchar = fillchar_status(&attr, wp == curwin);
6642 else
6643 fillchar = fillchar_vsep(&attr);
6644 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6645 attr);
6646 }
6647#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006648 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649}
6650
Bram Moolenaar238a5642006-02-21 22:12:05 +00006651#ifdef FEAT_STL_OPT
6652/*
6653 * Redraw the status line according to 'statusline' and take care of any
6654 * errors encountered.
6655 */
6656 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006657redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006658 win_T *wp;
6659{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006660 static int entered = FALSE;
6661 int save_called_emsg = called_emsg;
6662
6663 /* When called recursively return. This can happen when the statusline
6664 * contains an expression that triggers a redraw. */
6665 if (entered)
6666 return;
6667 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006668
6669 called_emsg = FALSE;
6670 win_redr_custom(wp, FALSE);
6671 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006672 {
6673 /* When there is an error disable the statusline, otherwise the
6674 * display is messed up with errors and a redraw triggers the problem
6675 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006676 set_string_option_direct((char_u *)"statusline", -1,
6677 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006678 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006679 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006680 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006681 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006682}
6683#endif
6684
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685# ifdef FEAT_VERTSPLIT
6686/*
6687 * Return TRUE if the status line of window "wp" is connected to the status
6688 * line of the window right of it. If not, then it's a vertical separator.
6689 * Only call if (wp->w_vsep_width != 0).
6690 */
6691 int
6692stl_connected(wp)
6693 win_T *wp;
6694{
6695 frame_T *fr;
6696
6697 fr = wp->w_frame;
6698 while (fr->fr_parent != NULL)
6699 {
6700 if (fr->fr_parent->fr_layout == FR_COL)
6701 {
6702 if (fr->fr_next != NULL)
6703 break;
6704 }
6705 else
6706 {
6707 if (fr->fr_next != NULL)
6708 return TRUE;
6709 }
6710 fr = fr->fr_parent;
6711 }
6712 return FALSE;
6713}
6714# endif
6715
6716#endif /* FEAT_WINDOWS */
6717
6718#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6719/*
6720 * Get the value to show for the language mappings, active 'keymap'.
6721 */
6722 int
6723get_keymap_str(wp, buf, len)
6724 win_T *wp;
6725 char_u *buf; /* buffer for the result */
6726 int len; /* length of buffer */
6727{
6728 char_u *p;
6729
6730 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6731 return FALSE;
6732
6733 {
6734#ifdef FEAT_EVAL
6735 buf_T *old_curbuf = curbuf;
6736 win_T *old_curwin = curwin;
6737 char_u *s;
6738
6739 curbuf = wp->w_buffer;
6740 curwin = wp;
6741 STRCPY(buf, "b:keymap_name"); /* must be writable */
6742 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006743 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 --emsg_skip;
6745 curbuf = old_curbuf;
6746 curwin = old_curwin;
6747 if (p == NULL || *p == NUL)
6748#endif
6749 {
6750#ifdef FEAT_KEYMAP
6751 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6752 p = wp->w_buffer->b_p_keymap;
6753 else
6754#endif
6755 p = (char_u *)"lang";
6756 }
6757 if ((int)(STRLEN(p) + 3) < len)
6758 sprintf((char *)buf, "<%s>", p);
6759 else
6760 buf[0] = NUL;
6761#ifdef FEAT_EVAL
6762 vim_free(s);
6763#endif
6764 }
6765 return buf[0] != NUL;
6766}
6767#endif
6768
6769#if defined(FEAT_STL_OPT) || defined(PROTO)
6770/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006771 * Redraw the status line or ruler of window "wp".
6772 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 */
6774 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006775win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006777 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006779 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 int attr;
6781 int curattr;
6782 int row;
6783 int col = 0;
6784 int maxwidth;
6785 int width;
6786 int n;
6787 int len;
6788 int fillchar;
6789 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006790 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006792 struct stl_hlrec hltab[STL_MAX_ITEM];
6793 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006794 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006795 win_T *ewp;
6796 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
Bram Moolenaar1d633412013-12-11 15:52:01 +01006798 /* There is a tiny chance that this gets called recursively: When
6799 * redrawing a status line triggers redrawing the ruler or tabline.
6800 * Avoid trouble by not allowing recursion. */
6801 if (entered)
6802 return;
6803 entered = TRUE;
6804
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006806 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006808 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006809 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006810 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006811 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006812 attr = hl_attr(HLF_TPF);
6813 maxwidth = Columns;
6814# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006815 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006818 else
6819 {
6820 row = W_WINROW(wp) + wp->w_height;
6821 fillchar = fillchar_status(&attr, wp == curwin);
6822 maxwidth = W_WIDTH(wp);
6823
6824 if (draw_ruler)
6825 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006826 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006827 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006828 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006829 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006830 if (*++stl == '-')
6831 stl++;
6832 if (atoi((char *)stl))
6833 while (VIM_ISDIGIT(*stl))
6834 stl++;
6835 if (*stl++ != '(')
6836 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006837 }
6838#ifdef FEAT_VERTSPLIT
6839 col = ru_col - (Columns - W_WIDTH(wp));
6840 if (col < (W_WIDTH(wp) + 1) / 2)
6841 col = (W_WIDTH(wp) + 1) / 2;
6842#else
6843 col = ru_col;
6844 if (col > (Columns + 1) / 2)
6845 col = (Columns + 1) / 2;
6846#endif
6847 maxwidth = W_WIDTH(wp) - col;
6848#ifdef FEAT_WINDOWS
6849 if (!wp->w_status_height)
6850#endif
6851 {
6852 row = Rows - 1;
6853 --maxwidth; /* writing in last column may cause scrolling */
6854 fillchar = ' ';
6855 attr = 0;
6856 }
6857
6858# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006859 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006860# endif
6861 }
6862 else
6863 {
6864 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006865 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006866 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006867 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006868# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006869 use_sandbox = was_set_insecurely((char_u *)"statusline",
6870 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006871# endif
6872 }
6873
6874#ifdef FEAT_VERTSPLIT
6875 col += W_WINCOL(wp);
6876#endif
6877 }
6878
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006880 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881
Bram Moolenaar61452852011-02-01 18:01:11 +01006882 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6883 * the cursor away and back. */
6884 ewp = wp == NULL ? curwin : wp;
6885 p_crb_save = ewp->w_p_crb;
6886 ewp->w_p_crb = FALSE;
6887
Bram Moolenaar362f3562009-11-03 16:20:34 +00006888 /* Make a copy, because the statusline may include a function call that
6889 * might change the option value and free the memory. */
6890 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006891 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006892 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006893 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006894 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006895 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006897 /* Make all characters printable. */
6898 p = transstr(buf);
6899 if (p != NULL)
6900 {
6901 vim_strncpy(buf, p, sizeof(buf) - 1);
6902 vim_free(p);
6903 }
6904
6905 /* fill up with "fillchar" */
6906 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006907 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {
6909#ifdef FEAT_MBYTE
6910 len += (*mb_char2bytes)(fillchar, buf + len);
6911#else
6912 buf[len++] = fillchar;
6913#endif
6914 ++width;
6915 }
6916 buf[len] = NUL;
6917
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006918 /*
6919 * Draw each snippet with the specified highlighting.
6920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 curattr = attr;
6922 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006923 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006925 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 screen_puts_len(p, len, row, col, curattr);
6927 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006928 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006930 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006932 else if (hltab[n].userhl < 0)
6933 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006935 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006936 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937#endif
6938 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006939 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006942
6943 if (wp == NULL)
6944 {
6945 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6946 col = 0;
6947 len = 0;
6948 p = buf;
6949 fillchar = 0;
6950 for (n = 0; tabtab[n].start != NULL; n++)
6951 {
6952 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6953 while (col < len)
6954 TabPageIdxs[col++] = fillchar;
6955 p = tabtab[n].start;
6956 fillchar = tabtab[n].userhl;
6957 }
6958 while (col < Columns)
6959 TabPageIdxs[col++] = fillchar;
6960 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006961
6962theend:
6963 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964}
6965
6966#endif /* FEAT_STL_OPT */
6967
6968/*
6969 * Output a single character directly to the screen and update ScreenLines.
6970 */
6971 void
6972screen_putchar(c, row, col, attr)
6973 int c;
6974 int row, col;
6975 int attr;
6976{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 char_u buf[MB_MAXBYTES + 1];
6978
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006979#ifdef FEAT_MBYTE
6980 if (has_mbyte)
6981 buf[(*mb_char2bytes)(c, buf)] = NUL;
6982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006984 {
6985 buf[0] = c;
6986 buf[1] = NUL;
6987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 screen_puts(buf, row, col, attr);
6989}
6990
6991/*
6992 * Get a single character directly from ScreenLines into "bytes[]".
6993 * Also return its attribute in *attrp;
6994 */
6995 void
6996screen_getbytes(row, col, bytes, attrp)
6997 int row, col;
6998 char_u *bytes;
6999 int *attrp;
7000{
7001 unsigned off;
7002
7003 /* safety check */
7004 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7005 {
7006 off = LineOffset[row] + col;
7007 *attrp = ScreenAttrs[off];
7008 bytes[0] = ScreenLines[off];
7009 bytes[1] = NUL;
7010
7011#ifdef FEAT_MBYTE
7012 if (enc_utf8 && ScreenLinesUC[off] != 0)
7013 bytes[utfc_char2bytes(off, bytes)] = NUL;
7014 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7015 {
7016 bytes[0] = ScreenLines[off];
7017 bytes[1] = ScreenLines2[off];
7018 bytes[2] = NUL;
7019 }
7020 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7021 {
7022 bytes[1] = ScreenLines[off + 1];
7023 bytes[2] = NUL;
7024 }
7025#endif
7026 }
7027}
7028
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007029#ifdef FEAT_MBYTE
7030static int screen_comp_differs __ARGS((int, int*));
7031
7032/*
7033 * Return TRUE if composing characters for screen posn "off" differs from
7034 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007035 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007036 */
7037 static int
7038screen_comp_differs(off, u8cc)
7039 int off;
7040 int *u8cc;
7041{
7042 int i;
7043
7044 for (i = 0; i < Screen_mco; ++i)
7045 {
7046 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7047 return TRUE;
7048 if (u8cc[i] == 0)
7049 break;
7050 }
7051 return FALSE;
7052}
7053#endif
7054
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055/*
7056 * Put string '*text' on the screen at position 'row' and 'col', with
7057 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7058 * Note: only outputs within one row, message is truncated at screen boundary!
7059 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7060 */
7061 void
7062screen_puts(text, row, col, attr)
7063 char_u *text;
7064 int row;
7065 int col;
7066 int attr;
7067{
7068 screen_puts_len(text, -1, row, col, attr);
7069}
7070
7071/*
7072 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7073 * a NUL.
7074 */
7075 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007076screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007078 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 int row;
7080 int col;
7081 int attr;
7082{
7083 unsigned off;
7084 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007085 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 int c;
7087#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007088 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 int mbyte_blen = 1;
7090 int mbyte_cells = 1;
7091 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007092 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 int clear_next_cell = FALSE;
7094# ifdef FEAT_ARABIC
7095 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007096 int pc, nc, nc1;
7097 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098# endif
7099#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007100#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7101 int force_redraw_this;
7102 int force_redraw_next = FALSE;
7103#endif
7104 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
7106 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7107 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007108 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
Bram Moolenaarc236c162008-07-13 17:41:49 +00007110#ifdef FEAT_MBYTE
7111 /* When drawing over the right halve of a double-wide char clear out the
7112 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007113 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007114# ifdef FEAT_GUI
7115 && !gui.in_use
7116# endif
7117 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007118 {
7119 ScreenLines[off - 1] = ' ';
7120 ScreenAttrs[off - 1] = 0;
7121 if (enc_utf8)
7122 {
7123 ScreenLinesUC[off - 1] = 0;
7124 ScreenLinesC[0][off - 1] = 0;
7125 }
7126 /* redraw the previous cell, make it empty */
7127 screen_char(off - 1, row, col - 1);
7128 /* force the cell at "col" to be redrawn */
7129 force_redraw_next = TRUE;
7130 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007131#endif
7132
Bram Moolenaar367329b2007-08-30 11:53:22 +00007133#ifdef FEAT_MBYTE
7134 max_off = LineOffset[row] + screen_Columns;
7135#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007136 while (col < screen_Columns
7137 && (len < 0 || (int)(ptr - text) < len)
7138 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
7140 c = *ptr;
7141#ifdef FEAT_MBYTE
7142 /* check if this is the first byte of a multibyte */
7143 if (has_mbyte)
7144 {
7145 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007146 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007148 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7150 mbyte_cells = 1;
7151 else if (enc_dbcs != 0)
7152 mbyte_cells = mbyte_blen;
7153 else /* enc_utf8 */
7154 {
7155 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007156 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 (int)((text + len) - ptr));
7158 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007159 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007161# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 /* Non-BMP character: display as ? or fullwidth ?. */
7163 if (u8c >= 0x10000)
7164 {
7165 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7166 if (attr == 0)
7167 attr = hl_attr(HLF_8);
7168 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170# ifdef FEAT_ARABIC
7171 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7172 {
7173 /* Do Arabic shaping. */
7174 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7175 {
7176 /* Past end of string to be displayed. */
7177 nc = NUL;
7178 nc1 = NUL;
7179 }
7180 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007181 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007182 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7183 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007184 nc1 = pcc[0];
7185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 pc = prev_c;
7187 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007188 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 }
7190 else
7191 prev_c = u8c;
7192# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007193 if (col + mbyte_cells > screen_Columns)
7194 {
7195 /* Only 1 cell left, but character requires 2 cells:
7196 * display a '>' in the last column to avoid wrapping. */
7197 c = '>';
7198 mbyte_cells = 1;
7199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
7201 }
7202#endif
7203
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007204#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7205 force_redraw_this = force_redraw_next;
7206 force_redraw_next = FALSE;
7207#endif
7208
7209 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210#ifdef FEAT_MBYTE
7211 || (mbyte_cells == 2
7212 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7213 || (enc_dbcs == DBCS_JPNU
7214 && c == 0x8e
7215 && ScreenLines2[off] != ptr[1])
7216 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007217 && (ScreenLinesUC[off] !=
7218 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7219 || (ScreenLinesUC[off] != 0
7220 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221#endif
7222 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007223 || exmode_active;
7224
7225 if (need_redraw
7226#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7227 || force_redraw_this
7228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 )
7230 {
7231#if defined(FEAT_GUI) || defined(UNIX)
7232 /* The bold trick makes a single row of pixels appear in the next
7233 * character. When a bold character is removed, the next
7234 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007235 * and for some xterms. */
7236 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237# ifdef FEAT_GUI
7238 gui.in_use
7239# endif
7240# if defined(FEAT_GUI) && defined(UNIX)
7241 ||
7242# endif
7243# ifdef UNIX
7244 term_is_xterm
7245# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007246 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007248 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007250 if (n > HL_ALL)
7251 n = syn_attr2attr(n);
7252 if (n & HL_BOLD)
7253 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
7255#endif
7256#ifdef FEAT_MBYTE
7257 /* When at the end of the text and overwriting a two-cell
7258 * character with a one-cell character, need to clear the next
7259 * cell. Also when overwriting the left halve of a two-cell char
7260 * with the right halve of a two-cell char. Do this only once
7261 * (mb_off2cells() may return 2 on the right halve). */
7262 if (clear_next_cell)
7263 clear_next_cell = FALSE;
7264 else if (has_mbyte
7265 && (len < 0 ? ptr[mbyte_blen] == NUL
7266 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007267 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007269 && (*mb_off2cells)(off, max_off) == 1
7270 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 clear_next_cell = TRUE;
7272
7273 /* Make sure we never leave a second byte of a double-byte behind,
7274 * it confuses mb_off2cells(). */
7275 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007276 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007278 && (*mb_off2cells)(off, max_off) == 1
7279 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 ScreenLines[off + mbyte_blen] = 0;
7281#endif
7282 ScreenLines[off] = c;
7283 ScreenAttrs[off] = attr;
7284#ifdef FEAT_MBYTE
7285 if (enc_utf8)
7286 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007287 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 ScreenLinesUC[off] = 0;
7289 else
7290 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007291 int i;
7292
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007294 for (i = 0; i < Screen_mco; ++i)
7295 {
7296 ScreenLinesC[i][off] = u8cc[i];
7297 if (u8cc[i] == 0)
7298 break;
7299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 }
7301 if (mbyte_cells == 2)
7302 {
7303 ScreenLines[off + 1] = 0;
7304 ScreenAttrs[off + 1] = attr;
7305 }
7306 screen_char(off, row, col);
7307 }
7308 else if (mbyte_cells == 2)
7309 {
7310 ScreenLines[off + 1] = ptr[1];
7311 ScreenAttrs[off + 1] = attr;
7312 screen_char_2(off, row, col);
7313 }
7314 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7315 {
7316 ScreenLines2[off] = ptr[1];
7317 screen_char(off, row, col);
7318 }
7319 else
7320#endif
7321 screen_char(off, row, col);
7322 }
7323#ifdef FEAT_MBYTE
7324 if (has_mbyte)
7325 {
7326 off += mbyte_cells;
7327 col += mbyte_cells;
7328 ptr += mbyte_blen;
7329 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007330 {
7331 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007333 len = -1;
7334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 }
7336 else
7337#endif
7338 {
7339 ++off;
7340 ++col;
7341 ++ptr;
7342 }
7343 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007344
7345#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7346 /* If we detected the next character needs to be redrawn, but the text
7347 * doesn't extend up to there, update the character here. */
7348 if (force_redraw_next && col < screen_Columns)
7349 {
7350# ifdef FEAT_MBYTE
7351 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7352 screen_char_2(off, row, col);
7353 else
7354# endif
7355 screen_char(off, row, col);
7356 }
7357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358}
7359
7360#ifdef FEAT_SEARCH_EXTRA
7361/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007362 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 */
7364 static void
7365start_search_hl()
7366{
7367 if (p_hls && !no_hlsearch)
7368 {
7369 last_pat_prog(&search_hl.rm);
7370 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007371# ifdef FEAT_RELTIME
7372 /* Set the time limit to 'redrawtime'. */
7373 profile_setlimit(p_rdt, &search_hl.tm);
7374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 }
7376}
7377
7378/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007379 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 */
7381 static void
7382end_search_hl()
7383{
7384 if (search_hl.rm.regprog != NULL)
7385 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007386 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 search_hl.rm.regprog = NULL;
7388 }
7389}
7390
7391/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007392 * Init for calling prepare_search_hl().
7393 */
7394 static void
7395init_search_hl(wp)
7396 win_T *wp;
7397{
7398 matchitem_T *cur;
7399
7400 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7401 * match */
7402 cur = wp->w_match_head;
7403 while (cur != NULL)
7404 {
7405 cur->hl.rm = cur->match;
7406 if (cur->hlg_id == 0)
7407 cur->hl.attr = 0;
7408 else
7409 cur->hl.attr = syn_id2attr(cur->hlg_id);
7410 cur->hl.buf = wp->w_buffer;
7411 cur->hl.lnum = 0;
7412 cur->hl.first_lnum = 0;
7413# ifdef FEAT_RELTIME
7414 /* Set the time limit to 'redrawtime'. */
7415 profile_setlimit(p_rdt, &(cur->hl.tm));
7416# endif
7417 cur = cur->next;
7418 }
7419 search_hl.buf = wp->w_buffer;
7420 search_hl.lnum = 0;
7421 search_hl.first_lnum = 0;
7422 /* time limit is set at the toplevel, for all windows */
7423}
7424
7425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 * Advance to the match in window "wp" line "lnum" or past it.
7427 */
7428 static void
7429prepare_search_hl(wp, lnum)
7430 win_T *wp;
7431 linenr_T lnum;
7432{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007433 matchitem_T *cur; /* points to the match list */
7434 match_T *shl; /* points to search_hl or a match */
7435 int shl_flag; /* flag to indicate whether search_hl
7436 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007437 int pos_inprogress; /* marks that position match search is
7438 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 int n;
7440
7441 /*
7442 * When using a multi-line pattern, start searching at the top
7443 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007444 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007446 cur = wp->w_match_head;
7447 shl_flag = FALSE;
7448 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007450 if (shl_flag == FALSE)
7451 {
7452 shl = &search_hl;
7453 shl_flag = TRUE;
7454 }
7455 else
7456 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 if (shl->rm.regprog != NULL
7458 && shl->lnum == 0
7459 && re_multiline(shl->rm.regprog))
7460 {
7461 if (shl->first_lnum == 0)
7462 {
7463# ifdef FEAT_FOLDING
7464 for (shl->first_lnum = lnum;
7465 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7466 if (hasFoldingWin(wp, shl->first_lnum - 1,
7467 NULL, NULL, TRUE, NULL))
7468 break;
7469# else
7470 shl->first_lnum = wp->w_topline;
7471# endif
7472 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007473 if (cur != NULL)
7474 cur->pos.cur = 0;
7475 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007477 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7478 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007480 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7481 pos_inprogress = cur == NULL || cur->pos.cur == 0
7482 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if (shl->lnum != 0)
7484 {
7485 shl->first_lnum = shl->lnum
7486 + shl->rm.endpos[0].lnum
7487 - shl->rm.startpos[0].lnum;
7488 n = shl->rm.endpos[0].col;
7489 }
7490 else
7491 {
7492 ++shl->first_lnum;
7493 n = 0;
7494 }
7495 }
7496 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007497 if (shl != &search_hl && cur != NULL)
7498 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 }
7500}
7501
7502/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007503 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 * Uses shl->buf.
7505 * Sets shl->lnum and shl->rm contents.
7506 * Note: Assumes a previous match is always before "lnum", unless
7507 * shl->lnum is zero.
7508 * Careful: Any pointers for buffer lines will become invalid.
7509 */
7510 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007511next_search_hl(win, shl, lnum, mincol, cur)
7512 win_T *win;
7513 match_T *shl; /* points to search_hl or a match */
7514 linenr_T lnum;
7515 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007516 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517{
7518 linenr_T l;
7519 colnr_T matchcol;
7520 long nmatched;
7521
7522 if (shl->lnum != 0)
7523 {
7524 /* Check for three situations:
7525 * 1. If the "lnum" is below a previous match, start a new search.
7526 * 2. If the previous match includes "mincol", use it.
7527 * 3. Continue after the previous match.
7528 */
7529 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7530 if (lnum > l)
7531 shl->lnum = 0;
7532 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7533 return;
7534 }
7535
7536 /*
7537 * Repeat searching for a match until one is found that includes "mincol"
7538 * or none is found in this line.
7539 */
7540 called_emsg = FALSE;
7541 for (;;)
7542 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007543#ifdef FEAT_RELTIME
7544 /* Stop searching after passing the time limit. */
7545 if (profile_passed_limit(&(shl->tm)))
7546 {
7547 shl->lnum = 0; /* no match found in time */
7548 break;
7549 }
7550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 /* Three situations:
7552 * 1. No useful previous match: search from start of line.
7553 * 2. Not Vi compatible or empty match: continue at next character.
7554 * Break the loop if this is beyond the end of the line.
7555 * 3. Vi compatible searching: continue at end of previous match.
7556 */
7557 if (shl->lnum == 0)
7558 matchcol = 0;
7559 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7560 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007561 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007563 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007564
7565 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007566 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007567 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007569 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 shl->lnum = 0;
7571 break;
7572 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007573#ifdef FEAT_MBYTE
7574 if (has_mbyte)
7575 matchcol += mb_ptr2len(ml);
7576 else
7577#endif
7578 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 }
7580 else
7581 matchcol = shl->rm.endpos[0].col;
7582
7583 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007584 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007586 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7587 matchcol,
7588#ifdef FEAT_RELTIME
7589 &(shl->tm)
7590#else
7591 NULL
7592#endif
7593 );
7594 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007595 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007596 /* Error while handling regexp: stop using this regexp. */
7597 if (shl == &search_hl)
7598 {
7599 /* don't free regprog in the match list, it's a copy */
7600 vim_regfree(shl->rm.regprog);
7601 SET_NO_HLSEARCH(TRUE);
7602 }
7603 shl->rm.regprog = NULL;
7604 shl->lnum = 0;
7605 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7606 message */
7607 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007608 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007609 }
7610 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007611 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007612 else
7613 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 if (nmatched == 0)
7615 {
7616 shl->lnum = 0; /* no match found */
7617 break;
7618 }
7619 if (shl->rm.startpos[0].lnum > 0
7620 || shl->rm.startpos[0].col >= mincol
7621 || nmatched > 1
7622 || shl->rm.endpos[0].col > mincol)
7623 {
7624 shl->lnum += shl->rm.startpos[0].lnum;
7625 break; /* useful match found */
7626 }
7627 }
7628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
Bram Moolenaarb3414592014-06-17 17:48:32 +02007630 static int
7631next_search_hl_pos(shl, lnum, posmatch, mincol)
7632 match_T *shl; /* points to a match */
7633 linenr_T lnum;
7634 posmatch_T *posmatch; /* match positions */
7635 colnr_T mincol; /* minimal column for a match */
7636{
7637 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007638 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007639
7640 shl->lnum = 0;
7641 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7642 {
7643 if (posmatch->pos[i].lnum == 0)
7644 break;
7645 if (posmatch->pos[i].col < mincol)
7646 continue;
7647 if (posmatch->pos[i].lnum == lnum)
7648 {
7649 if (shl->lnum == lnum)
7650 {
7651 /* partially sort positions by column numbers
7652 * on the same line */
7653 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7654 {
7655 llpos_T tmp = posmatch->pos[i];
7656
7657 posmatch->pos[i] = posmatch->pos[bot];
7658 posmatch->pos[bot] = tmp;
7659 }
7660 }
7661 else
7662 {
7663 bot = i;
7664 shl->lnum = lnum;
7665 }
7666 }
7667 }
7668 posmatch->cur = 0;
7669 if (shl->lnum == lnum)
7670 {
7671 colnr_T start = posmatch->pos[bot].col == 0
7672 ? 0 : posmatch->pos[bot].col - 1;
7673 colnr_T end = posmatch->pos[bot].col == 0
7674 ? MAXCOL : start + posmatch->pos[bot].len;
7675
7676 shl->rm.startpos[0].lnum = 0;
7677 shl->rm.startpos[0].col = start;
7678 shl->rm.endpos[0].lnum = 0;
7679 shl->rm.endpos[0].col = end;
7680 posmatch->cur = bot + 1;
7681 return TRUE;
7682 }
7683 return FALSE;
7684}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007685#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007686
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 static void
7688screen_start_highlight(attr)
7689 int attr;
7690{
7691 attrentry_T *aep = NULL;
7692
7693 screen_attr = attr;
7694 if (full_screen
7695#ifdef WIN3264
7696 && termcap_active
7697#endif
7698 )
7699 {
7700#ifdef FEAT_GUI
7701 if (gui.in_use)
7702 {
7703 char buf[20];
7704
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007705 /* The GUI handles this internally. */
7706 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 OUT_STR(buf);
7708 }
7709 else
7710#endif
7711 {
7712 if (attr > HL_ALL) /* special HL attr. */
7713 {
7714 if (t_colors > 1)
7715 aep = syn_cterm_attr2entry(attr);
7716 else
7717 aep = syn_term_attr2entry(attr);
7718 if (aep == NULL) /* did ":syntax clear" */
7719 attr = 0;
7720 else
7721 attr = aep->ae_attr;
7722 }
7723 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7724 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007725 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7726 && cterm_normal_fg_bold)
7727 /* If the Normal FG color has BOLD attribute and the new HL
7728 * has a FG color defined, clear BOLD. */
7729 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7731 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007732 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7733 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 out_str(T_US);
7735 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7736 out_str(T_CZH);
7737 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7738 out_str(T_MR);
7739
7740 /*
7741 * Output the color or start string after bold etc., in case the
7742 * bold etc. override the color setting.
7743 */
7744 if (aep != NULL)
7745 {
7746 if (t_colors > 1)
7747 {
7748 if (aep->ae_u.cterm.fg_color)
7749 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7750 if (aep->ae_u.cterm.bg_color)
7751 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7752 }
7753 else
7754 {
7755 if (aep->ae_u.term.start != NULL)
7756 out_str(aep->ae_u.term.start);
7757 }
7758 }
7759 }
7760 }
7761}
7762
7763 void
7764screen_stop_highlight()
7765{
7766 int do_ME = FALSE; /* output T_ME code */
7767
7768 if (screen_attr != 0
7769#ifdef WIN3264
7770 && termcap_active
7771#endif
7772 )
7773 {
7774#ifdef FEAT_GUI
7775 if (gui.in_use)
7776 {
7777 char buf[20];
7778
7779 /* use internal GUI code */
7780 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7781 OUT_STR(buf);
7782 }
7783 else
7784#endif
7785 {
7786 if (screen_attr > HL_ALL) /* special HL attr. */
7787 {
7788 attrentry_T *aep;
7789
7790 if (t_colors > 1)
7791 {
7792 /*
7793 * Assume that t_me restores the original colors!
7794 */
7795 aep = syn_cterm_attr2entry(screen_attr);
7796 if (aep != NULL && (aep->ae_u.cterm.fg_color
7797 || aep->ae_u.cterm.bg_color))
7798 do_ME = TRUE;
7799 }
7800 else
7801 {
7802 aep = syn_term_attr2entry(screen_attr);
7803 if (aep != NULL && aep->ae_u.term.stop != NULL)
7804 {
7805 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7806 do_ME = TRUE;
7807 else
7808 out_str(aep->ae_u.term.stop);
7809 }
7810 }
7811 if (aep == NULL) /* did ":syntax clear" */
7812 screen_attr = 0;
7813 else
7814 screen_attr = aep->ae_attr;
7815 }
7816
7817 /*
7818 * Often all ending-codes are equal to T_ME. Avoid outputting the
7819 * same sequence several times.
7820 */
7821 if (screen_attr & HL_STANDOUT)
7822 {
7823 if (STRCMP(T_SE, T_ME) == 0)
7824 do_ME = TRUE;
7825 else
7826 out_str(T_SE);
7827 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007828 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {
7830 if (STRCMP(T_UE, T_ME) == 0)
7831 do_ME = TRUE;
7832 else
7833 out_str(T_UE);
7834 }
7835 if (screen_attr & HL_ITALIC)
7836 {
7837 if (STRCMP(T_CZR, T_ME) == 0)
7838 do_ME = TRUE;
7839 else
7840 out_str(T_CZR);
7841 }
7842 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7843 out_str(T_ME);
7844
7845 if (t_colors > 1)
7846 {
7847 /* set Normal cterm colors */
7848 if (cterm_normal_fg_color != 0)
7849 term_fg_color(cterm_normal_fg_color - 1);
7850 if (cterm_normal_bg_color != 0)
7851 term_bg_color(cterm_normal_bg_color - 1);
7852 if (cterm_normal_fg_bold)
7853 out_str(T_MD);
7854 }
7855 }
7856 }
7857 screen_attr = 0;
7858}
7859
7860/*
7861 * Reset the colors for a cterm. Used when leaving Vim.
7862 * The machine specific code may override this again.
7863 */
7864 void
7865reset_cterm_colors()
7866{
7867 if (t_colors > 1)
7868 {
7869 /* set Normal cterm colors */
7870 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7871 {
7872 out_str(T_OP);
7873 screen_attr = -1;
7874 }
7875 if (cterm_normal_fg_bold)
7876 {
7877 out_str(T_ME);
7878 screen_attr = -1;
7879 }
7880 }
7881}
7882
7883/*
7884 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7885 * using the attributes from ScreenAttrs["off"].
7886 */
7887 static void
7888screen_char(off, row, col)
7889 unsigned off;
7890 int row;
7891 int col;
7892{
7893 int attr;
7894
7895 /* Check for illegal values, just in case (could happen just after
7896 * resizing). */
7897 if (row >= screen_Rows || col >= screen_Columns)
7898 return;
7899
7900 /* Outputting the last character on the screen may scrollup the screen.
7901 * Don't to it! Mark the character invalid (update it when scrolled up) */
7902 if (row == screen_Rows - 1 && col == screen_Columns - 1
7903#ifdef FEAT_RIGHTLEFT
7904 /* account for first command-line character in rightleft mode */
7905 && !cmdmsg_rl
7906#endif
7907 )
7908 {
7909 ScreenAttrs[off] = (sattr_T)-1;
7910 return;
7911 }
7912
7913 /*
7914 * Stop highlighting first, so it's easier to move the cursor.
7915 */
7916#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7917 if (screen_char_attr != 0)
7918 attr = screen_char_attr;
7919 else
7920#endif
7921 attr = ScreenAttrs[off];
7922 if (screen_attr != attr)
7923 screen_stop_highlight();
7924
7925 windgoto(row, col);
7926
7927 if (screen_attr != attr)
7928 screen_start_highlight(attr);
7929
7930#ifdef FEAT_MBYTE
7931 if (enc_utf8 && ScreenLinesUC[off] != 0)
7932 {
7933 char_u buf[MB_MAXBYTES + 1];
7934
7935 /* Convert UTF-8 character to bytes and write it. */
7936
7937 buf[utfc_char2bytes(off, buf)] = NUL;
7938
7939 out_str(buf);
7940 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7941 ++screen_cur_col;
7942 }
7943 else
7944#endif
7945 {
7946#ifdef FEAT_MBYTE
7947 out_flush_check();
7948#endif
7949 out_char(ScreenLines[off]);
7950#ifdef FEAT_MBYTE
7951 /* double-byte character in single-width cell */
7952 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7953 out_char(ScreenLines2[off]);
7954#endif
7955 }
7956
7957 screen_cur_col++;
7958}
7959
7960#ifdef FEAT_MBYTE
7961
7962/*
7963 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7964 * on the screen at position 'row' and 'col'.
7965 * The attributes of the first byte is used for all. This is required to
7966 * output the two bytes of a double-byte character with nothing in between.
7967 */
7968 static void
7969screen_char_2(off, row, col)
7970 unsigned off;
7971 int row;
7972 int col;
7973{
7974 /* Check for illegal values (could be wrong when screen was resized). */
7975 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7976 return;
7977
7978 /* Outputting the last character on the screen may scrollup the screen.
7979 * Don't to it! Mark the character invalid (update it when scrolled up) */
7980 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7981 {
7982 ScreenAttrs[off] = (sattr_T)-1;
7983 return;
7984 }
7985
7986 /* Output the first byte normally (positions the cursor), then write the
7987 * second byte directly. */
7988 screen_char(off, row, col);
7989 out_char(ScreenLines[off + 1]);
7990 ++screen_cur_col;
7991}
7992#endif
7993
7994#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7995/*
7996 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7997 * This uses the contents of ScreenLines[] and doesn't change it.
7998 */
7999 void
8000screen_draw_rectangle(row, col, height, width, invert)
8001 int row;
8002 int col;
8003 int height;
8004 int width;
8005 int invert;
8006{
8007 int r, c;
8008 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008009#ifdef FEAT_MBYTE
8010 int max_off;
8011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008013 /* Can't use ScreenLines unless initialized */
8014 if (ScreenLines == NULL)
8015 return;
8016
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (invert)
8018 screen_char_attr = HL_INVERSE;
8019 for (r = row; r < row + height; ++r)
8020 {
8021 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008022#ifdef FEAT_MBYTE
8023 max_off = off + screen_Columns;
8024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 for (c = col; c < col + width; ++c)
8026 {
8027#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008028 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {
8030 screen_char_2(off + c, r, c);
8031 ++c;
8032 }
8033 else
8034#endif
8035 {
8036 screen_char(off + c, r, c);
8037#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008038 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 ++c;
8040#endif
8041 }
8042 }
8043 }
8044 screen_char_attr = 0;
8045}
8046#endif
8047
8048#ifdef FEAT_VERTSPLIT
8049/*
8050 * Redraw the characters for a vertically split window.
8051 */
8052 static void
8053redraw_block(row, end, wp)
8054 int row;
8055 int end;
8056 win_T *wp;
8057{
8058 int col;
8059 int width;
8060
8061# ifdef FEAT_CLIPBOARD
8062 clip_may_clear_selection(row, end - 1);
8063# endif
8064
8065 if (wp == NULL)
8066 {
8067 col = 0;
8068 width = Columns;
8069 }
8070 else
8071 {
8072 col = wp->w_wincol;
8073 width = wp->w_width;
8074 }
8075 screen_draw_rectangle(row, col, end - row, width, FALSE);
8076}
8077#endif
8078
8079/*
8080 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8081 * with character 'c1' in first column followed by 'c2' in the other columns.
8082 * Use attributes 'attr'.
8083 */
8084 void
8085screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8086 int start_row, end_row;
8087 int start_col, end_col;
8088 int c1, c2;
8089 int attr;
8090{
8091 int row;
8092 int col;
8093 int off;
8094 int end_off;
8095 int did_delete;
8096 int c;
8097 int norm_term;
8098#if defined(FEAT_GUI) || defined(UNIX)
8099 int force_next = FALSE;
8100#endif
8101
8102 if (end_row > screen_Rows) /* safety check */
8103 end_row = screen_Rows;
8104 if (end_col > screen_Columns) /* safety check */
8105 end_col = screen_Columns;
8106 if (ScreenLines == NULL
8107 || start_row >= end_row
8108 || start_col >= end_col) /* nothing to do */
8109 return;
8110
8111 /* it's a "normal" terminal when not in a GUI or cterm */
8112 norm_term = (
8113#ifdef FEAT_GUI
8114 !gui.in_use &&
8115#endif
8116 t_colors <= 1);
8117 for (row = start_row; row < end_row; ++row)
8118 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008119#ifdef FEAT_MBYTE
8120 if (has_mbyte
8121# ifdef FEAT_GUI
8122 && !gui.in_use
8123# endif
8124 )
8125 {
8126 /* When drawing over the right halve of a double-wide char clear
8127 * out the left halve. When drawing over the left halve of a
8128 * double wide-char clear out the right halve. Only needed in a
8129 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008130 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008131 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008132 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008133 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008134 }
8135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 /*
8137 * Try to use delete-line termcap code, when no attributes or in a
8138 * "normal" terminal, where a bold/italic space is just a
8139 * space.
8140 */
8141 did_delete = FALSE;
8142 if (c2 == ' '
8143 && end_col == Columns
8144 && can_clear(T_CE)
8145 && (attr == 0
8146 || (norm_term
8147 && attr <= HL_ALL
8148 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8149 {
8150 /*
8151 * check if we really need to clear something
8152 */
8153 col = start_col;
8154 if (c1 != ' ') /* don't clear first char */
8155 ++col;
8156
8157 off = LineOffset[row] + col;
8158 end_off = LineOffset[row] + end_col;
8159
8160 /* skip blanks (used often, keep it fast!) */
8161#ifdef FEAT_MBYTE
8162 if (enc_utf8)
8163 while (off < end_off && ScreenLines[off] == ' '
8164 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8165 ++off;
8166 else
8167#endif
8168 while (off < end_off && ScreenLines[off] == ' '
8169 && ScreenAttrs[off] == 0)
8170 ++off;
8171 if (off < end_off) /* something to be cleared */
8172 {
8173 col = off - LineOffset[row];
8174 screen_stop_highlight();
8175 term_windgoto(row, col);/* clear rest of this screen line */
8176 out_str(T_CE);
8177 screen_start(); /* don't know where cursor is now */
8178 col = end_col - col;
8179 while (col--) /* clear chars in ScreenLines */
8180 {
8181 ScreenLines[off] = ' ';
8182#ifdef FEAT_MBYTE
8183 if (enc_utf8)
8184 ScreenLinesUC[off] = 0;
8185#endif
8186 ScreenAttrs[off] = 0;
8187 ++off;
8188 }
8189 }
8190 did_delete = TRUE; /* the chars are cleared now */
8191 }
8192
8193 off = LineOffset[row] + start_col;
8194 c = c1;
8195 for (col = start_col; col < end_col; ++col)
8196 {
8197 if (ScreenLines[off] != c
8198#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008199 || (enc_utf8 && (int)ScreenLinesUC[off]
8200 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201#endif
8202 || ScreenAttrs[off] != attr
8203#if defined(FEAT_GUI) || defined(UNIX)
8204 || force_next
8205#endif
8206 )
8207 {
8208#if defined(FEAT_GUI) || defined(UNIX)
8209 /* The bold trick may make a single row of pixels appear in
8210 * the next character. When a bold character is removed, the
8211 * next character should be redrawn too. This happens for our
8212 * own GUI and for some xterms. */
8213 if (
8214# ifdef FEAT_GUI
8215 gui.in_use
8216# endif
8217# if defined(FEAT_GUI) && defined(UNIX)
8218 ||
8219# endif
8220# ifdef UNIX
8221 term_is_xterm
8222# endif
8223 )
8224 {
8225 if (ScreenLines[off] != ' '
8226 && (ScreenAttrs[off] > HL_ALL
8227 || ScreenAttrs[off] & HL_BOLD))
8228 force_next = TRUE;
8229 else
8230 force_next = FALSE;
8231 }
8232#endif
8233 ScreenLines[off] = c;
8234#ifdef FEAT_MBYTE
8235 if (enc_utf8)
8236 {
8237 if (c >= 0x80)
8238 {
8239 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008240 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 }
8242 else
8243 ScreenLinesUC[off] = 0;
8244 }
8245#endif
8246 ScreenAttrs[off] = attr;
8247 if (!did_delete || c != ' ')
8248 screen_char(off, row, col);
8249 }
8250 ++off;
8251 if (col == start_col)
8252 {
8253 if (did_delete)
8254 break;
8255 c = c2;
8256 }
8257 }
8258 if (end_col == Columns)
8259 LineWraps[row] = FALSE;
8260 if (row == Rows - 1) /* overwritten the command line */
8261 {
8262 redraw_cmdline = TRUE;
8263 if (c1 == ' ' && c2 == ' ')
8264 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008265 if (start_col == 0)
8266 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 }
8268 }
8269}
8270
8271/*
8272 * Check if there should be a delay. Used before clearing or redrawing the
8273 * screen or the command line.
8274 */
8275 void
8276check_for_delay(check_msg_scroll)
8277 int check_msg_scroll;
8278{
8279 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8280 && !did_wait_return
8281 && emsg_silent == 0)
8282 {
8283 out_flush();
8284 ui_delay(1000L, TRUE);
8285 emsg_on_display = FALSE;
8286 if (check_msg_scroll)
8287 msg_scroll = FALSE;
8288 }
8289}
8290
8291/*
8292 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008293 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 * Returns TRUE if there is a valid screen to write to.
8295 * Returns FALSE when starting up and screen not initialized yet.
8296 */
8297 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008298screen_valid(doclear)
8299 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008301 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 return (ScreenLines != NULL);
8303}
8304
8305/*
8306 * Resize the shell to Rows and Columns.
8307 * Allocate ScreenLines[] and associated items.
8308 *
8309 * There may be some time between setting Rows and Columns and (re)allocating
8310 * ScreenLines[]. This happens when starting up and when (manually) changing
8311 * the shell size. Always use screen_Rows and screen_Columns to access items
8312 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8313 * final size of the shell is needed.
8314 */
8315 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008316screenalloc(doclear)
8317 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318{
8319 int new_row, old_row;
8320#ifdef FEAT_GUI
8321 int old_Rows;
8322#endif
8323 win_T *wp;
8324 int outofmem = FALSE;
8325 int len;
8326 schar_T *new_ScreenLines;
8327#ifdef FEAT_MBYTE
8328 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008329 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008331 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332#endif
8333 sattr_T *new_ScreenAttrs;
8334 unsigned *new_LineOffset;
8335 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008336#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008337 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008338 tabpage_T *tp;
8339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008341 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008342#ifdef FEAT_AUTOCMD
8343 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008345retry:
8346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 /*
8348 * Allocation of the screen buffers is done only when the size changes and
8349 * when Rows and Columns have been set and we have started doing full
8350 * screen stuff.
8351 */
8352 if ((ScreenLines != NULL
8353 && Rows == screen_Rows
8354 && Columns == screen_Columns
8355#ifdef FEAT_MBYTE
8356 && enc_utf8 == (ScreenLinesUC != NULL)
8357 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008358 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359#endif
8360 )
8361 || Rows == 0
8362 || Columns == 0
8363 || (!full_screen && ScreenLines == NULL))
8364 return;
8365
8366 /*
8367 * It's possible that we produce an out-of-memory message below, which
8368 * will cause this function to be called again. To break the loop, just
8369 * return here.
8370 */
8371 if (entered)
8372 return;
8373 entered = TRUE;
8374
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008375 /*
8376 * Note that the window sizes are updated before reallocating the arrays,
8377 * thus we must not redraw here!
8378 */
8379 ++RedrawingDisabled;
8380
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 win_new_shellsize(); /* fit the windows in the new sized shell */
8382
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 comp_col(); /* recompute columns for shown command and ruler */
8384
8385 /*
8386 * We're changing the size of the screen.
8387 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8388 * - Move lines from the old arrays into the new arrays, clear extra
8389 * lines (unless the screen is going to be cleared).
8390 * - Free the old arrays.
8391 *
8392 * If anything fails, make ScreenLines NULL, so we don't do anything!
8393 * Continuing with the old ScreenLines may result in a crash, because the
8394 * size is wrong.
8395 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008396 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008398#ifdef FEAT_AUTOCMD
8399 if (aucmd_win != NULL)
8400 win_free_lsize(aucmd_win);
8401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402
8403 new_ScreenLines = (schar_T *)lalloc((long_u)(
8404 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8405#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008406 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 if (enc_utf8)
8408 {
8409 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8410 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008411 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008412 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8414 }
8415 if (enc_dbcs == DBCS_JPNU)
8416 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8417 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8418#endif
8419 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8420 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8421 new_LineOffset = (unsigned *)lalloc((long_u)(
8422 Rows * sizeof(unsigned)), FALSE);
8423 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008424#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008425 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008428 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {
8430 if (win_alloc_lines(wp) == FAIL)
8431 {
8432 outofmem = TRUE;
8433#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008434 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435#endif
8436 }
8437 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008438#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008439 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8440 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008441 outofmem = TRUE;
8442#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008443#ifdef FEAT_WINDOWS
8444give_up:
8445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008447#ifdef FEAT_MBYTE
8448 for (i = 0; i < p_mco; ++i)
8449 if (new_ScreenLinesC[i] == NULL)
8450 break;
8451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 if (new_ScreenLines == NULL
8453#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008454 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8456#endif
8457 || new_ScreenAttrs == NULL
8458 || new_LineOffset == NULL
8459 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008460#ifdef FEAT_WINDOWS
8461 || new_TabPageIdxs == NULL
8462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 || outofmem)
8464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008465 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008466 {
8467 /* guess the size */
8468 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8469
8470 /* Remember we did this to avoid getting outofmem messages over
8471 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008472 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 vim_free(new_ScreenLines);
8475 new_ScreenLines = NULL;
8476#ifdef FEAT_MBYTE
8477 vim_free(new_ScreenLinesUC);
8478 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008479 for (i = 0; i < p_mco; ++i)
8480 {
8481 vim_free(new_ScreenLinesC[i]);
8482 new_ScreenLinesC[i] = NULL;
8483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 vim_free(new_ScreenLines2);
8485 new_ScreenLines2 = NULL;
8486#endif
8487 vim_free(new_ScreenAttrs);
8488 new_ScreenAttrs = NULL;
8489 vim_free(new_LineOffset);
8490 new_LineOffset = NULL;
8491 vim_free(new_LineWraps);
8492 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008493#ifdef FEAT_WINDOWS
8494 vim_free(new_TabPageIdxs);
8495 new_TabPageIdxs = NULL;
8496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 }
8498 else
8499 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008500 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008501
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 for (new_row = 0; new_row < Rows; ++new_row)
8503 {
8504 new_LineOffset[new_row] = new_row * Columns;
8505 new_LineWraps[new_row] = FALSE;
8506
8507 /*
8508 * If the screen is not going to be cleared, copy as much as
8509 * possible from the old screen to the new one and clear the rest
8510 * (used when resizing the window at the "--more--" prompt or when
8511 * executing an external command, for the GUI).
8512 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008513 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {
8515 (void)vim_memset(new_ScreenLines + new_row * Columns,
8516 ' ', (size_t)Columns * sizeof(schar_T));
8517#ifdef FEAT_MBYTE
8518 if (enc_utf8)
8519 {
8520 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8521 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008522 for (i = 0; i < p_mco; ++i)
8523 (void)vim_memset(new_ScreenLinesC[i]
8524 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 0, (size_t)Columns * sizeof(u8char_T));
8526 }
8527 if (enc_dbcs == DBCS_JPNU)
8528 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8529 0, (size_t)Columns * sizeof(schar_T));
8530#endif
8531 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8532 0, (size_t)Columns * sizeof(sattr_T));
8533 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008534 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 {
8536 if (screen_Columns < Columns)
8537 len = screen_Columns;
8538 else
8539 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008540#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008541 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008542 * may be invalid now. Also when p_mco changes. */
8543 if (!(enc_utf8 && ScreenLinesUC == NULL)
8544 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008545#endif
8546 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8547 ScreenLines + LineOffset[old_row],
8548 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008550 if (enc_utf8 && ScreenLinesUC != NULL
8551 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
8553 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8554 ScreenLinesUC + LineOffset[old_row],
8555 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008556 for (i = 0; i < p_mco; ++i)
8557 mch_memmove(new_ScreenLinesC[i]
8558 + new_LineOffset[new_row],
8559 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 (size_t)len * sizeof(u8char_T));
8561 }
8562 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8563 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8564 ScreenLines2 + LineOffset[old_row],
8565 (size_t)len * sizeof(schar_T));
8566#endif
8567 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8568 ScreenAttrs + LineOffset[old_row],
8569 (size_t)len * sizeof(sattr_T));
8570 }
8571 }
8572 }
8573 /* Use the last line of the screen for the current line. */
8574 current_ScreenLine = new_ScreenLines + Rows * Columns;
8575 }
8576
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008577 free_screenlines();
8578
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 ScreenLines = new_ScreenLines;
8580#ifdef FEAT_MBYTE
8581 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008582 for (i = 0; i < p_mco; ++i)
8583 ScreenLinesC[i] = new_ScreenLinesC[i];
8584 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 ScreenLines2 = new_ScreenLines2;
8586#endif
8587 ScreenAttrs = new_ScreenAttrs;
8588 LineOffset = new_LineOffset;
8589 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008590#ifdef FEAT_WINDOWS
8591 TabPageIdxs = new_TabPageIdxs;
8592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
8594 /* It's important that screen_Rows and screen_Columns reflect the actual
8595 * size of ScreenLines[]. Set them before calling anything. */
8596#ifdef FEAT_GUI
8597 old_Rows = screen_Rows;
8598#endif
8599 screen_Rows = Rows;
8600 screen_Columns = Columns;
8601
8602 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008603 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 screenclear2();
8605
8606#ifdef FEAT_GUI
8607 else if (gui.in_use
8608 && !gui.starting
8609 && ScreenLines != NULL
8610 && old_Rows != Rows)
8611 {
8612 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8613 /*
8614 * Adjust the position of the cursor, for when executing an external
8615 * command.
8616 */
8617 if (msg_row >= Rows) /* Rows got smaller */
8618 msg_row = Rows - 1; /* put cursor at last row */
8619 else if (Rows > old_Rows) /* Rows got bigger */
8620 msg_row += Rows - old_Rows; /* put cursor in same place */
8621 if (msg_col >= Columns) /* Columns got smaller */
8622 msg_col = Columns - 1; /* put cursor at last column */
8623 }
8624#endif
8625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008627 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008628
8629#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008630 /*
8631 * Do not apply autocommands more than 3 times to avoid an endless loop
8632 * in case applying autocommands always changes Rows or Columns.
8633 */
8634 if (starting == 0 && ++retry_count <= 3)
8635 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008636 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008637 /* In rare cases, autocommands may have altered Rows or Columns,
8638 * jump back to check if we need to allocate the screen again. */
8639 goto retry;
8640 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642}
8643
8644 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008645free_screenlines()
8646{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008647#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008648 int i;
8649
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008650 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008651 for (i = 0; i < Screen_mco; ++i)
8652 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008653 vim_free(ScreenLines2);
8654#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008655 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008656 vim_free(ScreenAttrs);
8657 vim_free(LineOffset);
8658 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008659#ifdef FEAT_WINDOWS
8660 vim_free(TabPageIdxs);
8661#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008662}
8663
8664 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665screenclear()
8666{
8667 check_for_delay(FALSE);
8668 screenalloc(FALSE); /* allocate screen buffers if size changed */
8669 screenclear2(); /* clear the screen */
8670}
8671
8672 static void
8673screenclear2()
8674{
8675 int i;
8676
8677 if (starting == NO_SCREEN || ScreenLines == NULL
8678#ifdef FEAT_GUI
8679 || (gui.in_use && gui.starting)
8680#endif
8681 )
8682 return;
8683
8684#ifdef FEAT_GUI
8685 if (!gui.in_use)
8686#endif
8687 screen_attr = -1; /* force setting the Normal colors */
8688 screen_stop_highlight(); /* don't want highlighting here */
8689
8690#ifdef FEAT_CLIPBOARD
8691 /* disable selection without redrawing it */
8692 clip_scroll_selection(9999);
8693#endif
8694
8695 /* blank out ScreenLines */
8696 for (i = 0; i < Rows; ++i)
8697 {
8698 lineclear(LineOffset[i], (int)Columns);
8699 LineWraps[i] = FALSE;
8700 }
8701
8702 if (can_clear(T_CL))
8703 {
8704 out_str(T_CL); /* clear the display */
8705 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008706 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 }
8708 else
8709 {
8710 /* can't clear the screen, mark all chars with invalid attributes */
8711 for (i = 0; i < Rows; ++i)
8712 lineinvalid(LineOffset[i], (int)Columns);
8713 clear_cmdline = TRUE;
8714 }
8715
8716 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8717
8718 win_rest_invalid(firstwin);
8719 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008720#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008721 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 if (must_redraw == CLEAR) /* no need to clear again */
8724 must_redraw = NOT_VALID;
8725 compute_cmdrow();
8726 msg_row = cmdline_row; /* put cursor on last line for messages */
8727 msg_col = 0;
8728 screen_start(); /* don't know where cursor is now */
8729 msg_scrolled = 0; /* can't scroll back */
8730 msg_didany = FALSE;
8731 msg_didout = FALSE;
8732}
8733
8734/*
8735 * Clear one line in ScreenLines.
8736 */
8737 static void
8738lineclear(off, width)
8739 unsigned off;
8740 int width;
8741{
8742 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8743#ifdef FEAT_MBYTE
8744 if (enc_utf8)
8745 (void)vim_memset(ScreenLinesUC + off, 0,
8746 (size_t)width * sizeof(u8char_T));
8747#endif
8748 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8749}
8750
8751/*
8752 * Mark one line in ScreenLines invalid by setting the attributes to an
8753 * invalid value.
8754 */
8755 static void
8756lineinvalid(off, width)
8757 unsigned off;
8758 int width;
8759{
8760 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8761}
8762
8763#ifdef FEAT_VERTSPLIT
8764/*
8765 * Copy part of a Screenline for vertically split window "wp".
8766 */
8767 static void
8768linecopy(to, from, wp)
8769 int to;
8770 int from;
8771 win_T *wp;
8772{
8773 unsigned off_to = LineOffset[to] + wp->w_wincol;
8774 unsigned off_from = LineOffset[from] + wp->w_wincol;
8775
8776 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8777 wp->w_width * sizeof(schar_T));
8778# ifdef FEAT_MBYTE
8779 if (enc_utf8)
8780 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008781 int i;
8782
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8784 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008785 for (i = 0; i < p_mco; ++i)
8786 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8787 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 }
8789 if (enc_dbcs == DBCS_JPNU)
8790 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8791 wp->w_width * sizeof(schar_T));
8792# endif
8793 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8794 wp->w_width * sizeof(sattr_T));
8795}
8796#endif
8797
8798/*
8799 * Return TRUE if clearing with term string "p" would work.
8800 * It can't work when the string is empty or it won't set the right background.
8801 */
8802 int
8803can_clear(p)
8804 char_u *p;
8805{
8806 return (*p != NUL && (t_colors <= 1
8807#ifdef FEAT_GUI
8808 || gui.in_use
8809#endif
8810 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8811}
8812
8813/*
8814 * Reset cursor position. Use whenever cursor was moved because of outputting
8815 * something directly to the screen (shell commands) or a terminal control
8816 * code.
8817 */
8818 void
8819screen_start()
8820{
8821 screen_cur_row = screen_cur_col = 9999;
8822}
8823
8824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 * Move the cursor to position "row","col" in the screen.
8826 * This tries to find the most efficient way to move, minimizing the number of
8827 * characters sent to the terminal.
8828 */
8829 void
8830windgoto(row, col)
8831 int row;
8832 int col;
8833{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008834 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 int i;
8836 int plan;
8837 int cost;
8838 int wouldbe_col;
8839 int noinvcurs;
8840 char_u *bs;
8841 int goto_cost;
8842 int attr;
8843
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008844#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8846
8847#define PLAN_LE 1
8848#define PLAN_CR 2
8849#define PLAN_NL 3
8850#define PLAN_WRITE 4
8851 /* Can't use ScreenLines unless initialized */
8852 if (ScreenLines == NULL)
8853 return;
8854
8855 if (col != screen_cur_col || row != screen_cur_row)
8856 {
8857 /* Check for valid position. */
8858 if (row < 0) /* window without text lines? */
8859 row = 0;
8860 if (row >= screen_Rows)
8861 row = screen_Rows - 1;
8862 if (col >= screen_Columns)
8863 col = screen_Columns - 1;
8864
8865 /* check if no cursor movement is allowed in highlight mode */
8866 if (screen_attr && *T_MS == NUL)
8867 noinvcurs = HIGHL_COST;
8868 else
8869 noinvcurs = 0;
8870 goto_cost = GOTO_COST + noinvcurs;
8871
8872 /*
8873 * Plan how to do the positioning:
8874 * 1. Use CR to move it to column 0, same row.
8875 * 2. Use T_LE to move it a few columns to the left.
8876 * 3. Use NL to move a few lines down, column 0.
8877 * 4. Move a few columns to the right with T_ND or by writing chars.
8878 *
8879 * Don't do this if the cursor went beyond the last column, the cursor
8880 * position is unknown then (some terminals wrap, some don't )
8881 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008882 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 * characters to move the cursor to the right.
8884 */
8885 if (row >= screen_cur_row && screen_cur_col < Columns)
8886 {
8887 /*
8888 * If the cursor is in the same row, bigger col, we can use CR
8889 * or T_LE.
8890 */
8891 bs = NULL; /* init for GCC */
8892 attr = screen_attr;
8893 if (row == screen_cur_row && col < screen_cur_col)
8894 {
8895 /* "le" is preferred over "bc", because "bc" is obsolete */
8896 if (*T_LE)
8897 bs = T_LE; /* "cursor left" */
8898 else
8899 bs = T_BC; /* "backspace character (old) */
8900 if (*bs)
8901 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8902 else
8903 cost = 999;
8904 if (col + 1 < cost) /* using CR is less characters */
8905 {
8906 plan = PLAN_CR;
8907 wouldbe_col = 0;
8908 cost = 1; /* CR is just one character */
8909 }
8910 else
8911 {
8912 plan = PLAN_LE;
8913 wouldbe_col = col;
8914 }
8915 if (noinvcurs) /* will stop highlighting */
8916 {
8917 cost += noinvcurs;
8918 attr = 0;
8919 }
8920 }
8921
8922 /*
8923 * If the cursor is above where we want to be, we can use CR LF.
8924 */
8925 else if (row > screen_cur_row)
8926 {
8927 plan = PLAN_NL;
8928 wouldbe_col = 0;
8929 cost = (row - screen_cur_row) * 2; /* CR LF */
8930 if (noinvcurs) /* will stop highlighting */
8931 {
8932 cost += noinvcurs;
8933 attr = 0;
8934 }
8935 }
8936
8937 /*
8938 * If the cursor is in the same row, smaller col, just use write.
8939 */
8940 else
8941 {
8942 plan = PLAN_WRITE;
8943 wouldbe_col = screen_cur_col;
8944 cost = 0;
8945 }
8946
8947 /*
8948 * Check if any characters that need to be written have the
8949 * correct attributes. Also avoid UTF-8 characters.
8950 */
8951 i = col - wouldbe_col;
8952 if (i > 0)
8953 cost += i;
8954 if (cost < goto_cost && i > 0)
8955 {
8956 /*
8957 * Check if the attributes are correct without additionally
8958 * stopping highlighting.
8959 */
8960 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8961 while (i && *p++ == attr)
8962 --i;
8963 if (i != 0)
8964 {
8965 /*
8966 * Try if it works when highlighting is stopped here.
8967 */
8968 if (*--p == 0)
8969 {
8970 cost += noinvcurs;
8971 while (i && *p++ == 0)
8972 --i;
8973 }
8974 if (i != 0)
8975 cost = 999; /* different attributes, don't do it */
8976 }
8977#ifdef FEAT_MBYTE
8978 if (enc_utf8)
8979 {
8980 /* Don't use an UTF-8 char for positioning, it's slow. */
8981 for (i = wouldbe_col; i < col; ++i)
8982 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8983 {
8984 cost = 999;
8985 break;
8986 }
8987 }
8988#endif
8989 }
8990
8991 /*
8992 * We can do it without term_windgoto()!
8993 */
8994 if (cost < goto_cost)
8995 {
8996 if (plan == PLAN_LE)
8997 {
8998 if (noinvcurs)
8999 screen_stop_highlight();
9000 while (screen_cur_col > col)
9001 {
9002 out_str(bs);
9003 --screen_cur_col;
9004 }
9005 }
9006 else if (plan == PLAN_CR)
9007 {
9008 if (noinvcurs)
9009 screen_stop_highlight();
9010 out_char('\r');
9011 screen_cur_col = 0;
9012 }
9013 else if (plan == PLAN_NL)
9014 {
9015 if (noinvcurs)
9016 screen_stop_highlight();
9017 while (screen_cur_row < row)
9018 {
9019 out_char('\n');
9020 ++screen_cur_row;
9021 }
9022 screen_cur_col = 0;
9023 }
9024
9025 i = col - screen_cur_col;
9026 if (i > 0)
9027 {
9028 /*
9029 * Use cursor-right if it's one character only. Avoids
9030 * removing a line of pixels from the last bold char, when
9031 * using the bold trick in the GUI.
9032 */
9033 if (T_ND[0] != NUL && T_ND[1] == NUL)
9034 {
9035 while (i-- > 0)
9036 out_char(*T_ND);
9037 }
9038 else
9039 {
9040 int off;
9041
9042 off = LineOffset[row] + screen_cur_col;
9043 while (i-- > 0)
9044 {
9045 if (ScreenAttrs[off] != screen_attr)
9046 screen_stop_highlight();
9047#ifdef FEAT_MBYTE
9048 out_flush_check();
9049#endif
9050 out_char(ScreenLines[off]);
9051#ifdef FEAT_MBYTE
9052 if (enc_dbcs == DBCS_JPNU
9053 && ScreenLines[off] == 0x8e)
9054 out_char(ScreenLines2[off]);
9055#endif
9056 ++off;
9057 }
9058 }
9059 }
9060 }
9061 }
9062 else
9063 cost = 999;
9064
9065 if (cost >= goto_cost)
9066 {
9067 if (noinvcurs)
9068 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009069 if (row == screen_cur_row && (col > screen_cur_col)
9070 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 term_cursor_right(col - screen_cur_col);
9072 else
9073 term_windgoto(row, col);
9074 }
9075 screen_cur_row = row;
9076 screen_cur_col = col;
9077 }
9078}
9079
9080/*
9081 * Set cursor to its position in the current window.
9082 */
9083 void
9084setcursor()
9085{
9086 if (redrawing())
9087 {
9088 validate_cursor();
9089 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9090 W_WINCOL(curwin) + (
9091#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009092 /* With 'rightleft' set and the cursor on a double-wide
9093 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9095# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009096 (has_mbyte
9097 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9098 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099# endif
9100 1)) :
9101#endif
9102 curwin->w_wcol));
9103 }
9104}
9105
9106
9107/*
9108 * insert 'line_count' lines at 'row' in window 'wp'
9109 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9110 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9111 * scrolling.
9112 * Returns FAIL if the lines are not inserted, OK for success.
9113 */
9114 int
9115win_ins_lines(wp, row, line_count, invalid, mayclear)
9116 win_T *wp;
9117 int row;
9118 int line_count;
9119 int invalid;
9120 int mayclear;
9121{
9122 int did_delete;
9123 int nextrow;
9124 int lastrow;
9125 int retval;
9126
9127 if (invalid)
9128 wp->w_lines_valid = 0;
9129
9130 if (wp->w_height < 5)
9131 return FAIL;
9132
9133 if (line_count > wp->w_height - row)
9134 line_count = wp->w_height - row;
9135
9136 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9137 if (retval != MAYBE)
9138 return retval;
9139
9140 /*
9141 * If there is a next window or a status line, we first try to delete the
9142 * lines at the bottom to avoid messing what is after the window.
9143 * If this fails and there are following windows, don't do anything to avoid
9144 * messing up those windows, better just redraw.
9145 */
9146 did_delete = FALSE;
9147#ifdef FEAT_WINDOWS
9148 if (wp->w_next != NULL || wp->w_status_height)
9149 {
9150 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9151 line_count, (int)Rows, FALSE, NULL) == OK)
9152 did_delete = TRUE;
9153 else if (wp->w_next)
9154 return FAIL;
9155 }
9156#endif
9157 /*
9158 * if no lines deleted, blank the lines that will end up below the window
9159 */
9160 if (!did_delete)
9161 {
9162#ifdef FEAT_WINDOWS
9163 wp->w_redr_status = TRUE;
9164#endif
9165 redraw_cmdline = TRUE;
9166 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9167 lastrow = nextrow + line_count;
9168 if (lastrow > Rows)
9169 lastrow = Rows;
9170 screen_fill(nextrow - line_count, lastrow - line_count,
9171 W_WINCOL(wp), (int)W_ENDCOL(wp),
9172 ' ', ' ', 0);
9173 }
9174
9175 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9176 == FAIL)
9177 {
9178 /* deletion will have messed up other windows */
9179 if (did_delete)
9180 {
9181#ifdef FEAT_WINDOWS
9182 wp->w_redr_status = TRUE;
9183#endif
9184 win_rest_invalid(W_NEXT(wp));
9185 }
9186 return FAIL;
9187 }
9188
9189 return OK;
9190}
9191
9192/*
9193 * delete "line_count" window lines at "row" in window "wp"
9194 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9195 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9196 * scrolling
9197 * Return OK for success, FAIL if the lines are not deleted.
9198 */
9199 int
9200win_del_lines(wp, row, line_count, invalid, mayclear)
9201 win_T *wp;
9202 int row;
9203 int line_count;
9204 int invalid;
9205 int mayclear;
9206{
9207 int retval;
9208
9209 if (invalid)
9210 wp->w_lines_valid = 0;
9211
9212 if (line_count > wp->w_height - row)
9213 line_count = wp->w_height - row;
9214
9215 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9216 if (retval != MAYBE)
9217 return retval;
9218
9219 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9220 (int)Rows, FALSE, NULL) == FAIL)
9221 return FAIL;
9222
9223#ifdef FEAT_WINDOWS
9224 /*
9225 * If there are windows or status lines below, try to put them at the
9226 * correct place. If we can't do that, they have to be redrawn.
9227 */
9228 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9229 {
9230 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9231 line_count, (int)Rows, NULL) == FAIL)
9232 {
9233 wp->w_redr_status = TRUE;
9234 win_rest_invalid(wp->w_next);
9235 }
9236 }
9237 /*
9238 * If this is the last window and there is no status line, redraw the
9239 * command line later.
9240 */
9241 else
9242#endif
9243 redraw_cmdline = TRUE;
9244 return OK;
9245}
9246
9247/*
9248 * Common code for win_ins_lines() and win_del_lines().
9249 * Returns OK or FAIL when the work has been done.
9250 * Returns MAYBE when not finished yet.
9251 */
9252 static int
9253win_do_lines(wp, row, line_count, mayclear, del)
9254 win_T *wp;
9255 int row;
9256 int line_count;
9257 int mayclear;
9258 int del;
9259{
9260 int retval;
9261
9262 if (!redrawing() || line_count <= 0)
9263 return FAIL;
9264
9265 /* only a few lines left: redraw is faster */
9266 if (mayclear && Rows - line_count < 5
9267#ifdef FEAT_VERTSPLIT
9268 && wp->w_width == Columns
9269#endif
9270 )
9271 {
9272 screenclear(); /* will set wp->w_lines_valid to 0 */
9273 return FAIL;
9274 }
9275
9276 /*
9277 * Delete all remaining lines
9278 */
9279 if (row + line_count >= wp->w_height)
9280 {
9281 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9282 W_WINCOL(wp), (int)W_ENDCOL(wp),
9283 ' ', ' ', 0);
9284 return OK;
9285 }
9286
9287 /*
9288 * when scrolling, the message on the command line should be cleared,
9289 * otherwise it will stay there forever.
9290 */
9291 clear_cmdline = TRUE;
9292
9293 /*
9294 * If the terminal can set a scroll region, use that.
9295 * Always do this in a vertically split window. This will redraw from
9296 * ScreenLines[] when t_CV isn't defined. That's faster than using
9297 * win_line().
9298 * Don't use a scroll region when we are going to redraw the text, writing
9299 * a character in the lower right corner of the scroll region causes a
9300 * scroll-up in the DJGPP version.
9301 */
9302 if (scroll_region
9303#ifdef FEAT_VERTSPLIT
9304 || W_WIDTH(wp) != Columns
9305#endif
9306 )
9307 {
9308#ifdef FEAT_VERTSPLIT
9309 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9310#endif
9311 scroll_region_set(wp, row);
9312 if (del)
9313 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9314 wp->w_height - row, FALSE, wp);
9315 else
9316 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9317 wp->w_height - row, wp);
9318#ifdef FEAT_VERTSPLIT
9319 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9320#endif
9321 scroll_region_reset();
9322 return retval;
9323 }
9324
9325#ifdef FEAT_WINDOWS
9326 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9327 return FAIL;
9328#endif
9329
9330 return MAYBE;
9331}
9332
9333/*
9334 * window 'wp' and everything after it is messed up, mark it for redraw
9335 */
9336 static void
9337win_rest_invalid(wp)
9338 win_T *wp;
9339{
9340#ifdef FEAT_WINDOWS
9341 while (wp != NULL)
9342#else
9343 if (wp != NULL)
9344#endif
9345 {
9346 redraw_win_later(wp, NOT_VALID);
9347#ifdef FEAT_WINDOWS
9348 wp->w_redr_status = TRUE;
9349 wp = wp->w_next;
9350#endif
9351 }
9352 redraw_cmdline = TRUE;
9353}
9354
9355/*
9356 * The rest of the routines in this file perform screen manipulations. The
9357 * given operation is performed physically on the screen. The corresponding
9358 * change is also made to the internal screen image. In this way, the editor
9359 * anticipates the effect of editing changes on the appearance of the screen.
9360 * That way, when we call screenupdate a complete redraw isn't usually
9361 * necessary. Another advantage is that we can keep adding code to anticipate
9362 * screen changes, and in the meantime, everything still works.
9363 */
9364
9365/*
9366 * types for inserting or deleting lines
9367 */
9368#define USE_T_CAL 1
9369#define USE_T_CDL 2
9370#define USE_T_AL 3
9371#define USE_T_CE 4
9372#define USE_T_DL 5
9373#define USE_T_SR 6
9374#define USE_NL 7
9375#define USE_T_CD 8
9376#define USE_REDRAW 9
9377
9378/*
9379 * insert lines on the screen and update ScreenLines[]
9380 * 'end' is the line after the scrolled part. Normally it is Rows.
9381 * When scrolling region used 'off' is the offset from the top for the region.
9382 * 'row' and 'end' are relative to the start of the region.
9383 *
9384 * return FAIL for failure, OK for success.
9385 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009386 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387screen_ins_lines(off, row, line_count, end, wp)
9388 int off;
9389 int row;
9390 int line_count;
9391 int end;
9392 win_T *wp; /* NULL or window to use width from */
9393{
9394 int i;
9395 int j;
9396 unsigned temp;
9397 int cursor_row;
9398 int type;
9399 int result_empty;
9400 int can_ce = can_clear(T_CE);
9401
9402 /*
9403 * FAIL if
9404 * - there is no valid screen
9405 * - the screen has to be redrawn completely
9406 * - the line count is less than one
9407 * - the line count is more than 'ttyscroll'
9408 */
9409 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9410 return FAIL;
9411
9412 /*
9413 * There are seven ways to insert lines:
9414 * 0. When in a vertically split window and t_CV isn't set, redraw the
9415 * characters from ScreenLines[].
9416 * 1. Use T_CD (clear to end of display) if it exists and the result of
9417 * the insert is just empty lines
9418 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9419 * present or line_count > 1. It looks better if we do all the inserts
9420 * at once.
9421 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9422 * insert is just empty lines and T_CE is not present or line_count >
9423 * 1.
9424 * 4. Use T_AL (insert line) if it exists.
9425 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9426 * just empty lines.
9427 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9428 * just empty lines.
9429 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9430 * the 'da' flag is not set or we have clear line capability.
9431 * 8. redraw the characters from ScreenLines[].
9432 *
9433 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9434 * the scrollbar for the window. It does have insert line, use that if it
9435 * exists.
9436 */
9437 result_empty = (row + line_count >= end);
9438#ifdef FEAT_VERTSPLIT
9439 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9440 type = USE_REDRAW;
9441 else
9442#endif
9443 if (can_clear(T_CD) && result_empty)
9444 type = USE_T_CD;
9445 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9446 type = USE_T_CAL;
9447 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9448 type = USE_T_CDL;
9449 else if (*T_AL != NUL)
9450 type = USE_T_AL;
9451 else if (can_ce && result_empty)
9452 type = USE_T_CE;
9453 else if (*T_DL != NUL && result_empty)
9454 type = USE_T_DL;
9455 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9456 type = USE_T_SR;
9457 else
9458 return FAIL;
9459
9460 /*
9461 * For clearing the lines screen_del_lines() is used. This will also take
9462 * care of t_db if necessary.
9463 */
9464 if (type == USE_T_CD || type == USE_T_CDL ||
9465 type == USE_T_CE || type == USE_T_DL)
9466 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9467
9468 /*
9469 * If text is retained below the screen, first clear or delete as many
9470 * lines at the bottom of the window as are about to be inserted so that
9471 * the deleted lines won't later surface during a screen_del_lines.
9472 */
9473 if (*T_DB)
9474 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9475
9476#ifdef FEAT_CLIPBOARD
9477 /* Remove a modeless selection when inserting lines halfway the screen
9478 * or not the full width of the screen. */
9479 if (off + row > 0
9480# ifdef FEAT_VERTSPLIT
9481 || (wp != NULL && wp->w_width != Columns)
9482# endif
9483 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009484 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 else
9486 clip_scroll_selection(-line_count);
9487#endif
9488
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489#ifdef FEAT_GUI
9490 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9491 * scrolling is actually carried out. */
9492 gui_dont_update_cursor();
9493#endif
9494
9495 if (*T_CCS != NUL) /* cursor relative to region */
9496 cursor_row = row;
9497 else
9498 cursor_row = row + off;
9499
9500 /*
9501 * Shift LineOffset[] line_count down to reflect the inserted lines.
9502 * Clear the inserted lines in ScreenLines[].
9503 */
9504 row += off;
9505 end += off;
9506 for (i = 0; i < line_count; ++i)
9507 {
9508#ifdef FEAT_VERTSPLIT
9509 if (wp != NULL && wp->w_width != Columns)
9510 {
9511 /* need to copy part of a line */
9512 j = end - 1 - i;
9513 while ((j -= line_count) >= row)
9514 linecopy(j + line_count, j, wp);
9515 j += line_count;
9516 if (can_clear((char_u *)" "))
9517 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9518 else
9519 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9520 LineWraps[j] = FALSE;
9521 }
9522 else
9523#endif
9524 {
9525 j = end - 1 - i;
9526 temp = LineOffset[j];
9527 while ((j -= line_count) >= row)
9528 {
9529 LineOffset[j + line_count] = LineOffset[j];
9530 LineWraps[j + line_count] = LineWraps[j];
9531 }
9532 LineOffset[j + line_count] = temp;
9533 LineWraps[j + line_count] = FALSE;
9534 if (can_clear((char_u *)" "))
9535 lineclear(temp, (int)Columns);
9536 else
9537 lineinvalid(temp, (int)Columns);
9538 }
9539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
9541 screen_stop_highlight();
9542 windgoto(cursor_row, 0);
9543
9544#ifdef FEAT_VERTSPLIT
9545 /* redraw the characters */
9546 if (type == USE_REDRAW)
9547 redraw_block(row, end, wp);
9548 else
9549#endif
9550 if (type == USE_T_CAL)
9551 {
9552 term_append_lines(line_count);
9553 screen_start(); /* don't know where cursor is now */
9554 }
9555 else
9556 {
9557 for (i = 0; i < line_count; i++)
9558 {
9559 if (type == USE_T_AL)
9560 {
9561 if (i && cursor_row != 0)
9562 windgoto(cursor_row, 0);
9563 out_str(T_AL);
9564 }
9565 else /* type == USE_T_SR */
9566 out_str(T_SR);
9567 screen_start(); /* don't know where cursor is now */
9568 }
9569 }
9570
9571 /*
9572 * With scroll-reverse and 'da' flag set we need to clear the lines that
9573 * have been scrolled down into the region.
9574 */
9575 if (type == USE_T_SR && *T_DA)
9576 {
9577 for (i = 0; i < line_count; ++i)
9578 {
9579 windgoto(off + i, 0);
9580 out_str(T_CE);
9581 screen_start(); /* don't know where cursor is now */
9582 }
9583 }
9584
9585#ifdef FEAT_GUI
9586 gui_can_update_cursor();
9587 if (gui.in_use)
9588 out_flush(); /* always flush after a scroll */
9589#endif
9590 return OK;
9591}
9592
9593/*
9594 * delete lines on the screen and update ScreenLines[]
9595 * 'end' is the line after the scrolled part. Normally it is Rows.
9596 * When scrolling region used 'off' is the offset from the top for the region.
9597 * 'row' and 'end' are relative to the start of the region.
9598 *
9599 * Return OK for success, FAIL if the lines are not deleted.
9600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 int
9602screen_del_lines(off, row, line_count, end, force, wp)
9603 int off;
9604 int row;
9605 int line_count;
9606 int end;
9607 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009608 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610 int j;
9611 int i;
9612 unsigned temp;
9613 int cursor_row;
9614 int cursor_end;
9615 int result_empty; /* result is empty until end of region */
9616 int can_delete; /* deleting line codes can be used */
9617 int type;
9618
9619 /*
9620 * FAIL if
9621 * - there is no valid screen
9622 * - the screen has to be redrawn completely
9623 * - the line count is less than one
9624 * - the line count is more than 'ttyscroll'
9625 */
9626 if (!screen_valid(TRUE) || line_count <= 0 ||
9627 (!force && line_count > p_ttyscroll))
9628 return FAIL;
9629
9630 /*
9631 * Check if the rest of the current region will become empty.
9632 */
9633 result_empty = row + line_count >= end;
9634
9635 /*
9636 * We can delete lines only when 'db' flag not set or when 'ce' option
9637 * available.
9638 */
9639 can_delete = (*T_DB == NUL || can_clear(T_CE));
9640
9641 /*
9642 * There are six ways to delete lines:
9643 * 0. When in a vertically split window and t_CV isn't set, redraw the
9644 * characters from ScreenLines[].
9645 * 1. Use T_CD if it exists and the result is empty.
9646 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9647 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9648 * none of the other ways work.
9649 * 4. Use T_CE (erase line) if the result is empty.
9650 * 5. Use T_DL (delete line) if it exists.
9651 * 6. redraw the characters from ScreenLines[].
9652 */
9653#ifdef FEAT_VERTSPLIT
9654 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9655 type = USE_REDRAW;
9656 else
9657#endif
9658 if (can_clear(T_CD) && result_empty)
9659 type = USE_T_CD;
9660#if defined(__BEOS__) && defined(BEOS_DR8)
9661 /*
9662 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9663 * its internal termcap... this works okay for tests which test *T_DB !=
9664 * NUL. It has the disadvantage that the user cannot use any :set t_*
9665 * command to get T_DB (back) to empty_option, only :set term=... will do
9666 * the trick...
9667 * Anyway, this hack will hopefully go away with the next OS release.
9668 * (Olaf Seibert)
9669 */
9670 else if (row == 0 && T_DB == empty_option
9671 && (line_count == 1 || *T_CDL == NUL))
9672#else
9673 else if (row == 0 && (
9674#ifndef AMIGA
9675 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9676 * up, so use delete-line command */
9677 line_count == 1 ||
9678#endif
9679 *T_CDL == NUL))
9680#endif
9681 type = USE_NL;
9682 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9683 type = USE_T_CDL;
9684 else if (can_clear(T_CE) && result_empty
9685#ifdef FEAT_VERTSPLIT
9686 && (wp == NULL || wp->w_width == Columns)
9687#endif
9688 )
9689 type = USE_T_CE;
9690 else if (*T_DL != NUL && can_delete)
9691 type = USE_T_DL;
9692 else if (*T_CDL != NUL && can_delete)
9693 type = USE_T_CDL;
9694 else
9695 return FAIL;
9696
9697#ifdef FEAT_CLIPBOARD
9698 /* Remove a modeless selection when deleting lines halfway the screen or
9699 * not the full width of the screen. */
9700 if (off + row > 0
9701# ifdef FEAT_VERTSPLIT
9702 || (wp != NULL && wp->w_width != Columns)
9703# endif
9704 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009705 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 else
9707 clip_scroll_selection(line_count);
9708#endif
9709
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710#ifdef FEAT_GUI
9711 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9712 * scrolling is actually carried out. */
9713 gui_dont_update_cursor();
9714#endif
9715
9716 if (*T_CCS != NUL) /* cursor relative to region */
9717 {
9718 cursor_row = row;
9719 cursor_end = end;
9720 }
9721 else
9722 {
9723 cursor_row = row + off;
9724 cursor_end = end + off;
9725 }
9726
9727 /*
9728 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9729 * Clear the inserted lines in ScreenLines[].
9730 */
9731 row += off;
9732 end += off;
9733 for (i = 0; i < line_count; ++i)
9734 {
9735#ifdef FEAT_VERTSPLIT
9736 if (wp != NULL && wp->w_width != Columns)
9737 {
9738 /* need to copy part of a line */
9739 j = row + i;
9740 while ((j += line_count) <= end - 1)
9741 linecopy(j - line_count, j, wp);
9742 j -= line_count;
9743 if (can_clear((char_u *)" "))
9744 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9745 else
9746 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9747 LineWraps[j] = FALSE;
9748 }
9749 else
9750#endif
9751 {
9752 /* whole width, moving the line pointers is faster */
9753 j = row + i;
9754 temp = LineOffset[j];
9755 while ((j += line_count) <= end - 1)
9756 {
9757 LineOffset[j - line_count] = LineOffset[j];
9758 LineWraps[j - line_count] = LineWraps[j];
9759 }
9760 LineOffset[j - line_count] = temp;
9761 LineWraps[j - line_count] = FALSE;
9762 if (can_clear((char_u *)" "))
9763 lineclear(temp, (int)Columns);
9764 else
9765 lineinvalid(temp, (int)Columns);
9766 }
9767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768
9769 screen_stop_highlight();
9770
9771#ifdef FEAT_VERTSPLIT
9772 /* redraw the characters */
9773 if (type == USE_REDRAW)
9774 redraw_block(row, end, wp);
9775 else
9776#endif
9777 if (type == USE_T_CD) /* delete the lines */
9778 {
9779 windgoto(cursor_row, 0);
9780 out_str(T_CD);
9781 screen_start(); /* don't know where cursor is now */
9782 }
9783 else if (type == USE_T_CDL)
9784 {
9785 windgoto(cursor_row, 0);
9786 term_delete_lines(line_count);
9787 screen_start(); /* don't know where cursor is now */
9788 }
9789 /*
9790 * Deleting lines at top of the screen or scroll region: Just scroll
9791 * the whole screen (scroll region) up by outputting newlines on the
9792 * last line.
9793 */
9794 else if (type == USE_NL)
9795 {
9796 windgoto(cursor_end - 1, 0);
9797 for (i = line_count; --i >= 0; )
9798 out_char('\n'); /* cursor will remain on same line */
9799 }
9800 else
9801 {
9802 for (i = line_count; --i >= 0; )
9803 {
9804 if (type == USE_T_DL)
9805 {
9806 windgoto(cursor_row, 0);
9807 out_str(T_DL); /* delete a line */
9808 }
9809 else /* type == USE_T_CE */
9810 {
9811 windgoto(cursor_row + i, 0);
9812 out_str(T_CE); /* erase a line */
9813 }
9814 screen_start(); /* don't know where cursor is now */
9815 }
9816 }
9817
9818 /*
9819 * If the 'db' flag is set, we need to clear the lines that have been
9820 * scrolled up at the bottom of the region.
9821 */
9822 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9823 {
9824 for (i = line_count; i > 0; --i)
9825 {
9826 windgoto(cursor_end - i, 0);
9827 out_str(T_CE); /* erase a line */
9828 screen_start(); /* don't know where cursor is now */
9829 }
9830 }
9831
9832#ifdef FEAT_GUI
9833 gui_can_update_cursor();
9834 if (gui.in_use)
9835 out_flush(); /* always flush after a scroll */
9836#endif
9837
9838 return OK;
9839}
9840
9841/*
9842 * show the current mode and ruler
9843 *
9844 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9845 * If clear_cmdline is FALSE there may be a message there that needs to be
9846 * cleared only if a mode is shown.
9847 * Return the length of the message (0 if no message).
9848 */
9849 int
9850showmode()
9851{
9852 int need_clear;
9853 int length = 0;
9854 int do_mode;
9855 int attr;
9856 int nwr_save;
9857#ifdef FEAT_INS_EXPAND
9858 int sub_attr;
9859#endif
9860
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009861 do_mode = ((p_smd && msg_silent == 0)
9862 && ((State & INSERT)
9863 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009864 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 if (do_mode || Recording)
9866 {
9867 /*
9868 * Don't show mode right now, when not redrawing or inside a mapping.
9869 * Call char_avail() only when we are going to show something, because
9870 * it takes a bit of time.
9871 */
9872 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9873 {
9874 redraw_cmdline = TRUE; /* show mode later */
9875 return 0;
9876 }
9877
9878 nwr_save = need_wait_return;
9879
9880 /* wait a bit before overwriting an important message */
9881 check_for_delay(FALSE);
9882
9883 /* if the cmdline is more than one line high, erase top lines */
9884 need_clear = clear_cmdline;
9885 if (clear_cmdline && cmdline_row < Rows - 1)
9886 msg_clr_cmdline(); /* will reset clear_cmdline */
9887
9888 /* Position on the last line in the window, column 0 */
9889 msg_pos_mode();
9890 cursor_off();
9891 attr = hl_attr(HLF_CM); /* Highlight mode */
9892 if (do_mode)
9893 {
9894 MSG_PUTS_ATTR("--", attr);
9895#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009896 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009897# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009898 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009899# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009900 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009901# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009902 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009903# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 MSG_PUTS_ATTR(" IM", attr);
9905# else
9906 MSG_PUTS_ATTR(" XIM", attr);
9907# endif
9908#endif
9909#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9910 if (gui.in_use)
9911 {
9912 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009913 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 }
9915#endif
9916#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009917 /* CTRL-X in Insert mode */
9918 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 {
9920 /* These messages can get long, avoid a wrap in a narrow
9921 * window. Prefer showing edit_submode_extra. */
9922 length = (Rows - msg_row) * Columns - 3;
9923 if (edit_submode_extra != NULL)
9924 length -= vim_strsize(edit_submode_extra);
9925 if (length > 0)
9926 {
9927 if (edit_submode_pre != NULL)
9928 length -= vim_strsize(edit_submode_pre);
9929 if (length - vim_strsize(edit_submode) > 0)
9930 {
9931 if (edit_submode_pre != NULL)
9932 msg_puts_attr(edit_submode_pre, attr);
9933 msg_puts_attr(edit_submode, attr);
9934 }
9935 if (edit_submode_extra != NULL)
9936 {
9937 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9938 if ((int)edit_submode_highl < (int)HLF_COUNT)
9939 sub_attr = hl_attr(edit_submode_highl);
9940 else
9941 sub_attr = attr;
9942 msg_puts_attr(edit_submode_extra, sub_attr);
9943 }
9944 }
9945 length = 0;
9946 }
9947 else
9948#endif
9949 {
9950#ifdef FEAT_VREPLACE
9951 if (State & VREPLACE_FLAG)
9952 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9953 else
9954#endif
9955 if (State & REPLACE_FLAG)
9956 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9957 else if (State & INSERT)
9958 {
9959#ifdef FEAT_RIGHTLEFT
9960 if (p_ri)
9961 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9962#endif
9963 MSG_PUTS_ATTR(_(" INSERT"), attr);
9964 }
9965 else if (restart_edit == 'I')
9966 MSG_PUTS_ATTR(_(" (insert)"), attr);
9967 else if (restart_edit == 'R')
9968 MSG_PUTS_ATTR(_(" (replace)"), attr);
9969 else if (restart_edit == 'V')
9970 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9971#ifdef FEAT_RIGHTLEFT
9972 if (p_hkmap)
9973 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9974# ifdef FEAT_FKMAP
9975 if (p_fkmap)
9976 MSG_PUTS_ATTR(farsi_text_5, attr);
9977# endif
9978#endif
9979#ifdef FEAT_KEYMAP
9980 if (State & LANGMAP)
9981 {
9982# ifdef FEAT_ARABIC
9983 if (curwin->w_p_arab)
9984 MSG_PUTS_ATTR(_(" Arabic"), attr);
9985 else
9986# endif
9987 MSG_PUTS_ATTR(_(" (lang)"), attr);
9988 }
9989#endif
9990 if ((State & INSERT) && p_paste)
9991 MSG_PUTS_ATTR(_(" (paste)"), attr);
9992
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 if (VIsual_active)
9994 {
9995 char *p;
9996
9997 /* Don't concatenate separate words to avoid translation
9998 * problems. */
9999 switch ((VIsual_select ? 4 : 0)
10000 + (VIsual_mode == Ctrl_V) * 2
10001 + (VIsual_mode == 'V'))
10002 {
10003 case 0: p = N_(" VISUAL"); break;
10004 case 1: p = N_(" VISUAL LINE"); break;
10005 case 2: p = N_(" VISUAL BLOCK"); break;
10006 case 4: p = N_(" SELECT"); break;
10007 case 5: p = N_(" SELECT LINE"); break;
10008 default: p = N_(" SELECT BLOCK"); break;
10009 }
10010 MSG_PUTS_ATTR(_(p), attr);
10011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 MSG_PUTS_ATTR(" --", attr);
10013 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010014
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 need_clear = TRUE;
10016 }
10017 if (Recording
10018#ifdef FEAT_INS_EXPAND
10019 && edit_submode == NULL /* otherwise it gets too long */
10020#endif
10021 )
10022 {
10023 MSG_PUTS_ATTR(_("recording"), attr);
10024 need_clear = TRUE;
10025 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010026
10027 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 if (need_clear || clear_cmdline)
10029 msg_clr_eos();
10030 msg_didout = FALSE; /* overwrite this message */
10031 length = msg_col;
10032 msg_col = 0;
10033 need_wait_return = nwr_save; /* never ask for hit-return for this */
10034 }
10035 else if (clear_cmdline && msg_silent == 0)
10036 /* Clear the whole command line. Will reset "clear_cmdline". */
10037 msg_clr_cmdline();
10038
10039#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 /* In Visual mode the size of the selected area must be redrawn. */
10041 if (VIsual_active)
10042 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
10044 /* If the last window has no status line, the ruler is after the mode
10045 * message and must be redrawn */
10046 if (redrawing()
10047# ifdef FEAT_WINDOWS
10048 && lastwin->w_status_height == 0
10049# endif
10050 )
10051 win_redr_ruler(lastwin, TRUE);
10052#endif
10053 redraw_cmdline = FALSE;
10054 clear_cmdline = FALSE;
10055
10056 return length;
10057}
10058
10059/*
10060 * Position for a mode message.
10061 */
10062 static void
10063msg_pos_mode()
10064{
10065 msg_col = 0;
10066 msg_row = Rows - 1;
10067}
10068
10069/*
10070 * Delete mode message. Used when ESC is typed which is expected to end
10071 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010072 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 */
10074 void
10075unshowmode(force)
10076 int force;
10077{
10078 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010079 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 */
10081 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10082 redraw_cmdline = TRUE; /* delete mode later */
10083 else
10084 {
10085 msg_pos_mode();
10086 if (Recording)
10087 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
10088 msg_clr_eos();
10089 }
10090}
10091
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010092#if defined(FEAT_WINDOWS)
10093/*
10094 * Draw the tab pages line at the top of the Vim window.
10095 */
10096 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010097draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010098{
10099 int tabcount = 0;
10100 tabpage_T *tp;
10101 int tabwidth;
10102 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010103 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010104 int attr;
10105 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010106 win_T *cwp;
10107 int wincount;
10108 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010109 int c;
10110 int len;
10111 int attr_sel = hl_attr(HLF_TPS);
10112 int attr_nosel = hl_attr(HLF_TP);
10113 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010114 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010115 int room;
10116 int use_sep_chars = (t_colors < 8
10117#ifdef FEAT_GUI
10118 && !gui.in_use
10119#endif
10120 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010121
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010122 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010123
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010124#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010125 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010126 if (gui_use_tabline())
10127 {
10128 gui_update_tabline();
10129 return;
10130 }
10131#endif
10132
10133 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010134 return;
10135
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010136#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010137
10138 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10139 for (scol = 0; scol < Columns; ++scol)
10140 TabPageIdxs[scol] = 0;
10141
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010142 /* Use the 'tabline' option if it's set. */
10143 if (*p_tal != NUL)
10144 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010145 int save_called_emsg = called_emsg;
10146
10147 /* Check for an error. If there is one we would loop in redrawing the
10148 * screen. Avoid that by making 'tabline' empty. */
10149 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010150 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010151 if (called_emsg)
10152 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010153 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010154 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010155 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010156 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010157#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010158 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010159 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10160 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010161
Bram Moolenaar238a5642006-02-21 22:12:05 +000010162 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10163 if (tabwidth < 6)
10164 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010165
Bram Moolenaar238a5642006-02-21 22:12:05 +000010166 attr = attr_nosel;
10167 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010168 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010169 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10170 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010171 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010172 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010173
Bram Moolenaar238a5642006-02-21 22:12:05 +000010174 if (tp->tp_topframe == topframe)
10175 attr = attr_sel;
10176 if (use_sep_chars && col > 0)
10177 screen_putchar('|', 0, col++, attr);
10178
10179 if (tp->tp_topframe != topframe)
10180 attr = attr_nosel;
10181
10182 screen_putchar(' ', 0, col++, attr);
10183
10184 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010185 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010186 cwp = curwin;
10187 wp = firstwin;
10188 }
10189 else
10190 {
10191 cwp = tp->tp_curwin;
10192 wp = tp->tp_firstwin;
10193 }
10194
10195 modified = FALSE;
10196 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10197 if (bufIsChanged(wp->w_buffer))
10198 modified = TRUE;
10199 if (modified || wincount > 1)
10200 {
10201 if (wincount > 1)
10202 {
10203 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010204 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010205 if (col + len >= Columns - 3)
10206 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010207 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010208#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010209 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010210#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010211 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010212#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010213 );
10214 col += len;
10215 }
10216 if (modified)
10217 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10218 screen_putchar(' ', 0, col++, attr);
10219 }
10220
10221 room = scol - col + tabwidth - 1;
10222 if (room > 0)
10223 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010224 /* Get buffer name in NameBuff[] */
10225 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010226 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010227 len = vim_strsize(NameBuff);
10228 p = NameBuff;
10229#ifdef FEAT_MBYTE
10230 if (has_mbyte)
10231 while (len > room)
10232 {
10233 len -= ptr2cells(p);
10234 mb_ptr_adv(p);
10235 }
10236 else
10237#endif
10238 if (len > room)
10239 {
10240 p += len - room;
10241 len = room;
10242 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010243 if (len > Columns - col - 1)
10244 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010245
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010246 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010247 col += len;
10248 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010249 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010250
10251 /* Store the tab page number in TabPageIdxs[], so that
10252 * jump_to_mouse() knows where each one is. */
10253 ++tabcount;
10254 while (scol < col)
10255 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010256 }
10257
Bram Moolenaar238a5642006-02-21 22:12:05 +000010258 if (use_sep_chars)
10259 c = '_';
10260 else
10261 c = ' ';
10262 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010263
10264 /* Put an "X" for closing the current tab if there are several. */
10265 if (first_tabpage->tp_next != NULL)
10266 {
10267 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10268 TabPageIdxs[Columns - 1] = -999;
10269 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010270 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010271
10272 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10273 * set. */
10274 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010275}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010276
10277/*
10278 * Get buffer name for "buf" into NameBuff[].
10279 * Takes care of special buffer names and translates special characters.
10280 */
10281 void
10282get_trans_bufname(buf)
10283 buf_T *buf;
10284{
10285 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010286 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010287 else
10288 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10289 trans_characters(NameBuff, MAXPATHL);
10290}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010291#endif
10292
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10294/*
10295 * Get the character to use in a status line. Get its attributes in "*attr".
10296 */
10297 static int
10298fillchar_status(attr, is_curwin)
10299 int *attr;
10300 int is_curwin;
10301{
10302 int fill;
10303 if (is_curwin)
10304 {
10305 *attr = hl_attr(HLF_S);
10306 fill = fill_stl;
10307 }
10308 else
10309 {
10310 *attr = hl_attr(HLF_SNC);
10311 fill = fill_stlnc;
10312 }
10313 /* Use fill when there is highlighting, and highlighting of current
10314 * window differs, or the fillchars differ, or this is not the
10315 * current window */
10316 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10317 || !is_curwin || firstwin == lastwin)
10318 || (fill_stl != fill_stlnc)))
10319 return fill;
10320 if (is_curwin)
10321 return '^';
10322 return '=';
10323}
10324#endif
10325
10326#ifdef FEAT_VERTSPLIT
10327/*
10328 * Get the character to use in a separator between vertically split windows.
10329 * Get its attributes in "*attr".
10330 */
10331 static int
10332fillchar_vsep(attr)
10333 int *attr;
10334{
10335 *attr = hl_attr(HLF_C);
10336 if (*attr == 0 && fill_vert == ' ')
10337 return '|';
10338 else
10339 return fill_vert;
10340}
10341#endif
10342
10343/*
10344 * Return TRUE if redrawing should currently be done.
10345 */
10346 int
10347redrawing()
10348{
10349 return (!RedrawingDisabled
10350 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10351}
10352
10353/*
10354 * Return TRUE if printing messages should currently be done.
10355 */
10356 int
10357messaging()
10358{
10359 return (!(p_lz && char_avail() && !KeyTyped));
10360}
10361
10362/*
10363 * Show current status info in ruler and various other places
10364 * If always is FALSE, only show ruler if position has changed.
10365 */
10366 void
10367showruler(always)
10368 int always;
10369{
10370 if (!always && !redrawing())
10371 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010372#ifdef FEAT_INS_EXPAND
10373 if (pum_visible())
10374 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010375# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010376 /* Don't redraw right now, do it later. */
10377 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010378# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010379 return;
10380 }
10381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010383 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010384 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010385 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 else
10388#endif
10389#ifdef FEAT_CMDL_INFO
10390 win_redr_ruler(curwin, always);
10391#endif
10392
10393#ifdef FEAT_TITLE
10394 if (need_maketitle
10395# ifdef FEAT_STL_OPT
10396 || (p_icon && (stl_syntax & STL_IN_ICON))
10397 || (p_title && (stl_syntax & STL_IN_TITLE))
10398# endif
10399 )
10400 maketitle();
10401#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010402#ifdef FEAT_WINDOWS
10403 /* Redraw the tab pages line if needed. */
10404 if (redraw_tabline)
10405 draw_tabline();
10406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407}
10408
10409#ifdef FEAT_CMDL_INFO
10410 static void
10411win_redr_ruler(wp, always)
10412 win_T *wp;
10413 int always;
10414{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010415#define RULER_BUF_LEN 70
10416 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 int row;
10418 int fillchar;
10419 int attr;
10420 int empty_line = FALSE;
10421 colnr_T virtcol;
10422 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010423 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 int o;
10425#ifdef FEAT_VERTSPLIT
10426 int this_ru_col;
10427 int off = 0;
10428 int width = Columns;
10429# define WITH_OFF(x) x
10430# define WITH_WIDTH(x) x
10431#else
10432# define WITH_OFF(x) 0
10433# define WITH_WIDTH(x) Columns
10434# define this_ru_col ru_col
10435#endif
10436
10437 /* If 'ruler' off or redrawing disabled, don't do anything */
10438 if (!p_ru)
10439 return;
10440
10441 /*
10442 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10443 * after deleting lines, before cursor.lnum is corrected.
10444 */
10445 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10446 return;
10447
10448#ifdef FEAT_INS_EXPAND
10449 /* Don't draw the ruler while doing insert-completion, it might overwrite
10450 * the (long) mode message. */
10451# ifdef FEAT_WINDOWS
10452 if (wp == lastwin && lastwin->w_status_height == 0)
10453# endif
10454 if (edit_submode != NULL)
10455 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010456 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10457 if (pum_visible())
10458 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459#endif
10460
10461#ifdef FEAT_STL_OPT
10462 if (*p_ruf)
10463 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010464 int save_called_emsg = called_emsg;
10465
10466 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010468 if (called_emsg)
10469 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010470 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010471 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 return;
10473 }
10474#endif
10475
10476 /*
10477 * Check if not in Insert mode and the line is empty (will show "0-1").
10478 */
10479 if (!(State & INSERT)
10480 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10481 empty_line = TRUE;
10482
10483 /*
10484 * Only draw the ruler when something changed.
10485 */
10486 validate_virtcol_win(wp);
10487 if ( redraw_cmdline
10488 || always
10489 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10490 || wp->w_cursor.col != wp->w_ru_cursor.col
10491 || wp->w_virtcol != wp->w_ru_virtcol
10492#ifdef FEAT_VIRTUALEDIT
10493 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10494#endif
10495 || wp->w_topline != wp->w_ru_topline
10496 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10497#ifdef FEAT_DIFF
10498 || wp->w_topfill != wp->w_ru_topfill
10499#endif
10500 || empty_line != wp->w_ru_empty)
10501 {
10502 cursor_off();
10503#ifdef FEAT_WINDOWS
10504 if (wp->w_status_height)
10505 {
10506 row = W_WINROW(wp) + wp->w_height;
10507 fillchar = fillchar_status(&attr, wp == curwin);
10508# ifdef FEAT_VERTSPLIT
10509 off = W_WINCOL(wp);
10510 width = W_WIDTH(wp);
10511# endif
10512 }
10513 else
10514#endif
10515 {
10516 row = Rows - 1;
10517 fillchar = ' ';
10518 attr = 0;
10519#ifdef FEAT_VERTSPLIT
10520 width = Columns;
10521 off = 0;
10522#endif
10523 }
10524
10525 /* In list mode virtcol needs to be recomputed */
10526 virtcol = wp->w_virtcol;
10527 if (wp->w_p_list && lcs_tab1 == NUL)
10528 {
10529 wp->w_p_list = FALSE;
10530 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10531 wp->w_p_list = TRUE;
10532 }
10533
10534 /*
10535 * Some sprintfs return the length, some return a pointer.
10536 * To avoid portability problems we use strlen() here.
10537 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010538 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10540 ? 0L
10541 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010542 len = STRLEN(buffer);
10543 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10545 (int)virtcol + 1);
10546
10547 /*
10548 * Add a "50%" if there is room for it.
10549 * On the last line, don't print in the last column (scrolls the
10550 * screen up on some terminals).
10551 */
10552 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010553 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 o = i + vim_strsize(buffer + i + 1);
10555#ifdef FEAT_WINDOWS
10556 if (wp->w_status_height == 0) /* can't use last char of screen */
10557#endif
10558 ++o;
10559#ifdef FEAT_VERTSPLIT
10560 this_ru_col = ru_col - (Columns - width);
10561 if (this_ru_col < 0)
10562 this_ru_col = 0;
10563#endif
10564 /* Never use more than half the window/screen width, leave the other
10565 * half for the filename. */
10566 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10567 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10568 if (this_ru_col + o < WITH_WIDTH(width))
10569 {
10570 while (this_ru_col + o < WITH_WIDTH(width))
10571 {
10572#ifdef FEAT_MBYTE
10573 if (has_mbyte)
10574 i += (*mb_char2bytes)(fillchar, buffer + i);
10575 else
10576#endif
10577 buffer[i++] = fillchar;
10578 ++o;
10579 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010580 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 }
10582 /* Truncate at window boundary. */
10583#ifdef FEAT_MBYTE
10584 if (has_mbyte)
10585 {
10586 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010587 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 {
10589 o += (*mb_ptr2cells)(buffer + i);
10590 if (this_ru_col + o > WITH_WIDTH(width))
10591 {
10592 buffer[i] = NUL;
10593 break;
10594 }
10595 }
10596 }
10597 else
10598#endif
10599 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10600 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10601
10602 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10603 i = redraw_cmdline;
10604 screen_fill(row, row + 1,
10605 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10606 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10607 fillchar, fillchar, attr);
10608 /* don't redraw the cmdline because of showing the ruler */
10609 redraw_cmdline = i;
10610 wp->w_ru_cursor = wp->w_cursor;
10611 wp->w_ru_virtcol = wp->w_virtcol;
10612 wp->w_ru_empty = empty_line;
10613 wp->w_ru_topline = wp->w_topline;
10614 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10615#ifdef FEAT_DIFF
10616 wp->w_ru_topfill = wp->w_topfill;
10617#endif
10618 }
10619}
10620#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010621
10622#if defined(FEAT_LINEBREAK) || defined(PROTO)
10623/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010624 * Return the width of the 'number' and 'relativenumber' column.
10625 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010626 * Otherwise it depends on 'numberwidth' and the line count.
10627 */
10628 int
10629number_width(wp)
10630 win_T *wp;
10631{
10632 int n;
10633 linenr_T lnum;
10634
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010635 if (wp->w_p_rnu && !wp->w_p_nu)
10636 /* cursor line shows "0" */
10637 lnum = wp->w_height;
10638 else
10639 /* cursor line shows absolute line number */
10640 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010641
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010642 if (lnum == wp->w_nrwidth_line_count)
10643 return wp->w_nrwidth_width;
10644 wp->w_nrwidth_line_count = lnum;
10645
10646 n = 0;
10647 do
10648 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010649 lnum /= 10;
10650 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010651 } while (lnum > 0);
10652
10653 /* 'numberwidth' gives the minimal width plus one */
10654 if (n < wp->w_p_nuw - 1)
10655 n = wp->w_p_nuw - 1;
10656
10657 wp->w_nrwidth_width = n;
10658 return n;
10659}
10660#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010661
10662/*
10663 * Return the current cursor column. This is the actual position on the
10664 * screen. First column is 0.
10665 */
10666 int
10667screen_screencol()
10668{
10669 return screen_cur_col;
10670}
10671
10672/*
10673 * Return the current cursor row. This is the actual position on the screen.
10674 * First row is 0.
10675 */
10676 int
10677screen_screenrow()
10678{
10679 return screen_cur_row;
10680}