blob: e7b8e78d78006ae4ffedfa068b9281ec15f31cbb [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 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003867#ifdef FEAT_MBYTE
3868 int tmp_col = v + MB_PTR2LEN(ptr);
3869
3870 if (shl->endcol < tmp_col)
3871 shl->endcol = tmp_col;
3872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 shl->attr_cur = shl->attr;
3874 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003875 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
3877 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003878 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3879 pos_inprogress = cur == NULL || cur->pos.cur == 0
3880 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
3882 /* Need to get the line again, a multi-line regexp
3883 * may have made it invalid. */
3884 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3885 ptr = line + v;
3886
3887 if (shl->lnum == lnum)
3888 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003889 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003891 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003893 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003895 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
3897 /* highlight empty match, try again after
3898 * it */
3899#ifdef FEAT_MBYTE
3900 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003901 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003902 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 else
3904#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003905 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907
3908 /* Loop to check if the match starts at the
3909 * current position */
3910 continue;
3911 }
3912 }
3913 break;
3914 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003915 if (shl != &search_hl && cur != NULL)
3916 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003918
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003919 /* Use attributes from match with highest priority among
3920 * 'search_hl' and the match list. */
3921 search_attr = search_hl.attr_cur;
3922 cur = wp->w_match_head;
3923 shl_flag = FALSE;
3924 while (cur != NULL || shl_flag == FALSE)
3925 {
3926 if (shl_flag == FALSE
3927 && ((cur != NULL
3928 && cur->priority > SEARCH_HL_PRIORITY)
3929 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003930 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003931 shl = &search_hl;
3932 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003933 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003934 else
3935 shl = &cur->hl;
3936 if (shl->attr_cur != 0)
3937 search_attr = shl->attr_cur;
3938 if (shl != &search_hl && cur != NULL)
3939 cur = cur->next;
3940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942#endif
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003945 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003947 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3948 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003950 if (diff_hlf == HLF_TXD && ptr - line > change_end
3951 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003953 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003954 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3955 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
3957#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003958
3959 /* Decide which of the highlight attributes to use. */
3960 attr_pri = TRUE;
3961 if (area_attr != 0)
3962 char_attr = area_attr;
3963 else if (search_attr != 0)
3964 char_attr = search_attr;
3965#ifdef LINE_ATTR
3966 /* Use line_attr when not in the Visual or 'incsearch' area
3967 * (area_attr may be 0 when "noinvcur" is set). */
3968 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003969 || vcol < fromcol || vcol_prev < fromcol_prev
3970 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003971 char_attr = line_attr;
3972#endif
3973 else
3974 {
3975 attr_pri = FALSE;
3976#ifdef FEAT_SYN_HL
3977 if (has_syntax)
3978 char_attr = syntax_attr;
3979 else
3980#endif
3981 char_attr = 0;
3982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984
3985 /*
3986 * Get the next character to put on the screen.
3987 */
3988 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003989 * The "p_extra" points to the extra stuff that is inserted to
3990 * represent special characters (non-printable stuff) and other
3991 * things. When all characters are the same, c_extra is used.
3992 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3993 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3995 */
3996 if (n_extra > 0)
3997 {
3998 if (c_extra != NUL)
3999 {
4000 c = c_extra;
4001#ifdef FEAT_MBYTE
4002 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4003 if (enc_utf8 && (*mb_char2len)(c) > 1)
4004 {
4005 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004006 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004007 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009 else
4010 mb_utf8 = FALSE;
4011#endif
4012 }
4013 else
4014 {
4015 c = *p_extra;
4016#ifdef FEAT_MBYTE
4017 if (has_mbyte)
4018 {
4019 mb_c = c;
4020 if (enc_utf8)
4021 {
4022 /* If the UTF-8 character is more than one byte:
4023 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004024 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 mb_utf8 = FALSE;
4026 if (mb_l > n_extra)
4027 mb_l = 1;
4028 else if (mb_l > 1)
4029 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004030 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 }
4035 else
4036 {
4037 /* if this is a DBCS character, put it in "mb_c" */
4038 mb_l = MB_BYTE2LEN(c);
4039 if (mb_l >= n_extra)
4040 mb_l = 1;
4041 else if (mb_l > 1)
4042 mb_c = (c << 8) + p_extra[1];
4043 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004044 if (mb_l == 0) /* at the NUL at end-of-line */
4045 mb_l = 1;
4046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 /* If a double-width char doesn't fit display a '>' in the
4048 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004049 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050# ifdef FEAT_RIGHTLEFT
4051 wp->w_p_rl ? (col <= 0) :
4052# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004053 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 && (*mb_char2cells)(mb_c) == 2)
4055 {
4056 c = '>';
4057 mb_c = c;
4058 mb_l = 1;
4059 mb_utf8 = FALSE;
4060 multi_attr = hl_attr(HLF_AT);
4061 /* put the pointer back to output the double-width
4062 * character at the start of the next line. */
4063 ++n_extra;
4064 --p_extra;
4065 }
4066 else
4067 {
4068 n_extra -= mb_l - 1;
4069 p_extra += mb_l - 1;
4070 }
4071 }
4072#endif
4073 ++p_extra;
4074 }
4075 --n_extra;
4076 }
4077 else
4078 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004079 if (p_extra_free != NULL)
4080 {
4081 vim_free(p_extra_free);
4082 p_extra_free = NULL;
4083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 /*
4085 * Get a character from the line itself.
4086 */
4087 c = *ptr;
4088#ifdef FEAT_MBYTE
4089 if (has_mbyte)
4090 {
4091 mb_c = c;
4092 if (enc_utf8)
4093 {
4094 /* If the UTF-8 character is more than one byte: Decode it
4095 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004096 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 mb_utf8 = FALSE;
4098 if (mb_l > 1)
4099 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004100 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 /* Overlong encoded ASCII or ASCII with composing char
4102 * is displayed normally, except a NUL. */
4103 if (mb_c < 0x80)
4104 c = mb_c;
4105 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004106
4107 /* At start of the line we can have a composing char.
4108 * Draw it as a space with a composing char. */
4109 if (utf_iscomposing(mb_c))
4110 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004111 int i;
4112
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004113 for (i = Screen_mco - 1; i > 0; --i)
4114 u8cc[i] = u8cc[i - 1];
4115 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004116 mb_c = ' ';
4117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
4119
4120 if ((mb_l == 1 && c >= 0x80)
4121 || (mb_l >= 1 && mb_c == 0)
4122 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004123# ifdef UNICODE16
4124 || mb_c >= 0x10000
4125# endif
4126 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
4128 /*
4129 * Illegal UTF-8 byte: display as <xx>.
4130 * Non-BMP character : display as ? or fullwidth ?.
4131 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004132# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
4136 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004137# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (wp->w_p_rl) /* reverse */
4139 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004142# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 else if (utf_char2cells(mb_c) != 2)
4144 STRCPY(extra, "?");
4145 else
4146 /* 0xff1f in UTF-8: full-width '?' */
4147 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
4150 p_extra = extra;
4151 c = *p_extra;
4152 mb_c = mb_ptr2char_adv(&p_extra);
4153 mb_utf8 = (c >= 0x80);
4154 n_extra = (int)STRLEN(p_extra);
4155 c_extra = NUL;
4156 if (area_attr == 0 && search_attr == 0)
4157 {
4158 n_attr = n_extra + 1;
4159 extra_attr = hl_attr(HLF_8);
4160 saved_attr2 = char_attr; /* save current attr */
4161 }
4162 }
4163 else if (mb_l == 0) /* at the NUL at end-of-line */
4164 mb_l = 1;
4165#ifdef FEAT_ARABIC
4166 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4167 {
4168 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004169 int pc, pc1, nc;
4170 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
4172 /* The idea of what is the previous and next
4173 * character depends on 'rightleft'. */
4174 if (wp->w_p_rl)
4175 {
4176 pc = prev_c;
4177 pc1 = prev_c1;
4178 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004179 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 else
4182 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004183 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004185 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 prev_c = mb_c;
4188
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004189 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 else
4192 prev_c = mb_c;
4193#endif
4194 }
4195 else /* enc_dbcs */
4196 {
4197 mb_l = MB_BYTE2LEN(c);
4198 if (mb_l == 0) /* at the NUL at end-of-line */
4199 mb_l = 1;
4200 else if (mb_l > 1)
4201 {
4202 /* We assume a second byte below 32 is illegal.
4203 * Hopefully this is OK for all double-byte encodings!
4204 */
4205 if (ptr[1] >= 32)
4206 mb_c = (c << 8) + ptr[1];
4207 else
4208 {
4209 if (ptr[1] == NUL)
4210 {
4211 /* head byte at end of line */
4212 mb_l = 1;
4213 transchar_nonprint(extra, c);
4214 }
4215 else
4216 {
4217 /* illegal tail byte */
4218 mb_l = 2;
4219 STRCPY(extra, "XX");
4220 }
4221 p_extra = extra;
4222 n_extra = (int)STRLEN(extra) - 1;
4223 c_extra = NUL;
4224 c = *p_extra++;
4225 if (area_attr == 0 && search_attr == 0)
4226 {
4227 n_attr = n_extra + 1;
4228 extra_attr = hl_attr(HLF_8);
4229 saved_attr2 = char_attr; /* save current attr */
4230 }
4231 mb_c = c;
4232 }
4233 }
4234 }
4235 /* If a double-width char doesn't fit display a '>' in the
4236 * last column; the character is displayed at the start of the
4237 * next line. */
4238 if ((
4239# ifdef FEAT_RIGHTLEFT
4240 wp->w_p_rl ? (col <= 0) :
4241# endif
4242 (col >= W_WIDTH(wp) - 1))
4243 && (*mb_char2cells)(mb_c) == 2)
4244 {
4245 c = '>';
4246 mb_c = c;
4247 mb_utf8 = FALSE;
4248 mb_l = 1;
4249 multi_attr = hl_attr(HLF_AT);
4250 /* Put pointer back so that the character will be
4251 * displayed at the start of the next line. */
4252 --ptr;
4253 }
4254 else if (*ptr != NUL)
4255 ptr += mb_l - 1;
4256
4257 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004258 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004259 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004260 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004263 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 c = ' ';
4265 if (area_attr == 0 && search_attr == 0)
4266 {
4267 n_attr = n_extra + 1;
4268 extra_attr = hl_attr(HLF_AT);
4269 saved_attr2 = char_attr; /* save current attr */
4270 }
4271 mb_c = c;
4272 mb_utf8 = FALSE;
4273 mb_l = 1;
4274 }
4275
4276 }
4277#endif
4278 ++ptr;
4279
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004280 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004281 if (wp->w_p_list && (c == 160
4282#ifdef FEAT_MBYTE
4283 || (mb_utf8 && mb_c == 160)
4284#endif
4285 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004286 {
4287 c = lcs_nbsp;
4288 if (area_attr == 0 && search_attr == 0)
4289 {
4290 n_attr = 1;
4291 extra_attr = hl_attr(HLF_8);
4292 saved_attr2 = char_attr; /* save current attr */
4293 }
4294#ifdef FEAT_MBYTE
4295 mb_c = c;
4296 if (enc_utf8 && (*mb_char2len)(c) > 1)
4297 {
4298 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004299 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004300 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004301 }
4302 else
4303 mb_utf8 = FALSE;
4304#endif
4305 }
4306
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 if (extra_check)
4308 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004309#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004310 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004311#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004312
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004313#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 /* Get syntax attribute, unless still at the start of the line
4315 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004316 v = (long)(ptr - line);
4317 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
4319 /* Get the syntax attribute for the character. If there
4320 * is an error, disable syntax highlighting. */
4321 save_did_emsg = did_emsg;
4322 did_emsg = FALSE;
4323
Bram Moolenaar217ad922005-03-20 22:37:15 +00004324 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004325# ifdef FEAT_SPELL
4326 has_spell ? &can_spell :
4327# endif
4328 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329
4330 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004331 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004332 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004333 has_syntax = FALSE;
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 else
4336 did_emsg = save_did_emsg;
4337
4338 /* Need to get the line again, a multi-line regexp may
4339 * have made it invalid. */
4340 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4341 ptr = line + v;
4342
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004343 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004345 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004346 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004347# ifdef FEAT_CONCEAL
4348 /* no concealing past the end of the line, it interferes
4349 * with line highlighting */
4350 if (c == NUL)
4351 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004352 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004353 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004356#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004357
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004358#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004359 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004360 * Only do this when there is no syntax highlighting, the
4361 * @Spell cluster is not used or the current syntax item
4362 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004363 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004364 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004365 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004366# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004367 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004368 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004369# endif
4370 if (c != 0 && (
4371# ifdef FEAT_SYN_HL
4372 !has_syntax ||
4373# endif
4374 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004375 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004376 char_u *prev_ptr, *p;
4377 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004378 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004379# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004380 if (has_mbyte)
4381 {
4382 prev_ptr = ptr - mb_l;
4383 v -= mb_l - 1;
4384 }
4385 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004386# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004387 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004388
4389 /* Use nextline[] if possible, it has the start of the
4390 * next line concatenated. */
4391 if ((prev_ptr - line) - nextlinecol >= 0)
4392 p = nextline + (prev_ptr - line) - nextlinecol;
4393 else
4394 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004395 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004396 len = spell_check(wp, p, &spell_hlf, &cap_col,
4397 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004398 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004399
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004400 /* In Insert mode only highlight a word that
4401 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004402 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004403 && (State & INSERT) != 0
4404 && wp->w_cursor.lnum == lnum
4405 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004406 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004407 && wp->w_cursor.col < (colnr_T)word_end)
4408 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004409 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004410 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004411 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004412
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004413 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004414 && (p - nextline) + len > nextline_idx)
4415 {
4416 /* Remember that the good word continues at the
4417 * start of the next line. */
4418 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004419 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004420 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004421
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004422 /* Turn index into actual attributes. */
4423 if (spell_hlf != HLF_COUNT)
4424 spell_attr = highlight_attr[spell_hlf];
4425
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004426 if (cap_col > 0)
4427 {
4428 if (p != prev_ptr
4429 && (p - nextline) + cap_col >= nextline_idx)
4430 {
4431 /* Remember that the word in the next line
4432 * must start with a capital. */
4433 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004434 cap_col = (int)((p - nextline) + cap_col
4435 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004436 }
4437 else
4438 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004439 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004440 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004441 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004442 }
4443 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004444 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004445 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004446 char_attr = hl_combine_attr(char_attr, spell_attr);
4447 else
4448 char_attr = hl_combine_attr(spell_attr, char_attr);
4449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450#endif
4451#ifdef FEAT_LINEBREAK
4452 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004453 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004455 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004457 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458# ifdef FEAT_MBYTE
4459 has_mbyte ? mb_l :
4460# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004461 1);
4462 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004463 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004464 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004465 if (c == TAB && n_extra + col > W_WIDTH(wp))
4466 n_extra = (int)wp->w_buffer->b_p_ts
4467 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4468
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 c_extra = ' ';
4470 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004471 {
4472#ifdef FEAT_CONCEAL
4473 if (c == TAB)
4474 /* See "Tab alignment" below. */
4475 FIX_FOR_BOGUSCOLS;
4476#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004477 if (!wp->w_p_list)
4478 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481#endif
4482
4483 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4484 {
4485 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004486 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 n_attr = 1;
4489 extra_attr = hl_attr(HLF_8);
4490 saved_attr2 = char_attr; /* save current attr */
4491 }
4492#ifdef FEAT_MBYTE
4493 mb_c = c;
4494 if (enc_utf8 && (*mb_char2len)(c) > 1)
4495 {
4496 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004497 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004498 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 else
4501 mb_utf8 = FALSE;
4502#endif
4503 }
4504 }
4505
4506 /*
4507 * Handling of non-printable characters.
4508 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004509 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 {
4511 /*
4512 * when getting a character from the file, we may have to
4513 * turn it into something else on the way to putting it
4514 * into "ScreenLines".
4515 */
4516 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4517 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004518 int tab_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004520 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004521 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004522#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004523 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004524#endif
4525 /* tab amount depends on current column */
4526 n_extra = tab_len;
4527#ifdef FEAT_LINEBREAK
4528 else
4529 {
4530 char_u *p;
4531 int len = n_extra;
4532 int i;
4533 int saved_nextra = n_extra;
4534
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004535#ifdef FEAT_CONCEAL
4536 if (is_concealing && vcol_off > 0)
4537 /* there are characters to conceal */
4538 tab_len += vcol_off;
4539#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004540 /* if n_extra > 0, it gives the number of chars, to
4541 * use for a tab, else we need to calculate the width
4542 * for a tab */
4543#ifdef FEAT_MBYTE
4544 len = (tab_len * mb_char2len(lcs_tab2));
4545 if (n_extra > 0)
4546 len += n_extra - tab_len;
4547#endif
4548 c = lcs_tab1;
4549 p = alloc((unsigned)(len + 1));
4550 vim_memset(p, ' ', len);
4551 p[len] = NUL;
4552 p_extra_free = p;
4553 for (i = 0; i < tab_len; i++)
4554 {
4555#ifdef FEAT_MBYTE
4556 mb_char2bytes(lcs_tab2, p);
4557 p += mb_char2len(lcs_tab2);
4558 n_extra += mb_char2len(lcs_tab2)
4559 - (saved_nextra > 0 ? 1 : 0);
4560#else
4561 p[i] = lcs_tab2;
4562#endif
4563 }
4564 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004565#ifdef FEAT_CONCEAL
4566 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4567 * macro below, so need to adjust for that here */
4568 if (is_concealing && vcol_off > 0)
4569 n_extra -= vcol_off;
4570#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004571 }
4572#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004573#ifdef FEAT_CONCEAL
4574 /* Tab alignment should be identical regardless of
4575 * 'conceallevel' value. So tab compensates of all
4576 * previous concealed characters, and thus resets vcol_off
4577 * and boguscols accumulated so far in the line. Note that
4578 * the tab can be longer than 'tabstop' when there
4579 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004580 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582#ifdef FEAT_MBYTE
4583 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4584#endif
4585 if (wp->w_p_list)
4586 {
4587 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004588#ifdef FEAT_LINEBREAK
4589 if (wp->w_p_lbr)
4590 c_extra = NUL; /* using p_extra from above */
4591 else
4592#endif
4593 c_extra = lcs_tab2;
4594 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 extra_attr = hl_attr(HLF_8);
4596 saved_attr2 = char_attr; /* save current attr */
4597#ifdef FEAT_MBYTE
4598 mb_c = c;
4599 if (enc_utf8 && (*mb_char2len)(c) > 1)
4600 {
4601 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004602 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004603 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605#endif
4606 }
4607 else
4608 {
4609 c_extra = ' ';
4610 c = ' ';
4611 }
4612 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004613 else if (c == NUL
4614 && ((wp->w_p_list && lcs_eol > 0)
4615 || ((fromcol >= 0 || fromcol_prev >= 0)
4616 && tocol > vcol
4617 && VIsual_mode != Ctrl_V
4618 && (
4619# ifdef FEAT_RIGHTLEFT
4620 wp->w_p_rl ? (col >= 0) :
4621# endif
4622 (col < W_WIDTH(wp)))
4623 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004624 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004625 && (colnr_T)vcol == wp->w_virtcol)))
4626 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004628 /* Display a '$' after the line or highlight an extra
4629 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4631 /* For a diff line the highlighting continues after the
4632 * "$". */
4633 if (
4634# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004635 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636# ifdef LINE_ATTR
4637 &&
4638# endif
4639# endif
4640# ifdef LINE_ATTR
4641 line_attr == 0
4642# endif
4643 )
4644#endif
4645 {
4646#ifdef FEAT_VIRTUALEDIT
4647 /* In virtualedit, visual selections may extend
4648 * beyond end of line. */
4649 if (area_highlighting && virtual_active()
4650 && tocol != MAXCOL && vcol < tocol)
4651 n_extra = 0;
4652 else
4653#endif
4654 {
4655 p_extra = at_end_str;
4656 n_extra = 1;
4657 c_extra = NUL;
4658 }
4659 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004660 if (wp->w_p_list)
4661 c = lcs_eol;
4662 else
4663 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 lcs_eol_one = -1;
4665 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004666 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
4668 extra_attr = hl_attr(HLF_AT);
4669 n_attr = 1;
4670 }
4671#ifdef FEAT_MBYTE
4672 mb_c = c;
4673 if (enc_utf8 && (*mb_char2len)(c) > 1)
4674 {
4675 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004676 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004677 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 else
4680 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4681#endif
4682 }
4683 else if (c != NUL)
4684 {
4685 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004686 if (n_extra == 0)
4687 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688#ifdef FEAT_RIGHTLEFT
4689 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4690 rl_mirror(p_extra); /* reverse "<12>" */
4691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004693#ifdef FEAT_LINEBREAK
4694 if (wp->w_p_lbr)
4695 {
4696 char_u *p;
4697
4698 c = *p_extra;
4699 p = alloc((unsigned)n_extra + 1);
4700 vim_memset(p, ' ', n_extra);
4701 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4702 p[n_extra] = NUL;
4703 p_extra_free = p_extra = p;
4704 }
4705 else
4706#endif
4707 {
4708 n_extra = byte2cells(c) - 1;
4709 c = *p_extra++;
4710 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004711 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
4713 n_attr = n_extra + 1;
4714 extra_attr = hl_attr(HLF_8);
4715 saved_attr2 = char_attr; /* save current attr */
4716 }
4717#ifdef FEAT_MBYTE
4718 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4719#endif
4720 }
4721#ifdef FEAT_VIRTUALEDIT
4722 else if (VIsual_active
4723 && (VIsual_mode == Ctrl_V
4724 || VIsual_mode == 'v')
4725 && virtual_active()
4726 && tocol != MAXCOL
4727 && vcol < tocol
4728 && (
4729# ifdef FEAT_RIGHTLEFT
4730 wp->w_p_rl ? (col >= 0) :
4731# endif
4732 (col < W_WIDTH(wp))))
4733 {
4734 c = ' ';
4735 --ptr; /* put it back at the NUL */
4736 }
4737#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004738#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 else if ((
4740# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004741 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 ) && (
4745# ifdef FEAT_RIGHTLEFT
4746 wp->w_p_rl ? (col >= 0) :
4747# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004748 (col
4749# ifdef FEAT_CONCEAL
4750 - boguscols
4751# endif
4752 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 /* Highlight until the right side of the window */
4755 c = ' ';
4756 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004757
4758 /* Remember we do the char for line highlighting. */
4759 ++did_line_attr;
4760
4761 /* don't do search HL for the rest of the line */
4762 if (line_attr != 0 && char_attr == search_attr && col > 0)
4763 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764# ifdef FEAT_DIFF
4765 if (diff_hlf == HLF_TXD)
4766 {
4767 diff_hlf = HLF_CHD;
4768 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004771 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4772 char_attr = hl_combine_attr(char_attr,
4773 hl_attr(HLF_CUL));
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776# endif
4777 }
4778#endif
4779 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004780
4781#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004782 if ( wp->w_p_cole > 0
4783 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4784 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004785 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004786 && !(lnum_in_visual_area
4787 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004788 {
4789 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004790 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004791 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4792 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004793 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004794 /* First time at this concealed item: display one
4795 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004796 if (syn_get_sub_char() != NUL)
4797 c = syn_get_sub_char();
4798 else if (lcs_conceal != NUL)
4799 c = lcs_conceal;
4800 else
4801 c = ' ';
4802
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004803 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004804
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004805 if (n_extra > 0)
4806 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004807 vcol += n_extra;
4808 if (wp->w_p_wrap && n_extra > 0)
4809 {
4810# ifdef FEAT_RIGHTLEFT
4811 if (wp->w_p_rl)
4812 {
4813 col -= n_extra;
4814 boguscols -= n_extra;
4815 }
4816 else
4817# endif
4818 {
4819 boguscols += n_extra;
4820 col += n_extra;
4821 }
4822 }
4823 n_extra = 0;
4824 n_attr = 0;
4825 }
4826 else if (n_skip == 0)
4827 {
4828 is_concealing = TRUE;
4829 n_skip = 1;
4830 }
4831# ifdef FEAT_MBYTE
4832 mb_c = c;
4833 if (enc_utf8 && (*mb_char2len)(c) > 1)
4834 {
4835 mb_utf8 = TRUE;
4836 u8cc[0] = 0;
4837 c = 0xc0;
4838 }
4839 else
4840 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4841# endif
4842 }
4843 else
4844 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004845 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004846 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004847 }
4848#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004851#ifdef FEAT_CONCEAL
4852 /* In the cursor line and we may be concealing characters: correct
4853 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004854 if (!did_wcol && draw_state == WL_LINE
4855 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004856 && conceal_cursor_line(wp)
4857 && (int)wp->w_virtcol <= vcol + n_skip)
4858 {
4859 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004860 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004861 did_wcol = TRUE;
4862 }
4863#endif
4864
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 /* Don't override visual selection highlighting. */
4866 if (n_attr > 0
4867 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004868 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 char_attr = extra_attr;
4870
Bram Moolenaar81695252004-12-29 20:58:21 +00004871#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 /* XIM don't send preedit_start and preedit_end, but they send
4873 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4874 * im_is_preediting() here. */
4875 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004876 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 && (State & INSERT)
4878 && !p_imdisable
4879 && im_is_preediting()
4880 && draw_state == WL_LINE)
4881 {
4882 colnr_T tcol;
4883
4884 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004885 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 else
4887 tcol = preedit_end_col;
4888 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4889 {
4890 if (feedback_old_attr < 0)
4891 {
4892 feedback_col = 0;
4893 feedback_old_attr = char_attr;
4894 }
4895 char_attr = im_get_feedback_attr(feedback_col);
4896 if (char_attr < 0)
4897 char_attr = feedback_old_attr;
4898 feedback_col++;
4899 }
4900 else if (feedback_old_attr >= 0)
4901 {
4902 char_attr = feedback_old_attr;
4903 feedback_old_attr = -1;
4904 feedback_col = 0;
4905 }
4906 }
4907#endif
4908 /*
4909 * Handle the case where we are in column 0 but not on the first
4910 * character of the line and the user wants us to show us a
4911 * special character (via 'listchars' option "precedes:<char>".
4912 */
4913 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02004914 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4916#ifdef FEAT_DIFF
4917 && filler_todo <= 0
4918#endif
4919 && draw_state > WL_NR
4920 && c != NUL)
4921 {
4922 c = lcs_prec;
4923 lcs_prec_todo = NUL;
4924#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004925 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4926 {
4927 /* Double-width character being overwritten by the "precedes"
4928 * character, need to fill up half the character. */
4929 c_extra = MB_FILLER_CHAR;
4930 n_extra = 1;
4931 n_attr = 2;
4932 extra_attr = hl_attr(HLF_AT);
4933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 mb_c = c;
4935 if (enc_utf8 && (*mb_char2len)(c) > 1)
4936 {
4937 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004938 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004939 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 else
4942 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4943#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004944 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
4946 saved_attr3 = char_attr; /* save current attr */
4947 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4948 n_attr3 = 1;
4949 }
4950 }
4951
4952 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004953 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004955 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004956#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004957 || did_line_attr == 1
4958#endif
4959 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004961#ifdef FEAT_SEARCH_EXTRA
4962 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004963
4964 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004965 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004966 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004967#endif
4968
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004969 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 * highlight match at end of line. If it's beyond the last
4971 * char on the screen, just overwrite that one (tricky!) Not
4972 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004973#ifdef FEAT_SEARCH_EXTRA
4974 prevcol_hl_flag = FALSE;
4975 if (prevcol == (long)search_hl.startcol)
4976 prevcol_hl_flag = TRUE;
4977 else
4978 {
4979 cur = wp->w_match_head;
4980 while (cur != NULL)
4981 {
4982 if (prevcol == (long)cur->hl.startcol)
4983 {
4984 prevcol_hl_flag = TRUE;
4985 break;
4986 }
4987 cur = cur->next;
4988 }
4989 }
4990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004992 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004993 && (VIsual_mode != Ctrl_V
4994 || lnum == VIsual.lnum
4995 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004996 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997#ifdef FEAT_SEARCH_EXTRA
4998 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004999 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005000# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005001 && did_line_attr <= 1
5002# endif
5003 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004#endif
5005 ))
5006 {
5007 int n = 0;
5008
5009#ifdef FEAT_RIGHTLEFT
5010 if (wp->w_p_rl)
5011 {
5012 if (col < 0)
5013 n = 1;
5014 }
5015 else
5016#endif
5017 {
5018 if (col >= W_WIDTH(wp))
5019 n = -1;
5020 }
5021 if (n != 0)
5022 {
5023 /* At the window boundary, highlight the last character
5024 * instead (better than nothing). */
5025 off += n;
5026 col += n;
5027 }
5028 else
5029 {
5030 /* Add a blank character to highlight. */
5031 ScreenLines[off] = ' ';
5032#ifdef FEAT_MBYTE
5033 if (enc_utf8)
5034 ScreenLinesUC[off] = 0;
5035#endif
5036 }
5037#ifdef FEAT_SEARCH_EXTRA
5038 if (area_attr == 0)
5039 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005040 /* Use attributes from match with highest priority among
5041 * 'search_hl' and the match list. */
5042 char_attr = search_hl.attr;
5043 cur = wp->w_match_head;
5044 shl_flag = FALSE;
5045 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005046 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005047 if (shl_flag == FALSE
5048 && ((cur != NULL
5049 && cur->priority > SEARCH_HL_PRIORITY)
5050 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005051 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005052 shl = &search_hl;
5053 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005054 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005055 else
5056 shl = &cur->hl;
5057 if ((ptr - line) - 1 == (long)shl->startcol)
5058 char_attr = shl->attr;
5059 if (shl != &search_hl && cur != NULL)
5060 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063#endif
5064 ScreenAttrs[off] = char_attr;
5065#ifdef FEAT_RIGHTLEFT
5066 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005069 --off;
5070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 else
5072#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005075 ++off;
5076 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005077 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005078#ifdef FEAT_SYN_HL
5079 eol_hl_off = 1;
5080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaar91170f82006-05-05 21:15:17 +00005084 /*
5085 * At end of the text line.
5086 */
5087 if (c == NUL)
5088 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005089#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005090 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5091 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005092 {
5093 /* highlight last char after line */
5094 --col;
5095 --off;
5096 --vcol;
5097 }
5098
Bram Moolenaar1a384422010-07-14 19:53:30 +02005099 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005100 if (wp->w_p_wrap)
5101 v = wp->w_skipcol;
5102 else
5103 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005104
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005105 /* check if line ends before left margin */
5106 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005107 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005108#ifdef FEAT_CONCEAL
5109 /* Get rid of the boguscols now, we want to draw until the right
5110 * edge for 'cursorcolumn'. */
5111 col -= boguscols;
5112 boguscols = 0;
5113#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005114
5115 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005116 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005117
5118 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005119 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5120 && (int)wp->w_virtcol <
5121 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005122 && lnum != wp->w_cursor.lnum)
5123 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005124# ifdef FEAT_RIGHTLEFT
5125 && !wp->w_p_rl
5126# endif
5127 )
5128 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005129 int rightmost_vcol = 0;
5130 int i;
5131
5132 if (wp->w_p_cuc)
5133 rightmost_vcol = wp->w_virtcol;
5134 if (draw_color_col)
5135 /* determine rightmost colorcolumn to possibly draw */
5136 for (i = 0; color_cols[i] >= 0; ++i)
5137 if (rightmost_vcol < color_cols[i])
5138 rightmost_vcol = color_cols[i];
5139
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005140 while (col < W_WIDTH(wp))
5141 {
5142 ScreenLines[off] = ' ';
5143#ifdef FEAT_MBYTE
5144 if (enc_utf8)
5145 ScreenLinesUC[off] = 0;
5146#endif
5147 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005148 if (draw_color_col)
5149 draw_color_col = advance_color_col(VCOL_HLC,
5150 &color_cols);
5151
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005152 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005153 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005154 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005155 ScreenAttrs[off++] = hl_attr(HLF_MC);
5156 else
5157 ScreenAttrs[off++] = 0;
5158
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005159 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005160 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005161
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005162 ++vcol;
5163 }
5164 }
5165#endif
5166
Bram Moolenaar860cae12010-06-05 23:22:07 +02005167 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5168 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 row++;
5170
5171 /*
5172 * Update w_cline_height and w_cline_folded if the cursor line was
5173 * updated (saves a call to plines() later).
5174 */
5175 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5176 {
5177 curwin->w_cline_row = startrow;
5178 curwin->w_cline_height = row - startrow;
5179#ifdef FEAT_FOLDING
5180 curwin->w_cline_folded = FALSE;
5181#endif
5182 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5183 }
5184
5185 break;
5186 }
5187
5188 /* line continues beyond line end */
5189 if (lcs_ext
5190 && !wp->w_p_wrap
5191#ifdef FEAT_DIFF
5192 && filler_todo <= 0
5193#endif
5194 && (
5195#ifdef FEAT_RIGHTLEFT
5196 wp->w_p_rl ? col == 0 :
5197#endif
5198 col == W_WIDTH(wp) - 1)
5199 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005200 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5202 {
5203 c = lcs_ext;
5204 char_attr = hl_attr(HLF_AT);
5205#ifdef FEAT_MBYTE
5206 mb_c = c;
5207 if (enc_utf8 && (*mb_char2len)(c) > 1)
5208 {
5209 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005210 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005211 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213 else
5214 mb_utf8 = FALSE;
5215#endif
5216 }
5217
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005218#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005219 /* advance to the next 'colorcolumn' */
5220 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005221 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005222
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005223 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005224 * highlight the cursor position itself.
5225 * Also highlight the 'colorcolumn' if it is different than
5226 * 'cursorcolumn' */
5227 vcol_save_attr = -1;
5228 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005229 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005230 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005231 && lnum != wp->w_cursor.lnum)
5232 {
5233 vcol_save_attr = char_attr;
5234 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5235 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005236 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005237 {
5238 vcol_save_attr = char_attr;
5239 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5240 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005241 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005242#endif
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 /*
5245 * Store character to be displayed.
5246 * Skip characters that are left of the screen for 'nowrap'.
5247 */
5248 vcol_prev = vcol;
5249 if (draw_state < WL_LINE || n_skip <= 0)
5250 {
5251 /*
5252 * Store the character.
5253 */
5254#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5255 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5256 {
5257 /* A double-wide character is: put first halve in left cell. */
5258 --off;
5259 --col;
5260 }
5261#endif
5262 ScreenLines[off] = c;
5263#ifdef FEAT_MBYTE
5264 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005265 {
5266 if ((mb_c & 0xff00) == 0x8e00)
5267 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 else if (enc_utf8)
5271 {
5272 if (mb_utf8)
5273 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005274 int i;
5275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005277 if ((c & 0xff) == 0)
5278 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005279 for (i = 0; i < Screen_mco; ++i)
5280 {
5281 ScreenLinesC[i][off] = u8cc[i];
5282 if (u8cc[i] == 0)
5283 break;
5284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 else
5287 ScreenLinesUC[off] = 0;
5288 }
5289 if (multi_attr)
5290 {
5291 ScreenAttrs[off] = multi_attr;
5292 multi_attr = 0;
5293 }
5294 else
5295#endif
5296 ScreenAttrs[off] = char_attr;
5297
5298#ifdef FEAT_MBYTE
5299 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5300 {
5301 /* Need to fill two screen columns. */
5302 ++off;
5303 ++col;
5304 if (enc_utf8)
5305 /* UTF-8: Put a 0 in the second screen char. */
5306 ScreenLines[off] = 0;
5307 else
5308 /* DBCS: Put second byte in the second screen char. */
5309 ScreenLines[off] = mb_c & 0xff;
5310 ++vcol;
5311 /* When "tocol" is halfway a character, set it to the end of
5312 * the character, otherwise highlighting won't stop. */
5313 if (tocol == vcol)
5314 ++tocol;
5315#ifdef FEAT_RIGHTLEFT
5316 if (wp->w_p_rl)
5317 {
5318 /* now it's time to backup one cell */
5319 --off;
5320 --col;
5321 }
5322#endif
5323 }
5324#endif
5325#ifdef FEAT_RIGHTLEFT
5326 if (wp->w_p_rl)
5327 {
5328 --off;
5329 --col;
5330 }
5331 else
5332#endif
5333 {
5334 ++off;
5335 ++col;
5336 }
5337 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005338#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005339 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005340 {
5341 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005342 ++vcol_off;
5343 if (n_extra > 0)
5344 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005345 if (wp->w_p_wrap)
5346 {
5347 /*
5348 * Special voodoo required if 'wrap' is on.
5349 *
5350 * Advance the column indicator to force the line
5351 * drawing to wrap early. This will make the line
5352 * take up the same screen space when parts are concealed,
5353 * so that cursor line computations aren't messed up.
5354 *
5355 * To avoid the fictitious advance of 'col' causing
5356 * trailing junk to be written out of the screen line
5357 * we are building, 'boguscols' keeps track of the number
5358 * of bad columns we have advanced.
5359 */
5360 if (n_extra > 0)
5361 {
5362 vcol += n_extra;
5363# ifdef FEAT_RIGHTLEFT
5364 if (wp->w_p_rl)
5365 {
5366 col -= n_extra;
5367 boguscols -= n_extra;
5368 }
5369 else
5370# endif
5371 {
5372 col += n_extra;
5373 boguscols += n_extra;
5374 }
5375 n_extra = 0;
5376 n_attr = 0;
5377 }
5378
5379
5380# ifdef FEAT_MBYTE
5381 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5382 {
5383 /* Need to fill two screen columns. */
5384# ifdef FEAT_RIGHTLEFT
5385 if (wp->w_p_rl)
5386 {
5387 --boguscols;
5388 --col;
5389 }
5390 else
5391# endif
5392 {
5393 ++boguscols;
5394 ++col;
5395 }
5396 }
5397# endif
5398
5399# ifdef FEAT_RIGHTLEFT
5400 if (wp->w_p_rl)
5401 {
5402 --boguscols;
5403 --col;
5404 }
5405 else
5406# endif
5407 {
5408 ++boguscols;
5409 ++col;
5410 }
5411 }
5412 else
5413 {
5414 if (n_extra > 0)
5415 {
5416 vcol += n_extra;
5417 n_extra = 0;
5418 n_attr = 0;
5419 }
5420 }
5421
5422 }
5423#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 else
5425 --n_skip;
5426
Bram Moolenaar64486672010-05-16 15:46:46 +02005427 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5428 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005429 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#ifdef FEAT_DIFF
5431 && filler_todo <= 0
5432#endif
5433 )
5434 ++vcol;
5435
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005436#ifdef FEAT_SYN_HL
5437 if (vcol_save_attr >= 0)
5438 char_attr = vcol_save_attr;
5439#endif
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 /* restore attributes after "predeces" in 'listchars' */
5442 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5443 char_attr = saved_attr3;
5444
5445 /* restore attributes after last 'listchars' or 'number' char */
5446 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5447 char_attr = saved_attr2;
5448
5449 /*
5450 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005451 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 */
5453 if ((
5454#ifdef FEAT_RIGHTLEFT
5455 wp->w_p_rl ? (col < 0) :
5456#endif
5457 (col >= W_WIDTH(wp)))
5458 && (*ptr != NUL
5459#ifdef FEAT_DIFF
5460 || filler_todo > 0
5461#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005462 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5464 )
5465 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005466#ifdef FEAT_CONCEAL
5467 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5468 (int)W_WIDTH(wp), wp->w_p_rl);
5469 boguscols = 0;
5470#else
5471 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5472 (int)W_WIDTH(wp), wp->w_p_rl);
5473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 ++row;
5475 ++screen_row;
5476
5477 /* When not wrapping and finished diff lines, or when displayed
5478 * '$' and highlighting until last column, break here. */
5479 if ((!wp->w_p_wrap
5480#ifdef FEAT_DIFF
5481 && filler_todo <= 0
5482#endif
5483 ) || lcs_eol_one == -1)
5484 break;
5485
5486 /* When the window is too narrow draw all "@" lines. */
5487 if (draw_state != WL_LINE
5488#ifdef FEAT_DIFF
5489 && filler_todo <= 0
5490#endif
5491 )
5492 {
5493 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5494#ifdef FEAT_VERTSPLIT
5495 draw_vsep_win(wp, row);
5496#endif
5497 row = endrow;
5498 }
5499
5500 /* When line got too long for screen break here. */
5501 if (row == endrow)
5502 {
5503 ++row;
5504 break;
5505 }
5506
5507 if (screen_cur_row == screen_row - 1
5508#ifdef FEAT_DIFF
5509 && filler_todo <= 0
5510#endif
5511 && W_WIDTH(wp) == Columns)
5512 {
5513 /* Remember that the line wraps, used for modeless copy. */
5514 LineWraps[screen_row - 1] = TRUE;
5515
5516 /*
5517 * Special trick to make copy/paste of wrapped lines work with
5518 * xterm/screen: write an extra character beyond the end of
5519 * the line. This will work with all terminal types
5520 * (regardless of the xn,am settings).
5521 * Only do this on a fast tty.
5522 * Only do this if the cursor is on the current line
5523 * (something has been written in it).
5524 * Don't do this for the GUI.
5525 * Don't do this for double-width characters.
5526 * Don't do this for a window not at the right screen border.
5527 */
5528 if (p_tf
5529#ifdef FEAT_GUI
5530 && !gui.in_use
5531#endif
5532#ifdef FEAT_MBYTE
5533 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005534 && ((*mb_off2cells)(LineOffset[screen_row],
5535 LineOffset[screen_row] + screen_Columns)
5536 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005538 + (int)Columns - 2,
5539 LineOffset[screen_row] + screen_Columns)
5540 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#endif
5542 )
5543 {
5544 /* First make sure we are at the end of the screen line,
5545 * then output the same character again to let the
5546 * terminal know about the wrap. If the terminal doesn't
5547 * auto-wrap, we overwrite the character. */
5548 if (screen_cur_col != W_WIDTH(wp))
5549 screen_char(LineOffset[screen_row - 1]
5550 + (unsigned)Columns - 1,
5551 screen_row - 1, (int)(Columns - 1));
5552
5553#ifdef FEAT_MBYTE
5554 /* When there is a multi-byte character, just output a
5555 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005556 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5557 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 out_char(' ');
5559 else
5560#endif
5561 out_char(ScreenLines[LineOffset[screen_row - 1]
5562 + (Columns - 1)]);
5563 /* force a redraw of the first char on the next line */
5564 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5565 screen_start(); /* don't know where cursor is now */
5566 }
5567 }
5568
5569 col = 0;
5570 off = (unsigned)(current_ScreenLine - ScreenLines);
5571#ifdef FEAT_RIGHTLEFT
5572 if (wp->w_p_rl)
5573 {
5574 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5575 off += col;
5576 }
5577#endif
5578
5579 /* reset the drawing state for the start of a wrapped line */
5580 draw_state = WL_START;
5581 saved_n_extra = n_extra;
5582 saved_p_extra = p_extra;
5583 saved_c_extra = c_extra;
5584 saved_char_attr = char_attr;
5585 n_extra = 0;
5586 lcs_prec_todo = lcs_prec;
5587#ifdef FEAT_LINEBREAK
5588# ifdef FEAT_DIFF
5589 if (filler_todo <= 0)
5590# endif
5591 need_showbreak = TRUE;
5592#endif
5593#ifdef FEAT_DIFF
5594 --filler_todo;
5595 /* When the filler lines are actually below the last line of the
5596 * file, don't draw the line itself, break here. */
5597 if (filler_todo == 0 && wp->w_botfill)
5598 break;
5599#endif
5600 }
5601
5602 } /* for every character in the line */
5603
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005604#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005605 /* After an empty line check first word for capital. */
5606 if (*skipwhite(line) == NUL)
5607 {
5608 capcol_lnum = lnum + 1;
5609 cap_col = 0;
5610 }
5611#endif
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 return row;
5614}
5615
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005616#ifdef FEAT_MBYTE
5617static int comp_char_differs __ARGS((int, int));
5618
5619/*
5620 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005621 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005622 */
5623 static int
5624comp_char_differs(off_from, off_to)
5625 int off_from;
5626 int off_to;
5627{
5628 int i;
5629
5630 for (i = 0; i < Screen_mco; ++i)
5631 {
5632 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5633 return TRUE;
5634 if (ScreenLinesC[i][off_from] == 0)
5635 break;
5636 }
5637 return FALSE;
5638}
5639#endif
5640
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641/*
5642 * Check whether the given character needs redrawing:
5643 * - the (first byte of the) character is different
5644 * - the attributes are different
5645 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005646 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 */
5648 static int
5649char_needs_redraw(off_from, off_to, cols)
5650 int off_from;
5651 int off_to;
5652 int cols;
5653{
5654 if (cols > 0
5655 && ((ScreenLines[off_from] != ScreenLines[off_to]
5656 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5657
5658#ifdef FEAT_MBYTE
5659 || (enc_dbcs != 0
5660 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5661 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5662 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5663 : (cols > 1 && ScreenLines[off_from + 1]
5664 != ScreenLines[off_to + 1])))
5665 || (enc_utf8
5666 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5667 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005668 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005669 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5670 && ScreenLines[off_from + 1]
5671 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672#endif
5673 ))
5674 return TRUE;
5675 return FALSE;
5676}
5677
5678/*
5679 * Move one "cooked" screen line to the screen, but only the characters that
5680 * have actually changed. Handle insert/delete character.
5681 * "coloff" gives the first column on the screen for this line.
5682 * "endcol" gives the columns where valid characters are.
5683 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5684 * needs to be cleared, negative otherwise.
5685 * "rlflag" is TRUE in a rightleft window:
5686 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5687 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5688 */
5689 static void
5690screen_line(row, coloff, endcol, clear_width
5691#ifdef FEAT_RIGHTLEFT
5692 , rlflag
5693#endif
5694 )
5695 int row;
5696 int coloff;
5697 int endcol;
5698 int clear_width;
5699#ifdef FEAT_RIGHTLEFT
5700 int rlflag;
5701#endif
5702{
5703 unsigned off_from;
5704 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005705#ifdef FEAT_MBYTE
5706 unsigned max_off_from;
5707 unsigned max_off_to;
5708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 int col = 0;
5710#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5711 int hl;
5712#endif
5713 int force = FALSE; /* force update rest of the line */
5714 int redraw_this /* bool: does character need redraw? */
5715#ifdef FEAT_GUI
5716 = TRUE /* For GUI when while-loop empty */
5717#endif
5718 ;
5719 int redraw_next; /* redraw_this for next character */
5720#ifdef FEAT_MBYTE
5721 int clear_next = FALSE;
5722 int char_cells; /* 1: normal char */
5723 /* 2: occupies two display cells */
5724# define CHAR_CELLS char_cells
5725#else
5726# define CHAR_CELLS 1
5727#endif
5728
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005729 /* Check for illegal row and col, just in case. */
5730 if (row >= Rows)
5731 row = Rows - 1;
5732 if (endcol > Columns)
5733 endcol = Columns;
5734
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735# ifdef FEAT_CLIPBOARD
5736 clip_may_clear_selection(row, row);
5737# endif
5738
5739 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5740 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005741#ifdef FEAT_MBYTE
5742 max_off_from = off_from + screen_Columns;
5743 max_off_to = LineOffset[row] + screen_Columns;
5744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
5746#ifdef FEAT_RIGHTLEFT
5747 if (rlflag)
5748 {
5749 /* Clear rest first, because it's left of the text. */
5750 if (clear_width > 0)
5751 {
5752 while (col <= endcol && ScreenLines[off_to] == ' '
5753 && ScreenAttrs[off_to] == 0
5754# ifdef FEAT_MBYTE
5755 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5756# endif
5757 )
5758 {
5759 ++off_to;
5760 ++col;
5761 }
5762 if (col <= endcol)
5763 screen_fill(row, row + 1, col + coloff,
5764 endcol + coloff + 1, ' ', ' ', 0);
5765 }
5766 col = endcol + 1;
5767 off_to = LineOffset[row] + col + coloff;
5768 off_from += col;
5769 endcol = (clear_width > 0 ? clear_width : -clear_width);
5770 }
5771#endif /* FEAT_RIGHTLEFT */
5772
5773 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5774
5775 while (col < endcol)
5776 {
5777#ifdef FEAT_MBYTE
5778 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005779 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 else
5781 char_cells = 1;
5782#endif
5783
5784 redraw_this = redraw_next;
5785 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5786 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5787
5788#ifdef FEAT_GUI
5789 /* If the next character was bold, then redraw the current character to
5790 * remove any pixels that might have spilt over into us. This only
5791 * happens in the GUI.
5792 */
5793 if (redraw_next && gui.in_use)
5794 {
5795 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005796 if (hl > HL_ALL)
5797 hl = syn_attr2attr(hl);
5798 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 redraw_this = TRUE;
5800 }
5801#endif
5802
5803 if (redraw_this)
5804 {
5805 /*
5806 * Special handling when 'xs' termcap flag set (hpterm):
5807 * Attributes for characters are stored at the position where the
5808 * cursor is when writing the highlighting code. The
5809 * start-highlighting code must be written with the cursor on the
5810 * first highlighted character. The stop-highlighting code must
5811 * be written with the cursor just after the last highlighted
5812 * character.
5813 * Overwriting a character doesn't remove it's highlighting. Need
5814 * to clear the rest of the line, and force redrawing it
5815 * completely.
5816 */
5817 if ( p_wiv
5818 && !force
5819#ifdef FEAT_GUI
5820 && !gui.in_use
5821#endif
5822 && ScreenAttrs[off_to] != 0
5823 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5824 {
5825 /*
5826 * Need to remove highlighting attributes here.
5827 */
5828 windgoto(row, col + coloff);
5829 out_str(T_CE); /* clear rest of this screen line */
5830 screen_start(); /* don't know where cursor is now */
5831 force = TRUE; /* force redraw of rest of the line */
5832 redraw_next = TRUE; /* or else next char would miss out */
5833
5834 /*
5835 * If the previous character was highlighted, need to stop
5836 * highlighting at this character.
5837 */
5838 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5839 {
5840 screen_attr = ScreenAttrs[off_to - 1];
5841 term_windgoto(row, col + coloff);
5842 screen_stop_highlight();
5843 }
5844 else
5845 screen_attr = 0; /* highlighting has stopped */
5846 }
5847#ifdef FEAT_MBYTE
5848 if (enc_dbcs != 0)
5849 {
5850 /* Check if overwriting a double-byte with a single-byte or
5851 * the other way around requires another character to be
5852 * redrawn. For UTF-8 this isn't needed, because comparing
5853 * ScreenLinesUC[] is sufficient. */
5854 if (char_cells == 1
5855 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005856 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {
5858 /* Writing a single-cell character over a double-cell
5859 * character: need to redraw the next cell. */
5860 ScreenLines[off_to + 1] = 0;
5861 redraw_next = TRUE;
5862 }
5863 else if (char_cells == 2
5864 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005865 && (*mb_off2cells)(off_to, max_off_to) == 1
5866 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {
5868 /* Writing the second half of a double-cell character over
5869 * a double-cell character: need to redraw the second
5870 * cell. */
5871 ScreenLines[off_to + 2] = 0;
5872 redraw_next = TRUE;
5873 }
5874
5875 if (enc_dbcs == DBCS_JPNU)
5876 ScreenLines2[off_to] = ScreenLines2[off_from];
5877 }
5878 /* When writing a single-width character over a double-width
5879 * character and at the end of the redrawn text, need to clear out
5880 * the right halve of the old character.
5881 * Also required when writing the right halve of a double-width
5882 * char over the left halve of an existing one. */
5883 if (has_mbyte && col + char_cells == endcol
5884 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005885 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005887 && (*mb_off2cells)(off_to, max_off_to) == 1
5888 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 clear_next = TRUE;
5890#endif
5891
5892 ScreenLines[off_to] = ScreenLines[off_from];
5893#ifdef FEAT_MBYTE
5894 if (enc_utf8)
5895 {
5896 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5897 if (ScreenLinesUC[off_from] != 0)
5898 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005899 int i;
5900
5901 for (i = 0; i < Screen_mco; ++i)
5902 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 }
5904 }
5905 if (char_cells == 2)
5906 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5907#endif
5908
5909#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005910 /* The bold trick makes a single column of pixels appear in the
5911 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 * character should be redrawn too. This happens for our own GUI
5913 * and for some xterms. */
5914 if (
5915# ifdef FEAT_GUI
5916 gui.in_use
5917# endif
5918# if defined(FEAT_GUI) && defined(UNIX)
5919 ||
5920# endif
5921# ifdef UNIX
5922 term_is_xterm
5923# endif
5924 )
5925 {
5926 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005927 if (hl > HL_ALL)
5928 hl = syn_attr2attr(hl);
5929 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 redraw_next = TRUE;
5931 }
5932#endif
5933 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5934#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005935 /* For simplicity set the attributes of second half of a
5936 * double-wide character equal to the first half. */
5937 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005939
5940 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 else
5943#endif
5944 screen_char(off_to, row, col + coloff);
5945 }
5946 else if ( p_wiv
5947#ifdef FEAT_GUI
5948 && !gui.in_use
5949#endif
5950 && col + coloff > 0)
5951 {
5952 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5953 {
5954 /*
5955 * Don't output stop-highlight when moving the cursor, it will
5956 * stop the highlighting when it should continue.
5957 */
5958 screen_attr = 0;
5959 }
5960 else if (screen_attr != 0)
5961 screen_stop_highlight();
5962 }
5963
5964 off_to += CHAR_CELLS;
5965 off_from += CHAR_CELLS;
5966 col += CHAR_CELLS;
5967 }
5968
5969#ifdef FEAT_MBYTE
5970 if (clear_next)
5971 {
5972 /* Clear the second half of a double-wide character of which the left
5973 * half was overwritten with a single-wide character. */
5974 ScreenLines[off_to] = ' ';
5975 if (enc_utf8)
5976 ScreenLinesUC[off_to] = 0;
5977 screen_char(off_to, row, col + coloff);
5978 }
5979#endif
5980
5981 if (clear_width > 0
5982#ifdef FEAT_RIGHTLEFT
5983 && !rlflag
5984#endif
5985 )
5986 {
5987#ifdef FEAT_GUI
5988 int startCol = col;
5989#endif
5990
5991 /* blank out the rest of the line */
5992 while (col < clear_width && ScreenLines[off_to] == ' '
5993 && ScreenAttrs[off_to] == 0
5994#ifdef FEAT_MBYTE
5995 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5996#endif
5997 )
5998 {
5999 ++off_to;
6000 ++col;
6001 }
6002 if (col < clear_width)
6003 {
6004#ifdef FEAT_GUI
6005 /*
6006 * In the GUI, clearing the rest of the line may leave pixels
6007 * behind if the first character cleared was bold. Some bold
6008 * fonts spill over the left. In this case we redraw the previous
6009 * character too. If we didn't skip any blanks above, then we
6010 * only redraw if the character wasn't already redrawn anyway.
6011 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006012 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {
6014 hl = ScreenAttrs[off_to];
6015 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006016 {
6017 int prev_cells = 1;
6018# ifdef FEAT_MBYTE
6019 if (enc_utf8)
6020 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6021 * that its width is 2. */
6022 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6023 else if (enc_dbcs != 0)
6024 {
6025 /* find previous character by counting from first
6026 * column and get its width. */
6027 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006028 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006029
6030 while (off < off_to)
6031 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006032 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006033 off += prev_cells;
6034 }
6035 }
6036
6037 if (enc_dbcs != 0 && prev_cells > 1)
6038 screen_char_2(off_to - prev_cells, row,
6039 col + coloff - prev_cells);
6040 else
6041# endif
6042 screen_char(off_to - prev_cells, row,
6043 col + coloff - prev_cells);
6044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 }
6046#endif
6047 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6048 ' ', ' ', 0);
6049#ifdef FEAT_VERTSPLIT
6050 off_to += clear_width - col;
6051 col = clear_width;
6052#endif
6053 }
6054 }
6055
6056 if (clear_width > 0)
6057 {
6058#ifdef FEAT_VERTSPLIT
6059 /* For a window that's left of another, draw the separator char. */
6060 if (col + coloff < Columns)
6061 {
6062 int c;
6063
6064 c = fillchar_vsep(&hl);
6065 if (ScreenLines[off_to] != c
6066# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006067 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6068 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069# endif
6070 || ScreenAttrs[off_to] != hl)
6071 {
6072 ScreenLines[off_to] = c;
6073 ScreenAttrs[off_to] = hl;
6074# ifdef FEAT_MBYTE
6075 if (enc_utf8)
6076 {
6077 if (c >= 0x80)
6078 {
6079 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006080 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 }
6082 else
6083 ScreenLinesUC[off_to] = 0;
6084 }
6085# endif
6086 screen_char(off_to, row, col + coloff);
6087 }
6088 }
6089 else
6090#endif
6091 LineWraps[row] = FALSE;
6092 }
6093}
6094
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006095#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006097 * Mirror text "str" for right-left displaying.
6098 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006100 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101rl_mirror(str)
6102 char_u *str;
6103{
6104 char_u *p1, *p2;
6105 int t;
6106
6107 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6108 {
6109 t = *p1;
6110 *p1 = *p2;
6111 *p2 = t;
6112 }
6113}
6114#endif
6115
6116#if defined(FEAT_WINDOWS) || defined(PROTO)
6117/*
6118 * mark all status lines for redraw; used after first :cd
6119 */
6120 void
6121status_redraw_all()
6122{
6123 win_T *wp;
6124
6125 for (wp = firstwin; wp; wp = wp->w_next)
6126 if (wp->w_status_height)
6127 {
6128 wp->w_redr_status = TRUE;
6129 redraw_later(VALID);
6130 }
6131}
6132
6133/*
6134 * mark all status lines of the current buffer for redraw
6135 */
6136 void
6137status_redraw_curbuf()
6138{
6139 win_T *wp;
6140
6141 for (wp = firstwin; wp; wp = wp->w_next)
6142 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6143 {
6144 wp->w_redr_status = TRUE;
6145 redraw_later(VALID);
6146 }
6147}
6148
6149/*
6150 * Redraw all status lines that need to be redrawn.
6151 */
6152 void
6153redraw_statuslines()
6154{
6155 win_T *wp;
6156
6157 for (wp = firstwin; wp; wp = wp->w_next)
6158 if (wp->w_redr_status)
6159 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006160 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006161 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162}
6163#endif
6164
6165#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6166/*
6167 * Redraw all status lines at the bottom of frame "frp".
6168 */
6169 void
6170win_redraw_last_status(frp)
6171 frame_T *frp;
6172{
6173 if (frp->fr_layout == FR_LEAF)
6174 frp->fr_win->w_redr_status = TRUE;
6175 else if (frp->fr_layout == FR_ROW)
6176 {
6177 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6178 win_redraw_last_status(frp);
6179 }
6180 else /* frp->fr_layout == FR_COL */
6181 {
6182 frp = frp->fr_child;
6183 while (frp->fr_next != NULL)
6184 frp = frp->fr_next;
6185 win_redraw_last_status(frp);
6186 }
6187}
6188#endif
6189
6190#ifdef FEAT_VERTSPLIT
6191/*
6192 * Draw the verticap separator right of window "wp" starting with line "row".
6193 */
6194 static void
6195draw_vsep_win(wp, row)
6196 win_T *wp;
6197 int row;
6198{
6199 int hl;
6200 int c;
6201
6202 if (wp->w_vsep_width)
6203 {
6204 /* draw the vertical separator right of this window */
6205 c = fillchar_vsep(&hl);
6206 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6207 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6208 c, ' ', hl);
6209 }
6210}
6211#endif
6212
6213#ifdef FEAT_WILDMENU
6214static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006215static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
6217/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006218 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 */
6220 static int
6221status_match_len(xp, s)
6222 expand_T *xp;
6223 char_u *s;
6224{
6225 int len = 0;
6226
6227#ifdef FEAT_MENU
6228 int emenu = (xp->xp_context == EXPAND_MENUS
6229 || xp->xp_context == EXPAND_MENUNAMES);
6230
6231 /* Check for menu separators - replace with '|'. */
6232 if (emenu && menu_is_separator(s))
6233 return 1;
6234#endif
6235
6236 while (*s != NUL)
6237 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006238 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006239 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006240 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
6242
6243 return len;
6244}
6245
6246/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006247 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006248 * These are backslashes used for escaping. Do show backslashes in help tags.
6249 */
6250 static int
6251skip_status_match_char(xp, s)
6252 expand_T *xp;
6253 char_u *s;
6254{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006255 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006256#ifdef FEAT_MENU
6257 || ((xp->xp_context == EXPAND_MENUS
6258 || xp->xp_context == EXPAND_MENUNAMES)
6259 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6260#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006261 )
6262 {
6263#ifndef BACKSLASH_IN_FILENAME
6264 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6265 return 2;
6266#endif
6267 return 1;
6268 }
6269 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006270}
6271
6272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 * Show wildchar matches in the status line.
6274 * Show at least the "match" item.
6275 * We start at item 'first_match' in the list and show all matches that fit.
6276 *
6277 * If inversion is possible we use it. Else '=' characters are used.
6278 */
6279 void
6280win_redr_status_matches(xp, num_matches, matches, match, showtail)
6281 expand_T *xp;
6282 int num_matches;
6283 char_u **matches; /* list of matches */
6284 int match;
6285 int showtail;
6286{
6287#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6288 int row;
6289 char_u *buf;
6290 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006291 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 int fillchar;
6293 int attr;
6294 int i;
6295 int highlight = TRUE;
6296 char_u *selstart = NULL;
6297 int selstart_col = 0;
6298 char_u *selend = NULL;
6299 static int first_match = 0;
6300 int add_left = FALSE;
6301 char_u *s;
6302#ifdef FEAT_MENU
6303 int emenu;
6304#endif
6305#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6306 int l;
6307#endif
6308
6309 if (matches == NULL) /* interrupted completion? */
6310 return;
6311
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006312#ifdef FEAT_MBYTE
6313 if (has_mbyte)
6314 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6315 else
6316#endif
6317 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 if (buf == NULL)
6319 return;
6320
6321 if (match == -1) /* don't show match but original text */
6322 {
6323 match = 0;
6324 highlight = FALSE;
6325 }
6326 /* count 1 for the ending ">" */
6327 clen = status_match_len(xp, L_MATCH(match)) + 3;
6328 if (match == 0)
6329 first_match = 0;
6330 else if (match < first_match)
6331 {
6332 /* jumping left, as far as we can go */
6333 first_match = match;
6334 add_left = TRUE;
6335 }
6336 else
6337 {
6338 /* check if match fits on the screen */
6339 for (i = first_match; i < match; ++i)
6340 clen += status_match_len(xp, L_MATCH(i)) + 2;
6341 if (first_match > 0)
6342 clen += 2;
6343 /* jumping right, put match at the left */
6344 if ((long)clen > Columns)
6345 {
6346 first_match = match;
6347 /* if showing the last match, we can add some on the left */
6348 clen = 2;
6349 for (i = match; i < num_matches; ++i)
6350 {
6351 clen += status_match_len(xp, L_MATCH(i)) + 2;
6352 if ((long)clen >= Columns)
6353 break;
6354 }
6355 if (i == num_matches)
6356 add_left = TRUE;
6357 }
6358 }
6359 if (add_left)
6360 while (first_match > 0)
6361 {
6362 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6363 if ((long)clen >= Columns)
6364 break;
6365 --first_match;
6366 }
6367
6368 fillchar = fillchar_status(&attr, TRUE);
6369
6370 if (first_match == 0)
6371 {
6372 *buf = NUL;
6373 len = 0;
6374 }
6375 else
6376 {
6377 STRCPY(buf, "< ");
6378 len = 2;
6379 }
6380 clen = len;
6381
6382 i = first_match;
6383 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6384 {
6385 if (i == match)
6386 {
6387 selstart = buf + len;
6388 selstart_col = clen;
6389 }
6390
6391 s = L_MATCH(i);
6392 /* Check for menu separators - replace with '|' */
6393#ifdef FEAT_MENU
6394 emenu = (xp->xp_context == EXPAND_MENUS
6395 || xp->xp_context == EXPAND_MENUNAMES);
6396 if (emenu && menu_is_separator(s))
6397 {
6398 STRCPY(buf + len, transchar('|'));
6399 l = (int)STRLEN(buf + len);
6400 len += l;
6401 clen += l;
6402 }
6403 else
6404#endif
6405 for ( ; *s != NUL; ++s)
6406 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006407 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 clen += ptr2cells(s);
6409#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006410 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 {
6412 STRNCPY(buf + len, s, l);
6413 s += l - 1;
6414 len += l;
6415 }
6416 else
6417#endif
6418 {
6419 STRCPY(buf + len, transchar_byte(*s));
6420 len += (int)STRLEN(buf + len);
6421 }
6422 }
6423 if (i == match)
6424 selend = buf + len;
6425
6426 *(buf + len++) = ' ';
6427 *(buf + len++) = ' ';
6428 clen += 2;
6429 if (++i == num_matches)
6430 break;
6431 }
6432
6433 if (i != num_matches)
6434 {
6435 *(buf + len++) = '>';
6436 ++clen;
6437 }
6438
6439 buf[len] = NUL;
6440
6441 row = cmdline_row - 1;
6442 if (row >= 0)
6443 {
6444 if (wild_menu_showing == 0)
6445 {
6446 if (msg_scrolled > 0)
6447 {
6448 /* Put the wildmenu just above the command line. If there is
6449 * no room, scroll the screen one line up. */
6450 if (cmdline_row == Rows - 1)
6451 {
6452 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6453 ++msg_scrolled;
6454 }
6455 else
6456 {
6457 ++cmdline_row;
6458 ++row;
6459 }
6460 wild_menu_showing = WM_SCROLLED;
6461 }
6462 else
6463 {
6464 /* Create status line if needed by setting 'laststatus' to 2.
6465 * Set 'winminheight' to zero to avoid that the window is
6466 * resized. */
6467 if (lastwin->w_status_height == 0)
6468 {
6469 save_p_ls = p_ls;
6470 save_p_wmh = p_wmh;
6471 p_ls = 2;
6472 p_wmh = 0;
6473 last_status(FALSE);
6474 }
6475 wild_menu_showing = WM_SHOWN;
6476 }
6477 }
6478
6479 screen_puts(buf, row, 0, attr);
6480 if (selstart != NULL && highlight)
6481 {
6482 *selend = NUL;
6483 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6484 }
6485
6486 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6487 }
6488
6489#ifdef FEAT_VERTSPLIT
6490 win_redraw_last_status(topframe);
6491#else
6492 lastwin->w_redr_status = TRUE;
6493#endif
6494 vim_free(buf);
6495}
6496#endif
6497
6498#if defined(FEAT_WINDOWS) || defined(PROTO)
6499/*
6500 * Redraw the status line of window wp.
6501 *
6502 * If inversion is possible we use it. Else '=' characters are used.
6503 */
6504 void
6505win_redr_status(wp)
6506 win_T *wp;
6507{
6508 int row;
6509 char_u *p;
6510 int len;
6511 int fillchar;
6512 int attr;
6513 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006514 static int busy = FALSE;
6515
6516 /* It's possible to get here recursively when 'statusline' (indirectly)
6517 * invokes ":redrawstatus". Simply ignore the call then. */
6518 if (busy)
6519 return;
6520 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
6522 wp->w_redr_status = FALSE;
6523 if (wp->w_status_height == 0)
6524 {
6525 /* no status line, can only be last window */
6526 redraw_cmdline = TRUE;
6527 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006528 else if (!redrawing()
6529#ifdef FEAT_INS_EXPAND
6530 /* don't update status line when popup menu is visible and may be
6531 * drawn over it */
6532 || pum_visible()
6533#endif
6534 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536 /* Don't redraw right now, do it later. */
6537 wp->w_redr_status = TRUE;
6538 }
6539#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006540 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
6542 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006543 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
6545#endif
6546 else
6547 {
6548 fillchar = fillchar_status(&attr, wp == curwin);
6549
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006550 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 p = NameBuff;
6552 len = (int)STRLEN(p);
6553
6554 if (wp->w_buffer->b_help
6555#ifdef FEAT_QUICKFIX
6556 || wp->w_p_pvw
6557#endif
6558 || bufIsChanged(wp->w_buffer)
6559 || wp->w_buffer->b_p_ro)
6560 *(p + len++) = ' ';
6561 if (wp->w_buffer->b_help)
6562 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006563 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 len += (int)STRLEN(p + len);
6565 }
6566#ifdef FEAT_QUICKFIX
6567 if (wp->w_p_pvw)
6568 {
6569 STRCPY(p + len, _("[Preview]"));
6570 len += (int)STRLEN(p + len);
6571 }
6572#endif
6573 if (bufIsChanged(wp->w_buffer))
6574 {
6575 STRCPY(p + len, "[+]");
6576 len += 3;
6577 }
6578 if (wp->w_buffer->b_p_ro)
6579 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006580 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 len += 4;
6582 }
6583
6584#ifndef FEAT_VERTSPLIT
6585 this_ru_col = ru_col;
6586 if (this_ru_col < (Columns + 1) / 2)
6587 this_ru_col = (Columns + 1) / 2;
6588#else
6589 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6590 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6591 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6592 if (this_ru_col <= 1)
6593 {
6594 p = (char_u *)"<"; /* No room for file name! */
6595 len = 1;
6596 }
6597 else
6598#endif
6599#ifdef FEAT_MBYTE
6600 if (has_mbyte)
6601 {
6602 int clen = 0, i;
6603
6604 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006605 clen = mb_string2cells(p, -1);
6606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 /* Find first character that will fit.
6608 * Going from start to end is much faster for DBCS. */
6609 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006610 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 clen -= (*mb_ptr2cells)(p + i);
6612 len = clen;
6613 if (i > 0)
6614 {
6615 p = p + i - 1;
6616 *p = '<';
6617 ++len;
6618 }
6619
6620 }
6621 else
6622#endif
6623 if (len > this_ru_col - 1)
6624 {
6625 p += len - (this_ru_col - 1);
6626 *p = '<';
6627 len = this_ru_col - 1;
6628 }
6629
6630 row = W_WINROW(wp) + wp->w_height;
6631 screen_puts(p, row, W_WINCOL(wp), attr);
6632 screen_fill(row, row + 1, len + W_WINCOL(wp),
6633 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6634
6635 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6636 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6637 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6638 - 1 + W_WINCOL(wp)), attr);
6639
6640#ifdef FEAT_CMDL_INFO
6641 win_redr_ruler(wp, TRUE);
6642#endif
6643 }
6644
6645#ifdef FEAT_VERTSPLIT
6646 /*
6647 * May need to draw the character below the vertical separator.
6648 */
6649 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6650 {
6651 if (stl_connected(wp))
6652 fillchar = fillchar_status(&attr, wp == curwin);
6653 else
6654 fillchar = fillchar_vsep(&attr);
6655 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6656 attr);
6657 }
6658#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006659 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660}
6661
Bram Moolenaar238a5642006-02-21 22:12:05 +00006662#ifdef FEAT_STL_OPT
6663/*
6664 * Redraw the status line according to 'statusline' and take care of any
6665 * errors encountered.
6666 */
6667 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006668redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006669 win_T *wp;
6670{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006671 static int entered = FALSE;
6672 int save_called_emsg = called_emsg;
6673
6674 /* When called recursively return. This can happen when the statusline
6675 * contains an expression that triggers a redraw. */
6676 if (entered)
6677 return;
6678 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006679
6680 called_emsg = FALSE;
6681 win_redr_custom(wp, FALSE);
6682 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006683 {
6684 /* When there is an error disable the statusline, otherwise the
6685 * display is messed up with errors and a redraw triggers the problem
6686 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006687 set_string_option_direct((char_u *)"statusline", -1,
6688 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006689 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006690 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006691 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006692 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006693}
6694#endif
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696# ifdef FEAT_VERTSPLIT
6697/*
6698 * Return TRUE if the status line of window "wp" is connected to the status
6699 * line of the window right of it. If not, then it's a vertical separator.
6700 * Only call if (wp->w_vsep_width != 0).
6701 */
6702 int
6703stl_connected(wp)
6704 win_T *wp;
6705{
6706 frame_T *fr;
6707
6708 fr = wp->w_frame;
6709 while (fr->fr_parent != NULL)
6710 {
6711 if (fr->fr_parent->fr_layout == FR_COL)
6712 {
6713 if (fr->fr_next != NULL)
6714 break;
6715 }
6716 else
6717 {
6718 if (fr->fr_next != NULL)
6719 return TRUE;
6720 }
6721 fr = fr->fr_parent;
6722 }
6723 return FALSE;
6724}
6725# endif
6726
6727#endif /* FEAT_WINDOWS */
6728
6729#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6730/*
6731 * Get the value to show for the language mappings, active 'keymap'.
6732 */
6733 int
6734get_keymap_str(wp, buf, len)
6735 win_T *wp;
6736 char_u *buf; /* buffer for the result */
6737 int len; /* length of buffer */
6738{
6739 char_u *p;
6740
6741 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6742 return FALSE;
6743
6744 {
6745#ifdef FEAT_EVAL
6746 buf_T *old_curbuf = curbuf;
6747 win_T *old_curwin = curwin;
6748 char_u *s;
6749
6750 curbuf = wp->w_buffer;
6751 curwin = wp;
6752 STRCPY(buf, "b:keymap_name"); /* must be writable */
6753 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006754 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 --emsg_skip;
6756 curbuf = old_curbuf;
6757 curwin = old_curwin;
6758 if (p == NULL || *p == NUL)
6759#endif
6760 {
6761#ifdef FEAT_KEYMAP
6762 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6763 p = wp->w_buffer->b_p_keymap;
6764 else
6765#endif
6766 p = (char_u *)"lang";
6767 }
6768 if ((int)(STRLEN(p) + 3) < len)
6769 sprintf((char *)buf, "<%s>", p);
6770 else
6771 buf[0] = NUL;
6772#ifdef FEAT_EVAL
6773 vim_free(s);
6774#endif
6775 }
6776 return buf[0] != NUL;
6777}
6778#endif
6779
6780#if defined(FEAT_STL_OPT) || defined(PROTO)
6781/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006782 * Redraw the status line or ruler of window "wp".
6783 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 */
6785 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006786win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006788 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006790 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 int attr;
6792 int curattr;
6793 int row;
6794 int col = 0;
6795 int maxwidth;
6796 int width;
6797 int n;
6798 int len;
6799 int fillchar;
6800 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006801 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006803 struct stl_hlrec hltab[STL_MAX_ITEM];
6804 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006805 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006806 win_T *ewp;
6807 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
Bram Moolenaar1d633412013-12-11 15:52:01 +01006809 /* There is a tiny chance that this gets called recursively: When
6810 * redrawing a status line triggers redrawing the ruler or tabline.
6811 * Avoid trouble by not allowing recursion. */
6812 if (entered)
6813 return;
6814 entered = TRUE;
6815
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006817 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006819 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006820 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006821 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006822 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006823 attr = hl_attr(HLF_TPF);
6824 maxwidth = Columns;
6825# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006826 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006829 else
6830 {
6831 row = W_WINROW(wp) + wp->w_height;
6832 fillchar = fillchar_status(&attr, wp == curwin);
6833 maxwidth = W_WIDTH(wp);
6834
6835 if (draw_ruler)
6836 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006837 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006838 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006839 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006840 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006841 if (*++stl == '-')
6842 stl++;
6843 if (atoi((char *)stl))
6844 while (VIM_ISDIGIT(*stl))
6845 stl++;
6846 if (*stl++ != '(')
6847 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006848 }
6849#ifdef FEAT_VERTSPLIT
6850 col = ru_col - (Columns - W_WIDTH(wp));
6851 if (col < (W_WIDTH(wp) + 1) / 2)
6852 col = (W_WIDTH(wp) + 1) / 2;
6853#else
6854 col = ru_col;
6855 if (col > (Columns + 1) / 2)
6856 col = (Columns + 1) / 2;
6857#endif
6858 maxwidth = W_WIDTH(wp) - col;
6859#ifdef FEAT_WINDOWS
6860 if (!wp->w_status_height)
6861#endif
6862 {
6863 row = Rows - 1;
6864 --maxwidth; /* writing in last column may cause scrolling */
6865 fillchar = ' ';
6866 attr = 0;
6867 }
6868
6869# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006870 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006871# endif
6872 }
6873 else
6874 {
6875 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006876 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006877 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006878 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006879# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006880 use_sandbox = was_set_insecurely((char_u *)"statusline",
6881 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006882# endif
6883 }
6884
6885#ifdef FEAT_VERTSPLIT
6886 col += W_WINCOL(wp);
6887#endif
6888 }
6889
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006891 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892
Bram Moolenaar61452852011-02-01 18:01:11 +01006893 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6894 * the cursor away and back. */
6895 ewp = wp == NULL ? curwin : wp;
6896 p_crb_save = ewp->w_p_crb;
6897 ewp->w_p_crb = FALSE;
6898
Bram Moolenaar362f3562009-11-03 16:20:34 +00006899 /* Make a copy, because the statusline may include a function call that
6900 * might change the option value and free the memory. */
6901 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006902 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006903 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006904 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006905 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006906 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006908 /* Make all characters printable. */
6909 p = transstr(buf);
6910 if (p != NULL)
6911 {
6912 vim_strncpy(buf, p, sizeof(buf) - 1);
6913 vim_free(p);
6914 }
6915
6916 /* fill up with "fillchar" */
6917 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006918 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {
6920#ifdef FEAT_MBYTE
6921 len += (*mb_char2bytes)(fillchar, buf + len);
6922#else
6923 buf[len++] = fillchar;
6924#endif
6925 ++width;
6926 }
6927 buf[len] = NUL;
6928
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006929 /*
6930 * Draw each snippet with the specified highlighting.
6931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 curattr = attr;
6933 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006934 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006936 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 screen_puts_len(p, len, row, col, curattr);
6938 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006939 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006941 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006943 else if (hltab[n].userhl < 0)
6944 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006946 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006947 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948#endif
6949 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006950 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
6952 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006953
6954 if (wp == NULL)
6955 {
6956 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6957 col = 0;
6958 len = 0;
6959 p = buf;
6960 fillchar = 0;
6961 for (n = 0; tabtab[n].start != NULL; n++)
6962 {
6963 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6964 while (col < len)
6965 TabPageIdxs[col++] = fillchar;
6966 p = tabtab[n].start;
6967 fillchar = tabtab[n].userhl;
6968 }
6969 while (col < Columns)
6970 TabPageIdxs[col++] = fillchar;
6971 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006972
6973theend:
6974 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975}
6976
6977#endif /* FEAT_STL_OPT */
6978
6979/*
6980 * Output a single character directly to the screen and update ScreenLines.
6981 */
6982 void
6983screen_putchar(c, row, col, attr)
6984 int c;
6985 int row, col;
6986 int attr;
6987{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 char_u buf[MB_MAXBYTES + 1];
6989
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006990#ifdef FEAT_MBYTE
6991 if (has_mbyte)
6992 buf[(*mb_char2bytes)(c, buf)] = NUL;
6993 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006995 {
6996 buf[0] = c;
6997 buf[1] = NUL;
6998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 screen_puts(buf, row, col, attr);
7000}
7001
7002/*
7003 * Get a single character directly from ScreenLines into "bytes[]".
7004 * Also return its attribute in *attrp;
7005 */
7006 void
7007screen_getbytes(row, col, bytes, attrp)
7008 int row, col;
7009 char_u *bytes;
7010 int *attrp;
7011{
7012 unsigned off;
7013
7014 /* safety check */
7015 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7016 {
7017 off = LineOffset[row] + col;
7018 *attrp = ScreenAttrs[off];
7019 bytes[0] = ScreenLines[off];
7020 bytes[1] = NUL;
7021
7022#ifdef FEAT_MBYTE
7023 if (enc_utf8 && ScreenLinesUC[off] != 0)
7024 bytes[utfc_char2bytes(off, bytes)] = NUL;
7025 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7026 {
7027 bytes[0] = ScreenLines[off];
7028 bytes[1] = ScreenLines2[off];
7029 bytes[2] = NUL;
7030 }
7031 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7032 {
7033 bytes[1] = ScreenLines[off + 1];
7034 bytes[2] = NUL;
7035 }
7036#endif
7037 }
7038}
7039
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007040#ifdef FEAT_MBYTE
7041static int screen_comp_differs __ARGS((int, int*));
7042
7043/*
7044 * Return TRUE if composing characters for screen posn "off" differs from
7045 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007046 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007047 */
7048 static int
7049screen_comp_differs(off, u8cc)
7050 int off;
7051 int *u8cc;
7052{
7053 int i;
7054
7055 for (i = 0; i < Screen_mco; ++i)
7056 {
7057 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7058 return TRUE;
7059 if (u8cc[i] == 0)
7060 break;
7061 }
7062 return FALSE;
7063}
7064#endif
7065
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066/*
7067 * Put string '*text' on the screen at position 'row' and 'col', with
7068 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7069 * Note: only outputs within one row, message is truncated at screen boundary!
7070 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7071 */
7072 void
7073screen_puts(text, row, col, attr)
7074 char_u *text;
7075 int row;
7076 int col;
7077 int attr;
7078{
7079 screen_puts_len(text, -1, row, col, attr);
7080}
7081
7082/*
7083 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7084 * a NUL.
7085 */
7086 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007087screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007089 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 int row;
7091 int col;
7092 int attr;
7093{
7094 unsigned off;
7095 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007096 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 int c;
7098#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007099 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 int mbyte_blen = 1;
7101 int mbyte_cells = 1;
7102 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007103 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 int clear_next_cell = FALSE;
7105# ifdef FEAT_ARABIC
7106 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007107 int pc, nc, nc1;
7108 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109# endif
7110#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007111#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7112 int force_redraw_this;
7113 int force_redraw_next = FALSE;
7114#endif
7115 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
7117 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7118 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007119 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
Bram Moolenaarc236c162008-07-13 17:41:49 +00007121#ifdef FEAT_MBYTE
7122 /* When drawing over the right halve of a double-wide char clear out the
7123 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007124 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007125# ifdef FEAT_GUI
7126 && !gui.in_use
7127# endif
7128 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007129 {
7130 ScreenLines[off - 1] = ' ';
7131 ScreenAttrs[off - 1] = 0;
7132 if (enc_utf8)
7133 {
7134 ScreenLinesUC[off - 1] = 0;
7135 ScreenLinesC[0][off - 1] = 0;
7136 }
7137 /* redraw the previous cell, make it empty */
7138 screen_char(off - 1, row, col - 1);
7139 /* force the cell at "col" to be redrawn */
7140 force_redraw_next = TRUE;
7141 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007142#endif
7143
Bram Moolenaar367329b2007-08-30 11:53:22 +00007144#ifdef FEAT_MBYTE
7145 max_off = LineOffset[row] + screen_Columns;
7146#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007147 while (col < screen_Columns
7148 && (len < 0 || (int)(ptr - text) < len)
7149 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {
7151 c = *ptr;
7152#ifdef FEAT_MBYTE
7153 /* check if this is the first byte of a multibyte */
7154 if (has_mbyte)
7155 {
7156 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007157 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007159 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7161 mbyte_cells = 1;
7162 else if (enc_dbcs != 0)
7163 mbyte_cells = mbyte_blen;
7164 else /* enc_utf8 */
7165 {
7166 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007167 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 (int)((text + len) - ptr));
7169 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007170 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007172# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 /* Non-BMP character: display as ? or fullwidth ?. */
7174 if (u8c >= 0x10000)
7175 {
7176 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7177 if (attr == 0)
7178 attr = hl_attr(HLF_8);
7179 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181# ifdef FEAT_ARABIC
7182 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7183 {
7184 /* Do Arabic shaping. */
7185 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7186 {
7187 /* Past end of string to be displayed. */
7188 nc = NUL;
7189 nc1 = NUL;
7190 }
7191 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007192 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007193 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7194 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007195 nc1 = pcc[0];
7196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 pc = prev_c;
7198 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007199 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
7201 else
7202 prev_c = u8c;
7203# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007204 if (col + mbyte_cells > screen_Columns)
7205 {
7206 /* Only 1 cell left, but character requires 2 cells:
7207 * display a '>' in the last column to avoid wrapping. */
7208 c = '>';
7209 mbyte_cells = 1;
7210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 }
7212 }
7213#endif
7214
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007215#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7216 force_redraw_this = force_redraw_next;
7217 force_redraw_next = FALSE;
7218#endif
7219
7220 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221#ifdef FEAT_MBYTE
7222 || (mbyte_cells == 2
7223 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7224 || (enc_dbcs == DBCS_JPNU
7225 && c == 0x8e
7226 && ScreenLines2[off] != ptr[1])
7227 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007228 && (ScreenLinesUC[off] !=
7229 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7230 || (ScreenLinesUC[off] != 0
7231 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232#endif
7233 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007234 || exmode_active;
7235
7236 if (need_redraw
7237#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7238 || force_redraw_this
7239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 )
7241 {
7242#if defined(FEAT_GUI) || defined(UNIX)
7243 /* The bold trick makes a single row of pixels appear in the next
7244 * character. When a bold character is removed, the next
7245 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007246 * and for some xterms. */
7247 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248# ifdef FEAT_GUI
7249 gui.in_use
7250# endif
7251# if defined(FEAT_GUI) && defined(UNIX)
7252 ||
7253# endif
7254# ifdef UNIX
7255 term_is_xterm
7256# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007257 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007259 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007261 if (n > HL_ALL)
7262 n = syn_attr2attr(n);
7263 if (n & HL_BOLD)
7264 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 }
7266#endif
7267#ifdef FEAT_MBYTE
7268 /* When at the end of the text and overwriting a two-cell
7269 * character with a one-cell character, need to clear the next
7270 * cell. Also when overwriting the left halve of a two-cell char
7271 * with the right halve of a two-cell char. Do this only once
7272 * (mb_off2cells() may return 2 on the right halve). */
7273 if (clear_next_cell)
7274 clear_next_cell = FALSE;
7275 else if (has_mbyte
7276 && (len < 0 ? ptr[mbyte_blen] == NUL
7277 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007278 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007280 && (*mb_off2cells)(off, max_off) == 1
7281 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 clear_next_cell = TRUE;
7283
7284 /* Make sure we never leave a second byte of a double-byte behind,
7285 * it confuses mb_off2cells(). */
7286 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007287 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007289 && (*mb_off2cells)(off, max_off) == 1
7290 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 ScreenLines[off + mbyte_blen] = 0;
7292#endif
7293 ScreenLines[off] = c;
7294 ScreenAttrs[off] = attr;
7295#ifdef FEAT_MBYTE
7296 if (enc_utf8)
7297 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007298 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 ScreenLinesUC[off] = 0;
7300 else
7301 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007302 int i;
7303
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007305 for (i = 0; i < Screen_mco; ++i)
7306 {
7307 ScreenLinesC[i][off] = u8cc[i];
7308 if (u8cc[i] == 0)
7309 break;
7310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
7312 if (mbyte_cells == 2)
7313 {
7314 ScreenLines[off + 1] = 0;
7315 ScreenAttrs[off + 1] = attr;
7316 }
7317 screen_char(off, row, col);
7318 }
7319 else if (mbyte_cells == 2)
7320 {
7321 ScreenLines[off + 1] = ptr[1];
7322 ScreenAttrs[off + 1] = attr;
7323 screen_char_2(off, row, col);
7324 }
7325 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7326 {
7327 ScreenLines2[off] = ptr[1];
7328 screen_char(off, row, col);
7329 }
7330 else
7331#endif
7332 screen_char(off, row, col);
7333 }
7334#ifdef FEAT_MBYTE
7335 if (has_mbyte)
7336 {
7337 off += mbyte_cells;
7338 col += mbyte_cells;
7339 ptr += mbyte_blen;
7340 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007341 {
7342 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007344 len = -1;
7345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 }
7347 else
7348#endif
7349 {
7350 ++off;
7351 ++col;
7352 ++ptr;
7353 }
7354 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007355
7356#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7357 /* If we detected the next character needs to be redrawn, but the text
7358 * doesn't extend up to there, update the character here. */
7359 if (force_redraw_next && col < screen_Columns)
7360 {
7361# ifdef FEAT_MBYTE
7362 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7363 screen_char_2(off, row, col);
7364 else
7365# endif
7366 screen_char(off, row, col);
7367 }
7368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369}
7370
7371#ifdef FEAT_SEARCH_EXTRA
7372/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007373 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 */
7375 static void
7376start_search_hl()
7377{
7378 if (p_hls && !no_hlsearch)
7379 {
7380 last_pat_prog(&search_hl.rm);
7381 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007382# ifdef FEAT_RELTIME
7383 /* Set the time limit to 'redrawtime'. */
7384 profile_setlimit(p_rdt, &search_hl.tm);
7385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 }
7387}
7388
7389/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007390 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 */
7392 static void
7393end_search_hl()
7394{
7395 if (search_hl.rm.regprog != NULL)
7396 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007397 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 search_hl.rm.regprog = NULL;
7399 }
7400}
7401
7402/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007403 * Init for calling prepare_search_hl().
7404 */
7405 static void
7406init_search_hl(wp)
7407 win_T *wp;
7408{
7409 matchitem_T *cur;
7410
7411 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7412 * match */
7413 cur = wp->w_match_head;
7414 while (cur != NULL)
7415 {
7416 cur->hl.rm = cur->match;
7417 if (cur->hlg_id == 0)
7418 cur->hl.attr = 0;
7419 else
7420 cur->hl.attr = syn_id2attr(cur->hlg_id);
7421 cur->hl.buf = wp->w_buffer;
7422 cur->hl.lnum = 0;
7423 cur->hl.first_lnum = 0;
7424# ifdef FEAT_RELTIME
7425 /* Set the time limit to 'redrawtime'. */
7426 profile_setlimit(p_rdt, &(cur->hl.tm));
7427# endif
7428 cur = cur->next;
7429 }
7430 search_hl.buf = wp->w_buffer;
7431 search_hl.lnum = 0;
7432 search_hl.first_lnum = 0;
7433 /* time limit is set at the toplevel, for all windows */
7434}
7435
7436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 * Advance to the match in window "wp" line "lnum" or past it.
7438 */
7439 static void
7440prepare_search_hl(wp, lnum)
7441 win_T *wp;
7442 linenr_T lnum;
7443{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007444 matchitem_T *cur; /* points to the match list */
7445 match_T *shl; /* points to search_hl or a match */
7446 int shl_flag; /* flag to indicate whether search_hl
7447 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007448 int pos_inprogress; /* marks that position match search is
7449 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 int n;
7451
7452 /*
7453 * When using a multi-line pattern, start searching at the top
7454 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007455 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007457 cur = wp->w_match_head;
7458 shl_flag = FALSE;
7459 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007461 if (shl_flag == FALSE)
7462 {
7463 shl = &search_hl;
7464 shl_flag = TRUE;
7465 }
7466 else
7467 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (shl->rm.regprog != NULL
7469 && shl->lnum == 0
7470 && re_multiline(shl->rm.regprog))
7471 {
7472 if (shl->first_lnum == 0)
7473 {
7474# ifdef FEAT_FOLDING
7475 for (shl->first_lnum = lnum;
7476 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7477 if (hasFoldingWin(wp, shl->first_lnum - 1,
7478 NULL, NULL, TRUE, NULL))
7479 break;
7480# else
7481 shl->first_lnum = wp->w_topline;
7482# endif
7483 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007484 if (cur != NULL)
7485 cur->pos.cur = 0;
7486 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007488 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7489 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007491 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7492 pos_inprogress = cur == NULL || cur->pos.cur == 0
7493 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 if (shl->lnum != 0)
7495 {
7496 shl->first_lnum = shl->lnum
7497 + shl->rm.endpos[0].lnum
7498 - shl->rm.startpos[0].lnum;
7499 n = shl->rm.endpos[0].col;
7500 }
7501 else
7502 {
7503 ++shl->first_lnum;
7504 n = 0;
7505 }
7506 }
7507 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007508 if (shl != &search_hl && cur != NULL)
7509 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
7511}
7512
7513/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007514 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 * Uses shl->buf.
7516 * Sets shl->lnum and shl->rm contents.
7517 * Note: Assumes a previous match is always before "lnum", unless
7518 * shl->lnum is zero.
7519 * Careful: Any pointers for buffer lines will become invalid.
7520 */
7521 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007522next_search_hl(win, shl, lnum, mincol, cur)
7523 win_T *win;
7524 match_T *shl; /* points to search_hl or a match */
7525 linenr_T lnum;
7526 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007527 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
7529 linenr_T l;
7530 colnr_T matchcol;
7531 long nmatched;
7532
7533 if (shl->lnum != 0)
7534 {
7535 /* Check for three situations:
7536 * 1. If the "lnum" is below a previous match, start a new search.
7537 * 2. If the previous match includes "mincol", use it.
7538 * 3. Continue after the previous match.
7539 */
7540 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7541 if (lnum > l)
7542 shl->lnum = 0;
7543 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7544 return;
7545 }
7546
7547 /*
7548 * Repeat searching for a match until one is found that includes "mincol"
7549 * or none is found in this line.
7550 */
7551 called_emsg = FALSE;
7552 for (;;)
7553 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007554#ifdef FEAT_RELTIME
7555 /* Stop searching after passing the time limit. */
7556 if (profile_passed_limit(&(shl->tm)))
7557 {
7558 shl->lnum = 0; /* no match found in time */
7559 break;
7560 }
7561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 /* Three situations:
7563 * 1. No useful previous match: search from start of line.
7564 * 2. Not Vi compatible or empty match: continue at next character.
7565 * Break the loop if this is beyond the end of the line.
7566 * 3. Vi compatible searching: continue at end of previous match.
7567 */
7568 if (shl->lnum == 0)
7569 matchcol = 0;
7570 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7571 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007572 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007574 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007575
7576 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007577 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007578 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007580 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 shl->lnum = 0;
7582 break;
7583 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007584#ifdef FEAT_MBYTE
7585 if (has_mbyte)
7586 matchcol += mb_ptr2len(ml);
7587 else
7588#endif
7589 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 }
7591 else
7592 matchcol = shl->rm.endpos[0].col;
7593
7594 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007595 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007597 /* Remember whether shl->rm is using a copy of the regprog in
7598 * cur->match. */
7599 int regprog_is_copy = (shl != &search_hl && cur != NULL
7600 && shl == &cur->hl
7601 && cur->match.regprog == cur->hl.rm.regprog);
7602
Bram Moolenaarb3414592014-06-17 17:48:32 +02007603 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7604 matchcol,
7605#ifdef FEAT_RELTIME
7606 &(shl->tm)
7607#else
7608 NULL
7609#endif
7610 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007611 /* Copy the regprog, in case it got freed and recompiled. */
7612 if (regprog_is_copy)
7613 cur->match.regprog = cur->hl.rm.regprog;
7614
Bram Moolenaarb3414592014-06-17 17:48:32 +02007615 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007616 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007617 /* Error while handling regexp: stop using this regexp. */
7618 if (shl == &search_hl)
7619 {
7620 /* don't free regprog in the match list, it's a copy */
7621 vim_regfree(shl->rm.regprog);
7622 SET_NO_HLSEARCH(TRUE);
7623 }
7624 shl->rm.regprog = NULL;
7625 shl->lnum = 0;
7626 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7627 message */
7628 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007629 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007630 }
7631 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007632 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007633 else
7634 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (nmatched == 0)
7636 {
7637 shl->lnum = 0; /* no match found */
7638 break;
7639 }
7640 if (shl->rm.startpos[0].lnum > 0
7641 || shl->rm.startpos[0].col >= mincol
7642 || nmatched > 1
7643 || shl->rm.endpos[0].col > mincol)
7644 {
7645 shl->lnum += shl->rm.startpos[0].lnum;
7646 break; /* useful match found */
7647 }
7648 }
7649}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
Bram Moolenaarb3414592014-06-17 17:48:32 +02007651 static int
7652next_search_hl_pos(shl, lnum, posmatch, mincol)
7653 match_T *shl; /* points to a match */
7654 linenr_T lnum;
7655 posmatch_T *posmatch; /* match positions */
7656 colnr_T mincol; /* minimal column for a match */
7657{
7658 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007659 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007660
7661 shl->lnum = 0;
7662 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7663 {
7664 if (posmatch->pos[i].lnum == 0)
7665 break;
7666 if (posmatch->pos[i].col < mincol)
7667 continue;
7668 if (posmatch->pos[i].lnum == lnum)
7669 {
7670 if (shl->lnum == lnum)
7671 {
7672 /* partially sort positions by column numbers
7673 * on the same line */
7674 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7675 {
7676 llpos_T tmp = posmatch->pos[i];
7677
7678 posmatch->pos[i] = posmatch->pos[bot];
7679 posmatch->pos[bot] = tmp;
7680 }
7681 }
7682 else
7683 {
7684 bot = i;
7685 shl->lnum = lnum;
7686 }
7687 }
7688 }
7689 posmatch->cur = 0;
7690 if (shl->lnum == lnum)
7691 {
7692 colnr_T start = posmatch->pos[bot].col == 0
7693 ? 0 : posmatch->pos[bot].col - 1;
7694 colnr_T end = posmatch->pos[bot].col == 0
7695 ? MAXCOL : start + posmatch->pos[bot].len;
7696
7697 shl->rm.startpos[0].lnum = 0;
7698 shl->rm.startpos[0].col = start;
7699 shl->rm.endpos[0].lnum = 0;
7700 shl->rm.endpos[0].col = end;
7701 posmatch->cur = bot + 1;
7702 return TRUE;
7703 }
7704 return FALSE;
7705}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007706#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007707
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 static void
7709screen_start_highlight(attr)
7710 int attr;
7711{
7712 attrentry_T *aep = NULL;
7713
7714 screen_attr = attr;
7715 if (full_screen
7716#ifdef WIN3264
7717 && termcap_active
7718#endif
7719 )
7720 {
7721#ifdef FEAT_GUI
7722 if (gui.in_use)
7723 {
7724 char buf[20];
7725
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007726 /* The GUI handles this internally. */
7727 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 OUT_STR(buf);
7729 }
7730 else
7731#endif
7732 {
7733 if (attr > HL_ALL) /* special HL attr. */
7734 {
7735 if (t_colors > 1)
7736 aep = syn_cterm_attr2entry(attr);
7737 else
7738 aep = syn_term_attr2entry(attr);
7739 if (aep == NULL) /* did ":syntax clear" */
7740 attr = 0;
7741 else
7742 attr = aep->ae_attr;
7743 }
7744 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7745 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007746 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7747 && cterm_normal_fg_bold)
7748 /* If the Normal FG color has BOLD attribute and the new HL
7749 * has a FG color defined, clear BOLD. */
7750 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7752 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007753 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7754 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 out_str(T_US);
7756 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7757 out_str(T_CZH);
7758 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7759 out_str(T_MR);
7760
7761 /*
7762 * Output the color or start string after bold etc., in case the
7763 * bold etc. override the color setting.
7764 */
7765 if (aep != NULL)
7766 {
7767 if (t_colors > 1)
7768 {
7769 if (aep->ae_u.cterm.fg_color)
7770 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7771 if (aep->ae_u.cterm.bg_color)
7772 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7773 }
7774 else
7775 {
7776 if (aep->ae_u.term.start != NULL)
7777 out_str(aep->ae_u.term.start);
7778 }
7779 }
7780 }
7781 }
7782}
7783
7784 void
7785screen_stop_highlight()
7786{
7787 int do_ME = FALSE; /* output T_ME code */
7788
7789 if (screen_attr != 0
7790#ifdef WIN3264
7791 && termcap_active
7792#endif
7793 )
7794 {
7795#ifdef FEAT_GUI
7796 if (gui.in_use)
7797 {
7798 char buf[20];
7799
7800 /* use internal GUI code */
7801 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7802 OUT_STR(buf);
7803 }
7804 else
7805#endif
7806 {
7807 if (screen_attr > HL_ALL) /* special HL attr. */
7808 {
7809 attrentry_T *aep;
7810
7811 if (t_colors > 1)
7812 {
7813 /*
7814 * Assume that t_me restores the original colors!
7815 */
7816 aep = syn_cterm_attr2entry(screen_attr);
7817 if (aep != NULL && (aep->ae_u.cterm.fg_color
7818 || aep->ae_u.cterm.bg_color))
7819 do_ME = TRUE;
7820 }
7821 else
7822 {
7823 aep = syn_term_attr2entry(screen_attr);
7824 if (aep != NULL && aep->ae_u.term.stop != NULL)
7825 {
7826 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7827 do_ME = TRUE;
7828 else
7829 out_str(aep->ae_u.term.stop);
7830 }
7831 }
7832 if (aep == NULL) /* did ":syntax clear" */
7833 screen_attr = 0;
7834 else
7835 screen_attr = aep->ae_attr;
7836 }
7837
7838 /*
7839 * Often all ending-codes are equal to T_ME. Avoid outputting the
7840 * same sequence several times.
7841 */
7842 if (screen_attr & HL_STANDOUT)
7843 {
7844 if (STRCMP(T_SE, T_ME) == 0)
7845 do_ME = TRUE;
7846 else
7847 out_str(T_SE);
7848 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007849 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {
7851 if (STRCMP(T_UE, T_ME) == 0)
7852 do_ME = TRUE;
7853 else
7854 out_str(T_UE);
7855 }
7856 if (screen_attr & HL_ITALIC)
7857 {
7858 if (STRCMP(T_CZR, T_ME) == 0)
7859 do_ME = TRUE;
7860 else
7861 out_str(T_CZR);
7862 }
7863 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7864 out_str(T_ME);
7865
7866 if (t_colors > 1)
7867 {
7868 /* set Normal cterm colors */
7869 if (cterm_normal_fg_color != 0)
7870 term_fg_color(cterm_normal_fg_color - 1);
7871 if (cterm_normal_bg_color != 0)
7872 term_bg_color(cterm_normal_bg_color - 1);
7873 if (cterm_normal_fg_bold)
7874 out_str(T_MD);
7875 }
7876 }
7877 }
7878 screen_attr = 0;
7879}
7880
7881/*
7882 * Reset the colors for a cterm. Used when leaving Vim.
7883 * The machine specific code may override this again.
7884 */
7885 void
7886reset_cterm_colors()
7887{
7888 if (t_colors > 1)
7889 {
7890 /* set Normal cterm colors */
7891 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7892 {
7893 out_str(T_OP);
7894 screen_attr = -1;
7895 }
7896 if (cterm_normal_fg_bold)
7897 {
7898 out_str(T_ME);
7899 screen_attr = -1;
7900 }
7901 }
7902}
7903
7904/*
7905 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7906 * using the attributes from ScreenAttrs["off"].
7907 */
7908 static void
7909screen_char(off, row, col)
7910 unsigned off;
7911 int row;
7912 int col;
7913{
7914 int attr;
7915
7916 /* Check for illegal values, just in case (could happen just after
7917 * resizing). */
7918 if (row >= screen_Rows || col >= screen_Columns)
7919 return;
7920
7921 /* Outputting the last character on the screen may scrollup the screen.
7922 * Don't to it! Mark the character invalid (update it when scrolled up) */
7923 if (row == screen_Rows - 1 && col == screen_Columns - 1
7924#ifdef FEAT_RIGHTLEFT
7925 /* account for first command-line character in rightleft mode */
7926 && !cmdmsg_rl
7927#endif
7928 )
7929 {
7930 ScreenAttrs[off] = (sattr_T)-1;
7931 return;
7932 }
7933
7934 /*
7935 * Stop highlighting first, so it's easier to move the cursor.
7936 */
7937#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7938 if (screen_char_attr != 0)
7939 attr = screen_char_attr;
7940 else
7941#endif
7942 attr = ScreenAttrs[off];
7943 if (screen_attr != attr)
7944 screen_stop_highlight();
7945
7946 windgoto(row, col);
7947
7948 if (screen_attr != attr)
7949 screen_start_highlight(attr);
7950
7951#ifdef FEAT_MBYTE
7952 if (enc_utf8 && ScreenLinesUC[off] != 0)
7953 {
7954 char_u buf[MB_MAXBYTES + 1];
7955
7956 /* Convert UTF-8 character to bytes and write it. */
7957
7958 buf[utfc_char2bytes(off, buf)] = NUL;
7959
7960 out_str(buf);
7961 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7962 ++screen_cur_col;
7963 }
7964 else
7965#endif
7966 {
7967#ifdef FEAT_MBYTE
7968 out_flush_check();
7969#endif
7970 out_char(ScreenLines[off]);
7971#ifdef FEAT_MBYTE
7972 /* double-byte character in single-width cell */
7973 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7974 out_char(ScreenLines2[off]);
7975#endif
7976 }
7977
7978 screen_cur_col++;
7979}
7980
7981#ifdef FEAT_MBYTE
7982
7983/*
7984 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7985 * on the screen at position 'row' and 'col'.
7986 * The attributes of the first byte is used for all. This is required to
7987 * output the two bytes of a double-byte character with nothing in between.
7988 */
7989 static void
7990screen_char_2(off, row, col)
7991 unsigned off;
7992 int row;
7993 int col;
7994{
7995 /* Check for illegal values (could be wrong when screen was resized). */
7996 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7997 return;
7998
7999 /* Outputting the last character on the screen may scrollup the screen.
8000 * Don't to it! Mark the character invalid (update it when scrolled up) */
8001 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8002 {
8003 ScreenAttrs[off] = (sattr_T)-1;
8004 return;
8005 }
8006
8007 /* Output the first byte normally (positions the cursor), then write the
8008 * second byte directly. */
8009 screen_char(off, row, col);
8010 out_char(ScreenLines[off + 1]);
8011 ++screen_cur_col;
8012}
8013#endif
8014
8015#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8016/*
8017 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8018 * This uses the contents of ScreenLines[] and doesn't change it.
8019 */
8020 void
8021screen_draw_rectangle(row, col, height, width, invert)
8022 int row;
8023 int col;
8024 int height;
8025 int width;
8026 int invert;
8027{
8028 int r, c;
8029 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008030#ifdef FEAT_MBYTE
8031 int max_off;
8032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008034 /* Can't use ScreenLines unless initialized */
8035 if (ScreenLines == NULL)
8036 return;
8037
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 if (invert)
8039 screen_char_attr = HL_INVERSE;
8040 for (r = row; r < row + height; ++r)
8041 {
8042 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008043#ifdef FEAT_MBYTE
8044 max_off = off + screen_Columns;
8045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 for (c = col; c < col + width; ++c)
8047 {
8048#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008049 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {
8051 screen_char_2(off + c, r, c);
8052 ++c;
8053 }
8054 else
8055#endif
8056 {
8057 screen_char(off + c, r, c);
8058#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008059 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 ++c;
8061#endif
8062 }
8063 }
8064 }
8065 screen_char_attr = 0;
8066}
8067#endif
8068
8069#ifdef FEAT_VERTSPLIT
8070/*
8071 * Redraw the characters for a vertically split window.
8072 */
8073 static void
8074redraw_block(row, end, wp)
8075 int row;
8076 int end;
8077 win_T *wp;
8078{
8079 int col;
8080 int width;
8081
8082# ifdef FEAT_CLIPBOARD
8083 clip_may_clear_selection(row, end - 1);
8084# endif
8085
8086 if (wp == NULL)
8087 {
8088 col = 0;
8089 width = Columns;
8090 }
8091 else
8092 {
8093 col = wp->w_wincol;
8094 width = wp->w_width;
8095 }
8096 screen_draw_rectangle(row, col, end - row, width, FALSE);
8097}
8098#endif
8099
8100/*
8101 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8102 * with character 'c1' in first column followed by 'c2' in the other columns.
8103 * Use attributes 'attr'.
8104 */
8105 void
8106screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8107 int start_row, end_row;
8108 int start_col, end_col;
8109 int c1, c2;
8110 int attr;
8111{
8112 int row;
8113 int col;
8114 int off;
8115 int end_off;
8116 int did_delete;
8117 int c;
8118 int norm_term;
8119#if defined(FEAT_GUI) || defined(UNIX)
8120 int force_next = FALSE;
8121#endif
8122
8123 if (end_row > screen_Rows) /* safety check */
8124 end_row = screen_Rows;
8125 if (end_col > screen_Columns) /* safety check */
8126 end_col = screen_Columns;
8127 if (ScreenLines == NULL
8128 || start_row >= end_row
8129 || start_col >= end_col) /* nothing to do */
8130 return;
8131
8132 /* it's a "normal" terminal when not in a GUI or cterm */
8133 norm_term = (
8134#ifdef FEAT_GUI
8135 !gui.in_use &&
8136#endif
8137 t_colors <= 1);
8138 for (row = start_row; row < end_row; ++row)
8139 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008140#ifdef FEAT_MBYTE
8141 if (has_mbyte
8142# ifdef FEAT_GUI
8143 && !gui.in_use
8144# endif
8145 )
8146 {
8147 /* When drawing over the right halve of a double-wide char clear
8148 * out the left halve. When drawing over the left halve of a
8149 * double wide-char clear out the right halve. Only needed in a
8150 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008151 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008152 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008153 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008154 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008155 }
8156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 /*
8158 * Try to use delete-line termcap code, when no attributes or in a
8159 * "normal" terminal, where a bold/italic space is just a
8160 * space.
8161 */
8162 did_delete = FALSE;
8163 if (c2 == ' '
8164 && end_col == Columns
8165 && can_clear(T_CE)
8166 && (attr == 0
8167 || (norm_term
8168 && attr <= HL_ALL
8169 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8170 {
8171 /*
8172 * check if we really need to clear something
8173 */
8174 col = start_col;
8175 if (c1 != ' ') /* don't clear first char */
8176 ++col;
8177
8178 off = LineOffset[row] + col;
8179 end_off = LineOffset[row] + end_col;
8180
8181 /* skip blanks (used often, keep it fast!) */
8182#ifdef FEAT_MBYTE
8183 if (enc_utf8)
8184 while (off < end_off && ScreenLines[off] == ' '
8185 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8186 ++off;
8187 else
8188#endif
8189 while (off < end_off && ScreenLines[off] == ' '
8190 && ScreenAttrs[off] == 0)
8191 ++off;
8192 if (off < end_off) /* something to be cleared */
8193 {
8194 col = off - LineOffset[row];
8195 screen_stop_highlight();
8196 term_windgoto(row, col);/* clear rest of this screen line */
8197 out_str(T_CE);
8198 screen_start(); /* don't know where cursor is now */
8199 col = end_col - col;
8200 while (col--) /* clear chars in ScreenLines */
8201 {
8202 ScreenLines[off] = ' ';
8203#ifdef FEAT_MBYTE
8204 if (enc_utf8)
8205 ScreenLinesUC[off] = 0;
8206#endif
8207 ScreenAttrs[off] = 0;
8208 ++off;
8209 }
8210 }
8211 did_delete = TRUE; /* the chars are cleared now */
8212 }
8213
8214 off = LineOffset[row] + start_col;
8215 c = c1;
8216 for (col = start_col; col < end_col; ++col)
8217 {
8218 if (ScreenLines[off] != c
8219#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008220 || (enc_utf8 && (int)ScreenLinesUC[off]
8221 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222#endif
8223 || ScreenAttrs[off] != attr
8224#if defined(FEAT_GUI) || defined(UNIX)
8225 || force_next
8226#endif
8227 )
8228 {
8229#if defined(FEAT_GUI) || defined(UNIX)
8230 /* The bold trick may make a single row of pixels appear in
8231 * the next character. When a bold character is removed, the
8232 * next character should be redrawn too. This happens for our
8233 * own GUI and for some xterms. */
8234 if (
8235# ifdef FEAT_GUI
8236 gui.in_use
8237# endif
8238# if defined(FEAT_GUI) && defined(UNIX)
8239 ||
8240# endif
8241# ifdef UNIX
8242 term_is_xterm
8243# endif
8244 )
8245 {
8246 if (ScreenLines[off] != ' '
8247 && (ScreenAttrs[off] > HL_ALL
8248 || ScreenAttrs[off] & HL_BOLD))
8249 force_next = TRUE;
8250 else
8251 force_next = FALSE;
8252 }
8253#endif
8254 ScreenLines[off] = c;
8255#ifdef FEAT_MBYTE
8256 if (enc_utf8)
8257 {
8258 if (c >= 0x80)
8259 {
8260 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008261 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 }
8263 else
8264 ScreenLinesUC[off] = 0;
8265 }
8266#endif
8267 ScreenAttrs[off] = attr;
8268 if (!did_delete || c != ' ')
8269 screen_char(off, row, col);
8270 }
8271 ++off;
8272 if (col == start_col)
8273 {
8274 if (did_delete)
8275 break;
8276 c = c2;
8277 }
8278 }
8279 if (end_col == Columns)
8280 LineWraps[row] = FALSE;
8281 if (row == Rows - 1) /* overwritten the command line */
8282 {
8283 redraw_cmdline = TRUE;
8284 if (c1 == ' ' && c2 == ' ')
8285 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008286 if (start_col == 0)
8287 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 }
8289 }
8290}
8291
8292/*
8293 * Check if there should be a delay. Used before clearing or redrawing the
8294 * screen or the command line.
8295 */
8296 void
8297check_for_delay(check_msg_scroll)
8298 int check_msg_scroll;
8299{
8300 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8301 && !did_wait_return
8302 && emsg_silent == 0)
8303 {
8304 out_flush();
8305 ui_delay(1000L, TRUE);
8306 emsg_on_display = FALSE;
8307 if (check_msg_scroll)
8308 msg_scroll = FALSE;
8309 }
8310}
8311
8312/*
8313 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008314 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 * Returns TRUE if there is a valid screen to write to.
8316 * Returns FALSE when starting up and screen not initialized yet.
8317 */
8318 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008319screen_valid(doclear)
8320 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008322 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 return (ScreenLines != NULL);
8324}
8325
8326/*
8327 * Resize the shell to Rows and Columns.
8328 * Allocate ScreenLines[] and associated items.
8329 *
8330 * There may be some time between setting Rows and Columns and (re)allocating
8331 * ScreenLines[]. This happens when starting up and when (manually) changing
8332 * the shell size. Always use screen_Rows and screen_Columns to access items
8333 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8334 * final size of the shell is needed.
8335 */
8336 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008337screenalloc(doclear)
8338 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339{
8340 int new_row, old_row;
8341#ifdef FEAT_GUI
8342 int old_Rows;
8343#endif
8344 win_T *wp;
8345 int outofmem = FALSE;
8346 int len;
8347 schar_T *new_ScreenLines;
8348#ifdef FEAT_MBYTE
8349 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008350 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008352 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353#endif
8354 sattr_T *new_ScreenAttrs;
8355 unsigned *new_LineOffset;
8356 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008357#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008358 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008359 tabpage_T *tp;
8360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008362 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008363#ifdef FEAT_AUTOCMD
8364 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008366retry:
8367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 /*
8369 * Allocation of the screen buffers is done only when the size changes and
8370 * when Rows and Columns have been set and we have started doing full
8371 * screen stuff.
8372 */
8373 if ((ScreenLines != NULL
8374 && Rows == screen_Rows
8375 && Columns == screen_Columns
8376#ifdef FEAT_MBYTE
8377 && enc_utf8 == (ScreenLinesUC != NULL)
8378 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008379 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380#endif
8381 )
8382 || Rows == 0
8383 || Columns == 0
8384 || (!full_screen && ScreenLines == NULL))
8385 return;
8386
8387 /*
8388 * It's possible that we produce an out-of-memory message below, which
8389 * will cause this function to be called again. To break the loop, just
8390 * return here.
8391 */
8392 if (entered)
8393 return;
8394 entered = TRUE;
8395
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008396 /*
8397 * Note that the window sizes are updated before reallocating the arrays,
8398 * thus we must not redraw here!
8399 */
8400 ++RedrawingDisabled;
8401
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 win_new_shellsize(); /* fit the windows in the new sized shell */
8403
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 comp_col(); /* recompute columns for shown command and ruler */
8405
8406 /*
8407 * We're changing the size of the screen.
8408 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8409 * - Move lines from the old arrays into the new arrays, clear extra
8410 * lines (unless the screen is going to be cleared).
8411 * - Free the old arrays.
8412 *
8413 * If anything fails, make ScreenLines NULL, so we don't do anything!
8414 * Continuing with the old ScreenLines may result in a crash, because the
8415 * size is wrong.
8416 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008417 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008419#ifdef FEAT_AUTOCMD
8420 if (aucmd_win != NULL)
8421 win_free_lsize(aucmd_win);
8422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423
8424 new_ScreenLines = (schar_T *)lalloc((long_u)(
8425 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8426#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008427 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 if (enc_utf8)
8429 {
8430 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8431 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008432 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008433 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8435 }
8436 if (enc_dbcs == DBCS_JPNU)
8437 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8438 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8439#endif
8440 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8441 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8442 new_LineOffset = (unsigned *)lalloc((long_u)(
8443 Rows * sizeof(unsigned)), FALSE);
8444 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008445#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008446 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008449 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {
8451 if (win_alloc_lines(wp) == FAIL)
8452 {
8453 outofmem = TRUE;
8454#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008455 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456#endif
8457 }
8458 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008459#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008460 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8461 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008462 outofmem = TRUE;
8463#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008464#ifdef FEAT_WINDOWS
8465give_up:
8466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008468#ifdef FEAT_MBYTE
8469 for (i = 0; i < p_mco; ++i)
8470 if (new_ScreenLinesC[i] == NULL)
8471 break;
8472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 if (new_ScreenLines == NULL
8474#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008475 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8477#endif
8478 || new_ScreenAttrs == NULL
8479 || new_LineOffset == NULL
8480 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008481#ifdef FEAT_WINDOWS
8482 || new_TabPageIdxs == NULL
8483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 || outofmem)
8485 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008486 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008487 {
8488 /* guess the size */
8489 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8490
8491 /* Remember we did this to avoid getting outofmem messages over
8492 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008493 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 vim_free(new_ScreenLines);
8496 new_ScreenLines = NULL;
8497#ifdef FEAT_MBYTE
8498 vim_free(new_ScreenLinesUC);
8499 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008500 for (i = 0; i < p_mco; ++i)
8501 {
8502 vim_free(new_ScreenLinesC[i]);
8503 new_ScreenLinesC[i] = NULL;
8504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 vim_free(new_ScreenLines2);
8506 new_ScreenLines2 = NULL;
8507#endif
8508 vim_free(new_ScreenAttrs);
8509 new_ScreenAttrs = NULL;
8510 vim_free(new_LineOffset);
8511 new_LineOffset = NULL;
8512 vim_free(new_LineWraps);
8513 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008514#ifdef FEAT_WINDOWS
8515 vim_free(new_TabPageIdxs);
8516 new_TabPageIdxs = NULL;
8517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 }
8519 else
8520 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008521 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008522
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 for (new_row = 0; new_row < Rows; ++new_row)
8524 {
8525 new_LineOffset[new_row] = new_row * Columns;
8526 new_LineWraps[new_row] = FALSE;
8527
8528 /*
8529 * If the screen is not going to be cleared, copy as much as
8530 * possible from the old screen to the new one and clear the rest
8531 * (used when resizing the window at the "--more--" prompt or when
8532 * executing an external command, for the GUI).
8533 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008534 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 {
8536 (void)vim_memset(new_ScreenLines + new_row * Columns,
8537 ' ', (size_t)Columns * sizeof(schar_T));
8538#ifdef FEAT_MBYTE
8539 if (enc_utf8)
8540 {
8541 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8542 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008543 for (i = 0; i < p_mco; ++i)
8544 (void)vim_memset(new_ScreenLinesC[i]
8545 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 0, (size_t)Columns * sizeof(u8char_T));
8547 }
8548 if (enc_dbcs == DBCS_JPNU)
8549 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8550 0, (size_t)Columns * sizeof(schar_T));
8551#endif
8552 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8553 0, (size_t)Columns * sizeof(sattr_T));
8554 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008555 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 {
8557 if (screen_Columns < Columns)
8558 len = screen_Columns;
8559 else
8560 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008561#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008562 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008563 * may be invalid now. Also when p_mco changes. */
8564 if (!(enc_utf8 && ScreenLinesUC == NULL)
8565 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008566#endif
8567 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8568 ScreenLines + LineOffset[old_row],
8569 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008571 if (enc_utf8 && ScreenLinesUC != NULL
8572 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 {
8574 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8575 ScreenLinesUC + LineOffset[old_row],
8576 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008577 for (i = 0; i < p_mco; ++i)
8578 mch_memmove(new_ScreenLinesC[i]
8579 + new_LineOffset[new_row],
8580 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 (size_t)len * sizeof(u8char_T));
8582 }
8583 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8584 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8585 ScreenLines2 + LineOffset[old_row],
8586 (size_t)len * sizeof(schar_T));
8587#endif
8588 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8589 ScreenAttrs + LineOffset[old_row],
8590 (size_t)len * sizeof(sattr_T));
8591 }
8592 }
8593 }
8594 /* Use the last line of the screen for the current line. */
8595 current_ScreenLine = new_ScreenLines + Rows * Columns;
8596 }
8597
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008598 free_screenlines();
8599
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 ScreenLines = new_ScreenLines;
8601#ifdef FEAT_MBYTE
8602 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008603 for (i = 0; i < p_mco; ++i)
8604 ScreenLinesC[i] = new_ScreenLinesC[i];
8605 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 ScreenLines2 = new_ScreenLines2;
8607#endif
8608 ScreenAttrs = new_ScreenAttrs;
8609 LineOffset = new_LineOffset;
8610 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008611#ifdef FEAT_WINDOWS
8612 TabPageIdxs = new_TabPageIdxs;
8613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614
8615 /* It's important that screen_Rows and screen_Columns reflect the actual
8616 * size of ScreenLines[]. Set them before calling anything. */
8617#ifdef FEAT_GUI
8618 old_Rows = screen_Rows;
8619#endif
8620 screen_Rows = Rows;
8621 screen_Columns = Columns;
8622
8623 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008624 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 screenclear2();
8626
8627#ifdef FEAT_GUI
8628 else if (gui.in_use
8629 && !gui.starting
8630 && ScreenLines != NULL
8631 && old_Rows != Rows)
8632 {
8633 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8634 /*
8635 * Adjust the position of the cursor, for when executing an external
8636 * command.
8637 */
8638 if (msg_row >= Rows) /* Rows got smaller */
8639 msg_row = Rows - 1; /* put cursor at last row */
8640 else if (Rows > old_Rows) /* Rows got bigger */
8641 msg_row += Rows - old_Rows; /* put cursor in same place */
8642 if (msg_col >= Columns) /* Columns got smaller */
8643 msg_col = Columns - 1; /* put cursor at last column */
8644 }
8645#endif
8646
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008648 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008649
8650#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008651 /*
8652 * Do not apply autocommands more than 3 times to avoid an endless loop
8653 * in case applying autocommands always changes Rows or Columns.
8654 */
8655 if (starting == 0 && ++retry_count <= 3)
8656 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008657 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008658 /* In rare cases, autocommands may have altered Rows or Columns,
8659 * jump back to check if we need to allocate the screen again. */
8660 goto retry;
8661 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663}
8664
8665 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008666free_screenlines()
8667{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008668#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008669 int i;
8670
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008671 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008672 for (i = 0; i < Screen_mco; ++i)
8673 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008674 vim_free(ScreenLines2);
8675#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008676 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008677 vim_free(ScreenAttrs);
8678 vim_free(LineOffset);
8679 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008680#ifdef FEAT_WINDOWS
8681 vim_free(TabPageIdxs);
8682#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008683}
8684
8685 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686screenclear()
8687{
8688 check_for_delay(FALSE);
8689 screenalloc(FALSE); /* allocate screen buffers if size changed */
8690 screenclear2(); /* clear the screen */
8691}
8692
8693 static void
8694screenclear2()
8695{
8696 int i;
8697
8698 if (starting == NO_SCREEN || ScreenLines == NULL
8699#ifdef FEAT_GUI
8700 || (gui.in_use && gui.starting)
8701#endif
8702 )
8703 return;
8704
8705#ifdef FEAT_GUI
8706 if (!gui.in_use)
8707#endif
8708 screen_attr = -1; /* force setting the Normal colors */
8709 screen_stop_highlight(); /* don't want highlighting here */
8710
8711#ifdef FEAT_CLIPBOARD
8712 /* disable selection without redrawing it */
8713 clip_scroll_selection(9999);
8714#endif
8715
8716 /* blank out ScreenLines */
8717 for (i = 0; i < Rows; ++i)
8718 {
8719 lineclear(LineOffset[i], (int)Columns);
8720 LineWraps[i] = FALSE;
8721 }
8722
8723 if (can_clear(T_CL))
8724 {
8725 out_str(T_CL); /* clear the display */
8726 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008727 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 }
8729 else
8730 {
8731 /* can't clear the screen, mark all chars with invalid attributes */
8732 for (i = 0; i < Rows; ++i)
8733 lineinvalid(LineOffset[i], (int)Columns);
8734 clear_cmdline = TRUE;
8735 }
8736
8737 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8738
8739 win_rest_invalid(firstwin);
8740 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008741#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008742 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 if (must_redraw == CLEAR) /* no need to clear again */
8745 must_redraw = NOT_VALID;
8746 compute_cmdrow();
8747 msg_row = cmdline_row; /* put cursor on last line for messages */
8748 msg_col = 0;
8749 screen_start(); /* don't know where cursor is now */
8750 msg_scrolled = 0; /* can't scroll back */
8751 msg_didany = FALSE;
8752 msg_didout = FALSE;
8753}
8754
8755/*
8756 * Clear one line in ScreenLines.
8757 */
8758 static void
8759lineclear(off, width)
8760 unsigned off;
8761 int width;
8762{
8763 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8764#ifdef FEAT_MBYTE
8765 if (enc_utf8)
8766 (void)vim_memset(ScreenLinesUC + off, 0,
8767 (size_t)width * sizeof(u8char_T));
8768#endif
8769 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8770}
8771
8772/*
8773 * Mark one line in ScreenLines invalid by setting the attributes to an
8774 * invalid value.
8775 */
8776 static void
8777lineinvalid(off, width)
8778 unsigned off;
8779 int width;
8780{
8781 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8782}
8783
8784#ifdef FEAT_VERTSPLIT
8785/*
8786 * Copy part of a Screenline for vertically split window "wp".
8787 */
8788 static void
8789linecopy(to, from, wp)
8790 int to;
8791 int from;
8792 win_T *wp;
8793{
8794 unsigned off_to = LineOffset[to] + wp->w_wincol;
8795 unsigned off_from = LineOffset[from] + wp->w_wincol;
8796
8797 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8798 wp->w_width * sizeof(schar_T));
8799# ifdef FEAT_MBYTE
8800 if (enc_utf8)
8801 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008802 int i;
8803
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8805 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008806 for (i = 0; i < p_mco; ++i)
8807 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8808 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 }
8810 if (enc_dbcs == DBCS_JPNU)
8811 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8812 wp->w_width * sizeof(schar_T));
8813# endif
8814 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8815 wp->w_width * sizeof(sattr_T));
8816}
8817#endif
8818
8819/*
8820 * Return TRUE if clearing with term string "p" would work.
8821 * It can't work when the string is empty or it won't set the right background.
8822 */
8823 int
8824can_clear(p)
8825 char_u *p;
8826{
8827 return (*p != NUL && (t_colors <= 1
8828#ifdef FEAT_GUI
8829 || gui.in_use
8830#endif
8831 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8832}
8833
8834/*
8835 * Reset cursor position. Use whenever cursor was moved because of outputting
8836 * something directly to the screen (shell commands) or a terminal control
8837 * code.
8838 */
8839 void
8840screen_start()
8841{
8842 screen_cur_row = screen_cur_col = 9999;
8843}
8844
8845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 * Move the cursor to position "row","col" in the screen.
8847 * This tries to find the most efficient way to move, minimizing the number of
8848 * characters sent to the terminal.
8849 */
8850 void
8851windgoto(row, col)
8852 int row;
8853 int col;
8854{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008855 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 int i;
8857 int plan;
8858 int cost;
8859 int wouldbe_col;
8860 int noinvcurs;
8861 char_u *bs;
8862 int goto_cost;
8863 int attr;
8864
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008865#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8867
8868#define PLAN_LE 1
8869#define PLAN_CR 2
8870#define PLAN_NL 3
8871#define PLAN_WRITE 4
8872 /* Can't use ScreenLines unless initialized */
8873 if (ScreenLines == NULL)
8874 return;
8875
8876 if (col != screen_cur_col || row != screen_cur_row)
8877 {
8878 /* Check for valid position. */
8879 if (row < 0) /* window without text lines? */
8880 row = 0;
8881 if (row >= screen_Rows)
8882 row = screen_Rows - 1;
8883 if (col >= screen_Columns)
8884 col = screen_Columns - 1;
8885
8886 /* check if no cursor movement is allowed in highlight mode */
8887 if (screen_attr && *T_MS == NUL)
8888 noinvcurs = HIGHL_COST;
8889 else
8890 noinvcurs = 0;
8891 goto_cost = GOTO_COST + noinvcurs;
8892
8893 /*
8894 * Plan how to do the positioning:
8895 * 1. Use CR to move it to column 0, same row.
8896 * 2. Use T_LE to move it a few columns to the left.
8897 * 3. Use NL to move a few lines down, column 0.
8898 * 4. Move a few columns to the right with T_ND or by writing chars.
8899 *
8900 * Don't do this if the cursor went beyond the last column, the cursor
8901 * position is unknown then (some terminals wrap, some don't )
8902 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008903 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 * characters to move the cursor to the right.
8905 */
8906 if (row >= screen_cur_row && screen_cur_col < Columns)
8907 {
8908 /*
8909 * If the cursor is in the same row, bigger col, we can use CR
8910 * or T_LE.
8911 */
8912 bs = NULL; /* init for GCC */
8913 attr = screen_attr;
8914 if (row == screen_cur_row && col < screen_cur_col)
8915 {
8916 /* "le" is preferred over "bc", because "bc" is obsolete */
8917 if (*T_LE)
8918 bs = T_LE; /* "cursor left" */
8919 else
8920 bs = T_BC; /* "backspace character (old) */
8921 if (*bs)
8922 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8923 else
8924 cost = 999;
8925 if (col + 1 < cost) /* using CR is less characters */
8926 {
8927 plan = PLAN_CR;
8928 wouldbe_col = 0;
8929 cost = 1; /* CR is just one character */
8930 }
8931 else
8932 {
8933 plan = PLAN_LE;
8934 wouldbe_col = col;
8935 }
8936 if (noinvcurs) /* will stop highlighting */
8937 {
8938 cost += noinvcurs;
8939 attr = 0;
8940 }
8941 }
8942
8943 /*
8944 * If the cursor is above where we want to be, we can use CR LF.
8945 */
8946 else if (row > screen_cur_row)
8947 {
8948 plan = PLAN_NL;
8949 wouldbe_col = 0;
8950 cost = (row - screen_cur_row) * 2; /* CR LF */
8951 if (noinvcurs) /* will stop highlighting */
8952 {
8953 cost += noinvcurs;
8954 attr = 0;
8955 }
8956 }
8957
8958 /*
8959 * If the cursor is in the same row, smaller col, just use write.
8960 */
8961 else
8962 {
8963 plan = PLAN_WRITE;
8964 wouldbe_col = screen_cur_col;
8965 cost = 0;
8966 }
8967
8968 /*
8969 * Check if any characters that need to be written have the
8970 * correct attributes. Also avoid UTF-8 characters.
8971 */
8972 i = col - wouldbe_col;
8973 if (i > 0)
8974 cost += i;
8975 if (cost < goto_cost && i > 0)
8976 {
8977 /*
8978 * Check if the attributes are correct without additionally
8979 * stopping highlighting.
8980 */
8981 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8982 while (i && *p++ == attr)
8983 --i;
8984 if (i != 0)
8985 {
8986 /*
8987 * Try if it works when highlighting is stopped here.
8988 */
8989 if (*--p == 0)
8990 {
8991 cost += noinvcurs;
8992 while (i && *p++ == 0)
8993 --i;
8994 }
8995 if (i != 0)
8996 cost = 999; /* different attributes, don't do it */
8997 }
8998#ifdef FEAT_MBYTE
8999 if (enc_utf8)
9000 {
9001 /* Don't use an UTF-8 char for positioning, it's slow. */
9002 for (i = wouldbe_col; i < col; ++i)
9003 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9004 {
9005 cost = 999;
9006 break;
9007 }
9008 }
9009#endif
9010 }
9011
9012 /*
9013 * We can do it without term_windgoto()!
9014 */
9015 if (cost < goto_cost)
9016 {
9017 if (plan == PLAN_LE)
9018 {
9019 if (noinvcurs)
9020 screen_stop_highlight();
9021 while (screen_cur_col > col)
9022 {
9023 out_str(bs);
9024 --screen_cur_col;
9025 }
9026 }
9027 else if (plan == PLAN_CR)
9028 {
9029 if (noinvcurs)
9030 screen_stop_highlight();
9031 out_char('\r');
9032 screen_cur_col = 0;
9033 }
9034 else if (plan == PLAN_NL)
9035 {
9036 if (noinvcurs)
9037 screen_stop_highlight();
9038 while (screen_cur_row < row)
9039 {
9040 out_char('\n');
9041 ++screen_cur_row;
9042 }
9043 screen_cur_col = 0;
9044 }
9045
9046 i = col - screen_cur_col;
9047 if (i > 0)
9048 {
9049 /*
9050 * Use cursor-right if it's one character only. Avoids
9051 * removing a line of pixels from the last bold char, when
9052 * using the bold trick in the GUI.
9053 */
9054 if (T_ND[0] != NUL && T_ND[1] == NUL)
9055 {
9056 while (i-- > 0)
9057 out_char(*T_ND);
9058 }
9059 else
9060 {
9061 int off;
9062
9063 off = LineOffset[row] + screen_cur_col;
9064 while (i-- > 0)
9065 {
9066 if (ScreenAttrs[off] != screen_attr)
9067 screen_stop_highlight();
9068#ifdef FEAT_MBYTE
9069 out_flush_check();
9070#endif
9071 out_char(ScreenLines[off]);
9072#ifdef FEAT_MBYTE
9073 if (enc_dbcs == DBCS_JPNU
9074 && ScreenLines[off] == 0x8e)
9075 out_char(ScreenLines2[off]);
9076#endif
9077 ++off;
9078 }
9079 }
9080 }
9081 }
9082 }
9083 else
9084 cost = 999;
9085
9086 if (cost >= goto_cost)
9087 {
9088 if (noinvcurs)
9089 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009090 if (row == screen_cur_row && (col > screen_cur_col)
9091 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 term_cursor_right(col - screen_cur_col);
9093 else
9094 term_windgoto(row, col);
9095 }
9096 screen_cur_row = row;
9097 screen_cur_col = col;
9098 }
9099}
9100
9101/*
9102 * Set cursor to its position in the current window.
9103 */
9104 void
9105setcursor()
9106{
9107 if (redrawing())
9108 {
9109 validate_cursor();
9110 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9111 W_WINCOL(curwin) + (
9112#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009113 /* With 'rightleft' set and the cursor on a double-wide
9114 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9116# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009117 (has_mbyte
9118 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9119 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120# endif
9121 1)) :
9122#endif
9123 curwin->w_wcol));
9124 }
9125}
9126
9127
9128/*
9129 * insert 'line_count' lines at 'row' in window 'wp'
9130 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9131 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9132 * scrolling.
9133 * Returns FAIL if the lines are not inserted, OK for success.
9134 */
9135 int
9136win_ins_lines(wp, row, line_count, invalid, mayclear)
9137 win_T *wp;
9138 int row;
9139 int line_count;
9140 int invalid;
9141 int mayclear;
9142{
9143 int did_delete;
9144 int nextrow;
9145 int lastrow;
9146 int retval;
9147
9148 if (invalid)
9149 wp->w_lines_valid = 0;
9150
9151 if (wp->w_height < 5)
9152 return FAIL;
9153
9154 if (line_count > wp->w_height - row)
9155 line_count = wp->w_height - row;
9156
9157 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9158 if (retval != MAYBE)
9159 return retval;
9160
9161 /*
9162 * If there is a next window or a status line, we first try to delete the
9163 * lines at the bottom to avoid messing what is after the window.
9164 * If this fails and there are following windows, don't do anything to avoid
9165 * messing up those windows, better just redraw.
9166 */
9167 did_delete = FALSE;
9168#ifdef FEAT_WINDOWS
9169 if (wp->w_next != NULL || wp->w_status_height)
9170 {
9171 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9172 line_count, (int)Rows, FALSE, NULL) == OK)
9173 did_delete = TRUE;
9174 else if (wp->w_next)
9175 return FAIL;
9176 }
9177#endif
9178 /*
9179 * if no lines deleted, blank the lines that will end up below the window
9180 */
9181 if (!did_delete)
9182 {
9183#ifdef FEAT_WINDOWS
9184 wp->w_redr_status = TRUE;
9185#endif
9186 redraw_cmdline = TRUE;
9187 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9188 lastrow = nextrow + line_count;
9189 if (lastrow > Rows)
9190 lastrow = Rows;
9191 screen_fill(nextrow - line_count, lastrow - line_count,
9192 W_WINCOL(wp), (int)W_ENDCOL(wp),
9193 ' ', ' ', 0);
9194 }
9195
9196 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9197 == FAIL)
9198 {
9199 /* deletion will have messed up other windows */
9200 if (did_delete)
9201 {
9202#ifdef FEAT_WINDOWS
9203 wp->w_redr_status = TRUE;
9204#endif
9205 win_rest_invalid(W_NEXT(wp));
9206 }
9207 return FAIL;
9208 }
9209
9210 return OK;
9211}
9212
9213/*
9214 * delete "line_count" window lines at "row" in window "wp"
9215 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9216 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9217 * scrolling
9218 * Return OK for success, FAIL if the lines are not deleted.
9219 */
9220 int
9221win_del_lines(wp, row, line_count, invalid, mayclear)
9222 win_T *wp;
9223 int row;
9224 int line_count;
9225 int invalid;
9226 int mayclear;
9227{
9228 int retval;
9229
9230 if (invalid)
9231 wp->w_lines_valid = 0;
9232
9233 if (line_count > wp->w_height - row)
9234 line_count = wp->w_height - row;
9235
9236 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9237 if (retval != MAYBE)
9238 return retval;
9239
9240 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9241 (int)Rows, FALSE, NULL) == FAIL)
9242 return FAIL;
9243
9244#ifdef FEAT_WINDOWS
9245 /*
9246 * If there are windows or status lines below, try to put them at the
9247 * correct place. If we can't do that, they have to be redrawn.
9248 */
9249 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9250 {
9251 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9252 line_count, (int)Rows, NULL) == FAIL)
9253 {
9254 wp->w_redr_status = TRUE;
9255 win_rest_invalid(wp->w_next);
9256 }
9257 }
9258 /*
9259 * If this is the last window and there is no status line, redraw the
9260 * command line later.
9261 */
9262 else
9263#endif
9264 redraw_cmdline = TRUE;
9265 return OK;
9266}
9267
9268/*
9269 * Common code for win_ins_lines() and win_del_lines().
9270 * Returns OK or FAIL when the work has been done.
9271 * Returns MAYBE when not finished yet.
9272 */
9273 static int
9274win_do_lines(wp, row, line_count, mayclear, del)
9275 win_T *wp;
9276 int row;
9277 int line_count;
9278 int mayclear;
9279 int del;
9280{
9281 int retval;
9282
9283 if (!redrawing() || line_count <= 0)
9284 return FAIL;
9285
9286 /* only a few lines left: redraw is faster */
9287 if (mayclear && Rows - line_count < 5
9288#ifdef FEAT_VERTSPLIT
9289 && wp->w_width == Columns
9290#endif
9291 )
9292 {
9293 screenclear(); /* will set wp->w_lines_valid to 0 */
9294 return FAIL;
9295 }
9296
9297 /*
9298 * Delete all remaining lines
9299 */
9300 if (row + line_count >= wp->w_height)
9301 {
9302 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9303 W_WINCOL(wp), (int)W_ENDCOL(wp),
9304 ' ', ' ', 0);
9305 return OK;
9306 }
9307
9308 /*
9309 * when scrolling, the message on the command line should be cleared,
9310 * otherwise it will stay there forever.
9311 */
9312 clear_cmdline = TRUE;
9313
9314 /*
9315 * If the terminal can set a scroll region, use that.
9316 * Always do this in a vertically split window. This will redraw from
9317 * ScreenLines[] when t_CV isn't defined. That's faster than using
9318 * win_line().
9319 * Don't use a scroll region when we are going to redraw the text, writing
9320 * a character in the lower right corner of the scroll region causes a
9321 * scroll-up in the DJGPP version.
9322 */
9323 if (scroll_region
9324#ifdef FEAT_VERTSPLIT
9325 || W_WIDTH(wp) != Columns
9326#endif
9327 )
9328 {
9329#ifdef FEAT_VERTSPLIT
9330 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9331#endif
9332 scroll_region_set(wp, row);
9333 if (del)
9334 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9335 wp->w_height - row, FALSE, wp);
9336 else
9337 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9338 wp->w_height - row, wp);
9339#ifdef FEAT_VERTSPLIT
9340 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9341#endif
9342 scroll_region_reset();
9343 return retval;
9344 }
9345
9346#ifdef FEAT_WINDOWS
9347 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9348 return FAIL;
9349#endif
9350
9351 return MAYBE;
9352}
9353
9354/*
9355 * window 'wp' and everything after it is messed up, mark it for redraw
9356 */
9357 static void
9358win_rest_invalid(wp)
9359 win_T *wp;
9360{
9361#ifdef FEAT_WINDOWS
9362 while (wp != NULL)
9363#else
9364 if (wp != NULL)
9365#endif
9366 {
9367 redraw_win_later(wp, NOT_VALID);
9368#ifdef FEAT_WINDOWS
9369 wp->w_redr_status = TRUE;
9370 wp = wp->w_next;
9371#endif
9372 }
9373 redraw_cmdline = TRUE;
9374}
9375
9376/*
9377 * The rest of the routines in this file perform screen manipulations. The
9378 * given operation is performed physically on the screen. The corresponding
9379 * change is also made to the internal screen image. In this way, the editor
9380 * anticipates the effect of editing changes on the appearance of the screen.
9381 * That way, when we call screenupdate a complete redraw isn't usually
9382 * necessary. Another advantage is that we can keep adding code to anticipate
9383 * screen changes, and in the meantime, everything still works.
9384 */
9385
9386/*
9387 * types for inserting or deleting lines
9388 */
9389#define USE_T_CAL 1
9390#define USE_T_CDL 2
9391#define USE_T_AL 3
9392#define USE_T_CE 4
9393#define USE_T_DL 5
9394#define USE_T_SR 6
9395#define USE_NL 7
9396#define USE_T_CD 8
9397#define USE_REDRAW 9
9398
9399/*
9400 * insert lines on the screen and update ScreenLines[]
9401 * 'end' is the line after the scrolled part. Normally it is Rows.
9402 * When scrolling region used 'off' is the offset from the top for the region.
9403 * 'row' and 'end' are relative to the start of the region.
9404 *
9405 * return FAIL for failure, OK for success.
9406 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009407 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408screen_ins_lines(off, row, line_count, end, wp)
9409 int off;
9410 int row;
9411 int line_count;
9412 int end;
9413 win_T *wp; /* NULL or window to use width from */
9414{
9415 int i;
9416 int j;
9417 unsigned temp;
9418 int cursor_row;
9419 int type;
9420 int result_empty;
9421 int can_ce = can_clear(T_CE);
9422
9423 /*
9424 * FAIL if
9425 * - there is no valid screen
9426 * - the screen has to be redrawn completely
9427 * - the line count is less than one
9428 * - the line count is more than 'ttyscroll'
9429 */
9430 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9431 return FAIL;
9432
9433 /*
9434 * There are seven ways to insert lines:
9435 * 0. When in a vertically split window and t_CV isn't set, redraw the
9436 * characters from ScreenLines[].
9437 * 1. Use T_CD (clear to end of display) if it exists and the result of
9438 * the insert is just empty lines
9439 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9440 * present or line_count > 1. It looks better if we do all the inserts
9441 * at once.
9442 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9443 * insert is just empty lines and T_CE is not present or line_count >
9444 * 1.
9445 * 4. Use T_AL (insert line) if it exists.
9446 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9447 * just empty lines.
9448 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9449 * just empty lines.
9450 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9451 * the 'da' flag is not set or we have clear line capability.
9452 * 8. redraw the characters from ScreenLines[].
9453 *
9454 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9455 * the scrollbar for the window. It does have insert line, use that if it
9456 * exists.
9457 */
9458 result_empty = (row + line_count >= end);
9459#ifdef FEAT_VERTSPLIT
9460 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9461 type = USE_REDRAW;
9462 else
9463#endif
9464 if (can_clear(T_CD) && result_empty)
9465 type = USE_T_CD;
9466 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9467 type = USE_T_CAL;
9468 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9469 type = USE_T_CDL;
9470 else if (*T_AL != NUL)
9471 type = USE_T_AL;
9472 else if (can_ce && result_empty)
9473 type = USE_T_CE;
9474 else if (*T_DL != NUL && result_empty)
9475 type = USE_T_DL;
9476 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9477 type = USE_T_SR;
9478 else
9479 return FAIL;
9480
9481 /*
9482 * For clearing the lines screen_del_lines() is used. This will also take
9483 * care of t_db if necessary.
9484 */
9485 if (type == USE_T_CD || type == USE_T_CDL ||
9486 type == USE_T_CE || type == USE_T_DL)
9487 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9488
9489 /*
9490 * If text is retained below the screen, first clear or delete as many
9491 * lines at the bottom of the window as are about to be inserted so that
9492 * the deleted lines won't later surface during a screen_del_lines.
9493 */
9494 if (*T_DB)
9495 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9496
9497#ifdef FEAT_CLIPBOARD
9498 /* Remove a modeless selection when inserting lines halfway the screen
9499 * or not the full width of the screen. */
9500 if (off + row > 0
9501# ifdef FEAT_VERTSPLIT
9502 || (wp != NULL && wp->w_width != Columns)
9503# endif
9504 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009505 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 else
9507 clip_scroll_selection(-line_count);
9508#endif
9509
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510#ifdef FEAT_GUI
9511 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9512 * scrolling is actually carried out. */
9513 gui_dont_update_cursor();
9514#endif
9515
9516 if (*T_CCS != NUL) /* cursor relative to region */
9517 cursor_row = row;
9518 else
9519 cursor_row = row + off;
9520
9521 /*
9522 * Shift LineOffset[] line_count down to reflect the inserted lines.
9523 * Clear the inserted lines in ScreenLines[].
9524 */
9525 row += off;
9526 end += off;
9527 for (i = 0; i < line_count; ++i)
9528 {
9529#ifdef FEAT_VERTSPLIT
9530 if (wp != NULL && wp->w_width != Columns)
9531 {
9532 /* need to copy part of a line */
9533 j = end - 1 - i;
9534 while ((j -= line_count) >= row)
9535 linecopy(j + line_count, j, wp);
9536 j += line_count;
9537 if (can_clear((char_u *)" "))
9538 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9539 else
9540 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9541 LineWraps[j] = FALSE;
9542 }
9543 else
9544#endif
9545 {
9546 j = end - 1 - i;
9547 temp = LineOffset[j];
9548 while ((j -= line_count) >= row)
9549 {
9550 LineOffset[j + line_count] = LineOffset[j];
9551 LineWraps[j + line_count] = LineWraps[j];
9552 }
9553 LineOffset[j + line_count] = temp;
9554 LineWraps[j + line_count] = FALSE;
9555 if (can_clear((char_u *)" "))
9556 lineclear(temp, (int)Columns);
9557 else
9558 lineinvalid(temp, (int)Columns);
9559 }
9560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561
9562 screen_stop_highlight();
9563 windgoto(cursor_row, 0);
9564
9565#ifdef FEAT_VERTSPLIT
9566 /* redraw the characters */
9567 if (type == USE_REDRAW)
9568 redraw_block(row, end, wp);
9569 else
9570#endif
9571 if (type == USE_T_CAL)
9572 {
9573 term_append_lines(line_count);
9574 screen_start(); /* don't know where cursor is now */
9575 }
9576 else
9577 {
9578 for (i = 0; i < line_count; i++)
9579 {
9580 if (type == USE_T_AL)
9581 {
9582 if (i && cursor_row != 0)
9583 windgoto(cursor_row, 0);
9584 out_str(T_AL);
9585 }
9586 else /* type == USE_T_SR */
9587 out_str(T_SR);
9588 screen_start(); /* don't know where cursor is now */
9589 }
9590 }
9591
9592 /*
9593 * With scroll-reverse and 'da' flag set we need to clear the lines that
9594 * have been scrolled down into the region.
9595 */
9596 if (type == USE_T_SR && *T_DA)
9597 {
9598 for (i = 0; i < line_count; ++i)
9599 {
9600 windgoto(off + i, 0);
9601 out_str(T_CE);
9602 screen_start(); /* don't know where cursor is now */
9603 }
9604 }
9605
9606#ifdef FEAT_GUI
9607 gui_can_update_cursor();
9608 if (gui.in_use)
9609 out_flush(); /* always flush after a scroll */
9610#endif
9611 return OK;
9612}
9613
9614/*
9615 * delete lines on the screen and update ScreenLines[]
9616 * 'end' is the line after the scrolled part. Normally it is Rows.
9617 * When scrolling region used 'off' is the offset from the top for the region.
9618 * 'row' and 'end' are relative to the start of the region.
9619 *
9620 * Return OK for success, FAIL if the lines are not deleted.
9621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 int
9623screen_del_lines(off, row, line_count, end, force, wp)
9624 int off;
9625 int row;
9626 int line_count;
9627 int end;
9628 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009629 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630{
9631 int j;
9632 int i;
9633 unsigned temp;
9634 int cursor_row;
9635 int cursor_end;
9636 int result_empty; /* result is empty until end of region */
9637 int can_delete; /* deleting line codes can be used */
9638 int type;
9639
9640 /*
9641 * FAIL if
9642 * - there is no valid screen
9643 * - the screen has to be redrawn completely
9644 * - the line count is less than one
9645 * - the line count is more than 'ttyscroll'
9646 */
9647 if (!screen_valid(TRUE) || line_count <= 0 ||
9648 (!force && line_count > p_ttyscroll))
9649 return FAIL;
9650
9651 /*
9652 * Check if the rest of the current region will become empty.
9653 */
9654 result_empty = row + line_count >= end;
9655
9656 /*
9657 * We can delete lines only when 'db' flag not set or when 'ce' option
9658 * available.
9659 */
9660 can_delete = (*T_DB == NUL || can_clear(T_CE));
9661
9662 /*
9663 * There are six ways to delete lines:
9664 * 0. When in a vertically split window and t_CV isn't set, redraw the
9665 * characters from ScreenLines[].
9666 * 1. Use T_CD if it exists and the result is empty.
9667 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9668 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9669 * none of the other ways work.
9670 * 4. Use T_CE (erase line) if the result is empty.
9671 * 5. Use T_DL (delete line) if it exists.
9672 * 6. redraw the characters from ScreenLines[].
9673 */
9674#ifdef FEAT_VERTSPLIT
9675 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9676 type = USE_REDRAW;
9677 else
9678#endif
9679 if (can_clear(T_CD) && result_empty)
9680 type = USE_T_CD;
9681#if defined(__BEOS__) && defined(BEOS_DR8)
9682 /*
9683 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9684 * its internal termcap... this works okay for tests which test *T_DB !=
9685 * NUL. It has the disadvantage that the user cannot use any :set t_*
9686 * command to get T_DB (back) to empty_option, only :set term=... will do
9687 * the trick...
9688 * Anyway, this hack will hopefully go away with the next OS release.
9689 * (Olaf Seibert)
9690 */
9691 else if (row == 0 && T_DB == empty_option
9692 && (line_count == 1 || *T_CDL == NUL))
9693#else
9694 else if (row == 0 && (
9695#ifndef AMIGA
9696 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9697 * up, so use delete-line command */
9698 line_count == 1 ||
9699#endif
9700 *T_CDL == NUL))
9701#endif
9702 type = USE_NL;
9703 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9704 type = USE_T_CDL;
9705 else if (can_clear(T_CE) && result_empty
9706#ifdef FEAT_VERTSPLIT
9707 && (wp == NULL || wp->w_width == Columns)
9708#endif
9709 )
9710 type = USE_T_CE;
9711 else if (*T_DL != NUL && can_delete)
9712 type = USE_T_DL;
9713 else if (*T_CDL != NUL && can_delete)
9714 type = USE_T_CDL;
9715 else
9716 return FAIL;
9717
9718#ifdef FEAT_CLIPBOARD
9719 /* Remove a modeless selection when deleting lines halfway the screen or
9720 * not the full width of the screen. */
9721 if (off + row > 0
9722# ifdef FEAT_VERTSPLIT
9723 || (wp != NULL && wp->w_width != Columns)
9724# endif
9725 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009726 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 else
9728 clip_scroll_selection(line_count);
9729#endif
9730
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731#ifdef FEAT_GUI
9732 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9733 * scrolling is actually carried out. */
9734 gui_dont_update_cursor();
9735#endif
9736
9737 if (*T_CCS != NUL) /* cursor relative to region */
9738 {
9739 cursor_row = row;
9740 cursor_end = end;
9741 }
9742 else
9743 {
9744 cursor_row = row + off;
9745 cursor_end = end + off;
9746 }
9747
9748 /*
9749 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9750 * Clear the inserted lines in ScreenLines[].
9751 */
9752 row += off;
9753 end += off;
9754 for (i = 0; i < line_count; ++i)
9755 {
9756#ifdef FEAT_VERTSPLIT
9757 if (wp != NULL && wp->w_width != Columns)
9758 {
9759 /* need to copy part of a line */
9760 j = row + i;
9761 while ((j += line_count) <= end - 1)
9762 linecopy(j - line_count, j, wp);
9763 j -= line_count;
9764 if (can_clear((char_u *)" "))
9765 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9766 else
9767 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9768 LineWraps[j] = FALSE;
9769 }
9770 else
9771#endif
9772 {
9773 /* whole width, moving the line pointers is faster */
9774 j = row + i;
9775 temp = LineOffset[j];
9776 while ((j += line_count) <= end - 1)
9777 {
9778 LineOffset[j - line_count] = LineOffset[j];
9779 LineWraps[j - line_count] = LineWraps[j];
9780 }
9781 LineOffset[j - line_count] = temp;
9782 LineWraps[j - line_count] = FALSE;
9783 if (can_clear((char_u *)" "))
9784 lineclear(temp, (int)Columns);
9785 else
9786 lineinvalid(temp, (int)Columns);
9787 }
9788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789
9790 screen_stop_highlight();
9791
9792#ifdef FEAT_VERTSPLIT
9793 /* redraw the characters */
9794 if (type == USE_REDRAW)
9795 redraw_block(row, end, wp);
9796 else
9797#endif
9798 if (type == USE_T_CD) /* delete the lines */
9799 {
9800 windgoto(cursor_row, 0);
9801 out_str(T_CD);
9802 screen_start(); /* don't know where cursor is now */
9803 }
9804 else if (type == USE_T_CDL)
9805 {
9806 windgoto(cursor_row, 0);
9807 term_delete_lines(line_count);
9808 screen_start(); /* don't know where cursor is now */
9809 }
9810 /*
9811 * Deleting lines at top of the screen or scroll region: Just scroll
9812 * the whole screen (scroll region) up by outputting newlines on the
9813 * last line.
9814 */
9815 else if (type == USE_NL)
9816 {
9817 windgoto(cursor_end - 1, 0);
9818 for (i = line_count; --i >= 0; )
9819 out_char('\n'); /* cursor will remain on same line */
9820 }
9821 else
9822 {
9823 for (i = line_count; --i >= 0; )
9824 {
9825 if (type == USE_T_DL)
9826 {
9827 windgoto(cursor_row, 0);
9828 out_str(T_DL); /* delete a line */
9829 }
9830 else /* type == USE_T_CE */
9831 {
9832 windgoto(cursor_row + i, 0);
9833 out_str(T_CE); /* erase a line */
9834 }
9835 screen_start(); /* don't know where cursor is now */
9836 }
9837 }
9838
9839 /*
9840 * If the 'db' flag is set, we need to clear the lines that have been
9841 * scrolled up at the bottom of the region.
9842 */
9843 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9844 {
9845 for (i = line_count; i > 0; --i)
9846 {
9847 windgoto(cursor_end - i, 0);
9848 out_str(T_CE); /* erase a line */
9849 screen_start(); /* don't know where cursor is now */
9850 }
9851 }
9852
9853#ifdef FEAT_GUI
9854 gui_can_update_cursor();
9855 if (gui.in_use)
9856 out_flush(); /* always flush after a scroll */
9857#endif
9858
9859 return OK;
9860}
9861
9862/*
9863 * show the current mode and ruler
9864 *
9865 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9866 * If clear_cmdline is FALSE there may be a message there that needs to be
9867 * cleared only if a mode is shown.
9868 * Return the length of the message (0 if no message).
9869 */
9870 int
9871showmode()
9872{
9873 int need_clear;
9874 int length = 0;
9875 int do_mode;
9876 int attr;
9877 int nwr_save;
9878#ifdef FEAT_INS_EXPAND
9879 int sub_attr;
9880#endif
9881
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009882 do_mode = ((p_smd && msg_silent == 0)
9883 && ((State & INSERT)
9884 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009885 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 if (do_mode || Recording)
9887 {
9888 /*
9889 * Don't show mode right now, when not redrawing or inside a mapping.
9890 * Call char_avail() only when we are going to show something, because
9891 * it takes a bit of time.
9892 */
9893 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9894 {
9895 redraw_cmdline = TRUE; /* show mode later */
9896 return 0;
9897 }
9898
9899 nwr_save = need_wait_return;
9900
9901 /* wait a bit before overwriting an important message */
9902 check_for_delay(FALSE);
9903
9904 /* if the cmdline is more than one line high, erase top lines */
9905 need_clear = clear_cmdline;
9906 if (clear_cmdline && cmdline_row < Rows - 1)
9907 msg_clr_cmdline(); /* will reset clear_cmdline */
9908
9909 /* Position on the last line in the window, column 0 */
9910 msg_pos_mode();
9911 cursor_off();
9912 attr = hl_attr(HLF_CM); /* Highlight mode */
9913 if (do_mode)
9914 {
9915 MSG_PUTS_ATTR("--", attr);
9916#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009917 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009918# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009919 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009920# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009921 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009922# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009923 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009924# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 MSG_PUTS_ATTR(" IM", attr);
9926# else
9927 MSG_PUTS_ATTR(" XIM", attr);
9928# endif
9929#endif
9930#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9931 if (gui.in_use)
9932 {
9933 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009934 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 }
9936#endif
9937#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009938 /* CTRL-X in Insert mode */
9939 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 {
9941 /* These messages can get long, avoid a wrap in a narrow
9942 * window. Prefer showing edit_submode_extra. */
9943 length = (Rows - msg_row) * Columns - 3;
9944 if (edit_submode_extra != NULL)
9945 length -= vim_strsize(edit_submode_extra);
9946 if (length > 0)
9947 {
9948 if (edit_submode_pre != NULL)
9949 length -= vim_strsize(edit_submode_pre);
9950 if (length - vim_strsize(edit_submode) > 0)
9951 {
9952 if (edit_submode_pre != NULL)
9953 msg_puts_attr(edit_submode_pre, attr);
9954 msg_puts_attr(edit_submode, attr);
9955 }
9956 if (edit_submode_extra != NULL)
9957 {
9958 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9959 if ((int)edit_submode_highl < (int)HLF_COUNT)
9960 sub_attr = hl_attr(edit_submode_highl);
9961 else
9962 sub_attr = attr;
9963 msg_puts_attr(edit_submode_extra, sub_attr);
9964 }
9965 }
9966 length = 0;
9967 }
9968 else
9969#endif
9970 {
9971#ifdef FEAT_VREPLACE
9972 if (State & VREPLACE_FLAG)
9973 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9974 else
9975#endif
9976 if (State & REPLACE_FLAG)
9977 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9978 else if (State & INSERT)
9979 {
9980#ifdef FEAT_RIGHTLEFT
9981 if (p_ri)
9982 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9983#endif
9984 MSG_PUTS_ATTR(_(" INSERT"), attr);
9985 }
9986 else if (restart_edit == 'I')
9987 MSG_PUTS_ATTR(_(" (insert)"), attr);
9988 else if (restart_edit == 'R')
9989 MSG_PUTS_ATTR(_(" (replace)"), attr);
9990 else if (restart_edit == 'V')
9991 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9992#ifdef FEAT_RIGHTLEFT
9993 if (p_hkmap)
9994 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9995# ifdef FEAT_FKMAP
9996 if (p_fkmap)
9997 MSG_PUTS_ATTR(farsi_text_5, attr);
9998# endif
9999#endif
10000#ifdef FEAT_KEYMAP
10001 if (State & LANGMAP)
10002 {
10003# ifdef FEAT_ARABIC
10004 if (curwin->w_p_arab)
10005 MSG_PUTS_ATTR(_(" Arabic"), attr);
10006 else
10007# endif
10008 MSG_PUTS_ATTR(_(" (lang)"), attr);
10009 }
10010#endif
10011 if ((State & INSERT) && p_paste)
10012 MSG_PUTS_ATTR(_(" (paste)"), attr);
10013
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 if (VIsual_active)
10015 {
10016 char *p;
10017
10018 /* Don't concatenate separate words to avoid translation
10019 * problems. */
10020 switch ((VIsual_select ? 4 : 0)
10021 + (VIsual_mode == Ctrl_V) * 2
10022 + (VIsual_mode == 'V'))
10023 {
10024 case 0: p = N_(" VISUAL"); break;
10025 case 1: p = N_(" VISUAL LINE"); break;
10026 case 2: p = N_(" VISUAL BLOCK"); break;
10027 case 4: p = N_(" SELECT"); break;
10028 case 5: p = N_(" SELECT LINE"); break;
10029 default: p = N_(" SELECT BLOCK"); break;
10030 }
10031 MSG_PUTS_ATTR(_(p), attr);
10032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 MSG_PUTS_ATTR(" --", attr);
10034 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010035
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 need_clear = TRUE;
10037 }
10038 if (Recording
10039#ifdef FEAT_INS_EXPAND
10040 && edit_submode == NULL /* otherwise it gets too long */
10041#endif
10042 )
10043 {
10044 MSG_PUTS_ATTR(_("recording"), attr);
10045 need_clear = TRUE;
10046 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010047
10048 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (need_clear || clear_cmdline)
10050 msg_clr_eos();
10051 msg_didout = FALSE; /* overwrite this message */
10052 length = msg_col;
10053 msg_col = 0;
10054 need_wait_return = nwr_save; /* never ask for hit-return for this */
10055 }
10056 else if (clear_cmdline && msg_silent == 0)
10057 /* Clear the whole command line. Will reset "clear_cmdline". */
10058 msg_clr_cmdline();
10059
10060#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 /* In Visual mode the size of the selected area must be redrawn. */
10062 if (VIsual_active)
10063 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064
10065 /* If the last window has no status line, the ruler is after the mode
10066 * message and must be redrawn */
10067 if (redrawing()
10068# ifdef FEAT_WINDOWS
10069 && lastwin->w_status_height == 0
10070# endif
10071 )
10072 win_redr_ruler(lastwin, TRUE);
10073#endif
10074 redraw_cmdline = FALSE;
10075 clear_cmdline = FALSE;
10076
10077 return length;
10078}
10079
10080/*
10081 * Position for a mode message.
10082 */
10083 static void
10084msg_pos_mode()
10085{
10086 msg_col = 0;
10087 msg_row = Rows - 1;
10088}
10089
10090/*
10091 * Delete mode message. Used when ESC is typed which is expected to end
10092 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010093 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 */
10095 void
10096unshowmode(force)
10097 int force;
10098{
10099 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010100 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 */
10102 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10103 redraw_cmdline = TRUE; /* delete mode later */
10104 else
10105 {
10106 msg_pos_mode();
10107 if (Recording)
10108 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
10109 msg_clr_eos();
10110 }
10111}
10112
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010113#if defined(FEAT_WINDOWS)
10114/*
10115 * Draw the tab pages line at the top of the Vim window.
10116 */
10117 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010118draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010119{
10120 int tabcount = 0;
10121 tabpage_T *tp;
10122 int tabwidth;
10123 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010124 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010125 int attr;
10126 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010127 win_T *cwp;
10128 int wincount;
10129 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010130 int c;
10131 int len;
10132 int attr_sel = hl_attr(HLF_TPS);
10133 int attr_nosel = hl_attr(HLF_TP);
10134 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010135 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010136 int room;
10137 int use_sep_chars = (t_colors < 8
10138#ifdef FEAT_GUI
10139 && !gui.in_use
10140#endif
10141 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010142
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010143 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010144
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010145#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010146 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010147 if (gui_use_tabline())
10148 {
10149 gui_update_tabline();
10150 return;
10151 }
10152#endif
10153
10154 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010155 return;
10156
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010157#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010158
10159 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10160 for (scol = 0; scol < Columns; ++scol)
10161 TabPageIdxs[scol] = 0;
10162
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010163 /* Use the 'tabline' option if it's set. */
10164 if (*p_tal != NUL)
10165 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010166 int save_called_emsg = called_emsg;
10167
10168 /* Check for an error. If there is one we would loop in redrawing the
10169 * screen. Avoid that by making 'tabline' empty. */
10170 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010171 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010172 if (called_emsg)
10173 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010174 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010175 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010176 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010177 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010178#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010179 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010180 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10181 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010182
Bram Moolenaar238a5642006-02-21 22:12:05 +000010183 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10184 if (tabwidth < 6)
10185 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010186
Bram Moolenaar238a5642006-02-21 22:12:05 +000010187 attr = attr_nosel;
10188 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010189 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010190 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10191 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010192 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010193 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010194
Bram Moolenaar238a5642006-02-21 22:12:05 +000010195 if (tp->tp_topframe == topframe)
10196 attr = attr_sel;
10197 if (use_sep_chars && col > 0)
10198 screen_putchar('|', 0, col++, attr);
10199
10200 if (tp->tp_topframe != topframe)
10201 attr = attr_nosel;
10202
10203 screen_putchar(' ', 0, col++, attr);
10204
10205 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010206 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010207 cwp = curwin;
10208 wp = firstwin;
10209 }
10210 else
10211 {
10212 cwp = tp->tp_curwin;
10213 wp = tp->tp_firstwin;
10214 }
10215
10216 modified = FALSE;
10217 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10218 if (bufIsChanged(wp->w_buffer))
10219 modified = TRUE;
10220 if (modified || wincount > 1)
10221 {
10222 if (wincount > 1)
10223 {
10224 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010225 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010226 if (col + len >= Columns - 3)
10227 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010228 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010229#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010230 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010231#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010232 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010233#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010234 );
10235 col += len;
10236 }
10237 if (modified)
10238 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10239 screen_putchar(' ', 0, col++, attr);
10240 }
10241
10242 room = scol - col + tabwidth - 1;
10243 if (room > 0)
10244 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010245 /* Get buffer name in NameBuff[] */
10246 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010247 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010248 len = vim_strsize(NameBuff);
10249 p = NameBuff;
10250#ifdef FEAT_MBYTE
10251 if (has_mbyte)
10252 while (len > room)
10253 {
10254 len -= ptr2cells(p);
10255 mb_ptr_adv(p);
10256 }
10257 else
10258#endif
10259 if (len > room)
10260 {
10261 p += len - room;
10262 len = room;
10263 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010264 if (len > Columns - col - 1)
10265 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010266
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010267 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010268 col += len;
10269 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010270 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010271
10272 /* Store the tab page number in TabPageIdxs[], so that
10273 * jump_to_mouse() knows where each one is. */
10274 ++tabcount;
10275 while (scol < col)
10276 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010277 }
10278
Bram Moolenaar238a5642006-02-21 22:12:05 +000010279 if (use_sep_chars)
10280 c = '_';
10281 else
10282 c = ' ';
10283 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010284
10285 /* Put an "X" for closing the current tab if there are several. */
10286 if (first_tabpage->tp_next != NULL)
10287 {
10288 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10289 TabPageIdxs[Columns - 1] = -999;
10290 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010291 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010292
10293 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10294 * set. */
10295 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010296}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010297
10298/*
10299 * Get buffer name for "buf" into NameBuff[].
10300 * Takes care of special buffer names and translates special characters.
10301 */
10302 void
10303get_trans_bufname(buf)
10304 buf_T *buf;
10305{
10306 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010307 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010308 else
10309 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10310 trans_characters(NameBuff, MAXPATHL);
10311}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010312#endif
10313
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10315/*
10316 * Get the character to use in a status line. Get its attributes in "*attr".
10317 */
10318 static int
10319fillchar_status(attr, is_curwin)
10320 int *attr;
10321 int is_curwin;
10322{
10323 int fill;
10324 if (is_curwin)
10325 {
10326 *attr = hl_attr(HLF_S);
10327 fill = fill_stl;
10328 }
10329 else
10330 {
10331 *attr = hl_attr(HLF_SNC);
10332 fill = fill_stlnc;
10333 }
10334 /* Use fill when there is highlighting, and highlighting of current
10335 * window differs, or the fillchars differ, or this is not the
10336 * current window */
10337 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10338 || !is_curwin || firstwin == lastwin)
10339 || (fill_stl != fill_stlnc)))
10340 return fill;
10341 if (is_curwin)
10342 return '^';
10343 return '=';
10344}
10345#endif
10346
10347#ifdef FEAT_VERTSPLIT
10348/*
10349 * Get the character to use in a separator between vertically split windows.
10350 * Get its attributes in "*attr".
10351 */
10352 static int
10353fillchar_vsep(attr)
10354 int *attr;
10355{
10356 *attr = hl_attr(HLF_C);
10357 if (*attr == 0 && fill_vert == ' ')
10358 return '|';
10359 else
10360 return fill_vert;
10361}
10362#endif
10363
10364/*
10365 * Return TRUE if redrawing should currently be done.
10366 */
10367 int
10368redrawing()
10369{
10370 return (!RedrawingDisabled
10371 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10372}
10373
10374/*
10375 * Return TRUE if printing messages should currently be done.
10376 */
10377 int
10378messaging()
10379{
10380 return (!(p_lz && char_avail() && !KeyTyped));
10381}
10382
10383/*
10384 * Show current status info in ruler and various other places
10385 * If always is FALSE, only show ruler if position has changed.
10386 */
10387 void
10388showruler(always)
10389 int always;
10390{
10391 if (!always && !redrawing())
10392 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010393#ifdef FEAT_INS_EXPAND
10394 if (pum_visible())
10395 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010396# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010397 /* Don't redraw right now, do it later. */
10398 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010399# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010400 return;
10401 }
10402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010404 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010405 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010406 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 else
10409#endif
10410#ifdef FEAT_CMDL_INFO
10411 win_redr_ruler(curwin, always);
10412#endif
10413
10414#ifdef FEAT_TITLE
10415 if (need_maketitle
10416# ifdef FEAT_STL_OPT
10417 || (p_icon && (stl_syntax & STL_IN_ICON))
10418 || (p_title && (stl_syntax & STL_IN_TITLE))
10419# endif
10420 )
10421 maketitle();
10422#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010423#ifdef FEAT_WINDOWS
10424 /* Redraw the tab pages line if needed. */
10425 if (redraw_tabline)
10426 draw_tabline();
10427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428}
10429
10430#ifdef FEAT_CMDL_INFO
10431 static void
10432win_redr_ruler(wp, always)
10433 win_T *wp;
10434 int always;
10435{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010436#define RULER_BUF_LEN 70
10437 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 int row;
10439 int fillchar;
10440 int attr;
10441 int empty_line = FALSE;
10442 colnr_T virtcol;
10443 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010444 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 int o;
10446#ifdef FEAT_VERTSPLIT
10447 int this_ru_col;
10448 int off = 0;
10449 int width = Columns;
10450# define WITH_OFF(x) x
10451# define WITH_WIDTH(x) x
10452#else
10453# define WITH_OFF(x) 0
10454# define WITH_WIDTH(x) Columns
10455# define this_ru_col ru_col
10456#endif
10457
10458 /* If 'ruler' off or redrawing disabled, don't do anything */
10459 if (!p_ru)
10460 return;
10461
10462 /*
10463 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10464 * after deleting lines, before cursor.lnum is corrected.
10465 */
10466 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10467 return;
10468
10469#ifdef FEAT_INS_EXPAND
10470 /* Don't draw the ruler while doing insert-completion, it might overwrite
10471 * the (long) mode message. */
10472# ifdef FEAT_WINDOWS
10473 if (wp == lastwin && lastwin->w_status_height == 0)
10474# endif
10475 if (edit_submode != NULL)
10476 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010477 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10478 if (pum_visible())
10479 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480#endif
10481
10482#ifdef FEAT_STL_OPT
10483 if (*p_ruf)
10484 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010485 int save_called_emsg = called_emsg;
10486
10487 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010489 if (called_emsg)
10490 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010491 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010492 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 return;
10494 }
10495#endif
10496
10497 /*
10498 * Check if not in Insert mode and the line is empty (will show "0-1").
10499 */
10500 if (!(State & INSERT)
10501 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10502 empty_line = TRUE;
10503
10504 /*
10505 * Only draw the ruler when something changed.
10506 */
10507 validate_virtcol_win(wp);
10508 if ( redraw_cmdline
10509 || always
10510 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10511 || wp->w_cursor.col != wp->w_ru_cursor.col
10512 || wp->w_virtcol != wp->w_ru_virtcol
10513#ifdef FEAT_VIRTUALEDIT
10514 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10515#endif
10516 || wp->w_topline != wp->w_ru_topline
10517 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10518#ifdef FEAT_DIFF
10519 || wp->w_topfill != wp->w_ru_topfill
10520#endif
10521 || empty_line != wp->w_ru_empty)
10522 {
10523 cursor_off();
10524#ifdef FEAT_WINDOWS
10525 if (wp->w_status_height)
10526 {
10527 row = W_WINROW(wp) + wp->w_height;
10528 fillchar = fillchar_status(&attr, wp == curwin);
10529# ifdef FEAT_VERTSPLIT
10530 off = W_WINCOL(wp);
10531 width = W_WIDTH(wp);
10532# endif
10533 }
10534 else
10535#endif
10536 {
10537 row = Rows - 1;
10538 fillchar = ' ';
10539 attr = 0;
10540#ifdef FEAT_VERTSPLIT
10541 width = Columns;
10542 off = 0;
10543#endif
10544 }
10545
10546 /* In list mode virtcol needs to be recomputed */
10547 virtcol = wp->w_virtcol;
10548 if (wp->w_p_list && lcs_tab1 == NUL)
10549 {
10550 wp->w_p_list = FALSE;
10551 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10552 wp->w_p_list = TRUE;
10553 }
10554
10555 /*
10556 * Some sprintfs return the length, some return a pointer.
10557 * To avoid portability problems we use strlen() here.
10558 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010559 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10561 ? 0L
10562 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010563 len = STRLEN(buffer);
10564 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10566 (int)virtcol + 1);
10567
10568 /*
10569 * Add a "50%" if there is room for it.
10570 * On the last line, don't print in the last column (scrolls the
10571 * screen up on some terminals).
10572 */
10573 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010574 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 o = i + vim_strsize(buffer + i + 1);
10576#ifdef FEAT_WINDOWS
10577 if (wp->w_status_height == 0) /* can't use last char of screen */
10578#endif
10579 ++o;
10580#ifdef FEAT_VERTSPLIT
10581 this_ru_col = ru_col - (Columns - width);
10582 if (this_ru_col < 0)
10583 this_ru_col = 0;
10584#endif
10585 /* Never use more than half the window/screen width, leave the other
10586 * half for the filename. */
10587 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10588 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10589 if (this_ru_col + o < WITH_WIDTH(width))
10590 {
10591 while (this_ru_col + o < WITH_WIDTH(width))
10592 {
10593#ifdef FEAT_MBYTE
10594 if (has_mbyte)
10595 i += (*mb_char2bytes)(fillchar, buffer + i);
10596 else
10597#endif
10598 buffer[i++] = fillchar;
10599 ++o;
10600 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010601 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 }
10603 /* Truncate at window boundary. */
10604#ifdef FEAT_MBYTE
10605 if (has_mbyte)
10606 {
10607 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010608 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 {
10610 o += (*mb_ptr2cells)(buffer + i);
10611 if (this_ru_col + o > WITH_WIDTH(width))
10612 {
10613 buffer[i] = NUL;
10614 break;
10615 }
10616 }
10617 }
10618 else
10619#endif
10620 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10621 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10622
10623 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10624 i = redraw_cmdline;
10625 screen_fill(row, row + 1,
10626 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10627 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10628 fillchar, fillchar, attr);
10629 /* don't redraw the cmdline because of showing the ruler */
10630 redraw_cmdline = i;
10631 wp->w_ru_cursor = wp->w_cursor;
10632 wp->w_ru_virtcol = wp->w_virtcol;
10633 wp->w_ru_empty = empty_line;
10634 wp->w_ru_topline = wp->w_topline;
10635 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10636#ifdef FEAT_DIFF
10637 wp->w_ru_topfill = wp->w_topfill;
10638#endif
10639 }
10640}
10641#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010642
10643#if defined(FEAT_LINEBREAK) || defined(PROTO)
10644/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010645 * Return the width of the 'number' and 'relativenumber' column.
10646 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010647 * Otherwise it depends on 'numberwidth' and the line count.
10648 */
10649 int
10650number_width(wp)
10651 win_T *wp;
10652{
10653 int n;
10654 linenr_T lnum;
10655
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010656 if (wp->w_p_rnu && !wp->w_p_nu)
10657 /* cursor line shows "0" */
10658 lnum = wp->w_height;
10659 else
10660 /* cursor line shows absolute line number */
10661 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010662
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010663 if (lnum == wp->w_nrwidth_line_count)
10664 return wp->w_nrwidth_width;
10665 wp->w_nrwidth_line_count = lnum;
10666
10667 n = 0;
10668 do
10669 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010670 lnum /= 10;
10671 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010672 } while (lnum > 0);
10673
10674 /* 'numberwidth' gives the minimal width plus one */
10675 if (n < wp->w_p_nuw - 1)
10676 n = wp->w_p_nuw - 1;
10677
10678 wp->w_nrwidth_width = n;
10679 return n;
10680}
10681#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010682
10683/*
10684 * Return the current cursor column. This is the actual position on the
10685 * screen. First column is 0.
10686 */
10687 int
10688screen_screencol()
10689{
10690 return screen_cur_col;
10691}
10692
10693/*
10694 * Return the current cursor row. This is the actual position on the screen.
10695 * First row is 0.
10696 */
10697 int
10698screen_screenrow()
10699{
10700 return screen_cur_row;
10701}