blob: e11bdc37752eac062d6c26c1ab4c37743e84bc0a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
112#endif
113
114/*
115 * Buffer for one screen line (characters and attributes).
116 */
117static schar_T *current_ScreenLine;
118
119static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000120static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_FOLDING
122static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
123static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
124static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
125#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000126static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
128#ifdef FEAT_RIGHTLEFT
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
131#else
132static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
133# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifdef FEAT_VERTSPLIT
136static void draw_vsep_win __ARGS((win_T *wp, int row));
137#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000139static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200142# define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void start_search_hl __ARGS((void));
144static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200145static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200147static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur));
148static int next_search_hl_pos __ARGS((match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
150static void screen_start_highlight __ARGS((int attr));
151static void screen_char __ARGS((unsigned off, int row, int col));
152#ifdef FEAT_MBYTE
153static void screen_char_2 __ARGS((unsigned off, int row, int col));
154#endif
155static void screenclear2 __ARGS((void));
156static void lineclear __ARGS((unsigned off, int width));
157static void lineinvalid __ARGS((unsigned off, int width));
158#ifdef FEAT_VERTSPLIT
159static void linecopy __ARGS((int to, int from, win_T *wp));
160static void redraw_block __ARGS((int row, int end, win_T *wp));
161#endif
162static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
163static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000165#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000166static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
169static int fillchar_status __ARGS((int *attr, int is_curwin));
170#endif
171#ifdef FEAT_VERTSPLIT
172static int fillchar_vsep __ARGS((int *attr));
173#endif
174#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000175static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#ifdef FEAT_CMDL_INFO
178static void win_redr_ruler __ARGS((win_T *wp, int always));
179#endif
180
181#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
182/* Ugly global: overrule attribute used by screen_char() */
183static int screen_char_attr = 0;
184#endif
185
186/*
187 * Redraw the current window later, with update_screen(type).
188 * Set must_redraw only if not already set to a higher value.
189 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
190 */
191 void
192redraw_later(type)
193 int type;
194{
195 redraw_win_later(curwin, type);
196}
197
198 void
199redraw_win_later(wp, type)
200 win_T *wp;
201 int type;
202{
203 if (wp->w_redr_type < type)
204 {
205 wp->w_redr_type = type;
206 if (type >= NOT_VALID)
207 wp->w_lines_valid = 0;
208 if (must_redraw < type) /* must_redraw is the maximum of all windows */
209 must_redraw = type;
210 }
211}
212
213/*
214 * Force a complete redraw later. Also resets the highlighting. To be used
215 * after executing a shell command that messes up the screen.
216 */
217 void
218redraw_later_clear()
219{
220 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000221#ifdef FEAT_GUI
222 if (gui.in_use)
223 /* Use a code that will reset gui.highlight_mask in
224 * gui_stop_highlight(). */
225 screen_attr = HL_ALL + 1;
226 else
227#endif
228 /* Use attributes that is very unlikely to appear in text. */
229 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230}
231
232/*
233 * Mark all windows to be redrawn later.
234 */
235 void
236redraw_all_later(type)
237 int type;
238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
251redraw_curbuf_later(type)
252 int type;
253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
258redraw_buf_later(buf, type)
259 buf_T *buf;
260 int type;
261{
262 win_T *wp;
263
264 FOR_ALL_WINDOWS(wp)
265 {
266 if (wp->w_buffer == buf)
267 redraw_win_later(wp, type);
268 }
269}
270
271/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200272 * Redraw as soon as possible. When the command line is not scrolled redraw
273 * right away and restore what was on the command line.
274 * Return a code indicating what happened.
275 */
276 int
277redraw_asap(type)
278 int type;
279{
280 int rows;
281 int r;
282 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200283 schar_T *screenline; /* copy from ScreenLines[] */
284 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285#ifdef FEAT_MBYTE
286 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200287 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200288 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200289 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200290#endif
291
292 redraw_later(type);
293 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY))
294 return ret;
295
296 /* Allocate space to save the text displayed in the command line area. */
297 rows = Rows - cmdline_row;
298 screenline = (schar_T *)lalloc(
299 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
300 screenattr = (sattr_T *)lalloc(
301 (long_u)(rows * Columns * sizeof(sattr_T)), FALSE);
302 if (screenline == NULL || screenattr == NULL)
303 ret = 2;
304#ifdef FEAT_MBYTE
305 if (enc_utf8)
306 {
307 screenlineUC = (u8char_T *)lalloc(
308 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
309 if (screenlineUC == NULL)
310 ret = 2;
311 for (i = 0; i < p_mco; ++i)
312 {
313 screenlineC[i] = (u8char_T *)lalloc(
314 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
315 if (screenlineC[i] == NULL)
316 ret = 2;
317 }
318 }
319 if (enc_dbcs == DBCS_JPNU)
320 {
321 screenline2 = (schar_T *)lalloc(
322 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
323 if (screenline2 == NULL)
324 ret = 2;
325 }
326#endif
327
328 if (ret != 2)
329 {
330 /* Save the text displayed in the command line area. */
331 for (r = 0; r < rows; ++r)
332 {
333 mch_memmove(screenline + r * Columns,
334 ScreenLines + LineOffset[cmdline_row + r],
335 (size_t)Columns * sizeof(schar_T));
336 mch_memmove(screenattr + r * Columns,
337 ScreenAttrs + LineOffset[cmdline_row + r],
338 (size_t)Columns * sizeof(sattr_T));
339#ifdef FEAT_MBYTE
340 if (enc_utf8)
341 {
342 mch_memmove(screenlineUC + r * Columns,
343 ScreenLinesUC + LineOffset[cmdline_row + r],
344 (size_t)Columns * sizeof(u8char_T));
345 for (i = 0; i < p_mco; ++i)
346 mch_memmove(screenlineC[i] + r * Columns,
347 ScreenLinesC[r] + LineOffset[cmdline_row + r],
348 (size_t)Columns * sizeof(u8char_T));
349 }
350 if (enc_dbcs == DBCS_JPNU)
351 mch_memmove(screenline2 + r * Columns,
352 ScreenLines2 + LineOffset[cmdline_row + r],
353 (size_t)Columns * sizeof(schar_T));
354#endif
355 }
356
357 update_screen(0);
358 ret = 3;
359
360 if (must_redraw == 0)
361 {
362 int off = (int)(current_ScreenLine - ScreenLines);
363
364 /* Restore the text displayed in the command line area. */
365 for (r = 0; r < rows; ++r)
366 {
367 mch_memmove(current_ScreenLine,
368 screenline + r * Columns,
369 (size_t)Columns * sizeof(schar_T));
370 mch_memmove(ScreenAttrs + off,
371 screenattr + r * Columns,
372 (size_t)Columns * sizeof(sattr_T));
373#ifdef FEAT_MBYTE
374 if (enc_utf8)
375 {
376 mch_memmove(ScreenLinesUC + off,
377 screenlineUC + r * Columns,
378 (size_t)Columns * sizeof(u8char_T));
379 for (i = 0; i < p_mco; ++i)
380 mch_memmove(ScreenLinesC[i] + off,
381 screenlineC[i] + r * Columns,
382 (size_t)Columns * sizeof(u8char_T));
383 }
384 if (enc_dbcs == DBCS_JPNU)
385 mch_memmove(ScreenLines2 + off,
386 screenline2 + r * Columns,
387 (size_t)Columns * sizeof(schar_T));
388#endif
389 SCREEN_LINE(cmdline_row + r, 0, Columns, Columns, FALSE);
390 }
391 ret = 4;
392 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 }
394
395 vim_free(screenline);
396 vim_free(screenattr);
397#ifdef FEAT_MBYTE
398 if (enc_utf8)
399 {
400 vim_free(screenlineUC);
401 for (i = 0; i < p_mco; ++i)
402 vim_free(screenlineC[i]);
403 }
404 if (enc_dbcs == DBCS_JPNU)
405 vim_free(screenline2);
406#endif
407
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200408 /* Show the intro message when appropriate. */
409 maybe_intro_message();
410
411 setcursor();
412
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 return ret;
414}
415
416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 * Changed something in the current window, at buffer line "lnum", that
418 * requires that line and possibly other lines to be redrawn.
419 * Used when entering/leaving Insert mode with the cursor on a folded line.
420 * Used to remove the "$" from a change command.
421 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
422 * may become invalid and the whole window will have to be redrawn.
423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 void
425redrawWinline(lnum, invalid)
426 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000427 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429#ifdef FEAT_FOLDING
430 int i;
431#endif
432
433 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
434 curwin->w_redraw_top = lnum;
435 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
436 curwin->w_redraw_bot = lnum;
437 redraw_later(VALID);
438
439#ifdef FEAT_FOLDING
440 if (invalid)
441 {
442 /* A w_lines[] entry for this lnum has become invalid. */
443 i = find_wl_entry(curwin, lnum);
444 if (i >= 0)
445 curwin->w_lines[i].wl_valid = FALSE;
446 }
447#endif
448}
449
450/*
451 * update all windows that are editing the current buffer
452 */
453 void
454update_curbuf(type)
455 int type;
456{
457 redraw_curbuf_later(type);
458 update_screen(type);
459}
460
461/*
462 * update_screen()
463 *
464 * Based on the current value of curwin->w_topline, transfer a screenfull
465 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
466 */
467 void
468update_screen(type)
469 int type;
470{
471 win_T *wp;
472 static int did_intro = FALSE;
473#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
474 int did_one;
475#endif
476
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000477 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (!screen_valid(TRUE))
479 return;
480
481 if (must_redraw)
482 {
483 if (type < must_redraw) /* use maximal type */
484 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000485
486 /* must_redraw is reset here, so that when we run into some weird
487 * reason to redraw while busy redrawing (e.g., asynchronous
488 * scrolling), or update_topline() in win_update() will cause a
489 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 must_redraw = 0;
491 }
492
493 /* Need to update w_lines[]. */
494 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
495 type = NOT_VALID;
496
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000497 /* Postpone the redrawing when it's not needed and when being called
498 * recursively. */
499 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 redraw_later(type); /* remember type for next time */
502 must_redraw = type;
503 if (type > INVERTED_ALL)
504 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
505 return;
506 }
507
508 updating_screen = TRUE;
509#ifdef FEAT_SYN_HL
510 ++display_tick; /* let syntax code know we're in a next round of
511 * display updating */
512#endif
513
514 /*
515 * if the screen was scrolled up when displaying a message, scroll it down
516 */
517 if (msg_scrolled)
518 {
519 clear_cmdline = TRUE;
520 if (msg_scrolled > Rows - 5) /* clearing is faster */
521 type = CLEAR;
522 else if (type != CLEAR)
523 {
524 check_for_delay(FALSE);
525 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
526 type = CLEAR;
527 FOR_ALL_WINDOWS(wp)
528 {
529 if (W_WINROW(wp) < msg_scrolled)
530 {
531 if (W_WINROW(wp) + wp->w_height > msg_scrolled
532 && wp->w_redr_type < REDRAW_TOP
533 && wp->w_lines_valid > 0
534 && wp->w_topline == wp->w_lines[0].wl_lnum)
535 {
536 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
537 wp->w_redr_type = REDRAW_TOP;
538 }
539 else
540 {
541 wp->w_redr_type = NOT_VALID;
542#ifdef FEAT_WINDOWS
543 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
544 <= msg_scrolled)
545 wp->w_redr_status = TRUE;
546#endif
547 }
548 }
549 }
550 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000551#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000552 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555 msg_scrolled = 0;
556 need_wait_return = FALSE;
557 }
558
559 /* reset cmdline_row now (may have been changed temporarily) */
560 compute_cmdrow();
561
562 /* Check for changed highlighting */
563 if (need_highlight_changed)
564 highlight_changed();
565
566 if (type == CLEAR) /* first clear screen */
567 {
568 screenclear(); /* will reset clear_cmdline */
569 type = NOT_VALID;
570 }
571
572 if (clear_cmdline) /* going to clear cmdline (done below) */
573 check_for_delay(FALSE);
574
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000575#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200576 /* Force redraw when width of 'number' or 'relativenumber' column
577 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000578 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200579 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
580 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000581 curwin->w_redr_type = NOT_VALID;
582#endif
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 /*
585 * Only start redrawing if there is really something to do.
586 */
587 if (type == INVERTED)
588 update_curswant();
589 if (curwin->w_redr_type < type
590 && !((type == VALID
591 && curwin->w_lines[0].wl_valid
592#ifdef FEAT_DIFF
593 && curwin->w_topfill == curwin->w_old_topfill
594 && curwin->w_botfill == curwin->w_old_botfill
595#endif
596 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000598 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
600 && curwin->w_old_visual_mode == VIsual_mode
601 && (curwin->w_valid & VALID_VIRTCOL)
602 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 ))
604 curwin->w_redr_type = type;
605
Bram Moolenaar5a305422006-04-28 22:38:25 +0000606#ifdef FEAT_WINDOWS
607 /* Redraw the tab pages line if needed. */
608 if (redraw_tabline || type >= NOT_VALID)
609 draw_tabline();
610#endif
611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#ifdef FEAT_SYN_HL
613 /*
614 * Correct stored syntax highlighting info for changes in each displayed
615 * buffer. Each buffer must only be done once.
616 */
617 FOR_ALL_WINDOWS(wp)
618 {
619 if (wp->w_buffer->b_mod_set)
620 {
621# ifdef FEAT_WINDOWS
622 win_T *wwp;
623
624 /* Check if we already did this buffer. */
625 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
626 if (wwp->w_buffer == wp->w_buffer)
627 break;
628# endif
629 if (
630# ifdef FEAT_WINDOWS
631 wwp == wp &&
632# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200633 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 syn_stack_apply_changes(wp->w_buffer);
635 }
636 }
637#endif
638
639 /*
640 * Go from top to bottom through the windows, redrawing the ones that need
641 * it.
642 */
643#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
644 did_one = FALSE;
645#endif
646#ifdef FEAT_SEARCH_EXTRA
647 search_hl.rm.regprog = NULL;
648#endif
649 FOR_ALL_WINDOWS(wp)
650 {
651 if (wp->w_redr_type != 0)
652 {
653 cursor_off();
654#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
655 if (!did_one)
656 {
657 did_one = TRUE;
658# ifdef FEAT_SEARCH_EXTRA
659 start_search_hl();
660# endif
661# ifdef FEAT_CLIPBOARD
662 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200663 if (clip_star.available && clip_isautosel_star())
664 clip_update_selection(&clip_star);
665 if (clip_plus.available && clip_isautosel_plus())
666 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667# endif
668#ifdef FEAT_GUI
669 /* Remove the cursor before starting to do anything, because
670 * scrolling may make it difficult to redraw the text under
671 * it. */
672 if (gui.in_use)
673 gui_undraw_cursor();
674#endif
675 }
676#endif
677 win_update(wp);
678 }
679
680#ifdef FEAT_WINDOWS
681 /* redraw status line after the window to minimize cursor movement */
682 if (wp->w_redr_status)
683 {
684 cursor_off();
685 win_redr_status(wp);
686 }
687#endif
688 }
689#if defined(FEAT_SEARCH_EXTRA)
690 end_search_hl();
691#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100692#ifdef FEAT_INS_EXPAND
693 /* May need to redraw the popup menu. */
694 if (pum_visible())
695 pum_redraw();
696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698#ifdef FEAT_WINDOWS
699 /* Reset b_mod_set flags. Going through all windows is probably faster
700 * than going through all buffers (there could be many buffers). */
701 for (wp = firstwin; wp != NULL; wp = wp->w_next)
702 wp->w_buffer->b_mod_set = FALSE;
703#else
704 curbuf->b_mod_set = FALSE;
705#endif
706
707 updating_screen = FALSE;
708#ifdef FEAT_GUI
709 gui_may_resize_shell();
710#endif
711
712 /* Clear or redraw the command line. Done last, because scrolling may
713 * mess up the command line. */
714 if (clear_cmdline || redraw_cmdline)
715 showmode();
716
717 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200718 if (!did_intro)
719 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 did_intro = TRUE;
721
722#ifdef FEAT_GUI
723 /* Redraw the cursor and update the scrollbars when all screen updating is
724 * done. */
725 if (gui.in_use)
726 {
727 out_flush(); /* required before updating the cursor */
728 if (did_one)
729 gui_update_cursor(FALSE, FALSE);
730 gui_update_scrollbars(FALSE);
731 }
732#endif
733}
734
Bram Moolenaar860cae12010-06-05 23:22:07 +0200735#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200736/*
737 * Return TRUE if the cursor line in window "wp" may be concealed, according
738 * to the 'concealcursor' option.
739 */
740 int
741conceal_cursor_line(wp)
742 win_T *wp;
743{
744 int c;
745
746 if (*wp->w_p_cocu == NUL)
747 return FALSE;
748 if (get_real_state() & VISUAL)
749 c = 'v';
750 else if (State & INSERT)
751 c = 'i';
752 else if (State & NORMAL)
753 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200754 else if (State & CMDLINE)
755 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200756 else
757 return FALSE;
758 return vim_strchr(wp->w_p_cocu, c) != NULL;
759}
760
761/*
762 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
763 */
764 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200765conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200766{
767 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
768 {
769 need_cursor_line_redraw = TRUE;
770 /* Need to recompute cursor column, e.g., when starting Visual mode
771 * without concealing. */
772 curs_columns(TRUE);
773 }
774}
775
Bram Moolenaar860cae12010-06-05 23:22:07 +0200776 void
777update_single_line(wp, lnum)
778 win_T *wp;
779 linenr_T lnum;
780{
781 int row;
782 int j;
783
784 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200785 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200786 {
787# ifdef FEAT_GUI
788 /* Remove the cursor before starting to do anything, because scrolling
789 * may make it difficult to redraw the text under it. */
790 if (gui.in_use)
791 gui_undraw_cursor();
792# endif
793 row = 0;
794 for (j = 0; j < wp->w_lines_valid; ++j)
795 {
796 if (lnum == wp->w_lines[j].wl_lnum)
797 {
798 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200799# ifdef FEAT_SEARCH_EXTRA
800 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200801 start_search_hl();
802 prepare_search_hl(wp, lnum);
803# endif
804 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
805# if defined(FEAT_SEARCH_EXTRA)
806 end_search_hl();
807# endif
808 break;
809 }
810 row += wp->w_lines[j].wl_size;
811 }
812# ifdef FEAT_GUI
813 /* Redraw the cursor */
814 if (gui.in_use)
815 {
816 out_flush(); /* required before updating the cursor */
817 gui_update_cursor(FALSE, FALSE);
818 }
819# endif
820 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200821 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200822}
823#endif
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
826static void update_prepare __ARGS((void));
827static void update_finish __ARGS((void));
828
829/*
830 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000831 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 */
833 static void
834update_prepare()
835{
836 cursor_off();
837 updating_screen = TRUE;
838#ifdef FEAT_GUI
839 /* Remove the cursor before starting to do anything, because scrolling may
840 * make it difficult to redraw the text under it. */
841 if (gui.in_use)
842 gui_undraw_cursor();
843#endif
844#ifdef FEAT_SEARCH_EXTRA
845 start_search_hl();
846#endif
847}
848
849/*
850 * Finish updating one or more windows.
851 */
852 static void
853update_finish()
854{
855 if (redraw_cmdline)
856 showmode();
857
858# ifdef FEAT_SEARCH_EXTRA
859 end_search_hl();
860# endif
861
862 updating_screen = FALSE;
863
864# ifdef FEAT_GUI
865 gui_may_resize_shell();
866
867 /* Redraw the cursor and update the scrollbars when all screen updating is
868 * done. */
869 if (gui.in_use)
870 {
871 out_flush(); /* required before updating the cursor */
872 gui_update_cursor(FALSE, FALSE);
873 gui_update_scrollbars(FALSE);
874 }
875# endif
876}
877#endif
878
879#if defined(FEAT_SIGNS) || defined(PROTO)
880 void
881update_debug_sign(buf, lnum)
882 buf_T *buf;
883 linenr_T lnum;
884{
885 win_T *wp;
886 int doit = FALSE;
887
888# ifdef FEAT_FOLDING
889 win_foldinfo.fi_level = 0;
890# endif
891
892 /* update/delete a specific mark */
893 FOR_ALL_WINDOWS(wp)
894 {
895 if (buf != NULL && lnum > 0)
896 {
897 if (wp->w_buffer == buf && lnum >= wp->w_topline
898 && lnum < wp->w_botline)
899 {
900 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
901 wp->w_redraw_top = lnum;
902 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
903 wp->w_redraw_bot = lnum;
904 redraw_win_later(wp, VALID);
905 }
906 }
907 else
908 redraw_win_later(wp, VALID);
909 if (wp->w_redr_type != 0)
910 doit = TRUE;
911 }
912
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100913 /* Return when there is nothing to do, screen updating is already
914 * happening (recursive call) or still starting up. */
915 if (!doit || updating_screen
916#ifdef FEAT_GUI
917 || gui.starting
918#endif
919 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 return;
921
922 /* update all windows that need updating */
923 update_prepare();
924
925# ifdef FEAT_WINDOWS
926 for (wp = firstwin; wp; wp = wp->w_next)
927 {
928 if (wp->w_redr_type != 0)
929 win_update(wp);
930 if (wp->w_redr_status)
931 win_redr_status(wp);
932 }
933# else
934 if (curwin->w_redr_type != 0)
935 win_update(curwin);
936# endif
937
938 update_finish();
939}
940#endif
941
942
943#if defined(FEAT_GUI) || defined(PROTO)
944/*
945 * Update a single window, its status line and maybe the command line msg.
946 * Used for the GUI scrollbar.
947 */
948 void
949updateWindow(wp)
950 win_T *wp;
951{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000952 /* return if already busy updating */
953 if (updating_screen)
954 return;
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 update_prepare();
957
958#ifdef FEAT_CLIPBOARD
959 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200960 if (clip_star.available && clip_isautosel_star())
961 clip_update_selection(&clip_star);
962 if (clip_plus.available && clip_isautosel_plus())
963 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000969 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000970 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000971 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 if (wp->w_redr_status
974# ifdef FEAT_CMDL_INFO
975 || p_ru
976# endif
977# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000978 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979# endif
980 )
981 win_redr_status(wp);
982#endif
983
984 update_finish();
985}
986#endif
987
988/*
989 * Update a single window.
990 *
991 * This may cause the windows below it also to be redrawn (when clearing the
992 * screen or scrolling lines).
993 *
994 * How the window is redrawn depends on wp->w_redr_type. Each type also
995 * implies the one below it.
996 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000997 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
999 * INVERTED redraw the changed part of the Visual area
1000 * INVERTED_ALL redraw the whole Visual area
1001 * VALID 1. scroll up/down to adjust for a changed w_topline
1002 * 2. update lines at the top when scrolled down
1003 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001004 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 * b_mod_top and b_mod_bot.
1006 * - if wp->w_redraw_top non-zero, redraw lines between
1007 * wp->w_redraw_top and wp->w_redr_bot.
1008 * - continue redrawing when syntax status is invalid.
1009 * 4. if scrolled up, update lines at the bottom.
1010 * This results in three areas that may need updating:
1011 * top: from first row to top_end (when scrolled down)
1012 * mid: from mid_start to mid_end (update inversion or changed text)
1013 * bot: from bot_start to last row (when scrolled up)
1014 */
1015 static void
1016win_update(wp)
1017 win_T *wp;
1018{
1019 buf_T *buf = wp->w_buffer;
1020 int type;
1021 int top_end = 0; /* Below last row of the top area that needs
1022 updating. 0 when no top area updating. */
1023 int mid_start = 999;/* first row of the mid area that needs
1024 updating. 999 when no mid area updating. */
1025 int mid_end = 0; /* Below last row of the mid area that needs
1026 updating. 0 when no mid area updating. */
1027 int bot_start = 999;/* first row of the bot area that needs
1028 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 int scrolled_down = FALSE; /* TRUE when scrolled down when
1030 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001032 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 int top_to_mod = FALSE; /* redraw above mod_top */
1034#endif
1035
1036 int row; /* current window row to display */
1037 linenr_T lnum; /* current buffer lnum to display */
1038 int idx; /* current index in w_lines[] */
1039 int srow; /* starting row of the current line */
1040
1041 int eof = FALSE; /* if TRUE, we hit the end of the file */
1042 int didline = FALSE; /* if TRUE, we finished the last line */
1043 int i;
1044 long j;
1045 static int recursive = FALSE; /* being called recursively */
1046 int old_botline = wp->w_botline;
1047#ifdef FEAT_FOLDING
1048 long fold_count;
1049#endif
1050#ifdef FEAT_SYN_HL
1051 /* remember what happened to the previous line, to know if
1052 * check_visual_highlight() can be used */
1053#define DID_NONE 1 /* didn't update a line */
1054#define DID_LINE 2 /* updated a normal line */
1055#define DID_FOLD 3 /* updated a folded line */
1056 int did_update = DID_NONE;
1057 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1058#endif
1059 linenr_T mod_top = 0;
1060 linenr_T mod_bot = 0;
1061#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1062 int save_got_int;
1063#endif
1064
1065 type = wp->w_redr_type;
1066
1067 if (type == NOT_VALID)
1068 {
1069#ifdef FEAT_WINDOWS
1070 wp->w_redr_status = TRUE;
1071#endif
1072 wp->w_lines_valid = 0;
1073 }
1074
1075 /* Window is zero-height: nothing to draw. */
1076 if (wp->w_height == 0)
1077 {
1078 wp->w_redr_type = 0;
1079 return;
1080 }
1081
1082#ifdef FEAT_VERTSPLIT
1083 /* Window is zero-width: Only need to draw the separator. */
1084 if (wp->w_width == 0)
1085 {
1086 /* draw the vertical separator right of this window */
1087 draw_vsep_win(wp, 0);
1088 wp->w_redr_type = 0;
1089 return;
1090 }
1091#endif
1092
1093#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001094 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095#endif
1096
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001097#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001098 /* Force redraw when width of 'number' or 'relativenumber' column
1099 * changes. */
1100 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001101 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001102 {
1103 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001104 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001105 }
1106 else
1107#endif
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1110 {
1111 /*
1112 * When there are both inserted/deleted lines and specific lines to be
1113 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1114 * everything (only happens when redrawing is off for while).
1115 */
1116 type = NOT_VALID;
1117 }
1118 else
1119 {
1120 /*
1121 * Set mod_top to the first line that needs displaying because of
1122 * changes. Set mod_bot to the first line after the changes.
1123 */
1124 mod_top = wp->w_redraw_top;
1125 if (wp->w_redraw_bot != 0)
1126 mod_bot = wp->w_redraw_bot + 1;
1127 else
1128 mod_bot = 0;
1129 wp->w_redraw_top = 0; /* reset for next time */
1130 wp->w_redraw_bot = 0;
1131 if (buf->b_mod_set)
1132 {
1133 if (mod_top == 0 || mod_top > buf->b_mod_top)
1134 {
1135 mod_top = buf->b_mod_top;
1136#ifdef FEAT_SYN_HL
1137 /* Need to redraw lines above the change that may be included
1138 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001139 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001141 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (mod_top < 1)
1143 mod_top = 1;
1144 }
1145#endif
1146 }
1147 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1148 mod_bot = buf->b_mod_bot;
1149
1150#ifdef FEAT_SEARCH_EXTRA
1151 /* When 'hlsearch' is on and using a multi-line search pattern, a
1152 * change in one line may make the Search highlighting in a
1153 * previous line invalid. Simple solution: redraw all visible
1154 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001155 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001157 if (search_hl.rm.regprog != NULL
1158 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001160 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001161 {
1162 cur = wp->w_match_head;
1163 while (cur != NULL)
1164 {
1165 if (cur->match.regprog != NULL
1166 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001167 {
1168 top_to_mod = TRUE;
1169 break;
1170 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001171 cur = cur->next;
1172 }
1173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174#endif
1175 }
1176#ifdef FEAT_FOLDING
1177 if (mod_top != 0 && hasAnyFolding(wp))
1178 {
1179 linenr_T lnumt, lnumb;
1180
1181 /*
1182 * A change in a line can cause lines above it to become folded or
1183 * unfolded. Find the top most buffer line that may be affected.
1184 * If the line was previously folded and displayed, get the first
1185 * line of that fold. If the line is folded now, get the first
1186 * folded line. Use the minimum of these two.
1187 */
1188
1189 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1190 * the line below it. If there is no valid entry, use w_topline.
1191 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1192 * to this line. If there is no valid entry, use MAXLNUM. */
1193 lnumt = wp->w_topline;
1194 lnumb = MAXLNUM;
1195 for (i = 0; i < wp->w_lines_valid; ++i)
1196 if (wp->w_lines[i].wl_valid)
1197 {
1198 if (wp->w_lines[i].wl_lastlnum < mod_top)
1199 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1200 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1201 {
1202 lnumb = wp->w_lines[i].wl_lnum;
1203 /* When there is a fold column it might need updating
1204 * in the next line ("J" just above an open fold). */
1205 if (wp->w_p_fdc > 0)
1206 ++lnumb;
1207 }
1208 }
1209
1210 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1211 if (mod_top > lnumt)
1212 mod_top = lnumt;
1213
1214 /* Now do the same for the bottom line (one above mod_bot). */
1215 --mod_bot;
1216 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1217 ++mod_bot;
1218 if (mod_bot < lnumb)
1219 mod_bot = lnumb;
1220 }
1221#endif
1222
1223 /* When a change starts above w_topline and the end is below
1224 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001225 * If the end of the change is above w_topline: do like no change was
1226 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (mod_top != 0 && mod_top < wp->w_topline)
1228 {
1229 if (mod_bot > wp->w_topline)
1230 mod_top = wp->w_topline;
1231#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001232 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 top_end = 1;
1234#endif
1235 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001236
1237 /* When line numbers are displayed need to redraw all lines below
1238 * inserted/deleted lines. */
1239 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1240 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242
1243 /*
1244 * When only displaying the lines at the top, set top_end. Used when
1245 * window has scrolled down for msg_scrolled.
1246 */
1247 if (type == REDRAW_TOP)
1248 {
1249 j = 0;
1250 for (i = 0; i < wp->w_lines_valid; ++i)
1251 {
1252 j += wp->w_lines[i].wl_size;
1253 if (j >= wp->w_upd_rows)
1254 {
1255 top_end = j;
1256 break;
1257 }
1258 }
1259 if (top_end == 0)
1260 /* not found (cannot happen?): redraw everything */
1261 type = NOT_VALID;
1262 else
1263 /* top area defined, the rest is VALID */
1264 type = VALID;
1265 }
1266
Bram Moolenaar367329b2007-08-30 11:53:22 +00001267 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001268 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1269 * non-zero and thus not FALSE) will indicate that screenclear() was not
1270 * called. */
1271 if (screen_cleared)
1272 screen_cleared = MAYBE;
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 /*
1275 * If there are no changes on the screen that require a complete redraw,
1276 * handle three cases:
1277 * 1: we are off the top of the screen by a few lines: scroll down
1278 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1279 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1280 * w_lines[] that needs updating.
1281 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001282 if ((type == VALID || type == SOME_VALID
1283 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284#ifdef FEAT_DIFF
1285 && !wp->w_botfill && !wp->w_old_botfill
1286#endif
1287 )
1288 {
1289 if (mod_top != 0 && wp->w_topline == mod_top)
1290 {
1291 /*
1292 * w_topline is the first changed line, the scrolling will be done
1293 * further down.
1294 */
1295 }
1296 else if (wp->w_lines[0].wl_valid
1297 && (wp->w_topline < wp->w_lines[0].wl_lnum
1298#ifdef FEAT_DIFF
1299 || (wp->w_topline == wp->w_lines[0].wl_lnum
1300 && wp->w_topfill > wp->w_old_topfill)
1301#endif
1302 ))
1303 {
1304 /*
1305 * New topline is above old topline: May scroll down.
1306 */
1307#ifdef FEAT_FOLDING
1308 if (hasAnyFolding(wp))
1309 {
1310 linenr_T ln;
1311
1312 /* count the number of lines we are off, counting a sequence
1313 * of folded lines as one */
1314 j = 0;
1315 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1316 {
1317 ++j;
1318 if (j >= wp->w_height - 2)
1319 break;
1320 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1321 }
1322 }
1323 else
1324#endif
1325 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1326 if (j < wp->w_height - 2) /* not too far off */
1327 {
1328 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1329#ifdef FEAT_DIFF
1330 /* insert extra lines for previously invisible filler lines */
1331 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1332 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1333 - wp->w_old_topfill;
1334#endif
1335 if (i < wp->w_height - 2) /* less than a screen off */
1336 {
1337 /*
1338 * Try to insert the correct number of lines.
1339 * If not the last window, delete the lines at the bottom.
1340 * win_ins_lines may fail when the terminal can't do it.
1341 */
1342 if (i > 0)
1343 check_for_delay(FALSE);
1344 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1345 {
1346 if (wp->w_lines_valid != 0)
1347 {
1348 /* Need to update rows that are new, stop at the
1349 * first one that scrolled down. */
1350 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
1353 /* Move the entries that were scrolled, disable
1354 * the entries for the lines to be redrawn. */
1355 if ((wp->w_lines_valid += j) > wp->w_height)
1356 wp->w_lines_valid = wp->w_height;
1357 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1358 wp->w_lines[idx] = wp->w_lines[idx - j];
1359 while (idx >= 0)
1360 wp->w_lines[idx--].wl_valid = FALSE;
1361 }
1362 }
1363 else
1364 mid_start = 0; /* redraw all lines */
1365 }
1366 else
1367 mid_start = 0; /* redraw all lines */
1368 }
1369 else
1370 mid_start = 0; /* redraw all lines */
1371 }
1372 else
1373 {
1374 /*
1375 * New topline is at or below old topline: May scroll up.
1376 * When topline didn't change, find first entry in w_lines[] that
1377 * needs updating.
1378 */
1379
1380 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1381 j = -1;
1382 row = 0;
1383 for (i = 0; i < wp->w_lines_valid; i++)
1384 {
1385 if (wp->w_lines[i].wl_valid
1386 && wp->w_lines[i].wl_lnum == wp->w_topline)
1387 {
1388 j = i;
1389 break;
1390 }
1391 row += wp->w_lines[i].wl_size;
1392 }
1393 if (j == -1)
1394 {
1395 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1396 * lines */
1397 mid_start = 0;
1398 }
1399 else
1400 {
1401 /*
1402 * Try to delete the correct number of lines.
1403 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1404 */
1405#ifdef FEAT_DIFF
1406 /* If the topline didn't change, delete old filler lines,
1407 * otherwise delete filler lines of the new topline... */
1408 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1409 row += wp->w_old_topfill;
1410 else
1411 row += diff_check_fill(wp, wp->w_topline);
1412 /* ... but don't delete new filler lines. */
1413 row -= wp->w_topfill;
1414#endif
1415 if (row > 0)
1416 {
1417 check_for_delay(FALSE);
1418 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1419 bot_start = wp->w_height - row;
1420 else
1421 mid_start = 0; /* redraw all lines */
1422 }
1423 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1424 {
1425 /*
1426 * Skip the lines (below the deleted lines) that are still
1427 * valid and don't need redrawing. Copy their info
1428 * upwards, to compensate for the deleted lines. Set
1429 * bot_start to the first row that needs redrawing.
1430 */
1431 bot_start = 0;
1432 idx = 0;
1433 for (;;)
1434 {
1435 wp->w_lines[idx] = wp->w_lines[j];
1436 /* stop at line that didn't fit, unless it is still
1437 * valid (no lines deleted) */
1438 if (row > 0 && bot_start + row
1439 + (int)wp->w_lines[j].wl_size > wp->w_height)
1440 {
1441 wp->w_lines_valid = idx + 1;
1442 break;
1443 }
1444 bot_start += wp->w_lines[idx++].wl_size;
1445
1446 /* stop at the last valid entry in w_lines[].wl_size */
1447 if (++j >= wp->w_lines_valid)
1448 {
1449 wp->w_lines_valid = idx;
1450 break;
1451 }
1452 }
1453#ifdef FEAT_DIFF
1454 /* Correct the first entry for filler lines at the top
1455 * when it won't get updated below. */
1456 if (wp->w_p_diff && bot_start > 0)
1457 wp->w_lines[0].wl_size =
1458 plines_win_nofill(wp, wp->w_topline, TRUE)
1459 + wp->w_topfill;
1460#endif
1461 }
1462 }
1463 }
1464
1465 /* When starting redraw in the first line, redraw all lines. When
1466 * there is only one window it's probably faster to clear the screen
1467 * first. */
1468 if (mid_start == 0)
1469 {
1470 mid_end = wp->w_height;
1471 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001472 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001473 /* Clear the screen when it was not done by win_del_lines() or
1474 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1475 * then. */
1476 if (screen_cleared != TRUE)
1477 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001478#ifdef FEAT_WINDOWS
1479 /* The screen was cleared, redraw the tab pages line. */
1480 if (redraw_tabline)
1481 draw_tabline();
1482#endif
1483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001485
1486 /* When win_del_lines() or win_ins_lines() caused the screen to be
1487 * cleared (only happens for the first window) or when screenclear()
1488 * was called directly above, "must_redraw" will have been set to
1489 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1490 if (screen_cleared == TRUE)
1491 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493 else
1494 {
1495 /* Not VALID or INVERTED: redraw all lines. */
1496 mid_start = 0;
1497 mid_end = wp->w_height;
1498 }
1499
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001500 if (type == SOME_VALID)
1501 {
1502 /* SOME_VALID: redraw all lines. */
1503 mid_start = 0;
1504 mid_end = wp->w_height;
1505 type = NOT_VALID;
1506 }
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 /* check if we are updating or removing the inverted part */
1509 if ((VIsual_active && buf == curwin->w_buffer)
1510 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1511 {
1512 linenr_T from, to;
1513
1514 if (VIsual_active)
1515 {
1516 if (VIsual_active
1517 && (VIsual_mode != wp->w_old_visual_mode
1518 || type == INVERTED_ALL))
1519 {
1520 /*
1521 * If the type of Visual selection changed, redraw the whole
1522 * selection. Also when the ownership of the X selection is
1523 * gained or lost.
1524 */
1525 if (curwin->w_cursor.lnum < VIsual.lnum)
1526 {
1527 from = curwin->w_cursor.lnum;
1528 to = VIsual.lnum;
1529 }
1530 else
1531 {
1532 from = VIsual.lnum;
1533 to = curwin->w_cursor.lnum;
1534 }
1535 /* redraw more when the cursor moved as well */
1536 if (wp->w_old_cursor_lnum < from)
1537 from = wp->w_old_cursor_lnum;
1538 if (wp->w_old_cursor_lnum > to)
1539 to = wp->w_old_cursor_lnum;
1540 if (wp->w_old_visual_lnum < from)
1541 from = wp->w_old_visual_lnum;
1542 if (wp->w_old_visual_lnum > to)
1543 to = wp->w_old_visual_lnum;
1544 }
1545 else
1546 {
1547 /*
1548 * Find the line numbers that need to be updated: The lines
1549 * between the old cursor position and the current cursor
1550 * position. Also check if the Visual position changed.
1551 */
1552 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1553 {
1554 from = curwin->w_cursor.lnum;
1555 to = wp->w_old_cursor_lnum;
1556 }
1557 else
1558 {
1559 from = wp->w_old_cursor_lnum;
1560 to = curwin->w_cursor.lnum;
1561 if (from == 0) /* Visual mode just started */
1562 from = to;
1563 }
1564
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001565 if (VIsual.lnum != wp->w_old_visual_lnum
1566 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
1568 if (wp->w_old_visual_lnum < from
1569 && wp->w_old_visual_lnum != 0)
1570 from = wp->w_old_visual_lnum;
1571 if (wp->w_old_visual_lnum > to)
1572 to = wp->w_old_visual_lnum;
1573 if (VIsual.lnum < from)
1574 from = VIsual.lnum;
1575 if (VIsual.lnum > to)
1576 to = VIsual.lnum;
1577 }
1578 }
1579
1580 /*
1581 * If in block mode and changed column or curwin->w_curswant:
1582 * update all lines.
1583 * First compute the actual start and end column.
1584 */
1585 if (VIsual_mode == Ctrl_V)
1586 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001587 colnr_T fromc, toc;
1588#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1589 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaar404406a2014-10-09 13:24:43 +02001591 if (curwin->w_p_lbr)
1592 ve_flags = VE_ALL;
1593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001595#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1596 ve_flags = save_ve_flags;
1597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 ++toc;
1599 if (curwin->w_curswant == MAXCOL)
1600 toc = MAXCOL;
1601
1602 if (fromc != wp->w_old_cursor_fcol
1603 || toc != wp->w_old_cursor_lcol)
1604 {
1605 if (from > VIsual.lnum)
1606 from = VIsual.lnum;
1607 if (to < VIsual.lnum)
1608 to = VIsual.lnum;
1609 }
1610 wp->w_old_cursor_fcol = fromc;
1611 wp->w_old_cursor_lcol = toc;
1612 }
1613 }
1614 else
1615 {
1616 /* Use the line numbers of the old Visual area. */
1617 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1618 {
1619 from = wp->w_old_cursor_lnum;
1620 to = wp->w_old_visual_lnum;
1621 }
1622 else
1623 {
1624 from = wp->w_old_visual_lnum;
1625 to = wp->w_old_cursor_lnum;
1626 }
1627 }
1628
1629 /*
1630 * There is no need to update lines above the top of the window.
1631 */
1632 if (from < wp->w_topline)
1633 from = wp->w_topline;
1634
1635 /*
1636 * If we know the value of w_botline, use it to restrict the update to
1637 * the lines that are visible in the window.
1638 */
1639 if (wp->w_valid & VALID_BOTLINE)
1640 {
1641 if (from >= wp->w_botline)
1642 from = wp->w_botline - 1;
1643 if (to >= wp->w_botline)
1644 to = wp->w_botline - 1;
1645 }
1646
1647 /*
1648 * Find the minimal part to be updated.
1649 * Watch out for scrolling that made entries in w_lines[] invalid.
1650 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1651 * top_end; need to redraw from top_end to the "to" line.
1652 * A middle mouse click with a Visual selection may change the text
1653 * above the Visual area and reset wl_valid, do count these for
1654 * mid_end (in srow).
1655 */
1656 if (mid_start > 0)
1657 {
1658 lnum = wp->w_topline;
1659 idx = 0;
1660 srow = 0;
1661 if (scrolled_down)
1662 mid_start = top_end;
1663 else
1664 mid_start = 0;
1665 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1666 {
1667 if (wp->w_lines[idx].wl_valid)
1668 mid_start += wp->w_lines[idx].wl_size;
1669 else if (!scrolled_down)
1670 srow += wp->w_lines[idx].wl_size;
1671 ++idx;
1672# ifdef FEAT_FOLDING
1673 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1674 lnum = wp->w_lines[idx].wl_lnum;
1675 else
1676# endif
1677 ++lnum;
1678 }
1679 srow += mid_start;
1680 mid_end = wp->w_height;
1681 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1682 {
1683 if (wp->w_lines[idx].wl_valid
1684 && wp->w_lines[idx].wl_lnum >= to + 1)
1685 {
1686 /* Only update until first row of this line */
1687 mid_end = srow;
1688 break;
1689 }
1690 srow += wp->w_lines[idx].wl_size;
1691 }
1692 }
1693 }
1694
1695 if (VIsual_active && buf == curwin->w_buffer)
1696 {
1697 wp->w_old_visual_mode = VIsual_mode;
1698 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1699 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001700 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 wp->w_old_curswant = curwin->w_curswant;
1702 }
1703 else
1704 {
1705 wp->w_old_visual_mode = 0;
1706 wp->w_old_cursor_lnum = 0;
1707 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001708 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1712 /* reset got_int, otherwise regexp won't work */
1713 save_got_int = got_int;
1714 got_int = 0;
1715#endif
1716#ifdef FEAT_FOLDING
1717 win_foldinfo.fi_level = 0;
1718#endif
1719
1720 /*
1721 * Update all the window rows.
1722 */
1723 idx = 0; /* first entry in w_lines[].wl_size */
1724 row = 0;
1725 srow = 0;
1726 lnum = wp->w_topline; /* first line shown in window */
1727 for (;;)
1728 {
1729 /* stop updating when reached the end of the window (check for _past_
1730 * the end of the window is at the end of the loop) */
1731 if (row == wp->w_height)
1732 {
1733 didline = TRUE;
1734 break;
1735 }
1736
1737 /* stop updating when hit the end of the file */
1738 if (lnum > buf->b_ml.ml_line_count)
1739 {
1740 eof = TRUE;
1741 break;
1742 }
1743
1744 /* Remember the starting row of the line that is going to be dealt
1745 * with. It is used further down when the line doesn't fit. */
1746 srow = row;
1747
1748 /*
1749 * Update a line when it is in an area that needs updating, when it
1750 * has changes or w_lines[idx] is invalid.
1751 * bot_start may be halfway a wrapped line after using
1752 * win_del_lines(), check if the current line includes it.
1753 * When syntax folding is being used, the saved syntax states will
1754 * already have been updated, we can't see where the syntax state is
1755 * the same again, just update until the end of the window.
1756 */
1757 if (row < top_end
1758 || (row >= mid_start && row < mid_end)
1759#ifdef FEAT_SEARCH_EXTRA
1760 || top_to_mod
1761#endif
1762 || idx >= wp->w_lines_valid
1763 || (row + wp->w_lines[idx].wl_size > bot_start)
1764 || (mod_top != 0
1765 && (lnum == mod_top
1766 || (lnum >= mod_top
1767 && (lnum < mod_bot
1768#ifdef FEAT_SYN_HL
1769 || did_update == DID_FOLD
1770 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001771 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 && (
1773# ifdef FEAT_FOLDING
1774 (foldmethodIsSyntax(wp)
1775 && hasAnyFolding(wp)) ||
1776# endif
1777 syntax_check_changed(lnum)))
1778#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001779#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001780 /* match in fixed position might need redraw
1781 * if lines were inserted or deleted */
1782 || (wp->w_match_head != NULL
1783 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 )))))
1786 {
1787#ifdef FEAT_SEARCH_EXTRA
1788 if (lnum == mod_top)
1789 top_to_mod = FALSE;
1790#endif
1791
1792 /*
1793 * When at start of changed lines: May scroll following lines
1794 * up or down to minimize redrawing.
1795 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001796 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 */
1798 if (lnum == mod_top
1799 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001800 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 int old_rows = 0;
1803 int new_rows = 0;
1804 int xtra_rows;
1805 linenr_T l;
1806
1807 /* Count the old number of window rows, using w_lines[], which
1808 * should still contain the sizes for the lines as they are
1809 * currently displayed. */
1810 for (i = idx; i < wp->w_lines_valid; ++i)
1811 {
1812 /* Only valid lines have a meaningful wl_lnum. Invalid
1813 * lines are part of the changed area. */
1814 if (wp->w_lines[i].wl_valid
1815 && wp->w_lines[i].wl_lnum == mod_bot)
1816 break;
1817 old_rows += wp->w_lines[i].wl_size;
1818#ifdef FEAT_FOLDING
1819 if (wp->w_lines[i].wl_valid
1820 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1821 {
1822 /* Must have found the last valid entry above mod_bot.
1823 * Add following invalid entries. */
1824 ++i;
1825 while (i < wp->w_lines_valid
1826 && !wp->w_lines[i].wl_valid)
1827 old_rows += wp->w_lines[i++].wl_size;
1828 break;
1829 }
1830#endif
1831 }
1832
1833 if (i >= wp->w_lines_valid)
1834 {
1835 /* We can't find a valid line below the changed lines,
1836 * need to redraw until the end of the window.
1837 * Inserting/deleting lines has no use. */
1838 bot_start = 0;
1839 }
1840 else
1841 {
1842 /* Able to count old number of rows: Count new window
1843 * rows, and may insert/delete lines */
1844 j = idx;
1845 for (l = lnum; l < mod_bot; ++l)
1846 {
1847#ifdef FEAT_FOLDING
1848 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1849 ++new_rows;
1850 else
1851#endif
1852#ifdef FEAT_DIFF
1853 if (l == wp->w_topline)
1854 new_rows += plines_win_nofill(wp, l, TRUE)
1855 + wp->w_topfill;
1856 else
1857#endif
1858 new_rows += plines_win(wp, l, TRUE);
1859 ++j;
1860 if (new_rows > wp->w_height - row - 2)
1861 {
1862 /* it's getting too much, must redraw the rest */
1863 new_rows = 9999;
1864 break;
1865 }
1866 }
1867 xtra_rows = new_rows - old_rows;
1868 if (xtra_rows < 0)
1869 {
1870 /* May scroll text up. If there is not enough
1871 * remaining text or scrolling fails, must redraw the
1872 * rest. If scrolling works, must redraw the text
1873 * below the scrolled text. */
1874 if (row - xtra_rows >= wp->w_height - 2)
1875 mod_bot = MAXLNUM;
1876 else
1877 {
1878 check_for_delay(FALSE);
1879 if (win_del_lines(wp, row,
1880 -xtra_rows, FALSE, FALSE) == FAIL)
1881 mod_bot = MAXLNUM;
1882 else
1883 bot_start = wp->w_height + xtra_rows;
1884 }
1885 }
1886 else if (xtra_rows > 0)
1887 {
1888 /* May scroll text down. If there is not enough
1889 * remaining text of scrolling fails, must redraw the
1890 * rest. */
1891 if (row + xtra_rows >= wp->w_height - 2)
1892 mod_bot = MAXLNUM;
1893 else
1894 {
1895 check_for_delay(FALSE);
1896 if (win_ins_lines(wp, row + old_rows,
1897 xtra_rows, FALSE, FALSE) == FAIL)
1898 mod_bot = MAXLNUM;
1899 else if (top_end > row + old_rows)
1900 /* Scrolled the part at the top that requires
1901 * updating down. */
1902 top_end += xtra_rows;
1903 }
1904 }
1905
1906 /* When not updating the rest, may need to move w_lines[]
1907 * entries. */
1908 if (mod_bot != MAXLNUM && i != j)
1909 {
1910 if (j < i)
1911 {
1912 int x = row + new_rows;
1913
1914 /* move entries in w_lines[] upwards */
1915 for (;;)
1916 {
1917 /* stop at last valid entry in w_lines[] */
1918 if (i >= wp->w_lines_valid)
1919 {
1920 wp->w_lines_valid = j;
1921 break;
1922 }
1923 wp->w_lines[j] = wp->w_lines[i];
1924 /* stop at a line that won't fit */
1925 if (x + (int)wp->w_lines[j].wl_size
1926 > wp->w_height)
1927 {
1928 wp->w_lines_valid = j + 1;
1929 break;
1930 }
1931 x += wp->w_lines[j++].wl_size;
1932 ++i;
1933 }
1934 if (bot_start > x)
1935 bot_start = x;
1936 }
1937 else /* j > i */
1938 {
1939 /* move entries in w_lines[] downwards */
1940 j -= i;
1941 wp->w_lines_valid += j;
1942 if (wp->w_lines_valid > wp->w_height)
1943 wp->w_lines_valid = wp->w_height;
1944 for (i = wp->w_lines_valid; i - j >= idx; --i)
1945 wp->w_lines[i] = wp->w_lines[i - j];
1946
1947 /* The w_lines[] entries for inserted lines are
1948 * now invalid, but wl_size may be used above.
1949 * Reset to zero. */
1950 while (i >= idx)
1951 {
1952 wp->w_lines[i].wl_size = 0;
1953 wp->w_lines[i--].wl_valid = FALSE;
1954 }
1955 }
1956 }
1957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959
1960#ifdef FEAT_FOLDING
1961 /*
1962 * When lines are folded, display one line for all of them.
1963 * Otherwise, display normally (can be several display lines when
1964 * 'wrap' is on).
1965 */
1966 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1967 if (fold_count != 0)
1968 {
1969 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1970 ++row;
1971 --fold_count;
1972 wp->w_lines[idx].wl_folded = TRUE;
1973 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1974# ifdef FEAT_SYN_HL
1975 did_update = DID_FOLD;
1976# endif
1977 }
1978 else
1979#endif
1980 if (idx < wp->w_lines_valid
1981 && wp->w_lines[idx].wl_valid
1982 && wp->w_lines[idx].wl_lnum == lnum
1983 && lnum > wp->w_topline
1984 && !(dy_flags & DY_LASTLINE)
1985 && srow + wp->w_lines[idx].wl_size > wp->w_height
1986#ifdef FEAT_DIFF
1987 && diff_check_fill(wp, lnum) == 0
1988#endif
1989 )
1990 {
1991 /* This line is not going to fit. Don't draw anything here,
1992 * will draw "@ " lines below. */
1993 row = wp->w_height + 1;
1994 }
1995 else
1996 {
1997#ifdef FEAT_SEARCH_EXTRA
1998 prepare_search_hl(wp, lnum);
1999#endif
2000#ifdef FEAT_SYN_HL
2001 /* Let the syntax stuff know we skipped a few lines. */
2002 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002003 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 syntax_end_parsing(syntax_last_parsed + 1);
2005#endif
2006
2007 /*
2008 * Display one line.
2009 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002010 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012#ifdef FEAT_FOLDING
2013 wp->w_lines[idx].wl_folded = FALSE;
2014 wp->w_lines[idx].wl_lastlnum = lnum;
2015#endif
2016#ifdef FEAT_SYN_HL
2017 did_update = DID_LINE;
2018 syntax_last_parsed = lnum;
2019#endif
2020 }
2021
2022 wp->w_lines[idx].wl_lnum = lnum;
2023 wp->w_lines[idx].wl_valid = TRUE;
2024 if (row > wp->w_height) /* past end of screen */
2025 {
2026 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002027 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2029 ++idx;
2030 break;
2031 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002032 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 wp->w_lines[idx].wl_size = row - srow;
2034 ++idx;
2035#ifdef FEAT_FOLDING
2036 lnum += fold_count + 1;
2037#else
2038 ++lnum;
2039#endif
2040 }
2041 else
2042 {
2043 /* This line does not need updating, advance to the next one */
2044 row += wp->w_lines[idx++].wl_size;
2045 if (row > wp->w_height) /* past end of screen */
2046 break;
2047#ifdef FEAT_FOLDING
2048 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2049#else
2050 ++lnum;
2051#endif
2052#ifdef FEAT_SYN_HL
2053 did_update = DID_NONE;
2054#endif
2055 }
2056
2057 if (lnum > buf->b_ml.ml_line_count)
2058 {
2059 eof = TRUE;
2060 break;
2061 }
2062 }
2063 /*
2064 * End of loop over all window lines.
2065 */
2066
2067
2068 if (idx > wp->w_lines_valid)
2069 wp->w_lines_valid = idx;
2070
2071#ifdef FEAT_SYN_HL
2072 /*
2073 * Let the syntax stuff know we stop parsing here.
2074 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002075 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 syntax_end_parsing(syntax_last_parsed + 1);
2077#endif
2078
2079 /*
2080 * If we didn't hit the end of the file, and we didn't finish the last
2081 * line we were working on, then the line didn't fit.
2082 */
2083 wp->w_empty_rows = 0;
2084#ifdef FEAT_DIFF
2085 wp->w_filler_rows = 0;
2086#endif
2087 if (!eof && !didline)
2088 {
2089 if (lnum == wp->w_topline)
2090 {
2091 /*
2092 * Single line that does not fit!
2093 * Don't overwrite it, it can be edited.
2094 */
2095 wp->w_botline = lnum + 1;
2096 }
2097#ifdef FEAT_DIFF
2098 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2099 {
2100 /* Window ends in filler lines. */
2101 wp->w_botline = lnum;
2102 wp->w_filler_rows = wp->w_height - srow;
2103 }
2104#endif
2105 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2106 {
2107 /*
2108 * Last line isn't finished: Display "@@@" at the end.
2109 */
2110 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2111 W_WINROW(wp) + wp->w_height,
2112 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2113 '@', '@', hl_attr(HLF_AT));
2114 set_empty_rows(wp, srow);
2115 wp->w_botline = lnum;
2116 }
2117 else
2118 {
2119 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2120 wp->w_botline = lnum;
2121 }
2122 }
2123 else
2124 {
2125#ifdef FEAT_VERTSPLIT
2126 draw_vsep_win(wp, row);
2127#endif
2128 if (eof) /* we hit the end of the file */
2129 {
2130 wp->w_botline = buf->b_ml.ml_line_count + 1;
2131#ifdef FEAT_DIFF
2132 j = diff_check_fill(wp, wp->w_botline);
2133 if (j > 0 && !wp->w_botfill)
2134 {
2135 /*
2136 * Display filler lines at the end of the file
2137 */
2138 if (char2cells(fill_diff) > 1)
2139 i = '-';
2140 else
2141 i = fill_diff;
2142 if (row + j > wp->w_height)
2143 j = wp->w_height - row;
2144 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2145 row += j;
2146 }
2147#endif
2148 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002149 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 wp->w_botline = lnum;
2151
2152 /* make sure the rest of the screen is blank */
2153 /* put '~'s on rows that aren't part of the file. */
2154 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2155 }
2156
2157 /* Reset the type of redrawing required, the window has been updated. */
2158 wp->w_redr_type = 0;
2159#ifdef FEAT_DIFF
2160 wp->w_old_topfill = wp->w_topfill;
2161 wp->w_old_botfill = wp->w_botfill;
2162#endif
2163
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002164 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 /*
2167 * There is a trick with w_botline. If we invalidate it on each
2168 * change that might modify it, this will cause a lot of expensive
2169 * calls to plines() in update_topline() each time. Therefore the
2170 * value of w_botline is often approximated, and this value is used to
2171 * compute the value of w_topline. If the value of w_botline was
2172 * wrong, check that the value of w_topline is correct (cursor is on
2173 * the visible part of the text). If it's not, we need to redraw
2174 * again. Mostly this just means scrolling up a few lines, so it
2175 * doesn't look too bad. Only do this for the current window (where
2176 * changes are relevant).
2177 */
2178 wp->w_valid |= VALID_BOTLINE;
2179 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2180 {
2181 recursive = TRUE;
2182 curwin->w_valid &= ~VALID_TOPLINE;
2183 update_topline(); /* may invalidate w_botline again */
2184 if (must_redraw != 0)
2185 {
2186 /* Don't update for changes in buffer again. */
2187 i = curbuf->b_mod_set;
2188 curbuf->b_mod_set = FALSE;
2189 win_update(curwin);
2190 must_redraw = 0;
2191 curbuf->b_mod_set = i;
2192 }
2193 recursive = FALSE;
2194 }
2195 }
2196
2197#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2198 /* restore got_int, unless CTRL-C was hit while redrawing */
2199 if (!got_int)
2200 got_int = save_got_int;
2201#endif
2202}
2203
2204#ifdef FEAT_SIGNS
2205static int draw_signcolumn __ARGS((win_T *wp));
2206
2207/*
2208 * Return TRUE when window "wp" has a column to draw signs in.
2209 */
2210 static int
2211draw_signcolumn(wp)
2212 win_T *wp;
2213{
2214 return (wp->w_buffer->b_signlist != NULL
2215# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002216 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217# endif
2218 );
2219}
2220#endif
2221
2222/*
2223 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2224 * as the filler character.
2225 */
2226 static void
2227win_draw_end(wp, c1, c2, row, endrow, hl)
2228 win_T *wp;
2229 int c1;
2230 int c2;
2231 int row;
2232 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002233 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2236 int n = 0;
2237# define FDC_OFF n
2238#else
2239# define FDC_OFF 0
2240#endif
2241
2242#ifdef FEAT_RIGHTLEFT
2243 if (wp->w_p_rl)
2244 {
2245 /* No check for cmdline window: should never be right-left. */
2246# ifdef FEAT_FOLDING
2247 n = wp->w_p_fdc;
2248
2249 if (n > 0)
2250 {
2251 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002252 if (n > W_WIDTH(wp))
2253 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2255 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2256 ' ', ' ', hl_attr(HLF_FC));
2257 }
2258# endif
2259# ifdef FEAT_SIGNS
2260 if (draw_signcolumn(wp))
2261 {
2262 int nn = n + 2;
2263
2264 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002265 if (nn > W_WIDTH(wp))
2266 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2268 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2269 ' ', ' ', hl_attr(HLF_SC));
2270 n = nn;
2271 }
2272# endif
2273 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2274 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2275 c2, c2, hl_attr(hl));
2276 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2277 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2278 c1, c2, hl_attr(hl));
2279 }
2280 else
2281#endif
2282 {
2283#ifdef FEAT_CMDWIN
2284 if (cmdwin_type != 0 && wp == curwin)
2285 {
2286 /* draw the cmdline character in the leftmost column */
2287 n = 1;
2288 if (n > wp->w_width)
2289 n = wp->w_width;
2290 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2291 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2292 cmdwin_type, ' ', hl_attr(HLF_AT));
2293 }
2294#endif
2295#ifdef FEAT_FOLDING
2296 if (wp->w_p_fdc > 0)
2297 {
2298 int nn = n + wp->w_p_fdc;
2299
2300 /* draw the fold column at the left */
2301 if (nn > W_WIDTH(wp))
2302 nn = W_WIDTH(wp);
2303 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2304 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2305 ' ', ' ', hl_attr(HLF_FC));
2306 n = nn;
2307 }
2308#endif
2309#ifdef FEAT_SIGNS
2310 if (draw_signcolumn(wp))
2311 {
2312 int nn = n + 2;
2313
2314 /* draw the sign column after the fold column */
2315 if (nn > W_WIDTH(wp))
2316 nn = W_WIDTH(wp);
2317 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2318 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2319 ' ', ' ', hl_attr(HLF_SC));
2320 n = nn;
2321 }
2322#endif
2323 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2324 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2325 c1, c2, hl_attr(hl));
2326 }
2327 set_empty_rows(wp, row);
2328}
2329
Bram Moolenaar1a384422010-07-14 19:53:30 +02002330#ifdef FEAT_SYN_HL
2331static int advance_color_col __ARGS((int vcol, int **color_cols));
2332
2333/*
2334 * Advance **color_cols and return TRUE when there are columns to draw.
2335 */
2336 static int
2337advance_color_col(vcol, color_cols)
2338 int vcol;
2339 int **color_cols;
2340{
2341 while (**color_cols >= 0 && vcol > **color_cols)
2342 ++*color_cols;
2343 return (**color_cols >= 0);
2344}
2345#endif
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#ifdef FEAT_FOLDING
2348/*
2349 * Display one folded line.
2350 */
2351 static void
2352fold_line(wp, fold_count, foldinfo, lnum, row)
2353 win_T *wp;
2354 long fold_count;
2355 foldinfo_T *foldinfo;
2356 linenr_T lnum;
2357 int row;
2358{
2359 char_u buf[51];
2360 pos_T *top, *bot;
2361 linenr_T lnume = lnum + fold_count - 1;
2362 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002363 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 int col;
2366 int txtcol;
2367 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002368 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 /* Build the fold line:
2371 * 1. Add the cmdwin_type for the command-line window
2372 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002373 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * 4. Compose the text
2375 * 5. Add the text
2376 * 6. set highlighting for the Visual area an other text
2377 */
2378 col = 0;
2379
2380 /*
2381 * 1. Add the cmdwin_type for the command-line window
2382 * Ignores 'rightleft', this window is never right-left.
2383 */
2384#ifdef FEAT_CMDWIN
2385 if (cmdwin_type != 0 && wp == curwin)
2386 {
2387 ScreenLines[off] = cmdwin_type;
2388 ScreenAttrs[off] = hl_attr(HLF_AT);
2389#ifdef FEAT_MBYTE
2390 if (enc_utf8)
2391 ScreenLinesUC[off] = 0;
2392#endif
2393 ++col;
2394 }
2395#endif
2396
2397 /*
2398 * 2. Add the 'foldcolumn'
2399 */
2400 fdc = wp->w_p_fdc;
2401 if (fdc > W_WIDTH(wp) - col)
2402 fdc = W_WIDTH(wp) - col;
2403 if (fdc > 0)
2404 {
2405 fill_foldcolumn(buf, wp, TRUE, lnum);
2406#ifdef FEAT_RIGHTLEFT
2407 if (wp->w_p_rl)
2408 {
2409 int i;
2410
2411 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2412 hl_attr(HLF_FC));
2413 /* reverse the fold column */
2414 for (i = 0; i < fdc; ++i)
2415 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2416 }
2417 else
2418#endif
2419 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2420 col += fdc;
2421 }
2422
2423#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002424# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2425 for (ri = 0; ri < l; ++ri) \
2426 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2427 else \
2428 for (ri = 0; ri < l; ++ri) \
2429 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002431# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2432 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433#endif
2434
Bram Moolenaar64486672010-05-16 15:46:46 +02002435 /* Set all attributes of the 'number' or 'relativenumber' column and the
2436 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002437 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439#ifdef FEAT_SIGNS
2440 /* If signs are being displayed, add two spaces. */
2441 if (draw_signcolumn(wp))
2442 {
2443 len = W_WIDTH(wp) - col;
2444 if (len > 0)
2445 {
2446 if (len > 2)
2447 len = 2;
2448# ifdef FEAT_RIGHTLEFT
2449 if (wp->w_p_rl)
2450 /* the line number isn't reversed */
2451 copy_text_attr(off + W_WIDTH(wp) - len - col,
2452 (char_u *)" ", len, hl_attr(HLF_FL));
2453 else
2454# endif
2455 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2456 col += len;
2457 }
2458 }
2459#endif
2460
2461 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002462 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002464 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 len = W_WIDTH(wp) - col;
2467 if (len > 0)
2468 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002469 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002470 long num;
2471 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002472
2473 if (len > w + 1)
2474 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002475
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002476 if (wp->w_p_nu && !wp->w_p_rnu)
2477 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002478 num = (long)lnum;
2479 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002480 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002481 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002482 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002483 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002484 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002485 /* 'number' + 'relativenumber': cursor line shows absolute
2486 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002487 num = lnum;
2488 fmt = "%-*ld ";
2489 }
2490 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002491
Bram Moolenaar700e7342013-01-30 12:31:36 +01002492 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493#ifdef FEAT_RIGHTLEFT
2494 if (wp->w_p_rl)
2495 /* the line number isn't reversed */
2496 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2497 hl_attr(HLF_FL));
2498 else
2499#endif
2500 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2501 col += len;
2502 }
2503 }
2504
2505 /*
2506 * 4. Compose the folded-line string with 'foldtext', if set.
2507 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002508 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 txtcol = col; /* remember where text starts */
2511
2512 /*
2513 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2514 * Right-left text is put in columns 0 - number-col, normal text is put
2515 * in columns number-col - window-width.
2516 */
2517#ifdef FEAT_MBYTE
2518 if (has_mbyte)
2519 {
2520 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002521 int u8c, u8cc[MAX_MCO];
2522 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 int idx;
2524 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002525 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526# ifdef FEAT_ARABIC
2527 int prev_c = 0; /* previous Arabic character */
2528 int prev_c1 = 0; /* first composing char for prev_c */
2529# endif
2530
2531# ifdef FEAT_RIGHTLEFT
2532 if (wp->w_p_rl)
2533 idx = off;
2534 else
2535# endif
2536 idx = off + col;
2537
2538 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2539 for (p = text; *p != NUL; )
2540 {
2541 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002542 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (col + cells > W_WIDTH(wp)
2544# ifdef FEAT_RIGHTLEFT
2545 - (wp->w_p_rl ? col : 0)
2546# endif
2547 )
2548 break;
2549 ScreenLines[idx] = *p;
2550 if (enc_utf8)
2551 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002552 u8c = utfc_ptr2char(p, u8cc);
2553 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 ScreenLinesUC[idx] = 0;
2556#ifdef FEAT_ARABIC
2557 prev_c = u8c;
2558#endif
2559 }
2560 else
2561 {
2562#ifdef FEAT_ARABIC
2563 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2564 {
2565 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002566 int pc, pc1, nc;
2567 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 int firstbyte = *p;
2569
2570 /* The idea of what is the previous and next
2571 * character depends on 'rightleft'. */
2572 if (wp->w_p_rl)
2573 {
2574 pc = prev_c;
2575 pc1 = prev_c1;
2576 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002577 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579 else
2580 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002581 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002583 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585 prev_c = u8c;
2586
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002587 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 pc, pc1, nc);
2589 ScreenLines[idx] = firstbyte;
2590 }
2591 else
2592 prev_c = u8c;
2593#endif
2594 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002595#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 if (u8c >= 0x10000)
2597 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2598 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002601 for (i = 0; i < Screen_mco; ++i)
2602 {
2603 ScreenLinesC[i][idx] = u8cc[i];
2604 if (u8cc[i] == 0)
2605 break;
2606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608 if (cells > 1)
2609 ScreenLines[idx + 1] = 0;
2610 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002611 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2612 /* double-byte single width character */
2613 ScreenLines2[idx] = p[1];
2614 else if (cells > 1)
2615 /* double-width character */
2616 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 col += cells;
2618 idx += cells;
2619 p += c_len;
2620 }
2621 }
2622 else
2623#endif
2624 {
2625 len = (int)STRLEN(text);
2626 if (len > W_WIDTH(wp) - col)
2627 len = W_WIDTH(wp) - col;
2628 if (len > 0)
2629 {
2630#ifdef FEAT_RIGHTLEFT
2631 if (wp->w_p_rl)
2632 STRNCPY(current_ScreenLine, text, len);
2633 else
2634#endif
2635 STRNCPY(current_ScreenLine + col, text, len);
2636 col += len;
2637 }
2638 }
2639
2640 /* Fill the rest of the line with the fold filler */
2641#ifdef FEAT_RIGHTLEFT
2642 if (wp->w_p_rl)
2643 col -= txtcol;
2644#endif
2645 while (col < W_WIDTH(wp)
2646#ifdef FEAT_RIGHTLEFT
2647 - (wp->w_p_rl ? txtcol : 0)
2648#endif
2649 )
2650 {
2651#ifdef FEAT_MBYTE
2652 if (enc_utf8)
2653 {
2654 if (fill_fold >= 0x80)
2655 {
2656 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002657 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659 else
2660 ScreenLinesUC[off + col] = 0;
2661 }
2662#endif
2663 ScreenLines[off + col++] = fill_fold;
2664 }
2665
2666 if (text != buf)
2667 vim_free(text);
2668
2669 /*
2670 * 6. set highlighting for the Visual area an other text.
2671 * If all folded lines are in the Visual area, highlight the line.
2672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2674 {
2675 if (ltoreq(curwin->w_cursor, VIsual))
2676 {
2677 /* Visual is after curwin->w_cursor */
2678 top = &curwin->w_cursor;
2679 bot = &VIsual;
2680 }
2681 else
2682 {
2683 /* Visual is before curwin->w_cursor */
2684 top = &VIsual;
2685 bot = &curwin->w_cursor;
2686 }
2687 if (lnum >= top->lnum
2688 && lnume <= bot->lnum
2689 && (VIsual_mode != 'v'
2690 || ((lnum > top->lnum
2691 || (lnum == top->lnum
2692 && top->col == 0))
2693 && (lnume < bot->lnum
2694 || (lnume == bot->lnum
2695 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002696 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 if (VIsual_mode == Ctrl_V)
2699 {
2700 /* Visual block mode: highlight the chars part of the block */
2701 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2702 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002703 if (wp->w_old_cursor_lcol != MAXCOL
2704 && wp->w_old_cursor_lcol + txtcol
2705 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 len = wp->w_old_cursor_lcol;
2707 else
2708 len = W_WIDTH(wp) - txtcol;
2709 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002710 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712 }
2713 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002716 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002721#ifdef FEAT_SYN_HL
2722 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002723 if (wp->w_p_cuc)
2724 {
2725 txtcol += wp->w_virtcol;
2726 if (wp->w_p_wrap)
2727 txtcol -= wp->w_skipcol;
2728 else
2729 txtcol -= wp->w_leftcol;
2730 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2731 ScreenAttrs[off + txtcol] = hl_combine_attr(
2732 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2733 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2737 (int)W_WIDTH(wp), FALSE);
2738
2739 /*
2740 * Update w_cline_height and w_cline_folded if the cursor line was
2741 * updated (saves a call to plines() later).
2742 */
2743 if (wp == curwin
2744 && lnum <= curwin->w_cursor.lnum
2745 && lnume >= curwin->w_cursor.lnum)
2746 {
2747 curwin->w_cline_row = row;
2748 curwin->w_cline_height = 1;
2749 curwin->w_cline_folded = TRUE;
2750 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2751 }
2752}
2753
2754/*
2755 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2756 */
2757 static void
2758copy_text_attr(off, buf, len, attr)
2759 int off;
2760 char_u *buf;
2761 int len;
2762 int attr;
2763{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002764 int i;
2765
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 mch_memmove(ScreenLines + off, buf, (size_t)len);
2767# ifdef FEAT_MBYTE
2768 if (enc_utf8)
2769 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2770# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002771 for (i = 0; i < len; ++i)
2772 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773}
2774
2775/*
2776 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002777 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 */
2779 static void
2780fill_foldcolumn(p, wp, closed, lnum)
2781 char_u *p;
2782 win_T *wp;
2783 int closed; /* TRUE of FALSE */
2784 linenr_T lnum; /* current line number */
2785{
2786 int i = 0;
2787 int level;
2788 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002789 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 /* Init to all spaces. */
2792 copy_spaces(p, (size_t)wp->w_p_fdc);
2793
2794 level = win_foldinfo.fi_level;
2795 if (level > 0)
2796 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002797 /* If there is only one column put more info in it. */
2798 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2799
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 /* If the column is too narrow, we start at the lowest level that
2801 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002802 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (first_level < 1)
2804 first_level = 1;
2805
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002806 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
2808 if (win_foldinfo.fi_lnum == lnum
2809 && first_level + i >= win_foldinfo.fi_low_level)
2810 p[i] = '-';
2811 else if (first_level == 1)
2812 p[i] = '|';
2813 else if (first_level + i <= 9)
2814 p[i] = '0' + first_level + i;
2815 else
2816 p[i] = '>';
2817 if (first_level + i == level)
2818 break;
2819 }
2820 }
2821 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002822 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823}
2824#endif /* FEAT_FOLDING */
2825
2826/*
2827 * Display line "lnum" of window 'wp' on the screen.
2828 * Start at row "startrow", stop when "endrow" is reached.
2829 * wp->w_virtcol needs to be valid.
2830 *
2831 * Return the number of last row the line occupies.
2832 */
2833 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002834win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 win_T *wp;
2836 linenr_T lnum;
2837 int startrow;
2838 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002839 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 int col; /* visual column on screen */
2842 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2843 int c = 0; /* init for GCC */
2844 long vcol = 0; /* virtual column (for tabs) */
2845 long vcol_prev = -1; /* "vcol" of previous character */
2846 char_u *line; /* current line */
2847 char_u *ptr; /* current position in "line" */
2848 int row; /* row in the window, excl w_winrow */
2849 int screen_row; /* row on the screen, incl w_winrow */
2850
2851 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2852 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002853 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002854 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 int c_extra = NUL; /* extra chars, all the same */
2856 int extra_attr = 0; /* attributes when n_extra != 0 */
2857 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2858 displaying lcs_eol at end-of-line */
2859 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2860 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2861
2862 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2863 int saved_n_extra = 0;
2864 char_u *saved_p_extra = NULL;
2865 int saved_c_extra = 0;
2866 int saved_char_attr = 0;
2867
2868 int n_attr = 0; /* chars with special attr */
2869 int saved_attr2 = 0; /* char_attr saved for n_attr */
2870 int n_attr3 = 0; /* chars with overruling special attr */
2871 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2872
2873 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2874
2875 int fromcol, tocol; /* start/end of inverting */
2876 int fromcol_prev = -2; /* start of inverting after cursor */
2877 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002879 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 pos_T pos;
2881 long v;
2882
2883 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002884 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2886 in this line */
2887 int attr = 0; /* attributes for area highlighting */
2888 int area_attr = 0; /* attributes desired by highlighting */
2889 int search_attr = 0; /* attributes desired by 'hlsearch' */
2890#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002891 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 int syntax_attr = 0; /* attributes desired by syntax */
2893 int has_syntax = FALSE; /* this buffer has syntax highl. */
2894 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002895 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002896 int draw_color_col = FALSE; /* highlight colorcolumn */
2897 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002898#endif
2899#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002900 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002901# define SPWORDLEN 150
2902 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002903 int nextlinecol = 0; /* column where nextline[] starts */
2904 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002905 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002906 int spell_attr = 0; /* attributes desired by spelling */
2907 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002908 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2909 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002910 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002911 static int cap_col = -1; /* column to check for Cap word */
2912 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002913 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#endif
2915 int extra_check; /* has syntax or linebreak */
2916#ifdef FEAT_MBYTE
2917 int multi_attr = 0; /* attributes desired by multibyte */
2918 int mb_l = 1; /* multi-byte byte length */
2919 int mb_c = 0; /* decoded multi-byte character */
2920 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002921 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922#endif
2923#ifdef FEAT_DIFF
2924 int filler_lines; /* nr of filler lines to be drawn */
2925 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002926 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 int change_start = MAXCOL; /* first col of changed area */
2928 int change_end = -1; /* last col of changed area */
2929#endif
2930 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2931#ifdef FEAT_LINEBREAK
2932 int need_showbreak = FALSE;
2933#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002934#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2935 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002937 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938#endif
2939#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002940 matchitem_T *cur; /* points to the match list */
2941 match_T *shl; /* points to search_hl or a match */
2942 int shl_flag; /* flag to indicate whether search_hl
2943 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002944 int pos_inprogress; /* marks that position match search is
2945 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002946 int prevcol_hl_flag; /* flag to indicate whether prevcol
2947 equals startcol of search_hl or one
2948 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949#endif
2950#ifdef FEAT_ARABIC
2951 int prev_c = 0; /* previous Arabic character */
2952 int prev_c1 = 0; /* first composing char for prev_c */
2953#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002954#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002955 int did_line_attr = 0;
2956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 /* draw_state: items that are drawn in sequence: */
2959#define WL_START 0 /* nothing done yet */
2960#ifdef FEAT_CMDWIN
2961# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2962#else
2963# define WL_CMDLINE WL_START
2964#endif
2965#ifdef FEAT_FOLDING
2966# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2967#else
2968# define WL_FOLD WL_CMDLINE
2969#endif
2970#ifdef FEAT_SIGNS
2971# define WL_SIGN WL_FOLD + 1 /* column for signs */
2972#else
2973# define WL_SIGN WL_FOLD /* column for signs */
2974#endif
2975#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002976#ifdef FEAT_LINEBREAK
2977# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02002979# define WL_BRI WL_NR
2980#endif
2981#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2982# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
2983#else
2984# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#endif
2986#define WL_LINE WL_SBR + 1 /* text in the line */
2987 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002988#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 int feedback_col = 0;
2990 int feedback_old_attr = -1;
2991#endif
2992
Bram Moolenaar860cae12010-06-05 23:22:07 +02002993#ifdef FEAT_CONCEAL
2994 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002995 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002996 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002997 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002998 int is_concealing = FALSE;
2999 int boguscols = 0; /* nonexistent columns added to force
3000 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003001 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003002 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003003# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003004# define FIX_FOR_BOGUSCOLS \
3005 { \
3006 n_extra += vcol_off; \
3007 vcol -= vcol_off; \
3008 vcol_off = 0; \
3009 col -= boguscols; \
3010 boguscols = 0; \
3011 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003012#else
3013# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
3016 if (startrow > endrow) /* past the end already! */
3017 return startrow;
3018
3019 row = startrow;
3020 screen_row = row + W_WINROW(wp);
3021
3022 /*
3023 * To speed up the loop below, set extra_check when there is linebreak,
3024 * trailing white space and/or syntax processing to be done.
3025 */
3026#ifdef FEAT_LINEBREAK
3027 extra_check = wp->w_p_lbr;
3028#else
3029 extra_check = 0;
3030#endif
3031#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003032 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
3034 /* Prepare for syntax highlighting in this line. When there is an
3035 * error, stop syntax highlighting. */
3036 save_did_emsg = did_emsg;
3037 did_emsg = FALSE;
3038 syntax_start(wp, lnum);
3039 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003040 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else
3042 {
3043 did_emsg = save_did_emsg;
3044 has_syntax = TRUE;
3045 extra_check = TRUE;
3046 }
3047 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003048
3049 /* Check for columns to display for 'colorcolumn'. */
3050 color_cols = wp->w_p_cc_cols;
3051 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003052 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003053#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003054
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003055#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003056 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003057 && *wp->w_s->b_p_spl != NUL
3058 && wp->w_s->b_langp.ga_len > 0
3059 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003060 {
3061 /* Prepare for spell checking. */
3062 has_spell = TRUE;
3063 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003064
3065 /* Get the start of the next line, so that words that wrap to the next
3066 * line are found too: "et<line-break>al.".
3067 * Trick: skip a few chars for C/shell/Vim comments */
3068 nextline[SPWORDLEN] = NUL;
3069 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3070 {
3071 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3072 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3073 }
3074
3075 /* When a word wrapped from the previous line the start of the current
3076 * line is valid. */
3077 if (lnum == checked_lnum)
3078 cur_checked_col = checked_col;
3079 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003080
3081 /* When there was a sentence end in the previous line may require a
3082 * word starting with capital in this line. In line 1 always check
3083 * the first word. */
3084 if (lnum != capcol_lnum)
3085 cap_col = -1;
3086 if (lnum == 1)
3087 cap_col = 0;
3088 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#endif
3091
3092 /*
3093 * handle visual active in this window
3094 */
3095 fromcol = -10;
3096 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3098 {
3099 /* Visual is after curwin->w_cursor */
3100 if (ltoreq(curwin->w_cursor, VIsual))
3101 {
3102 top = &curwin->w_cursor;
3103 bot = &VIsual;
3104 }
3105 else /* Visual is before curwin->w_cursor */
3106 {
3107 top = &VIsual;
3108 bot = &curwin->w_cursor;
3109 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003110 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 if (VIsual_mode == Ctrl_V) /* block mode */
3112 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003113 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
3115 fromcol = wp->w_old_cursor_fcol;
3116 tocol = wp->w_old_cursor_lcol;
3117 }
3118 }
3119 else /* non-block mode */
3120 {
3121 if (lnum > top->lnum && lnum <= bot->lnum)
3122 fromcol = 0;
3123 else if (lnum == top->lnum)
3124 {
3125 if (VIsual_mode == 'V') /* linewise */
3126 fromcol = 0;
3127 else
3128 {
3129 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3130 if (gchar_pos(top) == NUL)
3131 tocol = fromcol + 1;
3132 }
3133 }
3134 if (VIsual_mode != 'V' && lnum == bot->lnum)
3135 {
3136 if (*p_sel == 'e' && bot->col == 0
3137#ifdef FEAT_VIRTUALEDIT
3138 && bot->coladd == 0
3139#endif
3140 )
3141 {
3142 fromcol = -10;
3143 tocol = MAXCOL;
3144 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003145 else if (bot->col == MAXCOL)
3146 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 else
3148 {
3149 pos = *bot;
3150 if (*p_sel == 'e')
3151 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3152 else
3153 {
3154 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3155 ++tocol;
3156 }
3157 }
3158 }
3159 }
3160
3161#ifndef MSDOS
3162 /* Check if the character under the cursor should not be inverted */
3163 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3164# ifdef FEAT_GUI
3165 && !gui.in_use
3166# endif
3167 )
3168 noinvcur = TRUE;
3169#endif
3170
3171 /* if inverting in this line set area_highlighting */
3172 if (fromcol >= 0)
3173 {
3174 area_highlighting = TRUE;
3175 attr = hl_attr(HLF_V);
3176#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003177 if ((clip_star.available && !clip_star.owned
3178 && clip_isautosel_star())
3179 || (clip_plus.available && !clip_plus.owned
3180 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 attr = hl_attr(HLF_VNC);
3182#endif
3183 }
3184 }
3185
3186 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003187 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003189 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 && wp == curwin
3191 && lnum >= curwin->w_cursor.lnum
3192 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3193 {
3194 if (lnum == curwin->w_cursor.lnum)
3195 getvcol(curwin, &(curwin->w_cursor),
3196 (colnr_T *)&fromcol, NULL, NULL);
3197 else
3198 fromcol = 0;
3199 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3200 {
3201 pos.lnum = lnum;
3202 pos.col = search_match_endcol;
3203 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3204 }
3205 else
3206 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003207 /* do at least one character; happens when past end of line */
3208 if (fromcol == tocol)
3209 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 area_highlighting = TRUE;
3211 attr = hl_attr(HLF_I);
3212 }
3213
3214#ifdef FEAT_DIFF
3215 filler_lines = diff_check(wp, lnum);
3216 if (filler_lines < 0)
3217 {
3218 if (filler_lines == -1)
3219 {
3220 if (diff_find_change(wp, lnum, &change_start, &change_end))
3221 diff_hlf = HLF_ADD; /* added line */
3222 else if (change_start == 0)
3223 diff_hlf = HLF_TXD; /* changed text */
3224 else
3225 diff_hlf = HLF_CHD; /* changed line */
3226 }
3227 else
3228 diff_hlf = HLF_ADD; /* added line */
3229 filler_lines = 0;
3230 area_highlighting = TRUE;
3231 }
3232 if (lnum == wp->w_topline)
3233 filler_lines = wp->w_topfill;
3234 filler_todo = filler_lines;
3235#endif
3236
3237#ifdef LINE_ATTR
3238# ifdef FEAT_SIGNS
3239 /* If this line has a sign with line highlighting set line_attr. */
3240 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3241 if (v != 0)
3242 line_attr = sign_get_attr((int)v, TRUE);
3243# endif
3244# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3245 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003246 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 line_attr = hl_attr(HLF_L);
3248# endif
3249 if (line_attr != 0)
3250 area_highlighting = TRUE;
3251#endif
3252
3253 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3254 ptr = line;
3255
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003256#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003257 if (has_spell)
3258 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003259 /* For checking first word with a capital skip white space. */
3260 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003261 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003262
Bram Moolenaar30abd282005-06-22 22:35:10 +00003263 /* To be able to spell-check over line boundaries copy the end of the
3264 * current line into nextline[]. Above the start of the next line was
3265 * copied to nextline[SPWORDLEN]. */
3266 if (nextline[SPWORDLEN] == NUL)
3267 {
3268 /* No next line or it is empty. */
3269 nextlinecol = MAXCOL;
3270 nextline_idx = 0;
3271 }
3272 else
3273 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003274 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003275 if (v < SPWORDLEN)
3276 {
3277 /* Short line, use it completely and append the start of the
3278 * next line. */
3279 nextlinecol = 0;
3280 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003281 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003282 nextline_idx = v + 1;
3283 }
3284 else
3285 {
3286 /* Long line, use only the last SPWORDLEN bytes. */
3287 nextlinecol = v - SPWORDLEN;
3288 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3289 nextline_idx = SPWORDLEN + 1;
3290 }
3291 }
3292 }
3293#endif
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 /* find start of trailing whitespace */
3296 if (wp->w_p_list && lcs_trail)
3297 {
3298 trailcol = (colnr_T)STRLEN(ptr);
3299 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3300 --trailcol;
3301 trailcol += (colnr_T) (ptr - line);
3302 extra_check = TRUE;
3303 }
3304
3305 /*
3306 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3307 * first character to be displayed.
3308 */
3309 if (wp->w_p_wrap)
3310 v = wp->w_skipcol;
3311 else
3312 v = wp->w_leftcol;
3313 if (v > 0)
3314 {
3315#ifdef FEAT_MBYTE
3316 char_u *prev_ptr = ptr;
3317#endif
3318 while (vcol < v && *ptr != NUL)
3319 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003320 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 vcol += c;
3322#ifdef FEAT_MBYTE
3323 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003325 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003328 /* When:
3329 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003330 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003331 * - 'virtualedit' is set, or
3332 * - the visual mode is active,
3333 * the end of the line may be before the start of the displayed part.
3334 */
3335 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003336#ifdef FEAT_SYN_HL
3337 wp->w_p_cuc || draw_color_col ||
3338#endif
3339#ifdef FEAT_VIRTUALEDIT
3340 virtual_active() ||
3341#endif
3342 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
3347 /* Handle a character that's not completely on the screen: Put ptr at
3348 * that character but skip the first few screen characters. */
3349 if (vcol > v)
3350 {
3351 vcol -= c;
3352#ifdef FEAT_MBYTE
3353 ptr = prev_ptr;
3354#else
3355 --ptr;
3356#endif
3357 n_skip = v - vcol;
3358 }
3359
3360 /*
3361 * Adjust for when the inverted text is before the screen,
3362 * and when the start of the inverted text is before the screen.
3363 */
3364 if (tocol <= vcol)
3365 fromcol = 0;
3366 else if (fromcol >= 0 && fromcol < vcol)
3367 fromcol = vcol;
3368
3369#ifdef FEAT_LINEBREAK
3370 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3371 if (wp->w_p_wrap)
3372 need_showbreak = TRUE;
3373#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003374#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003375 /* When spell checking a word we need to figure out the start of the
3376 * word and if it's badly spelled or not. */
3377 if (has_spell)
3378 {
3379 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003380 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003381 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003382
3383 pos = wp->w_cursor;
3384 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003385 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003386 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003387
3388 /* spell_move_to() may call ml_get() and make "line" invalid */
3389 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3390 ptr = line + linecol;
3391
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003392 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003393 {
3394 /* no bad word found at line start, don't check until end of a
3395 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003396 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003397 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003398 }
3399 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003400 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003401 /* bad word found, use attributes until end of word */
3402 word_end = wp->w_cursor.col + len + 1;
3403
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003404 /* Turn index into actual attributes. */
3405 if (spell_hlf != HLF_COUNT)
3406 spell_attr = highlight_attr[spell_hlf];
3407 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003408 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003409
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003410# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003411 /* Need to restart syntax highlighting for this line. */
3412 if (has_syntax)
3413 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003414# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003415 }
3416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418
3419 /*
3420 * Correct highlighting for cursor that can't be disabled.
3421 * Avoids having to check this for each character.
3422 */
3423 if (fromcol >= 0)
3424 {
3425 if (noinvcur)
3426 {
3427 if ((colnr_T)fromcol == wp->w_virtcol)
3428 {
3429 /* highlighting starts at cursor, let it start just after the
3430 * cursor */
3431 fromcol_prev = fromcol;
3432 fromcol = -1;
3433 }
3434 else if ((colnr_T)fromcol < wp->w_virtcol)
3435 /* restart highlighting after the cursor */
3436 fromcol_prev = wp->w_virtcol;
3437 }
3438 if (fromcol >= tocol)
3439 fromcol = -1;
3440 }
3441
3442#ifdef FEAT_SEARCH_EXTRA
3443 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003444 * Handle highlighting the last used search pattern and matches.
3445 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003447 cur = wp->w_match_head;
3448 shl_flag = FALSE;
3449 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003451 if (shl_flag == FALSE)
3452 {
3453 shl = &search_hl;
3454 shl_flag = TRUE;
3455 }
3456 else
3457 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003458 shl->startcol = MAXCOL;
3459 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003461 v = (long)(ptr - line);
3462 if (cur != NULL)
3463 cur->pos.cur = 0;
3464 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3465
3466 /* Need to get the line again, a multi-line regexp may have made it
3467 * invalid. */
3468 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3469 ptr = line + v;
3470
3471 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003473 if (shl->lnum == lnum)
3474 shl->startcol = shl->rm.startpos[0].col;
3475 else
3476 shl->startcol = 0;
3477 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3478 - shl->rm.startpos[0].lnum)
3479 shl->endcol = shl->rm.endpos[0].col;
3480 else
3481 shl->endcol = MAXCOL;
3482 /* Highlight one character for an empty match. */
3483 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003486 if (has_mbyte && line[shl->endcol] != NUL)
3487 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3488 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003490 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003492 if ((long)shl->startcol < v) /* match at leftcol */
3493 {
3494 shl->attr_cur = shl->attr;
3495 search_attr = shl->attr;
3496 }
3497 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003499 if (shl != &search_hl && cur != NULL)
3500 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502#endif
3503
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003504#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003505 /* Cursor line highlighting for 'cursorline' in the current window. Not
3506 * when Visual mode is active, because it's not clear what is selected
3507 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003508 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003509 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003510 {
3511 line_attr = hl_attr(HLF_CUL);
3512 area_highlighting = TRUE;
3513 }
3514#endif
3515
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003516 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 col = 0;
3518#ifdef FEAT_RIGHTLEFT
3519 if (wp->w_p_rl)
3520 {
3521 /* Rightleft window: process the text in the normal direction, but put
3522 * it in current_ScreenLine[] from right to left. Start at the
3523 * rightmost column of the window. */
3524 col = W_WIDTH(wp) - 1;
3525 off += col;
3526 }
3527#endif
3528
3529 /*
3530 * Repeat for the whole displayed line.
3531 */
3532 for (;;)
3533 {
3534 /* Skip this quickly when working on the text. */
3535 if (draw_state != WL_LINE)
3536 {
3537#ifdef FEAT_CMDWIN
3538 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3539 {
3540 draw_state = WL_CMDLINE;
3541 if (cmdwin_type != 0 && wp == curwin)
3542 {
3543 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003545 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 char_attr = hl_attr(HLF_AT);
3547 }
3548 }
3549#endif
3550
3551#ifdef FEAT_FOLDING
3552 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3553 {
3554 draw_state = WL_FOLD;
3555 if (wp->w_p_fdc > 0)
3556 {
3557 /* Draw the 'foldcolumn'. */
3558 fill_foldcolumn(extra, wp, FALSE, lnum);
3559 n_extra = wp->w_p_fdc;
3560 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003561 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 c_extra = NUL;
3563 char_attr = hl_attr(HLF_FC);
3564 }
3565 }
3566#endif
3567
3568#ifdef FEAT_SIGNS
3569 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3570 {
3571 draw_state = WL_SIGN;
3572 /* Show the sign column when there are any signs in this
3573 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003574 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003576 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003578 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579# endif
3580
3581 /* Draw two cells with the sign value or blank. */
3582 c_extra = ' ';
3583 char_attr = hl_attr(HLF_SC);
3584 n_extra = 2;
3585
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003586 if (row == startrow
3587#ifdef FEAT_DIFF
3588 + filler_lines && filler_todo <= 0
3589#endif
3590 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3593 SIGN_TEXT);
3594# ifdef FEAT_SIGN_ICONS
3595 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3596 SIGN_ICON);
3597 if (gui.in_use && icon_sign != 0)
3598 {
3599 /* Use the image in this position. */
3600 c_extra = SIGN_BYTE;
3601# ifdef FEAT_NETBEANS_INTG
3602 if (buf_signcount(wp->w_buffer, lnum) > 1)
3603 c_extra = MULTISIGN_BYTE;
3604# endif
3605 char_attr = icon_sign;
3606 }
3607 else
3608# endif
3609 if (text_sign != 0)
3610 {
3611 p_extra = sign_get_text(text_sign);
3612 if (p_extra != NULL)
3613 {
3614 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003615 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617 char_attr = sign_get_attr(text_sign, FALSE);
3618 }
3619 }
3620 }
3621 }
3622#endif
3623
3624 if (draw_state == WL_NR - 1 && n_extra == 0)
3625 {
3626 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003627 /* Display the absolute or relative line number. After the
3628 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3629 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 && (row == startrow
3631#ifdef FEAT_DIFF
3632 + filler_lines
3633#endif
3634 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3635 {
3636 /* Draw the line number (empty space after wrapping). */
3637 if (row == startrow
3638#ifdef FEAT_DIFF
3639 + filler_lines
3640#endif
3641 )
3642 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003643 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003644 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003645
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003646 if (wp->w_p_nu && !wp->w_p_rnu)
3647 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003648 num = (long)lnum;
3649 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003650 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003651 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003652 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003653 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003654 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003655 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003656 num = lnum;
3657 fmt = "%-*ld ";
3658 }
3659 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003660
Bram Moolenaar700e7342013-01-30 12:31:36 +01003661 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003662 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (wp->w_skipcol > 0)
3664 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3665 *p_extra = '-';
3666#ifdef FEAT_RIGHTLEFT
3667 if (wp->w_p_rl) /* reverse line numbers */
3668 rl_mirror(extra);
3669#endif
3670 p_extra = extra;
3671 c_extra = NUL;
3672 }
3673 else
3674 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003675 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003677#ifdef FEAT_SYN_HL
3678 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003679 * the current line differently.
3680 * TODO: Can we use CursorLine instead of CursorLineNr
3681 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003682 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003683 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003684 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 }
3688
Bram Moolenaar597a4222014-06-25 14:39:50 +02003689#ifdef FEAT_LINEBREAK
3690 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3691 && n_extra == 0 && *p_sbr != NUL)
3692 /* draw indent after showbreak value */
3693 draw_state = WL_BRI;
3694 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3695 /* After the showbreak, draw the breakindent */
3696 draw_state = WL_BRI - 1;
3697
3698 /* draw 'breakindent': indent wrapped text accordingly */
3699 if (draw_state == WL_BRI - 1 && n_extra == 0)
3700 {
3701 draw_state = WL_BRI;
3702# ifdef FEAT_DIFF
3703# endif
3704 if (wp->w_p_bri && n_extra == 0 && row != startrow
3705#ifdef FEAT_DIFF
3706 && filler_lines == 0
3707#endif
3708 )
3709 {
3710 char_attr = 0; /* was: hl_attr(HLF_AT); */
3711#ifdef FEAT_DIFF
3712 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003713 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003714 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003715 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3716 char_attr = hl_combine_attr(char_attr,
3717 hl_attr(HLF_CUL));
3718 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003719#endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003720 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003721 c_extra = ' ';
3722 n_extra = get_breakindent_win(wp,
3723 ml_get_buf(wp->w_buffer, lnum, FALSE));
3724 /* Correct end of highlighted area for 'breakindent',
3725 * required when 'linebreak' is also set. */
3726 if (tocol == vcol)
3727 tocol += n_extra;
3728 }
3729 }
3730#endif
3731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3733 if (draw_state == WL_SBR - 1 && n_extra == 0)
3734 {
3735 draw_state = WL_SBR;
3736# ifdef FEAT_DIFF
3737 if (filler_todo > 0)
3738 {
3739 /* Draw "deleted" diff line(s). */
3740 if (char2cells(fill_diff) > 1)
3741 c_extra = '-';
3742 else
3743 c_extra = fill_diff;
3744# ifdef FEAT_RIGHTLEFT
3745 if (wp->w_p_rl)
3746 n_extra = col + 1;
3747 else
3748# endif
3749 n_extra = W_WIDTH(wp) - col;
3750 char_attr = hl_attr(HLF_DED);
3751 }
3752# endif
3753# ifdef FEAT_LINEBREAK
3754 if (*p_sbr != NUL && need_showbreak)
3755 {
3756 /* Draw 'showbreak' at the start of each broken line. */
3757 p_extra = p_sbr;
3758 c_extra = NUL;
3759 n_extra = (int)STRLEN(p_sbr);
3760 char_attr = hl_attr(HLF_AT);
3761 need_showbreak = FALSE;
3762 /* Correct end of highlighted area for 'showbreak',
3763 * required when 'linebreak' is also set. */
3764 if (tocol == vcol)
3765 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003766#ifdef FEAT_SYN_HL
3767 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003768 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003769 char_attr = hl_combine_attr(char_attr,
3770 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773# endif
3774 }
3775#endif
3776
3777 if (draw_state == WL_LINE - 1 && n_extra == 0)
3778 {
3779 draw_state = WL_LINE;
3780 if (saved_n_extra)
3781 {
3782 /* Continue item from end of wrapped line. */
3783 n_extra = saved_n_extra;
3784 c_extra = saved_c_extra;
3785 p_extra = saved_p_extra;
3786 char_attr = saved_char_attr;
3787 }
3788 else
3789 char_attr = 0;
3790 }
3791 }
3792
3793 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003794 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003795 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#ifdef FEAT_DIFF
3797 && filler_todo <= 0
3798#endif
3799 )
3800 {
3801 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3802 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003803 /* Pretend we have finished updating the window. Except when
3804 * 'cursorcolumn' is set. */
3805#ifdef FEAT_SYN_HL
3806 if (wp->w_p_cuc)
3807 row = wp->w_cline_row + wp->w_cline_height;
3808 else
3809#endif
3810 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 break;
3812 }
3813
3814 if (draw_state == WL_LINE && area_highlighting)
3815 {
3816 /* handle Visual or match highlighting in this line */
3817 if (vcol == fromcol
3818#ifdef FEAT_MBYTE
3819 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3820 && (*mb_ptr2cells)(ptr) > 1)
3821#endif
3822 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003823 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 && vcol < tocol))
3825 area_attr = attr; /* start highlighting */
3826 else if (area_attr != 0
3827 && (vcol == tocol
3828 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831#ifdef FEAT_SEARCH_EXTRA
3832 if (!n_extra)
3833 {
3834 /*
3835 * Check for start/end of search pattern match.
3836 * After end, check for start/end of next match.
3837 * When another match, have to check for start again.
3838 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003839 * Do this for 'search_hl' and the match list (ordered by
3840 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003842 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003843 cur = wp->w_match_head;
3844 shl_flag = FALSE;
3845 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003847 if (shl_flag == FALSE
3848 && ((cur != NULL
3849 && cur->priority > SEARCH_HL_PRIORITY)
3850 || cur == NULL))
3851 {
3852 shl = &search_hl;
3853 shl_flag = TRUE;
3854 }
3855 else
3856 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003857 if (cur != NULL)
3858 cur->pos.cur = 0;
3859 pos_inprogress = TRUE;
3860 while (shl->rm.regprog != NULL
3861 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003863 if (shl->startcol != MAXCOL
3864 && v >= (long)shl->startcol
3865 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867 shl->attr_cur = shl->attr;
3868 }
Bram Moolenaar5307de02014-08-16 16:28:36 +02003869 else if (v >= (long)shl->endcol && shl->lnum == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
3871 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003872 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3873 pos_inprogress = cur == NULL || cur->pos.cur == 0
3874 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
3876 /* Need to get the line again, a multi-line regexp
3877 * may have made it invalid. */
3878 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3879 ptr = line + v;
3880
3881 if (shl->lnum == lnum)
3882 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003883 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003885 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003887 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003889 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
3891 /* highlight empty match, try again after
3892 * it */
3893#ifdef FEAT_MBYTE
3894 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003895 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003896 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 else
3898#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003899 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901
3902 /* Loop to check if the match starts at the
3903 * current position */
3904 continue;
3905 }
3906 }
3907 break;
3908 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003909 if (shl != &search_hl && cur != NULL)
3910 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003912
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003913 /* Use attributes from match with highest priority among
3914 * 'search_hl' and the match list. */
3915 search_attr = search_hl.attr_cur;
3916 cur = wp->w_match_head;
3917 shl_flag = FALSE;
3918 while (cur != NULL || shl_flag == FALSE)
3919 {
3920 if (shl_flag == FALSE
3921 && ((cur != NULL
3922 && cur->priority > SEARCH_HL_PRIORITY)
3923 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003924 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003925 shl = &search_hl;
3926 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003927 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003928 else
3929 shl = &cur->hl;
3930 if (shl->attr_cur != 0)
3931 search_attr = shl->attr_cur;
3932 if (shl != &search_hl && cur != NULL)
3933 cur = cur->next;
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936#endif
3937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003939 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003941 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3942 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003944 if (diff_hlf == HLF_TXD && ptr - line > change_end
3945 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003947 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003948 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3949 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003952
3953 /* Decide which of the highlight attributes to use. */
3954 attr_pri = TRUE;
3955 if (area_attr != 0)
3956 char_attr = area_attr;
3957 else if (search_attr != 0)
3958 char_attr = search_attr;
3959#ifdef LINE_ATTR
3960 /* Use line_attr when not in the Visual or 'incsearch' area
3961 * (area_attr may be 0 when "noinvcur" is set). */
3962 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003963 || vcol < fromcol || vcol_prev < fromcol_prev
3964 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003965 char_attr = line_attr;
3966#endif
3967 else
3968 {
3969 attr_pri = FALSE;
3970#ifdef FEAT_SYN_HL
3971 if (has_syntax)
3972 char_attr = syntax_attr;
3973 else
3974#endif
3975 char_attr = 0;
3976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978
3979 /*
3980 * Get the next character to put on the screen.
3981 */
3982 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003983 * The "p_extra" points to the extra stuff that is inserted to
3984 * represent special characters (non-printable stuff) and other
3985 * things. When all characters are the same, c_extra is used.
3986 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3987 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3989 */
3990 if (n_extra > 0)
3991 {
3992 if (c_extra != NUL)
3993 {
3994 c = c_extra;
3995#ifdef FEAT_MBYTE
3996 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3997 if (enc_utf8 && (*mb_char2len)(c) > 1)
3998 {
3999 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004000 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004001 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003 else
4004 mb_utf8 = FALSE;
4005#endif
4006 }
4007 else
4008 {
4009 c = *p_extra;
4010#ifdef FEAT_MBYTE
4011 if (has_mbyte)
4012 {
4013 mb_c = c;
4014 if (enc_utf8)
4015 {
4016 /* If the UTF-8 character is more than one byte:
4017 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004018 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 mb_utf8 = FALSE;
4020 if (mb_l > n_extra)
4021 mb_l = 1;
4022 else if (mb_l > 1)
4023 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004024 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004026 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028 }
4029 else
4030 {
4031 /* if this is a DBCS character, put it in "mb_c" */
4032 mb_l = MB_BYTE2LEN(c);
4033 if (mb_l >= n_extra)
4034 mb_l = 1;
4035 else if (mb_l > 1)
4036 mb_c = (c << 8) + p_extra[1];
4037 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004038 if (mb_l == 0) /* at the NUL at end-of-line */
4039 mb_l = 1;
4040
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 /* If a double-width char doesn't fit display a '>' in the
4042 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004043 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044# ifdef FEAT_RIGHTLEFT
4045 wp->w_p_rl ? (col <= 0) :
4046# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004047 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 && (*mb_char2cells)(mb_c) == 2)
4049 {
4050 c = '>';
4051 mb_c = c;
4052 mb_l = 1;
4053 mb_utf8 = FALSE;
4054 multi_attr = hl_attr(HLF_AT);
4055 /* put the pointer back to output the double-width
4056 * character at the start of the next line. */
4057 ++n_extra;
4058 --p_extra;
4059 }
4060 else
4061 {
4062 n_extra -= mb_l - 1;
4063 p_extra += mb_l - 1;
4064 }
4065 }
4066#endif
4067 ++p_extra;
4068 }
4069 --n_extra;
4070 }
4071 else
4072 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004073 if (p_extra_free != NULL)
4074 {
4075 vim_free(p_extra_free);
4076 p_extra_free = NULL;
4077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 /*
4079 * Get a character from the line itself.
4080 */
4081 c = *ptr;
4082#ifdef FEAT_MBYTE
4083 if (has_mbyte)
4084 {
4085 mb_c = c;
4086 if (enc_utf8)
4087 {
4088 /* If the UTF-8 character is more than one byte: Decode it
4089 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004090 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 mb_utf8 = FALSE;
4092 if (mb_l > 1)
4093 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004094 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 /* Overlong encoded ASCII or ASCII with composing char
4096 * is displayed normally, except a NUL. */
4097 if (mb_c < 0x80)
4098 c = mb_c;
4099 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004100
4101 /* At start of the line we can have a composing char.
4102 * Draw it as a space with a composing char. */
4103 if (utf_iscomposing(mb_c))
4104 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004105 int i;
4106
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004107 for (i = Screen_mco - 1; i > 0; --i)
4108 u8cc[i] = u8cc[i - 1];
4109 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004110 mb_c = ' ';
4111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 if ((mb_l == 1 && c >= 0x80)
4115 || (mb_l >= 1 && mb_c == 0)
4116 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004117# ifdef UNICODE16
4118 || mb_c >= 0x10000
4119# endif
4120 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
4122 /*
4123 * Illegal UTF-8 byte: display as <xx>.
4124 * Non-BMP character : display as ? or fullwidth ?.
4125 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004126# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004131# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (wp->w_p_rl) /* reverse */
4133 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004136# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else if (utf_char2cells(mb_c) != 2)
4138 STRCPY(extra, "?");
4139 else
4140 /* 0xff1f in UTF-8: full-width '?' */
4141 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 p_extra = extra;
4145 c = *p_extra;
4146 mb_c = mb_ptr2char_adv(&p_extra);
4147 mb_utf8 = (c >= 0x80);
4148 n_extra = (int)STRLEN(p_extra);
4149 c_extra = NUL;
4150 if (area_attr == 0 && search_attr == 0)
4151 {
4152 n_attr = n_extra + 1;
4153 extra_attr = hl_attr(HLF_8);
4154 saved_attr2 = char_attr; /* save current attr */
4155 }
4156 }
4157 else if (mb_l == 0) /* at the NUL at end-of-line */
4158 mb_l = 1;
4159#ifdef FEAT_ARABIC
4160 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4161 {
4162 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004163 int pc, pc1, nc;
4164 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /* The idea of what is the previous and next
4167 * character depends on 'rightleft'. */
4168 if (wp->w_p_rl)
4169 {
4170 pc = prev_c;
4171 pc1 = prev_c1;
4172 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004173 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175 else
4176 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004177 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004179 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 prev_c = mb_c;
4182
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004183 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 else
4186 prev_c = mb_c;
4187#endif
4188 }
4189 else /* enc_dbcs */
4190 {
4191 mb_l = MB_BYTE2LEN(c);
4192 if (mb_l == 0) /* at the NUL at end-of-line */
4193 mb_l = 1;
4194 else if (mb_l > 1)
4195 {
4196 /* We assume a second byte below 32 is illegal.
4197 * Hopefully this is OK for all double-byte encodings!
4198 */
4199 if (ptr[1] >= 32)
4200 mb_c = (c << 8) + ptr[1];
4201 else
4202 {
4203 if (ptr[1] == NUL)
4204 {
4205 /* head byte at end of line */
4206 mb_l = 1;
4207 transchar_nonprint(extra, c);
4208 }
4209 else
4210 {
4211 /* illegal tail byte */
4212 mb_l = 2;
4213 STRCPY(extra, "XX");
4214 }
4215 p_extra = extra;
4216 n_extra = (int)STRLEN(extra) - 1;
4217 c_extra = NUL;
4218 c = *p_extra++;
4219 if (area_attr == 0 && search_attr == 0)
4220 {
4221 n_attr = n_extra + 1;
4222 extra_attr = hl_attr(HLF_8);
4223 saved_attr2 = char_attr; /* save current attr */
4224 }
4225 mb_c = c;
4226 }
4227 }
4228 }
4229 /* If a double-width char doesn't fit display a '>' in the
4230 * last column; the character is displayed at the start of the
4231 * next line. */
4232 if ((
4233# ifdef FEAT_RIGHTLEFT
4234 wp->w_p_rl ? (col <= 0) :
4235# endif
4236 (col >= W_WIDTH(wp) - 1))
4237 && (*mb_char2cells)(mb_c) == 2)
4238 {
4239 c = '>';
4240 mb_c = c;
4241 mb_utf8 = FALSE;
4242 mb_l = 1;
4243 multi_attr = hl_attr(HLF_AT);
4244 /* Put pointer back so that the character will be
4245 * displayed at the start of the next line. */
4246 --ptr;
4247 }
4248 else if (*ptr != NUL)
4249 ptr += mb_l - 1;
4250
4251 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004252 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004253 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004254 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004257 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 c = ' ';
4259 if (area_attr == 0 && search_attr == 0)
4260 {
4261 n_attr = n_extra + 1;
4262 extra_attr = hl_attr(HLF_AT);
4263 saved_attr2 = char_attr; /* save current attr */
4264 }
4265 mb_c = c;
4266 mb_utf8 = FALSE;
4267 mb_l = 1;
4268 }
4269
4270 }
4271#endif
4272 ++ptr;
4273
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004274 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004275 if (wp->w_p_list && (c == 160
4276#ifdef FEAT_MBYTE
4277 || (mb_utf8 && mb_c == 160)
4278#endif
4279 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004280 {
4281 c = lcs_nbsp;
4282 if (area_attr == 0 && search_attr == 0)
4283 {
4284 n_attr = 1;
4285 extra_attr = hl_attr(HLF_8);
4286 saved_attr2 = char_attr; /* save current attr */
4287 }
4288#ifdef FEAT_MBYTE
4289 mb_c = c;
4290 if (enc_utf8 && (*mb_char2len)(c) > 1)
4291 {
4292 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004293 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004294 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004295 }
4296 else
4297 mb_utf8 = FALSE;
4298#endif
4299 }
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 if (extra_check)
4302 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004303#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004304 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004305#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004306
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004307#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 /* Get syntax attribute, unless still at the start of the line
4309 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004310 v = (long)(ptr - line);
4311 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 /* Get the syntax attribute for the character. If there
4314 * is an error, disable syntax highlighting. */
4315 save_did_emsg = did_emsg;
4316 did_emsg = FALSE;
4317
Bram Moolenaar217ad922005-03-20 22:37:15 +00004318 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004319# ifdef FEAT_SPELL
4320 has_spell ? &can_spell :
4321# endif
4322 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004325 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004326 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004327 has_syntax = FALSE;
4328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 else
4330 did_emsg = save_did_emsg;
4331
4332 /* Need to get the line again, a multi-line regexp may
4333 * have made it invalid. */
4334 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4335 ptr = line + v;
4336
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004337 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004339 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004340 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004341# ifdef FEAT_CONCEAL
4342 /* no concealing past the end of the line, it interferes
4343 * with line highlighting */
4344 if (c == NUL)
4345 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004346 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004347 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004350#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004351
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004352#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004353 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004354 * Only do this when there is no syntax highlighting, the
4355 * @Spell cluster is not used or the current syntax item
4356 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004357 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004358 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004359 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004360# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004361 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004362 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004363# endif
4364 if (c != 0 && (
4365# ifdef FEAT_SYN_HL
4366 !has_syntax ||
4367# endif
4368 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004369 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004370 char_u *prev_ptr, *p;
4371 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004372 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004373# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004374 if (has_mbyte)
4375 {
4376 prev_ptr = ptr - mb_l;
4377 v -= mb_l - 1;
4378 }
4379 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004380# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004381 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004382
4383 /* Use nextline[] if possible, it has the start of the
4384 * next line concatenated. */
4385 if ((prev_ptr - line) - nextlinecol >= 0)
4386 p = nextline + (prev_ptr - line) - nextlinecol;
4387 else
4388 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004389 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004390 len = spell_check(wp, p, &spell_hlf, &cap_col,
4391 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004392 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004393
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004394 /* In Insert mode only highlight a word that
4395 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004396 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004397 && (State & INSERT) != 0
4398 && wp->w_cursor.lnum == lnum
4399 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004400 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004401 && wp->w_cursor.col < (colnr_T)word_end)
4402 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004403 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004404 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004405 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004406
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004407 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004408 && (p - nextline) + len > nextline_idx)
4409 {
4410 /* Remember that the good word continues at the
4411 * start of the next line. */
4412 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004413 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004414 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004415
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004416 /* Turn index into actual attributes. */
4417 if (spell_hlf != HLF_COUNT)
4418 spell_attr = highlight_attr[spell_hlf];
4419
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004420 if (cap_col > 0)
4421 {
4422 if (p != prev_ptr
4423 && (p - nextline) + cap_col >= nextline_idx)
4424 {
4425 /* Remember that the word in the next line
4426 * must start with a capital. */
4427 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004428 cap_col = (int)((p - nextline) + cap_col
4429 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004430 }
4431 else
4432 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004433 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004434 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004435 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004436 }
4437 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004438 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004439 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004440 char_attr = hl_combine_attr(char_attr, spell_attr);
4441 else
4442 char_attr = hl_combine_attr(spell_attr, char_attr);
4443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#endif
4445#ifdef FEAT_LINEBREAK
4446 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004447 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004449 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004451 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452# ifdef FEAT_MBYTE
4453 has_mbyte ? mb_l :
4454# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004455 1);
4456 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004457 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004458 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004459 if (c == TAB && n_extra + col > W_WIDTH(wp))
4460 n_extra = (int)wp->w_buffer->b_p_ts
4461 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4462
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 c_extra = ' ';
4464 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004465 {
4466#ifdef FEAT_CONCEAL
4467 if (c == TAB)
4468 /* See "Tab alignment" below. */
4469 FIX_FOR_BOGUSCOLS;
4470#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004471 if (!wp->w_p_list)
4472 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475#endif
4476
4477 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4478 {
4479 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004480 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
4482 n_attr = 1;
4483 extra_attr = hl_attr(HLF_8);
4484 saved_attr2 = char_attr; /* save current attr */
4485 }
4486#ifdef FEAT_MBYTE
4487 mb_c = c;
4488 if (enc_utf8 && (*mb_char2len)(c) > 1)
4489 {
4490 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004491 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004492 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 else
4495 mb_utf8 = FALSE;
4496#endif
4497 }
4498 }
4499
4500 /*
4501 * Handling of non-printable characters.
4502 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004503 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
4505 /*
4506 * when getting a character from the file, we may have to
4507 * turn it into something else on the way to putting it
4508 * into "ScreenLines".
4509 */
4510 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4511 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004512 int tab_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004514 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004515 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004516#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004517 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004518#endif
4519 /* tab amount depends on current column */
4520 n_extra = tab_len;
4521#ifdef FEAT_LINEBREAK
4522 else
4523 {
4524 char_u *p;
4525 int len = n_extra;
4526 int i;
4527 int saved_nextra = n_extra;
4528
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004529#ifdef FEAT_CONCEAL
4530 if (is_concealing && vcol_off > 0)
4531 /* there are characters to conceal */
4532 tab_len += vcol_off;
4533#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004534 /* if n_extra > 0, it gives the number of chars, to
4535 * use for a tab, else we need to calculate the width
4536 * for a tab */
4537#ifdef FEAT_MBYTE
4538 len = (tab_len * mb_char2len(lcs_tab2));
4539 if (n_extra > 0)
4540 len += n_extra - tab_len;
4541#endif
4542 c = lcs_tab1;
4543 p = alloc((unsigned)(len + 1));
4544 vim_memset(p, ' ', len);
4545 p[len] = NUL;
4546 p_extra_free = p;
4547 for (i = 0; i < tab_len; i++)
4548 {
4549#ifdef FEAT_MBYTE
4550 mb_char2bytes(lcs_tab2, p);
4551 p += mb_char2len(lcs_tab2);
4552 n_extra += mb_char2len(lcs_tab2)
4553 - (saved_nextra > 0 ? 1 : 0);
4554#else
4555 p[i] = lcs_tab2;
4556#endif
4557 }
4558 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004559#ifdef FEAT_CONCEAL
4560 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4561 * macro below, so need to adjust for that here */
4562 if (is_concealing && vcol_off > 0)
4563 n_extra -= vcol_off;
4564#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004565 }
4566#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004567#ifdef FEAT_CONCEAL
4568 /* Tab alignment should be identical regardless of
4569 * 'conceallevel' value. So tab compensates of all
4570 * previous concealed characters, and thus resets vcol_off
4571 * and boguscols accumulated so far in the line. Note that
4572 * the tab can be longer than 'tabstop' when there
4573 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004574 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#ifdef FEAT_MBYTE
4577 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4578#endif
4579 if (wp->w_p_list)
4580 {
4581 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004582#ifdef FEAT_LINEBREAK
4583 if (wp->w_p_lbr)
4584 c_extra = NUL; /* using p_extra from above */
4585 else
4586#endif
4587 c_extra = lcs_tab2;
4588 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 extra_attr = hl_attr(HLF_8);
4590 saved_attr2 = char_attr; /* save current attr */
4591#ifdef FEAT_MBYTE
4592 mb_c = c;
4593 if (enc_utf8 && (*mb_char2len)(c) > 1)
4594 {
4595 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004596 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004597 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599#endif
4600 }
4601 else
4602 {
4603 c_extra = ' ';
4604 c = ' ';
4605 }
4606 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004607 else if (c == NUL
4608 && ((wp->w_p_list && lcs_eol > 0)
4609 || ((fromcol >= 0 || fromcol_prev >= 0)
4610 && tocol > vcol
4611 && VIsual_mode != Ctrl_V
4612 && (
4613# ifdef FEAT_RIGHTLEFT
4614 wp->w_p_rl ? (col >= 0) :
4615# endif
4616 (col < W_WIDTH(wp)))
4617 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004618 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004619 && (colnr_T)vcol == wp->w_virtcol)))
4620 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004622 /* Display a '$' after the line or highlight an extra
4623 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4625 /* For a diff line the highlighting continues after the
4626 * "$". */
4627 if (
4628# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004629 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630# ifdef LINE_ATTR
4631 &&
4632# endif
4633# endif
4634# ifdef LINE_ATTR
4635 line_attr == 0
4636# endif
4637 )
4638#endif
4639 {
4640#ifdef FEAT_VIRTUALEDIT
4641 /* In virtualedit, visual selections may extend
4642 * beyond end of line. */
4643 if (area_highlighting && virtual_active()
4644 && tocol != MAXCOL && vcol < tocol)
4645 n_extra = 0;
4646 else
4647#endif
4648 {
4649 p_extra = at_end_str;
4650 n_extra = 1;
4651 c_extra = NUL;
4652 }
4653 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004654 if (wp->w_p_list)
4655 c = lcs_eol;
4656 else
4657 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 lcs_eol_one = -1;
4659 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004660 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
4662 extra_attr = hl_attr(HLF_AT);
4663 n_attr = 1;
4664 }
4665#ifdef FEAT_MBYTE
4666 mb_c = c;
4667 if (enc_utf8 && (*mb_char2len)(c) > 1)
4668 {
4669 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004670 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004671 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 else
4674 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4675#endif
4676 }
4677 else if (c != NUL)
4678 {
4679 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004680 if (n_extra == 0)
4681 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682#ifdef FEAT_RIGHTLEFT
4683 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4684 rl_mirror(p_extra); /* reverse "<12>" */
4685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004687#ifdef FEAT_LINEBREAK
4688 if (wp->w_p_lbr)
4689 {
4690 char_u *p;
4691
4692 c = *p_extra;
4693 p = alloc((unsigned)n_extra + 1);
4694 vim_memset(p, ' ', n_extra);
4695 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4696 p[n_extra] = NUL;
4697 p_extra_free = p_extra = p;
4698 }
4699 else
4700#endif
4701 {
4702 n_extra = byte2cells(c) - 1;
4703 c = *p_extra++;
4704 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004705 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
4707 n_attr = n_extra + 1;
4708 extra_attr = hl_attr(HLF_8);
4709 saved_attr2 = char_attr; /* save current attr */
4710 }
4711#ifdef FEAT_MBYTE
4712 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4713#endif
4714 }
4715#ifdef FEAT_VIRTUALEDIT
4716 else if (VIsual_active
4717 && (VIsual_mode == Ctrl_V
4718 || VIsual_mode == 'v')
4719 && virtual_active()
4720 && tocol != MAXCOL
4721 && vcol < tocol
4722 && (
4723# ifdef FEAT_RIGHTLEFT
4724 wp->w_p_rl ? (col >= 0) :
4725# endif
4726 (col < W_WIDTH(wp))))
4727 {
4728 c = ' ';
4729 --ptr; /* put it back at the NUL */
4730 }
4731#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004732#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 else if ((
4734# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004735 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 ) && (
4739# ifdef FEAT_RIGHTLEFT
4740 wp->w_p_rl ? (col >= 0) :
4741# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004742 (col
4743# ifdef FEAT_CONCEAL
4744 - boguscols
4745# endif
4746 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 /* Highlight until the right side of the window */
4749 c = ' ';
4750 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004751
4752 /* Remember we do the char for line highlighting. */
4753 ++did_line_attr;
4754
4755 /* don't do search HL for the rest of the line */
4756 if (line_attr != 0 && char_attr == search_attr && col > 0)
4757 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758# ifdef FEAT_DIFF
4759 if (diff_hlf == HLF_TXD)
4760 {
4761 diff_hlf = HLF_CHD;
4762 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004765 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4766 char_attr = hl_combine_attr(char_attr,
4767 hl_attr(HLF_CUL));
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770# endif
4771 }
4772#endif
4773 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004774
4775#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004776 if ( wp->w_p_cole > 0
4777 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4778 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004779 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004780 && !(lnum_in_visual_area
4781 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004782 {
4783 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004784 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004785 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4786 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004787 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004788 /* First time at this concealed item: display one
4789 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004790 if (syn_get_sub_char() != NUL)
4791 c = syn_get_sub_char();
4792 else if (lcs_conceal != NUL)
4793 c = lcs_conceal;
4794 else
4795 c = ' ';
4796
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004797 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004798
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004799 if (n_extra > 0)
4800 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004801 vcol += n_extra;
4802 if (wp->w_p_wrap && n_extra > 0)
4803 {
4804# ifdef FEAT_RIGHTLEFT
4805 if (wp->w_p_rl)
4806 {
4807 col -= n_extra;
4808 boguscols -= n_extra;
4809 }
4810 else
4811# endif
4812 {
4813 boguscols += n_extra;
4814 col += n_extra;
4815 }
4816 }
4817 n_extra = 0;
4818 n_attr = 0;
4819 }
4820 else if (n_skip == 0)
4821 {
4822 is_concealing = TRUE;
4823 n_skip = 1;
4824 }
4825# ifdef FEAT_MBYTE
4826 mb_c = c;
4827 if (enc_utf8 && (*mb_char2len)(c) > 1)
4828 {
4829 mb_utf8 = TRUE;
4830 u8cc[0] = 0;
4831 c = 0xc0;
4832 }
4833 else
4834 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4835# endif
4836 }
4837 else
4838 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004839 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004840 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004841 }
4842#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004845#ifdef FEAT_CONCEAL
4846 /* In the cursor line and we may be concealing characters: correct
4847 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004848 if (!did_wcol && draw_state == WL_LINE
4849 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004850 && conceal_cursor_line(wp)
4851 && (int)wp->w_virtcol <= vcol + n_skip)
4852 {
4853 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004854 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004855 did_wcol = TRUE;
4856 }
4857#endif
4858
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 /* Don't override visual selection highlighting. */
4860 if (n_attr > 0
4861 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004862 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 char_attr = extra_attr;
4864
Bram Moolenaar81695252004-12-29 20:58:21 +00004865#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 /* XIM don't send preedit_start and preedit_end, but they send
4867 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4868 * im_is_preediting() here. */
4869 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004870 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 && (State & INSERT)
4872 && !p_imdisable
4873 && im_is_preediting()
4874 && draw_state == WL_LINE)
4875 {
4876 colnr_T tcol;
4877
4878 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004879 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 else
4881 tcol = preedit_end_col;
4882 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4883 {
4884 if (feedback_old_attr < 0)
4885 {
4886 feedback_col = 0;
4887 feedback_old_attr = char_attr;
4888 }
4889 char_attr = im_get_feedback_attr(feedback_col);
4890 if (char_attr < 0)
4891 char_attr = feedback_old_attr;
4892 feedback_col++;
4893 }
4894 else if (feedback_old_attr >= 0)
4895 {
4896 char_attr = feedback_old_attr;
4897 feedback_old_attr = -1;
4898 feedback_col = 0;
4899 }
4900 }
4901#endif
4902 /*
4903 * Handle the case where we are in column 0 but not on the first
4904 * character of the line and the user wants us to show us a
4905 * special character (via 'listchars' option "precedes:<char>".
4906 */
4907 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02004908 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4910#ifdef FEAT_DIFF
4911 && filler_todo <= 0
4912#endif
4913 && draw_state > WL_NR
4914 && c != NUL)
4915 {
4916 c = lcs_prec;
4917 lcs_prec_todo = NUL;
4918#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004919 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4920 {
4921 /* Double-width character being overwritten by the "precedes"
4922 * character, need to fill up half the character. */
4923 c_extra = MB_FILLER_CHAR;
4924 n_extra = 1;
4925 n_attr = 2;
4926 extra_attr = hl_attr(HLF_AT);
4927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 mb_c = c;
4929 if (enc_utf8 && (*mb_char2len)(c) > 1)
4930 {
4931 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004932 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004933 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935 else
4936 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4937#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004938 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
4940 saved_attr3 = char_attr; /* save current attr */
4941 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4942 n_attr3 = 1;
4943 }
4944 }
4945
4946 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004947 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004949 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004950#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004951 || did_line_attr == 1
4952#endif
4953 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004955#ifdef FEAT_SEARCH_EXTRA
4956 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004957
4958 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004959 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004960 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004961#endif
4962
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004963 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 * highlight match at end of line. If it's beyond the last
4965 * char on the screen, just overwrite that one (tricky!) Not
4966 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004967#ifdef FEAT_SEARCH_EXTRA
4968 prevcol_hl_flag = FALSE;
4969 if (prevcol == (long)search_hl.startcol)
4970 prevcol_hl_flag = TRUE;
4971 else
4972 {
4973 cur = wp->w_match_head;
4974 while (cur != NULL)
4975 {
4976 if (prevcol == (long)cur->hl.startcol)
4977 {
4978 prevcol_hl_flag = TRUE;
4979 break;
4980 }
4981 cur = cur->next;
4982 }
4983 }
4984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004986 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004987 && (VIsual_mode != Ctrl_V
4988 || lnum == VIsual.lnum
4989 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004990 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991#ifdef FEAT_SEARCH_EXTRA
4992 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004993 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004994# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004995 && did_line_attr <= 1
4996# endif
4997 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998#endif
4999 ))
5000 {
5001 int n = 0;
5002
5003#ifdef FEAT_RIGHTLEFT
5004 if (wp->w_p_rl)
5005 {
5006 if (col < 0)
5007 n = 1;
5008 }
5009 else
5010#endif
5011 {
5012 if (col >= W_WIDTH(wp))
5013 n = -1;
5014 }
5015 if (n != 0)
5016 {
5017 /* At the window boundary, highlight the last character
5018 * instead (better than nothing). */
5019 off += n;
5020 col += n;
5021 }
5022 else
5023 {
5024 /* Add a blank character to highlight. */
5025 ScreenLines[off] = ' ';
5026#ifdef FEAT_MBYTE
5027 if (enc_utf8)
5028 ScreenLinesUC[off] = 0;
5029#endif
5030 }
5031#ifdef FEAT_SEARCH_EXTRA
5032 if (area_attr == 0)
5033 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005034 /* Use attributes from match with highest priority among
5035 * 'search_hl' and the match list. */
5036 char_attr = search_hl.attr;
5037 cur = wp->w_match_head;
5038 shl_flag = FALSE;
5039 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005040 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005041 if (shl_flag == FALSE
5042 && ((cur != NULL
5043 && cur->priority > SEARCH_HL_PRIORITY)
5044 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005045 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005046 shl = &search_hl;
5047 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005048 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005049 else
5050 shl = &cur->hl;
5051 if ((ptr - line) - 1 == (long)shl->startcol)
5052 char_attr = shl->attr;
5053 if (shl != &search_hl && cur != NULL)
5054 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057#endif
5058 ScreenAttrs[off] = char_attr;
5059#ifdef FEAT_RIGHTLEFT
5060 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005063 --off;
5064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 else
5066#endif
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 Moolenaar600dddc2006-03-12 22:05:10 +00005071 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005072#ifdef FEAT_SYN_HL
5073 eol_hl_off = 1;
5074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
Bram Moolenaar91170f82006-05-05 21:15:17 +00005078 /*
5079 * At end of the text line.
5080 */
5081 if (c == NUL)
5082 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005083#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005084 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5085 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005086 {
5087 /* highlight last char after line */
5088 --col;
5089 --off;
5090 --vcol;
5091 }
5092
Bram Moolenaar1a384422010-07-14 19:53:30 +02005093 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005094 if (wp->w_p_wrap)
5095 v = wp->w_skipcol;
5096 else
5097 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005098
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005099 /* check if line ends before left margin */
5100 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005101 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005102#ifdef FEAT_CONCEAL
5103 /* Get rid of the boguscols now, we want to draw until the right
5104 * edge for 'cursorcolumn'. */
5105 col -= boguscols;
5106 boguscols = 0;
5107#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005108
5109 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005110 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005111
5112 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005113 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5114 && (int)wp->w_virtcol <
5115 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005116 && lnum != wp->w_cursor.lnum)
5117 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005118# ifdef FEAT_RIGHTLEFT
5119 && !wp->w_p_rl
5120# endif
5121 )
5122 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005123 int rightmost_vcol = 0;
5124 int i;
5125
5126 if (wp->w_p_cuc)
5127 rightmost_vcol = wp->w_virtcol;
5128 if (draw_color_col)
5129 /* determine rightmost colorcolumn to possibly draw */
5130 for (i = 0; color_cols[i] >= 0; ++i)
5131 if (rightmost_vcol < color_cols[i])
5132 rightmost_vcol = color_cols[i];
5133
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005134 while (col < W_WIDTH(wp))
5135 {
5136 ScreenLines[off] = ' ';
5137#ifdef FEAT_MBYTE
5138 if (enc_utf8)
5139 ScreenLinesUC[off] = 0;
5140#endif
5141 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005142 if (draw_color_col)
5143 draw_color_col = advance_color_col(VCOL_HLC,
5144 &color_cols);
5145
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005146 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005147 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005148 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005149 ScreenAttrs[off++] = hl_attr(HLF_MC);
5150 else
5151 ScreenAttrs[off++] = 0;
5152
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005153 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005154 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005155
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005156 ++vcol;
5157 }
5158 }
5159#endif
5160
Bram Moolenaar860cae12010-06-05 23:22:07 +02005161 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5162 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 row++;
5164
5165 /*
5166 * Update w_cline_height and w_cline_folded if the cursor line was
5167 * updated (saves a call to plines() later).
5168 */
5169 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5170 {
5171 curwin->w_cline_row = startrow;
5172 curwin->w_cline_height = row - startrow;
5173#ifdef FEAT_FOLDING
5174 curwin->w_cline_folded = FALSE;
5175#endif
5176 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5177 }
5178
5179 break;
5180 }
5181
5182 /* line continues beyond line end */
5183 if (lcs_ext
5184 && !wp->w_p_wrap
5185#ifdef FEAT_DIFF
5186 && filler_todo <= 0
5187#endif
5188 && (
5189#ifdef FEAT_RIGHTLEFT
5190 wp->w_p_rl ? col == 0 :
5191#endif
5192 col == W_WIDTH(wp) - 1)
5193 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005194 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5196 {
5197 c = lcs_ext;
5198 char_attr = hl_attr(HLF_AT);
5199#ifdef FEAT_MBYTE
5200 mb_c = c;
5201 if (enc_utf8 && (*mb_char2len)(c) > 1)
5202 {
5203 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005204 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005205 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
5207 else
5208 mb_utf8 = FALSE;
5209#endif
5210 }
5211
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005212#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005213 /* advance to the next 'colorcolumn' */
5214 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005215 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005216
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005217 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005218 * highlight the cursor position itself.
5219 * Also highlight the 'colorcolumn' if it is different than
5220 * 'cursorcolumn' */
5221 vcol_save_attr = -1;
5222 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005223 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005224 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005225 && lnum != wp->w_cursor.lnum)
5226 {
5227 vcol_save_attr = char_attr;
5228 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5229 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005230 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005231 {
5232 vcol_save_attr = char_attr;
5233 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5234 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005235 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005236#endif
5237
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 /*
5239 * Store character to be displayed.
5240 * Skip characters that are left of the screen for 'nowrap'.
5241 */
5242 vcol_prev = vcol;
5243 if (draw_state < WL_LINE || n_skip <= 0)
5244 {
5245 /*
5246 * Store the character.
5247 */
5248#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5249 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5250 {
5251 /* A double-wide character is: put first halve in left cell. */
5252 --off;
5253 --col;
5254 }
5255#endif
5256 ScreenLines[off] = c;
5257#ifdef FEAT_MBYTE
5258 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005259 {
5260 if ((mb_c & 0xff00) == 0x8e00)
5261 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 else if (enc_utf8)
5265 {
5266 if (mb_utf8)
5267 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005268 int i;
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005271 if ((c & 0xff) == 0)
5272 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005273 for (i = 0; i < Screen_mco; ++i)
5274 {
5275 ScreenLinesC[i][off] = u8cc[i];
5276 if (u8cc[i] == 0)
5277 break;
5278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280 else
5281 ScreenLinesUC[off] = 0;
5282 }
5283 if (multi_attr)
5284 {
5285 ScreenAttrs[off] = multi_attr;
5286 multi_attr = 0;
5287 }
5288 else
5289#endif
5290 ScreenAttrs[off] = char_attr;
5291
5292#ifdef FEAT_MBYTE
5293 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5294 {
5295 /* Need to fill two screen columns. */
5296 ++off;
5297 ++col;
5298 if (enc_utf8)
5299 /* UTF-8: Put a 0 in the second screen char. */
5300 ScreenLines[off] = 0;
5301 else
5302 /* DBCS: Put second byte in the second screen char. */
5303 ScreenLines[off] = mb_c & 0xff;
5304 ++vcol;
5305 /* When "tocol" is halfway a character, set it to the end of
5306 * the character, otherwise highlighting won't stop. */
5307 if (tocol == vcol)
5308 ++tocol;
5309#ifdef FEAT_RIGHTLEFT
5310 if (wp->w_p_rl)
5311 {
5312 /* now it's time to backup one cell */
5313 --off;
5314 --col;
5315 }
5316#endif
5317 }
5318#endif
5319#ifdef FEAT_RIGHTLEFT
5320 if (wp->w_p_rl)
5321 {
5322 --off;
5323 --col;
5324 }
5325 else
5326#endif
5327 {
5328 ++off;
5329 ++col;
5330 }
5331 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005332#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005333 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005334 {
5335 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005336 ++vcol_off;
5337 if (n_extra > 0)
5338 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005339 if (wp->w_p_wrap)
5340 {
5341 /*
5342 * Special voodoo required if 'wrap' is on.
5343 *
5344 * Advance the column indicator to force the line
5345 * drawing to wrap early. This will make the line
5346 * take up the same screen space when parts are concealed,
5347 * so that cursor line computations aren't messed up.
5348 *
5349 * To avoid the fictitious advance of 'col' causing
5350 * trailing junk to be written out of the screen line
5351 * we are building, 'boguscols' keeps track of the number
5352 * of bad columns we have advanced.
5353 */
5354 if (n_extra > 0)
5355 {
5356 vcol += n_extra;
5357# ifdef FEAT_RIGHTLEFT
5358 if (wp->w_p_rl)
5359 {
5360 col -= n_extra;
5361 boguscols -= n_extra;
5362 }
5363 else
5364# endif
5365 {
5366 col += n_extra;
5367 boguscols += n_extra;
5368 }
5369 n_extra = 0;
5370 n_attr = 0;
5371 }
5372
5373
5374# ifdef FEAT_MBYTE
5375 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5376 {
5377 /* Need to fill two screen columns. */
5378# ifdef FEAT_RIGHTLEFT
5379 if (wp->w_p_rl)
5380 {
5381 --boguscols;
5382 --col;
5383 }
5384 else
5385# endif
5386 {
5387 ++boguscols;
5388 ++col;
5389 }
5390 }
5391# endif
5392
5393# ifdef FEAT_RIGHTLEFT
5394 if (wp->w_p_rl)
5395 {
5396 --boguscols;
5397 --col;
5398 }
5399 else
5400# endif
5401 {
5402 ++boguscols;
5403 ++col;
5404 }
5405 }
5406 else
5407 {
5408 if (n_extra > 0)
5409 {
5410 vcol += n_extra;
5411 n_extra = 0;
5412 n_attr = 0;
5413 }
5414 }
5415
5416 }
5417#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 else
5419 --n_skip;
5420
Bram Moolenaar64486672010-05-16 15:46:46 +02005421 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5422 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005423 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424#ifdef FEAT_DIFF
5425 && filler_todo <= 0
5426#endif
5427 )
5428 ++vcol;
5429
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005430#ifdef FEAT_SYN_HL
5431 if (vcol_save_attr >= 0)
5432 char_attr = vcol_save_attr;
5433#endif
5434
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 /* restore attributes after "predeces" in 'listchars' */
5436 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5437 char_attr = saved_attr3;
5438
5439 /* restore attributes after last 'listchars' or 'number' char */
5440 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5441 char_attr = saved_attr2;
5442
5443 /*
5444 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005445 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 */
5447 if ((
5448#ifdef FEAT_RIGHTLEFT
5449 wp->w_p_rl ? (col < 0) :
5450#endif
5451 (col >= W_WIDTH(wp)))
5452 && (*ptr != NUL
5453#ifdef FEAT_DIFF
5454 || filler_todo > 0
5455#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005456 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5458 )
5459 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005460#ifdef FEAT_CONCEAL
5461 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5462 (int)W_WIDTH(wp), wp->w_p_rl);
5463 boguscols = 0;
5464#else
5465 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5466 (int)W_WIDTH(wp), wp->w_p_rl);
5467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 ++row;
5469 ++screen_row;
5470
5471 /* When not wrapping and finished diff lines, or when displayed
5472 * '$' and highlighting until last column, break here. */
5473 if ((!wp->w_p_wrap
5474#ifdef FEAT_DIFF
5475 && filler_todo <= 0
5476#endif
5477 ) || lcs_eol_one == -1)
5478 break;
5479
5480 /* When the window is too narrow draw all "@" lines. */
5481 if (draw_state != WL_LINE
5482#ifdef FEAT_DIFF
5483 && filler_todo <= 0
5484#endif
5485 )
5486 {
5487 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5488#ifdef FEAT_VERTSPLIT
5489 draw_vsep_win(wp, row);
5490#endif
5491 row = endrow;
5492 }
5493
5494 /* When line got too long for screen break here. */
5495 if (row == endrow)
5496 {
5497 ++row;
5498 break;
5499 }
5500
5501 if (screen_cur_row == screen_row - 1
5502#ifdef FEAT_DIFF
5503 && filler_todo <= 0
5504#endif
5505 && W_WIDTH(wp) == Columns)
5506 {
5507 /* Remember that the line wraps, used for modeless copy. */
5508 LineWraps[screen_row - 1] = TRUE;
5509
5510 /*
5511 * Special trick to make copy/paste of wrapped lines work with
5512 * xterm/screen: write an extra character beyond the end of
5513 * the line. This will work with all terminal types
5514 * (regardless of the xn,am settings).
5515 * Only do this on a fast tty.
5516 * Only do this if the cursor is on the current line
5517 * (something has been written in it).
5518 * Don't do this for the GUI.
5519 * Don't do this for double-width characters.
5520 * Don't do this for a window not at the right screen border.
5521 */
5522 if (p_tf
5523#ifdef FEAT_GUI
5524 && !gui.in_use
5525#endif
5526#ifdef FEAT_MBYTE
5527 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005528 && ((*mb_off2cells)(LineOffset[screen_row],
5529 LineOffset[screen_row] + screen_Columns)
5530 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005532 + (int)Columns - 2,
5533 LineOffset[screen_row] + screen_Columns)
5534 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535#endif
5536 )
5537 {
5538 /* First make sure we are at the end of the screen line,
5539 * then output the same character again to let the
5540 * terminal know about the wrap. If the terminal doesn't
5541 * auto-wrap, we overwrite the character. */
5542 if (screen_cur_col != W_WIDTH(wp))
5543 screen_char(LineOffset[screen_row - 1]
5544 + (unsigned)Columns - 1,
5545 screen_row - 1, (int)(Columns - 1));
5546
5547#ifdef FEAT_MBYTE
5548 /* When there is a multi-byte character, just output a
5549 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005550 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5551 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 out_char(' ');
5553 else
5554#endif
5555 out_char(ScreenLines[LineOffset[screen_row - 1]
5556 + (Columns - 1)]);
5557 /* force a redraw of the first char on the next line */
5558 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5559 screen_start(); /* don't know where cursor is now */
5560 }
5561 }
5562
5563 col = 0;
5564 off = (unsigned)(current_ScreenLine - ScreenLines);
5565#ifdef FEAT_RIGHTLEFT
5566 if (wp->w_p_rl)
5567 {
5568 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5569 off += col;
5570 }
5571#endif
5572
5573 /* reset the drawing state for the start of a wrapped line */
5574 draw_state = WL_START;
5575 saved_n_extra = n_extra;
5576 saved_p_extra = p_extra;
5577 saved_c_extra = c_extra;
5578 saved_char_attr = char_attr;
5579 n_extra = 0;
5580 lcs_prec_todo = lcs_prec;
5581#ifdef FEAT_LINEBREAK
5582# ifdef FEAT_DIFF
5583 if (filler_todo <= 0)
5584# endif
5585 need_showbreak = TRUE;
5586#endif
5587#ifdef FEAT_DIFF
5588 --filler_todo;
5589 /* When the filler lines are actually below the last line of the
5590 * file, don't draw the line itself, break here. */
5591 if (filler_todo == 0 && wp->w_botfill)
5592 break;
5593#endif
5594 }
5595
5596 } /* for every character in the line */
5597
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005598#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005599 /* After an empty line check first word for capital. */
5600 if (*skipwhite(line) == NUL)
5601 {
5602 capcol_lnum = lnum + 1;
5603 cap_col = 0;
5604 }
5605#endif
5606
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 return row;
5608}
5609
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005610#ifdef FEAT_MBYTE
5611static int comp_char_differs __ARGS((int, int));
5612
5613/*
5614 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005615 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005616 */
5617 static int
5618comp_char_differs(off_from, off_to)
5619 int off_from;
5620 int off_to;
5621{
5622 int i;
5623
5624 for (i = 0; i < Screen_mco; ++i)
5625 {
5626 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5627 return TRUE;
5628 if (ScreenLinesC[i][off_from] == 0)
5629 break;
5630 }
5631 return FALSE;
5632}
5633#endif
5634
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635/*
5636 * Check whether the given character needs redrawing:
5637 * - the (first byte of the) character is different
5638 * - the attributes are different
5639 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005640 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 */
5642 static int
5643char_needs_redraw(off_from, off_to, cols)
5644 int off_from;
5645 int off_to;
5646 int cols;
5647{
5648 if (cols > 0
5649 && ((ScreenLines[off_from] != ScreenLines[off_to]
5650 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5651
5652#ifdef FEAT_MBYTE
5653 || (enc_dbcs != 0
5654 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5655 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5656 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5657 : (cols > 1 && ScreenLines[off_from + 1]
5658 != ScreenLines[off_to + 1])))
5659 || (enc_utf8
5660 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5661 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005662 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005663 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5664 && ScreenLines[off_from + 1]
5665 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666#endif
5667 ))
5668 return TRUE;
5669 return FALSE;
5670}
5671
5672/*
5673 * Move one "cooked" screen line to the screen, but only the characters that
5674 * have actually changed. Handle insert/delete character.
5675 * "coloff" gives the first column on the screen for this line.
5676 * "endcol" gives the columns where valid characters are.
5677 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5678 * needs to be cleared, negative otherwise.
5679 * "rlflag" is TRUE in a rightleft window:
5680 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5681 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5682 */
5683 static void
5684screen_line(row, coloff, endcol, clear_width
5685#ifdef FEAT_RIGHTLEFT
5686 , rlflag
5687#endif
5688 )
5689 int row;
5690 int coloff;
5691 int endcol;
5692 int clear_width;
5693#ifdef FEAT_RIGHTLEFT
5694 int rlflag;
5695#endif
5696{
5697 unsigned off_from;
5698 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005699#ifdef FEAT_MBYTE
5700 unsigned max_off_from;
5701 unsigned max_off_to;
5702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 int col = 0;
5704#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5705 int hl;
5706#endif
5707 int force = FALSE; /* force update rest of the line */
5708 int redraw_this /* bool: does character need redraw? */
5709#ifdef FEAT_GUI
5710 = TRUE /* For GUI when while-loop empty */
5711#endif
5712 ;
5713 int redraw_next; /* redraw_this for next character */
5714#ifdef FEAT_MBYTE
5715 int clear_next = FALSE;
5716 int char_cells; /* 1: normal char */
5717 /* 2: occupies two display cells */
5718# define CHAR_CELLS char_cells
5719#else
5720# define CHAR_CELLS 1
5721#endif
5722
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005723 /* Check for illegal row and col, just in case. */
5724 if (row >= Rows)
5725 row = Rows - 1;
5726 if (endcol > Columns)
5727 endcol = Columns;
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729# ifdef FEAT_CLIPBOARD
5730 clip_may_clear_selection(row, row);
5731# endif
5732
5733 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5734 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005735#ifdef FEAT_MBYTE
5736 max_off_from = off_from + screen_Columns;
5737 max_off_to = LineOffset[row] + screen_Columns;
5738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
5740#ifdef FEAT_RIGHTLEFT
5741 if (rlflag)
5742 {
5743 /* Clear rest first, because it's left of the text. */
5744 if (clear_width > 0)
5745 {
5746 while (col <= endcol && ScreenLines[off_to] == ' '
5747 && ScreenAttrs[off_to] == 0
5748# ifdef FEAT_MBYTE
5749 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5750# endif
5751 )
5752 {
5753 ++off_to;
5754 ++col;
5755 }
5756 if (col <= endcol)
5757 screen_fill(row, row + 1, col + coloff,
5758 endcol + coloff + 1, ' ', ' ', 0);
5759 }
5760 col = endcol + 1;
5761 off_to = LineOffset[row] + col + coloff;
5762 off_from += col;
5763 endcol = (clear_width > 0 ? clear_width : -clear_width);
5764 }
5765#endif /* FEAT_RIGHTLEFT */
5766
5767 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5768
5769 while (col < endcol)
5770 {
5771#ifdef FEAT_MBYTE
5772 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005773 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 else
5775 char_cells = 1;
5776#endif
5777
5778 redraw_this = redraw_next;
5779 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5780 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5781
5782#ifdef FEAT_GUI
5783 /* If the next character was bold, then redraw the current character to
5784 * remove any pixels that might have spilt over into us. This only
5785 * happens in the GUI.
5786 */
5787 if (redraw_next && gui.in_use)
5788 {
5789 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005790 if (hl > HL_ALL)
5791 hl = syn_attr2attr(hl);
5792 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 redraw_this = TRUE;
5794 }
5795#endif
5796
5797 if (redraw_this)
5798 {
5799 /*
5800 * Special handling when 'xs' termcap flag set (hpterm):
5801 * Attributes for characters are stored at the position where the
5802 * cursor is when writing the highlighting code. The
5803 * start-highlighting code must be written with the cursor on the
5804 * first highlighted character. The stop-highlighting code must
5805 * be written with the cursor just after the last highlighted
5806 * character.
5807 * Overwriting a character doesn't remove it's highlighting. Need
5808 * to clear the rest of the line, and force redrawing it
5809 * completely.
5810 */
5811 if ( p_wiv
5812 && !force
5813#ifdef FEAT_GUI
5814 && !gui.in_use
5815#endif
5816 && ScreenAttrs[off_to] != 0
5817 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5818 {
5819 /*
5820 * Need to remove highlighting attributes here.
5821 */
5822 windgoto(row, col + coloff);
5823 out_str(T_CE); /* clear rest of this screen line */
5824 screen_start(); /* don't know where cursor is now */
5825 force = TRUE; /* force redraw of rest of the line */
5826 redraw_next = TRUE; /* or else next char would miss out */
5827
5828 /*
5829 * If the previous character was highlighted, need to stop
5830 * highlighting at this character.
5831 */
5832 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5833 {
5834 screen_attr = ScreenAttrs[off_to - 1];
5835 term_windgoto(row, col + coloff);
5836 screen_stop_highlight();
5837 }
5838 else
5839 screen_attr = 0; /* highlighting has stopped */
5840 }
5841#ifdef FEAT_MBYTE
5842 if (enc_dbcs != 0)
5843 {
5844 /* Check if overwriting a double-byte with a single-byte or
5845 * the other way around requires another character to be
5846 * redrawn. For UTF-8 this isn't needed, because comparing
5847 * ScreenLinesUC[] is sufficient. */
5848 if (char_cells == 1
5849 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005850 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
5852 /* Writing a single-cell character over a double-cell
5853 * character: need to redraw the next cell. */
5854 ScreenLines[off_to + 1] = 0;
5855 redraw_next = TRUE;
5856 }
5857 else if (char_cells == 2
5858 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005859 && (*mb_off2cells)(off_to, max_off_to) == 1
5860 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {
5862 /* Writing the second half of a double-cell character over
5863 * a double-cell character: need to redraw the second
5864 * cell. */
5865 ScreenLines[off_to + 2] = 0;
5866 redraw_next = TRUE;
5867 }
5868
5869 if (enc_dbcs == DBCS_JPNU)
5870 ScreenLines2[off_to] = ScreenLines2[off_from];
5871 }
5872 /* When writing a single-width character over a double-width
5873 * character and at the end of the redrawn text, need to clear out
5874 * the right halve of the old character.
5875 * Also required when writing the right halve of a double-width
5876 * char over the left halve of an existing one. */
5877 if (has_mbyte && col + char_cells == endcol
5878 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005879 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005881 && (*mb_off2cells)(off_to, max_off_to) == 1
5882 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 clear_next = TRUE;
5884#endif
5885
5886 ScreenLines[off_to] = ScreenLines[off_from];
5887#ifdef FEAT_MBYTE
5888 if (enc_utf8)
5889 {
5890 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5891 if (ScreenLinesUC[off_from] != 0)
5892 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005893 int i;
5894
5895 for (i = 0; i < Screen_mco; ++i)
5896 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
5898 }
5899 if (char_cells == 2)
5900 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5901#endif
5902
5903#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005904 /* The bold trick makes a single column of pixels appear in the
5905 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 * character should be redrawn too. This happens for our own GUI
5907 * and for some xterms. */
5908 if (
5909# ifdef FEAT_GUI
5910 gui.in_use
5911# endif
5912# if defined(FEAT_GUI) && defined(UNIX)
5913 ||
5914# endif
5915# ifdef UNIX
5916 term_is_xterm
5917# endif
5918 )
5919 {
5920 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005921 if (hl > HL_ALL)
5922 hl = syn_attr2attr(hl);
5923 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 redraw_next = TRUE;
5925 }
5926#endif
5927 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5928#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005929 /* For simplicity set the attributes of second half of a
5930 * double-wide character equal to the first half. */
5931 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005933
5934 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 else
5937#endif
5938 screen_char(off_to, row, col + coloff);
5939 }
5940 else if ( p_wiv
5941#ifdef FEAT_GUI
5942 && !gui.in_use
5943#endif
5944 && col + coloff > 0)
5945 {
5946 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5947 {
5948 /*
5949 * Don't output stop-highlight when moving the cursor, it will
5950 * stop the highlighting when it should continue.
5951 */
5952 screen_attr = 0;
5953 }
5954 else if (screen_attr != 0)
5955 screen_stop_highlight();
5956 }
5957
5958 off_to += CHAR_CELLS;
5959 off_from += CHAR_CELLS;
5960 col += CHAR_CELLS;
5961 }
5962
5963#ifdef FEAT_MBYTE
5964 if (clear_next)
5965 {
5966 /* Clear the second half of a double-wide character of which the left
5967 * half was overwritten with a single-wide character. */
5968 ScreenLines[off_to] = ' ';
5969 if (enc_utf8)
5970 ScreenLinesUC[off_to] = 0;
5971 screen_char(off_to, row, col + coloff);
5972 }
5973#endif
5974
5975 if (clear_width > 0
5976#ifdef FEAT_RIGHTLEFT
5977 && !rlflag
5978#endif
5979 )
5980 {
5981#ifdef FEAT_GUI
5982 int startCol = col;
5983#endif
5984
5985 /* blank out the rest of the line */
5986 while (col < clear_width && ScreenLines[off_to] == ' '
5987 && ScreenAttrs[off_to] == 0
5988#ifdef FEAT_MBYTE
5989 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5990#endif
5991 )
5992 {
5993 ++off_to;
5994 ++col;
5995 }
5996 if (col < clear_width)
5997 {
5998#ifdef FEAT_GUI
5999 /*
6000 * In the GUI, clearing the rest of the line may leave pixels
6001 * behind if the first character cleared was bold. Some bold
6002 * fonts spill over the left. In this case we redraw the previous
6003 * character too. If we didn't skip any blanks above, then we
6004 * only redraw if the character wasn't already redrawn anyway.
6005 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006006 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 {
6008 hl = ScreenAttrs[off_to];
6009 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006010 {
6011 int prev_cells = 1;
6012# ifdef FEAT_MBYTE
6013 if (enc_utf8)
6014 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6015 * that its width is 2. */
6016 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6017 else if (enc_dbcs != 0)
6018 {
6019 /* find previous character by counting from first
6020 * column and get its width. */
6021 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006022 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006023
6024 while (off < off_to)
6025 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006026 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006027 off += prev_cells;
6028 }
6029 }
6030
6031 if (enc_dbcs != 0 && prev_cells > 1)
6032 screen_char_2(off_to - prev_cells, row,
6033 col + coloff - prev_cells);
6034 else
6035# endif
6036 screen_char(off_to - prev_cells, row,
6037 col + coloff - prev_cells);
6038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 }
6040#endif
6041 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6042 ' ', ' ', 0);
6043#ifdef FEAT_VERTSPLIT
6044 off_to += clear_width - col;
6045 col = clear_width;
6046#endif
6047 }
6048 }
6049
6050 if (clear_width > 0)
6051 {
6052#ifdef FEAT_VERTSPLIT
6053 /* For a window that's left of another, draw the separator char. */
6054 if (col + coloff < Columns)
6055 {
6056 int c;
6057
6058 c = fillchar_vsep(&hl);
6059 if (ScreenLines[off_to] != c
6060# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006061 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6062 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063# endif
6064 || ScreenAttrs[off_to] != hl)
6065 {
6066 ScreenLines[off_to] = c;
6067 ScreenAttrs[off_to] = hl;
6068# ifdef FEAT_MBYTE
6069 if (enc_utf8)
6070 {
6071 if (c >= 0x80)
6072 {
6073 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006074 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 }
6076 else
6077 ScreenLinesUC[off_to] = 0;
6078 }
6079# endif
6080 screen_char(off_to, row, col + coloff);
6081 }
6082 }
6083 else
6084#endif
6085 LineWraps[row] = FALSE;
6086 }
6087}
6088
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006089#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006091 * Mirror text "str" for right-left displaying.
6092 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006094 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095rl_mirror(str)
6096 char_u *str;
6097{
6098 char_u *p1, *p2;
6099 int t;
6100
6101 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6102 {
6103 t = *p1;
6104 *p1 = *p2;
6105 *p2 = t;
6106 }
6107}
6108#endif
6109
6110#if defined(FEAT_WINDOWS) || defined(PROTO)
6111/*
6112 * mark all status lines for redraw; used after first :cd
6113 */
6114 void
6115status_redraw_all()
6116{
6117 win_T *wp;
6118
6119 for (wp = firstwin; wp; wp = wp->w_next)
6120 if (wp->w_status_height)
6121 {
6122 wp->w_redr_status = TRUE;
6123 redraw_later(VALID);
6124 }
6125}
6126
6127/*
6128 * mark all status lines of the current buffer for redraw
6129 */
6130 void
6131status_redraw_curbuf()
6132{
6133 win_T *wp;
6134
6135 for (wp = firstwin; wp; wp = wp->w_next)
6136 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6137 {
6138 wp->w_redr_status = TRUE;
6139 redraw_later(VALID);
6140 }
6141}
6142
6143/*
6144 * Redraw all status lines that need to be redrawn.
6145 */
6146 void
6147redraw_statuslines()
6148{
6149 win_T *wp;
6150
6151 for (wp = firstwin; wp; wp = wp->w_next)
6152 if (wp->w_redr_status)
6153 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006154 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006155 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156}
6157#endif
6158
6159#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6160/*
6161 * Redraw all status lines at the bottom of frame "frp".
6162 */
6163 void
6164win_redraw_last_status(frp)
6165 frame_T *frp;
6166{
6167 if (frp->fr_layout == FR_LEAF)
6168 frp->fr_win->w_redr_status = TRUE;
6169 else if (frp->fr_layout == FR_ROW)
6170 {
6171 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6172 win_redraw_last_status(frp);
6173 }
6174 else /* frp->fr_layout == FR_COL */
6175 {
6176 frp = frp->fr_child;
6177 while (frp->fr_next != NULL)
6178 frp = frp->fr_next;
6179 win_redraw_last_status(frp);
6180 }
6181}
6182#endif
6183
6184#ifdef FEAT_VERTSPLIT
6185/*
6186 * Draw the verticap separator right of window "wp" starting with line "row".
6187 */
6188 static void
6189draw_vsep_win(wp, row)
6190 win_T *wp;
6191 int row;
6192{
6193 int hl;
6194 int c;
6195
6196 if (wp->w_vsep_width)
6197 {
6198 /* draw the vertical separator right of this window */
6199 c = fillchar_vsep(&hl);
6200 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6201 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6202 c, ' ', hl);
6203 }
6204}
6205#endif
6206
6207#ifdef FEAT_WILDMENU
6208static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006209static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
6211/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006212 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 */
6214 static int
6215status_match_len(xp, s)
6216 expand_T *xp;
6217 char_u *s;
6218{
6219 int len = 0;
6220
6221#ifdef FEAT_MENU
6222 int emenu = (xp->xp_context == EXPAND_MENUS
6223 || xp->xp_context == EXPAND_MENUNAMES);
6224
6225 /* Check for menu separators - replace with '|'. */
6226 if (emenu && menu_is_separator(s))
6227 return 1;
6228#endif
6229
6230 while (*s != NUL)
6231 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006232 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006233 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006234 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 }
6236
6237 return len;
6238}
6239
6240/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006241 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006242 * These are backslashes used for escaping. Do show backslashes in help tags.
6243 */
6244 static int
6245skip_status_match_char(xp, s)
6246 expand_T *xp;
6247 char_u *s;
6248{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006249 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006250#ifdef FEAT_MENU
6251 || ((xp->xp_context == EXPAND_MENUS
6252 || xp->xp_context == EXPAND_MENUNAMES)
6253 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6254#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006255 )
6256 {
6257#ifndef BACKSLASH_IN_FILENAME
6258 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6259 return 2;
6260#endif
6261 return 1;
6262 }
6263 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006264}
6265
6266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 * Show wildchar matches in the status line.
6268 * Show at least the "match" item.
6269 * We start at item 'first_match' in the list and show all matches that fit.
6270 *
6271 * If inversion is possible we use it. Else '=' characters are used.
6272 */
6273 void
6274win_redr_status_matches(xp, num_matches, matches, match, showtail)
6275 expand_T *xp;
6276 int num_matches;
6277 char_u **matches; /* list of matches */
6278 int match;
6279 int showtail;
6280{
6281#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6282 int row;
6283 char_u *buf;
6284 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006285 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 int fillchar;
6287 int attr;
6288 int i;
6289 int highlight = TRUE;
6290 char_u *selstart = NULL;
6291 int selstart_col = 0;
6292 char_u *selend = NULL;
6293 static int first_match = 0;
6294 int add_left = FALSE;
6295 char_u *s;
6296#ifdef FEAT_MENU
6297 int emenu;
6298#endif
6299#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6300 int l;
6301#endif
6302
6303 if (matches == NULL) /* interrupted completion? */
6304 return;
6305
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006306#ifdef FEAT_MBYTE
6307 if (has_mbyte)
6308 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6309 else
6310#endif
6311 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (buf == NULL)
6313 return;
6314
6315 if (match == -1) /* don't show match but original text */
6316 {
6317 match = 0;
6318 highlight = FALSE;
6319 }
6320 /* count 1 for the ending ">" */
6321 clen = status_match_len(xp, L_MATCH(match)) + 3;
6322 if (match == 0)
6323 first_match = 0;
6324 else if (match < first_match)
6325 {
6326 /* jumping left, as far as we can go */
6327 first_match = match;
6328 add_left = TRUE;
6329 }
6330 else
6331 {
6332 /* check if match fits on the screen */
6333 for (i = first_match; i < match; ++i)
6334 clen += status_match_len(xp, L_MATCH(i)) + 2;
6335 if (first_match > 0)
6336 clen += 2;
6337 /* jumping right, put match at the left */
6338 if ((long)clen > Columns)
6339 {
6340 first_match = match;
6341 /* if showing the last match, we can add some on the left */
6342 clen = 2;
6343 for (i = match; i < num_matches; ++i)
6344 {
6345 clen += status_match_len(xp, L_MATCH(i)) + 2;
6346 if ((long)clen >= Columns)
6347 break;
6348 }
6349 if (i == num_matches)
6350 add_left = TRUE;
6351 }
6352 }
6353 if (add_left)
6354 while (first_match > 0)
6355 {
6356 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6357 if ((long)clen >= Columns)
6358 break;
6359 --first_match;
6360 }
6361
6362 fillchar = fillchar_status(&attr, TRUE);
6363
6364 if (first_match == 0)
6365 {
6366 *buf = NUL;
6367 len = 0;
6368 }
6369 else
6370 {
6371 STRCPY(buf, "< ");
6372 len = 2;
6373 }
6374 clen = len;
6375
6376 i = first_match;
6377 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6378 {
6379 if (i == match)
6380 {
6381 selstart = buf + len;
6382 selstart_col = clen;
6383 }
6384
6385 s = L_MATCH(i);
6386 /* Check for menu separators - replace with '|' */
6387#ifdef FEAT_MENU
6388 emenu = (xp->xp_context == EXPAND_MENUS
6389 || xp->xp_context == EXPAND_MENUNAMES);
6390 if (emenu && menu_is_separator(s))
6391 {
6392 STRCPY(buf + len, transchar('|'));
6393 l = (int)STRLEN(buf + len);
6394 len += l;
6395 clen += l;
6396 }
6397 else
6398#endif
6399 for ( ; *s != NUL; ++s)
6400 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006401 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 clen += ptr2cells(s);
6403#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006404 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
6406 STRNCPY(buf + len, s, l);
6407 s += l - 1;
6408 len += l;
6409 }
6410 else
6411#endif
6412 {
6413 STRCPY(buf + len, transchar_byte(*s));
6414 len += (int)STRLEN(buf + len);
6415 }
6416 }
6417 if (i == match)
6418 selend = buf + len;
6419
6420 *(buf + len++) = ' ';
6421 *(buf + len++) = ' ';
6422 clen += 2;
6423 if (++i == num_matches)
6424 break;
6425 }
6426
6427 if (i != num_matches)
6428 {
6429 *(buf + len++) = '>';
6430 ++clen;
6431 }
6432
6433 buf[len] = NUL;
6434
6435 row = cmdline_row - 1;
6436 if (row >= 0)
6437 {
6438 if (wild_menu_showing == 0)
6439 {
6440 if (msg_scrolled > 0)
6441 {
6442 /* Put the wildmenu just above the command line. If there is
6443 * no room, scroll the screen one line up. */
6444 if (cmdline_row == Rows - 1)
6445 {
6446 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6447 ++msg_scrolled;
6448 }
6449 else
6450 {
6451 ++cmdline_row;
6452 ++row;
6453 }
6454 wild_menu_showing = WM_SCROLLED;
6455 }
6456 else
6457 {
6458 /* Create status line if needed by setting 'laststatus' to 2.
6459 * Set 'winminheight' to zero to avoid that the window is
6460 * resized. */
6461 if (lastwin->w_status_height == 0)
6462 {
6463 save_p_ls = p_ls;
6464 save_p_wmh = p_wmh;
6465 p_ls = 2;
6466 p_wmh = 0;
6467 last_status(FALSE);
6468 }
6469 wild_menu_showing = WM_SHOWN;
6470 }
6471 }
6472
6473 screen_puts(buf, row, 0, attr);
6474 if (selstart != NULL && highlight)
6475 {
6476 *selend = NUL;
6477 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6478 }
6479
6480 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6481 }
6482
6483#ifdef FEAT_VERTSPLIT
6484 win_redraw_last_status(topframe);
6485#else
6486 lastwin->w_redr_status = TRUE;
6487#endif
6488 vim_free(buf);
6489}
6490#endif
6491
6492#if defined(FEAT_WINDOWS) || defined(PROTO)
6493/*
6494 * Redraw the status line of window wp.
6495 *
6496 * If inversion is possible we use it. Else '=' characters are used.
6497 */
6498 void
6499win_redr_status(wp)
6500 win_T *wp;
6501{
6502 int row;
6503 char_u *p;
6504 int len;
6505 int fillchar;
6506 int attr;
6507 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006508 static int busy = FALSE;
6509
6510 /* It's possible to get here recursively when 'statusline' (indirectly)
6511 * invokes ":redrawstatus". Simply ignore the call then. */
6512 if (busy)
6513 return;
6514 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516 wp->w_redr_status = FALSE;
6517 if (wp->w_status_height == 0)
6518 {
6519 /* no status line, can only be last window */
6520 redraw_cmdline = TRUE;
6521 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006522 else if (!redrawing()
6523#ifdef FEAT_INS_EXPAND
6524 /* don't update status line when popup menu is visible and may be
6525 * drawn over it */
6526 || pum_visible()
6527#endif
6528 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
6530 /* Don't redraw right now, do it later. */
6531 wp->w_redr_status = TRUE;
6532 }
6533#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006534 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006537 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 }
6539#endif
6540 else
6541 {
6542 fillchar = fillchar_status(&attr, wp == curwin);
6543
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006544 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 p = NameBuff;
6546 len = (int)STRLEN(p);
6547
6548 if (wp->w_buffer->b_help
6549#ifdef FEAT_QUICKFIX
6550 || wp->w_p_pvw
6551#endif
6552 || bufIsChanged(wp->w_buffer)
6553 || wp->w_buffer->b_p_ro)
6554 *(p + len++) = ' ';
6555 if (wp->w_buffer->b_help)
6556 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006557 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 len += (int)STRLEN(p + len);
6559 }
6560#ifdef FEAT_QUICKFIX
6561 if (wp->w_p_pvw)
6562 {
6563 STRCPY(p + len, _("[Preview]"));
6564 len += (int)STRLEN(p + len);
6565 }
6566#endif
6567 if (bufIsChanged(wp->w_buffer))
6568 {
6569 STRCPY(p + len, "[+]");
6570 len += 3;
6571 }
6572 if (wp->w_buffer->b_p_ro)
6573 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006574 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 len += 4;
6576 }
6577
6578#ifndef FEAT_VERTSPLIT
6579 this_ru_col = ru_col;
6580 if (this_ru_col < (Columns + 1) / 2)
6581 this_ru_col = (Columns + 1) / 2;
6582#else
6583 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6584 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6585 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6586 if (this_ru_col <= 1)
6587 {
6588 p = (char_u *)"<"; /* No room for file name! */
6589 len = 1;
6590 }
6591 else
6592#endif
6593#ifdef FEAT_MBYTE
6594 if (has_mbyte)
6595 {
6596 int clen = 0, i;
6597
6598 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006599 clen = mb_string2cells(p, -1);
6600
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 /* Find first character that will fit.
6602 * Going from start to end is much faster for DBCS. */
6603 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006604 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 clen -= (*mb_ptr2cells)(p + i);
6606 len = clen;
6607 if (i > 0)
6608 {
6609 p = p + i - 1;
6610 *p = '<';
6611 ++len;
6612 }
6613
6614 }
6615 else
6616#endif
6617 if (len > this_ru_col - 1)
6618 {
6619 p += len - (this_ru_col - 1);
6620 *p = '<';
6621 len = this_ru_col - 1;
6622 }
6623
6624 row = W_WINROW(wp) + wp->w_height;
6625 screen_puts(p, row, W_WINCOL(wp), attr);
6626 screen_fill(row, row + 1, len + W_WINCOL(wp),
6627 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6628
6629 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6630 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6631 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6632 - 1 + W_WINCOL(wp)), attr);
6633
6634#ifdef FEAT_CMDL_INFO
6635 win_redr_ruler(wp, TRUE);
6636#endif
6637 }
6638
6639#ifdef FEAT_VERTSPLIT
6640 /*
6641 * May need to draw the character below the vertical separator.
6642 */
6643 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6644 {
6645 if (stl_connected(wp))
6646 fillchar = fillchar_status(&attr, wp == curwin);
6647 else
6648 fillchar = fillchar_vsep(&attr);
6649 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6650 attr);
6651 }
6652#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006653 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654}
6655
Bram Moolenaar238a5642006-02-21 22:12:05 +00006656#ifdef FEAT_STL_OPT
6657/*
6658 * Redraw the status line according to 'statusline' and take care of any
6659 * errors encountered.
6660 */
6661 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006662redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006663 win_T *wp;
6664{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006665 static int entered = FALSE;
6666 int save_called_emsg = called_emsg;
6667
6668 /* When called recursively return. This can happen when the statusline
6669 * contains an expression that triggers a redraw. */
6670 if (entered)
6671 return;
6672 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006673
6674 called_emsg = FALSE;
6675 win_redr_custom(wp, FALSE);
6676 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006677 {
6678 /* When there is an error disable the statusline, otherwise the
6679 * display is messed up with errors and a redraw triggers the problem
6680 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006681 set_string_option_direct((char_u *)"statusline", -1,
6682 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006683 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006684 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006685 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006686 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006687}
6688#endif
6689
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690# ifdef FEAT_VERTSPLIT
6691/*
6692 * Return TRUE if the status line of window "wp" is connected to the status
6693 * line of the window right of it. If not, then it's a vertical separator.
6694 * Only call if (wp->w_vsep_width != 0).
6695 */
6696 int
6697stl_connected(wp)
6698 win_T *wp;
6699{
6700 frame_T *fr;
6701
6702 fr = wp->w_frame;
6703 while (fr->fr_parent != NULL)
6704 {
6705 if (fr->fr_parent->fr_layout == FR_COL)
6706 {
6707 if (fr->fr_next != NULL)
6708 break;
6709 }
6710 else
6711 {
6712 if (fr->fr_next != NULL)
6713 return TRUE;
6714 }
6715 fr = fr->fr_parent;
6716 }
6717 return FALSE;
6718}
6719# endif
6720
6721#endif /* FEAT_WINDOWS */
6722
6723#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6724/*
6725 * Get the value to show for the language mappings, active 'keymap'.
6726 */
6727 int
6728get_keymap_str(wp, buf, len)
6729 win_T *wp;
6730 char_u *buf; /* buffer for the result */
6731 int len; /* length of buffer */
6732{
6733 char_u *p;
6734
6735 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6736 return FALSE;
6737
6738 {
6739#ifdef FEAT_EVAL
6740 buf_T *old_curbuf = curbuf;
6741 win_T *old_curwin = curwin;
6742 char_u *s;
6743
6744 curbuf = wp->w_buffer;
6745 curwin = wp;
6746 STRCPY(buf, "b:keymap_name"); /* must be writable */
6747 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006748 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 --emsg_skip;
6750 curbuf = old_curbuf;
6751 curwin = old_curwin;
6752 if (p == NULL || *p == NUL)
6753#endif
6754 {
6755#ifdef FEAT_KEYMAP
6756 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6757 p = wp->w_buffer->b_p_keymap;
6758 else
6759#endif
6760 p = (char_u *)"lang";
6761 }
6762 if ((int)(STRLEN(p) + 3) < len)
6763 sprintf((char *)buf, "<%s>", p);
6764 else
6765 buf[0] = NUL;
6766#ifdef FEAT_EVAL
6767 vim_free(s);
6768#endif
6769 }
6770 return buf[0] != NUL;
6771}
6772#endif
6773
6774#if defined(FEAT_STL_OPT) || defined(PROTO)
6775/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006776 * Redraw the status line or ruler of window "wp".
6777 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 */
6779 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006780win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006782 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006784 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 int attr;
6786 int curattr;
6787 int row;
6788 int col = 0;
6789 int maxwidth;
6790 int width;
6791 int n;
6792 int len;
6793 int fillchar;
6794 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006795 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006797 struct stl_hlrec hltab[STL_MAX_ITEM];
6798 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006799 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006800 win_T *ewp;
6801 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
Bram Moolenaar1d633412013-12-11 15:52:01 +01006803 /* There is a tiny chance that this gets called recursively: When
6804 * redrawing a status line triggers redrawing the ruler or tabline.
6805 * Avoid trouble by not allowing recursion. */
6806 if (entered)
6807 return;
6808 entered = TRUE;
6809
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006811 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006813 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006814 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006815 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006816 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006817 attr = hl_attr(HLF_TPF);
6818 maxwidth = Columns;
6819# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006820 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006823 else
6824 {
6825 row = W_WINROW(wp) + wp->w_height;
6826 fillchar = fillchar_status(&attr, wp == curwin);
6827 maxwidth = W_WIDTH(wp);
6828
6829 if (draw_ruler)
6830 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006831 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006832 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006833 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006834 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006835 if (*++stl == '-')
6836 stl++;
6837 if (atoi((char *)stl))
6838 while (VIM_ISDIGIT(*stl))
6839 stl++;
6840 if (*stl++ != '(')
6841 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006842 }
6843#ifdef FEAT_VERTSPLIT
6844 col = ru_col - (Columns - W_WIDTH(wp));
6845 if (col < (W_WIDTH(wp) + 1) / 2)
6846 col = (W_WIDTH(wp) + 1) / 2;
6847#else
6848 col = ru_col;
6849 if (col > (Columns + 1) / 2)
6850 col = (Columns + 1) / 2;
6851#endif
6852 maxwidth = W_WIDTH(wp) - col;
6853#ifdef FEAT_WINDOWS
6854 if (!wp->w_status_height)
6855#endif
6856 {
6857 row = Rows - 1;
6858 --maxwidth; /* writing in last column may cause scrolling */
6859 fillchar = ' ';
6860 attr = 0;
6861 }
6862
6863# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006864 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006865# endif
6866 }
6867 else
6868 {
6869 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006870 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006871 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006872 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006873# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006874 use_sandbox = was_set_insecurely((char_u *)"statusline",
6875 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006876# endif
6877 }
6878
6879#ifdef FEAT_VERTSPLIT
6880 col += W_WINCOL(wp);
6881#endif
6882 }
6883
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006885 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886
Bram Moolenaar61452852011-02-01 18:01:11 +01006887 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6888 * the cursor away and back. */
6889 ewp = wp == NULL ? curwin : wp;
6890 p_crb_save = ewp->w_p_crb;
6891 ewp->w_p_crb = FALSE;
6892
Bram Moolenaar362f3562009-11-03 16:20:34 +00006893 /* Make a copy, because the statusline may include a function call that
6894 * might change the option value and free the memory. */
6895 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006896 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006897 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006898 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006899 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006900 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006902 /* Make all characters printable. */
6903 p = transstr(buf);
6904 if (p != NULL)
6905 {
6906 vim_strncpy(buf, p, sizeof(buf) - 1);
6907 vim_free(p);
6908 }
6909
6910 /* fill up with "fillchar" */
6911 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006912 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
6914#ifdef FEAT_MBYTE
6915 len += (*mb_char2bytes)(fillchar, buf + len);
6916#else
6917 buf[len++] = fillchar;
6918#endif
6919 ++width;
6920 }
6921 buf[len] = NUL;
6922
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006923 /*
6924 * Draw each snippet with the specified highlighting.
6925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 curattr = attr;
6927 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006928 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006930 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 screen_puts_len(p, len, row, col, curattr);
6932 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006933 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006935 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006937 else if (hltab[n].userhl < 0)
6938 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006940 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006941 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942#endif
6943 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006944 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 }
6946 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006947
6948 if (wp == NULL)
6949 {
6950 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6951 col = 0;
6952 len = 0;
6953 p = buf;
6954 fillchar = 0;
6955 for (n = 0; tabtab[n].start != NULL; n++)
6956 {
6957 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6958 while (col < len)
6959 TabPageIdxs[col++] = fillchar;
6960 p = tabtab[n].start;
6961 fillchar = tabtab[n].userhl;
6962 }
6963 while (col < Columns)
6964 TabPageIdxs[col++] = fillchar;
6965 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006966
6967theend:
6968 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969}
6970
6971#endif /* FEAT_STL_OPT */
6972
6973/*
6974 * Output a single character directly to the screen and update ScreenLines.
6975 */
6976 void
6977screen_putchar(c, row, col, attr)
6978 int c;
6979 int row, col;
6980 int attr;
6981{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 char_u buf[MB_MAXBYTES + 1];
6983
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006984#ifdef FEAT_MBYTE
6985 if (has_mbyte)
6986 buf[(*mb_char2bytes)(c, buf)] = NUL;
6987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006989 {
6990 buf[0] = c;
6991 buf[1] = NUL;
6992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 screen_puts(buf, row, col, attr);
6994}
6995
6996/*
6997 * Get a single character directly from ScreenLines into "bytes[]".
6998 * Also return its attribute in *attrp;
6999 */
7000 void
7001screen_getbytes(row, col, bytes, attrp)
7002 int row, col;
7003 char_u *bytes;
7004 int *attrp;
7005{
7006 unsigned off;
7007
7008 /* safety check */
7009 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7010 {
7011 off = LineOffset[row] + col;
7012 *attrp = ScreenAttrs[off];
7013 bytes[0] = ScreenLines[off];
7014 bytes[1] = NUL;
7015
7016#ifdef FEAT_MBYTE
7017 if (enc_utf8 && ScreenLinesUC[off] != 0)
7018 bytes[utfc_char2bytes(off, bytes)] = NUL;
7019 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7020 {
7021 bytes[0] = ScreenLines[off];
7022 bytes[1] = ScreenLines2[off];
7023 bytes[2] = NUL;
7024 }
7025 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7026 {
7027 bytes[1] = ScreenLines[off + 1];
7028 bytes[2] = NUL;
7029 }
7030#endif
7031 }
7032}
7033
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007034#ifdef FEAT_MBYTE
7035static int screen_comp_differs __ARGS((int, int*));
7036
7037/*
7038 * Return TRUE if composing characters for screen posn "off" differs from
7039 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007040 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007041 */
7042 static int
7043screen_comp_differs(off, u8cc)
7044 int off;
7045 int *u8cc;
7046{
7047 int i;
7048
7049 for (i = 0; i < Screen_mco; ++i)
7050 {
7051 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7052 return TRUE;
7053 if (u8cc[i] == 0)
7054 break;
7055 }
7056 return FALSE;
7057}
7058#endif
7059
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060/*
7061 * Put string '*text' on the screen at position 'row' and 'col', with
7062 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7063 * Note: only outputs within one row, message is truncated at screen boundary!
7064 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7065 */
7066 void
7067screen_puts(text, row, col, attr)
7068 char_u *text;
7069 int row;
7070 int col;
7071 int attr;
7072{
7073 screen_puts_len(text, -1, row, col, attr);
7074}
7075
7076/*
7077 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7078 * a NUL.
7079 */
7080 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007081screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007083 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 int row;
7085 int col;
7086 int attr;
7087{
7088 unsigned off;
7089 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007090 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 int c;
7092#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007093 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 int mbyte_blen = 1;
7095 int mbyte_cells = 1;
7096 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007097 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 int clear_next_cell = FALSE;
7099# ifdef FEAT_ARABIC
7100 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007101 int pc, nc, nc1;
7102 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103# endif
7104#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007105#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7106 int force_redraw_this;
7107 int force_redraw_next = FALSE;
7108#endif
7109 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
7111 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7112 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007113 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114
Bram Moolenaarc236c162008-07-13 17:41:49 +00007115#ifdef FEAT_MBYTE
7116 /* When drawing over the right halve of a double-wide char clear out the
7117 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007118 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007119# ifdef FEAT_GUI
7120 && !gui.in_use
7121# endif
7122 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007123 {
7124 ScreenLines[off - 1] = ' ';
7125 ScreenAttrs[off - 1] = 0;
7126 if (enc_utf8)
7127 {
7128 ScreenLinesUC[off - 1] = 0;
7129 ScreenLinesC[0][off - 1] = 0;
7130 }
7131 /* redraw the previous cell, make it empty */
7132 screen_char(off - 1, row, col - 1);
7133 /* force the cell at "col" to be redrawn */
7134 force_redraw_next = TRUE;
7135 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007136#endif
7137
Bram Moolenaar367329b2007-08-30 11:53:22 +00007138#ifdef FEAT_MBYTE
7139 max_off = LineOffset[row] + screen_Columns;
7140#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007141 while (col < screen_Columns
7142 && (len < 0 || (int)(ptr - text) < len)
7143 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {
7145 c = *ptr;
7146#ifdef FEAT_MBYTE
7147 /* check if this is the first byte of a multibyte */
7148 if (has_mbyte)
7149 {
7150 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007151 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007153 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7155 mbyte_cells = 1;
7156 else if (enc_dbcs != 0)
7157 mbyte_cells = mbyte_blen;
7158 else /* enc_utf8 */
7159 {
7160 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007161 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 (int)((text + len) - ptr));
7163 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007164 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007166# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 /* Non-BMP character: display as ? or fullwidth ?. */
7168 if (u8c >= 0x10000)
7169 {
7170 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7171 if (attr == 0)
7172 attr = hl_attr(HLF_8);
7173 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175# ifdef FEAT_ARABIC
7176 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7177 {
7178 /* Do Arabic shaping. */
7179 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7180 {
7181 /* Past end of string to be displayed. */
7182 nc = NUL;
7183 nc1 = NUL;
7184 }
7185 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007186 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007187 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7188 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007189 nc1 = pcc[0];
7190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 pc = prev_c;
7192 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007193 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 }
7195 else
7196 prev_c = u8c;
7197# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007198 if (col + mbyte_cells > screen_Columns)
7199 {
7200 /* Only 1 cell left, but character requires 2 cells:
7201 * display a '>' in the last column to avoid wrapping. */
7202 c = '>';
7203 mbyte_cells = 1;
7204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206 }
7207#endif
7208
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007209#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7210 force_redraw_this = force_redraw_next;
7211 force_redraw_next = FALSE;
7212#endif
7213
7214 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215#ifdef FEAT_MBYTE
7216 || (mbyte_cells == 2
7217 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7218 || (enc_dbcs == DBCS_JPNU
7219 && c == 0x8e
7220 && ScreenLines2[off] != ptr[1])
7221 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007222 && (ScreenLinesUC[off] !=
7223 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7224 || (ScreenLinesUC[off] != 0
7225 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226#endif
7227 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007228 || exmode_active;
7229
7230 if (need_redraw
7231#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7232 || force_redraw_this
7233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 )
7235 {
7236#if defined(FEAT_GUI) || defined(UNIX)
7237 /* The bold trick makes a single row of pixels appear in the next
7238 * character. When a bold character is removed, the next
7239 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007240 * and for some xterms. */
7241 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242# ifdef FEAT_GUI
7243 gui.in_use
7244# endif
7245# if defined(FEAT_GUI) && defined(UNIX)
7246 ||
7247# endif
7248# ifdef UNIX
7249 term_is_xterm
7250# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007251 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007253 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007255 if (n > HL_ALL)
7256 n = syn_attr2attr(n);
7257 if (n & HL_BOLD)
7258 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260#endif
7261#ifdef FEAT_MBYTE
7262 /* When at the end of the text and overwriting a two-cell
7263 * character with a one-cell character, need to clear the next
7264 * cell. Also when overwriting the left halve of a two-cell char
7265 * with the right halve of a two-cell char. Do this only once
7266 * (mb_off2cells() may return 2 on the right halve). */
7267 if (clear_next_cell)
7268 clear_next_cell = FALSE;
7269 else if (has_mbyte
7270 && (len < 0 ? ptr[mbyte_blen] == NUL
7271 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007272 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007274 && (*mb_off2cells)(off, max_off) == 1
7275 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 clear_next_cell = TRUE;
7277
7278 /* Make sure we never leave a second byte of a double-byte behind,
7279 * it confuses mb_off2cells(). */
7280 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007281 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007283 && (*mb_off2cells)(off, max_off) == 1
7284 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 ScreenLines[off + mbyte_blen] = 0;
7286#endif
7287 ScreenLines[off] = c;
7288 ScreenAttrs[off] = attr;
7289#ifdef FEAT_MBYTE
7290 if (enc_utf8)
7291 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007292 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 ScreenLinesUC[off] = 0;
7294 else
7295 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007296 int i;
7297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007299 for (i = 0; i < Screen_mco; ++i)
7300 {
7301 ScreenLinesC[i][off] = u8cc[i];
7302 if (u8cc[i] == 0)
7303 break;
7304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 }
7306 if (mbyte_cells == 2)
7307 {
7308 ScreenLines[off + 1] = 0;
7309 ScreenAttrs[off + 1] = attr;
7310 }
7311 screen_char(off, row, col);
7312 }
7313 else if (mbyte_cells == 2)
7314 {
7315 ScreenLines[off + 1] = ptr[1];
7316 ScreenAttrs[off + 1] = attr;
7317 screen_char_2(off, row, col);
7318 }
7319 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7320 {
7321 ScreenLines2[off] = ptr[1];
7322 screen_char(off, row, col);
7323 }
7324 else
7325#endif
7326 screen_char(off, row, col);
7327 }
7328#ifdef FEAT_MBYTE
7329 if (has_mbyte)
7330 {
7331 off += mbyte_cells;
7332 col += mbyte_cells;
7333 ptr += mbyte_blen;
7334 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007335 {
7336 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007338 len = -1;
7339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 }
7341 else
7342#endif
7343 {
7344 ++off;
7345 ++col;
7346 ++ptr;
7347 }
7348 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007349
7350#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7351 /* If we detected the next character needs to be redrawn, but the text
7352 * doesn't extend up to there, update the character here. */
7353 if (force_redraw_next && col < screen_Columns)
7354 {
7355# ifdef FEAT_MBYTE
7356 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7357 screen_char_2(off, row, col);
7358 else
7359# endif
7360 screen_char(off, row, col);
7361 }
7362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363}
7364
7365#ifdef FEAT_SEARCH_EXTRA
7366/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007367 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 */
7369 static void
7370start_search_hl()
7371{
7372 if (p_hls && !no_hlsearch)
7373 {
7374 last_pat_prog(&search_hl.rm);
7375 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007376# ifdef FEAT_RELTIME
7377 /* Set the time limit to 'redrawtime'. */
7378 profile_setlimit(p_rdt, &search_hl.tm);
7379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381}
7382
7383/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007384 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 */
7386 static void
7387end_search_hl()
7388{
7389 if (search_hl.rm.regprog != NULL)
7390 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007391 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 search_hl.rm.regprog = NULL;
7393 }
7394}
7395
7396/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007397 * Init for calling prepare_search_hl().
7398 */
7399 static void
7400init_search_hl(wp)
7401 win_T *wp;
7402{
7403 matchitem_T *cur;
7404
7405 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7406 * match */
7407 cur = wp->w_match_head;
7408 while (cur != NULL)
7409 {
7410 cur->hl.rm = cur->match;
7411 if (cur->hlg_id == 0)
7412 cur->hl.attr = 0;
7413 else
7414 cur->hl.attr = syn_id2attr(cur->hlg_id);
7415 cur->hl.buf = wp->w_buffer;
7416 cur->hl.lnum = 0;
7417 cur->hl.first_lnum = 0;
7418# ifdef FEAT_RELTIME
7419 /* Set the time limit to 'redrawtime'. */
7420 profile_setlimit(p_rdt, &(cur->hl.tm));
7421# endif
7422 cur = cur->next;
7423 }
7424 search_hl.buf = wp->w_buffer;
7425 search_hl.lnum = 0;
7426 search_hl.first_lnum = 0;
7427 /* time limit is set at the toplevel, for all windows */
7428}
7429
7430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 * Advance to the match in window "wp" line "lnum" or past it.
7432 */
7433 static void
7434prepare_search_hl(wp, lnum)
7435 win_T *wp;
7436 linenr_T lnum;
7437{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007438 matchitem_T *cur; /* points to the match list */
7439 match_T *shl; /* points to search_hl or a match */
7440 int shl_flag; /* flag to indicate whether search_hl
7441 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007442 int pos_inprogress; /* marks that position match search is
7443 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 int n;
7445
7446 /*
7447 * When using a multi-line pattern, start searching at the top
7448 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007449 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007451 cur = wp->w_match_head;
7452 shl_flag = FALSE;
7453 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007455 if (shl_flag == FALSE)
7456 {
7457 shl = &search_hl;
7458 shl_flag = TRUE;
7459 }
7460 else
7461 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 if (shl->rm.regprog != NULL
7463 && shl->lnum == 0
7464 && re_multiline(shl->rm.regprog))
7465 {
7466 if (shl->first_lnum == 0)
7467 {
7468# ifdef FEAT_FOLDING
7469 for (shl->first_lnum = lnum;
7470 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7471 if (hasFoldingWin(wp, shl->first_lnum - 1,
7472 NULL, NULL, TRUE, NULL))
7473 break;
7474# else
7475 shl->first_lnum = wp->w_topline;
7476# endif
7477 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007478 if (cur != NULL)
7479 cur->pos.cur = 0;
7480 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007482 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7483 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007485 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7486 pos_inprogress = cur == NULL || cur->pos.cur == 0
7487 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 if (shl->lnum != 0)
7489 {
7490 shl->first_lnum = shl->lnum
7491 + shl->rm.endpos[0].lnum
7492 - shl->rm.startpos[0].lnum;
7493 n = shl->rm.endpos[0].col;
7494 }
7495 else
7496 {
7497 ++shl->first_lnum;
7498 n = 0;
7499 }
7500 }
7501 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007502 if (shl != &search_hl && cur != NULL)
7503 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
7505}
7506
7507/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007508 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 * Uses shl->buf.
7510 * Sets shl->lnum and shl->rm contents.
7511 * Note: Assumes a previous match is always before "lnum", unless
7512 * shl->lnum is zero.
7513 * Careful: Any pointers for buffer lines will become invalid.
7514 */
7515 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007516next_search_hl(win, shl, lnum, mincol, cur)
7517 win_T *win;
7518 match_T *shl; /* points to search_hl or a match */
7519 linenr_T lnum;
7520 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007521 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 linenr_T l;
7524 colnr_T matchcol;
7525 long nmatched;
7526
7527 if (shl->lnum != 0)
7528 {
7529 /* Check for three situations:
7530 * 1. If the "lnum" is below a previous match, start a new search.
7531 * 2. If the previous match includes "mincol", use it.
7532 * 3. Continue after the previous match.
7533 */
7534 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7535 if (lnum > l)
7536 shl->lnum = 0;
7537 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7538 return;
7539 }
7540
7541 /*
7542 * Repeat searching for a match until one is found that includes "mincol"
7543 * or none is found in this line.
7544 */
7545 called_emsg = FALSE;
7546 for (;;)
7547 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007548#ifdef FEAT_RELTIME
7549 /* Stop searching after passing the time limit. */
7550 if (profile_passed_limit(&(shl->tm)))
7551 {
7552 shl->lnum = 0; /* no match found in time */
7553 break;
7554 }
7555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 /* Three situations:
7557 * 1. No useful previous match: search from start of line.
7558 * 2. Not Vi compatible or empty match: continue at next character.
7559 * Break the loop if this is beyond the end of the line.
7560 * 3. Vi compatible searching: continue at end of previous match.
7561 */
7562 if (shl->lnum == 0)
7563 matchcol = 0;
7564 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7565 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007566 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007568 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007569
7570 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007571 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007572 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007574 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 shl->lnum = 0;
7576 break;
7577 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007578#ifdef FEAT_MBYTE
7579 if (has_mbyte)
7580 matchcol += mb_ptr2len(ml);
7581 else
7582#endif
7583 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 }
7585 else
7586 matchcol = shl->rm.endpos[0].col;
7587
7588 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007589 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007591 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7592 matchcol,
7593#ifdef FEAT_RELTIME
7594 &(shl->tm)
7595#else
7596 NULL
7597#endif
7598 );
7599 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007600 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007601 /* Error while handling regexp: stop using this regexp. */
7602 if (shl == &search_hl)
7603 {
7604 /* don't free regprog in the match list, it's a copy */
7605 vim_regfree(shl->rm.regprog);
7606 SET_NO_HLSEARCH(TRUE);
7607 }
7608 shl->rm.regprog = NULL;
7609 shl->lnum = 0;
7610 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7611 message */
7612 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007613 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007614 }
7615 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007616 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007617 else
7618 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (nmatched == 0)
7620 {
7621 shl->lnum = 0; /* no match found */
7622 break;
7623 }
7624 if (shl->rm.startpos[0].lnum > 0
7625 || shl->rm.startpos[0].col >= mincol
7626 || nmatched > 1
7627 || shl->rm.endpos[0].col > mincol)
7628 {
7629 shl->lnum += shl->rm.startpos[0].lnum;
7630 break; /* useful match found */
7631 }
7632 }
7633}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
Bram Moolenaarb3414592014-06-17 17:48:32 +02007635 static int
7636next_search_hl_pos(shl, lnum, posmatch, mincol)
7637 match_T *shl; /* points to a match */
7638 linenr_T lnum;
7639 posmatch_T *posmatch; /* match positions */
7640 colnr_T mincol; /* minimal column for a match */
7641{
7642 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007643 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007644
7645 shl->lnum = 0;
7646 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7647 {
7648 if (posmatch->pos[i].lnum == 0)
7649 break;
7650 if (posmatch->pos[i].col < mincol)
7651 continue;
7652 if (posmatch->pos[i].lnum == lnum)
7653 {
7654 if (shl->lnum == lnum)
7655 {
7656 /* partially sort positions by column numbers
7657 * on the same line */
7658 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7659 {
7660 llpos_T tmp = posmatch->pos[i];
7661
7662 posmatch->pos[i] = posmatch->pos[bot];
7663 posmatch->pos[bot] = tmp;
7664 }
7665 }
7666 else
7667 {
7668 bot = i;
7669 shl->lnum = lnum;
7670 }
7671 }
7672 }
7673 posmatch->cur = 0;
7674 if (shl->lnum == lnum)
7675 {
7676 colnr_T start = posmatch->pos[bot].col == 0
7677 ? 0 : posmatch->pos[bot].col - 1;
7678 colnr_T end = posmatch->pos[bot].col == 0
7679 ? MAXCOL : start + posmatch->pos[bot].len;
7680
7681 shl->rm.startpos[0].lnum = 0;
7682 shl->rm.startpos[0].col = start;
7683 shl->rm.endpos[0].lnum = 0;
7684 shl->rm.endpos[0].col = end;
7685 posmatch->cur = bot + 1;
7686 return TRUE;
7687 }
7688 return FALSE;
7689}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007690#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007691
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 static void
7693screen_start_highlight(attr)
7694 int attr;
7695{
7696 attrentry_T *aep = NULL;
7697
7698 screen_attr = attr;
7699 if (full_screen
7700#ifdef WIN3264
7701 && termcap_active
7702#endif
7703 )
7704 {
7705#ifdef FEAT_GUI
7706 if (gui.in_use)
7707 {
7708 char buf[20];
7709
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007710 /* The GUI handles this internally. */
7711 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 OUT_STR(buf);
7713 }
7714 else
7715#endif
7716 {
7717 if (attr > HL_ALL) /* special HL attr. */
7718 {
7719 if (t_colors > 1)
7720 aep = syn_cterm_attr2entry(attr);
7721 else
7722 aep = syn_term_attr2entry(attr);
7723 if (aep == NULL) /* did ":syntax clear" */
7724 attr = 0;
7725 else
7726 attr = aep->ae_attr;
7727 }
7728 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7729 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007730 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7731 && cterm_normal_fg_bold)
7732 /* If the Normal FG color has BOLD attribute and the new HL
7733 * has a FG color defined, clear BOLD. */
7734 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7736 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007737 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7738 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 out_str(T_US);
7740 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7741 out_str(T_CZH);
7742 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7743 out_str(T_MR);
7744
7745 /*
7746 * Output the color or start string after bold etc., in case the
7747 * bold etc. override the color setting.
7748 */
7749 if (aep != NULL)
7750 {
7751 if (t_colors > 1)
7752 {
7753 if (aep->ae_u.cterm.fg_color)
7754 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7755 if (aep->ae_u.cterm.bg_color)
7756 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7757 }
7758 else
7759 {
7760 if (aep->ae_u.term.start != NULL)
7761 out_str(aep->ae_u.term.start);
7762 }
7763 }
7764 }
7765 }
7766}
7767
7768 void
7769screen_stop_highlight()
7770{
7771 int do_ME = FALSE; /* output T_ME code */
7772
7773 if (screen_attr != 0
7774#ifdef WIN3264
7775 && termcap_active
7776#endif
7777 )
7778 {
7779#ifdef FEAT_GUI
7780 if (gui.in_use)
7781 {
7782 char buf[20];
7783
7784 /* use internal GUI code */
7785 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7786 OUT_STR(buf);
7787 }
7788 else
7789#endif
7790 {
7791 if (screen_attr > HL_ALL) /* special HL attr. */
7792 {
7793 attrentry_T *aep;
7794
7795 if (t_colors > 1)
7796 {
7797 /*
7798 * Assume that t_me restores the original colors!
7799 */
7800 aep = syn_cterm_attr2entry(screen_attr);
7801 if (aep != NULL && (aep->ae_u.cterm.fg_color
7802 || aep->ae_u.cterm.bg_color))
7803 do_ME = TRUE;
7804 }
7805 else
7806 {
7807 aep = syn_term_attr2entry(screen_attr);
7808 if (aep != NULL && aep->ae_u.term.stop != NULL)
7809 {
7810 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7811 do_ME = TRUE;
7812 else
7813 out_str(aep->ae_u.term.stop);
7814 }
7815 }
7816 if (aep == NULL) /* did ":syntax clear" */
7817 screen_attr = 0;
7818 else
7819 screen_attr = aep->ae_attr;
7820 }
7821
7822 /*
7823 * Often all ending-codes are equal to T_ME. Avoid outputting the
7824 * same sequence several times.
7825 */
7826 if (screen_attr & HL_STANDOUT)
7827 {
7828 if (STRCMP(T_SE, T_ME) == 0)
7829 do_ME = TRUE;
7830 else
7831 out_str(T_SE);
7832 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007833 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {
7835 if (STRCMP(T_UE, T_ME) == 0)
7836 do_ME = TRUE;
7837 else
7838 out_str(T_UE);
7839 }
7840 if (screen_attr & HL_ITALIC)
7841 {
7842 if (STRCMP(T_CZR, T_ME) == 0)
7843 do_ME = TRUE;
7844 else
7845 out_str(T_CZR);
7846 }
7847 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7848 out_str(T_ME);
7849
7850 if (t_colors > 1)
7851 {
7852 /* set Normal cterm colors */
7853 if (cterm_normal_fg_color != 0)
7854 term_fg_color(cterm_normal_fg_color - 1);
7855 if (cterm_normal_bg_color != 0)
7856 term_bg_color(cterm_normal_bg_color - 1);
7857 if (cterm_normal_fg_bold)
7858 out_str(T_MD);
7859 }
7860 }
7861 }
7862 screen_attr = 0;
7863}
7864
7865/*
7866 * Reset the colors for a cterm. Used when leaving Vim.
7867 * The machine specific code may override this again.
7868 */
7869 void
7870reset_cterm_colors()
7871{
7872 if (t_colors > 1)
7873 {
7874 /* set Normal cterm colors */
7875 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7876 {
7877 out_str(T_OP);
7878 screen_attr = -1;
7879 }
7880 if (cterm_normal_fg_bold)
7881 {
7882 out_str(T_ME);
7883 screen_attr = -1;
7884 }
7885 }
7886}
7887
7888/*
7889 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7890 * using the attributes from ScreenAttrs["off"].
7891 */
7892 static void
7893screen_char(off, row, col)
7894 unsigned off;
7895 int row;
7896 int col;
7897{
7898 int attr;
7899
7900 /* Check for illegal values, just in case (could happen just after
7901 * resizing). */
7902 if (row >= screen_Rows || col >= screen_Columns)
7903 return;
7904
7905 /* Outputting the last character on the screen may scrollup the screen.
7906 * Don't to it! Mark the character invalid (update it when scrolled up) */
7907 if (row == screen_Rows - 1 && col == screen_Columns - 1
7908#ifdef FEAT_RIGHTLEFT
7909 /* account for first command-line character in rightleft mode */
7910 && !cmdmsg_rl
7911#endif
7912 )
7913 {
7914 ScreenAttrs[off] = (sattr_T)-1;
7915 return;
7916 }
7917
7918 /*
7919 * Stop highlighting first, so it's easier to move the cursor.
7920 */
7921#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7922 if (screen_char_attr != 0)
7923 attr = screen_char_attr;
7924 else
7925#endif
7926 attr = ScreenAttrs[off];
7927 if (screen_attr != attr)
7928 screen_stop_highlight();
7929
7930 windgoto(row, col);
7931
7932 if (screen_attr != attr)
7933 screen_start_highlight(attr);
7934
7935#ifdef FEAT_MBYTE
7936 if (enc_utf8 && ScreenLinesUC[off] != 0)
7937 {
7938 char_u buf[MB_MAXBYTES + 1];
7939
7940 /* Convert UTF-8 character to bytes and write it. */
7941
7942 buf[utfc_char2bytes(off, buf)] = NUL;
7943
7944 out_str(buf);
7945 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7946 ++screen_cur_col;
7947 }
7948 else
7949#endif
7950 {
7951#ifdef FEAT_MBYTE
7952 out_flush_check();
7953#endif
7954 out_char(ScreenLines[off]);
7955#ifdef FEAT_MBYTE
7956 /* double-byte character in single-width cell */
7957 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7958 out_char(ScreenLines2[off]);
7959#endif
7960 }
7961
7962 screen_cur_col++;
7963}
7964
7965#ifdef FEAT_MBYTE
7966
7967/*
7968 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7969 * on the screen at position 'row' and 'col'.
7970 * The attributes of the first byte is used for all. This is required to
7971 * output the two bytes of a double-byte character with nothing in between.
7972 */
7973 static void
7974screen_char_2(off, row, col)
7975 unsigned off;
7976 int row;
7977 int col;
7978{
7979 /* Check for illegal values (could be wrong when screen was resized). */
7980 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7981 return;
7982
7983 /* Outputting the last character on the screen may scrollup the screen.
7984 * Don't to it! Mark the character invalid (update it when scrolled up) */
7985 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7986 {
7987 ScreenAttrs[off] = (sattr_T)-1;
7988 return;
7989 }
7990
7991 /* Output the first byte normally (positions the cursor), then write the
7992 * second byte directly. */
7993 screen_char(off, row, col);
7994 out_char(ScreenLines[off + 1]);
7995 ++screen_cur_col;
7996}
7997#endif
7998
7999#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8000/*
8001 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8002 * This uses the contents of ScreenLines[] and doesn't change it.
8003 */
8004 void
8005screen_draw_rectangle(row, col, height, width, invert)
8006 int row;
8007 int col;
8008 int height;
8009 int width;
8010 int invert;
8011{
8012 int r, c;
8013 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008014#ifdef FEAT_MBYTE
8015 int max_off;
8016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008018 /* Can't use ScreenLines unless initialized */
8019 if (ScreenLines == NULL)
8020 return;
8021
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 if (invert)
8023 screen_char_attr = HL_INVERSE;
8024 for (r = row; r < row + height; ++r)
8025 {
8026 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008027#ifdef FEAT_MBYTE
8028 max_off = off + screen_Columns;
8029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 for (c = col; c < col + width; ++c)
8031 {
8032#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008033 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 {
8035 screen_char_2(off + c, r, c);
8036 ++c;
8037 }
8038 else
8039#endif
8040 {
8041 screen_char(off + c, r, c);
8042#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008043 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 ++c;
8045#endif
8046 }
8047 }
8048 }
8049 screen_char_attr = 0;
8050}
8051#endif
8052
8053#ifdef FEAT_VERTSPLIT
8054/*
8055 * Redraw the characters for a vertically split window.
8056 */
8057 static void
8058redraw_block(row, end, wp)
8059 int row;
8060 int end;
8061 win_T *wp;
8062{
8063 int col;
8064 int width;
8065
8066# ifdef FEAT_CLIPBOARD
8067 clip_may_clear_selection(row, end - 1);
8068# endif
8069
8070 if (wp == NULL)
8071 {
8072 col = 0;
8073 width = Columns;
8074 }
8075 else
8076 {
8077 col = wp->w_wincol;
8078 width = wp->w_width;
8079 }
8080 screen_draw_rectangle(row, col, end - row, width, FALSE);
8081}
8082#endif
8083
8084/*
8085 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8086 * with character 'c1' in first column followed by 'c2' in the other columns.
8087 * Use attributes 'attr'.
8088 */
8089 void
8090screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8091 int start_row, end_row;
8092 int start_col, end_col;
8093 int c1, c2;
8094 int attr;
8095{
8096 int row;
8097 int col;
8098 int off;
8099 int end_off;
8100 int did_delete;
8101 int c;
8102 int norm_term;
8103#if defined(FEAT_GUI) || defined(UNIX)
8104 int force_next = FALSE;
8105#endif
8106
8107 if (end_row > screen_Rows) /* safety check */
8108 end_row = screen_Rows;
8109 if (end_col > screen_Columns) /* safety check */
8110 end_col = screen_Columns;
8111 if (ScreenLines == NULL
8112 || start_row >= end_row
8113 || start_col >= end_col) /* nothing to do */
8114 return;
8115
8116 /* it's a "normal" terminal when not in a GUI or cterm */
8117 norm_term = (
8118#ifdef FEAT_GUI
8119 !gui.in_use &&
8120#endif
8121 t_colors <= 1);
8122 for (row = start_row; row < end_row; ++row)
8123 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008124#ifdef FEAT_MBYTE
8125 if (has_mbyte
8126# ifdef FEAT_GUI
8127 && !gui.in_use
8128# endif
8129 )
8130 {
8131 /* When drawing over the right halve of a double-wide char clear
8132 * out the left halve. When drawing over the left halve of a
8133 * double wide-char clear out the right halve. Only needed in a
8134 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008135 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008136 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008137 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008138 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008139 }
8140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 /*
8142 * Try to use delete-line termcap code, when no attributes or in a
8143 * "normal" terminal, where a bold/italic space is just a
8144 * space.
8145 */
8146 did_delete = FALSE;
8147 if (c2 == ' '
8148 && end_col == Columns
8149 && can_clear(T_CE)
8150 && (attr == 0
8151 || (norm_term
8152 && attr <= HL_ALL
8153 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8154 {
8155 /*
8156 * check if we really need to clear something
8157 */
8158 col = start_col;
8159 if (c1 != ' ') /* don't clear first char */
8160 ++col;
8161
8162 off = LineOffset[row] + col;
8163 end_off = LineOffset[row] + end_col;
8164
8165 /* skip blanks (used often, keep it fast!) */
8166#ifdef FEAT_MBYTE
8167 if (enc_utf8)
8168 while (off < end_off && ScreenLines[off] == ' '
8169 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8170 ++off;
8171 else
8172#endif
8173 while (off < end_off && ScreenLines[off] == ' '
8174 && ScreenAttrs[off] == 0)
8175 ++off;
8176 if (off < end_off) /* something to be cleared */
8177 {
8178 col = off - LineOffset[row];
8179 screen_stop_highlight();
8180 term_windgoto(row, col);/* clear rest of this screen line */
8181 out_str(T_CE);
8182 screen_start(); /* don't know where cursor is now */
8183 col = end_col - col;
8184 while (col--) /* clear chars in ScreenLines */
8185 {
8186 ScreenLines[off] = ' ';
8187#ifdef FEAT_MBYTE
8188 if (enc_utf8)
8189 ScreenLinesUC[off] = 0;
8190#endif
8191 ScreenAttrs[off] = 0;
8192 ++off;
8193 }
8194 }
8195 did_delete = TRUE; /* the chars are cleared now */
8196 }
8197
8198 off = LineOffset[row] + start_col;
8199 c = c1;
8200 for (col = start_col; col < end_col; ++col)
8201 {
8202 if (ScreenLines[off] != c
8203#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008204 || (enc_utf8 && (int)ScreenLinesUC[off]
8205 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206#endif
8207 || ScreenAttrs[off] != attr
8208#if defined(FEAT_GUI) || defined(UNIX)
8209 || force_next
8210#endif
8211 )
8212 {
8213#if defined(FEAT_GUI) || defined(UNIX)
8214 /* The bold trick may make a single row of pixels appear in
8215 * the next character. When a bold character is removed, the
8216 * next character should be redrawn too. This happens for our
8217 * own GUI and for some xterms. */
8218 if (
8219# ifdef FEAT_GUI
8220 gui.in_use
8221# endif
8222# if defined(FEAT_GUI) && defined(UNIX)
8223 ||
8224# endif
8225# ifdef UNIX
8226 term_is_xterm
8227# endif
8228 )
8229 {
8230 if (ScreenLines[off] != ' '
8231 && (ScreenAttrs[off] > HL_ALL
8232 || ScreenAttrs[off] & HL_BOLD))
8233 force_next = TRUE;
8234 else
8235 force_next = FALSE;
8236 }
8237#endif
8238 ScreenLines[off] = c;
8239#ifdef FEAT_MBYTE
8240 if (enc_utf8)
8241 {
8242 if (c >= 0x80)
8243 {
8244 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008245 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 }
8247 else
8248 ScreenLinesUC[off] = 0;
8249 }
8250#endif
8251 ScreenAttrs[off] = attr;
8252 if (!did_delete || c != ' ')
8253 screen_char(off, row, col);
8254 }
8255 ++off;
8256 if (col == start_col)
8257 {
8258 if (did_delete)
8259 break;
8260 c = c2;
8261 }
8262 }
8263 if (end_col == Columns)
8264 LineWraps[row] = FALSE;
8265 if (row == Rows - 1) /* overwritten the command line */
8266 {
8267 redraw_cmdline = TRUE;
8268 if (c1 == ' ' && c2 == ' ')
8269 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008270 if (start_col == 0)
8271 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 }
8273 }
8274}
8275
8276/*
8277 * Check if there should be a delay. Used before clearing or redrawing the
8278 * screen or the command line.
8279 */
8280 void
8281check_for_delay(check_msg_scroll)
8282 int check_msg_scroll;
8283{
8284 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8285 && !did_wait_return
8286 && emsg_silent == 0)
8287 {
8288 out_flush();
8289 ui_delay(1000L, TRUE);
8290 emsg_on_display = FALSE;
8291 if (check_msg_scroll)
8292 msg_scroll = FALSE;
8293 }
8294}
8295
8296/*
8297 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008298 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 * Returns TRUE if there is a valid screen to write to.
8300 * Returns FALSE when starting up and screen not initialized yet.
8301 */
8302 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008303screen_valid(doclear)
8304 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008306 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 return (ScreenLines != NULL);
8308}
8309
8310/*
8311 * Resize the shell to Rows and Columns.
8312 * Allocate ScreenLines[] and associated items.
8313 *
8314 * There may be some time between setting Rows and Columns and (re)allocating
8315 * ScreenLines[]. This happens when starting up and when (manually) changing
8316 * the shell size. Always use screen_Rows and screen_Columns to access items
8317 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8318 * final size of the shell is needed.
8319 */
8320 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008321screenalloc(doclear)
8322 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323{
8324 int new_row, old_row;
8325#ifdef FEAT_GUI
8326 int old_Rows;
8327#endif
8328 win_T *wp;
8329 int outofmem = FALSE;
8330 int len;
8331 schar_T *new_ScreenLines;
8332#ifdef FEAT_MBYTE
8333 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008334 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008336 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337#endif
8338 sattr_T *new_ScreenAttrs;
8339 unsigned *new_LineOffset;
8340 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008341#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008342 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008343 tabpage_T *tp;
8344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008346 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008347#ifdef FEAT_AUTOCMD
8348 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008350retry:
8351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 /*
8353 * Allocation of the screen buffers is done only when the size changes and
8354 * when Rows and Columns have been set and we have started doing full
8355 * screen stuff.
8356 */
8357 if ((ScreenLines != NULL
8358 && Rows == screen_Rows
8359 && Columns == screen_Columns
8360#ifdef FEAT_MBYTE
8361 && enc_utf8 == (ScreenLinesUC != NULL)
8362 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008363 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364#endif
8365 )
8366 || Rows == 0
8367 || Columns == 0
8368 || (!full_screen && ScreenLines == NULL))
8369 return;
8370
8371 /*
8372 * It's possible that we produce an out-of-memory message below, which
8373 * will cause this function to be called again. To break the loop, just
8374 * return here.
8375 */
8376 if (entered)
8377 return;
8378 entered = TRUE;
8379
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008380 /*
8381 * Note that the window sizes are updated before reallocating the arrays,
8382 * thus we must not redraw here!
8383 */
8384 ++RedrawingDisabled;
8385
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 win_new_shellsize(); /* fit the windows in the new sized shell */
8387
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 comp_col(); /* recompute columns for shown command and ruler */
8389
8390 /*
8391 * We're changing the size of the screen.
8392 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8393 * - Move lines from the old arrays into the new arrays, clear extra
8394 * lines (unless the screen is going to be cleared).
8395 * - Free the old arrays.
8396 *
8397 * If anything fails, make ScreenLines NULL, so we don't do anything!
8398 * Continuing with the old ScreenLines may result in a crash, because the
8399 * size is wrong.
8400 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008401 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008403#ifdef FEAT_AUTOCMD
8404 if (aucmd_win != NULL)
8405 win_free_lsize(aucmd_win);
8406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407
8408 new_ScreenLines = (schar_T *)lalloc((long_u)(
8409 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8410#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008411 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 if (enc_utf8)
8413 {
8414 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8415 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008416 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008417 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8419 }
8420 if (enc_dbcs == DBCS_JPNU)
8421 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8422 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8423#endif
8424 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8425 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8426 new_LineOffset = (unsigned *)lalloc((long_u)(
8427 Rows * sizeof(unsigned)), FALSE);
8428 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008429#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008430 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008433 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 {
8435 if (win_alloc_lines(wp) == FAIL)
8436 {
8437 outofmem = TRUE;
8438#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008439 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440#endif
8441 }
8442 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008443#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008444 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8445 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008446 outofmem = TRUE;
8447#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008448#ifdef FEAT_WINDOWS
8449give_up:
8450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008452#ifdef FEAT_MBYTE
8453 for (i = 0; i < p_mco; ++i)
8454 if (new_ScreenLinesC[i] == NULL)
8455 break;
8456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 if (new_ScreenLines == NULL
8458#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008459 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8461#endif
8462 || new_ScreenAttrs == NULL
8463 || new_LineOffset == NULL
8464 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008465#ifdef FEAT_WINDOWS
8466 || new_TabPageIdxs == NULL
8467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 || outofmem)
8469 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008470 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008471 {
8472 /* guess the size */
8473 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8474
8475 /* Remember we did this to avoid getting outofmem messages over
8476 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008477 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 vim_free(new_ScreenLines);
8480 new_ScreenLines = NULL;
8481#ifdef FEAT_MBYTE
8482 vim_free(new_ScreenLinesUC);
8483 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008484 for (i = 0; i < p_mco; ++i)
8485 {
8486 vim_free(new_ScreenLinesC[i]);
8487 new_ScreenLinesC[i] = NULL;
8488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 vim_free(new_ScreenLines2);
8490 new_ScreenLines2 = NULL;
8491#endif
8492 vim_free(new_ScreenAttrs);
8493 new_ScreenAttrs = NULL;
8494 vim_free(new_LineOffset);
8495 new_LineOffset = NULL;
8496 vim_free(new_LineWraps);
8497 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008498#ifdef FEAT_WINDOWS
8499 vim_free(new_TabPageIdxs);
8500 new_TabPageIdxs = NULL;
8501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 }
8503 else
8504 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008505 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008506
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 for (new_row = 0; new_row < Rows; ++new_row)
8508 {
8509 new_LineOffset[new_row] = new_row * Columns;
8510 new_LineWraps[new_row] = FALSE;
8511
8512 /*
8513 * If the screen is not going to be cleared, copy as much as
8514 * possible from the old screen to the new one and clear the rest
8515 * (used when resizing the window at the "--more--" prompt or when
8516 * executing an external command, for the GUI).
8517 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008518 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {
8520 (void)vim_memset(new_ScreenLines + new_row * Columns,
8521 ' ', (size_t)Columns * sizeof(schar_T));
8522#ifdef FEAT_MBYTE
8523 if (enc_utf8)
8524 {
8525 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8526 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008527 for (i = 0; i < p_mco; ++i)
8528 (void)vim_memset(new_ScreenLinesC[i]
8529 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 0, (size_t)Columns * sizeof(u8char_T));
8531 }
8532 if (enc_dbcs == DBCS_JPNU)
8533 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8534 0, (size_t)Columns * sizeof(schar_T));
8535#endif
8536 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8537 0, (size_t)Columns * sizeof(sattr_T));
8538 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008539 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {
8541 if (screen_Columns < Columns)
8542 len = screen_Columns;
8543 else
8544 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008545#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008546 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008547 * may be invalid now. Also when p_mco changes. */
8548 if (!(enc_utf8 && ScreenLinesUC == NULL)
8549 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008550#endif
8551 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8552 ScreenLines + LineOffset[old_row],
8553 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008555 if (enc_utf8 && ScreenLinesUC != NULL
8556 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {
8558 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8559 ScreenLinesUC + LineOffset[old_row],
8560 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008561 for (i = 0; i < p_mco; ++i)
8562 mch_memmove(new_ScreenLinesC[i]
8563 + new_LineOffset[new_row],
8564 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 (size_t)len * sizeof(u8char_T));
8566 }
8567 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8568 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8569 ScreenLines2 + LineOffset[old_row],
8570 (size_t)len * sizeof(schar_T));
8571#endif
8572 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8573 ScreenAttrs + LineOffset[old_row],
8574 (size_t)len * sizeof(sattr_T));
8575 }
8576 }
8577 }
8578 /* Use the last line of the screen for the current line. */
8579 current_ScreenLine = new_ScreenLines + Rows * Columns;
8580 }
8581
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008582 free_screenlines();
8583
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 ScreenLines = new_ScreenLines;
8585#ifdef FEAT_MBYTE
8586 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008587 for (i = 0; i < p_mco; ++i)
8588 ScreenLinesC[i] = new_ScreenLinesC[i];
8589 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 ScreenLines2 = new_ScreenLines2;
8591#endif
8592 ScreenAttrs = new_ScreenAttrs;
8593 LineOffset = new_LineOffset;
8594 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008595#ifdef FEAT_WINDOWS
8596 TabPageIdxs = new_TabPageIdxs;
8597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598
8599 /* It's important that screen_Rows and screen_Columns reflect the actual
8600 * size of ScreenLines[]. Set them before calling anything. */
8601#ifdef FEAT_GUI
8602 old_Rows = screen_Rows;
8603#endif
8604 screen_Rows = Rows;
8605 screen_Columns = Columns;
8606
8607 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008608 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 screenclear2();
8610
8611#ifdef FEAT_GUI
8612 else if (gui.in_use
8613 && !gui.starting
8614 && ScreenLines != NULL
8615 && old_Rows != Rows)
8616 {
8617 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8618 /*
8619 * Adjust the position of the cursor, for when executing an external
8620 * command.
8621 */
8622 if (msg_row >= Rows) /* Rows got smaller */
8623 msg_row = Rows - 1; /* put cursor at last row */
8624 else if (Rows > old_Rows) /* Rows got bigger */
8625 msg_row += Rows - old_Rows; /* put cursor in same place */
8626 if (msg_col >= Columns) /* Columns got smaller */
8627 msg_col = Columns - 1; /* put cursor at last column */
8628 }
8629#endif
8630
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008632 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008633
8634#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008635 /*
8636 * Do not apply autocommands more than 3 times to avoid an endless loop
8637 * in case applying autocommands always changes Rows or Columns.
8638 */
8639 if (starting == 0 && ++retry_count <= 3)
8640 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008641 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008642 /* In rare cases, autocommands may have altered Rows or Columns,
8643 * jump back to check if we need to allocate the screen again. */
8644 goto retry;
8645 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647}
8648
8649 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008650free_screenlines()
8651{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008652#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008653 int i;
8654
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008655 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008656 for (i = 0; i < Screen_mco; ++i)
8657 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008658 vim_free(ScreenLines2);
8659#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008660 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008661 vim_free(ScreenAttrs);
8662 vim_free(LineOffset);
8663 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008664#ifdef FEAT_WINDOWS
8665 vim_free(TabPageIdxs);
8666#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008667}
8668
8669 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670screenclear()
8671{
8672 check_for_delay(FALSE);
8673 screenalloc(FALSE); /* allocate screen buffers if size changed */
8674 screenclear2(); /* clear the screen */
8675}
8676
8677 static void
8678screenclear2()
8679{
8680 int i;
8681
8682 if (starting == NO_SCREEN || ScreenLines == NULL
8683#ifdef FEAT_GUI
8684 || (gui.in_use && gui.starting)
8685#endif
8686 )
8687 return;
8688
8689#ifdef FEAT_GUI
8690 if (!gui.in_use)
8691#endif
8692 screen_attr = -1; /* force setting the Normal colors */
8693 screen_stop_highlight(); /* don't want highlighting here */
8694
8695#ifdef FEAT_CLIPBOARD
8696 /* disable selection without redrawing it */
8697 clip_scroll_selection(9999);
8698#endif
8699
8700 /* blank out ScreenLines */
8701 for (i = 0; i < Rows; ++i)
8702 {
8703 lineclear(LineOffset[i], (int)Columns);
8704 LineWraps[i] = FALSE;
8705 }
8706
8707 if (can_clear(T_CL))
8708 {
8709 out_str(T_CL); /* clear the display */
8710 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008711 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
8713 else
8714 {
8715 /* can't clear the screen, mark all chars with invalid attributes */
8716 for (i = 0; i < Rows; ++i)
8717 lineinvalid(LineOffset[i], (int)Columns);
8718 clear_cmdline = TRUE;
8719 }
8720
8721 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8722
8723 win_rest_invalid(firstwin);
8724 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008725#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008726 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 if (must_redraw == CLEAR) /* no need to clear again */
8729 must_redraw = NOT_VALID;
8730 compute_cmdrow();
8731 msg_row = cmdline_row; /* put cursor on last line for messages */
8732 msg_col = 0;
8733 screen_start(); /* don't know where cursor is now */
8734 msg_scrolled = 0; /* can't scroll back */
8735 msg_didany = FALSE;
8736 msg_didout = FALSE;
8737}
8738
8739/*
8740 * Clear one line in ScreenLines.
8741 */
8742 static void
8743lineclear(off, width)
8744 unsigned off;
8745 int width;
8746{
8747 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8748#ifdef FEAT_MBYTE
8749 if (enc_utf8)
8750 (void)vim_memset(ScreenLinesUC + off, 0,
8751 (size_t)width * sizeof(u8char_T));
8752#endif
8753 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8754}
8755
8756/*
8757 * Mark one line in ScreenLines invalid by setting the attributes to an
8758 * invalid value.
8759 */
8760 static void
8761lineinvalid(off, width)
8762 unsigned off;
8763 int width;
8764{
8765 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8766}
8767
8768#ifdef FEAT_VERTSPLIT
8769/*
8770 * Copy part of a Screenline for vertically split window "wp".
8771 */
8772 static void
8773linecopy(to, from, wp)
8774 int to;
8775 int from;
8776 win_T *wp;
8777{
8778 unsigned off_to = LineOffset[to] + wp->w_wincol;
8779 unsigned off_from = LineOffset[from] + wp->w_wincol;
8780
8781 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8782 wp->w_width * sizeof(schar_T));
8783# ifdef FEAT_MBYTE
8784 if (enc_utf8)
8785 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008786 int i;
8787
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8789 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008790 for (i = 0; i < p_mco; ++i)
8791 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8792 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 }
8794 if (enc_dbcs == DBCS_JPNU)
8795 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8796 wp->w_width * sizeof(schar_T));
8797# endif
8798 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8799 wp->w_width * sizeof(sattr_T));
8800}
8801#endif
8802
8803/*
8804 * Return TRUE if clearing with term string "p" would work.
8805 * It can't work when the string is empty or it won't set the right background.
8806 */
8807 int
8808can_clear(p)
8809 char_u *p;
8810{
8811 return (*p != NUL && (t_colors <= 1
8812#ifdef FEAT_GUI
8813 || gui.in_use
8814#endif
8815 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8816}
8817
8818/*
8819 * Reset cursor position. Use whenever cursor was moved because of outputting
8820 * something directly to the screen (shell commands) or a terminal control
8821 * code.
8822 */
8823 void
8824screen_start()
8825{
8826 screen_cur_row = screen_cur_col = 9999;
8827}
8828
8829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 * Move the cursor to position "row","col" in the screen.
8831 * This tries to find the most efficient way to move, minimizing the number of
8832 * characters sent to the terminal.
8833 */
8834 void
8835windgoto(row, col)
8836 int row;
8837 int col;
8838{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008839 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 int i;
8841 int plan;
8842 int cost;
8843 int wouldbe_col;
8844 int noinvcurs;
8845 char_u *bs;
8846 int goto_cost;
8847 int attr;
8848
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008849#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8851
8852#define PLAN_LE 1
8853#define PLAN_CR 2
8854#define PLAN_NL 3
8855#define PLAN_WRITE 4
8856 /* Can't use ScreenLines unless initialized */
8857 if (ScreenLines == NULL)
8858 return;
8859
8860 if (col != screen_cur_col || row != screen_cur_row)
8861 {
8862 /* Check for valid position. */
8863 if (row < 0) /* window without text lines? */
8864 row = 0;
8865 if (row >= screen_Rows)
8866 row = screen_Rows - 1;
8867 if (col >= screen_Columns)
8868 col = screen_Columns - 1;
8869
8870 /* check if no cursor movement is allowed in highlight mode */
8871 if (screen_attr && *T_MS == NUL)
8872 noinvcurs = HIGHL_COST;
8873 else
8874 noinvcurs = 0;
8875 goto_cost = GOTO_COST + noinvcurs;
8876
8877 /*
8878 * Plan how to do the positioning:
8879 * 1. Use CR to move it to column 0, same row.
8880 * 2. Use T_LE to move it a few columns to the left.
8881 * 3. Use NL to move a few lines down, column 0.
8882 * 4. Move a few columns to the right with T_ND or by writing chars.
8883 *
8884 * Don't do this if the cursor went beyond the last column, the cursor
8885 * position is unknown then (some terminals wrap, some don't )
8886 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008887 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 * characters to move the cursor to the right.
8889 */
8890 if (row >= screen_cur_row && screen_cur_col < Columns)
8891 {
8892 /*
8893 * If the cursor is in the same row, bigger col, we can use CR
8894 * or T_LE.
8895 */
8896 bs = NULL; /* init for GCC */
8897 attr = screen_attr;
8898 if (row == screen_cur_row && col < screen_cur_col)
8899 {
8900 /* "le" is preferred over "bc", because "bc" is obsolete */
8901 if (*T_LE)
8902 bs = T_LE; /* "cursor left" */
8903 else
8904 bs = T_BC; /* "backspace character (old) */
8905 if (*bs)
8906 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8907 else
8908 cost = 999;
8909 if (col + 1 < cost) /* using CR is less characters */
8910 {
8911 plan = PLAN_CR;
8912 wouldbe_col = 0;
8913 cost = 1; /* CR is just one character */
8914 }
8915 else
8916 {
8917 plan = PLAN_LE;
8918 wouldbe_col = col;
8919 }
8920 if (noinvcurs) /* will stop highlighting */
8921 {
8922 cost += noinvcurs;
8923 attr = 0;
8924 }
8925 }
8926
8927 /*
8928 * If the cursor is above where we want to be, we can use CR LF.
8929 */
8930 else if (row > screen_cur_row)
8931 {
8932 plan = PLAN_NL;
8933 wouldbe_col = 0;
8934 cost = (row - screen_cur_row) * 2; /* CR LF */
8935 if (noinvcurs) /* will stop highlighting */
8936 {
8937 cost += noinvcurs;
8938 attr = 0;
8939 }
8940 }
8941
8942 /*
8943 * If the cursor is in the same row, smaller col, just use write.
8944 */
8945 else
8946 {
8947 plan = PLAN_WRITE;
8948 wouldbe_col = screen_cur_col;
8949 cost = 0;
8950 }
8951
8952 /*
8953 * Check if any characters that need to be written have the
8954 * correct attributes. Also avoid UTF-8 characters.
8955 */
8956 i = col - wouldbe_col;
8957 if (i > 0)
8958 cost += i;
8959 if (cost < goto_cost && i > 0)
8960 {
8961 /*
8962 * Check if the attributes are correct without additionally
8963 * stopping highlighting.
8964 */
8965 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8966 while (i && *p++ == attr)
8967 --i;
8968 if (i != 0)
8969 {
8970 /*
8971 * Try if it works when highlighting is stopped here.
8972 */
8973 if (*--p == 0)
8974 {
8975 cost += noinvcurs;
8976 while (i && *p++ == 0)
8977 --i;
8978 }
8979 if (i != 0)
8980 cost = 999; /* different attributes, don't do it */
8981 }
8982#ifdef FEAT_MBYTE
8983 if (enc_utf8)
8984 {
8985 /* Don't use an UTF-8 char for positioning, it's slow. */
8986 for (i = wouldbe_col; i < col; ++i)
8987 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8988 {
8989 cost = 999;
8990 break;
8991 }
8992 }
8993#endif
8994 }
8995
8996 /*
8997 * We can do it without term_windgoto()!
8998 */
8999 if (cost < goto_cost)
9000 {
9001 if (plan == PLAN_LE)
9002 {
9003 if (noinvcurs)
9004 screen_stop_highlight();
9005 while (screen_cur_col > col)
9006 {
9007 out_str(bs);
9008 --screen_cur_col;
9009 }
9010 }
9011 else if (plan == PLAN_CR)
9012 {
9013 if (noinvcurs)
9014 screen_stop_highlight();
9015 out_char('\r');
9016 screen_cur_col = 0;
9017 }
9018 else if (plan == PLAN_NL)
9019 {
9020 if (noinvcurs)
9021 screen_stop_highlight();
9022 while (screen_cur_row < row)
9023 {
9024 out_char('\n');
9025 ++screen_cur_row;
9026 }
9027 screen_cur_col = 0;
9028 }
9029
9030 i = col - screen_cur_col;
9031 if (i > 0)
9032 {
9033 /*
9034 * Use cursor-right if it's one character only. Avoids
9035 * removing a line of pixels from the last bold char, when
9036 * using the bold trick in the GUI.
9037 */
9038 if (T_ND[0] != NUL && T_ND[1] == NUL)
9039 {
9040 while (i-- > 0)
9041 out_char(*T_ND);
9042 }
9043 else
9044 {
9045 int off;
9046
9047 off = LineOffset[row] + screen_cur_col;
9048 while (i-- > 0)
9049 {
9050 if (ScreenAttrs[off] != screen_attr)
9051 screen_stop_highlight();
9052#ifdef FEAT_MBYTE
9053 out_flush_check();
9054#endif
9055 out_char(ScreenLines[off]);
9056#ifdef FEAT_MBYTE
9057 if (enc_dbcs == DBCS_JPNU
9058 && ScreenLines[off] == 0x8e)
9059 out_char(ScreenLines2[off]);
9060#endif
9061 ++off;
9062 }
9063 }
9064 }
9065 }
9066 }
9067 else
9068 cost = 999;
9069
9070 if (cost >= goto_cost)
9071 {
9072 if (noinvcurs)
9073 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009074 if (row == screen_cur_row && (col > screen_cur_col)
9075 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 term_cursor_right(col - screen_cur_col);
9077 else
9078 term_windgoto(row, col);
9079 }
9080 screen_cur_row = row;
9081 screen_cur_col = col;
9082 }
9083}
9084
9085/*
9086 * Set cursor to its position in the current window.
9087 */
9088 void
9089setcursor()
9090{
9091 if (redrawing())
9092 {
9093 validate_cursor();
9094 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9095 W_WINCOL(curwin) + (
9096#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009097 /* With 'rightleft' set and the cursor on a double-wide
9098 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9100# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009101 (has_mbyte
9102 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9103 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104# endif
9105 1)) :
9106#endif
9107 curwin->w_wcol));
9108 }
9109}
9110
9111
9112/*
9113 * insert 'line_count' lines at 'row' in window 'wp'
9114 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9115 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9116 * scrolling.
9117 * Returns FAIL if the lines are not inserted, OK for success.
9118 */
9119 int
9120win_ins_lines(wp, row, line_count, invalid, mayclear)
9121 win_T *wp;
9122 int row;
9123 int line_count;
9124 int invalid;
9125 int mayclear;
9126{
9127 int did_delete;
9128 int nextrow;
9129 int lastrow;
9130 int retval;
9131
9132 if (invalid)
9133 wp->w_lines_valid = 0;
9134
9135 if (wp->w_height < 5)
9136 return FAIL;
9137
9138 if (line_count > wp->w_height - row)
9139 line_count = wp->w_height - row;
9140
9141 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9142 if (retval != MAYBE)
9143 return retval;
9144
9145 /*
9146 * If there is a next window or a status line, we first try to delete the
9147 * lines at the bottom to avoid messing what is after the window.
9148 * If this fails and there are following windows, don't do anything to avoid
9149 * messing up those windows, better just redraw.
9150 */
9151 did_delete = FALSE;
9152#ifdef FEAT_WINDOWS
9153 if (wp->w_next != NULL || wp->w_status_height)
9154 {
9155 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9156 line_count, (int)Rows, FALSE, NULL) == OK)
9157 did_delete = TRUE;
9158 else if (wp->w_next)
9159 return FAIL;
9160 }
9161#endif
9162 /*
9163 * if no lines deleted, blank the lines that will end up below the window
9164 */
9165 if (!did_delete)
9166 {
9167#ifdef FEAT_WINDOWS
9168 wp->w_redr_status = TRUE;
9169#endif
9170 redraw_cmdline = TRUE;
9171 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9172 lastrow = nextrow + line_count;
9173 if (lastrow > Rows)
9174 lastrow = Rows;
9175 screen_fill(nextrow - line_count, lastrow - line_count,
9176 W_WINCOL(wp), (int)W_ENDCOL(wp),
9177 ' ', ' ', 0);
9178 }
9179
9180 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9181 == FAIL)
9182 {
9183 /* deletion will have messed up other windows */
9184 if (did_delete)
9185 {
9186#ifdef FEAT_WINDOWS
9187 wp->w_redr_status = TRUE;
9188#endif
9189 win_rest_invalid(W_NEXT(wp));
9190 }
9191 return FAIL;
9192 }
9193
9194 return OK;
9195}
9196
9197/*
9198 * delete "line_count" window lines at "row" in window "wp"
9199 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9200 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9201 * scrolling
9202 * Return OK for success, FAIL if the lines are not deleted.
9203 */
9204 int
9205win_del_lines(wp, row, line_count, invalid, mayclear)
9206 win_T *wp;
9207 int row;
9208 int line_count;
9209 int invalid;
9210 int mayclear;
9211{
9212 int retval;
9213
9214 if (invalid)
9215 wp->w_lines_valid = 0;
9216
9217 if (line_count > wp->w_height - row)
9218 line_count = wp->w_height - row;
9219
9220 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9221 if (retval != MAYBE)
9222 return retval;
9223
9224 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9225 (int)Rows, FALSE, NULL) == FAIL)
9226 return FAIL;
9227
9228#ifdef FEAT_WINDOWS
9229 /*
9230 * If there are windows or status lines below, try to put them at the
9231 * correct place. If we can't do that, they have to be redrawn.
9232 */
9233 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9234 {
9235 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9236 line_count, (int)Rows, NULL) == FAIL)
9237 {
9238 wp->w_redr_status = TRUE;
9239 win_rest_invalid(wp->w_next);
9240 }
9241 }
9242 /*
9243 * If this is the last window and there is no status line, redraw the
9244 * command line later.
9245 */
9246 else
9247#endif
9248 redraw_cmdline = TRUE;
9249 return OK;
9250}
9251
9252/*
9253 * Common code for win_ins_lines() and win_del_lines().
9254 * Returns OK or FAIL when the work has been done.
9255 * Returns MAYBE when not finished yet.
9256 */
9257 static int
9258win_do_lines(wp, row, line_count, mayclear, del)
9259 win_T *wp;
9260 int row;
9261 int line_count;
9262 int mayclear;
9263 int del;
9264{
9265 int retval;
9266
9267 if (!redrawing() || line_count <= 0)
9268 return FAIL;
9269
9270 /* only a few lines left: redraw is faster */
9271 if (mayclear && Rows - line_count < 5
9272#ifdef FEAT_VERTSPLIT
9273 && wp->w_width == Columns
9274#endif
9275 )
9276 {
9277 screenclear(); /* will set wp->w_lines_valid to 0 */
9278 return FAIL;
9279 }
9280
9281 /*
9282 * Delete all remaining lines
9283 */
9284 if (row + line_count >= wp->w_height)
9285 {
9286 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9287 W_WINCOL(wp), (int)W_ENDCOL(wp),
9288 ' ', ' ', 0);
9289 return OK;
9290 }
9291
9292 /*
9293 * when scrolling, the message on the command line should be cleared,
9294 * otherwise it will stay there forever.
9295 */
9296 clear_cmdline = TRUE;
9297
9298 /*
9299 * If the terminal can set a scroll region, use that.
9300 * Always do this in a vertically split window. This will redraw from
9301 * ScreenLines[] when t_CV isn't defined. That's faster than using
9302 * win_line().
9303 * Don't use a scroll region when we are going to redraw the text, writing
9304 * a character in the lower right corner of the scroll region causes a
9305 * scroll-up in the DJGPP version.
9306 */
9307 if (scroll_region
9308#ifdef FEAT_VERTSPLIT
9309 || W_WIDTH(wp) != Columns
9310#endif
9311 )
9312 {
9313#ifdef FEAT_VERTSPLIT
9314 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9315#endif
9316 scroll_region_set(wp, row);
9317 if (del)
9318 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9319 wp->w_height - row, FALSE, wp);
9320 else
9321 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9322 wp->w_height - row, wp);
9323#ifdef FEAT_VERTSPLIT
9324 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9325#endif
9326 scroll_region_reset();
9327 return retval;
9328 }
9329
9330#ifdef FEAT_WINDOWS
9331 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9332 return FAIL;
9333#endif
9334
9335 return MAYBE;
9336}
9337
9338/*
9339 * window 'wp' and everything after it is messed up, mark it for redraw
9340 */
9341 static void
9342win_rest_invalid(wp)
9343 win_T *wp;
9344{
9345#ifdef FEAT_WINDOWS
9346 while (wp != NULL)
9347#else
9348 if (wp != NULL)
9349#endif
9350 {
9351 redraw_win_later(wp, NOT_VALID);
9352#ifdef FEAT_WINDOWS
9353 wp->w_redr_status = TRUE;
9354 wp = wp->w_next;
9355#endif
9356 }
9357 redraw_cmdline = TRUE;
9358}
9359
9360/*
9361 * The rest of the routines in this file perform screen manipulations. The
9362 * given operation is performed physically on the screen. The corresponding
9363 * change is also made to the internal screen image. In this way, the editor
9364 * anticipates the effect of editing changes on the appearance of the screen.
9365 * That way, when we call screenupdate a complete redraw isn't usually
9366 * necessary. Another advantage is that we can keep adding code to anticipate
9367 * screen changes, and in the meantime, everything still works.
9368 */
9369
9370/*
9371 * types for inserting or deleting lines
9372 */
9373#define USE_T_CAL 1
9374#define USE_T_CDL 2
9375#define USE_T_AL 3
9376#define USE_T_CE 4
9377#define USE_T_DL 5
9378#define USE_T_SR 6
9379#define USE_NL 7
9380#define USE_T_CD 8
9381#define USE_REDRAW 9
9382
9383/*
9384 * insert lines on the screen and update ScreenLines[]
9385 * 'end' is the line after the scrolled part. Normally it is Rows.
9386 * When scrolling region used 'off' is the offset from the top for the region.
9387 * 'row' and 'end' are relative to the start of the region.
9388 *
9389 * return FAIL for failure, OK for success.
9390 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009391 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392screen_ins_lines(off, row, line_count, end, wp)
9393 int off;
9394 int row;
9395 int line_count;
9396 int end;
9397 win_T *wp; /* NULL or window to use width from */
9398{
9399 int i;
9400 int j;
9401 unsigned temp;
9402 int cursor_row;
9403 int type;
9404 int result_empty;
9405 int can_ce = can_clear(T_CE);
9406
9407 /*
9408 * FAIL if
9409 * - there is no valid screen
9410 * - the screen has to be redrawn completely
9411 * - the line count is less than one
9412 * - the line count is more than 'ttyscroll'
9413 */
9414 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9415 return FAIL;
9416
9417 /*
9418 * There are seven ways to insert lines:
9419 * 0. When in a vertically split window and t_CV isn't set, redraw the
9420 * characters from ScreenLines[].
9421 * 1. Use T_CD (clear to end of display) if it exists and the result of
9422 * the insert is just empty lines
9423 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9424 * present or line_count > 1. It looks better if we do all the inserts
9425 * at once.
9426 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9427 * insert is just empty lines and T_CE is not present or line_count >
9428 * 1.
9429 * 4. Use T_AL (insert line) if it exists.
9430 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9431 * just empty lines.
9432 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9433 * just empty lines.
9434 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9435 * the 'da' flag is not set or we have clear line capability.
9436 * 8. redraw the characters from ScreenLines[].
9437 *
9438 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9439 * the scrollbar for the window. It does have insert line, use that if it
9440 * exists.
9441 */
9442 result_empty = (row + line_count >= end);
9443#ifdef FEAT_VERTSPLIT
9444 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9445 type = USE_REDRAW;
9446 else
9447#endif
9448 if (can_clear(T_CD) && result_empty)
9449 type = USE_T_CD;
9450 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9451 type = USE_T_CAL;
9452 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9453 type = USE_T_CDL;
9454 else if (*T_AL != NUL)
9455 type = USE_T_AL;
9456 else if (can_ce && result_empty)
9457 type = USE_T_CE;
9458 else if (*T_DL != NUL && result_empty)
9459 type = USE_T_DL;
9460 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9461 type = USE_T_SR;
9462 else
9463 return FAIL;
9464
9465 /*
9466 * For clearing the lines screen_del_lines() is used. This will also take
9467 * care of t_db if necessary.
9468 */
9469 if (type == USE_T_CD || type == USE_T_CDL ||
9470 type == USE_T_CE || type == USE_T_DL)
9471 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9472
9473 /*
9474 * If text is retained below the screen, first clear or delete as many
9475 * lines at the bottom of the window as are about to be inserted so that
9476 * the deleted lines won't later surface during a screen_del_lines.
9477 */
9478 if (*T_DB)
9479 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9480
9481#ifdef FEAT_CLIPBOARD
9482 /* Remove a modeless selection when inserting lines halfway the screen
9483 * or not the full width of the screen. */
9484 if (off + row > 0
9485# ifdef FEAT_VERTSPLIT
9486 || (wp != NULL && wp->w_width != Columns)
9487# endif
9488 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009489 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 else
9491 clip_scroll_selection(-line_count);
9492#endif
9493
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494#ifdef FEAT_GUI
9495 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9496 * scrolling is actually carried out. */
9497 gui_dont_update_cursor();
9498#endif
9499
9500 if (*T_CCS != NUL) /* cursor relative to region */
9501 cursor_row = row;
9502 else
9503 cursor_row = row + off;
9504
9505 /*
9506 * Shift LineOffset[] line_count down to reflect the inserted lines.
9507 * Clear the inserted lines in ScreenLines[].
9508 */
9509 row += off;
9510 end += off;
9511 for (i = 0; i < line_count; ++i)
9512 {
9513#ifdef FEAT_VERTSPLIT
9514 if (wp != NULL && wp->w_width != Columns)
9515 {
9516 /* need to copy part of a line */
9517 j = end - 1 - i;
9518 while ((j -= line_count) >= row)
9519 linecopy(j + line_count, j, wp);
9520 j += line_count;
9521 if (can_clear((char_u *)" "))
9522 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9523 else
9524 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9525 LineWraps[j] = FALSE;
9526 }
9527 else
9528#endif
9529 {
9530 j = end - 1 - i;
9531 temp = LineOffset[j];
9532 while ((j -= line_count) >= row)
9533 {
9534 LineOffset[j + line_count] = LineOffset[j];
9535 LineWraps[j + line_count] = LineWraps[j];
9536 }
9537 LineOffset[j + line_count] = temp;
9538 LineWraps[j + line_count] = FALSE;
9539 if (can_clear((char_u *)" "))
9540 lineclear(temp, (int)Columns);
9541 else
9542 lineinvalid(temp, (int)Columns);
9543 }
9544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
9546 screen_stop_highlight();
9547 windgoto(cursor_row, 0);
9548
9549#ifdef FEAT_VERTSPLIT
9550 /* redraw the characters */
9551 if (type == USE_REDRAW)
9552 redraw_block(row, end, wp);
9553 else
9554#endif
9555 if (type == USE_T_CAL)
9556 {
9557 term_append_lines(line_count);
9558 screen_start(); /* don't know where cursor is now */
9559 }
9560 else
9561 {
9562 for (i = 0; i < line_count; i++)
9563 {
9564 if (type == USE_T_AL)
9565 {
9566 if (i && cursor_row != 0)
9567 windgoto(cursor_row, 0);
9568 out_str(T_AL);
9569 }
9570 else /* type == USE_T_SR */
9571 out_str(T_SR);
9572 screen_start(); /* don't know where cursor is now */
9573 }
9574 }
9575
9576 /*
9577 * With scroll-reverse and 'da' flag set we need to clear the lines that
9578 * have been scrolled down into the region.
9579 */
9580 if (type == USE_T_SR && *T_DA)
9581 {
9582 for (i = 0; i < line_count; ++i)
9583 {
9584 windgoto(off + i, 0);
9585 out_str(T_CE);
9586 screen_start(); /* don't know where cursor is now */
9587 }
9588 }
9589
9590#ifdef FEAT_GUI
9591 gui_can_update_cursor();
9592 if (gui.in_use)
9593 out_flush(); /* always flush after a scroll */
9594#endif
9595 return OK;
9596}
9597
9598/*
9599 * delete lines on the screen and update ScreenLines[]
9600 * 'end' is the line after the scrolled part. Normally it is Rows.
9601 * When scrolling region used 'off' is the offset from the top for the region.
9602 * 'row' and 'end' are relative to the start of the region.
9603 *
9604 * Return OK for success, FAIL if the lines are not deleted.
9605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 int
9607screen_del_lines(off, row, line_count, end, force, wp)
9608 int off;
9609 int row;
9610 int line_count;
9611 int end;
9612 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009613 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 int j;
9616 int i;
9617 unsigned temp;
9618 int cursor_row;
9619 int cursor_end;
9620 int result_empty; /* result is empty until end of region */
9621 int can_delete; /* deleting line codes can be used */
9622 int type;
9623
9624 /*
9625 * FAIL if
9626 * - there is no valid screen
9627 * - the screen has to be redrawn completely
9628 * - the line count is less than one
9629 * - the line count is more than 'ttyscroll'
9630 */
9631 if (!screen_valid(TRUE) || line_count <= 0 ||
9632 (!force && line_count > p_ttyscroll))
9633 return FAIL;
9634
9635 /*
9636 * Check if the rest of the current region will become empty.
9637 */
9638 result_empty = row + line_count >= end;
9639
9640 /*
9641 * We can delete lines only when 'db' flag not set or when 'ce' option
9642 * available.
9643 */
9644 can_delete = (*T_DB == NUL || can_clear(T_CE));
9645
9646 /*
9647 * There are six ways to delete lines:
9648 * 0. When in a vertically split window and t_CV isn't set, redraw the
9649 * characters from ScreenLines[].
9650 * 1. Use T_CD if it exists and the result is empty.
9651 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9652 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9653 * none of the other ways work.
9654 * 4. Use T_CE (erase line) if the result is empty.
9655 * 5. Use T_DL (delete line) if it exists.
9656 * 6. redraw the characters from ScreenLines[].
9657 */
9658#ifdef FEAT_VERTSPLIT
9659 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9660 type = USE_REDRAW;
9661 else
9662#endif
9663 if (can_clear(T_CD) && result_empty)
9664 type = USE_T_CD;
9665#if defined(__BEOS__) && defined(BEOS_DR8)
9666 /*
9667 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9668 * its internal termcap... this works okay for tests which test *T_DB !=
9669 * NUL. It has the disadvantage that the user cannot use any :set t_*
9670 * command to get T_DB (back) to empty_option, only :set term=... will do
9671 * the trick...
9672 * Anyway, this hack will hopefully go away with the next OS release.
9673 * (Olaf Seibert)
9674 */
9675 else if (row == 0 && T_DB == empty_option
9676 && (line_count == 1 || *T_CDL == NUL))
9677#else
9678 else if (row == 0 && (
9679#ifndef AMIGA
9680 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9681 * up, so use delete-line command */
9682 line_count == 1 ||
9683#endif
9684 *T_CDL == NUL))
9685#endif
9686 type = USE_NL;
9687 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9688 type = USE_T_CDL;
9689 else if (can_clear(T_CE) && result_empty
9690#ifdef FEAT_VERTSPLIT
9691 && (wp == NULL || wp->w_width == Columns)
9692#endif
9693 )
9694 type = USE_T_CE;
9695 else if (*T_DL != NUL && can_delete)
9696 type = USE_T_DL;
9697 else if (*T_CDL != NUL && can_delete)
9698 type = USE_T_CDL;
9699 else
9700 return FAIL;
9701
9702#ifdef FEAT_CLIPBOARD
9703 /* Remove a modeless selection when deleting lines halfway the screen or
9704 * not the full width of the screen. */
9705 if (off + row > 0
9706# ifdef FEAT_VERTSPLIT
9707 || (wp != NULL && wp->w_width != Columns)
9708# endif
9709 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009710 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 else
9712 clip_scroll_selection(line_count);
9713#endif
9714
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715#ifdef FEAT_GUI
9716 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9717 * scrolling is actually carried out. */
9718 gui_dont_update_cursor();
9719#endif
9720
9721 if (*T_CCS != NUL) /* cursor relative to region */
9722 {
9723 cursor_row = row;
9724 cursor_end = end;
9725 }
9726 else
9727 {
9728 cursor_row = row + off;
9729 cursor_end = end + off;
9730 }
9731
9732 /*
9733 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9734 * Clear the inserted lines in ScreenLines[].
9735 */
9736 row += off;
9737 end += off;
9738 for (i = 0; i < line_count; ++i)
9739 {
9740#ifdef FEAT_VERTSPLIT
9741 if (wp != NULL && wp->w_width != Columns)
9742 {
9743 /* need to copy part of a line */
9744 j = row + i;
9745 while ((j += line_count) <= end - 1)
9746 linecopy(j - line_count, j, wp);
9747 j -= line_count;
9748 if (can_clear((char_u *)" "))
9749 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9750 else
9751 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9752 LineWraps[j] = FALSE;
9753 }
9754 else
9755#endif
9756 {
9757 /* whole width, moving the line pointers is faster */
9758 j = row + i;
9759 temp = LineOffset[j];
9760 while ((j += line_count) <= end - 1)
9761 {
9762 LineOffset[j - line_count] = LineOffset[j];
9763 LineWraps[j - line_count] = LineWraps[j];
9764 }
9765 LineOffset[j - line_count] = temp;
9766 LineWraps[j - line_count] = FALSE;
9767 if (can_clear((char_u *)" "))
9768 lineclear(temp, (int)Columns);
9769 else
9770 lineinvalid(temp, (int)Columns);
9771 }
9772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773
9774 screen_stop_highlight();
9775
9776#ifdef FEAT_VERTSPLIT
9777 /* redraw the characters */
9778 if (type == USE_REDRAW)
9779 redraw_block(row, end, wp);
9780 else
9781#endif
9782 if (type == USE_T_CD) /* delete the lines */
9783 {
9784 windgoto(cursor_row, 0);
9785 out_str(T_CD);
9786 screen_start(); /* don't know where cursor is now */
9787 }
9788 else if (type == USE_T_CDL)
9789 {
9790 windgoto(cursor_row, 0);
9791 term_delete_lines(line_count);
9792 screen_start(); /* don't know where cursor is now */
9793 }
9794 /*
9795 * Deleting lines at top of the screen or scroll region: Just scroll
9796 * the whole screen (scroll region) up by outputting newlines on the
9797 * last line.
9798 */
9799 else if (type == USE_NL)
9800 {
9801 windgoto(cursor_end - 1, 0);
9802 for (i = line_count; --i >= 0; )
9803 out_char('\n'); /* cursor will remain on same line */
9804 }
9805 else
9806 {
9807 for (i = line_count; --i >= 0; )
9808 {
9809 if (type == USE_T_DL)
9810 {
9811 windgoto(cursor_row, 0);
9812 out_str(T_DL); /* delete a line */
9813 }
9814 else /* type == USE_T_CE */
9815 {
9816 windgoto(cursor_row + i, 0);
9817 out_str(T_CE); /* erase a line */
9818 }
9819 screen_start(); /* don't know where cursor is now */
9820 }
9821 }
9822
9823 /*
9824 * If the 'db' flag is set, we need to clear the lines that have been
9825 * scrolled up at the bottom of the region.
9826 */
9827 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9828 {
9829 for (i = line_count; i > 0; --i)
9830 {
9831 windgoto(cursor_end - i, 0);
9832 out_str(T_CE); /* erase a line */
9833 screen_start(); /* don't know where cursor is now */
9834 }
9835 }
9836
9837#ifdef FEAT_GUI
9838 gui_can_update_cursor();
9839 if (gui.in_use)
9840 out_flush(); /* always flush after a scroll */
9841#endif
9842
9843 return OK;
9844}
9845
9846/*
9847 * show the current mode and ruler
9848 *
9849 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9850 * If clear_cmdline is FALSE there may be a message there that needs to be
9851 * cleared only if a mode is shown.
9852 * Return the length of the message (0 if no message).
9853 */
9854 int
9855showmode()
9856{
9857 int need_clear;
9858 int length = 0;
9859 int do_mode;
9860 int attr;
9861 int nwr_save;
9862#ifdef FEAT_INS_EXPAND
9863 int sub_attr;
9864#endif
9865
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009866 do_mode = ((p_smd && msg_silent == 0)
9867 && ((State & INSERT)
9868 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009869 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 if (do_mode || Recording)
9871 {
9872 /*
9873 * Don't show mode right now, when not redrawing or inside a mapping.
9874 * Call char_avail() only when we are going to show something, because
9875 * it takes a bit of time.
9876 */
9877 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9878 {
9879 redraw_cmdline = TRUE; /* show mode later */
9880 return 0;
9881 }
9882
9883 nwr_save = need_wait_return;
9884
9885 /* wait a bit before overwriting an important message */
9886 check_for_delay(FALSE);
9887
9888 /* if the cmdline is more than one line high, erase top lines */
9889 need_clear = clear_cmdline;
9890 if (clear_cmdline && cmdline_row < Rows - 1)
9891 msg_clr_cmdline(); /* will reset clear_cmdline */
9892
9893 /* Position on the last line in the window, column 0 */
9894 msg_pos_mode();
9895 cursor_off();
9896 attr = hl_attr(HLF_CM); /* Highlight mode */
9897 if (do_mode)
9898 {
9899 MSG_PUTS_ATTR("--", attr);
9900#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009901 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009902# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009903 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009904# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009905 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009906# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009907 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009908# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 MSG_PUTS_ATTR(" IM", attr);
9910# else
9911 MSG_PUTS_ATTR(" XIM", attr);
9912# endif
9913#endif
9914#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9915 if (gui.in_use)
9916 {
9917 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009918 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 }
9920#endif
9921#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009922 /* CTRL-X in Insert mode */
9923 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 {
9925 /* These messages can get long, avoid a wrap in a narrow
9926 * window. Prefer showing edit_submode_extra. */
9927 length = (Rows - msg_row) * Columns - 3;
9928 if (edit_submode_extra != NULL)
9929 length -= vim_strsize(edit_submode_extra);
9930 if (length > 0)
9931 {
9932 if (edit_submode_pre != NULL)
9933 length -= vim_strsize(edit_submode_pre);
9934 if (length - vim_strsize(edit_submode) > 0)
9935 {
9936 if (edit_submode_pre != NULL)
9937 msg_puts_attr(edit_submode_pre, attr);
9938 msg_puts_attr(edit_submode, attr);
9939 }
9940 if (edit_submode_extra != NULL)
9941 {
9942 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9943 if ((int)edit_submode_highl < (int)HLF_COUNT)
9944 sub_attr = hl_attr(edit_submode_highl);
9945 else
9946 sub_attr = attr;
9947 msg_puts_attr(edit_submode_extra, sub_attr);
9948 }
9949 }
9950 length = 0;
9951 }
9952 else
9953#endif
9954 {
9955#ifdef FEAT_VREPLACE
9956 if (State & VREPLACE_FLAG)
9957 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9958 else
9959#endif
9960 if (State & REPLACE_FLAG)
9961 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9962 else if (State & INSERT)
9963 {
9964#ifdef FEAT_RIGHTLEFT
9965 if (p_ri)
9966 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9967#endif
9968 MSG_PUTS_ATTR(_(" INSERT"), attr);
9969 }
9970 else if (restart_edit == 'I')
9971 MSG_PUTS_ATTR(_(" (insert)"), attr);
9972 else if (restart_edit == 'R')
9973 MSG_PUTS_ATTR(_(" (replace)"), attr);
9974 else if (restart_edit == 'V')
9975 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9976#ifdef FEAT_RIGHTLEFT
9977 if (p_hkmap)
9978 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9979# ifdef FEAT_FKMAP
9980 if (p_fkmap)
9981 MSG_PUTS_ATTR(farsi_text_5, attr);
9982# endif
9983#endif
9984#ifdef FEAT_KEYMAP
9985 if (State & LANGMAP)
9986 {
9987# ifdef FEAT_ARABIC
9988 if (curwin->w_p_arab)
9989 MSG_PUTS_ATTR(_(" Arabic"), attr);
9990 else
9991# endif
9992 MSG_PUTS_ATTR(_(" (lang)"), attr);
9993 }
9994#endif
9995 if ((State & INSERT) && p_paste)
9996 MSG_PUTS_ATTR(_(" (paste)"), attr);
9997
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 if (VIsual_active)
9999 {
10000 char *p;
10001
10002 /* Don't concatenate separate words to avoid translation
10003 * problems. */
10004 switch ((VIsual_select ? 4 : 0)
10005 + (VIsual_mode == Ctrl_V) * 2
10006 + (VIsual_mode == 'V'))
10007 {
10008 case 0: p = N_(" VISUAL"); break;
10009 case 1: p = N_(" VISUAL LINE"); break;
10010 case 2: p = N_(" VISUAL BLOCK"); break;
10011 case 4: p = N_(" SELECT"); break;
10012 case 5: p = N_(" SELECT LINE"); break;
10013 default: p = N_(" SELECT BLOCK"); break;
10014 }
10015 MSG_PUTS_ATTR(_(p), attr);
10016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 MSG_PUTS_ATTR(" --", attr);
10018 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010019
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 need_clear = TRUE;
10021 }
10022 if (Recording
10023#ifdef FEAT_INS_EXPAND
10024 && edit_submode == NULL /* otherwise it gets too long */
10025#endif
10026 )
10027 {
10028 MSG_PUTS_ATTR(_("recording"), attr);
10029 need_clear = TRUE;
10030 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010031
10032 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 if (need_clear || clear_cmdline)
10034 msg_clr_eos();
10035 msg_didout = FALSE; /* overwrite this message */
10036 length = msg_col;
10037 msg_col = 0;
10038 need_wait_return = nwr_save; /* never ask for hit-return for this */
10039 }
10040 else if (clear_cmdline && msg_silent == 0)
10041 /* Clear the whole command line. Will reset "clear_cmdline". */
10042 msg_clr_cmdline();
10043
10044#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 /* In Visual mode the size of the selected area must be redrawn. */
10046 if (VIsual_active)
10047 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048
10049 /* If the last window has no status line, the ruler is after the mode
10050 * message and must be redrawn */
10051 if (redrawing()
10052# ifdef FEAT_WINDOWS
10053 && lastwin->w_status_height == 0
10054# endif
10055 )
10056 win_redr_ruler(lastwin, TRUE);
10057#endif
10058 redraw_cmdline = FALSE;
10059 clear_cmdline = FALSE;
10060
10061 return length;
10062}
10063
10064/*
10065 * Position for a mode message.
10066 */
10067 static void
10068msg_pos_mode()
10069{
10070 msg_col = 0;
10071 msg_row = Rows - 1;
10072}
10073
10074/*
10075 * Delete mode message. Used when ESC is typed which is expected to end
10076 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010077 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 */
10079 void
10080unshowmode(force)
10081 int force;
10082{
10083 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010084 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 */
10086 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10087 redraw_cmdline = TRUE; /* delete mode later */
10088 else
10089 {
10090 msg_pos_mode();
10091 if (Recording)
10092 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
10093 msg_clr_eos();
10094 }
10095}
10096
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010097#if defined(FEAT_WINDOWS)
10098/*
10099 * Draw the tab pages line at the top of the Vim window.
10100 */
10101 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010102draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010103{
10104 int tabcount = 0;
10105 tabpage_T *tp;
10106 int tabwidth;
10107 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010108 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010109 int attr;
10110 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010111 win_T *cwp;
10112 int wincount;
10113 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010114 int c;
10115 int len;
10116 int attr_sel = hl_attr(HLF_TPS);
10117 int attr_nosel = hl_attr(HLF_TP);
10118 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010119 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010120 int room;
10121 int use_sep_chars = (t_colors < 8
10122#ifdef FEAT_GUI
10123 && !gui.in_use
10124#endif
10125 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010126
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010127 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010128
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010129#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010130 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010131 if (gui_use_tabline())
10132 {
10133 gui_update_tabline();
10134 return;
10135 }
10136#endif
10137
10138 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010139 return;
10140
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010141#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010142
10143 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10144 for (scol = 0; scol < Columns; ++scol)
10145 TabPageIdxs[scol] = 0;
10146
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010147 /* Use the 'tabline' option if it's set. */
10148 if (*p_tal != NUL)
10149 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010150 int save_called_emsg = called_emsg;
10151
10152 /* Check for an error. If there is one we would loop in redrawing the
10153 * screen. Avoid that by making 'tabline' empty. */
10154 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010155 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010156 if (called_emsg)
10157 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010158 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010159 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010160 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010161 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010162#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010163 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010164 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10165 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010166
Bram Moolenaar238a5642006-02-21 22:12:05 +000010167 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10168 if (tabwidth < 6)
10169 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010170
Bram Moolenaar238a5642006-02-21 22:12:05 +000010171 attr = attr_nosel;
10172 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010173 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010174 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10175 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010176 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010177 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010178
Bram Moolenaar238a5642006-02-21 22:12:05 +000010179 if (tp->tp_topframe == topframe)
10180 attr = attr_sel;
10181 if (use_sep_chars && col > 0)
10182 screen_putchar('|', 0, col++, attr);
10183
10184 if (tp->tp_topframe != topframe)
10185 attr = attr_nosel;
10186
10187 screen_putchar(' ', 0, col++, attr);
10188
10189 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010190 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010191 cwp = curwin;
10192 wp = firstwin;
10193 }
10194 else
10195 {
10196 cwp = tp->tp_curwin;
10197 wp = tp->tp_firstwin;
10198 }
10199
10200 modified = FALSE;
10201 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10202 if (bufIsChanged(wp->w_buffer))
10203 modified = TRUE;
10204 if (modified || wincount > 1)
10205 {
10206 if (wincount > 1)
10207 {
10208 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010209 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010210 if (col + len >= Columns - 3)
10211 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010212 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010213#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010214 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010215#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010216 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010217#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010218 );
10219 col += len;
10220 }
10221 if (modified)
10222 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10223 screen_putchar(' ', 0, col++, attr);
10224 }
10225
10226 room = scol - col + tabwidth - 1;
10227 if (room > 0)
10228 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010229 /* Get buffer name in NameBuff[] */
10230 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010231 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010232 len = vim_strsize(NameBuff);
10233 p = NameBuff;
10234#ifdef FEAT_MBYTE
10235 if (has_mbyte)
10236 while (len > room)
10237 {
10238 len -= ptr2cells(p);
10239 mb_ptr_adv(p);
10240 }
10241 else
10242#endif
10243 if (len > room)
10244 {
10245 p += len - room;
10246 len = room;
10247 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010248 if (len > Columns - col - 1)
10249 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010250
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010251 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010252 col += len;
10253 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010254 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010255
10256 /* Store the tab page number in TabPageIdxs[], so that
10257 * jump_to_mouse() knows where each one is. */
10258 ++tabcount;
10259 while (scol < col)
10260 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010261 }
10262
Bram Moolenaar238a5642006-02-21 22:12:05 +000010263 if (use_sep_chars)
10264 c = '_';
10265 else
10266 c = ' ';
10267 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010268
10269 /* Put an "X" for closing the current tab if there are several. */
10270 if (first_tabpage->tp_next != NULL)
10271 {
10272 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10273 TabPageIdxs[Columns - 1] = -999;
10274 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010275 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010276
10277 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10278 * set. */
10279 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010280}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010281
10282/*
10283 * Get buffer name for "buf" into NameBuff[].
10284 * Takes care of special buffer names and translates special characters.
10285 */
10286 void
10287get_trans_bufname(buf)
10288 buf_T *buf;
10289{
10290 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010291 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010292 else
10293 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10294 trans_characters(NameBuff, MAXPATHL);
10295}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010296#endif
10297
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10299/*
10300 * Get the character to use in a status line. Get its attributes in "*attr".
10301 */
10302 static int
10303fillchar_status(attr, is_curwin)
10304 int *attr;
10305 int is_curwin;
10306{
10307 int fill;
10308 if (is_curwin)
10309 {
10310 *attr = hl_attr(HLF_S);
10311 fill = fill_stl;
10312 }
10313 else
10314 {
10315 *attr = hl_attr(HLF_SNC);
10316 fill = fill_stlnc;
10317 }
10318 /* Use fill when there is highlighting, and highlighting of current
10319 * window differs, or the fillchars differ, or this is not the
10320 * current window */
10321 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10322 || !is_curwin || firstwin == lastwin)
10323 || (fill_stl != fill_stlnc)))
10324 return fill;
10325 if (is_curwin)
10326 return '^';
10327 return '=';
10328}
10329#endif
10330
10331#ifdef FEAT_VERTSPLIT
10332/*
10333 * Get the character to use in a separator between vertically split windows.
10334 * Get its attributes in "*attr".
10335 */
10336 static int
10337fillchar_vsep(attr)
10338 int *attr;
10339{
10340 *attr = hl_attr(HLF_C);
10341 if (*attr == 0 && fill_vert == ' ')
10342 return '|';
10343 else
10344 return fill_vert;
10345}
10346#endif
10347
10348/*
10349 * Return TRUE if redrawing should currently be done.
10350 */
10351 int
10352redrawing()
10353{
10354 return (!RedrawingDisabled
10355 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10356}
10357
10358/*
10359 * Return TRUE if printing messages should currently be done.
10360 */
10361 int
10362messaging()
10363{
10364 return (!(p_lz && char_avail() && !KeyTyped));
10365}
10366
10367/*
10368 * Show current status info in ruler and various other places
10369 * If always is FALSE, only show ruler if position has changed.
10370 */
10371 void
10372showruler(always)
10373 int always;
10374{
10375 if (!always && !redrawing())
10376 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010377#ifdef FEAT_INS_EXPAND
10378 if (pum_visible())
10379 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010380# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010381 /* Don't redraw right now, do it later. */
10382 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010383# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010384 return;
10385 }
10386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010388 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010389 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010390 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 else
10393#endif
10394#ifdef FEAT_CMDL_INFO
10395 win_redr_ruler(curwin, always);
10396#endif
10397
10398#ifdef FEAT_TITLE
10399 if (need_maketitle
10400# ifdef FEAT_STL_OPT
10401 || (p_icon && (stl_syntax & STL_IN_ICON))
10402 || (p_title && (stl_syntax & STL_IN_TITLE))
10403# endif
10404 )
10405 maketitle();
10406#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010407#ifdef FEAT_WINDOWS
10408 /* Redraw the tab pages line if needed. */
10409 if (redraw_tabline)
10410 draw_tabline();
10411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412}
10413
10414#ifdef FEAT_CMDL_INFO
10415 static void
10416win_redr_ruler(wp, always)
10417 win_T *wp;
10418 int always;
10419{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010420#define RULER_BUF_LEN 70
10421 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 int row;
10423 int fillchar;
10424 int attr;
10425 int empty_line = FALSE;
10426 colnr_T virtcol;
10427 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010428 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 int o;
10430#ifdef FEAT_VERTSPLIT
10431 int this_ru_col;
10432 int off = 0;
10433 int width = Columns;
10434# define WITH_OFF(x) x
10435# define WITH_WIDTH(x) x
10436#else
10437# define WITH_OFF(x) 0
10438# define WITH_WIDTH(x) Columns
10439# define this_ru_col ru_col
10440#endif
10441
10442 /* If 'ruler' off or redrawing disabled, don't do anything */
10443 if (!p_ru)
10444 return;
10445
10446 /*
10447 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10448 * after deleting lines, before cursor.lnum is corrected.
10449 */
10450 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10451 return;
10452
10453#ifdef FEAT_INS_EXPAND
10454 /* Don't draw the ruler while doing insert-completion, it might overwrite
10455 * the (long) mode message. */
10456# ifdef FEAT_WINDOWS
10457 if (wp == lastwin && lastwin->w_status_height == 0)
10458# endif
10459 if (edit_submode != NULL)
10460 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010461 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10462 if (pum_visible())
10463 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464#endif
10465
10466#ifdef FEAT_STL_OPT
10467 if (*p_ruf)
10468 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010469 int save_called_emsg = called_emsg;
10470
10471 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010473 if (called_emsg)
10474 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010475 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010476 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 return;
10478 }
10479#endif
10480
10481 /*
10482 * Check if not in Insert mode and the line is empty (will show "0-1").
10483 */
10484 if (!(State & INSERT)
10485 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10486 empty_line = TRUE;
10487
10488 /*
10489 * Only draw the ruler when something changed.
10490 */
10491 validate_virtcol_win(wp);
10492 if ( redraw_cmdline
10493 || always
10494 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10495 || wp->w_cursor.col != wp->w_ru_cursor.col
10496 || wp->w_virtcol != wp->w_ru_virtcol
10497#ifdef FEAT_VIRTUALEDIT
10498 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10499#endif
10500 || wp->w_topline != wp->w_ru_topline
10501 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10502#ifdef FEAT_DIFF
10503 || wp->w_topfill != wp->w_ru_topfill
10504#endif
10505 || empty_line != wp->w_ru_empty)
10506 {
10507 cursor_off();
10508#ifdef FEAT_WINDOWS
10509 if (wp->w_status_height)
10510 {
10511 row = W_WINROW(wp) + wp->w_height;
10512 fillchar = fillchar_status(&attr, wp == curwin);
10513# ifdef FEAT_VERTSPLIT
10514 off = W_WINCOL(wp);
10515 width = W_WIDTH(wp);
10516# endif
10517 }
10518 else
10519#endif
10520 {
10521 row = Rows - 1;
10522 fillchar = ' ';
10523 attr = 0;
10524#ifdef FEAT_VERTSPLIT
10525 width = Columns;
10526 off = 0;
10527#endif
10528 }
10529
10530 /* In list mode virtcol needs to be recomputed */
10531 virtcol = wp->w_virtcol;
10532 if (wp->w_p_list && lcs_tab1 == NUL)
10533 {
10534 wp->w_p_list = FALSE;
10535 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10536 wp->w_p_list = TRUE;
10537 }
10538
10539 /*
10540 * Some sprintfs return the length, some return a pointer.
10541 * To avoid portability problems we use strlen() here.
10542 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010543 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10545 ? 0L
10546 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010547 len = STRLEN(buffer);
10548 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10550 (int)virtcol + 1);
10551
10552 /*
10553 * Add a "50%" if there is room for it.
10554 * On the last line, don't print in the last column (scrolls the
10555 * screen up on some terminals).
10556 */
10557 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010558 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 o = i + vim_strsize(buffer + i + 1);
10560#ifdef FEAT_WINDOWS
10561 if (wp->w_status_height == 0) /* can't use last char of screen */
10562#endif
10563 ++o;
10564#ifdef FEAT_VERTSPLIT
10565 this_ru_col = ru_col - (Columns - width);
10566 if (this_ru_col < 0)
10567 this_ru_col = 0;
10568#endif
10569 /* Never use more than half the window/screen width, leave the other
10570 * half for the filename. */
10571 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10572 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10573 if (this_ru_col + o < WITH_WIDTH(width))
10574 {
10575 while (this_ru_col + o < WITH_WIDTH(width))
10576 {
10577#ifdef FEAT_MBYTE
10578 if (has_mbyte)
10579 i += (*mb_char2bytes)(fillchar, buffer + i);
10580 else
10581#endif
10582 buffer[i++] = fillchar;
10583 ++o;
10584 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010585 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 }
10587 /* Truncate at window boundary. */
10588#ifdef FEAT_MBYTE
10589 if (has_mbyte)
10590 {
10591 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010592 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 {
10594 o += (*mb_ptr2cells)(buffer + i);
10595 if (this_ru_col + o > WITH_WIDTH(width))
10596 {
10597 buffer[i] = NUL;
10598 break;
10599 }
10600 }
10601 }
10602 else
10603#endif
10604 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10605 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10606
10607 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10608 i = redraw_cmdline;
10609 screen_fill(row, row + 1,
10610 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10611 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10612 fillchar, fillchar, attr);
10613 /* don't redraw the cmdline because of showing the ruler */
10614 redraw_cmdline = i;
10615 wp->w_ru_cursor = wp->w_cursor;
10616 wp->w_ru_virtcol = wp->w_virtcol;
10617 wp->w_ru_empty = empty_line;
10618 wp->w_ru_topline = wp->w_topline;
10619 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10620#ifdef FEAT_DIFF
10621 wp->w_ru_topfill = wp->w_topfill;
10622#endif
10623 }
10624}
10625#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010626
10627#if defined(FEAT_LINEBREAK) || defined(PROTO)
10628/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010629 * Return the width of the 'number' and 'relativenumber' column.
10630 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010631 * Otherwise it depends on 'numberwidth' and the line count.
10632 */
10633 int
10634number_width(wp)
10635 win_T *wp;
10636{
10637 int n;
10638 linenr_T lnum;
10639
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010640 if (wp->w_p_rnu && !wp->w_p_nu)
10641 /* cursor line shows "0" */
10642 lnum = wp->w_height;
10643 else
10644 /* cursor line shows absolute line number */
10645 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010646
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010647 if (lnum == wp->w_nrwidth_line_count)
10648 return wp->w_nrwidth_width;
10649 wp->w_nrwidth_line_count = lnum;
10650
10651 n = 0;
10652 do
10653 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010654 lnum /= 10;
10655 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010656 } while (lnum > 0);
10657
10658 /* 'numberwidth' gives the minimal width plus one */
10659 if (n < wp->w_p_nuw - 1)
10660 n = wp->w_p_nuw - 1;
10661
10662 wp->w_nrwidth_width = n;
10663 return n;
10664}
10665#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010666
10667/*
10668 * Return the current cursor column. This is the actual position on the
10669 * screen. First column is 0.
10670 */
10671 int
10672screen_screencol()
10673{
10674 return screen_cur_col;
10675}
10676
10677/*
10678 * Return the current cursor row. This is the actual position on the screen.
10679 * First row is 0.
10680 */
10681 int
10682screen_screenrow()
10683{
10684 return screen_cur_row;
10685}