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