blob: 8e616915bf91cc65aad22f8168e40d93be09c160 [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 {
1587 colnr_T fromc, toc;
1588
1589 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1590 ++toc;
1591 if (curwin->w_curswant == MAXCOL)
1592 toc = MAXCOL;
1593
1594 if (fromc != wp->w_old_cursor_fcol
1595 || toc != wp->w_old_cursor_lcol)
1596 {
1597 if (from > VIsual.lnum)
1598 from = VIsual.lnum;
1599 if (to < VIsual.lnum)
1600 to = VIsual.lnum;
1601 }
1602 wp->w_old_cursor_fcol = fromc;
1603 wp->w_old_cursor_lcol = toc;
1604 }
1605 }
1606 else
1607 {
1608 /* Use the line numbers of the old Visual area. */
1609 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1610 {
1611 from = wp->w_old_cursor_lnum;
1612 to = wp->w_old_visual_lnum;
1613 }
1614 else
1615 {
1616 from = wp->w_old_visual_lnum;
1617 to = wp->w_old_cursor_lnum;
1618 }
1619 }
1620
1621 /*
1622 * There is no need to update lines above the top of the window.
1623 */
1624 if (from < wp->w_topline)
1625 from = wp->w_topline;
1626
1627 /*
1628 * If we know the value of w_botline, use it to restrict the update to
1629 * the lines that are visible in the window.
1630 */
1631 if (wp->w_valid & VALID_BOTLINE)
1632 {
1633 if (from >= wp->w_botline)
1634 from = wp->w_botline - 1;
1635 if (to >= wp->w_botline)
1636 to = wp->w_botline - 1;
1637 }
1638
1639 /*
1640 * Find the minimal part to be updated.
1641 * Watch out for scrolling that made entries in w_lines[] invalid.
1642 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1643 * top_end; need to redraw from top_end to the "to" line.
1644 * A middle mouse click with a Visual selection may change the text
1645 * above the Visual area and reset wl_valid, do count these for
1646 * mid_end (in srow).
1647 */
1648 if (mid_start > 0)
1649 {
1650 lnum = wp->w_topline;
1651 idx = 0;
1652 srow = 0;
1653 if (scrolled_down)
1654 mid_start = top_end;
1655 else
1656 mid_start = 0;
1657 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1658 {
1659 if (wp->w_lines[idx].wl_valid)
1660 mid_start += wp->w_lines[idx].wl_size;
1661 else if (!scrolled_down)
1662 srow += wp->w_lines[idx].wl_size;
1663 ++idx;
1664# ifdef FEAT_FOLDING
1665 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1666 lnum = wp->w_lines[idx].wl_lnum;
1667 else
1668# endif
1669 ++lnum;
1670 }
1671 srow += mid_start;
1672 mid_end = wp->w_height;
1673 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1674 {
1675 if (wp->w_lines[idx].wl_valid
1676 && wp->w_lines[idx].wl_lnum >= to + 1)
1677 {
1678 /* Only update until first row of this line */
1679 mid_end = srow;
1680 break;
1681 }
1682 srow += wp->w_lines[idx].wl_size;
1683 }
1684 }
1685 }
1686
1687 if (VIsual_active && buf == curwin->w_buffer)
1688 {
1689 wp->w_old_visual_mode = VIsual_mode;
1690 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1691 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001692 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 wp->w_old_curswant = curwin->w_curswant;
1694 }
1695 else
1696 {
1697 wp->w_old_visual_mode = 0;
1698 wp->w_old_cursor_lnum = 0;
1699 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001700 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1704 /* reset got_int, otherwise regexp won't work */
1705 save_got_int = got_int;
1706 got_int = 0;
1707#endif
1708#ifdef FEAT_FOLDING
1709 win_foldinfo.fi_level = 0;
1710#endif
1711
1712 /*
1713 * Update all the window rows.
1714 */
1715 idx = 0; /* first entry in w_lines[].wl_size */
1716 row = 0;
1717 srow = 0;
1718 lnum = wp->w_topline; /* first line shown in window */
1719 for (;;)
1720 {
1721 /* stop updating when reached the end of the window (check for _past_
1722 * the end of the window is at the end of the loop) */
1723 if (row == wp->w_height)
1724 {
1725 didline = TRUE;
1726 break;
1727 }
1728
1729 /* stop updating when hit the end of the file */
1730 if (lnum > buf->b_ml.ml_line_count)
1731 {
1732 eof = TRUE;
1733 break;
1734 }
1735
1736 /* Remember the starting row of the line that is going to be dealt
1737 * with. It is used further down when the line doesn't fit. */
1738 srow = row;
1739
1740 /*
1741 * Update a line when it is in an area that needs updating, when it
1742 * has changes or w_lines[idx] is invalid.
1743 * bot_start may be halfway a wrapped line after using
1744 * win_del_lines(), check if the current line includes it.
1745 * When syntax folding is being used, the saved syntax states will
1746 * already have been updated, we can't see where the syntax state is
1747 * the same again, just update until the end of the window.
1748 */
1749 if (row < top_end
1750 || (row >= mid_start && row < mid_end)
1751#ifdef FEAT_SEARCH_EXTRA
1752 || top_to_mod
1753#endif
1754 || idx >= wp->w_lines_valid
1755 || (row + wp->w_lines[idx].wl_size > bot_start)
1756 || (mod_top != 0
1757 && (lnum == mod_top
1758 || (lnum >= mod_top
1759 && (lnum < mod_bot
1760#ifdef FEAT_SYN_HL
1761 || did_update == DID_FOLD
1762 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001763 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 && (
1765# ifdef FEAT_FOLDING
1766 (foldmethodIsSyntax(wp)
1767 && hasAnyFolding(wp)) ||
1768# endif
1769 syntax_check_changed(lnum)))
1770#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001771#ifdef FEAT_SEARCH_EXTRA
1772 /* match in fixed position might need redraw */
1773 || wp->w_match_head != NULL
1774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 )))))
1776 {
1777#ifdef FEAT_SEARCH_EXTRA
1778 if (lnum == mod_top)
1779 top_to_mod = FALSE;
1780#endif
1781
1782 /*
1783 * When at start of changed lines: May scroll following lines
1784 * up or down to minimize redrawing.
1785 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001786 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 */
1788 if (lnum == mod_top
1789 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001790 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 int old_rows = 0;
1793 int new_rows = 0;
1794 int xtra_rows;
1795 linenr_T l;
1796
1797 /* Count the old number of window rows, using w_lines[], which
1798 * should still contain the sizes for the lines as they are
1799 * currently displayed. */
1800 for (i = idx; i < wp->w_lines_valid; ++i)
1801 {
1802 /* Only valid lines have a meaningful wl_lnum. Invalid
1803 * lines are part of the changed area. */
1804 if (wp->w_lines[i].wl_valid
1805 && wp->w_lines[i].wl_lnum == mod_bot)
1806 break;
1807 old_rows += wp->w_lines[i].wl_size;
1808#ifdef FEAT_FOLDING
1809 if (wp->w_lines[i].wl_valid
1810 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1811 {
1812 /* Must have found the last valid entry above mod_bot.
1813 * Add following invalid entries. */
1814 ++i;
1815 while (i < wp->w_lines_valid
1816 && !wp->w_lines[i].wl_valid)
1817 old_rows += wp->w_lines[i++].wl_size;
1818 break;
1819 }
1820#endif
1821 }
1822
1823 if (i >= wp->w_lines_valid)
1824 {
1825 /* We can't find a valid line below the changed lines,
1826 * need to redraw until the end of the window.
1827 * Inserting/deleting lines has no use. */
1828 bot_start = 0;
1829 }
1830 else
1831 {
1832 /* Able to count old number of rows: Count new window
1833 * rows, and may insert/delete lines */
1834 j = idx;
1835 for (l = lnum; l < mod_bot; ++l)
1836 {
1837#ifdef FEAT_FOLDING
1838 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1839 ++new_rows;
1840 else
1841#endif
1842#ifdef FEAT_DIFF
1843 if (l == wp->w_topline)
1844 new_rows += plines_win_nofill(wp, l, TRUE)
1845 + wp->w_topfill;
1846 else
1847#endif
1848 new_rows += plines_win(wp, l, TRUE);
1849 ++j;
1850 if (new_rows > wp->w_height - row - 2)
1851 {
1852 /* it's getting too much, must redraw the rest */
1853 new_rows = 9999;
1854 break;
1855 }
1856 }
1857 xtra_rows = new_rows - old_rows;
1858 if (xtra_rows < 0)
1859 {
1860 /* May scroll text up. If there is not enough
1861 * remaining text or scrolling fails, must redraw the
1862 * rest. If scrolling works, must redraw the text
1863 * below the scrolled text. */
1864 if (row - xtra_rows >= wp->w_height - 2)
1865 mod_bot = MAXLNUM;
1866 else
1867 {
1868 check_for_delay(FALSE);
1869 if (win_del_lines(wp, row,
1870 -xtra_rows, FALSE, FALSE) == FAIL)
1871 mod_bot = MAXLNUM;
1872 else
1873 bot_start = wp->w_height + xtra_rows;
1874 }
1875 }
1876 else if (xtra_rows > 0)
1877 {
1878 /* May scroll text down. If there is not enough
1879 * remaining text of scrolling fails, must redraw the
1880 * rest. */
1881 if (row + xtra_rows >= wp->w_height - 2)
1882 mod_bot = MAXLNUM;
1883 else
1884 {
1885 check_for_delay(FALSE);
1886 if (win_ins_lines(wp, row + old_rows,
1887 xtra_rows, FALSE, FALSE) == FAIL)
1888 mod_bot = MAXLNUM;
1889 else if (top_end > row + old_rows)
1890 /* Scrolled the part at the top that requires
1891 * updating down. */
1892 top_end += xtra_rows;
1893 }
1894 }
1895
1896 /* When not updating the rest, may need to move w_lines[]
1897 * entries. */
1898 if (mod_bot != MAXLNUM && i != j)
1899 {
1900 if (j < i)
1901 {
1902 int x = row + new_rows;
1903
1904 /* move entries in w_lines[] upwards */
1905 for (;;)
1906 {
1907 /* stop at last valid entry in w_lines[] */
1908 if (i >= wp->w_lines_valid)
1909 {
1910 wp->w_lines_valid = j;
1911 break;
1912 }
1913 wp->w_lines[j] = wp->w_lines[i];
1914 /* stop at a line that won't fit */
1915 if (x + (int)wp->w_lines[j].wl_size
1916 > wp->w_height)
1917 {
1918 wp->w_lines_valid = j + 1;
1919 break;
1920 }
1921 x += wp->w_lines[j++].wl_size;
1922 ++i;
1923 }
1924 if (bot_start > x)
1925 bot_start = x;
1926 }
1927 else /* j > i */
1928 {
1929 /* move entries in w_lines[] downwards */
1930 j -= i;
1931 wp->w_lines_valid += j;
1932 if (wp->w_lines_valid > wp->w_height)
1933 wp->w_lines_valid = wp->w_height;
1934 for (i = wp->w_lines_valid; i - j >= idx; --i)
1935 wp->w_lines[i] = wp->w_lines[i - j];
1936
1937 /* The w_lines[] entries for inserted lines are
1938 * now invalid, but wl_size may be used above.
1939 * Reset to zero. */
1940 while (i >= idx)
1941 {
1942 wp->w_lines[i].wl_size = 0;
1943 wp->w_lines[i--].wl_valid = FALSE;
1944 }
1945 }
1946 }
1947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949
1950#ifdef FEAT_FOLDING
1951 /*
1952 * When lines are folded, display one line for all of them.
1953 * Otherwise, display normally (can be several display lines when
1954 * 'wrap' is on).
1955 */
1956 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1957 if (fold_count != 0)
1958 {
1959 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1960 ++row;
1961 --fold_count;
1962 wp->w_lines[idx].wl_folded = TRUE;
1963 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1964# ifdef FEAT_SYN_HL
1965 did_update = DID_FOLD;
1966# endif
1967 }
1968 else
1969#endif
1970 if (idx < wp->w_lines_valid
1971 && wp->w_lines[idx].wl_valid
1972 && wp->w_lines[idx].wl_lnum == lnum
1973 && lnum > wp->w_topline
1974 && !(dy_flags & DY_LASTLINE)
1975 && srow + wp->w_lines[idx].wl_size > wp->w_height
1976#ifdef FEAT_DIFF
1977 && diff_check_fill(wp, lnum) == 0
1978#endif
1979 )
1980 {
1981 /* This line is not going to fit. Don't draw anything here,
1982 * will draw "@ " lines below. */
1983 row = wp->w_height + 1;
1984 }
1985 else
1986 {
1987#ifdef FEAT_SEARCH_EXTRA
1988 prepare_search_hl(wp, lnum);
1989#endif
1990#ifdef FEAT_SYN_HL
1991 /* Let the syntax stuff know we skipped a few lines. */
1992 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001993 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 syntax_end_parsing(syntax_last_parsed + 1);
1995#endif
1996
1997 /*
1998 * Display one line.
1999 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002000 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002#ifdef FEAT_FOLDING
2003 wp->w_lines[idx].wl_folded = FALSE;
2004 wp->w_lines[idx].wl_lastlnum = lnum;
2005#endif
2006#ifdef FEAT_SYN_HL
2007 did_update = DID_LINE;
2008 syntax_last_parsed = lnum;
2009#endif
2010 }
2011
2012 wp->w_lines[idx].wl_lnum = lnum;
2013 wp->w_lines[idx].wl_valid = TRUE;
2014 if (row > wp->w_height) /* past end of screen */
2015 {
2016 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002017 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2019 ++idx;
2020 break;
2021 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002022 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 wp->w_lines[idx].wl_size = row - srow;
2024 ++idx;
2025#ifdef FEAT_FOLDING
2026 lnum += fold_count + 1;
2027#else
2028 ++lnum;
2029#endif
2030 }
2031 else
2032 {
2033 /* This line does not need updating, advance to the next one */
2034 row += wp->w_lines[idx++].wl_size;
2035 if (row > wp->w_height) /* past end of screen */
2036 break;
2037#ifdef FEAT_FOLDING
2038 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2039#else
2040 ++lnum;
2041#endif
2042#ifdef FEAT_SYN_HL
2043 did_update = DID_NONE;
2044#endif
2045 }
2046
2047 if (lnum > buf->b_ml.ml_line_count)
2048 {
2049 eof = TRUE;
2050 break;
2051 }
2052 }
2053 /*
2054 * End of loop over all window lines.
2055 */
2056
2057
2058 if (idx > wp->w_lines_valid)
2059 wp->w_lines_valid = idx;
2060
2061#ifdef FEAT_SYN_HL
2062 /*
2063 * Let the syntax stuff know we stop parsing here.
2064 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002065 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 syntax_end_parsing(syntax_last_parsed + 1);
2067#endif
2068
2069 /*
2070 * If we didn't hit the end of the file, and we didn't finish the last
2071 * line we were working on, then the line didn't fit.
2072 */
2073 wp->w_empty_rows = 0;
2074#ifdef FEAT_DIFF
2075 wp->w_filler_rows = 0;
2076#endif
2077 if (!eof && !didline)
2078 {
2079 if (lnum == wp->w_topline)
2080 {
2081 /*
2082 * Single line that does not fit!
2083 * Don't overwrite it, it can be edited.
2084 */
2085 wp->w_botline = lnum + 1;
2086 }
2087#ifdef FEAT_DIFF
2088 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2089 {
2090 /* Window ends in filler lines. */
2091 wp->w_botline = lnum;
2092 wp->w_filler_rows = wp->w_height - srow;
2093 }
2094#endif
2095 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2096 {
2097 /*
2098 * Last line isn't finished: Display "@@@" at the end.
2099 */
2100 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2101 W_WINROW(wp) + wp->w_height,
2102 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2103 '@', '@', hl_attr(HLF_AT));
2104 set_empty_rows(wp, srow);
2105 wp->w_botline = lnum;
2106 }
2107 else
2108 {
2109 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2110 wp->w_botline = lnum;
2111 }
2112 }
2113 else
2114 {
2115#ifdef FEAT_VERTSPLIT
2116 draw_vsep_win(wp, row);
2117#endif
2118 if (eof) /* we hit the end of the file */
2119 {
2120 wp->w_botline = buf->b_ml.ml_line_count + 1;
2121#ifdef FEAT_DIFF
2122 j = diff_check_fill(wp, wp->w_botline);
2123 if (j > 0 && !wp->w_botfill)
2124 {
2125 /*
2126 * Display filler lines at the end of the file
2127 */
2128 if (char2cells(fill_diff) > 1)
2129 i = '-';
2130 else
2131 i = fill_diff;
2132 if (row + j > wp->w_height)
2133 j = wp->w_height - row;
2134 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2135 row += j;
2136 }
2137#endif
2138 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002139 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 wp->w_botline = lnum;
2141
2142 /* make sure the rest of the screen is blank */
2143 /* put '~'s on rows that aren't part of the file. */
2144 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2145 }
2146
2147 /* Reset the type of redrawing required, the window has been updated. */
2148 wp->w_redr_type = 0;
2149#ifdef FEAT_DIFF
2150 wp->w_old_topfill = wp->w_topfill;
2151 wp->w_old_botfill = wp->w_botfill;
2152#endif
2153
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002154 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156 /*
2157 * There is a trick with w_botline. If we invalidate it on each
2158 * change that might modify it, this will cause a lot of expensive
2159 * calls to plines() in update_topline() each time. Therefore the
2160 * value of w_botline is often approximated, and this value is used to
2161 * compute the value of w_topline. If the value of w_botline was
2162 * wrong, check that the value of w_topline is correct (cursor is on
2163 * the visible part of the text). If it's not, we need to redraw
2164 * again. Mostly this just means scrolling up a few lines, so it
2165 * doesn't look too bad. Only do this for the current window (where
2166 * changes are relevant).
2167 */
2168 wp->w_valid |= VALID_BOTLINE;
2169 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2170 {
2171 recursive = TRUE;
2172 curwin->w_valid &= ~VALID_TOPLINE;
2173 update_topline(); /* may invalidate w_botline again */
2174 if (must_redraw != 0)
2175 {
2176 /* Don't update for changes in buffer again. */
2177 i = curbuf->b_mod_set;
2178 curbuf->b_mod_set = FALSE;
2179 win_update(curwin);
2180 must_redraw = 0;
2181 curbuf->b_mod_set = i;
2182 }
2183 recursive = FALSE;
2184 }
2185 }
2186
2187#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2188 /* restore got_int, unless CTRL-C was hit while redrawing */
2189 if (!got_int)
2190 got_int = save_got_int;
2191#endif
2192}
2193
2194#ifdef FEAT_SIGNS
2195static int draw_signcolumn __ARGS((win_T *wp));
2196
2197/*
2198 * Return TRUE when window "wp" has a column to draw signs in.
2199 */
2200 static int
2201draw_signcolumn(wp)
2202 win_T *wp;
2203{
2204 return (wp->w_buffer->b_signlist != NULL
2205# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002206 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207# endif
2208 );
2209}
2210#endif
2211
2212/*
2213 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2214 * as the filler character.
2215 */
2216 static void
2217win_draw_end(wp, c1, c2, row, endrow, hl)
2218 win_T *wp;
2219 int c1;
2220 int c2;
2221 int row;
2222 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002223 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2226 int n = 0;
2227# define FDC_OFF n
2228#else
2229# define FDC_OFF 0
2230#endif
2231
2232#ifdef FEAT_RIGHTLEFT
2233 if (wp->w_p_rl)
2234 {
2235 /* No check for cmdline window: should never be right-left. */
2236# ifdef FEAT_FOLDING
2237 n = wp->w_p_fdc;
2238
2239 if (n > 0)
2240 {
2241 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002242 if (n > W_WIDTH(wp))
2243 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2245 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2246 ' ', ' ', hl_attr(HLF_FC));
2247 }
2248# endif
2249# ifdef FEAT_SIGNS
2250 if (draw_signcolumn(wp))
2251 {
2252 int nn = n + 2;
2253
2254 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002255 if (nn > W_WIDTH(wp))
2256 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2258 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2259 ' ', ' ', hl_attr(HLF_SC));
2260 n = nn;
2261 }
2262# endif
2263 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2264 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2265 c2, c2, hl_attr(hl));
2266 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2267 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2268 c1, c2, hl_attr(hl));
2269 }
2270 else
2271#endif
2272 {
2273#ifdef FEAT_CMDWIN
2274 if (cmdwin_type != 0 && wp == curwin)
2275 {
2276 /* draw the cmdline character in the leftmost column */
2277 n = 1;
2278 if (n > wp->w_width)
2279 n = wp->w_width;
2280 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2281 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2282 cmdwin_type, ' ', hl_attr(HLF_AT));
2283 }
2284#endif
2285#ifdef FEAT_FOLDING
2286 if (wp->w_p_fdc > 0)
2287 {
2288 int nn = n + wp->w_p_fdc;
2289
2290 /* draw the fold column at the left */
2291 if (nn > W_WIDTH(wp))
2292 nn = W_WIDTH(wp);
2293 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2294 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2295 ' ', ' ', hl_attr(HLF_FC));
2296 n = nn;
2297 }
2298#endif
2299#ifdef FEAT_SIGNS
2300 if (draw_signcolumn(wp))
2301 {
2302 int nn = n + 2;
2303
2304 /* draw the sign column after the fold column */
2305 if (nn > W_WIDTH(wp))
2306 nn = W_WIDTH(wp);
2307 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2308 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2309 ' ', ' ', hl_attr(HLF_SC));
2310 n = nn;
2311 }
2312#endif
2313 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2314 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2315 c1, c2, hl_attr(hl));
2316 }
2317 set_empty_rows(wp, row);
2318}
2319
Bram Moolenaar1a384422010-07-14 19:53:30 +02002320#ifdef FEAT_SYN_HL
2321static int advance_color_col __ARGS((int vcol, int **color_cols));
2322
2323/*
2324 * Advance **color_cols and return TRUE when there are columns to draw.
2325 */
2326 static int
2327advance_color_col(vcol, color_cols)
2328 int vcol;
2329 int **color_cols;
2330{
2331 while (**color_cols >= 0 && vcol > **color_cols)
2332 ++*color_cols;
2333 return (**color_cols >= 0);
2334}
2335#endif
2336
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337#ifdef FEAT_FOLDING
2338/*
2339 * Display one folded line.
2340 */
2341 static void
2342fold_line(wp, fold_count, foldinfo, lnum, row)
2343 win_T *wp;
2344 long fold_count;
2345 foldinfo_T *foldinfo;
2346 linenr_T lnum;
2347 int row;
2348{
2349 char_u buf[51];
2350 pos_T *top, *bot;
2351 linenr_T lnume = lnum + fold_count - 1;
2352 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002353 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 int col;
2356 int txtcol;
2357 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002358 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 /* Build the fold line:
2361 * 1. Add the cmdwin_type for the command-line window
2362 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002363 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 * 4. Compose the text
2365 * 5. Add the text
2366 * 6. set highlighting for the Visual area an other text
2367 */
2368 col = 0;
2369
2370 /*
2371 * 1. Add the cmdwin_type for the command-line window
2372 * Ignores 'rightleft', this window is never right-left.
2373 */
2374#ifdef FEAT_CMDWIN
2375 if (cmdwin_type != 0 && wp == curwin)
2376 {
2377 ScreenLines[off] = cmdwin_type;
2378 ScreenAttrs[off] = hl_attr(HLF_AT);
2379#ifdef FEAT_MBYTE
2380 if (enc_utf8)
2381 ScreenLinesUC[off] = 0;
2382#endif
2383 ++col;
2384 }
2385#endif
2386
2387 /*
2388 * 2. Add the 'foldcolumn'
2389 */
2390 fdc = wp->w_p_fdc;
2391 if (fdc > W_WIDTH(wp) - col)
2392 fdc = W_WIDTH(wp) - col;
2393 if (fdc > 0)
2394 {
2395 fill_foldcolumn(buf, wp, TRUE, lnum);
2396#ifdef FEAT_RIGHTLEFT
2397 if (wp->w_p_rl)
2398 {
2399 int i;
2400
2401 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2402 hl_attr(HLF_FC));
2403 /* reverse the fold column */
2404 for (i = 0; i < fdc; ++i)
2405 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2406 }
2407 else
2408#endif
2409 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2410 col += fdc;
2411 }
2412
2413#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002414# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2415 for (ri = 0; ri < l; ++ri) \
2416 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2417 else \
2418 for (ri = 0; ri < l; ++ri) \
2419 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002421# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2422 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#endif
2424
Bram Moolenaar64486672010-05-16 15:46:46 +02002425 /* Set all attributes of the 'number' or 'relativenumber' column and the
2426 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002427 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429#ifdef FEAT_SIGNS
2430 /* If signs are being displayed, add two spaces. */
2431 if (draw_signcolumn(wp))
2432 {
2433 len = W_WIDTH(wp) - col;
2434 if (len > 0)
2435 {
2436 if (len > 2)
2437 len = 2;
2438# ifdef FEAT_RIGHTLEFT
2439 if (wp->w_p_rl)
2440 /* the line number isn't reversed */
2441 copy_text_attr(off + W_WIDTH(wp) - len - col,
2442 (char_u *)" ", len, hl_attr(HLF_FL));
2443 else
2444# endif
2445 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2446 col += len;
2447 }
2448 }
2449#endif
2450
2451 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002452 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002454 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
2456 len = W_WIDTH(wp) - col;
2457 if (len > 0)
2458 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002459 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002460 long num;
2461 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002462
2463 if (len > w + 1)
2464 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002465
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002466 if (wp->w_p_nu && !wp->w_p_rnu)
2467 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002468 num = (long)lnum;
2469 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002470 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002471 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002472 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002473 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002474 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002475 /* 'number' + 'relativenumber': cursor line shows absolute
2476 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002477 num = lnum;
2478 fmt = "%-*ld ";
2479 }
2480 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002481
Bram Moolenaar700e7342013-01-30 12:31:36 +01002482 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483#ifdef FEAT_RIGHTLEFT
2484 if (wp->w_p_rl)
2485 /* the line number isn't reversed */
2486 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2487 hl_attr(HLF_FL));
2488 else
2489#endif
2490 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2491 col += len;
2492 }
2493 }
2494
2495 /*
2496 * 4. Compose the folded-line string with 'foldtext', if set.
2497 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002498 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 txtcol = col; /* remember where text starts */
2501
2502 /*
2503 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2504 * Right-left text is put in columns 0 - number-col, normal text is put
2505 * in columns number-col - window-width.
2506 */
2507#ifdef FEAT_MBYTE
2508 if (has_mbyte)
2509 {
2510 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002511 int u8c, u8cc[MAX_MCO];
2512 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 int idx;
2514 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002515 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516# ifdef FEAT_ARABIC
2517 int prev_c = 0; /* previous Arabic character */
2518 int prev_c1 = 0; /* first composing char for prev_c */
2519# endif
2520
2521# ifdef FEAT_RIGHTLEFT
2522 if (wp->w_p_rl)
2523 idx = off;
2524 else
2525# endif
2526 idx = off + col;
2527
2528 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2529 for (p = text; *p != NUL; )
2530 {
2531 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002532 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (col + cells > W_WIDTH(wp)
2534# ifdef FEAT_RIGHTLEFT
2535 - (wp->w_p_rl ? col : 0)
2536# endif
2537 )
2538 break;
2539 ScreenLines[idx] = *p;
2540 if (enc_utf8)
2541 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002542 u8c = utfc_ptr2char(p, u8cc);
2543 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 ScreenLinesUC[idx] = 0;
2546#ifdef FEAT_ARABIC
2547 prev_c = u8c;
2548#endif
2549 }
2550 else
2551 {
2552#ifdef FEAT_ARABIC
2553 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2554 {
2555 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002556 int pc, pc1, nc;
2557 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 int firstbyte = *p;
2559
2560 /* The idea of what is the previous and next
2561 * character depends on 'rightleft'. */
2562 if (wp->w_p_rl)
2563 {
2564 pc = prev_c;
2565 pc1 = prev_c1;
2566 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002567 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 else
2570 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002571 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575 prev_c = u8c;
2576
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002577 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 pc, pc1, nc);
2579 ScreenLines[idx] = firstbyte;
2580 }
2581 else
2582 prev_c = u8c;
2583#endif
2584 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002585#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (u8c >= 0x10000)
2587 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2588 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002591 for (i = 0; i < Screen_mco; ++i)
2592 {
2593 ScreenLinesC[i][idx] = u8cc[i];
2594 if (u8cc[i] == 0)
2595 break;
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598 if (cells > 1)
2599 ScreenLines[idx + 1] = 0;
2600 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002601 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2602 /* double-byte single width character */
2603 ScreenLines2[idx] = p[1];
2604 else if (cells > 1)
2605 /* double-width character */
2606 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 col += cells;
2608 idx += cells;
2609 p += c_len;
2610 }
2611 }
2612 else
2613#endif
2614 {
2615 len = (int)STRLEN(text);
2616 if (len > W_WIDTH(wp) - col)
2617 len = W_WIDTH(wp) - col;
2618 if (len > 0)
2619 {
2620#ifdef FEAT_RIGHTLEFT
2621 if (wp->w_p_rl)
2622 STRNCPY(current_ScreenLine, text, len);
2623 else
2624#endif
2625 STRNCPY(current_ScreenLine + col, text, len);
2626 col += len;
2627 }
2628 }
2629
2630 /* Fill the rest of the line with the fold filler */
2631#ifdef FEAT_RIGHTLEFT
2632 if (wp->w_p_rl)
2633 col -= txtcol;
2634#endif
2635 while (col < W_WIDTH(wp)
2636#ifdef FEAT_RIGHTLEFT
2637 - (wp->w_p_rl ? txtcol : 0)
2638#endif
2639 )
2640 {
2641#ifdef FEAT_MBYTE
2642 if (enc_utf8)
2643 {
2644 if (fill_fold >= 0x80)
2645 {
2646 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002647 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 else
2650 ScreenLinesUC[off + col] = 0;
2651 }
2652#endif
2653 ScreenLines[off + col++] = fill_fold;
2654 }
2655
2656 if (text != buf)
2657 vim_free(text);
2658
2659 /*
2660 * 6. set highlighting for the Visual area an other text.
2661 * If all folded lines are in the Visual area, highlight the line.
2662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2664 {
2665 if (ltoreq(curwin->w_cursor, VIsual))
2666 {
2667 /* Visual is after curwin->w_cursor */
2668 top = &curwin->w_cursor;
2669 bot = &VIsual;
2670 }
2671 else
2672 {
2673 /* Visual is before curwin->w_cursor */
2674 top = &VIsual;
2675 bot = &curwin->w_cursor;
2676 }
2677 if (lnum >= top->lnum
2678 && lnume <= bot->lnum
2679 && (VIsual_mode != 'v'
2680 || ((lnum > top->lnum
2681 || (lnum == top->lnum
2682 && top->col == 0))
2683 && (lnume < bot->lnum
2684 || (lnume == bot->lnum
2685 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002686 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
2688 if (VIsual_mode == Ctrl_V)
2689 {
2690 /* Visual block mode: highlight the chars part of the block */
2691 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2692 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002693 if (wp->w_old_cursor_lcol != MAXCOL
2694 && wp->w_old_cursor_lcol + txtcol
2695 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 len = wp->w_old_cursor_lcol;
2697 else
2698 len = W_WIDTH(wp) - txtcol;
2699 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002700 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702 }
2703 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002706 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002711#ifdef FEAT_SYN_HL
2712 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002713 if (wp->w_p_cuc)
2714 {
2715 txtcol += wp->w_virtcol;
2716 if (wp->w_p_wrap)
2717 txtcol -= wp->w_skipcol;
2718 else
2719 txtcol -= wp->w_leftcol;
2720 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2721 ScreenAttrs[off + txtcol] = hl_combine_attr(
2722 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2723 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2727 (int)W_WIDTH(wp), FALSE);
2728
2729 /*
2730 * Update w_cline_height and w_cline_folded if the cursor line was
2731 * updated (saves a call to plines() later).
2732 */
2733 if (wp == curwin
2734 && lnum <= curwin->w_cursor.lnum
2735 && lnume >= curwin->w_cursor.lnum)
2736 {
2737 curwin->w_cline_row = row;
2738 curwin->w_cline_height = 1;
2739 curwin->w_cline_folded = TRUE;
2740 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2741 }
2742}
2743
2744/*
2745 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2746 */
2747 static void
2748copy_text_attr(off, buf, len, attr)
2749 int off;
2750 char_u *buf;
2751 int len;
2752 int attr;
2753{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002754 int i;
2755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 mch_memmove(ScreenLines + off, buf, (size_t)len);
2757# ifdef FEAT_MBYTE
2758 if (enc_utf8)
2759 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2760# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002761 for (i = 0; i < len; ++i)
2762 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763}
2764
2765/*
2766 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002767 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 */
2769 static void
2770fill_foldcolumn(p, wp, closed, lnum)
2771 char_u *p;
2772 win_T *wp;
2773 int closed; /* TRUE of FALSE */
2774 linenr_T lnum; /* current line number */
2775{
2776 int i = 0;
2777 int level;
2778 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002779 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
2781 /* Init to all spaces. */
2782 copy_spaces(p, (size_t)wp->w_p_fdc);
2783
2784 level = win_foldinfo.fi_level;
2785 if (level > 0)
2786 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002787 /* If there is only one column put more info in it. */
2788 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 /* If the column is too narrow, we start at the lowest level that
2791 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002792 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (first_level < 1)
2794 first_level = 1;
2795
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002796 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
2798 if (win_foldinfo.fi_lnum == lnum
2799 && first_level + i >= win_foldinfo.fi_low_level)
2800 p[i] = '-';
2801 else if (first_level == 1)
2802 p[i] = '|';
2803 else if (first_level + i <= 9)
2804 p[i] = '0' + first_level + i;
2805 else
2806 p[i] = '>';
2807 if (first_level + i == level)
2808 break;
2809 }
2810 }
2811 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002812 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813}
2814#endif /* FEAT_FOLDING */
2815
2816/*
2817 * Display line "lnum" of window 'wp' on the screen.
2818 * Start at row "startrow", stop when "endrow" is reached.
2819 * wp->w_virtcol needs to be valid.
2820 *
2821 * Return the number of last row the line occupies.
2822 */
2823 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002824win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 win_T *wp;
2826 linenr_T lnum;
2827 int startrow;
2828 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002829 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831 int col; /* visual column on screen */
2832 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2833 int c = 0; /* init for GCC */
2834 long vcol = 0; /* virtual column (for tabs) */
2835 long vcol_prev = -1; /* "vcol" of previous character */
2836 char_u *line; /* current line */
2837 char_u *ptr; /* current position in "line" */
2838 int row; /* row in the window, excl w_winrow */
2839 int screen_row; /* row on the screen, incl w_winrow */
2840
2841 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2842 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002843 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 int c_extra = NUL; /* extra chars, all the same */
2845 int extra_attr = 0; /* attributes when n_extra != 0 */
2846 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2847 displaying lcs_eol at end-of-line */
2848 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2849 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2850
2851 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2852 int saved_n_extra = 0;
2853 char_u *saved_p_extra = NULL;
2854 int saved_c_extra = 0;
2855 int saved_char_attr = 0;
2856
2857 int n_attr = 0; /* chars with special attr */
2858 int saved_attr2 = 0; /* char_attr saved for n_attr */
2859 int n_attr3 = 0; /* chars with overruling special attr */
2860 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2861
2862 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2863
2864 int fromcol, tocol; /* start/end of inverting */
2865 int fromcol_prev = -2; /* start of inverting after cursor */
2866 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002868 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 pos_T pos;
2870 long v;
2871
2872 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002873 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2875 in this line */
2876 int attr = 0; /* attributes for area highlighting */
2877 int area_attr = 0; /* attributes desired by highlighting */
2878 int search_attr = 0; /* attributes desired by 'hlsearch' */
2879#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002880 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 int syntax_attr = 0; /* attributes desired by syntax */
2882 int has_syntax = FALSE; /* this buffer has syntax highl. */
2883 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002884 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002885 int draw_color_col = FALSE; /* highlight colorcolumn */
2886 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002887#endif
2888#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002889 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002890# define SPWORDLEN 150
2891 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002892 int nextlinecol = 0; /* column where nextline[] starts */
2893 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002894 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002895 int spell_attr = 0; /* attributes desired by spelling */
2896 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002897 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2898 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002899 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002900 static int cap_col = -1; /* column to check for Cap word */
2901 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002902 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903#endif
2904 int extra_check; /* has syntax or linebreak */
2905#ifdef FEAT_MBYTE
2906 int multi_attr = 0; /* attributes desired by multibyte */
2907 int mb_l = 1; /* multi-byte byte length */
2908 int mb_c = 0; /* decoded multi-byte character */
2909 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002910 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911#endif
2912#ifdef FEAT_DIFF
2913 int filler_lines; /* nr of filler lines to be drawn */
2914 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002915 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 int change_start = MAXCOL; /* first col of changed area */
2917 int change_end = -1; /* last col of changed area */
2918#endif
2919 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2920#ifdef FEAT_LINEBREAK
2921 int need_showbreak = FALSE;
2922#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002923#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2924 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002926 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927#endif
2928#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002929 matchitem_T *cur; /* points to the match list */
2930 match_T *shl; /* points to search_hl or a match */
2931 int shl_flag; /* flag to indicate whether search_hl
2932 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002933 int pos_inprogress; /* marks that position match search is
2934 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002935 int prevcol_hl_flag; /* flag to indicate whether prevcol
2936 equals startcol of search_hl or one
2937 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938#endif
2939#ifdef FEAT_ARABIC
2940 int prev_c = 0; /* previous Arabic character */
2941 int prev_c1 = 0; /* first composing char for prev_c */
2942#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002943#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002944 int did_line_attr = 0;
2945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 /* draw_state: items that are drawn in sequence: */
2948#define WL_START 0 /* nothing done yet */
2949#ifdef FEAT_CMDWIN
2950# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2951#else
2952# define WL_CMDLINE WL_START
2953#endif
2954#ifdef FEAT_FOLDING
2955# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2956#else
2957# define WL_FOLD WL_CMDLINE
2958#endif
2959#ifdef FEAT_SIGNS
2960# define WL_SIGN WL_FOLD + 1 /* column for signs */
2961#else
2962# define WL_SIGN WL_FOLD /* column for signs */
2963#endif
2964#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002965#ifdef FEAT_LINEBREAK
2966# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02002968# define WL_BRI WL_NR
2969#endif
2970#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2971# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
2972#else
2973# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974#endif
2975#define WL_LINE WL_SBR + 1 /* text in the line */
2976 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002977#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 int feedback_col = 0;
2979 int feedback_old_attr = -1;
2980#endif
2981
Bram Moolenaar860cae12010-06-05 23:22:07 +02002982#ifdef FEAT_CONCEAL
2983 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002984 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002985 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002986 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002987 int is_concealing = FALSE;
2988 int boguscols = 0; /* nonexistent columns added to force
2989 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002990 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002991 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002992# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02002993# define FIX_FOR_BOGUSCOLS \
2994 { \
2995 n_extra += vcol_off; \
2996 vcol -= vcol_off; \
2997 vcol_off = 0; \
2998 col -= boguscols; \
2999 boguscols = 0; \
3000 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003001#else
3002# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 if (startrow > endrow) /* past the end already! */
3006 return startrow;
3007
3008 row = startrow;
3009 screen_row = row + W_WINROW(wp);
3010
3011 /*
3012 * To speed up the loop below, set extra_check when there is linebreak,
3013 * trailing white space and/or syntax processing to be done.
3014 */
3015#ifdef FEAT_LINEBREAK
3016 extra_check = wp->w_p_lbr;
3017#else
3018 extra_check = 0;
3019#endif
3020#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003021 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 {
3023 /* Prepare for syntax highlighting in this line. When there is an
3024 * error, stop syntax highlighting. */
3025 save_did_emsg = did_emsg;
3026 did_emsg = FALSE;
3027 syntax_start(wp, lnum);
3028 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003029 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 else
3031 {
3032 did_emsg = save_did_emsg;
3033 has_syntax = TRUE;
3034 extra_check = TRUE;
3035 }
3036 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003037
3038 /* Check for columns to display for 'colorcolumn'. */
3039 color_cols = wp->w_p_cc_cols;
3040 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003041 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003042#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003043
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003044#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003045 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003046 && *wp->w_s->b_p_spl != NUL
3047 && wp->w_s->b_langp.ga_len > 0
3048 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003049 {
3050 /* Prepare for spell checking. */
3051 has_spell = TRUE;
3052 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003053
3054 /* Get the start of the next line, so that words that wrap to the next
3055 * line are found too: "et<line-break>al.".
3056 * Trick: skip a few chars for C/shell/Vim comments */
3057 nextline[SPWORDLEN] = NUL;
3058 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3059 {
3060 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3061 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3062 }
3063
3064 /* When a word wrapped from the previous line the start of the current
3065 * line is valid. */
3066 if (lnum == checked_lnum)
3067 cur_checked_col = checked_col;
3068 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003069
3070 /* When there was a sentence end in the previous line may require a
3071 * word starting with capital in this line. In line 1 always check
3072 * the first word. */
3073 if (lnum != capcol_lnum)
3074 cap_col = -1;
3075 if (lnum == 1)
3076 cap_col = 0;
3077 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079#endif
3080
3081 /*
3082 * handle visual active in this window
3083 */
3084 fromcol = -10;
3085 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3087 {
3088 /* Visual is after curwin->w_cursor */
3089 if (ltoreq(curwin->w_cursor, VIsual))
3090 {
3091 top = &curwin->w_cursor;
3092 bot = &VIsual;
3093 }
3094 else /* Visual is before curwin->w_cursor */
3095 {
3096 top = &VIsual;
3097 bot = &curwin->w_cursor;
3098 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003099 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (VIsual_mode == Ctrl_V) /* block mode */
3101 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003102 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
3104 fromcol = wp->w_old_cursor_fcol;
3105 tocol = wp->w_old_cursor_lcol;
3106 }
3107 }
3108 else /* non-block mode */
3109 {
3110 if (lnum > top->lnum && lnum <= bot->lnum)
3111 fromcol = 0;
3112 else if (lnum == top->lnum)
3113 {
3114 if (VIsual_mode == 'V') /* linewise */
3115 fromcol = 0;
3116 else
3117 {
3118 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3119 if (gchar_pos(top) == NUL)
3120 tocol = fromcol + 1;
3121 }
3122 }
3123 if (VIsual_mode != 'V' && lnum == bot->lnum)
3124 {
3125 if (*p_sel == 'e' && bot->col == 0
3126#ifdef FEAT_VIRTUALEDIT
3127 && bot->coladd == 0
3128#endif
3129 )
3130 {
3131 fromcol = -10;
3132 tocol = MAXCOL;
3133 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003134 else if (bot->col == MAXCOL)
3135 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 else
3137 {
3138 pos = *bot;
3139 if (*p_sel == 'e')
3140 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3141 else
3142 {
3143 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3144 ++tocol;
3145 }
3146 }
3147 }
3148 }
3149
3150#ifndef MSDOS
3151 /* Check if the character under the cursor should not be inverted */
3152 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3153# ifdef FEAT_GUI
3154 && !gui.in_use
3155# endif
3156 )
3157 noinvcur = TRUE;
3158#endif
3159
3160 /* if inverting in this line set area_highlighting */
3161 if (fromcol >= 0)
3162 {
3163 area_highlighting = TRUE;
3164 attr = hl_attr(HLF_V);
3165#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003166 if ((clip_star.available && !clip_star.owned
3167 && clip_isautosel_star())
3168 || (clip_plus.available && !clip_plus.owned
3169 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 attr = hl_attr(HLF_VNC);
3171#endif
3172 }
3173 }
3174
3175 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003176 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003178 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 && wp == curwin
3180 && lnum >= curwin->w_cursor.lnum
3181 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3182 {
3183 if (lnum == curwin->w_cursor.lnum)
3184 getvcol(curwin, &(curwin->w_cursor),
3185 (colnr_T *)&fromcol, NULL, NULL);
3186 else
3187 fromcol = 0;
3188 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3189 {
3190 pos.lnum = lnum;
3191 pos.col = search_match_endcol;
3192 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3193 }
3194 else
3195 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003196 /* do at least one character; happens when past end of line */
3197 if (fromcol == tocol)
3198 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 area_highlighting = TRUE;
3200 attr = hl_attr(HLF_I);
3201 }
3202
3203#ifdef FEAT_DIFF
3204 filler_lines = diff_check(wp, lnum);
3205 if (filler_lines < 0)
3206 {
3207 if (filler_lines == -1)
3208 {
3209 if (diff_find_change(wp, lnum, &change_start, &change_end))
3210 diff_hlf = HLF_ADD; /* added line */
3211 else if (change_start == 0)
3212 diff_hlf = HLF_TXD; /* changed text */
3213 else
3214 diff_hlf = HLF_CHD; /* changed line */
3215 }
3216 else
3217 diff_hlf = HLF_ADD; /* added line */
3218 filler_lines = 0;
3219 area_highlighting = TRUE;
3220 }
3221 if (lnum == wp->w_topline)
3222 filler_lines = wp->w_topfill;
3223 filler_todo = filler_lines;
3224#endif
3225
3226#ifdef LINE_ATTR
3227# ifdef FEAT_SIGNS
3228 /* If this line has a sign with line highlighting set line_attr. */
3229 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3230 if (v != 0)
3231 line_attr = sign_get_attr((int)v, TRUE);
3232# endif
3233# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3234 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003235 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 line_attr = hl_attr(HLF_L);
3237# endif
3238 if (line_attr != 0)
3239 area_highlighting = TRUE;
3240#endif
3241
3242 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3243 ptr = line;
3244
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003245#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003246 if (has_spell)
3247 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003248 /* For checking first word with a capital skip white space. */
3249 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003250 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003251
Bram Moolenaar30abd282005-06-22 22:35:10 +00003252 /* To be able to spell-check over line boundaries copy the end of the
3253 * current line into nextline[]. Above the start of the next line was
3254 * copied to nextline[SPWORDLEN]. */
3255 if (nextline[SPWORDLEN] == NUL)
3256 {
3257 /* No next line or it is empty. */
3258 nextlinecol = MAXCOL;
3259 nextline_idx = 0;
3260 }
3261 else
3262 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003263 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003264 if (v < SPWORDLEN)
3265 {
3266 /* Short line, use it completely and append the start of the
3267 * next line. */
3268 nextlinecol = 0;
3269 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003270 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003271 nextline_idx = v + 1;
3272 }
3273 else
3274 {
3275 /* Long line, use only the last SPWORDLEN bytes. */
3276 nextlinecol = v - SPWORDLEN;
3277 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3278 nextline_idx = SPWORDLEN + 1;
3279 }
3280 }
3281 }
3282#endif
3283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 /* find start of trailing whitespace */
3285 if (wp->w_p_list && lcs_trail)
3286 {
3287 trailcol = (colnr_T)STRLEN(ptr);
3288 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3289 --trailcol;
3290 trailcol += (colnr_T) (ptr - line);
3291 extra_check = TRUE;
3292 }
3293
3294 /*
3295 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3296 * first character to be displayed.
3297 */
3298 if (wp->w_p_wrap)
3299 v = wp->w_skipcol;
3300 else
3301 v = wp->w_leftcol;
3302 if (v > 0)
3303 {
3304#ifdef FEAT_MBYTE
3305 char_u *prev_ptr = ptr;
3306#endif
3307 while (vcol < v && *ptr != NUL)
3308 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003309 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 vcol += c;
3311#ifdef FEAT_MBYTE
3312 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003314 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003317 /* When:
3318 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003319 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003320 * - 'virtualedit' is set, or
3321 * - the visual mode is active,
3322 * the end of the line may be before the start of the displayed part.
3323 */
3324 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003325#ifdef FEAT_SYN_HL
3326 wp->w_p_cuc || draw_color_col ||
3327#endif
3328#ifdef FEAT_VIRTUALEDIT
3329 virtual_active() ||
3330#endif
3331 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 /* Handle a character that's not completely on the screen: Put ptr at
3337 * that character but skip the first few screen characters. */
3338 if (vcol > v)
3339 {
3340 vcol -= c;
3341#ifdef FEAT_MBYTE
3342 ptr = prev_ptr;
3343#else
3344 --ptr;
3345#endif
3346 n_skip = v - vcol;
3347 }
3348
3349 /*
3350 * Adjust for when the inverted text is before the screen,
3351 * and when the start of the inverted text is before the screen.
3352 */
3353 if (tocol <= vcol)
3354 fromcol = 0;
3355 else if (fromcol >= 0 && fromcol < vcol)
3356 fromcol = vcol;
3357
3358#ifdef FEAT_LINEBREAK
3359 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3360 if (wp->w_p_wrap)
3361 need_showbreak = TRUE;
3362#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003363#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003364 /* When spell checking a word we need to figure out the start of the
3365 * word and if it's badly spelled or not. */
3366 if (has_spell)
3367 {
3368 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003369 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003370 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003371
3372 pos = wp->w_cursor;
3373 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003374 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003375 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003376
3377 /* spell_move_to() may call ml_get() and make "line" invalid */
3378 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3379 ptr = line + linecol;
3380
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003381 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003382 {
3383 /* no bad word found at line start, don't check until end of a
3384 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003385 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003386 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003387 }
3388 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003389 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003390 /* bad word found, use attributes until end of word */
3391 word_end = wp->w_cursor.col + len + 1;
3392
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003393 /* Turn index into actual attributes. */
3394 if (spell_hlf != HLF_COUNT)
3395 spell_attr = highlight_attr[spell_hlf];
3396 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003397 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003398
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003399# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003400 /* Need to restart syntax highlighting for this line. */
3401 if (has_syntax)
3402 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003403# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003404 }
3405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
3407
3408 /*
3409 * Correct highlighting for cursor that can't be disabled.
3410 * Avoids having to check this for each character.
3411 */
3412 if (fromcol >= 0)
3413 {
3414 if (noinvcur)
3415 {
3416 if ((colnr_T)fromcol == wp->w_virtcol)
3417 {
3418 /* highlighting starts at cursor, let it start just after the
3419 * cursor */
3420 fromcol_prev = fromcol;
3421 fromcol = -1;
3422 }
3423 else if ((colnr_T)fromcol < wp->w_virtcol)
3424 /* restart highlighting after the cursor */
3425 fromcol_prev = wp->w_virtcol;
3426 }
3427 if (fromcol >= tocol)
3428 fromcol = -1;
3429 }
3430
3431#ifdef FEAT_SEARCH_EXTRA
3432 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003433 * Handle highlighting the last used search pattern and matches.
3434 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003436 cur = wp->w_match_head;
3437 shl_flag = FALSE;
3438 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003440 if (shl_flag == FALSE)
3441 {
3442 shl = &search_hl;
3443 shl_flag = TRUE;
3444 }
3445 else
3446 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003447 shl->startcol = MAXCOL;
3448 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003450 v = (long)(ptr - line);
3451 if (cur != NULL)
3452 cur->pos.cur = 0;
3453 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3454
3455 /* Need to get the line again, a multi-line regexp may have made it
3456 * invalid. */
3457 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3458 ptr = line + v;
3459
3460 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003462 if (shl->lnum == lnum)
3463 shl->startcol = shl->rm.startpos[0].col;
3464 else
3465 shl->startcol = 0;
3466 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3467 - shl->rm.startpos[0].lnum)
3468 shl->endcol = shl->rm.endpos[0].col;
3469 else
3470 shl->endcol = MAXCOL;
3471 /* Highlight one character for an empty match. */
3472 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003475 if (has_mbyte && line[shl->endcol] != NUL)
3476 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3477 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003479 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003481 if ((long)shl->startcol < v) /* match at leftcol */
3482 {
3483 shl->attr_cur = shl->attr;
3484 search_attr = shl->attr;
3485 }
3486 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003488 if (shl != &search_hl && cur != NULL)
3489 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491#endif
3492
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003493#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003494 /* Cursor line highlighting for 'cursorline' in the current window. Not
3495 * when Visual mode is active, because it's not clear what is selected
3496 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003497 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003498 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003499 {
3500 line_attr = hl_attr(HLF_CUL);
3501 area_highlighting = TRUE;
3502 }
3503#endif
3504
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003505 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 col = 0;
3507#ifdef FEAT_RIGHTLEFT
3508 if (wp->w_p_rl)
3509 {
3510 /* Rightleft window: process the text in the normal direction, but put
3511 * it in current_ScreenLine[] from right to left. Start at the
3512 * rightmost column of the window. */
3513 col = W_WIDTH(wp) - 1;
3514 off += col;
3515 }
3516#endif
3517
3518 /*
3519 * Repeat for the whole displayed line.
3520 */
3521 for (;;)
3522 {
3523 /* Skip this quickly when working on the text. */
3524 if (draw_state != WL_LINE)
3525 {
3526#ifdef FEAT_CMDWIN
3527 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3528 {
3529 draw_state = WL_CMDLINE;
3530 if (cmdwin_type != 0 && wp == curwin)
3531 {
3532 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003534 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_attr = hl_attr(HLF_AT);
3536 }
3537 }
3538#endif
3539
3540#ifdef FEAT_FOLDING
3541 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3542 {
3543 draw_state = WL_FOLD;
3544 if (wp->w_p_fdc > 0)
3545 {
3546 /* Draw the 'foldcolumn'. */
3547 fill_foldcolumn(extra, wp, FALSE, lnum);
3548 n_extra = wp->w_p_fdc;
3549 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003550 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 c_extra = NUL;
3552 char_attr = hl_attr(HLF_FC);
3553 }
3554 }
3555#endif
3556
3557#ifdef FEAT_SIGNS
3558 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3559 {
3560 draw_state = WL_SIGN;
3561 /* Show the sign column when there are any signs in this
3562 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003563 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003565 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003567 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568# endif
3569
3570 /* Draw two cells with the sign value or blank. */
3571 c_extra = ' ';
3572 char_attr = hl_attr(HLF_SC);
3573 n_extra = 2;
3574
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003575 if (row == startrow
3576#ifdef FEAT_DIFF
3577 + filler_lines && filler_todo <= 0
3578#endif
3579 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3582 SIGN_TEXT);
3583# ifdef FEAT_SIGN_ICONS
3584 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3585 SIGN_ICON);
3586 if (gui.in_use && icon_sign != 0)
3587 {
3588 /* Use the image in this position. */
3589 c_extra = SIGN_BYTE;
3590# ifdef FEAT_NETBEANS_INTG
3591 if (buf_signcount(wp->w_buffer, lnum) > 1)
3592 c_extra = MULTISIGN_BYTE;
3593# endif
3594 char_attr = icon_sign;
3595 }
3596 else
3597# endif
3598 if (text_sign != 0)
3599 {
3600 p_extra = sign_get_text(text_sign);
3601 if (p_extra != NULL)
3602 {
3603 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003604 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606 char_attr = sign_get_attr(text_sign, FALSE);
3607 }
3608 }
3609 }
3610 }
3611#endif
3612
3613 if (draw_state == WL_NR - 1 && n_extra == 0)
3614 {
3615 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003616 /* Display the absolute or relative line number. After the
3617 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3618 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 && (row == startrow
3620#ifdef FEAT_DIFF
3621 + filler_lines
3622#endif
3623 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3624 {
3625 /* Draw the line number (empty space after wrapping). */
3626 if (row == startrow
3627#ifdef FEAT_DIFF
3628 + filler_lines
3629#endif
3630 )
3631 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003632 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003633 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003634
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003635 if (wp->w_p_nu && !wp->w_p_rnu)
3636 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003637 num = (long)lnum;
3638 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003639 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003640 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003641 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003642 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003643 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003644 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003645 num = lnum;
3646 fmt = "%-*ld ";
3647 }
3648 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003649
Bram Moolenaar700e7342013-01-30 12:31:36 +01003650 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003651 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (wp->w_skipcol > 0)
3653 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3654 *p_extra = '-';
3655#ifdef FEAT_RIGHTLEFT
3656 if (wp->w_p_rl) /* reverse line numbers */
3657 rl_mirror(extra);
3658#endif
3659 p_extra = extra;
3660 c_extra = NUL;
3661 }
3662 else
3663 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003664 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003666#ifdef FEAT_SYN_HL
3667 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003668 * the current line differently.
3669 * TODO: Can we use CursorLine instead of CursorLineNr
3670 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003671 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003672 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003673 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 }
3677
Bram Moolenaar597a4222014-06-25 14:39:50 +02003678#ifdef FEAT_LINEBREAK
3679 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3680 && n_extra == 0 && *p_sbr != NUL)
3681 /* draw indent after showbreak value */
3682 draw_state = WL_BRI;
3683 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3684 /* After the showbreak, draw the breakindent */
3685 draw_state = WL_BRI - 1;
3686
3687 /* draw 'breakindent': indent wrapped text accordingly */
3688 if (draw_state == WL_BRI - 1 && n_extra == 0)
3689 {
3690 draw_state = WL_BRI;
3691# ifdef FEAT_DIFF
3692# endif
3693 if (wp->w_p_bri && n_extra == 0 && row != startrow
3694#ifdef FEAT_DIFF
3695 && filler_lines == 0
3696#endif
3697 )
3698 {
3699 char_attr = 0; /* was: hl_attr(HLF_AT); */
3700#ifdef FEAT_DIFF
3701 if (diff_hlf != (hlf_T)0)
3702 char_attr = hl_attr(diff_hlf);
3703#endif
3704 p_extra = NUL;
3705 c_extra = ' ';
3706 n_extra = get_breakindent_win(wp,
3707 ml_get_buf(wp->w_buffer, lnum, FALSE));
3708 /* Correct end of highlighted area for 'breakindent',
3709 * required when 'linebreak' is also set. */
3710 if (tocol == vcol)
3711 tocol += n_extra;
3712 }
3713 }
3714#endif
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3717 if (draw_state == WL_SBR - 1 && n_extra == 0)
3718 {
3719 draw_state = WL_SBR;
3720# ifdef FEAT_DIFF
3721 if (filler_todo > 0)
3722 {
3723 /* Draw "deleted" diff line(s). */
3724 if (char2cells(fill_diff) > 1)
3725 c_extra = '-';
3726 else
3727 c_extra = fill_diff;
3728# ifdef FEAT_RIGHTLEFT
3729 if (wp->w_p_rl)
3730 n_extra = col + 1;
3731 else
3732# endif
3733 n_extra = W_WIDTH(wp) - col;
3734 char_attr = hl_attr(HLF_DED);
3735 }
3736# endif
3737# ifdef FEAT_LINEBREAK
3738 if (*p_sbr != NUL && need_showbreak)
3739 {
3740 /* Draw 'showbreak' at the start of each broken line. */
3741 p_extra = p_sbr;
3742 c_extra = NUL;
3743 n_extra = (int)STRLEN(p_sbr);
3744 char_attr = hl_attr(HLF_AT);
3745 need_showbreak = FALSE;
3746 /* Correct end of highlighted area for 'showbreak',
3747 * required when 'linebreak' is also set. */
3748 if (tocol == vcol)
3749 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003750#ifdef FEAT_SYN_HL
3751 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003752 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003753 char_attr = hl_combine_attr(char_attr, HLF_CLN);
3754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756# endif
3757 }
3758#endif
3759
3760 if (draw_state == WL_LINE - 1 && n_extra == 0)
3761 {
3762 draw_state = WL_LINE;
3763 if (saved_n_extra)
3764 {
3765 /* Continue item from end of wrapped line. */
3766 n_extra = saved_n_extra;
3767 c_extra = saved_c_extra;
3768 p_extra = saved_p_extra;
3769 char_attr = saved_char_attr;
3770 }
3771 else
3772 char_attr = 0;
3773 }
3774 }
3775
3776 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003777 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003778 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779#ifdef FEAT_DIFF
3780 && filler_todo <= 0
3781#endif
3782 )
3783 {
3784 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3785 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003786 /* Pretend we have finished updating the window. Except when
3787 * 'cursorcolumn' is set. */
3788#ifdef FEAT_SYN_HL
3789 if (wp->w_p_cuc)
3790 row = wp->w_cline_row + wp->w_cline_height;
3791 else
3792#endif
3793 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 break;
3795 }
3796
3797 if (draw_state == WL_LINE && area_highlighting)
3798 {
3799 /* handle Visual or match highlighting in this line */
3800 if (vcol == fromcol
3801#ifdef FEAT_MBYTE
3802 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3803 && (*mb_ptr2cells)(ptr) > 1)
3804#endif
3805 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003806 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 && vcol < tocol))
3808 area_attr = attr; /* start highlighting */
3809 else if (area_attr != 0
3810 && (vcol == tocol
3811 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814#ifdef FEAT_SEARCH_EXTRA
3815 if (!n_extra)
3816 {
3817 /*
3818 * Check for start/end of search pattern match.
3819 * After end, check for start/end of next match.
3820 * When another match, have to check for start again.
3821 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003822 * Do this for 'search_hl' and the match list (ordered by
3823 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003825 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003826 cur = wp->w_match_head;
3827 shl_flag = FALSE;
3828 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003830 if (shl_flag == FALSE
3831 && ((cur != NULL
3832 && cur->priority > SEARCH_HL_PRIORITY)
3833 || cur == NULL))
3834 {
3835 shl = &search_hl;
3836 shl_flag = TRUE;
3837 }
3838 else
3839 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003840 if (cur != NULL)
3841 cur->pos.cur = 0;
3842 pos_inprogress = TRUE;
3843 while (shl->rm.regprog != NULL
3844 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003846 if (shl->startcol != MAXCOL
3847 && v >= (long)shl->startcol
3848 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
3850 shl->attr_cur = shl->attr;
3851 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003852 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
3854 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003855 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3856 pos_inprogress = cur == NULL || cur->pos.cur == 0
3857 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
3859 /* Need to get the line again, a multi-line regexp
3860 * may have made it invalid. */
3861 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3862 ptr = line + v;
3863
3864 if (shl->lnum == lnum)
3865 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003866 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003868 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003870 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003872 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
3874 /* highlight empty match, try again after
3875 * it */
3876#ifdef FEAT_MBYTE
3877 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003878 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003879 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 else
3881#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003882 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884
3885 /* Loop to check if the match starts at the
3886 * current position */
3887 continue;
3888 }
3889 }
3890 break;
3891 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003892 if (shl != &search_hl && cur != NULL)
3893 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003895
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003896 /* Use attributes from match with highest priority among
3897 * 'search_hl' and the match list. */
3898 search_attr = search_hl.attr_cur;
3899 cur = wp->w_match_head;
3900 shl_flag = FALSE;
3901 while (cur != NULL || shl_flag == FALSE)
3902 {
3903 if (shl_flag == FALSE
3904 && ((cur != NULL
3905 && cur->priority > SEARCH_HL_PRIORITY)
3906 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003907 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003908 shl = &search_hl;
3909 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003910 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003911 else
3912 shl = &cur->hl;
3913 if (shl->attr_cur != 0)
3914 search_attr = shl->attr_cur;
3915 if (shl != &search_hl && cur != NULL)
3916 cur = cur->next;
3917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919#endif
3920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003922 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003924 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3925 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003927 if (diff_hlf == HLF_TXD && ptr - line > change_end
3928 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003930 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003933
3934 /* Decide which of the highlight attributes to use. */
3935 attr_pri = TRUE;
3936 if (area_attr != 0)
3937 char_attr = area_attr;
3938 else if (search_attr != 0)
3939 char_attr = search_attr;
3940#ifdef LINE_ATTR
3941 /* Use line_attr when not in the Visual or 'incsearch' area
3942 * (area_attr may be 0 when "noinvcur" is set). */
3943 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003944 || vcol < fromcol || vcol_prev < fromcol_prev
3945 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003946 char_attr = line_attr;
3947#endif
3948 else
3949 {
3950 attr_pri = FALSE;
3951#ifdef FEAT_SYN_HL
3952 if (has_syntax)
3953 char_attr = syntax_attr;
3954 else
3955#endif
3956 char_attr = 0;
3957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959
3960 /*
3961 * Get the next character to put on the screen.
3962 */
3963 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003964 * The "p_extra" points to the extra stuff that is inserted to
3965 * represent special characters (non-printable stuff) and other
3966 * things. When all characters are the same, c_extra is used.
3967 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3968 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3970 */
3971 if (n_extra > 0)
3972 {
3973 if (c_extra != NUL)
3974 {
3975 c = c_extra;
3976#ifdef FEAT_MBYTE
3977 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3978 if (enc_utf8 && (*mb_char2len)(c) > 1)
3979 {
3980 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003981 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003982 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984 else
3985 mb_utf8 = FALSE;
3986#endif
3987 }
3988 else
3989 {
3990 c = *p_extra;
3991#ifdef FEAT_MBYTE
3992 if (has_mbyte)
3993 {
3994 mb_c = c;
3995 if (enc_utf8)
3996 {
3997 /* If the UTF-8 character is more than one byte:
3998 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003999 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 mb_utf8 = FALSE;
4001 if (mb_l > n_extra)
4002 mb_l = 1;
4003 else if (mb_l > 1)
4004 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004005 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004007 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009 }
4010 else
4011 {
4012 /* if this is a DBCS character, put it in "mb_c" */
4013 mb_l = MB_BYTE2LEN(c);
4014 if (mb_l >= n_extra)
4015 mb_l = 1;
4016 else if (mb_l > 1)
4017 mb_c = (c << 8) + p_extra[1];
4018 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004019 if (mb_l == 0) /* at the NUL at end-of-line */
4020 mb_l = 1;
4021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 /* If a double-width char doesn't fit display a '>' in the
4023 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004024 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025# ifdef FEAT_RIGHTLEFT
4026 wp->w_p_rl ? (col <= 0) :
4027# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004028 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 && (*mb_char2cells)(mb_c) == 2)
4030 {
4031 c = '>';
4032 mb_c = c;
4033 mb_l = 1;
4034 mb_utf8 = FALSE;
4035 multi_attr = hl_attr(HLF_AT);
4036 /* put the pointer back to output the double-width
4037 * character at the start of the next line. */
4038 ++n_extra;
4039 --p_extra;
4040 }
4041 else
4042 {
4043 n_extra -= mb_l - 1;
4044 p_extra += mb_l - 1;
4045 }
4046 }
4047#endif
4048 ++p_extra;
4049 }
4050 --n_extra;
4051 }
4052 else
4053 {
4054 /*
4055 * Get a character from the line itself.
4056 */
4057 c = *ptr;
4058#ifdef FEAT_MBYTE
4059 if (has_mbyte)
4060 {
4061 mb_c = c;
4062 if (enc_utf8)
4063 {
4064 /* If the UTF-8 character is more than one byte: Decode it
4065 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004066 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 mb_utf8 = FALSE;
4068 if (mb_l > 1)
4069 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004070 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 /* Overlong encoded ASCII or ASCII with composing char
4072 * is displayed normally, except a NUL. */
4073 if (mb_c < 0x80)
4074 c = mb_c;
4075 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004076
4077 /* At start of the line we can have a composing char.
4078 * Draw it as a space with a composing char. */
4079 if (utf_iscomposing(mb_c))
4080 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004081 int i;
4082
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004083 for (i = Screen_mco - 1; i > 0; --i)
4084 u8cc[i] = u8cc[i - 1];
4085 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004086 mb_c = ' ';
4087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089
4090 if ((mb_l == 1 && c >= 0x80)
4091 || (mb_l >= 1 && mb_c == 0)
4092 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004093# ifdef UNICODE16
4094 || mb_c >= 0x10000
4095# endif
4096 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
4098 /*
4099 * Illegal UTF-8 byte: display as <xx>.
4100 * Non-BMP character : display as ? or fullwidth ?.
4101 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004102# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
4106 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004107# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (wp->w_p_rl) /* reverse */
4109 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004112# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 else if (utf_char2cells(mb_c) != 2)
4114 STRCPY(extra, "?");
4115 else
4116 /* 0xff1f in UTF-8: full-width '?' */
4117 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120 p_extra = extra;
4121 c = *p_extra;
4122 mb_c = mb_ptr2char_adv(&p_extra);
4123 mb_utf8 = (c >= 0x80);
4124 n_extra = (int)STRLEN(p_extra);
4125 c_extra = NUL;
4126 if (area_attr == 0 && search_attr == 0)
4127 {
4128 n_attr = n_extra + 1;
4129 extra_attr = hl_attr(HLF_8);
4130 saved_attr2 = char_attr; /* save current attr */
4131 }
4132 }
4133 else if (mb_l == 0) /* at the NUL at end-of-line */
4134 mb_l = 1;
4135#ifdef FEAT_ARABIC
4136 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4137 {
4138 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004139 int pc, pc1, nc;
4140 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /* The idea of what is the previous and next
4143 * character depends on 'rightleft'. */
4144 if (wp->w_p_rl)
4145 {
4146 pc = prev_c;
4147 pc1 = prev_c1;
4148 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004149 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 }
4151 else
4152 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004153 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004155 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157 prev_c = mb_c;
4158
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004159 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
4161 else
4162 prev_c = mb_c;
4163#endif
4164 }
4165 else /* enc_dbcs */
4166 {
4167 mb_l = MB_BYTE2LEN(c);
4168 if (mb_l == 0) /* at the NUL at end-of-line */
4169 mb_l = 1;
4170 else if (mb_l > 1)
4171 {
4172 /* We assume a second byte below 32 is illegal.
4173 * Hopefully this is OK for all double-byte encodings!
4174 */
4175 if (ptr[1] >= 32)
4176 mb_c = (c << 8) + ptr[1];
4177 else
4178 {
4179 if (ptr[1] == NUL)
4180 {
4181 /* head byte at end of line */
4182 mb_l = 1;
4183 transchar_nonprint(extra, c);
4184 }
4185 else
4186 {
4187 /* illegal tail byte */
4188 mb_l = 2;
4189 STRCPY(extra, "XX");
4190 }
4191 p_extra = extra;
4192 n_extra = (int)STRLEN(extra) - 1;
4193 c_extra = NUL;
4194 c = *p_extra++;
4195 if (area_attr == 0 && search_attr == 0)
4196 {
4197 n_attr = n_extra + 1;
4198 extra_attr = hl_attr(HLF_8);
4199 saved_attr2 = char_attr; /* save current attr */
4200 }
4201 mb_c = c;
4202 }
4203 }
4204 }
4205 /* If a double-width char doesn't fit display a '>' in the
4206 * last column; the character is displayed at the start of the
4207 * next line. */
4208 if ((
4209# ifdef FEAT_RIGHTLEFT
4210 wp->w_p_rl ? (col <= 0) :
4211# endif
4212 (col >= W_WIDTH(wp) - 1))
4213 && (*mb_char2cells)(mb_c) == 2)
4214 {
4215 c = '>';
4216 mb_c = c;
4217 mb_utf8 = FALSE;
4218 mb_l = 1;
4219 multi_attr = hl_attr(HLF_AT);
4220 /* Put pointer back so that the character will be
4221 * displayed at the start of the next line. */
4222 --ptr;
4223 }
4224 else if (*ptr != NUL)
4225 ptr += mb_l - 1;
4226
4227 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004228 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004229 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004230 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004233 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 c = ' ';
4235 if (area_attr == 0 && search_attr == 0)
4236 {
4237 n_attr = n_extra + 1;
4238 extra_attr = hl_attr(HLF_AT);
4239 saved_attr2 = char_attr; /* save current attr */
4240 }
4241 mb_c = c;
4242 mb_utf8 = FALSE;
4243 mb_l = 1;
4244 }
4245
4246 }
4247#endif
4248 ++ptr;
4249
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004250 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004251 if (wp->w_p_list && (c == 160
4252#ifdef FEAT_MBYTE
4253 || (mb_utf8 && mb_c == 160)
4254#endif
4255 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004256 {
4257 c = lcs_nbsp;
4258 if (area_attr == 0 && search_attr == 0)
4259 {
4260 n_attr = 1;
4261 extra_attr = hl_attr(HLF_8);
4262 saved_attr2 = char_attr; /* save current attr */
4263 }
4264#ifdef FEAT_MBYTE
4265 mb_c = c;
4266 if (enc_utf8 && (*mb_char2len)(c) > 1)
4267 {
4268 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004269 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004270 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004271 }
4272 else
4273 mb_utf8 = FALSE;
4274#endif
4275 }
4276
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (extra_check)
4278 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004279#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004280 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004281#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004282
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004283#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 /* Get syntax attribute, unless still at the start of the line
4285 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004286 v = (long)(ptr - line);
4287 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 /* Get the syntax attribute for the character. If there
4290 * is an error, disable syntax highlighting. */
4291 save_did_emsg = did_emsg;
4292 did_emsg = FALSE;
4293
Bram Moolenaar217ad922005-03-20 22:37:15 +00004294 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004295# ifdef FEAT_SPELL
4296 has_spell ? &can_spell :
4297# endif
4298 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
4300 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004301 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004302 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004303 has_syntax = FALSE;
4304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 else
4306 did_emsg = save_did_emsg;
4307
4308 /* Need to get the line again, a multi-line regexp may
4309 * have made it invalid. */
4310 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4311 ptr = line + v;
4312
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004313 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004315 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004316 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004317# ifdef FEAT_CONCEAL
4318 /* no concealing past the end of the line, it interferes
4319 * with line highlighting */
4320 if (c == NUL)
4321 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004322 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004323 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004326#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004327
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004328#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004329 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004330 * Only do this when there is no syntax highlighting, the
4331 * @Spell cluster is not used or the current syntax item
4332 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004333 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004334 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004335 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004336# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004337 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004338 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004339# endif
4340 if (c != 0 && (
4341# ifdef FEAT_SYN_HL
4342 !has_syntax ||
4343# endif
4344 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004345 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004346 char_u *prev_ptr, *p;
4347 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004348 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004349# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004350 if (has_mbyte)
4351 {
4352 prev_ptr = ptr - mb_l;
4353 v -= mb_l - 1;
4354 }
4355 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004356# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004357 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004358
4359 /* Use nextline[] if possible, it has the start of the
4360 * next line concatenated. */
4361 if ((prev_ptr - line) - nextlinecol >= 0)
4362 p = nextline + (prev_ptr - line) - nextlinecol;
4363 else
4364 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004365 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004366 len = spell_check(wp, p, &spell_hlf, &cap_col,
4367 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004368 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004369
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004370 /* In Insert mode only highlight a word that
4371 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004372 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004373 && (State & INSERT) != 0
4374 && wp->w_cursor.lnum == lnum
4375 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004376 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004377 && wp->w_cursor.col < (colnr_T)word_end)
4378 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004379 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004380 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004381 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004382
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004383 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004384 && (p - nextline) + len > nextline_idx)
4385 {
4386 /* Remember that the good word continues at the
4387 * start of the next line. */
4388 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004389 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004390 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004391
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004392 /* Turn index into actual attributes. */
4393 if (spell_hlf != HLF_COUNT)
4394 spell_attr = highlight_attr[spell_hlf];
4395
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004396 if (cap_col > 0)
4397 {
4398 if (p != prev_ptr
4399 && (p - nextline) + cap_col >= nextline_idx)
4400 {
4401 /* Remember that the word in the next line
4402 * must start with a capital. */
4403 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004404 cap_col = (int)((p - nextline) + cap_col
4405 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004406 }
4407 else
4408 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004409 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004410 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004411 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004412 }
4413 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004414 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004415 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004416 char_attr = hl_combine_attr(char_attr, spell_attr);
4417 else
4418 char_attr = hl_combine_attr(spell_attr, char_attr);
4419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420#endif
4421#ifdef FEAT_LINEBREAK
4422 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004423 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 */
4425 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004426 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004428 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429# ifdef FEAT_MBYTE
4430 has_mbyte ? mb_l :
4431# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004432 1);
4433 /* TODO: is passing p for start of the line OK? */
4434 n_extra = win_lbr_chartabsize(wp, p, p, (colnr_T)vcol,
4435 NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 c_extra = ' ';
4437 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004438 {
4439#ifdef FEAT_CONCEAL
4440 if (c == TAB)
4441 /* See "Tab alignment" below. */
4442 FIX_FOR_BOGUSCOLS;
4443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447#endif
4448
4449 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4450 {
4451 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004452 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
4454 n_attr = 1;
4455 extra_attr = hl_attr(HLF_8);
4456 saved_attr2 = char_attr; /* save current attr */
4457 }
4458#ifdef FEAT_MBYTE
4459 mb_c = c;
4460 if (enc_utf8 && (*mb_char2len)(c) > 1)
4461 {
4462 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004463 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004464 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466 else
4467 mb_utf8 = FALSE;
4468#endif
4469 }
4470 }
4471
4472 /*
4473 * Handling of non-printable characters.
4474 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004475 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 /*
4478 * when getting a character from the file, we may have to
4479 * turn it into something else on the way to putting it
4480 * into "ScreenLines".
4481 */
4482 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4483 {
4484 /* tab amount depends on current column */
4485 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004486 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4487#ifdef FEAT_CONCEAL
4488 /* Tab alignment should be identical regardless of
4489 * 'conceallevel' value. So tab compensates of all
4490 * previous concealed characters, and thus resets vcol_off
4491 * and boguscols accumulated so far in the line. Note that
4492 * the tab can be longer than 'tabstop' when there
4493 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004494 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496#ifdef FEAT_MBYTE
4497 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4498#endif
4499 if (wp->w_p_list)
4500 {
4501 c = lcs_tab1;
4502 c_extra = lcs_tab2;
4503 n_attr = n_extra + 1;
4504 extra_attr = hl_attr(HLF_8);
4505 saved_attr2 = char_attr; /* save current attr */
4506#ifdef FEAT_MBYTE
4507 mb_c = c;
4508 if (enc_utf8 && (*mb_char2len)(c) > 1)
4509 {
4510 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004511 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004512 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514#endif
4515 }
4516 else
4517 {
4518 c_extra = ' ';
4519 c = ' ';
4520 }
4521 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004522 else if (c == NUL
4523 && ((wp->w_p_list && lcs_eol > 0)
4524 || ((fromcol >= 0 || fromcol_prev >= 0)
4525 && tocol > vcol
4526 && VIsual_mode != Ctrl_V
4527 && (
4528# ifdef FEAT_RIGHTLEFT
4529 wp->w_p_rl ? (col >= 0) :
4530# endif
4531 (col < W_WIDTH(wp)))
4532 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004533 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004534 && (colnr_T)vcol == wp->w_virtcol)))
4535 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004537 /* Display a '$' after the line or highlight an extra
4538 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4540 /* For a diff line the highlighting continues after the
4541 * "$". */
4542 if (
4543# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004544 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545# ifdef LINE_ATTR
4546 &&
4547# endif
4548# endif
4549# ifdef LINE_ATTR
4550 line_attr == 0
4551# endif
4552 )
4553#endif
4554 {
4555#ifdef FEAT_VIRTUALEDIT
4556 /* In virtualedit, visual selections may extend
4557 * beyond end of line. */
4558 if (area_highlighting && virtual_active()
4559 && tocol != MAXCOL && vcol < tocol)
4560 n_extra = 0;
4561 else
4562#endif
4563 {
4564 p_extra = at_end_str;
4565 n_extra = 1;
4566 c_extra = NUL;
4567 }
4568 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004569 if (wp->w_p_list)
4570 c = lcs_eol;
4571 else
4572 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 lcs_eol_one = -1;
4574 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004575 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
4577 extra_attr = hl_attr(HLF_AT);
4578 n_attr = 1;
4579 }
4580#ifdef FEAT_MBYTE
4581 mb_c = c;
4582 if (enc_utf8 && (*mb_char2len)(c) > 1)
4583 {
4584 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004585 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004586 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 else
4589 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4590#endif
4591 }
4592 else if (c != NUL)
4593 {
4594 p_extra = transchar(c);
4595#ifdef FEAT_RIGHTLEFT
4596 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4597 rl_mirror(p_extra); /* reverse "<12>" */
4598#endif
4599 n_extra = byte2cells(c) - 1;
4600 c_extra = NUL;
4601 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004602 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
4604 n_attr = n_extra + 1;
4605 extra_attr = hl_attr(HLF_8);
4606 saved_attr2 = char_attr; /* save current attr */
4607 }
4608#ifdef FEAT_MBYTE
4609 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4610#endif
4611 }
4612#ifdef FEAT_VIRTUALEDIT
4613 else if (VIsual_active
4614 && (VIsual_mode == Ctrl_V
4615 || VIsual_mode == 'v')
4616 && virtual_active()
4617 && tocol != MAXCOL
4618 && vcol < tocol
4619 && (
4620# ifdef FEAT_RIGHTLEFT
4621 wp->w_p_rl ? (col >= 0) :
4622# endif
4623 (col < W_WIDTH(wp))))
4624 {
4625 c = ' ';
4626 --ptr; /* put it back at the NUL */
4627 }
4628#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004629#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 else if ((
4631# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004632 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 ) && (
4636# ifdef FEAT_RIGHTLEFT
4637 wp->w_p_rl ? (col >= 0) :
4638# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004639 (col
4640# ifdef FEAT_CONCEAL
4641 - boguscols
4642# endif
4643 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
4645 /* Highlight until the right side of the window */
4646 c = ' ';
4647 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004648
4649 /* Remember we do the char for line highlighting. */
4650 ++did_line_attr;
4651
4652 /* don't do search HL for the rest of the line */
4653 if (line_attr != 0 && char_attr == search_attr && col > 0)
4654 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655# ifdef FEAT_DIFF
4656 if (diff_hlf == HLF_TXD)
4657 {
4658 diff_hlf = HLF_CHD;
4659 if (attr == 0 || char_attr != attr)
4660 char_attr = hl_attr(diff_hlf);
4661 }
4662# endif
4663 }
4664#endif
4665 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004666
4667#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004668 if ( wp->w_p_cole > 0
4669 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4670 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004671 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004672 && !(lnum_in_visual_area
4673 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004674 {
4675 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004676 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004677 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4678 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004679 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004680 /* First time at this concealed item: display one
4681 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004682 if (syn_get_sub_char() != NUL)
4683 c = syn_get_sub_char();
4684 else if (lcs_conceal != NUL)
4685 c = lcs_conceal;
4686 else
4687 c = ' ';
4688
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004689 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004690
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004691 if (n_extra > 0)
4692 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004693 vcol += n_extra;
4694 if (wp->w_p_wrap && n_extra > 0)
4695 {
4696# ifdef FEAT_RIGHTLEFT
4697 if (wp->w_p_rl)
4698 {
4699 col -= n_extra;
4700 boguscols -= n_extra;
4701 }
4702 else
4703# endif
4704 {
4705 boguscols += n_extra;
4706 col += n_extra;
4707 }
4708 }
4709 n_extra = 0;
4710 n_attr = 0;
4711 }
4712 else if (n_skip == 0)
4713 {
4714 is_concealing = TRUE;
4715 n_skip = 1;
4716 }
4717# ifdef FEAT_MBYTE
4718 mb_c = c;
4719 if (enc_utf8 && (*mb_char2len)(c) > 1)
4720 {
4721 mb_utf8 = TRUE;
4722 u8cc[0] = 0;
4723 c = 0xc0;
4724 }
4725 else
4726 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4727# endif
4728 }
4729 else
4730 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004731 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004732 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004733 }
4734#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004737#ifdef FEAT_CONCEAL
4738 /* In the cursor line and we may be concealing characters: correct
4739 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004740 if (!did_wcol && draw_state == WL_LINE
4741 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004742 && conceal_cursor_line(wp)
4743 && (int)wp->w_virtcol <= vcol + n_skip)
4744 {
4745 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004746 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004747 did_wcol = TRUE;
4748 }
4749#endif
4750
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 /* Don't override visual selection highlighting. */
4752 if (n_attr > 0
4753 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004754 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 char_attr = extra_attr;
4756
Bram Moolenaar81695252004-12-29 20:58:21 +00004757#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 /* XIM don't send preedit_start and preedit_end, but they send
4759 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4760 * im_is_preediting() here. */
4761 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004762 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 && (State & INSERT)
4764 && !p_imdisable
4765 && im_is_preediting()
4766 && draw_state == WL_LINE)
4767 {
4768 colnr_T tcol;
4769
4770 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004771 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 else
4773 tcol = preedit_end_col;
4774 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4775 {
4776 if (feedback_old_attr < 0)
4777 {
4778 feedback_col = 0;
4779 feedback_old_attr = char_attr;
4780 }
4781 char_attr = im_get_feedback_attr(feedback_col);
4782 if (char_attr < 0)
4783 char_attr = feedback_old_attr;
4784 feedback_col++;
4785 }
4786 else if (feedback_old_attr >= 0)
4787 {
4788 char_attr = feedback_old_attr;
4789 feedback_old_attr = -1;
4790 feedback_col = 0;
4791 }
4792 }
4793#endif
4794 /*
4795 * Handle the case where we are in column 0 but not on the first
4796 * character of the line and the user wants us to show us a
4797 * special character (via 'listchars' option "precedes:<char>".
4798 */
4799 if (lcs_prec_todo != NUL
4800 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4801#ifdef FEAT_DIFF
4802 && filler_todo <= 0
4803#endif
4804 && draw_state > WL_NR
4805 && c != NUL)
4806 {
4807 c = lcs_prec;
4808 lcs_prec_todo = NUL;
4809#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004810 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4811 {
4812 /* Double-width character being overwritten by the "precedes"
4813 * character, need to fill up half the character. */
4814 c_extra = MB_FILLER_CHAR;
4815 n_extra = 1;
4816 n_attr = 2;
4817 extra_attr = hl_attr(HLF_AT);
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 mb_c = c;
4820 if (enc_utf8 && (*mb_char2len)(c) > 1)
4821 {
4822 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004823 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004824 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826 else
4827 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4828#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004829 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
4831 saved_attr3 = char_attr; /* save current attr */
4832 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4833 n_attr3 = 1;
4834 }
4835 }
4836
4837 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004838 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004840 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004841#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004842 || did_line_attr == 1
4843#endif
4844 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004846#ifdef FEAT_SEARCH_EXTRA
4847 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004848
4849 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004850 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004851 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004852#endif
4853
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004854 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 * highlight match at end of line. If it's beyond the last
4856 * char on the screen, just overwrite that one (tricky!) Not
4857 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004858#ifdef FEAT_SEARCH_EXTRA
4859 prevcol_hl_flag = FALSE;
4860 if (prevcol == (long)search_hl.startcol)
4861 prevcol_hl_flag = TRUE;
4862 else
4863 {
4864 cur = wp->w_match_head;
4865 while (cur != NULL)
4866 {
4867 if (prevcol == (long)cur->hl.startcol)
4868 {
4869 prevcol_hl_flag = TRUE;
4870 break;
4871 }
4872 cur = cur->next;
4873 }
4874 }
4875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004877 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004878 && (VIsual_mode != Ctrl_V
4879 || lnum == VIsual.lnum
4880 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004881 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882#ifdef FEAT_SEARCH_EXTRA
4883 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004884 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004885# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004886 && did_line_attr <= 1
4887# endif
4888 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889#endif
4890 ))
4891 {
4892 int n = 0;
4893
4894#ifdef FEAT_RIGHTLEFT
4895 if (wp->w_p_rl)
4896 {
4897 if (col < 0)
4898 n = 1;
4899 }
4900 else
4901#endif
4902 {
4903 if (col >= W_WIDTH(wp))
4904 n = -1;
4905 }
4906 if (n != 0)
4907 {
4908 /* At the window boundary, highlight the last character
4909 * instead (better than nothing). */
4910 off += n;
4911 col += n;
4912 }
4913 else
4914 {
4915 /* Add a blank character to highlight. */
4916 ScreenLines[off] = ' ';
4917#ifdef FEAT_MBYTE
4918 if (enc_utf8)
4919 ScreenLinesUC[off] = 0;
4920#endif
4921 }
4922#ifdef FEAT_SEARCH_EXTRA
4923 if (area_attr == 0)
4924 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004925 /* Use attributes from match with highest priority among
4926 * 'search_hl' and the match list. */
4927 char_attr = search_hl.attr;
4928 cur = wp->w_match_head;
4929 shl_flag = FALSE;
4930 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004931 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004932 if (shl_flag == FALSE
4933 && ((cur != NULL
4934 && cur->priority > SEARCH_HL_PRIORITY)
4935 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004936 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004937 shl = &search_hl;
4938 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004939 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004940 else
4941 shl = &cur->hl;
4942 if ((ptr - line) - 1 == (long)shl->startcol)
4943 char_attr = shl->attr;
4944 if (shl != &search_hl && cur != NULL)
4945 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948#endif
4949 ScreenAttrs[off] = char_attr;
4950#ifdef FEAT_RIGHTLEFT
4951 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004954 --off;
4955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 else
4957#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004960 ++off;
4961 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004962 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004963#ifdef FEAT_SYN_HL
4964 eol_hl_off = 1;
4965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
Bram Moolenaar91170f82006-05-05 21:15:17 +00004969 /*
4970 * At end of the text line.
4971 */
4972 if (c == NUL)
4973 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004974#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004975 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4976 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004977 {
4978 /* highlight last char after line */
4979 --col;
4980 --off;
4981 --vcol;
4982 }
4983
Bram Moolenaar1a384422010-07-14 19:53:30 +02004984 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004985 if (wp->w_p_wrap)
4986 v = wp->w_skipcol;
4987 else
4988 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004989
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004990 /* check if line ends before left margin */
4991 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004992 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004993#ifdef FEAT_CONCEAL
4994 /* Get rid of the boguscols now, we want to draw until the right
4995 * edge for 'cursorcolumn'. */
4996 col -= boguscols;
4997 boguscols = 0;
4998#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004999
5000 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005001 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005002
5003 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005004 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5005 && (int)wp->w_virtcol <
5006 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005007 && lnum != wp->w_cursor.lnum)
5008 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005009# ifdef FEAT_RIGHTLEFT
5010 && !wp->w_p_rl
5011# endif
5012 )
5013 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005014 int rightmost_vcol = 0;
5015 int i;
5016
5017 if (wp->w_p_cuc)
5018 rightmost_vcol = wp->w_virtcol;
5019 if (draw_color_col)
5020 /* determine rightmost colorcolumn to possibly draw */
5021 for (i = 0; color_cols[i] >= 0; ++i)
5022 if (rightmost_vcol < color_cols[i])
5023 rightmost_vcol = color_cols[i];
5024
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005025 while (col < W_WIDTH(wp))
5026 {
5027 ScreenLines[off] = ' ';
5028#ifdef FEAT_MBYTE
5029 if (enc_utf8)
5030 ScreenLinesUC[off] = 0;
5031#endif
5032 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005033 if (draw_color_col)
5034 draw_color_col = advance_color_col(VCOL_HLC,
5035 &color_cols);
5036
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005037 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005038 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005039 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005040 ScreenAttrs[off++] = hl_attr(HLF_MC);
5041 else
5042 ScreenAttrs[off++] = 0;
5043
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005044 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005045 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005046
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005047 ++vcol;
5048 }
5049 }
5050#endif
5051
Bram Moolenaar860cae12010-06-05 23:22:07 +02005052 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5053 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 row++;
5055
5056 /*
5057 * Update w_cline_height and w_cline_folded if the cursor line was
5058 * updated (saves a call to plines() later).
5059 */
5060 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5061 {
5062 curwin->w_cline_row = startrow;
5063 curwin->w_cline_height = row - startrow;
5064#ifdef FEAT_FOLDING
5065 curwin->w_cline_folded = FALSE;
5066#endif
5067 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5068 }
5069
5070 break;
5071 }
5072
5073 /* line continues beyond line end */
5074 if (lcs_ext
5075 && !wp->w_p_wrap
5076#ifdef FEAT_DIFF
5077 && filler_todo <= 0
5078#endif
5079 && (
5080#ifdef FEAT_RIGHTLEFT
5081 wp->w_p_rl ? col == 0 :
5082#endif
5083 col == W_WIDTH(wp) - 1)
5084 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005085 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5087 {
5088 c = lcs_ext;
5089 char_attr = hl_attr(HLF_AT);
5090#ifdef FEAT_MBYTE
5091 mb_c = c;
5092 if (enc_utf8 && (*mb_char2len)(c) > 1)
5093 {
5094 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005095 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005096 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 else
5099 mb_utf8 = FALSE;
5100#endif
5101 }
5102
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005103#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005104 /* advance to the next 'colorcolumn' */
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
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005108 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005109 * highlight the cursor position itself.
5110 * Also highlight the 'colorcolumn' if it is different than
5111 * 'cursorcolumn' */
5112 vcol_save_attr = -1;
5113 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005114 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005115 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005116 && lnum != wp->w_cursor.lnum)
5117 {
5118 vcol_save_attr = char_attr;
5119 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5120 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005121 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005122 {
5123 vcol_save_attr = char_attr;
5124 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5125 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005126 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005127#endif
5128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 /*
5130 * Store character to be displayed.
5131 * Skip characters that are left of the screen for 'nowrap'.
5132 */
5133 vcol_prev = vcol;
5134 if (draw_state < WL_LINE || n_skip <= 0)
5135 {
5136 /*
5137 * Store the character.
5138 */
5139#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5140 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5141 {
5142 /* A double-wide character is: put first halve in left cell. */
5143 --off;
5144 --col;
5145 }
5146#endif
5147 ScreenLines[off] = c;
5148#ifdef FEAT_MBYTE
5149 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005150 {
5151 if ((mb_c & 0xff00) == 0x8e00)
5152 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 else if (enc_utf8)
5156 {
5157 if (mb_utf8)
5158 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005159 int i;
5160
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005162 if ((c & 0xff) == 0)
5163 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005164 for (i = 0; i < Screen_mco; ++i)
5165 {
5166 ScreenLinesC[i][off] = u8cc[i];
5167 if (u8cc[i] == 0)
5168 break;
5169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171 else
5172 ScreenLinesUC[off] = 0;
5173 }
5174 if (multi_attr)
5175 {
5176 ScreenAttrs[off] = multi_attr;
5177 multi_attr = 0;
5178 }
5179 else
5180#endif
5181 ScreenAttrs[off] = char_attr;
5182
5183#ifdef FEAT_MBYTE
5184 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5185 {
5186 /* Need to fill two screen columns. */
5187 ++off;
5188 ++col;
5189 if (enc_utf8)
5190 /* UTF-8: Put a 0 in the second screen char. */
5191 ScreenLines[off] = 0;
5192 else
5193 /* DBCS: Put second byte in the second screen char. */
5194 ScreenLines[off] = mb_c & 0xff;
5195 ++vcol;
5196 /* When "tocol" is halfway a character, set it to the end of
5197 * the character, otherwise highlighting won't stop. */
5198 if (tocol == vcol)
5199 ++tocol;
5200#ifdef FEAT_RIGHTLEFT
5201 if (wp->w_p_rl)
5202 {
5203 /* now it's time to backup one cell */
5204 --off;
5205 --col;
5206 }
5207#endif
5208 }
5209#endif
5210#ifdef FEAT_RIGHTLEFT
5211 if (wp->w_p_rl)
5212 {
5213 --off;
5214 --col;
5215 }
5216 else
5217#endif
5218 {
5219 ++off;
5220 ++col;
5221 }
5222 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005223#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005224 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005225 {
5226 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005227 ++vcol_off;
5228 if (n_extra > 0)
5229 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005230 if (wp->w_p_wrap)
5231 {
5232 /*
5233 * Special voodoo required if 'wrap' is on.
5234 *
5235 * Advance the column indicator to force the line
5236 * drawing to wrap early. This will make the line
5237 * take up the same screen space when parts are concealed,
5238 * so that cursor line computations aren't messed up.
5239 *
5240 * To avoid the fictitious advance of 'col' causing
5241 * trailing junk to be written out of the screen line
5242 * we are building, 'boguscols' keeps track of the number
5243 * of bad columns we have advanced.
5244 */
5245 if (n_extra > 0)
5246 {
5247 vcol += n_extra;
5248# ifdef FEAT_RIGHTLEFT
5249 if (wp->w_p_rl)
5250 {
5251 col -= n_extra;
5252 boguscols -= n_extra;
5253 }
5254 else
5255# endif
5256 {
5257 col += n_extra;
5258 boguscols += n_extra;
5259 }
5260 n_extra = 0;
5261 n_attr = 0;
5262 }
5263
5264
5265# ifdef FEAT_MBYTE
5266 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5267 {
5268 /* Need to fill two screen columns. */
5269# ifdef FEAT_RIGHTLEFT
5270 if (wp->w_p_rl)
5271 {
5272 --boguscols;
5273 --col;
5274 }
5275 else
5276# endif
5277 {
5278 ++boguscols;
5279 ++col;
5280 }
5281 }
5282# endif
5283
5284# ifdef FEAT_RIGHTLEFT
5285 if (wp->w_p_rl)
5286 {
5287 --boguscols;
5288 --col;
5289 }
5290 else
5291# endif
5292 {
5293 ++boguscols;
5294 ++col;
5295 }
5296 }
5297 else
5298 {
5299 if (n_extra > 0)
5300 {
5301 vcol += n_extra;
5302 n_extra = 0;
5303 n_attr = 0;
5304 }
5305 }
5306
5307 }
5308#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 else
5310 --n_skip;
5311
Bram Moolenaar64486672010-05-16 15:46:46 +02005312 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5313 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005314 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#ifdef FEAT_DIFF
5316 && filler_todo <= 0
5317#endif
5318 )
5319 ++vcol;
5320
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005321#ifdef FEAT_SYN_HL
5322 if (vcol_save_attr >= 0)
5323 char_attr = vcol_save_attr;
5324#endif
5325
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 /* restore attributes after "predeces" in 'listchars' */
5327 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5328 char_attr = saved_attr3;
5329
5330 /* restore attributes after last 'listchars' or 'number' char */
5331 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5332 char_attr = saved_attr2;
5333
5334 /*
5335 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005336 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 */
5338 if ((
5339#ifdef FEAT_RIGHTLEFT
5340 wp->w_p_rl ? (col < 0) :
5341#endif
5342 (col >= W_WIDTH(wp)))
5343 && (*ptr != NUL
5344#ifdef FEAT_DIFF
5345 || filler_todo > 0
5346#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005347 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5349 )
5350 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005351#ifdef FEAT_CONCEAL
5352 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5353 (int)W_WIDTH(wp), wp->w_p_rl);
5354 boguscols = 0;
5355#else
5356 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5357 (int)W_WIDTH(wp), wp->w_p_rl);
5358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 ++row;
5360 ++screen_row;
5361
5362 /* When not wrapping and finished diff lines, or when displayed
5363 * '$' and highlighting until last column, break here. */
5364 if ((!wp->w_p_wrap
5365#ifdef FEAT_DIFF
5366 && filler_todo <= 0
5367#endif
5368 ) || lcs_eol_one == -1)
5369 break;
5370
5371 /* When the window is too narrow draw all "@" lines. */
5372 if (draw_state != WL_LINE
5373#ifdef FEAT_DIFF
5374 && filler_todo <= 0
5375#endif
5376 )
5377 {
5378 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5379#ifdef FEAT_VERTSPLIT
5380 draw_vsep_win(wp, row);
5381#endif
5382 row = endrow;
5383 }
5384
5385 /* When line got too long for screen break here. */
5386 if (row == endrow)
5387 {
5388 ++row;
5389 break;
5390 }
5391
5392 if (screen_cur_row == screen_row - 1
5393#ifdef FEAT_DIFF
5394 && filler_todo <= 0
5395#endif
5396 && W_WIDTH(wp) == Columns)
5397 {
5398 /* Remember that the line wraps, used for modeless copy. */
5399 LineWraps[screen_row - 1] = TRUE;
5400
5401 /*
5402 * Special trick to make copy/paste of wrapped lines work with
5403 * xterm/screen: write an extra character beyond the end of
5404 * the line. This will work with all terminal types
5405 * (regardless of the xn,am settings).
5406 * Only do this on a fast tty.
5407 * Only do this if the cursor is on the current line
5408 * (something has been written in it).
5409 * Don't do this for the GUI.
5410 * Don't do this for double-width characters.
5411 * Don't do this for a window not at the right screen border.
5412 */
5413 if (p_tf
5414#ifdef FEAT_GUI
5415 && !gui.in_use
5416#endif
5417#ifdef FEAT_MBYTE
5418 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005419 && ((*mb_off2cells)(LineOffset[screen_row],
5420 LineOffset[screen_row] + screen_Columns)
5421 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005423 + (int)Columns - 2,
5424 LineOffset[screen_row] + screen_Columns)
5425 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426#endif
5427 )
5428 {
5429 /* First make sure we are at the end of the screen line,
5430 * then output the same character again to let the
5431 * terminal know about the wrap. If the terminal doesn't
5432 * auto-wrap, we overwrite the character. */
5433 if (screen_cur_col != W_WIDTH(wp))
5434 screen_char(LineOffset[screen_row - 1]
5435 + (unsigned)Columns - 1,
5436 screen_row - 1, (int)(Columns - 1));
5437
5438#ifdef FEAT_MBYTE
5439 /* When there is a multi-byte character, just output a
5440 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005441 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5442 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 out_char(' ');
5444 else
5445#endif
5446 out_char(ScreenLines[LineOffset[screen_row - 1]
5447 + (Columns - 1)]);
5448 /* force a redraw of the first char on the next line */
5449 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5450 screen_start(); /* don't know where cursor is now */
5451 }
5452 }
5453
5454 col = 0;
5455 off = (unsigned)(current_ScreenLine - ScreenLines);
5456#ifdef FEAT_RIGHTLEFT
5457 if (wp->w_p_rl)
5458 {
5459 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5460 off += col;
5461 }
5462#endif
5463
5464 /* reset the drawing state for the start of a wrapped line */
5465 draw_state = WL_START;
5466 saved_n_extra = n_extra;
5467 saved_p_extra = p_extra;
5468 saved_c_extra = c_extra;
5469 saved_char_attr = char_attr;
5470 n_extra = 0;
5471 lcs_prec_todo = lcs_prec;
5472#ifdef FEAT_LINEBREAK
5473# ifdef FEAT_DIFF
5474 if (filler_todo <= 0)
5475# endif
5476 need_showbreak = TRUE;
5477#endif
5478#ifdef FEAT_DIFF
5479 --filler_todo;
5480 /* When the filler lines are actually below the last line of the
5481 * file, don't draw the line itself, break here. */
5482 if (filler_todo == 0 && wp->w_botfill)
5483 break;
5484#endif
5485 }
5486
5487 } /* for every character in the line */
5488
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005489#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005490 /* After an empty line check first word for capital. */
5491 if (*skipwhite(line) == NUL)
5492 {
5493 capcol_lnum = lnum + 1;
5494 cap_col = 0;
5495 }
5496#endif
5497
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 return row;
5499}
5500
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005501#ifdef FEAT_MBYTE
5502static int comp_char_differs __ARGS((int, int));
5503
5504/*
5505 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005506 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005507 */
5508 static int
5509comp_char_differs(off_from, off_to)
5510 int off_from;
5511 int off_to;
5512{
5513 int i;
5514
5515 for (i = 0; i < Screen_mco; ++i)
5516 {
5517 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5518 return TRUE;
5519 if (ScreenLinesC[i][off_from] == 0)
5520 break;
5521 }
5522 return FALSE;
5523}
5524#endif
5525
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526/*
5527 * Check whether the given character needs redrawing:
5528 * - the (first byte of the) character is different
5529 * - the attributes are different
5530 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005531 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 */
5533 static int
5534char_needs_redraw(off_from, off_to, cols)
5535 int off_from;
5536 int off_to;
5537 int cols;
5538{
5539 if (cols > 0
5540 && ((ScreenLines[off_from] != ScreenLines[off_to]
5541 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5542
5543#ifdef FEAT_MBYTE
5544 || (enc_dbcs != 0
5545 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5546 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5547 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5548 : (cols > 1 && ScreenLines[off_from + 1]
5549 != ScreenLines[off_to + 1])))
5550 || (enc_utf8
5551 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5552 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005553 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005554 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5555 && ScreenLines[off_from + 1]
5556 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557#endif
5558 ))
5559 return TRUE;
5560 return FALSE;
5561}
5562
5563/*
5564 * Move one "cooked" screen line to the screen, but only the characters that
5565 * have actually changed. Handle insert/delete character.
5566 * "coloff" gives the first column on the screen for this line.
5567 * "endcol" gives the columns where valid characters are.
5568 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5569 * needs to be cleared, negative otherwise.
5570 * "rlflag" is TRUE in a rightleft window:
5571 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5572 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5573 */
5574 static void
5575screen_line(row, coloff, endcol, clear_width
5576#ifdef FEAT_RIGHTLEFT
5577 , rlflag
5578#endif
5579 )
5580 int row;
5581 int coloff;
5582 int endcol;
5583 int clear_width;
5584#ifdef FEAT_RIGHTLEFT
5585 int rlflag;
5586#endif
5587{
5588 unsigned off_from;
5589 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005590#ifdef FEAT_MBYTE
5591 unsigned max_off_from;
5592 unsigned max_off_to;
5593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 int col = 0;
5595#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5596 int hl;
5597#endif
5598 int force = FALSE; /* force update rest of the line */
5599 int redraw_this /* bool: does character need redraw? */
5600#ifdef FEAT_GUI
5601 = TRUE /* For GUI when while-loop empty */
5602#endif
5603 ;
5604 int redraw_next; /* redraw_this for next character */
5605#ifdef FEAT_MBYTE
5606 int clear_next = FALSE;
5607 int char_cells; /* 1: normal char */
5608 /* 2: occupies two display cells */
5609# define CHAR_CELLS char_cells
5610#else
5611# define CHAR_CELLS 1
5612#endif
5613
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005614 /* Check for illegal row and col, just in case. */
5615 if (row >= Rows)
5616 row = Rows - 1;
5617 if (endcol > Columns)
5618 endcol = Columns;
5619
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620# ifdef FEAT_CLIPBOARD
5621 clip_may_clear_selection(row, row);
5622# endif
5623
5624 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5625 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005626#ifdef FEAT_MBYTE
5627 max_off_from = off_from + screen_Columns;
5628 max_off_to = LineOffset[row] + screen_Columns;
5629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
5631#ifdef FEAT_RIGHTLEFT
5632 if (rlflag)
5633 {
5634 /* Clear rest first, because it's left of the text. */
5635 if (clear_width > 0)
5636 {
5637 while (col <= endcol && ScreenLines[off_to] == ' '
5638 && ScreenAttrs[off_to] == 0
5639# ifdef FEAT_MBYTE
5640 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5641# endif
5642 )
5643 {
5644 ++off_to;
5645 ++col;
5646 }
5647 if (col <= endcol)
5648 screen_fill(row, row + 1, col + coloff,
5649 endcol + coloff + 1, ' ', ' ', 0);
5650 }
5651 col = endcol + 1;
5652 off_to = LineOffset[row] + col + coloff;
5653 off_from += col;
5654 endcol = (clear_width > 0 ? clear_width : -clear_width);
5655 }
5656#endif /* FEAT_RIGHTLEFT */
5657
5658 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5659
5660 while (col < endcol)
5661 {
5662#ifdef FEAT_MBYTE
5663 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005664 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 else
5666 char_cells = 1;
5667#endif
5668
5669 redraw_this = redraw_next;
5670 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5671 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5672
5673#ifdef FEAT_GUI
5674 /* If the next character was bold, then redraw the current character to
5675 * remove any pixels that might have spilt over into us. This only
5676 * happens in the GUI.
5677 */
5678 if (redraw_next && gui.in_use)
5679 {
5680 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005681 if (hl > HL_ALL)
5682 hl = syn_attr2attr(hl);
5683 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 redraw_this = TRUE;
5685 }
5686#endif
5687
5688 if (redraw_this)
5689 {
5690 /*
5691 * Special handling when 'xs' termcap flag set (hpterm):
5692 * Attributes for characters are stored at the position where the
5693 * cursor is when writing the highlighting code. The
5694 * start-highlighting code must be written with the cursor on the
5695 * first highlighted character. The stop-highlighting code must
5696 * be written with the cursor just after the last highlighted
5697 * character.
5698 * Overwriting a character doesn't remove it's highlighting. Need
5699 * to clear the rest of the line, and force redrawing it
5700 * completely.
5701 */
5702 if ( p_wiv
5703 && !force
5704#ifdef FEAT_GUI
5705 && !gui.in_use
5706#endif
5707 && ScreenAttrs[off_to] != 0
5708 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5709 {
5710 /*
5711 * Need to remove highlighting attributes here.
5712 */
5713 windgoto(row, col + coloff);
5714 out_str(T_CE); /* clear rest of this screen line */
5715 screen_start(); /* don't know where cursor is now */
5716 force = TRUE; /* force redraw of rest of the line */
5717 redraw_next = TRUE; /* or else next char would miss out */
5718
5719 /*
5720 * If the previous character was highlighted, need to stop
5721 * highlighting at this character.
5722 */
5723 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5724 {
5725 screen_attr = ScreenAttrs[off_to - 1];
5726 term_windgoto(row, col + coloff);
5727 screen_stop_highlight();
5728 }
5729 else
5730 screen_attr = 0; /* highlighting has stopped */
5731 }
5732#ifdef FEAT_MBYTE
5733 if (enc_dbcs != 0)
5734 {
5735 /* Check if overwriting a double-byte with a single-byte or
5736 * the other way around requires another character to be
5737 * redrawn. For UTF-8 this isn't needed, because comparing
5738 * ScreenLinesUC[] is sufficient. */
5739 if (char_cells == 1
5740 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005741 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 {
5743 /* Writing a single-cell character over a double-cell
5744 * character: need to redraw the next cell. */
5745 ScreenLines[off_to + 1] = 0;
5746 redraw_next = TRUE;
5747 }
5748 else if (char_cells == 2
5749 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005750 && (*mb_off2cells)(off_to, max_off_to) == 1
5751 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 /* Writing the second half of a double-cell character over
5754 * a double-cell character: need to redraw the second
5755 * cell. */
5756 ScreenLines[off_to + 2] = 0;
5757 redraw_next = TRUE;
5758 }
5759
5760 if (enc_dbcs == DBCS_JPNU)
5761 ScreenLines2[off_to] = ScreenLines2[off_from];
5762 }
5763 /* When writing a single-width character over a double-width
5764 * character and at the end of the redrawn text, need to clear out
5765 * the right halve of the old character.
5766 * Also required when writing the right halve of a double-width
5767 * char over the left halve of an existing one. */
5768 if (has_mbyte && col + char_cells == endcol
5769 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005770 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005772 && (*mb_off2cells)(off_to, max_off_to) == 1
5773 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 clear_next = TRUE;
5775#endif
5776
5777 ScreenLines[off_to] = ScreenLines[off_from];
5778#ifdef FEAT_MBYTE
5779 if (enc_utf8)
5780 {
5781 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5782 if (ScreenLinesUC[off_from] != 0)
5783 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005784 int i;
5785
5786 for (i = 0; i < Screen_mco; ++i)
5787 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 }
5789 }
5790 if (char_cells == 2)
5791 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5792#endif
5793
5794#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005795 /* The bold trick makes a single column of pixels appear in the
5796 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 * character should be redrawn too. This happens for our own GUI
5798 * and for some xterms. */
5799 if (
5800# ifdef FEAT_GUI
5801 gui.in_use
5802# endif
5803# if defined(FEAT_GUI) && defined(UNIX)
5804 ||
5805# endif
5806# ifdef UNIX
5807 term_is_xterm
5808# endif
5809 )
5810 {
5811 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005812 if (hl > HL_ALL)
5813 hl = syn_attr2attr(hl);
5814 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 redraw_next = TRUE;
5816 }
5817#endif
5818 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5819#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005820 /* For simplicity set the attributes of second half of a
5821 * double-wide character equal to the first half. */
5822 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005824
5825 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 else
5828#endif
5829 screen_char(off_to, row, col + coloff);
5830 }
5831 else if ( p_wiv
5832#ifdef FEAT_GUI
5833 && !gui.in_use
5834#endif
5835 && col + coloff > 0)
5836 {
5837 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5838 {
5839 /*
5840 * Don't output stop-highlight when moving the cursor, it will
5841 * stop the highlighting when it should continue.
5842 */
5843 screen_attr = 0;
5844 }
5845 else if (screen_attr != 0)
5846 screen_stop_highlight();
5847 }
5848
5849 off_to += CHAR_CELLS;
5850 off_from += CHAR_CELLS;
5851 col += CHAR_CELLS;
5852 }
5853
5854#ifdef FEAT_MBYTE
5855 if (clear_next)
5856 {
5857 /* Clear the second half of a double-wide character of which the left
5858 * half was overwritten with a single-wide character. */
5859 ScreenLines[off_to] = ' ';
5860 if (enc_utf8)
5861 ScreenLinesUC[off_to] = 0;
5862 screen_char(off_to, row, col + coloff);
5863 }
5864#endif
5865
5866 if (clear_width > 0
5867#ifdef FEAT_RIGHTLEFT
5868 && !rlflag
5869#endif
5870 )
5871 {
5872#ifdef FEAT_GUI
5873 int startCol = col;
5874#endif
5875
5876 /* blank out the rest of the line */
5877 while (col < clear_width && ScreenLines[off_to] == ' '
5878 && ScreenAttrs[off_to] == 0
5879#ifdef FEAT_MBYTE
5880 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5881#endif
5882 )
5883 {
5884 ++off_to;
5885 ++col;
5886 }
5887 if (col < clear_width)
5888 {
5889#ifdef FEAT_GUI
5890 /*
5891 * In the GUI, clearing the rest of the line may leave pixels
5892 * behind if the first character cleared was bold. Some bold
5893 * fonts spill over the left. In this case we redraw the previous
5894 * character too. If we didn't skip any blanks above, then we
5895 * only redraw if the character wasn't already redrawn anyway.
5896 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005897 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
5899 hl = ScreenAttrs[off_to];
5900 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005901 {
5902 int prev_cells = 1;
5903# ifdef FEAT_MBYTE
5904 if (enc_utf8)
5905 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5906 * that its width is 2. */
5907 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5908 else if (enc_dbcs != 0)
5909 {
5910 /* find previous character by counting from first
5911 * column and get its width. */
5912 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005913 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005914
5915 while (off < off_to)
5916 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005917 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005918 off += prev_cells;
5919 }
5920 }
5921
5922 if (enc_dbcs != 0 && prev_cells > 1)
5923 screen_char_2(off_to - prev_cells, row,
5924 col + coloff - prev_cells);
5925 else
5926# endif
5927 screen_char(off_to - prev_cells, row,
5928 col + coloff - prev_cells);
5929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
5931#endif
5932 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5933 ' ', ' ', 0);
5934#ifdef FEAT_VERTSPLIT
5935 off_to += clear_width - col;
5936 col = clear_width;
5937#endif
5938 }
5939 }
5940
5941 if (clear_width > 0)
5942 {
5943#ifdef FEAT_VERTSPLIT
5944 /* For a window that's left of another, draw the separator char. */
5945 if (col + coloff < Columns)
5946 {
5947 int c;
5948
5949 c = fillchar_vsep(&hl);
5950 if (ScreenLines[off_to] != c
5951# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005952 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5953 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954# endif
5955 || ScreenAttrs[off_to] != hl)
5956 {
5957 ScreenLines[off_to] = c;
5958 ScreenAttrs[off_to] = hl;
5959# ifdef FEAT_MBYTE
5960 if (enc_utf8)
5961 {
5962 if (c >= 0x80)
5963 {
5964 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005965 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
5967 else
5968 ScreenLinesUC[off_to] = 0;
5969 }
5970# endif
5971 screen_char(off_to, row, col + coloff);
5972 }
5973 }
5974 else
5975#endif
5976 LineWraps[row] = FALSE;
5977 }
5978}
5979
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005980#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005982 * Mirror text "str" for right-left displaying.
5983 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005985 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986rl_mirror(str)
5987 char_u *str;
5988{
5989 char_u *p1, *p2;
5990 int t;
5991
5992 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5993 {
5994 t = *p1;
5995 *p1 = *p2;
5996 *p2 = t;
5997 }
5998}
5999#endif
6000
6001#if defined(FEAT_WINDOWS) || defined(PROTO)
6002/*
6003 * mark all status lines for redraw; used after first :cd
6004 */
6005 void
6006status_redraw_all()
6007{
6008 win_T *wp;
6009
6010 for (wp = firstwin; wp; wp = wp->w_next)
6011 if (wp->w_status_height)
6012 {
6013 wp->w_redr_status = TRUE;
6014 redraw_later(VALID);
6015 }
6016}
6017
6018/*
6019 * mark all status lines of the current buffer for redraw
6020 */
6021 void
6022status_redraw_curbuf()
6023{
6024 win_T *wp;
6025
6026 for (wp = firstwin; wp; wp = wp->w_next)
6027 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6028 {
6029 wp->w_redr_status = TRUE;
6030 redraw_later(VALID);
6031 }
6032}
6033
6034/*
6035 * Redraw all status lines that need to be redrawn.
6036 */
6037 void
6038redraw_statuslines()
6039{
6040 win_T *wp;
6041
6042 for (wp = firstwin; wp; wp = wp->w_next)
6043 if (wp->w_redr_status)
6044 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006045 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006046 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047}
6048#endif
6049
6050#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6051/*
6052 * Redraw all status lines at the bottom of frame "frp".
6053 */
6054 void
6055win_redraw_last_status(frp)
6056 frame_T *frp;
6057{
6058 if (frp->fr_layout == FR_LEAF)
6059 frp->fr_win->w_redr_status = TRUE;
6060 else if (frp->fr_layout == FR_ROW)
6061 {
6062 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6063 win_redraw_last_status(frp);
6064 }
6065 else /* frp->fr_layout == FR_COL */
6066 {
6067 frp = frp->fr_child;
6068 while (frp->fr_next != NULL)
6069 frp = frp->fr_next;
6070 win_redraw_last_status(frp);
6071 }
6072}
6073#endif
6074
6075#ifdef FEAT_VERTSPLIT
6076/*
6077 * Draw the verticap separator right of window "wp" starting with line "row".
6078 */
6079 static void
6080draw_vsep_win(wp, row)
6081 win_T *wp;
6082 int row;
6083{
6084 int hl;
6085 int c;
6086
6087 if (wp->w_vsep_width)
6088 {
6089 /* draw the vertical separator right of this window */
6090 c = fillchar_vsep(&hl);
6091 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6092 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6093 c, ' ', hl);
6094 }
6095}
6096#endif
6097
6098#ifdef FEAT_WILDMENU
6099static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006100static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006103 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 */
6105 static int
6106status_match_len(xp, s)
6107 expand_T *xp;
6108 char_u *s;
6109{
6110 int len = 0;
6111
6112#ifdef FEAT_MENU
6113 int emenu = (xp->xp_context == EXPAND_MENUS
6114 || xp->xp_context == EXPAND_MENUNAMES);
6115
6116 /* Check for menu separators - replace with '|'. */
6117 if (emenu && menu_is_separator(s))
6118 return 1;
6119#endif
6120
6121 while (*s != NUL)
6122 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006123 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006124 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006125 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 }
6127
6128 return len;
6129}
6130
6131/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006132 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006133 * These are backslashes used for escaping. Do show backslashes in help tags.
6134 */
6135 static int
6136skip_status_match_char(xp, s)
6137 expand_T *xp;
6138 char_u *s;
6139{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006140 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006141#ifdef FEAT_MENU
6142 || ((xp->xp_context == EXPAND_MENUS
6143 || xp->xp_context == EXPAND_MENUNAMES)
6144 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6145#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006146 )
6147 {
6148#ifndef BACKSLASH_IN_FILENAME
6149 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6150 return 2;
6151#endif
6152 return 1;
6153 }
6154 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006155}
6156
6157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 * Show wildchar matches in the status line.
6159 * Show at least the "match" item.
6160 * We start at item 'first_match' in the list and show all matches that fit.
6161 *
6162 * If inversion is possible we use it. Else '=' characters are used.
6163 */
6164 void
6165win_redr_status_matches(xp, num_matches, matches, match, showtail)
6166 expand_T *xp;
6167 int num_matches;
6168 char_u **matches; /* list of matches */
6169 int match;
6170 int showtail;
6171{
6172#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6173 int row;
6174 char_u *buf;
6175 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006176 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 int fillchar;
6178 int attr;
6179 int i;
6180 int highlight = TRUE;
6181 char_u *selstart = NULL;
6182 int selstart_col = 0;
6183 char_u *selend = NULL;
6184 static int first_match = 0;
6185 int add_left = FALSE;
6186 char_u *s;
6187#ifdef FEAT_MENU
6188 int emenu;
6189#endif
6190#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6191 int l;
6192#endif
6193
6194 if (matches == NULL) /* interrupted completion? */
6195 return;
6196
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006197#ifdef FEAT_MBYTE
6198 if (has_mbyte)
6199 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6200 else
6201#endif
6202 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (buf == NULL)
6204 return;
6205
6206 if (match == -1) /* don't show match but original text */
6207 {
6208 match = 0;
6209 highlight = FALSE;
6210 }
6211 /* count 1 for the ending ">" */
6212 clen = status_match_len(xp, L_MATCH(match)) + 3;
6213 if (match == 0)
6214 first_match = 0;
6215 else if (match < first_match)
6216 {
6217 /* jumping left, as far as we can go */
6218 first_match = match;
6219 add_left = TRUE;
6220 }
6221 else
6222 {
6223 /* check if match fits on the screen */
6224 for (i = first_match; i < match; ++i)
6225 clen += status_match_len(xp, L_MATCH(i)) + 2;
6226 if (first_match > 0)
6227 clen += 2;
6228 /* jumping right, put match at the left */
6229 if ((long)clen > Columns)
6230 {
6231 first_match = match;
6232 /* if showing the last match, we can add some on the left */
6233 clen = 2;
6234 for (i = match; i < num_matches; ++i)
6235 {
6236 clen += status_match_len(xp, L_MATCH(i)) + 2;
6237 if ((long)clen >= Columns)
6238 break;
6239 }
6240 if (i == num_matches)
6241 add_left = TRUE;
6242 }
6243 }
6244 if (add_left)
6245 while (first_match > 0)
6246 {
6247 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6248 if ((long)clen >= Columns)
6249 break;
6250 --first_match;
6251 }
6252
6253 fillchar = fillchar_status(&attr, TRUE);
6254
6255 if (first_match == 0)
6256 {
6257 *buf = NUL;
6258 len = 0;
6259 }
6260 else
6261 {
6262 STRCPY(buf, "< ");
6263 len = 2;
6264 }
6265 clen = len;
6266
6267 i = first_match;
6268 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6269 {
6270 if (i == match)
6271 {
6272 selstart = buf + len;
6273 selstart_col = clen;
6274 }
6275
6276 s = L_MATCH(i);
6277 /* Check for menu separators - replace with '|' */
6278#ifdef FEAT_MENU
6279 emenu = (xp->xp_context == EXPAND_MENUS
6280 || xp->xp_context == EXPAND_MENUNAMES);
6281 if (emenu && menu_is_separator(s))
6282 {
6283 STRCPY(buf + len, transchar('|'));
6284 l = (int)STRLEN(buf + len);
6285 len += l;
6286 clen += l;
6287 }
6288 else
6289#endif
6290 for ( ; *s != NUL; ++s)
6291 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006292 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 clen += ptr2cells(s);
6294#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006295 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 {
6297 STRNCPY(buf + len, s, l);
6298 s += l - 1;
6299 len += l;
6300 }
6301 else
6302#endif
6303 {
6304 STRCPY(buf + len, transchar_byte(*s));
6305 len += (int)STRLEN(buf + len);
6306 }
6307 }
6308 if (i == match)
6309 selend = buf + len;
6310
6311 *(buf + len++) = ' ';
6312 *(buf + len++) = ' ';
6313 clen += 2;
6314 if (++i == num_matches)
6315 break;
6316 }
6317
6318 if (i != num_matches)
6319 {
6320 *(buf + len++) = '>';
6321 ++clen;
6322 }
6323
6324 buf[len] = NUL;
6325
6326 row = cmdline_row - 1;
6327 if (row >= 0)
6328 {
6329 if (wild_menu_showing == 0)
6330 {
6331 if (msg_scrolled > 0)
6332 {
6333 /* Put the wildmenu just above the command line. If there is
6334 * no room, scroll the screen one line up. */
6335 if (cmdline_row == Rows - 1)
6336 {
6337 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6338 ++msg_scrolled;
6339 }
6340 else
6341 {
6342 ++cmdline_row;
6343 ++row;
6344 }
6345 wild_menu_showing = WM_SCROLLED;
6346 }
6347 else
6348 {
6349 /* Create status line if needed by setting 'laststatus' to 2.
6350 * Set 'winminheight' to zero to avoid that the window is
6351 * resized. */
6352 if (lastwin->w_status_height == 0)
6353 {
6354 save_p_ls = p_ls;
6355 save_p_wmh = p_wmh;
6356 p_ls = 2;
6357 p_wmh = 0;
6358 last_status(FALSE);
6359 }
6360 wild_menu_showing = WM_SHOWN;
6361 }
6362 }
6363
6364 screen_puts(buf, row, 0, attr);
6365 if (selstart != NULL && highlight)
6366 {
6367 *selend = NUL;
6368 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6369 }
6370
6371 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6372 }
6373
6374#ifdef FEAT_VERTSPLIT
6375 win_redraw_last_status(topframe);
6376#else
6377 lastwin->w_redr_status = TRUE;
6378#endif
6379 vim_free(buf);
6380}
6381#endif
6382
6383#if defined(FEAT_WINDOWS) || defined(PROTO)
6384/*
6385 * Redraw the status line of window wp.
6386 *
6387 * If inversion is possible we use it. Else '=' characters are used.
6388 */
6389 void
6390win_redr_status(wp)
6391 win_T *wp;
6392{
6393 int row;
6394 char_u *p;
6395 int len;
6396 int fillchar;
6397 int attr;
6398 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006399 static int busy = FALSE;
6400
6401 /* It's possible to get here recursively when 'statusline' (indirectly)
6402 * invokes ":redrawstatus". Simply ignore the call then. */
6403 if (busy)
6404 return;
6405 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 wp->w_redr_status = FALSE;
6408 if (wp->w_status_height == 0)
6409 {
6410 /* no status line, can only be last window */
6411 redraw_cmdline = TRUE;
6412 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006413 else if (!redrawing()
6414#ifdef FEAT_INS_EXPAND
6415 /* don't update status line when popup menu is visible and may be
6416 * drawn over it */
6417 || pum_visible()
6418#endif
6419 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 {
6421 /* Don't redraw right now, do it later. */
6422 wp->w_redr_status = TRUE;
6423 }
6424#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006425 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
6427 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006428 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 }
6430#endif
6431 else
6432 {
6433 fillchar = fillchar_status(&attr, wp == curwin);
6434
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006435 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 p = NameBuff;
6437 len = (int)STRLEN(p);
6438
6439 if (wp->w_buffer->b_help
6440#ifdef FEAT_QUICKFIX
6441 || wp->w_p_pvw
6442#endif
6443 || bufIsChanged(wp->w_buffer)
6444 || wp->w_buffer->b_p_ro)
6445 *(p + len++) = ' ';
6446 if (wp->w_buffer->b_help)
6447 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006448 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 len += (int)STRLEN(p + len);
6450 }
6451#ifdef FEAT_QUICKFIX
6452 if (wp->w_p_pvw)
6453 {
6454 STRCPY(p + len, _("[Preview]"));
6455 len += (int)STRLEN(p + len);
6456 }
6457#endif
6458 if (bufIsChanged(wp->w_buffer))
6459 {
6460 STRCPY(p + len, "[+]");
6461 len += 3;
6462 }
6463 if (wp->w_buffer->b_p_ro)
6464 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006465 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 len += 4;
6467 }
6468
6469#ifndef FEAT_VERTSPLIT
6470 this_ru_col = ru_col;
6471 if (this_ru_col < (Columns + 1) / 2)
6472 this_ru_col = (Columns + 1) / 2;
6473#else
6474 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6475 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6476 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6477 if (this_ru_col <= 1)
6478 {
6479 p = (char_u *)"<"; /* No room for file name! */
6480 len = 1;
6481 }
6482 else
6483#endif
6484#ifdef FEAT_MBYTE
6485 if (has_mbyte)
6486 {
6487 int clen = 0, i;
6488
6489 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006490 clen = mb_string2cells(p, -1);
6491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 /* Find first character that will fit.
6493 * Going from start to end is much faster for DBCS. */
6494 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006495 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 clen -= (*mb_ptr2cells)(p + i);
6497 len = clen;
6498 if (i > 0)
6499 {
6500 p = p + i - 1;
6501 *p = '<';
6502 ++len;
6503 }
6504
6505 }
6506 else
6507#endif
6508 if (len > this_ru_col - 1)
6509 {
6510 p += len - (this_ru_col - 1);
6511 *p = '<';
6512 len = this_ru_col - 1;
6513 }
6514
6515 row = W_WINROW(wp) + wp->w_height;
6516 screen_puts(p, row, W_WINCOL(wp), attr);
6517 screen_fill(row, row + 1, len + W_WINCOL(wp),
6518 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6519
6520 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6521 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6522 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6523 - 1 + W_WINCOL(wp)), attr);
6524
6525#ifdef FEAT_CMDL_INFO
6526 win_redr_ruler(wp, TRUE);
6527#endif
6528 }
6529
6530#ifdef FEAT_VERTSPLIT
6531 /*
6532 * May need to draw the character below the vertical separator.
6533 */
6534 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6535 {
6536 if (stl_connected(wp))
6537 fillchar = fillchar_status(&attr, wp == curwin);
6538 else
6539 fillchar = fillchar_vsep(&attr);
6540 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6541 attr);
6542 }
6543#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006544 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545}
6546
Bram Moolenaar238a5642006-02-21 22:12:05 +00006547#ifdef FEAT_STL_OPT
6548/*
6549 * Redraw the status line according to 'statusline' and take care of any
6550 * errors encountered.
6551 */
6552 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006553redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006554 win_T *wp;
6555{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006556 static int entered = FALSE;
6557 int save_called_emsg = called_emsg;
6558
6559 /* When called recursively return. This can happen when the statusline
6560 * contains an expression that triggers a redraw. */
6561 if (entered)
6562 return;
6563 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006564
6565 called_emsg = FALSE;
6566 win_redr_custom(wp, FALSE);
6567 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006568 {
6569 /* When there is an error disable the statusline, otherwise the
6570 * display is messed up with errors and a redraw triggers the problem
6571 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006572 set_string_option_direct((char_u *)"statusline", -1,
6573 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006574 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006575 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006576 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006577 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006578}
6579#endif
6580
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581# ifdef FEAT_VERTSPLIT
6582/*
6583 * Return TRUE if the status line of window "wp" is connected to the status
6584 * line of the window right of it. If not, then it's a vertical separator.
6585 * Only call if (wp->w_vsep_width != 0).
6586 */
6587 int
6588stl_connected(wp)
6589 win_T *wp;
6590{
6591 frame_T *fr;
6592
6593 fr = wp->w_frame;
6594 while (fr->fr_parent != NULL)
6595 {
6596 if (fr->fr_parent->fr_layout == FR_COL)
6597 {
6598 if (fr->fr_next != NULL)
6599 break;
6600 }
6601 else
6602 {
6603 if (fr->fr_next != NULL)
6604 return TRUE;
6605 }
6606 fr = fr->fr_parent;
6607 }
6608 return FALSE;
6609}
6610# endif
6611
6612#endif /* FEAT_WINDOWS */
6613
6614#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6615/*
6616 * Get the value to show for the language mappings, active 'keymap'.
6617 */
6618 int
6619get_keymap_str(wp, buf, len)
6620 win_T *wp;
6621 char_u *buf; /* buffer for the result */
6622 int len; /* length of buffer */
6623{
6624 char_u *p;
6625
6626 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6627 return FALSE;
6628
6629 {
6630#ifdef FEAT_EVAL
6631 buf_T *old_curbuf = curbuf;
6632 win_T *old_curwin = curwin;
6633 char_u *s;
6634
6635 curbuf = wp->w_buffer;
6636 curwin = wp;
6637 STRCPY(buf, "b:keymap_name"); /* must be writable */
6638 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006639 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 --emsg_skip;
6641 curbuf = old_curbuf;
6642 curwin = old_curwin;
6643 if (p == NULL || *p == NUL)
6644#endif
6645 {
6646#ifdef FEAT_KEYMAP
6647 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6648 p = wp->w_buffer->b_p_keymap;
6649 else
6650#endif
6651 p = (char_u *)"lang";
6652 }
6653 if ((int)(STRLEN(p) + 3) < len)
6654 sprintf((char *)buf, "<%s>", p);
6655 else
6656 buf[0] = NUL;
6657#ifdef FEAT_EVAL
6658 vim_free(s);
6659#endif
6660 }
6661 return buf[0] != NUL;
6662}
6663#endif
6664
6665#if defined(FEAT_STL_OPT) || defined(PROTO)
6666/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006667 * Redraw the status line or ruler of window "wp".
6668 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 */
6670 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006671win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006673 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006675 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 int attr;
6677 int curattr;
6678 int row;
6679 int col = 0;
6680 int maxwidth;
6681 int width;
6682 int n;
6683 int len;
6684 int fillchar;
6685 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006686 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006688 struct stl_hlrec hltab[STL_MAX_ITEM];
6689 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006690 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006691 win_T *ewp;
6692 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
Bram Moolenaar1d633412013-12-11 15:52:01 +01006694 /* There is a tiny chance that this gets called recursively: When
6695 * redrawing a status line triggers redrawing the ruler or tabline.
6696 * Avoid trouble by not allowing recursion. */
6697 if (entered)
6698 return;
6699 entered = TRUE;
6700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006702 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006704 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006705 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006706 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006707 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006708 attr = hl_attr(HLF_TPF);
6709 maxwidth = Columns;
6710# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006711 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006714 else
6715 {
6716 row = W_WINROW(wp) + wp->w_height;
6717 fillchar = fillchar_status(&attr, wp == curwin);
6718 maxwidth = W_WIDTH(wp);
6719
6720 if (draw_ruler)
6721 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006722 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006723 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006724 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006725 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006726 if (*++stl == '-')
6727 stl++;
6728 if (atoi((char *)stl))
6729 while (VIM_ISDIGIT(*stl))
6730 stl++;
6731 if (*stl++ != '(')
6732 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006733 }
6734#ifdef FEAT_VERTSPLIT
6735 col = ru_col - (Columns - W_WIDTH(wp));
6736 if (col < (W_WIDTH(wp) + 1) / 2)
6737 col = (W_WIDTH(wp) + 1) / 2;
6738#else
6739 col = ru_col;
6740 if (col > (Columns + 1) / 2)
6741 col = (Columns + 1) / 2;
6742#endif
6743 maxwidth = W_WIDTH(wp) - col;
6744#ifdef FEAT_WINDOWS
6745 if (!wp->w_status_height)
6746#endif
6747 {
6748 row = Rows - 1;
6749 --maxwidth; /* writing in last column may cause scrolling */
6750 fillchar = ' ';
6751 attr = 0;
6752 }
6753
6754# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006755 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006756# endif
6757 }
6758 else
6759 {
6760 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006761 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006762 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006763 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006764# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006765 use_sandbox = was_set_insecurely((char_u *)"statusline",
6766 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006767# endif
6768 }
6769
6770#ifdef FEAT_VERTSPLIT
6771 col += W_WINCOL(wp);
6772#endif
6773 }
6774
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006776 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
Bram Moolenaar61452852011-02-01 18:01:11 +01006778 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6779 * the cursor away and back. */
6780 ewp = wp == NULL ? curwin : wp;
6781 p_crb_save = ewp->w_p_crb;
6782 ewp->w_p_crb = FALSE;
6783
Bram Moolenaar362f3562009-11-03 16:20:34 +00006784 /* Make a copy, because the statusline may include a function call that
6785 * might change the option value and free the memory. */
6786 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006787 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006788 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006789 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006790 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006791 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006793 /* Make all characters printable. */
6794 p = transstr(buf);
6795 if (p != NULL)
6796 {
6797 vim_strncpy(buf, p, sizeof(buf) - 1);
6798 vim_free(p);
6799 }
6800
6801 /* fill up with "fillchar" */
6802 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006803 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
6805#ifdef FEAT_MBYTE
6806 len += (*mb_char2bytes)(fillchar, buf + len);
6807#else
6808 buf[len++] = fillchar;
6809#endif
6810 ++width;
6811 }
6812 buf[len] = NUL;
6813
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006814 /*
6815 * Draw each snippet with the specified highlighting.
6816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 curattr = attr;
6818 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006819 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006821 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 screen_puts_len(p, len, row, col, curattr);
6823 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006824 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006826 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006828 else if (hltab[n].userhl < 0)
6829 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006831 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006832 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833#endif
6834 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006835 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006838
6839 if (wp == NULL)
6840 {
6841 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6842 col = 0;
6843 len = 0;
6844 p = buf;
6845 fillchar = 0;
6846 for (n = 0; tabtab[n].start != NULL; n++)
6847 {
6848 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6849 while (col < len)
6850 TabPageIdxs[col++] = fillchar;
6851 p = tabtab[n].start;
6852 fillchar = tabtab[n].userhl;
6853 }
6854 while (col < Columns)
6855 TabPageIdxs[col++] = fillchar;
6856 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006857
6858theend:
6859 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860}
6861
6862#endif /* FEAT_STL_OPT */
6863
6864/*
6865 * Output a single character directly to the screen and update ScreenLines.
6866 */
6867 void
6868screen_putchar(c, row, col, attr)
6869 int c;
6870 int row, col;
6871 int attr;
6872{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 char_u buf[MB_MAXBYTES + 1];
6874
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006875#ifdef FEAT_MBYTE
6876 if (has_mbyte)
6877 buf[(*mb_char2bytes)(c, buf)] = NUL;
6878 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006880 {
6881 buf[0] = c;
6882 buf[1] = NUL;
6883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 screen_puts(buf, row, col, attr);
6885}
6886
6887/*
6888 * Get a single character directly from ScreenLines into "bytes[]".
6889 * Also return its attribute in *attrp;
6890 */
6891 void
6892screen_getbytes(row, col, bytes, attrp)
6893 int row, col;
6894 char_u *bytes;
6895 int *attrp;
6896{
6897 unsigned off;
6898
6899 /* safety check */
6900 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6901 {
6902 off = LineOffset[row] + col;
6903 *attrp = ScreenAttrs[off];
6904 bytes[0] = ScreenLines[off];
6905 bytes[1] = NUL;
6906
6907#ifdef FEAT_MBYTE
6908 if (enc_utf8 && ScreenLinesUC[off] != 0)
6909 bytes[utfc_char2bytes(off, bytes)] = NUL;
6910 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6911 {
6912 bytes[0] = ScreenLines[off];
6913 bytes[1] = ScreenLines2[off];
6914 bytes[2] = NUL;
6915 }
6916 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6917 {
6918 bytes[1] = ScreenLines[off + 1];
6919 bytes[2] = NUL;
6920 }
6921#endif
6922 }
6923}
6924
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006925#ifdef FEAT_MBYTE
6926static int screen_comp_differs __ARGS((int, int*));
6927
6928/*
6929 * Return TRUE if composing characters for screen posn "off" differs from
6930 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006931 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006932 */
6933 static int
6934screen_comp_differs(off, u8cc)
6935 int off;
6936 int *u8cc;
6937{
6938 int i;
6939
6940 for (i = 0; i < Screen_mco; ++i)
6941 {
6942 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6943 return TRUE;
6944 if (u8cc[i] == 0)
6945 break;
6946 }
6947 return FALSE;
6948}
6949#endif
6950
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951/*
6952 * Put string '*text' on the screen at position 'row' and 'col', with
6953 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6954 * Note: only outputs within one row, message is truncated at screen boundary!
6955 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6956 */
6957 void
6958screen_puts(text, row, col, attr)
6959 char_u *text;
6960 int row;
6961 int col;
6962 int attr;
6963{
6964 screen_puts_len(text, -1, row, col, attr);
6965}
6966
6967/*
6968 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6969 * a NUL.
6970 */
6971 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006972screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006974 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 int row;
6976 int col;
6977 int attr;
6978{
6979 unsigned off;
6980 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006981 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 int c;
6983#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006984 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 int mbyte_blen = 1;
6986 int mbyte_cells = 1;
6987 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006988 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 int clear_next_cell = FALSE;
6990# ifdef FEAT_ARABIC
6991 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006992 int pc, nc, nc1;
6993 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994# endif
6995#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006996#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6997 int force_redraw_this;
6998 int force_redraw_next = FALSE;
6999#endif
7000 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
7002 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7003 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007004 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
Bram Moolenaarc236c162008-07-13 17:41:49 +00007006#ifdef FEAT_MBYTE
7007 /* When drawing over the right halve of a double-wide char clear out the
7008 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007009 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007010# ifdef FEAT_GUI
7011 && !gui.in_use
7012# endif
7013 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007014 {
7015 ScreenLines[off - 1] = ' ';
7016 ScreenAttrs[off - 1] = 0;
7017 if (enc_utf8)
7018 {
7019 ScreenLinesUC[off - 1] = 0;
7020 ScreenLinesC[0][off - 1] = 0;
7021 }
7022 /* redraw the previous cell, make it empty */
7023 screen_char(off - 1, row, col - 1);
7024 /* force the cell at "col" to be redrawn */
7025 force_redraw_next = TRUE;
7026 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007027#endif
7028
Bram Moolenaar367329b2007-08-30 11:53:22 +00007029#ifdef FEAT_MBYTE
7030 max_off = LineOffset[row] + screen_Columns;
7031#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007032 while (col < screen_Columns
7033 && (len < 0 || (int)(ptr - text) < len)
7034 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
7036 c = *ptr;
7037#ifdef FEAT_MBYTE
7038 /* check if this is the first byte of a multibyte */
7039 if (has_mbyte)
7040 {
7041 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007042 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007044 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7046 mbyte_cells = 1;
7047 else if (enc_dbcs != 0)
7048 mbyte_cells = mbyte_blen;
7049 else /* enc_utf8 */
7050 {
7051 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007052 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 (int)((text + len) - ptr));
7054 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007055 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007057# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 /* Non-BMP character: display as ? or fullwidth ?. */
7059 if (u8c >= 0x10000)
7060 {
7061 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7062 if (attr == 0)
7063 attr = hl_attr(HLF_8);
7064 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066# ifdef FEAT_ARABIC
7067 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7068 {
7069 /* Do Arabic shaping. */
7070 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7071 {
7072 /* Past end of string to be displayed. */
7073 nc = NUL;
7074 nc1 = NUL;
7075 }
7076 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007077 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007078 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7079 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007080 nc1 = pcc[0];
7081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 pc = prev_c;
7083 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007084 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
7086 else
7087 prev_c = u8c;
7088# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007089 if (col + mbyte_cells > screen_Columns)
7090 {
7091 /* Only 1 cell left, but character requires 2 cells:
7092 * display a '>' in the last column to avoid wrapping. */
7093 c = '>';
7094 mbyte_cells = 1;
7095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
7097 }
7098#endif
7099
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007100#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7101 force_redraw_this = force_redraw_next;
7102 force_redraw_next = FALSE;
7103#endif
7104
7105 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106#ifdef FEAT_MBYTE
7107 || (mbyte_cells == 2
7108 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7109 || (enc_dbcs == DBCS_JPNU
7110 && c == 0x8e
7111 && ScreenLines2[off] != ptr[1])
7112 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007113 && (ScreenLinesUC[off] !=
7114 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7115 || (ScreenLinesUC[off] != 0
7116 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117#endif
7118 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007119 || exmode_active;
7120
7121 if (need_redraw
7122#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7123 || force_redraw_this
7124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 )
7126 {
7127#if defined(FEAT_GUI) || defined(UNIX)
7128 /* The bold trick makes a single row of pixels appear in the next
7129 * character. When a bold character is removed, the next
7130 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007131 * and for some xterms. */
7132 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133# ifdef FEAT_GUI
7134 gui.in_use
7135# endif
7136# if defined(FEAT_GUI) && defined(UNIX)
7137 ||
7138# endif
7139# ifdef UNIX
7140 term_is_xterm
7141# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007142 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007144 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007146 if (n > HL_ALL)
7147 n = syn_attr2attr(n);
7148 if (n & HL_BOLD)
7149 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 }
7151#endif
7152#ifdef FEAT_MBYTE
7153 /* When at the end of the text and overwriting a two-cell
7154 * character with a one-cell character, need to clear the next
7155 * cell. Also when overwriting the left halve of a two-cell char
7156 * with the right halve of a two-cell char. Do this only once
7157 * (mb_off2cells() may return 2 on the right halve). */
7158 if (clear_next_cell)
7159 clear_next_cell = FALSE;
7160 else if (has_mbyte
7161 && (len < 0 ? ptr[mbyte_blen] == NUL
7162 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007163 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007165 && (*mb_off2cells)(off, max_off) == 1
7166 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 clear_next_cell = TRUE;
7168
7169 /* Make sure we never leave a second byte of a double-byte behind,
7170 * it confuses mb_off2cells(). */
7171 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007172 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007174 && (*mb_off2cells)(off, max_off) == 1
7175 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 ScreenLines[off + mbyte_blen] = 0;
7177#endif
7178 ScreenLines[off] = c;
7179 ScreenAttrs[off] = attr;
7180#ifdef FEAT_MBYTE
7181 if (enc_utf8)
7182 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007183 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 ScreenLinesUC[off] = 0;
7185 else
7186 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007187 int i;
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007190 for (i = 0; i < Screen_mco; ++i)
7191 {
7192 ScreenLinesC[i][off] = u8cc[i];
7193 if (u8cc[i] == 0)
7194 break;
7195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
7197 if (mbyte_cells == 2)
7198 {
7199 ScreenLines[off + 1] = 0;
7200 ScreenAttrs[off + 1] = attr;
7201 }
7202 screen_char(off, row, col);
7203 }
7204 else if (mbyte_cells == 2)
7205 {
7206 ScreenLines[off + 1] = ptr[1];
7207 ScreenAttrs[off + 1] = attr;
7208 screen_char_2(off, row, col);
7209 }
7210 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7211 {
7212 ScreenLines2[off] = ptr[1];
7213 screen_char(off, row, col);
7214 }
7215 else
7216#endif
7217 screen_char(off, row, col);
7218 }
7219#ifdef FEAT_MBYTE
7220 if (has_mbyte)
7221 {
7222 off += mbyte_cells;
7223 col += mbyte_cells;
7224 ptr += mbyte_blen;
7225 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007226 {
7227 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007229 len = -1;
7230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 }
7232 else
7233#endif
7234 {
7235 ++off;
7236 ++col;
7237 ++ptr;
7238 }
7239 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007240
7241#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7242 /* If we detected the next character needs to be redrawn, but the text
7243 * doesn't extend up to there, update the character here. */
7244 if (force_redraw_next && col < screen_Columns)
7245 {
7246# ifdef FEAT_MBYTE
7247 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7248 screen_char_2(off, row, col);
7249 else
7250# endif
7251 screen_char(off, row, col);
7252 }
7253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254}
7255
7256#ifdef FEAT_SEARCH_EXTRA
7257/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007258 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 */
7260 static void
7261start_search_hl()
7262{
7263 if (p_hls && !no_hlsearch)
7264 {
7265 last_pat_prog(&search_hl.rm);
7266 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007267# ifdef FEAT_RELTIME
7268 /* Set the time limit to 'redrawtime'. */
7269 profile_setlimit(p_rdt, &search_hl.tm);
7270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 }
7272}
7273
7274/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007275 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 */
7277 static void
7278end_search_hl()
7279{
7280 if (search_hl.rm.regprog != NULL)
7281 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007282 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 search_hl.rm.regprog = NULL;
7284 }
7285}
7286
7287/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007288 * Init for calling prepare_search_hl().
7289 */
7290 static void
7291init_search_hl(wp)
7292 win_T *wp;
7293{
7294 matchitem_T *cur;
7295
7296 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7297 * match */
7298 cur = wp->w_match_head;
7299 while (cur != NULL)
7300 {
7301 cur->hl.rm = cur->match;
7302 if (cur->hlg_id == 0)
7303 cur->hl.attr = 0;
7304 else
7305 cur->hl.attr = syn_id2attr(cur->hlg_id);
7306 cur->hl.buf = wp->w_buffer;
7307 cur->hl.lnum = 0;
7308 cur->hl.first_lnum = 0;
7309# ifdef FEAT_RELTIME
7310 /* Set the time limit to 'redrawtime'. */
7311 profile_setlimit(p_rdt, &(cur->hl.tm));
7312# endif
7313 cur = cur->next;
7314 }
7315 search_hl.buf = wp->w_buffer;
7316 search_hl.lnum = 0;
7317 search_hl.first_lnum = 0;
7318 /* time limit is set at the toplevel, for all windows */
7319}
7320
7321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 * Advance to the match in window "wp" line "lnum" or past it.
7323 */
7324 static void
7325prepare_search_hl(wp, lnum)
7326 win_T *wp;
7327 linenr_T lnum;
7328{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007329 matchitem_T *cur; /* points to the match list */
7330 match_T *shl; /* points to search_hl or a match */
7331 int shl_flag; /* flag to indicate whether search_hl
7332 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007333 int pos_inprogress; /* marks that position match search is
7334 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 int n;
7336
7337 /*
7338 * When using a multi-line pattern, start searching at the top
7339 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007340 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007342 cur = wp->w_match_head;
7343 shl_flag = FALSE;
7344 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007346 if (shl_flag == FALSE)
7347 {
7348 shl = &search_hl;
7349 shl_flag = TRUE;
7350 }
7351 else
7352 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 if (shl->rm.regprog != NULL
7354 && shl->lnum == 0
7355 && re_multiline(shl->rm.regprog))
7356 {
7357 if (shl->first_lnum == 0)
7358 {
7359# ifdef FEAT_FOLDING
7360 for (shl->first_lnum = lnum;
7361 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7362 if (hasFoldingWin(wp, shl->first_lnum - 1,
7363 NULL, NULL, TRUE, NULL))
7364 break;
7365# else
7366 shl->first_lnum = wp->w_topline;
7367# endif
7368 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007369 if (cur != NULL)
7370 cur->pos.cur = 0;
7371 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007373 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7374 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007376 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7377 pos_inprogress = cur == NULL || cur->pos.cur == 0
7378 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 if (shl->lnum != 0)
7380 {
7381 shl->first_lnum = shl->lnum
7382 + shl->rm.endpos[0].lnum
7383 - shl->rm.startpos[0].lnum;
7384 n = shl->rm.endpos[0].col;
7385 }
7386 else
7387 {
7388 ++shl->first_lnum;
7389 n = 0;
7390 }
7391 }
7392 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007393 if (shl != &search_hl && cur != NULL)
7394 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 }
7396}
7397
7398/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007399 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 * Uses shl->buf.
7401 * Sets shl->lnum and shl->rm contents.
7402 * Note: Assumes a previous match is always before "lnum", unless
7403 * shl->lnum is zero.
7404 * Careful: Any pointers for buffer lines will become invalid.
7405 */
7406 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007407next_search_hl(win, shl, lnum, mincol, cur)
7408 win_T *win;
7409 match_T *shl; /* points to search_hl or a match */
7410 linenr_T lnum;
7411 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007412 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414 linenr_T l;
7415 colnr_T matchcol;
7416 long nmatched;
7417
7418 if (shl->lnum != 0)
7419 {
7420 /* Check for three situations:
7421 * 1. If the "lnum" is below a previous match, start a new search.
7422 * 2. If the previous match includes "mincol", use it.
7423 * 3. Continue after the previous match.
7424 */
7425 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7426 if (lnum > l)
7427 shl->lnum = 0;
7428 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7429 return;
7430 }
7431
7432 /*
7433 * Repeat searching for a match until one is found that includes "mincol"
7434 * or none is found in this line.
7435 */
7436 called_emsg = FALSE;
7437 for (;;)
7438 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007439#ifdef FEAT_RELTIME
7440 /* Stop searching after passing the time limit. */
7441 if (profile_passed_limit(&(shl->tm)))
7442 {
7443 shl->lnum = 0; /* no match found in time */
7444 break;
7445 }
7446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 /* Three situations:
7448 * 1. No useful previous match: search from start of line.
7449 * 2. Not Vi compatible or empty match: continue at next character.
7450 * Break the loop if this is beyond the end of the line.
7451 * 3. Vi compatible searching: continue at end of previous match.
7452 */
7453 if (shl->lnum == 0)
7454 matchcol = 0;
7455 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7456 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007457 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007459 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007460
7461 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007462 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007463 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007465 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 shl->lnum = 0;
7467 break;
7468 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007469#ifdef FEAT_MBYTE
7470 if (has_mbyte)
7471 matchcol += mb_ptr2len(ml);
7472 else
7473#endif
7474 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476 else
7477 matchcol = shl->rm.endpos[0].col;
7478
7479 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007480 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007482 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7483 matchcol,
7484#ifdef FEAT_RELTIME
7485 &(shl->tm)
7486#else
7487 NULL
7488#endif
7489 );
7490 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007491 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007492 /* Error while handling regexp: stop using this regexp. */
7493 if (shl == &search_hl)
7494 {
7495 /* don't free regprog in the match list, it's a copy */
7496 vim_regfree(shl->rm.regprog);
7497 SET_NO_HLSEARCH(TRUE);
7498 }
7499 shl->rm.regprog = NULL;
7500 shl->lnum = 0;
7501 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7502 message */
7503 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007504 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007505 }
7506 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007507 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007508 else
7509 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 if (nmatched == 0)
7511 {
7512 shl->lnum = 0; /* no match found */
7513 break;
7514 }
7515 if (shl->rm.startpos[0].lnum > 0
7516 || shl->rm.startpos[0].col >= mincol
7517 || nmatched > 1
7518 || shl->rm.endpos[0].col > mincol)
7519 {
7520 shl->lnum += shl->rm.startpos[0].lnum;
7521 break; /* useful match found */
7522 }
7523 }
7524}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
Bram Moolenaarb3414592014-06-17 17:48:32 +02007526 static int
7527next_search_hl_pos(shl, lnum, posmatch, mincol)
7528 match_T *shl; /* points to a match */
7529 linenr_T lnum;
7530 posmatch_T *posmatch; /* match positions */
7531 colnr_T mincol; /* minimal column for a match */
7532{
7533 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007534 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007535
7536 shl->lnum = 0;
7537 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7538 {
7539 if (posmatch->pos[i].lnum == 0)
7540 break;
7541 if (posmatch->pos[i].col < mincol)
7542 continue;
7543 if (posmatch->pos[i].lnum == lnum)
7544 {
7545 if (shl->lnum == lnum)
7546 {
7547 /* partially sort positions by column numbers
7548 * on the same line */
7549 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7550 {
7551 llpos_T tmp = posmatch->pos[i];
7552
7553 posmatch->pos[i] = posmatch->pos[bot];
7554 posmatch->pos[bot] = tmp;
7555 }
7556 }
7557 else
7558 {
7559 bot = i;
7560 shl->lnum = lnum;
7561 }
7562 }
7563 }
7564 posmatch->cur = 0;
7565 if (shl->lnum == lnum)
7566 {
7567 colnr_T start = posmatch->pos[bot].col == 0
7568 ? 0 : posmatch->pos[bot].col - 1;
7569 colnr_T end = posmatch->pos[bot].col == 0
7570 ? MAXCOL : start + posmatch->pos[bot].len;
7571
7572 shl->rm.startpos[0].lnum = 0;
7573 shl->rm.startpos[0].col = start;
7574 shl->rm.endpos[0].lnum = 0;
7575 shl->rm.endpos[0].col = end;
7576 posmatch->cur = bot + 1;
7577 return TRUE;
7578 }
7579 return FALSE;
7580}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007581#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007582
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 static void
7584screen_start_highlight(attr)
7585 int attr;
7586{
7587 attrentry_T *aep = NULL;
7588
7589 screen_attr = attr;
7590 if (full_screen
7591#ifdef WIN3264
7592 && termcap_active
7593#endif
7594 )
7595 {
7596#ifdef FEAT_GUI
7597 if (gui.in_use)
7598 {
7599 char buf[20];
7600
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007601 /* The GUI handles this internally. */
7602 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 OUT_STR(buf);
7604 }
7605 else
7606#endif
7607 {
7608 if (attr > HL_ALL) /* special HL attr. */
7609 {
7610 if (t_colors > 1)
7611 aep = syn_cterm_attr2entry(attr);
7612 else
7613 aep = syn_term_attr2entry(attr);
7614 if (aep == NULL) /* did ":syntax clear" */
7615 attr = 0;
7616 else
7617 attr = aep->ae_attr;
7618 }
7619 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7620 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007621 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7622 && cterm_normal_fg_bold)
7623 /* If the Normal FG color has BOLD attribute and the new HL
7624 * has a FG color defined, clear BOLD. */
7625 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7627 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007628 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7629 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 out_str(T_US);
7631 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7632 out_str(T_CZH);
7633 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7634 out_str(T_MR);
7635
7636 /*
7637 * Output the color or start string after bold etc., in case the
7638 * bold etc. override the color setting.
7639 */
7640 if (aep != NULL)
7641 {
7642 if (t_colors > 1)
7643 {
7644 if (aep->ae_u.cterm.fg_color)
7645 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7646 if (aep->ae_u.cterm.bg_color)
7647 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7648 }
7649 else
7650 {
7651 if (aep->ae_u.term.start != NULL)
7652 out_str(aep->ae_u.term.start);
7653 }
7654 }
7655 }
7656 }
7657}
7658
7659 void
7660screen_stop_highlight()
7661{
7662 int do_ME = FALSE; /* output T_ME code */
7663
7664 if (screen_attr != 0
7665#ifdef WIN3264
7666 && termcap_active
7667#endif
7668 )
7669 {
7670#ifdef FEAT_GUI
7671 if (gui.in_use)
7672 {
7673 char buf[20];
7674
7675 /* use internal GUI code */
7676 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7677 OUT_STR(buf);
7678 }
7679 else
7680#endif
7681 {
7682 if (screen_attr > HL_ALL) /* special HL attr. */
7683 {
7684 attrentry_T *aep;
7685
7686 if (t_colors > 1)
7687 {
7688 /*
7689 * Assume that t_me restores the original colors!
7690 */
7691 aep = syn_cterm_attr2entry(screen_attr);
7692 if (aep != NULL && (aep->ae_u.cterm.fg_color
7693 || aep->ae_u.cterm.bg_color))
7694 do_ME = TRUE;
7695 }
7696 else
7697 {
7698 aep = syn_term_attr2entry(screen_attr);
7699 if (aep != NULL && aep->ae_u.term.stop != NULL)
7700 {
7701 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7702 do_ME = TRUE;
7703 else
7704 out_str(aep->ae_u.term.stop);
7705 }
7706 }
7707 if (aep == NULL) /* did ":syntax clear" */
7708 screen_attr = 0;
7709 else
7710 screen_attr = aep->ae_attr;
7711 }
7712
7713 /*
7714 * Often all ending-codes are equal to T_ME. Avoid outputting the
7715 * same sequence several times.
7716 */
7717 if (screen_attr & HL_STANDOUT)
7718 {
7719 if (STRCMP(T_SE, T_ME) == 0)
7720 do_ME = TRUE;
7721 else
7722 out_str(T_SE);
7723 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007724 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 {
7726 if (STRCMP(T_UE, T_ME) == 0)
7727 do_ME = TRUE;
7728 else
7729 out_str(T_UE);
7730 }
7731 if (screen_attr & HL_ITALIC)
7732 {
7733 if (STRCMP(T_CZR, T_ME) == 0)
7734 do_ME = TRUE;
7735 else
7736 out_str(T_CZR);
7737 }
7738 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7739 out_str(T_ME);
7740
7741 if (t_colors > 1)
7742 {
7743 /* set Normal cterm colors */
7744 if (cterm_normal_fg_color != 0)
7745 term_fg_color(cterm_normal_fg_color - 1);
7746 if (cterm_normal_bg_color != 0)
7747 term_bg_color(cterm_normal_bg_color - 1);
7748 if (cterm_normal_fg_bold)
7749 out_str(T_MD);
7750 }
7751 }
7752 }
7753 screen_attr = 0;
7754}
7755
7756/*
7757 * Reset the colors for a cterm. Used when leaving Vim.
7758 * The machine specific code may override this again.
7759 */
7760 void
7761reset_cterm_colors()
7762{
7763 if (t_colors > 1)
7764 {
7765 /* set Normal cterm colors */
7766 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7767 {
7768 out_str(T_OP);
7769 screen_attr = -1;
7770 }
7771 if (cterm_normal_fg_bold)
7772 {
7773 out_str(T_ME);
7774 screen_attr = -1;
7775 }
7776 }
7777}
7778
7779/*
7780 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7781 * using the attributes from ScreenAttrs["off"].
7782 */
7783 static void
7784screen_char(off, row, col)
7785 unsigned off;
7786 int row;
7787 int col;
7788{
7789 int attr;
7790
7791 /* Check for illegal values, just in case (could happen just after
7792 * resizing). */
7793 if (row >= screen_Rows || col >= screen_Columns)
7794 return;
7795
7796 /* Outputting the last character on the screen may scrollup the screen.
7797 * Don't to it! Mark the character invalid (update it when scrolled up) */
7798 if (row == screen_Rows - 1 && col == screen_Columns - 1
7799#ifdef FEAT_RIGHTLEFT
7800 /* account for first command-line character in rightleft mode */
7801 && !cmdmsg_rl
7802#endif
7803 )
7804 {
7805 ScreenAttrs[off] = (sattr_T)-1;
7806 return;
7807 }
7808
7809 /*
7810 * Stop highlighting first, so it's easier to move the cursor.
7811 */
7812#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7813 if (screen_char_attr != 0)
7814 attr = screen_char_attr;
7815 else
7816#endif
7817 attr = ScreenAttrs[off];
7818 if (screen_attr != attr)
7819 screen_stop_highlight();
7820
7821 windgoto(row, col);
7822
7823 if (screen_attr != attr)
7824 screen_start_highlight(attr);
7825
7826#ifdef FEAT_MBYTE
7827 if (enc_utf8 && ScreenLinesUC[off] != 0)
7828 {
7829 char_u buf[MB_MAXBYTES + 1];
7830
7831 /* Convert UTF-8 character to bytes and write it. */
7832
7833 buf[utfc_char2bytes(off, buf)] = NUL;
7834
7835 out_str(buf);
7836 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7837 ++screen_cur_col;
7838 }
7839 else
7840#endif
7841 {
7842#ifdef FEAT_MBYTE
7843 out_flush_check();
7844#endif
7845 out_char(ScreenLines[off]);
7846#ifdef FEAT_MBYTE
7847 /* double-byte character in single-width cell */
7848 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7849 out_char(ScreenLines2[off]);
7850#endif
7851 }
7852
7853 screen_cur_col++;
7854}
7855
7856#ifdef FEAT_MBYTE
7857
7858/*
7859 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7860 * on the screen at position 'row' and 'col'.
7861 * The attributes of the first byte is used for all. This is required to
7862 * output the two bytes of a double-byte character with nothing in between.
7863 */
7864 static void
7865screen_char_2(off, row, col)
7866 unsigned off;
7867 int row;
7868 int col;
7869{
7870 /* Check for illegal values (could be wrong when screen was resized). */
7871 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7872 return;
7873
7874 /* Outputting the last character on the screen may scrollup the screen.
7875 * Don't to it! Mark the character invalid (update it when scrolled up) */
7876 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7877 {
7878 ScreenAttrs[off] = (sattr_T)-1;
7879 return;
7880 }
7881
7882 /* Output the first byte normally (positions the cursor), then write the
7883 * second byte directly. */
7884 screen_char(off, row, col);
7885 out_char(ScreenLines[off + 1]);
7886 ++screen_cur_col;
7887}
7888#endif
7889
7890#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7891/*
7892 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7893 * This uses the contents of ScreenLines[] and doesn't change it.
7894 */
7895 void
7896screen_draw_rectangle(row, col, height, width, invert)
7897 int row;
7898 int col;
7899 int height;
7900 int width;
7901 int invert;
7902{
7903 int r, c;
7904 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007905#ifdef FEAT_MBYTE
7906 int max_off;
7907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007909 /* Can't use ScreenLines unless initialized */
7910 if (ScreenLines == NULL)
7911 return;
7912
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 if (invert)
7914 screen_char_attr = HL_INVERSE;
7915 for (r = row; r < row + height; ++r)
7916 {
7917 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007918#ifdef FEAT_MBYTE
7919 max_off = off + screen_Columns;
7920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 for (c = col; c < col + width; ++c)
7922 {
7923#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007924 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
7926 screen_char_2(off + c, r, c);
7927 ++c;
7928 }
7929 else
7930#endif
7931 {
7932 screen_char(off + c, r, c);
7933#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007934 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 ++c;
7936#endif
7937 }
7938 }
7939 }
7940 screen_char_attr = 0;
7941}
7942#endif
7943
7944#ifdef FEAT_VERTSPLIT
7945/*
7946 * Redraw the characters for a vertically split window.
7947 */
7948 static void
7949redraw_block(row, end, wp)
7950 int row;
7951 int end;
7952 win_T *wp;
7953{
7954 int col;
7955 int width;
7956
7957# ifdef FEAT_CLIPBOARD
7958 clip_may_clear_selection(row, end - 1);
7959# endif
7960
7961 if (wp == NULL)
7962 {
7963 col = 0;
7964 width = Columns;
7965 }
7966 else
7967 {
7968 col = wp->w_wincol;
7969 width = wp->w_width;
7970 }
7971 screen_draw_rectangle(row, col, end - row, width, FALSE);
7972}
7973#endif
7974
7975/*
7976 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7977 * with character 'c1' in first column followed by 'c2' in the other columns.
7978 * Use attributes 'attr'.
7979 */
7980 void
7981screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7982 int start_row, end_row;
7983 int start_col, end_col;
7984 int c1, c2;
7985 int attr;
7986{
7987 int row;
7988 int col;
7989 int off;
7990 int end_off;
7991 int did_delete;
7992 int c;
7993 int norm_term;
7994#if defined(FEAT_GUI) || defined(UNIX)
7995 int force_next = FALSE;
7996#endif
7997
7998 if (end_row > screen_Rows) /* safety check */
7999 end_row = screen_Rows;
8000 if (end_col > screen_Columns) /* safety check */
8001 end_col = screen_Columns;
8002 if (ScreenLines == NULL
8003 || start_row >= end_row
8004 || start_col >= end_col) /* nothing to do */
8005 return;
8006
8007 /* it's a "normal" terminal when not in a GUI or cterm */
8008 norm_term = (
8009#ifdef FEAT_GUI
8010 !gui.in_use &&
8011#endif
8012 t_colors <= 1);
8013 for (row = start_row; row < end_row; ++row)
8014 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008015#ifdef FEAT_MBYTE
8016 if (has_mbyte
8017# ifdef FEAT_GUI
8018 && !gui.in_use
8019# endif
8020 )
8021 {
8022 /* When drawing over the right halve of a double-wide char clear
8023 * out the left halve. When drawing over the left halve of a
8024 * double wide-char clear out the right halve. Only needed in a
8025 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008026 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008027 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008028 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008029 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008030 }
8031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 /*
8033 * Try to use delete-line termcap code, when no attributes or in a
8034 * "normal" terminal, where a bold/italic space is just a
8035 * space.
8036 */
8037 did_delete = FALSE;
8038 if (c2 == ' '
8039 && end_col == Columns
8040 && can_clear(T_CE)
8041 && (attr == 0
8042 || (norm_term
8043 && attr <= HL_ALL
8044 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8045 {
8046 /*
8047 * check if we really need to clear something
8048 */
8049 col = start_col;
8050 if (c1 != ' ') /* don't clear first char */
8051 ++col;
8052
8053 off = LineOffset[row] + col;
8054 end_off = LineOffset[row] + end_col;
8055
8056 /* skip blanks (used often, keep it fast!) */
8057#ifdef FEAT_MBYTE
8058 if (enc_utf8)
8059 while (off < end_off && ScreenLines[off] == ' '
8060 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8061 ++off;
8062 else
8063#endif
8064 while (off < end_off && ScreenLines[off] == ' '
8065 && ScreenAttrs[off] == 0)
8066 ++off;
8067 if (off < end_off) /* something to be cleared */
8068 {
8069 col = off - LineOffset[row];
8070 screen_stop_highlight();
8071 term_windgoto(row, col);/* clear rest of this screen line */
8072 out_str(T_CE);
8073 screen_start(); /* don't know where cursor is now */
8074 col = end_col - col;
8075 while (col--) /* clear chars in ScreenLines */
8076 {
8077 ScreenLines[off] = ' ';
8078#ifdef FEAT_MBYTE
8079 if (enc_utf8)
8080 ScreenLinesUC[off] = 0;
8081#endif
8082 ScreenAttrs[off] = 0;
8083 ++off;
8084 }
8085 }
8086 did_delete = TRUE; /* the chars are cleared now */
8087 }
8088
8089 off = LineOffset[row] + start_col;
8090 c = c1;
8091 for (col = start_col; col < end_col; ++col)
8092 {
8093 if (ScreenLines[off] != c
8094#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008095 || (enc_utf8 && (int)ScreenLinesUC[off]
8096 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097#endif
8098 || ScreenAttrs[off] != attr
8099#if defined(FEAT_GUI) || defined(UNIX)
8100 || force_next
8101#endif
8102 )
8103 {
8104#if defined(FEAT_GUI) || defined(UNIX)
8105 /* The bold trick may make a single row of pixels appear in
8106 * the next character. When a bold character is removed, the
8107 * next character should be redrawn too. This happens for our
8108 * own GUI and for some xterms. */
8109 if (
8110# ifdef FEAT_GUI
8111 gui.in_use
8112# endif
8113# if defined(FEAT_GUI) && defined(UNIX)
8114 ||
8115# endif
8116# ifdef UNIX
8117 term_is_xterm
8118# endif
8119 )
8120 {
8121 if (ScreenLines[off] != ' '
8122 && (ScreenAttrs[off] > HL_ALL
8123 || ScreenAttrs[off] & HL_BOLD))
8124 force_next = TRUE;
8125 else
8126 force_next = FALSE;
8127 }
8128#endif
8129 ScreenLines[off] = c;
8130#ifdef FEAT_MBYTE
8131 if (enc_utf8)
8132 {
8133 if (c >= 0x80)
8134 {
8135 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008136 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 }
8138 else
8139 ScreenLinesUC[off] = 0;
8140 }
8141#endif
8142 ScreenAttrs[off] = attr;
8143 if (!did_delete || c != ' ')
8144 screen_char(off, row, col);
8145 }
8146 ++off;
8147 if (col == start_col)
8148 {
8149 if (did_delete)
8150 break;
8151 c = c2;
8152 }
8153 }
8154 if (end_col == Columns)
8155 LineWraps[row] = FALSE;
8156 if (row == Rows - 1) /* overwritten the command line */
8157 {
8158 redraw_cmdline = TRUE;
8159 if (c1 == ' ' && c2 == ' ')
8160 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008161 if (start_col == 0)
8162 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 }
8164 }
8165}
8166
8167/*
8168 * Check if there should be a delay. Used before clearing or redrawing the
8169 * screen or the command line.
8170 */
8171 void
8172check_for_delay(check_msg_scroll)
8173 int check_msg_scroll;
8174{
8175 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8176 && !did_wait_return
8177 && emsg_silent == 0)
8178 {
8179 out_flush();
8180 ui_delay(1000L, TRUE);
8181 emsg_on_display = FALSE;
8182 if (check_msg_scroll)
8183 msg_scroll = FALSE;
8184 }
8185}
8186
8187/*
8188 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008189 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 * Returns TRUE if there is a valid screen to write to.
8191 * Returns FALSE when starting up and screen not initialized yet.
8192 */
8193 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008194screen_valid(doclear)
8195 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008197 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 return (ScreenLines != NULL);
8199}
8200
8201/*
8202 * Resize the shell to Rows and Columns.
8203 * Allocate ScreenLines[] and associated items.
8204 *
8205 * There may be some time between setting Rows and Columns and (re)allocating
8206 * ScreenLines[]. This happens when starting up and when (manually) changing
8207 * the shell size. Always use screen_Rows and screen_Columns to access items
8208 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8209 * final size of the shell is needed.
8210 */
8211 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008212screenalloc(doclear)
8213 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
8215 int new_row, old_row;
8216#ifdef FEAT_GUI
8217 int old_Rows;
8218#endif
8219 win_T *wp;
8220 int outofmem = FALSE;
8221 int len;
8222 schar_T *new_ScreenLines;
8223#ifdef FEAT_MBYTE
8224 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008225 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008227 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228#endif
8229 sattr_T *new_ScreenAttrs;
8230 unsigned *new_LineOffset;
8231 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008232#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008233 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008234 tabpage_T *tp;
8235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008237 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008238#ifdef FEAT_AUTOCMD
8239 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008241retry:
8242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 /*
8244 * Allocation of the screen buffers is done only when the size changes and
8245 * when Rows and Columns have been set and we have started doing full
8246 * screen stuff.
8247 */
8248 if ((ScreenLines != NULL
8249 && Rows == screen_Rows
8250 && Columns == screen_Columns
8251#ifdef FEAT_MBYTE
8252 && enc_utf8 == (ScreenLinesUC != NULL)
8253 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008254 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255#endif
8256 )
8257 || Rows == 0
8258 || Columns == 0
8259 || (!full_screen && ScreenLines == NULL))
8260 return;
8261
8262 /*
8263 * It's possible that we produce an out-of-memory message below, which
8264 * will cause this function to be called again. To break the loop, just
8265 * return here.
8266 */
8267 if (entered)
8268 return;
8269 entered = TRUE;
8270
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008271 /*
8272 * Note that the window sizes are updated before reallocating the arrays,
8273 * thus we must not redraw here!
8274 */
8275 ++RedrawingDisabled;
8276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 win_new_shellsize(); /* fit the windows in the new sized shell */
8278
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 comp_col(); /* recompute columns for shown command and ruler */
8280
8281 /*
8282 * We're changing the size of the screen.
8283 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8284 * - Move lines from the old arrays into the new arrays, clear extra
8285 * lines (unless the screen is going to be cleared).
8286 * - Free the old arrays.
8287 *
8288 * If anything fails, make ScreenLines NULL, so we don't do anything!
8289 * Continuing with the old ScreenLines may result in a crash, because the
8290 * size is wrong.
8291 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008292 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008294#ifdef FEAT_AUTOCMD
8295 if (aucmd_win != NULL)
8296 win_free_lsize(aucmd_win);
8297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298
8299 new_ScreenLines = (schar_T *)lalloc((long_u)(
8300 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8301#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008302 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 if (enc_utf8)
8304 {
8305 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8306 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008307 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008308 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8310 }
8311 if (enc_dbcs == DBCS_JPNU)
8312 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8313 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8314#endif
8315 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8316 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8317 new_LineOffset = (unsigned *)lalloc((long_u)(
8318 Rows * sizeof(unsigned)), FALSE);
8319 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008320#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008321 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008324 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {
8326 if (win_alloc_lines(wp) == FAIL)
8327 {
8328 outofmem = TRUE;
8329#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008330 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331#endif
8332 }
8333 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008334#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008335 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8336 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008337 outofmem = TRUE;
8338#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008339#ifdef FEAT_WINDOWS
8340give_up:
8341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008343#ifdef FEAT_MBYTE
8344 for (i = 0; i < p_mco; ++i)
8345 if (new_ScreenLinesC[i] == NULL)
8346 break;
8347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 if (new_ScreenLines == NULL
8349#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008350 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8352#endif
8353 || new_ScreenAttrs == NULL
8354 || new_LineOffset == NULL
8355 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008356#ifdef FEAT_WINDOWS
8357 || new_TabPageIdxs == NULL
8358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 || outofmem)
8360 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008361 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008362 {
8363 /* guess the size */
8364 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8365
8366 /* Remember we did this to avoid getting outofmem messages over
8367 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008368 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 vim_free(new_ScreenLines);
8371 new_ScreenLines = NULL;
8372#ifdef FEAT_MBYTE
8373 vim_free(new_ScreenLinesUC);
8374 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008375 for (i = 0; i < p_mco; ++i)
8376 {
8377 vim_free(new_ScreenLinesC[i]);
8378 new_ScreenLinesC[i] = NULL;
8379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 vim_free(new_ScreenLines2);
8381 new_ScreenLines2 = NULL;
8382#endif
8383 vim_free(new_ScreenAttrs);
8384 new_ScreenAttrs = NULL;
8385 vim_free(new_LineOffset);
8386 new_LineOffset = NULL;
8387 vim_free(new_LineWraps);
8388 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008389#ifdef FEAT_WINDOWS
8390 vim_free(new_TabPageIdxs);
8391 new_TabPageIdxs = NULL;
8392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 }
8394 else
8395 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008396 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008397
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 for (new_row = 0; new_row < Rows; ++new_row)
8399 {
8400 new_LineOffset[new_row] = new_row * Columns;
8401 new_LineWraps[new_row] = FALSE;
8402
8403 /*
8404 * If the screen is not going to be cleared, copy as much as
8405 * possible from the old screen to the new one and clear the rest
8406 * (used when resizing the window at the "--more--" prompt or when
8407 * executing an external command, for the GUI).
8408 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008409 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
8411 (void)vim_memset(new_ScreenLines + new_row * Columns,
8412 ' ', (size_t)Columns * sizeof(schar_T));
8413#ifdef FEAT_MBYTE
8414 if (enc_utf8)
8415 {
8416 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8417 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008418 for (i = 0; i < p_mco; ++i)
8419 (void)vim_memset(new_ScreenLinesC[i]
8420 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 0, (size_t)Columns * sizeof(u8char_T));
8422 }
8423 if (enc_dbcs == DBCS_JPNU)
8424 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8425 0, (size_t)Columns * sizeof(schar_T));
8426#endif
8427 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8428 0, (size_t)Columns * sizeof(sattr_T));
8429 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008430 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {
8432 if (screen_Columns < Columns)
8433 len = screen_Columns;
8434 else
8435 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008436#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008437 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008438 * may be invalid now. Also when p_mco changes. */
8439 if (!(enc_utf8 && ScreenLinesUC == NULL)
8440 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008441#endif
8442 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8443 ScreenLines + LineOffset[old_row],
8444 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008446 if (enc_utf8 && ScreenLinesUC != NULL
8447 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {
8449 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8450 ScreenLinesUC + LineOffset[old_row],
8451 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008452 for (i = 0; i < p_mco; ++i)
8453 mch_memmove(new_ScreenLinesC[i]
8454 + new_LineOffset[new_row],
8455 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 (size_t)len * sizeof(u8char_T));
8457 }
8458 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8459 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8460 ScreenLines2 + LineOffset[old_row],
8461 (size_t)len * sizeof(schar_T));
8462#endif
8463 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8464 ScreenAttrs + LineOffset[old_row],
8465 (size_t)len * sizeof(sattr_T));
8466 }
8467 }
8468 }
8469 /* Use the last line of the screen for the current line. */
8470 current_ScreenLine = new_ScreenLines + Rows * Columns;
8471 }
8472
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008473 free_screenlines();
8474
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 ScreenLines = new_ScreenLines;
8476#ifdef FEAT_MBYTE
8477 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008478 for (i = 0; i < p_mco; ++i)
8479 ScreenLinesC[i] = new_ScreenLinesC[i];
8480 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 ScreenLines2 = new_ScreenLines2;
8482#endif
8483 ScreenAttrs = new_ScreenAttrs;
8484 LineOffset = new_LineOffset;
8485 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008486#ifdef FEAT_WINDOWS
8487 TabPageIdxs = new_TabPageIdxs;
8488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
8490 /* It's important that screen_Rows and screen_Columns reflect the actual
8491 * size of ScreenLines[]. Set them before calling anything. */
8492#ifdef FEAT_GUI
8493 old_Rows = screen_Rows;
8494#endif
8495 screen_Rows = Rows;
8496 screen_Columns = Columns;
8497
8498 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008499 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 screenclear2();
8501
8502#ifdef FEAT_GUI
8503 else if (gui.in_use
8504 && !gui.starting
8505 && ScreenLines != NULL
8506 && old_Rows != Rows)
8507 {
8508 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8509 /*
8510 * Adjust the position of the cursor, for when executing an external
8511 * command.
8512 */
8513 if (msg_row >= Rows) /* Rows got smaller */
8514 msg_row = Rows - 1; /* put cursor at last row */
8515 else if (Rows > old_Rows) /* Rows got bigger */
8516 msg_row += Rows - old_Rows; /* put cursor in same place */
8517 if (msg_col >= Columns) /* Columns got smaller */
8518 msg_col = Columns - 1; /* put cursor at last column */
8519 }
8520#endif
8521
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008523 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008524
8525#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008526 /*
8527 * Do not apply autocommands more than 3 times to avoid an endless loop
8528 * in case applying autocommands always changes Rows or Columns.
8529 */
8530 if (starting == 0 && ++retry_count <= 3)
8531 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008532 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008533 /* In rare cases, autocommands may have altered Rows or Columns,
8534 * jump back to check if we need to allocate the screen again. */
8535 goto retry;
8536 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538}
8539
8540 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008541free_screenlines()
8542{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008543#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008544 int i;
8545
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008546 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008547 for (i = 0; i < Screen_mco; ++i)
8548 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008549 vim_free(ScreenLines2);
8550#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008551 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008552 vim_free(ScreenAttrs);
8553 vim_free(LineOffset);
8554 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008555#ifdef FEAT_WINDOWS
8556 vim_free(TabPageIdxs);
8557#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008558}
8559
8560 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561screenclear()
8562{
8563 check_for_delay(FALSE);
8564 screenalloc(FALSE); /* allocate screen buffers if size changed */
8565 screenclear2(); /* clear the screen */
8566}
8567
8568 static void
8569screenclear2()
8570{
8571 int i;
8572
8573 if (starting == NO_SCREEN || ScreenLines == NULL
8574#ifdef FEAT_GUI
8575 || (gui.in_use && gui.starting)
8576#endif
8577 )
8578 return;
8579
8580#ifdef FEAT_GUI
8581 if (!gui.in_use)
8582#endif
8583 screen_attr = -1; /* force setting the Normal colors */
8584 screen_stop_highlight(); /* don't want highlighting here */
8585
8586#ifdef FEAT_CLIPBOARD
8587 /* disable selection without redrawing it */
8588 clip_scroll_selection(9999);
8589#endif
8590
8591 /* blank out ScreenLines */
8592 for (i = 0; i < Rows; ++i)
8593 {
8594 lineclear(LineOffset[i], (int)Columns);
8595 LineWraps[i] = FALSE;
8596 }
8597
8598 if (can_clear(T_CL))
8599 {
8600 out_str(T_CL); /* clear the display */
8601 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008602 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 }
8604 else
8605 {
8606 /* can't clear the screen, mark all chars with invalid attributes */
8607 for (i = 0; i < Rows; ++i)
8608 lineinvalid(LineOffset[i], (int)Columns);
8609 clear_cmdline = TRUE;
8610 }
8611
8612 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8613
8614 win_rest_invalid(firstwin);
8615 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008616#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008617 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 if (must_redraw == CLEAR) /* no need to clear again */
8620 must_redraw = NOT_VALID;
8621 compute_cmdrow();
8622 msg_row = cmdline_row; /* put cursor on last line for messages */
8623 msg_col = 0;
8624 screen_start(); /* don't know where cursor is now */
8625 msg_scrolled = 0; /* can't scroll back */
8626 msg_didany = FALSE;
8627 msg_didout = FALSE;
8628}
8629
8630/*
8631 * Clear one line in ScreenLines.
8632 */
8633 static void
8634lineclear(off, width)
8635 unsigned off;
8636 int width;
8637{
8638 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8639#ifdef FEAT_MBYTE
8640 if (enc_utf8)
8641 (void)vim_memset(ScreenLinesUC + off, 0,
8642 (size_t)width * sizeof(u8char_T));
8643#endif
8644 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8645}
8646
8647/*
8648 * Mark one line in ScreenLines invalid by setting the attributes to an
8649 * invalid value.
8650 */
8651 static void
8652lineinvalid(off, width)
8653 unsigned off;
8654 int width;
8655{
8656 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8657}
8658
8659#ifdef FEAT_VERTSPLIT
8660/*
8661 * Copy part of a Screenline for vertically split window "wp".
8662 */
8663 static void
8664linecopy(to, from, wp)
8665 int to;
8666 int from;
8667 win_T *wp;
8668{
8669 unsigned off_to = LineOffset[to] + wp->w_wincol;
8670 unsigned off_from = LineOffset[from] + wp->w_wincol;
8671
8672 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8673 wp->w_width * sizeof(schar_T));
8674# ifdef FEAT_MBYTE
8675 if (enc_utf8)
8676 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008677 int i;
8678
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8680 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008681 for (i = 0; i < p_mco; ++i)
8682 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8683 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 }
8685 if (enc_dbcs == DBCS_JPNU)
8686 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8687 wp->w_width * sizeof(schar_T));
8688# endif
8689 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8690 wp->w_width * sizeof(sattr_T));
8691}
8692#endif
8693
8694/*
8695 * Return TRUE if clearing with term string "p" would work.
8696 * It can't work when the string is empty or it won't set the right background.
8697 */
8698 int
8699can_clear(p)
8700 char_u *p;
8701{
8702 return (*p != NUL && (t_colors <= 1
8703#ifdef FEAT_GUI
8704 || gui.in_use
8705#endif
8706 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8707}
8708
8709/*
8710 * Reset cursor position. Use whenever cursor was moved because of outputting
8711 * something directly to the screen (shell commands) or a terminal control
8712 * code.
8713 */
8714 void
8715screen_start()
8716{
8717 screen_cur_row = screen_cur_col = 9999;
8718}
8719
8720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 * Move the cursor to position "row","col" in the screen.
8722 * This tries to find the most efficient way to move, minimizing the number of
8723 * characters sent to the terminal.
8724 */
8725 void
8726windgoto(row, col)
8727 int row;
8728 int col;
8729{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008730 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 int i;
8732 int plan;
8733 int cost;
8734 int wouldbe_col;
8735 int noinvcurs;
8736 char_u *bs;
8737 int goto_cost;
8738 int attr;
8739
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008740#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8742
8743#define PLAN_LE 1
8744#define PLAN_CR 2
8745#define PLAN_NL 3
8746#define PLAN_WRITE 4
8747 /* Can't use ScreenLines unless initialized */
8748 if (ScreenLines == NULL)
8749 return;
8750
8751 if (col != screen_cur_col || row != screen_cur_row)
8752 {
8753 /* Check for valid position. */
8754 if (row < 0) /* window without text lines? */
8755 row = 0;
8756 if (row >= screen_Rows)
8757 row = screen_Rows - 1;
8758 if (col >= screen_Columns)
8759 col = screen_Columns - 1;
8760
8761 /* check if no cursor movement is allowed in highlight mode */
8762 if (screen_attr && *T_MS == NUL)
8763 noinvcurs = HIGHL_COST;
8764 else
8765 noinvcurs = 0;
8766 goto_cost = GOTO_COST + noinvcurs;
8767
8768 /*
8769 * Plan how to do the positioning:
8770 * 1. Use CR to move it to column 0, same row.
8771 * 2. Use T_LE to move it a few columns to the left.
8772 * 3. Use NL to move a few lines down, column 0.
8773 * 4. Move a few columns to the right with T_ND or by writing chars.
8774 *
8775 * Don't do this if the cursor went beyond the last column, the cursor
8776 * position is unknown then (some terminals wrap, some don't )
8777 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008778 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 * characters to move the cursor to the right.
8780 */
8781 if (row >= screen_cur_row && screen_cur_col < Columns)
8782 {
8783 /*
8784 * If the cursor is in the same row, bigger col, we can use CR
8785 * or T_LE.
8786 */
8787 bs = NULL; /* init for GCC */
8788 attr = screen_attr;
8789 if (row == screen_cur_row && col < screen_cur_col)
8790 {
8791 /* "le" is preferred over "bc", because "bc" is obsolete */
8792 if (*T_LE)
8793 bs = T_LE; /* "cursor left" */
8794 else
8795 bs = T_BC; /* "backspace character (old) */
8796 if (*bs)
8797 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8798 else
8799 cost = 999;
8800 if (col + 1 < cost) /* using CR is less characters */
8801 {
8802 plan = PLAN_CR;
8803 wouldbe_col = 0;
8804 cost = 1; /* CR is just one character */
8805 }
8806 else
8807 {
8808 plan = PLAN_LE;
8809 wouldbe_col = col;
8810 }
8811 if (noinvcurs) /* will stop highlighting */
8812 {
8813 cost += noinvcurs;
8814 attr = 0;
8815 }
8816 }
8817
8818 /*
8819 * If the cursor is above where we want to be, we can use CR LF.
8820 */
8821 else if (row > screen_cur_row)
8822 {
8823 plan = PLAN_NL;
8824 wouldbe_col = 0;
8825 cost = (row - screen_cur_row) * 2; /* CR LF */
8826 if (noinvcurs) /* will stop highlighting */
8827 {
8828 cost += noinvcurs;
8829 attr = 0;
8830 }
8831 }
8832
8833 /*
8834 * If the cursor is in the same row, smaller col, just use write.
8835 */
8836 else
8837 {
8838 plan = PLAN_WRITE;
8839 wouldbe_col = screen_cur_col;
8840 cost = 0;
8841 }
8842
8843 /*
8844 * Check if any characters that need to be written have the
8845 * correct attributes. Also avoid UTF-8 characters.
8846 */
8847 i = col - wouldbe_col;
8848 if (i > 0)
8849 cost += i;
8850 if (cost < goto_cost && i > 0)
8851 {
8852 /*
8853 * Check if the attributes are correct without additionally
8854 * stopping highlighting.
8855 */
8856 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8857 while (i && *p++ == attr)
8858 --i;
8859 if (i != 0)
8860 {
8861 /*
8862 * Try if it works when highlighting is stopped here.
8863 */
8864 if (*--p == 0)
8865 {
8866 cost += noinvcurs;
8867 while (i && *p++ == 0)
8868 --i;
8869 }
8870 if (i != 0)
8871 cost = 999; /* different attributes, don't do it */
8872 }
8873#ifdef FEAT_MBYTE
8874 if (enc_utf8)
8875 {
8876 /* Don't use an UTF-8 char for positioning, it's slow. */
8877 for (i = wouldbe_col; i < col; ++i)
8878 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8879 {
8880 cost = 999;
8881 break;
8882 }
8883 }
8884#endif
8885 }
8886
8887 /*
8888 * We can do it without term_windgoto()!
8889 */
8890 if (cost < goto_cost)
8891 {
8892 if (plan == PLAN_LE)
8893 {
8894 if (noinvcurs)
8895 screen_stop_highlight();
8896 while (screen_cur_col > col)
8897 {
8898 out_str(bs);
8899 --screen_cur_col;
8900 }
8901 }
8902 else if (plan == PLAN_CR)
8903 {
8904 if (noinvcurs)
8905 screen_stop_highlight();
8906 out_char('\r');
8907 screen_cur_col = 0;
8908 }
8909 else if (plan == PLAN_NL)
8910 {
8911 if (noinvcurs)
8912 screen_stop_highlight();
8913 while (screen_cur_row < row)
8914 {
8915 out_char('\n');
8916 ++screen_cur_row;
8917 }
8918 screen_cur_col = 0;
8919 }
8920
8921 i = col - screen_cur_col;
8922 if (i > 0)
8923 {
8924 /*
8925 * Use cursor-right if it's one character only. Avoids
8926 * removing a line of pixels from the last bold char, when
8927 * using the bold trick in the GUI.
8928 */
8929 if (T_ND[0] != NUL && T_ND[1] == NUL)
8930 {
8931 while (i-- > 0)
8932 out_char(*T_ND);
8933 }
8934 else
8935 {
8936 int off;
8937
8938 off = LineOffset[row] + screen_cur_col;
8939 while (i-- > 0)
8940 {
8941 if (ScreenAttrs[off] != screen_attr)
8942 screen_stop_highlight();
8943#ifdef FEAT_MBYTE
8944 out_flush_check();
8945#endif
8946 out_char(ScreenLines[off]);
8947#ifdef FEAT_MBYTE
8948 if (enc_dbcs == DBCS_JPNU
8949 && ScreenLines[off] == 0x8e)
8950 out_char(ScreenLines2[off]);
8951#endif
8952 ++off;
8953 }
8954 }
8955 }
8956 }
8957 }
8958 else
8959 cost = 999;
8960
8961 if (cost >= goto_cost)
8962 {
8963 if (noinvcurs)
8964 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02008965 if (row == screen_cur_row && (col > screen_cur_col)
8966 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 term_cursor_right(col - screen_cur_col);
8968 else
8969 term_windgoto(row, col);
8970 }
8971 screen_cur_row = row;
8972 screen_cur_col = col;
8973 }
8974}
8975
8976/*
8977 * Set cursor to its position in the current window.
8978 */
8979 void
8980setcursor()
8981{
8982 if (redrawing())
8983 {
8984 validate_cursor();
8985 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8986 W_WINCOL(curwin) + (
8987#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008988 /* With 'rightleft' set and the cursor on a double-wide
8989 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8991# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008992 (has_mbyte
8993 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8994 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995# endif
8996 1)) :
8997#endif
8998 curwin->w_wcol));
8999 }
9000}
9001
9002
9003/*
9004 * insert 'line_count' lines at 'row' in window 'wp'
9005 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9006 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9007 * scrolling.
9008 * Returns FAIL if the lines are not inserted, OK for success.
9009 */
9010 int
9011win_ins_lines(wp, row, line_count, invalid, mayclear)
9012 win_T *wp;
9013 int row;
9014 int line_count;
9015 int invalid;
9016 int mayclear;
9017{
9018 int did_delete;
9019 int nextrow;
9020 int lastrow;
9021 int retval;
9022
9023 if (invalid)
9024 wp->w_lines_valid = 0;
9025
9026 if (wp->w_height < 5)
9027 return FAIL;
9028
9029 if (line_count > wp->w_height - row)
9030 line_count = wp->w_height - row;
9031
9032 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9033 if (retval != MAYBE)
9034 return retval;
9035
9036 /*
9037 * If there is a next window or a status line, we first try to delete the
9038 * lines at the bottom to avoid messing what is after the window.
9039 * If this fails and there are following windows, don't do anything to avoid
9040 * messing up those windows, better just redraw.
9041 */
9042 did_delete = FALSE;
9043#ifdef FEAT_WINDOWS
9044 if (wp->w_next != NULL || wp->w_status_height)
9045 {
9046 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9047 line_count, (int)Rows, FALSE, NULL) == OK)
9048 did_delete = TRUE;
9049 else if (wp->w_next)
9050 return FAIL;
9051 }
9052#endif
9053 /*
9054 * if no lines deleted, blank the lines that will end up below the window
9055 */
9056 if (!did_delete)
9057 {
9058#ifdef FEAT_WINDOWS
9059 wp->w_redr_status = TRUE;
9060#endif
9061 redraw_cmdline = TRUE;
9062 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9063 lastrow = nextrow + line_count;
9064 if (lastrow > Rows)
9065 lastrow = Rows;
9066 screen_fill(nextrow - line_count, lastrow - line_count,
9067 W_WINCOL(wp), (int)W_ENDCOL(wp),
9068 ' ', ' ', 0);
9069 }
9070
9071 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9072 == FAIL)
9073 {
9074 /* deletion will have messed up other windows */
9075 if (did_delete)
9076 {
9077#ifdef FEAT_WINDOWS
9078 wp->w_redr_status = TRUE;
9079#endif
9080 win_rest_invalid(W_NEXT(wp));
9081 }
9082 return FAIL;
9083 }
9084
9085 return OK;
9086}
9087
9088/*
9089 * delete "line_count" window lines at "row" in window "wp"
9090 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9091 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9092 * scrolling
9093 * Return OK for success, FAIL if the lines are not deleted.
9094 */
9095 int
9096win_del_lines(wp, row, line_count, invalid, mayclear)
9097 win_T *wp;
9098 int row;
9099 int line_count;
9100 int invalid;
9101 int mayclear;
9102{
9103 int retval;
9104
9105 if (invalid)
9106 wp->w_lines_valid = 0;
9107
9108 if (line_count > wp->w_height - row)
9109 line_count = wp->w_height - row;
9110
9111 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9112 if (retval != MAYBE)
9113 return retval;
9114
9115 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9116 (int)Rows, FALSE, NULL) == FAIL)
9117 return FAIL;
9118
9119#ifdef FEAT_WINDOWS
9120 /*
9121 * If there are windows or status lines below, try to put them at the
9122 * correct place. If we can't do that, they have to be redrawn.
9123 */
9124 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9125 {
9126 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9127 line_count, (int)Rows, NULL) == FAIL)
9128 {
9129 wp->w_redr_status = TRUE;
9130 win_rest_invalid(wp->w_next);
9131 }
9132 }
9133 /*
9134 * If this is the last window and there is no status line, redraw the
9135 * command line later.
9136 */
9137 else
9138#endif
9139 redraw_cmdline = TRUE;
9140 return OK;
9141}
9142
9143/*
9144 * Common code for win_ins_lines() and win_del_lines().
9145 * Returns OK or FAIL when the work has been done.
9146 * Returns MAYBE when not finished yet.
9147 */
9148 static int
9149win_do_lines(wp, row, line_count, mayclear, del)
9150 win_T *wp;
9151 int row;
9152 int line_count;
9153 int mayclear;
9154 int del;
9155{
9156 int retval;
9157
9158 if (!redrawing() || line_count <= 0)
9159 return FAIL;
9160
9161 /* only a few lines left: redraw is faster */
9162 if (mayclear && Rows - line_count < 5
9163#ifdef FEAT_VERTSPLIT
9164 && wp->w_width == Columns
9165#endif
9166 )
9167 {
9168 screenclear(); /* will set wp->w_lines_valid to 0 */
9169 return FAIL;
9170 }
9171
9172 /*
9173 * Delete all remaining lines
9174 */
9175 if (row + line_count >= wp->w_height)
9176 {
9177 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9178 W_WINCOL(wp), (int)W_ENDCOL(wp),
9179 ' ', ' ', 0);
9180 return OK;
9181 }
9182
9183 /*
9184 * when scrolling, the message on the command line should be cleared,
9185 * otherwise it will stay there forever.
9186 */
9187 clear_cmdline = TRUE;
9188
9189 /*
9190 * If the terminal can set a scroll region, use that.
9191 * Always do this in a vertically split window. This will redraw from
9192 * ScreenLines[] when t_CV isn't defined. That's faster than using
9193 * win_line().
9194 * Don't use a scroll region when we are going to redraw the text, writing
9195 * a character in the lower right corner of the scroll region causes a
9196 * scroll-up in the DJGPP version.
9197 */
9198 if (scroll_region
9199#ifdef FEAT_VERTSPLIT
9200 || W_WIDTH(wp) != Columns
9201#endif
9202 )
9203 {
9204#ifdef FEAT_VERTSPLIT
9205 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9206#endif
9207 scroll_region_set(wp, row);
9208 if (del)
9209 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9210 wp->w_height - row, FALSE, wp);
9211 else
9212 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9213 wp->w_height - row, wp);
9214#ifdef FEAT_VERTSPLIT
9215 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9216#endif
9217 scroll_region_reset();
9218 return retval;
9219 }
9220
9221#ifdef FEAT_WINDOWS
9222 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9223 return FAIL;
9224#endif
9225
9226 return MAYBE;
9227}
9228
9229/*
9230 * window 'wp' and everything after it is messed up, mark it for redraw
9231 */
9232 static void
9233win_rest_invalid(wp)
9234 win_T *wp;
9235{
9236#ifdef FEAT_WINDOWS
9237 while (wp != NULL)
9238#else
9239 if (wp != NULL)
9240#endif
9241 {
9242 redraw_win_later(wp, NOT_VALID);
9243#ifdef FEAT_WINDOWS
9244 wp->w_redr_status = TRUE;
9245 wp = wp->w_next;
9246#endif
9247 }
9248 redraw_cmdline = TRUE;
9249}
9250
9251/*
9252 * The rest of the routines in this file perform screen manipulations. The
9253 * given operation is performed physically on the screen. The corresponding
9254 * change is also made to the internal screen image. In this way, the editor
9255 * anticipates the effect of editing changes on the appearance of the screen.
9256 * That way, when we call screenupdate a complete redraw isn't usually
9257 * necessary. Another advantage is that we can keep adding code to anticipate
9258 * screen changes, and in the meantime, everything still works.
9259 */
9260
9261/*
9262 * types for inserting or deleting lines
9263 */
9264#define USE_T_CAL 1
9265#define USE_T_CDL 2
9266#define USE_T_AL 3
9267#define USE_T_CE 4
9268#define USE_T_DL 5
9269#define USE_T_SR 6
9270#define USE_NL 7
9271#define USE_T_CD 8
9272#define USE_REDRAW 9
9273
9274/*
9275 * insert lines on the screen and update ScreenLines[]
9276 * 'end' is the line after the scrolled part. Normally it is Rows.
9277 * When scrolling region used 'off' is the offset from the top for the region.
9278 * 'row' and 'end' are relative to the start of the region.
9279 *
9280 * return FAIL for failure, OK for success.
9281 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009282 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283screen_ins_lines(off, row, line_count, end, wp)
9284 int off;
9285 int row;
9286 int line_count;
9287 int end;
9288 win_T *wp; /* NULL or window to use width from */
9289{
9290 int i;
9291 int j;
9292 unsigned temp;
9293 int cursor_row;
9294 int type;
9295 int result_empty;
9296 int can_ce = can_clear(T_CE);
9297
9298 /*
9299 * FAIL if
9300 * - there is no valid screen
9301 * - the screen has to be redrawn completely
9302 * - the line count is less than one
9303 * - the line count is more than 'ttyscroll'
9304 */
9305 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9306 return FAIL;
9307
9308 /*
9309 * There are seven ways to insert lines:
9310 * 0. When in a vertically split window and t_CV isn't set, redraw the
9311 * characters from ScreenLines[].
9312 * 1. Use T_CD (clear to end of display) if it exists and the result of
9313 * the insert is just empty lines
9314 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9315 * present or line_count > 1. It looks better if we do all the inserts
9316 * at once.
9317 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9318 * insert is just empty lines and T_CE is not present or line_count >
9319 * 1.
9320 * 4. Use T_AL (insert line) if it exists.
9321 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9322 * just empty lines.
9323 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9324 * just empty lines.
9325 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9326 * the 'da' flag is not set or we have clear line capability.
9327 * 8. redraw the characters from ScreenLines[].
9328 *
9329 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9330 * the scrollbar for the window. It does have insert line, use that if it
9331 * exists.
9332 */
9333 result_empty = (row + line_count >= end);
9334#ifdef FEAT_VERTSPLIT
9335 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9336 type = USE_REDRAW;
9337 else
9338#endif
9339 if (can_clear(T_CD) && result_empty)
9340 type = USE_T_CD;
9341 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9342 type = USE_T_CAL;
9343 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9344 type = USE_T_CDL;
9345 else if (*T_AL != NUL)
9346 type = USE_T_AL;
9347 else if (can_ce && result_empty)
9348 type = USE_T_CE;
9349 else if (*T_DL != NUL && result_empty)
9350 type = USE_T_DL;
9351 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9352 type = USE_T_SR;
9353 else
9354 return FAIL;
9355
9356 /*
9357 * For clearing the lines screen_del_lines() is used. This will also take
9358 * care of t_db if necessary.
9359 */
9360 if (type == USE_T_CD || type == USE_T_CDL ||
9361 type == USE_T_CE || type == USE_T_DL)
9362 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9363
9364 /*
9365 * If text is retained below the screen, first clear or delete as many
9366 * lines at the bottom of the window as are about to be inserted so that
9367 * the deleted lines won't later surface during a screen_del_lines.
9368 */
9369 if (*T_DB)
9370 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9371
9372#ifdef FEAT_CLIPBOARD
9373 /* Remove a modeless selection when inserting lines halfway the screen
9374 * or not the full width of the screen. */
9375 if (off + row > 0
9376# ifdef FEAT_VERTSPLIT
9377 || (wp != NULL && wp->w_width != Columns)
9378# endif
9379 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009380 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 else
9382 clip_scroll_selection(-line_count);
9383#endif
9384
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385#ifdef FEAT_GUI
9386 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9387 * scrolling is actually carried out. */
9388 gui_dont_update_cursor();
9389#endif
9390
9391 if (*T_CCS != NUL) /* cursor relative to region */
9392 cursor_row = row;
9393 else
9394 cursor_row = row + off;
9395
9396 /*
9397 * Shift LineOffset[] line_count down to reflect the inserted lines.
9398 * Clear the inserted lines in ScreenLines[].
9399 */
9400 row += off;
9401 end += off;
9402 for (i = 0; i < line_count; ++i)
9403 {
9404#ifdef FEAT_VERTSPLIT
9405 if (wp != NULL && wp->w_width != Columns)
9406 {
9407 /* need to copy part of a line */
9408 j = end - 1 - i;
9409 while ((j -= line_count) >= row)
9410 linecopy(j + line_count, j, wp);
9411 j += line_count;
9412 if (can_clear((char_u *)" "))
9413 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9414 else
9415 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9416 LineWraps[j] = FALSE;
9417 }
9418 else
9419#endif
9420 {
9421 j = end - 1 - i;
9422 temp = LineOffset[j];
9423 while ((j -= line_count) >= row)
9424 {
9425 LineOffset[j + line_count] = LineOffset[j];
9426 LineWraps[j + line_count] = LineWraps[j];
9427 }
9428 LineOffset[j + line_count] = temp;
9429 LineWraps[j + line_count] = FALSE;
9430 if (can_clear((char_u *)" "))
9431 lineclear(temp, (int)Columns);
9432 else
9433 lineinvalid(temp, (int)Columns);
9434 }
9435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436
9437 screen_stop_highlight();
9438 windgoto(cursor_row, 0);
9439
9440#ifdef FEAT_VERTSPLIT
9441 /* redraw the characters */
9442 if (type == USE_REDRAW)
9443 redraw_block(row, end, wp);
9444 else
9445#endif
9446 if (type == USE_T_CAL)
9447 {
9448 term_append_lines(line_count);
9449 screen_start(); /* don't know where cursor is now */
9450 }
9451 else
9452 {
9453 for (i = 0; i < line_count; i++)
9454 {
9455 if (type == USE_T_AL)
9456 {
9457 if (i && cursor_row != 0)
9458 windgoto(cursor_row, 0);
9459 out_str(T_AL);
9460 }
9461 else /* type == USE_T_SR */
9462 out_str(T_SR);
9463 screen_start(); /* don't know where cursor is now */
9464 }
9465 }
9466
9467 /*
9468 * With scroll-reverse and 'da' flag set we need to clear the lines that
9469 * have been scrolled down into the region.
9470 */
9471 if (type == USE_T_SR && *T_DA)
9472 {
9473 for (i = 0; i < line_count; ++i)
9474 {
9475 windgoto(off + i, 0);
9476 out_str(T_CE);
9477 screen_start(); /* don't know where cursor is now */
9478 }
9479 }
9480
9481#ifdef FEAT_GUI
9482 gui_can_update_cursor();
9483 if (gui.in_use)
9484 out_flush(); /* always flush after a scroll */
9485#endif
9486 return OK;
9487}
9488
9489/*
9490 * delete lines on the screen and update ScreenLines[]
9491 * 'end' is the line after the scrolled part. Normally it is Rows.
9492 * When scrolling region used 'off' is the offset from the top for the region.
9493 * 'row' and 'end' are relative to the start of the region.
9494 *
9495 * Return OK for success, FAIL if the lines are not deleted.
9496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 int
9498screen_del_lines(off, row, line_count, end, force, wp)
9499 int off;
9500 int row;
9501 int line_count;
9502 int end;
9503 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009504 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506 int j;
9507 int i;
9508 unsigned temp;
9509 int cursor_row;
9510 int cursor_end;
9511 int result_empty; /* result is empty until end of region */
9512 int can_delete; /* deleting line codes can be used */
9513 int type;
9514
9515 /*
9516 * FAIL if
9517 * - there is no valid screen
9518 * - the screen has to be redrawn completely
9519 * - the line count is less than one
9520 * - the line count is more than 'ttyscroll'
9521 */
9522 if (!screen_valid(TRUE) || line_count <= 0 ||
9523 (!force && line_count > p_ttyscroll))
9524 return FAIL;
9525
9526 /*
9527 * Check if the rest of the current region will become empty.
9528 */
9529 result_empty = row + line_count >= end;
9530
9531 /*
9532 * We can delete lines only when 'db' flag not set or when 'ce' option
9533 * available.
9534 */
9535 can_delete = (*T_DB == NUL || can_clear(T_CE));
9536
9537 /*
9538 * There are six ways to delete lines:
9539 * 0. When in a vertically split window and t_CV isn't set, redraw the
9540 * characters from ScreenLines[].
9541 * 1. Use T_CD if it exists and the result is empty.
9542 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9543 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9544 * none of the other ways work.
9545 * 4. Use T_CE (erase line) if the result is empty.
9546 * 5. Use T_DL (delete line) if it exists.
9547 * 6. redraw the characters from ScreenLines[].
9548 */
9549#ifdef FEAT_VERTSPLIT
9550 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9551 type = USE_REDRAW;
9552 else
9553#endif
9554 if (can_clear(T_CD) && result_empty)
9555 type = USE_T_CD;
9556#if defined(__BEOS__) && defined(BEOS_DR8)
9557 /*
9558 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9559 * its internal termcap... this works okay for tests which test *T_DB !=
9560 * NUL. It has the disadvantage that the user cannot use any :set t_*
9561 * command to get T_DB (back) to empty_option, only :set term=... will do
9562 * the trick...
9563 * Anyway, this hack will hopefully go away with the next OS release.
9564 * (Olaf Seibert)
9565 */
9566 else if (row == 0 && T_DB == empty_option
9567 && (line_count == 1 || *T_CDL == NUL))
9568#else
9569 else if (row == 0 && (
9570#ifndef AMIGA
9571 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9572 * up, so use delete-line command */
9573 line_count == 1 ||
9574#endif
9575 *T_CDL == NUL))
9576#endif
9577 type = USE_NL;
9578 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9579 type = USE_T_CDL;
9580 else if (can_clear(T_CE) && result_empty
9581#ifdef FEAT_VERTSPLIT
9582 && (wp == NULL || wp->w_width == Columns)
9583#endif
9584 )
9585 type = USE_T_CE;
9586 else if (*T_DL != NUL && can_delete)
9587 type = USE_T_DL;
9588 else if (*T_CDL != NUL && can_delete)
9589 type = USE_T_CDL;
9590 else
9591 return FAIL;
9592
9593#ifdef FEAT_CLIPBOARD
9594 /* Remove a modeless selection when deleting lines halfway the screen or
9595 * not the full width of the screen. */
9596 if (off + row > 0
9597# ifdef FEAT_VERTSPLIT
9598 || (wp != NULL && wp->w_width != Columns)
9599# endif
9600 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009601 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 else
9603 clip_scroll_selection(line_count);
9604#endif
9605
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606#ifdef FEAT_GUI
9607 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9608 * scrolling is actually carried out. */
9609 gui_dont_update_cursor();
9610#endif
9611
9612 if (*T_CCS != NUL) /* cursor relative to region */
9613 {
9614 cursor_row = row;
9615 cursor_end = end;
9616 }
9617 else
9618 {
9619 cursor_row = row + off;
9620 cursor_end = end + off;
9621 }
9622
9623 /*
9624 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9625 * Clear the inserted lines in ScreenLines[].
9626 */
9627 row += off;
9628 end += off;
9629 for (i = 0; i < line_count; ++i)
9630 {
9631#ifdef FEAT_VERTSPLIT
9632 if (wp != NULL && wp->w_width != Columns)
9633 {
9634 /* need to copy part of a line */
9635 j = row + i;
9636 while ((j += line_count) <= end - 1)
9637 linecopy(j - line_count, j, wp);
9638 j -= line_count;
9639 if (can_clear((char_u *)" "))
9640 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9641 else
9642 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9643 LineWraps[j] = FALSE;
9644 }
9645 else
9646#endif
9647 {
9648 /* whole width, moving the line pointers is faster */
9649 j = row + i;
9650 temp = LineOffset[j];
9651 while ((j += line_count) <= end - 1)
9652 {
9653 LineOffset[j - line_count] = LineOffset[j];
9654 LineWraps[j - line_count] = LineWraps[j];
9655 }
9656 LineOffset[j - line_count] = temp;
9657 LineWraps[j - line_count] = FALSE;
9658 if (can_clear((char_u *)" "))
9659 lineclear(temp, (int)Columns);
9660 else
9661 lineinvalid(temp, (int)Columns);
9662 }
9663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664
9665 screen_stop_highlight();
9666
9667#ifdef FEAT_VERTSPLIT
9668 /* redraw the characters */
9669 if (type == USE_REDRAW)
9670 redraw_block(row, end, wp);
9671 else
9672#endif
9673 if (type == USE_T_CD) /* delete the lines */
9674 {
9675 windgoto(cursor_row, 0);
9676 out_str(T_CD);
9677 screen_start(); /* don't know where cursor is now */
9678 }
9679 else if (type == USE_T_CDL)
9680 {
9681 windgoto(cursor_row, 0);
9682 term_delete_lines(line_count);
9683 screen_start(); /* don't know where cursor is now */
9684 }
9685 /*
9686 * Deleting lines at top of the screen or scroll region: Just scroll
9687 * the whole screen (scroll region) up by outputting newlines on the
9688 * last line.
9689 */
9690 else if (type == USE_NL)
9691 {
9692 windgoto(cursor_end - 1, 0);
9693 for (i = line_count; --i >= 0; )
9694 out_char('\n'); /* cursor will remain on same line */
9695 }
9696 else
9697 {
9698 for (i = line_count; --i >= 0; )
9699 {
9700 if (type == USE_T_DL)
9701 {
9702 windgoto(cursor_row, 0);
9703 out_str(T_DL); /* delete a line */
9704 }
9705 else /* type == USE_T_CE */
9706 {
9707 windgoto(cursor_row + i, 0);
9708 out_str(T_CE); /* erase a line */
9709 }
9710 screen_start(); /* don't know where cursor is now */
9711 }
9712 }
9713
9714 /*
9715 * If the 'db' flag is set, we need to clear the lines that have been
9716 * scrolled up at the bottom of the region.
9717 */
9718 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9719 {
9720 for (i = line_count; i > 0; --i)
9721 {
9722 windgoto(cursor_end - i, 0);
9723 out_str(T_CE); /* erase a line */
9724 screen_start(); /* don't know where cursor is now */
9725 }
9726 }
9727
9728#ifdef FEAT_GUI
9729 gui_can_update_cursor();
9730 if (gui.in_use)
9731 out_flush(); /* always flush after a scroll */
9732#endif
9733
9734 return OK;
9735}
9736
9737/*
9738 * show the current mode and ruler
9739 *
9740 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9741 * If clear_cmdline is FALSE there may be a message there that needs to be
9742 * cleared only if a mode is shown.
9743 * Return the length of the message (0 if no message).
9744 */
9745 int
9746showmode()
9747{
9748 int need_clear;
9749 int length = 0;
9750 int do_mode;
9751 int attr;
9752 int nwr_save;
9753#ifdef FEAT_INS_EXPAND
9754 int sub_attr;
9755#endif
9756
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009757 do_mode = ((p_smd && msg_silent == 0)
9758 && ((State & INSERT)
9759 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009760 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 if (do_mode || Recording)
9762 {
9763 /*
9764 * Don't show mode right now, when not redrawing or inside a mapping.
9765 * Call char_avail() only when we are going to show something, because
9766 * it takes a bit of time.
9767 */
9768 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9769 {
9770 redraw_cmdline = TRUE; /* show mode later */
9771 return 0;
9772 }
9773
9774 nwr_save = need_wait_return;
9775
9776 /* wait a bit before overwriting an important message */
9777 check_for_delay(FALSE);
9778
9779 /* if the cmdline is more than one line high, erase top lines */
9780 need_clear = clear_cmdline;
9781 if (clear_cmdline && cmdline_row < Rows - 1)
9782 msg_clr_cmdline(); /* will reset clear_cmdline */
9783
9784 /* Position on the last line in the window, column 0 */
9785 msg_pos_mode();
9786 cursor_off();
9787 attr = hl_attr(HLF_CM); /* Highlight mode */
9788 if (do_mode)
9789 {
9790 MSG_PUTS_ATTR("--", attr);
9791#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009792 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009793# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009794 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009795# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009796 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009797# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009798 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009799# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 MSG_PUTS_ATTR(" IM", attr);
9801# else
9802 MSG_PUTS_ATTR(" XIM", attr);
9803# endif
9804#endif
9805#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9806 if (gui.in_use)
9807 {
9808 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009809 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 }
9811#endif
9812#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009813 /* CTRL-X in Insert mode */
9814 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 {
9816 /* These messages can get long, avoid a wrap in a narrow
9817 * window. Prefer showing edit_submode_extra. */
9818 length = (Rows - msg_row) * Columns - 3;
9819 if (edit_submode_extra != NULL)
9820 length -= vim_strsize(edit_submode_extra);
9821 if (length > 0)
9822 {
9823 if (edit_submode_pre != NULL)
9824 length -= vim_strsize(edit_submode_pre);
9825 if (length - vim_strsize(edit_submode) > 0)
9826 {
9827 if (edit_submode_pre != NULL)
9828 msg_puts_attr(edit_submode_pre, attr);
9829 msg_puts_attr(edit_submode, attr);
9830 }
9831 if (edit_submode_extra != NULL)
9832 {
9833 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9834 if ((int)edit_submode_highl < (int)HLF_COUNT)
9835 sub_attr = hl_attr(edit_submode_highl);
9836 else
9837 sub_attr = attr;
9838 msg_puts_attr(edit_submode_extra, sub_attr);
9839 }
9840 }
9841 length = 0;
9842 }
9843 else
9844#endif
9845 {
9846#ifdef FEAT_VREPLACE
9847 if (State & VREPLACE_FLAG)
9848 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9849 else
9850#endif
9851 if (State & REPLACE_FLAG)
9852 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9853 else if (State & INSERT)
9854 {
9855#ifdef FEAT_RIGHTLEFT
9856 if (p_ri)
9857 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9858#endif
9859 MSG_PUTS_ATTR(_(" INSERT"), attr);
9860 }
9861 else if (restart_edit == 'I')
9862 MSG_PUTS_ATTR(_(" (insert)"), attr);
9863 else if (restart_edit == 'R')
9864 MSG_PUTS_ATTR(_(" (replace)"), attr);
9865 else if (restart_edit == 'V')
9866 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9867#ifdef FEAT_RIGHTLEFT
9868 if (p_hkmap)
9869 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9870# ifdef FEAT_FKMAP
9871 if (p_fkmap)
9872 MSG_PUTS_ATTR(farsi_text_5, attr);
9873# endif
9874#endif
9875#ifdef FEAT_KEYMAP
9876 if (State & LANGMAP)
9877 {
9878# ifdef FEAT_ARABIC
9879 if (curwin->w_p_arab)
9880 MSG_PUTS_ATTR(_(" Arabic"), attr);
9881 else
9882# endif
9883 MSG_PUTS_ATTR(_(" (lang)"), attr);
9884 }
9885#endif
9886 if ((State & INSERT) && p_paste)
9887 MSG_PUTS_ATTR(_(" (paste)"), attr);
9888
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (VIsual_active)
9890 {
9891 char *p;
9892
9893 /* Don't concatenate separate words to avoid translation
9894 * problems. */
9895 switch ((VIsual_select ? 4 : 0)
9896 + (VIsual_mode == Ctrl_V) * 2
9897 + (VIsual_mode == 'V'))
9898 {
9899 case 0: p = N_(" VISUAL"); break;
9900 case 1: p = N_(" VISUAL LINE"); break;
9901 case 2: p = N_(" VISUAL BLOCK"); break;
9902 case 4: p = N_(" SELECT"); break;
9903 case 5: p = N_(" SELECT LINE"); break;
9904 default: p = N_(" SELECT BLOCK"); break;
9905 }
9906 MSG_PUTS_ATTR(_(p), attr);
9907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 MSG_PUTS_ATTR(" --", attr);
9909 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009910
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 need_clear = TRUE;
9912 }
9913 if (Recording
9914#ifdef FEAT_INS_EXPAND
9915 && edit_submode == NULL /* otherwise it gets too long */
9916#endif
9917 )
9918 {
9919 MSG_PUTS_ATTR(_("recording"), attr);
9920 need_clear = TRUE;
9921 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009922
9923 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 if (need_clear || clear_cmdline)
9925 msg_clr_eos();
9926 msg_didout = FALSE; /* overwrite this message */
9927 length = msg_col;
9928 msg_col = 0;
9929 need_wait_return = nwr_save; /* never ask for hit-return for this */
9930 }
9931 else if (clear_cmdline && msg_silent == 0)
9932 /* Clear the whole command line. Will reset "clear_cmdline". */
9933 msg_clr_cmdline();
9934
9935#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 /* In Visual mode the size of the selected area must be redrawn. */
9937 if (VIsual_active)
9938 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
9940 /* If the last window has no status line, the ruler is after the mode
9941 * message and must be redrawn */
9942 if (redrawing()
9943# ifdef FEAT_WINDOWS
9944 && lastwin->w_status_height == 0
9945# endif
9946 )
9947 win_redr_ruler(lastwin, TRUE);
9948#endif
9949 redraw_cmdline = FALSE;
9950 clear_cmdline = FALSE;
9951
9952 return length;
9953}
9954
9955/*
9956 * Position for a mode message.
9957 */
9958 static void
9959msg_pos_mode()
9960{
9961 msg_col = 0;
9962 msg_row = Rows - 1;
9963}
9964
9965/*
9966 * Delete mode message. Used when ESC is typed which is expected to end
9967 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009968 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 */
9970 void
9971unshowmode(force)
9972 int force;
9973{
9974 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009975 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 */
9977 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9978 redraw_cmdline = TRUE; /* delete mode later */
9979 else
9980 {
9981 msg_pos_mode();
9982 if (Recording)
9983 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9984 msg_clr_eos();
9985 }
9986}
9987
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009988#if defined(FEAT_WINDOWS)
9989/*
9990 * Draw the tab pages line at the top of the Vim window.
9991 */
9992 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009993draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009994{
9995 int tabcount = 0;
9996 tabpage_T *tp;
9997 int tabwidth;
9998 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009999 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010000 int attr;
10001 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010002 win_T *cwp;
10003 int wincount;
10004 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010005 int c;
10006 int len;
10007 int attr_sel = hl_attr(HLF_TPS);
10008 int attr_nosel = hl_attr(HLF_TP);
10009 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010010 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010011 int room;
10012 int use_sep_chars = (t_colors < 8
10013#ifdef FEAT_GUI
10014 && !gui.in_use
10015#endif
10016 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010017
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010018 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010019
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010020#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010021 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010022 if (gui_use_tabline())
10023 {
10024 gui_update_tabline();
10025 return;
10026 }
10027#endif
10028
10029 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010030 return;
10031
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010032#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010033
10034 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10035 for (scol = 0; scol < Columns; ++scol)
10036 TabPageIdxs[scol] = 0;
10037
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010038 /* Use the 'tabline' option if it's set. */
10039 if (*p_tal != NUL)
10040 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010041 int save_called_emsg = called_emsg;
10042
10043 /* Check for an error. If there is one we would loop in redrawing the
10044 * screen. Avoid that by making 'tabline' empty. */
10045 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010046 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010047 if (called_emsg)
10048 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010049 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010050 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010051 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010052 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010053#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010054 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010055 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10056 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010057
Bram Moolenaar238a5642006-02-21 22:12:05 +000010058 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10059 if (tabwidth < 6)
10060 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010061
Bram Moolenaar238a5642006-02-21 22:12:05 +000010062 attr = attr_nosel;
10063 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010064 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010065 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10066 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010067 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010068 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010069
Bram Moolenaar238a5642006-02-21 22:12:05 +000010070 if (tp->tp_topframe == topframe)
10071 attr = attr_sel;
10072 if (use_sep_chars && col > 0)
10073 screen_putchar('|', 0, col++, attr);
10074
10075 if (tp->tp_topframe != topframe)
10076 attr = attr_nosel;
10077
10078 screen_putchar(' ', 0, col++, attr);
10079
10080 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010081 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010082 cwp = curwin;
10083 wp = firstwin;
10084 }
10085 else
10086 {
10087 cwp = tp->tp_curwin;
10088 wp = tp->tp_firstwin;
10089 }
10090
10091 modified = FALSE;
10092 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10093 if (bufIsChanged(wp->w_buffer))
10094 modified = TRUE;
10095 if (modified || wincount > 1)
10096 {
10097 if (wincount > 1)
10098 {
10099 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010100 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010101 if (col + len >= Columns - 3)
10102 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010103 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010104#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010105 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010106#else
Bram Moolenaar238a5642006-02-21 22:12:05 +000010107 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010108#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010109 );
10110 col += len;
10111 }
10112 if (modified)
10113 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10114 screen_putchar(' ', 0, col++, attr);
10115 }
10116
10117 room = scol - col + tabwidth - 1;
10118 if (room > 0)
10119 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010120 /* Get buffer name in NameBuff[] */
10121 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010122 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010123 len = vim_strsize(NameBuff);
10124 p = NameBuff;
10125#ifdef FEAT_MBYTE
10126 if (has_mbyte)
10127 while (len > room)
10128 {
10129 len -= ptr2cells(p);
10130 mb_ptr_adv(p);
10131 }
10132 else
10133#endif
10134 if (len > room)
10135 {
10136 p += len - room;
10137 len = room;
10138 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010139 if (len > Columns - col - 1)
10140 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010142 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010143 col += len;
10144 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010145 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010146
10147 /* Store the tab page number in TabPageIdxs[], so that
10148 * jump_to_mouse() knows where each one is. */
10149 ++tabcount;
10150 while (scol < col)
10151 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010152 }
10153
Bram Moolenaar238a5642006-02-21 22:12:05 +000010154 if (use_sep_chars)
10155 c = '_';
10156 else
10157 c = ' ';
10158 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010159
10160 /* Put an "X" for closing the current tab if there are several. */
10161 if (first_tabpage->tp_next != NULL)
10162 {
10163 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10164 TabPageIdxs[Columns - 1] = -999;
10165 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010166 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010167
10168 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10169 * set. */
10170 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010171}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010172
10173/*
10174 * Get buffer name for "buf" into NameBuff[].
10175 * Takes care of special buffer names and translates special characters.
10176 */
10177 void
10178get_trans_bufname(buf)
10179 buf_T *buf;
10180{
10181 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010182 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010183 else
10184 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10185 trans_characters(NameBuff, MAXPATHL);
10186}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010187#endif
10188
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10190/*
10191 * Get the character to use in a status line. Get its attributes in "*attr".
10192 */
10193 static int
10194fillchar_status(attr, is_curwin)
10195 int *attr;
10196 int is_curwin;
10197{
10198 int fill;
10199 if (is_curwin)
10200 {
10201 *attr = hl_attr(HLF_S);
10202 fill = fill_stl;
10203 }
10204 else
10205 {
10206 *attr = hl_attr(HLF_SNC);
10207 fill = fill_stlnc;
10208 }
10209 /* Use fill when there is highlighting, and highlighting of current
10210 * window differs, or the fillchars differ, or this is not the
10211 * current window */
10212 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10213 || !is_curwin || firstwin == lastwin)
10214 || (fill_stl != fill_stlnc)))
10215 return fill;
10216 if (is_curwin)
10217 return '^';
10218 return '=';
10219}
10220#endif
10221
10222#ifdef FEAT_VERTSPLIT
10223/*
10224 * Get the character to use in a separator between vertically split windows.
10225 * Get its attributes in "*attr".
10226 */
10227 static int
10228fillchar_vsep(attr)
10229 int *attr;
10230{
10231 *attr = hl_attr(HLF_C);
10232 if (*attr == 0 && fill_vert == ' ')
10233 return '|';
10234 else
10235 return fill_vert;
10236}
10237#endif
10238
10239/*
10240 * Return TRUE if redrawing should currently be done.
10241 */
10242 int
10243redrawing()
10244{
10245 return (!RedrawingDisabled
10246 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10247}
10248
10249/*
10250 * Return TRUE if printing messages should currently be done.
10251 */
10252 int
10253messaging()
10254{
10255 return (!(p_lz && char_avail() && !KeyTyped));
10256}
10257
10258/*
10259 * Show current status info in ruler and various other places
10260 * If always is FALSE, only show ruler if position has changed.
10261 */
10262 void
10263showruler(always)
10264 int always;
10265{
10266 if (!always && !redrawing())
10267 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010268#ifdef FEAT_INS_EXPAND
10269 if (pum_visible())
10270 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010271# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010272 /* Don't redraw right now, do it later. */
10273 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010274# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010275 return;
10276 }
10277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010279 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010280 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010281 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 else
10284#endif
10285#ifdef FEAT_CMDL_INFO
10286 win_redr_ruler(curwin, always);
10287#endif
10288
10289#ifdef FEAT_TITLE
10290 if (need_maketitle
10291# ifdef FEAT_STL_OPT
10292 || (p_icon && (stl_syntax & STL_IN_ICON))
10293 || (p_title && (stl_syntax & STL_IN_TITLE))
10294# endif
10295 )
10296 maketitle();
10297#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010298#ifdef FEAT_WINDOWS
10299 /* Redraw the tab pages line if needed. */
10300 if (redraw_tabline)
10301 draw_tabline();
10302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303}
10304
10305#ifdef FEAT_CMDL_INFO
10306 static void
10307win_redr_ruler(wp, always)
10308 win_T *wp;
10309 int always;
10310{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010311#define RULER_BUF_LEN 70
10312 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 int row;
10314 int fillchar;
10315 int attr;
10316 int empty_line = FALSE;
10317 colnr_T virtcol;
10318 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010319 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 int o;
10321#ifdef FEAT_VERTSPLIT
10322 int this_ru_col;
10323 int off = 0;
10324 int width = Columns;
10325# define WITH_OFF(x) x
10326# define WITH_WIDTH(x) x
10327#else
10328# define WITH_OFF(x) 0
10329# define WITH_WIDTH(x) Columns
10330# define this_ru_col ru_col
10331#endif
10332
10333 /* If 'ruler' off or redrawing disabled, don't do anything */
10334 if (!p_ru)
10335 return;
10336
10337 /*
10338 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10339 * after deleting lines, before cursor.lnum is corrected.
10340 */
10341 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10342 return;
10343
10344#ifdef FEAT_INS_EXPAND
10345 /* Don't draw the ruler while doing insert-completion, it might overwrite
10346 * the (long) mode message. */
10347# ifdef FEAT_WINDOWS
10348 if (wp == lastwin && lastwin->w_status_height == 0)
10349# endif
10350 if (edit_submode != NULL)
10351 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010352 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10353 if (pum_visible())
10354 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355#endif
10356
10357#ifdef FEAT_STL_OPT
10358 if (*p_ruf)
10359 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010360 int save_called_emsg = called_emsg;
10361
10362 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010364 if (called_emsg)
10365 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010366 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010367 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 return;
10369 }
10370#endif
10371
10372 /*
10373 * Check if not in Insert mode and the line is empty (will show "0-1").
10374 */
10375 if (!(State & INSERT)
10376 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10377 empty_line = TRUE;
10378
10379 /*
10380 * Only draw the ruler when something changed.
10381 */
10382 validate_virtcol_win(wp);
10383 if ( redraw_cmdline
10384 || always
10385 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10386 || wp->w_cursor.col != wp->w_ru_cursor.col
10387 || wp->w_virtcol != wp->w_ru_virtcol
10388#ifdef FEAT_VIRTUALEDIT
10389 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10390#endif
10391 || wp->w_topline != wp->w_ru_topline
10392 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10393#ifdef FEAT_DIFF
10394 || wp->w_topfill != wp->w_ru_topfill
10395#endif
10396 || empty_line != wp->w_ru_empty)
10397 {
10398 cursor_off();
10399#ifdef FEAT_WINDOWS
10400 if (wp->w_status_height)
10401 {
10402 row = W_WINROW(wp) + wp->w_height;
10403 fillchar = fillchar_status(&attr, wp == curwin);
10404# ifdef FEAT_VERTSPLIT
10405 off = W_WINCOL(wp);
10406 width = W_WIDTH(wp);
10407# endif
10408 }
10409 else
10410#endif
10411 {
10412 row = Rows - 1;
10413 fillchar = ' ';
10414 attr = 0;
10415#ifdef FEAT_VERTSPLIT
10416 width = Columns;
10417 off = 0;
10418#endif
10419 }
10420
10421 /* In list mode virtcol needs to be recomputed */
10422 virtcol = wp->w_virtcol;
10423 if (wp->w_p_list && lcs_tab1 == NUL)
10424 {
10425 wp->w_p_list = FALSE;
10426 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10427 wp->w_p_list = TRUE;
10428 }
10429
10430 /*
10431 * Some sprintfs return the length, some return a pointer.
10432 * To avoid portability problems we use strlen() here.
10433 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010434 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10436 ? 0L
10437 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010438 len = STRLEN(buffer);
10439 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10441 (int)virtcol + 1);
10442
10443 /*
10444 * Add a "50%" if there is room for it.
10445 * On the last line, don't print in the last column (scrolls the
10446 * screen up on some terminals).
10447 */
10448 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010449 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 o = i + vim_strsize(buffer + i + 1);
10451#ifdef FEAT_WINDOWS
10452 if (wp->w_status_height == 0) /* can't use last char of screen */
10453#endif
10454 ++o;
10455#ifdef FEAT_VERTSPLIT
10456 this_ru_col = ru_col - (Columns - width);
10457 if (this_ru_col < 0)
10458 this_ru_col = 0;
10459#endif
10460 /* Never use more than half the window/screen width, leave the other
10461 * half for the filename. */
10462 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10463 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10464 if (this_ru_col + o < WITH_WIDTH(width))
10465 {
10466 while (this_ru_col + o < WITH_WIDTH(width))
10467 {
10468#ifdef FEAT_MBYTE
10469 if (has_mbyte)
10470 i += (*mb_char2bytes)(fillchar, buffer + i);
10471 else
10472#endif
10473 buffer[i++] = fillchar;
10474 ++o;
10475 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010476 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 }
10478 /* Truncate at window boundary. */
10479#ifdef FEAT_MBYTE
10480 if (has_mbyte)
10481 {
10482 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010483 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 {
10485 o += (*mb_ptr2cells)(buffer + i);
10486 if (this_ru_col + o > WITH_WIDTH(width))
10487 {
10488 buffer[i] = NUL;
10489 break;
10490 }
10491 }
10492 }
10493 else
10494#endif
10495 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10496 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10497
10498 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10499 i = redraw_cmdline;
10500 screen_fill(row, row + 1,
10501 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10502 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10503 fillchar, fillchar, attr);
10504 /* don't redraw the cmdline because of showing the ruler */
10505 redraw_cmdline = i;
10506 wp->w_ru_cursor = wp->w_cursor;
10507 wp->w_ru_virtcol = wp->w_virtcol;
10508 wp->w_ru_empty = empty_line;
10509 wp->w_ru_topline = wp->w_topline;
10510 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10511#ifdef FEAT_DIFF
10512 wp->w_ru_topfill = wp->w_topfill;
10513#endif
10514 }
10515}
10516#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010517
10518#if defined(FEAT_LINEBREAK) || defined(PROTO)
10519/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010520 * Return the width of the 'number' and 'relativenumber' column.
10521 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010522 * Otherwise it depends on 'numberwidth' and the line count.
10523 */
10524 int
10525number_width(wp)
10526 win_T *wp;
10527{
10528 int n;
10529 linenr_T lnum;
10530
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010531 if (wp->w_p_rnu && !wp->w_p_nu)
10532 /* cursor line shows "0" */
10533 lnum = wp->w_height;
10534 else
10535 /* cursor line shows absolute line number */
10536 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010537
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010538 if (lnum == wp->w_nrwidth_line_count)
10539 return wp->w_nrwidth_width;
10540 wp->w_nrwidth_line_count = lnum;
10541
10542 n = 0;
10543 do
10544 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010545 lnum /= 10;
10546 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010547 } while (lnum > 0);
10548
10549 /* 'numberwidth' gives the minimal width plus one */
10550 if (n < wp->w_p_nuw - 1)
10551 n = wp->w_p_nuw - 1;
10552
10553 wp->w_nrwidth_width = n;
10554 return n;
10555}
10556#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010557
10558/*
10559 * Return the current cursor column. This is the actual position on the
10560 * screen. First column is 0.
10561 */
10562 int
10563screen_screencol()
10564{
10565 return screen_cur_col;
10566}
10567
10568/*
10569 * Return the current cursor row. This is the actual position on the screen.
10570 * First row is 0.
10571 */
10572 int
10573screen_screenrow()
10574{
10575 return screen_cur_row;
10576}