blob: 65aadc4235205f66f9743837e11f452d4fd8f86e [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
Bram Moolenaardab70c62014-07-02 17:16:58 +02001772 /* match in fixed position might need redraw
1773 * if lines were inserted or deleted */
1774 || (wp->w_match_head != NULL
1775 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 )))))
1778 {
1779#ifdef FEAT_SEARCH_EXTRA
1780 if (lnum == mod_top)
1781 top_to_mod = FALSE;
1782#endif
1783
1784 /*
1785 * When at start of changed lines: May scroll following lines
1786 * up or down to minimize redrawing.
1787 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001788 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 */
1790 if (lnum == mod_top
1791 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001792 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
1794 int old_rows = 0;
1795 int new_rows = 0;
1796 int xtra_rows;
1797 linenr_T l;
1798
1799 /* Count the old number of window rows, using w_lines[], which
1800 * should still contain the sizes for the lines as they are
1801 * currently displayed. */
1802 for (i = idx; i < wp->w_lines_valid; ++i)
1803 {
1804 /* Only valid lines have a meaningful wl_lnum. Invalid
1805 * lines are part of the changed area. */
1806 if (wp->w_lines[i].wl_valid
1807 && wp->w_lines[i].wl_lnum == mod_bot)
1808 break;
1809 old_rows += wp->w_lines[i].wl_size;
1810#ifdef FEAT_FOLDING
1811 if (wp->w_lines[i].wl_valid
1812 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1813 {
1814 /* Must have found the last valid entry above mod_bot.
1815 * Add following invalid entries. */
1816 ++i;
1817 while (i < wp->w_lines_valid
1818 && !wp->w_lines[i].wl_valid)
1819 old_rows += wp->w_lines[i++].wl_size;
1820 break;
1821 }
1822#endif
1823 }
1824
1825 if (i >= wp->w_lines_valid)
1826 {
1827 /* We can't find a valid line below the changed lines,
1828 * need to redraw until the end of the window.
1829 * Inserting/deleting lines has no use. */
1830 bot_start = 0;
1831 }
1832 else
1833 {
1834 /* Able to count old number of rows: Count new window
1835 * rows, and may insert/delete lines */
1836 j = idx;
1837 for (l = lnum; l < mod_bot; ++l)
1838 {
1839#ifdef FEAT_FOLDING
1840 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1841 ++new_rows;
1842 else
1843#endif
1844#ifdef FEAT_DIFF
1845 if (l == wp->w_topline)
1846 new_rows += plines_win_nofill(wp, l, TRUE)
1847 + wp->w_topfill;
1848 else
1849#endif
1850 new_rows += plines_win(wp, l, TRUE);
1851 ++j;
1852 if (new_rows > wp->w_height - row - 2)
1853 {
1854 /* it's getting too much, must redraw the rest */
1855 new_rows = 9999;
1856 break;
1857 }
1858 }
1859 xtra_rows = new_rows - old_rows;
1860 if (xtra_rows < 0)
1861 {
1862 /* May scroll text up. If there is not enough
1863 * remaining text or scrolling fails, must redraw the
1864 * rest. If scrolling works, must redraw the text
1865 * below the scrolled text. */
1866 if (row - xtra_rows >= wp->w_height - 2)
1867 mod_bot = MAXLNUM;
1868 else
1869 {
1870 check_for_delay(FALSE);
1871 if (win_del_lines(wp, row,
1872 -xtra_rows, FALSE, FALSE) == FAIL)
1873 mod_bot = MAXLNUM;
1874 else
1875 bot_start = wp->w_height + xtra_rows;
1876 }
1877 }
1878 else if (xtra_rows > 0)
1879 {
1880 /* May scroll text down. If there is not enough
1881 * remaining text of scrolling fails, must redraw the
1882 * rest. */
1883 if (row + xtra_rows >= wp->w_height - 2)
1884 mod_bot = MAXLNUM;
1885 else
1886 {
1887 check_for_delay(FALSE);
1888 if (win_ins_lines(wp, row + old_rows,
1889 xtra_rows, FALSE, FALSE) == FAIL)
1890 mod_bot = MAXLNUM;
1891 else if (top_end > row + old_rows)
1892 /* Scrolled the part at the top that requires
1893 * updating down. */
1894 top_end += xtra_rows;
1895 }
1896 }
1897
1898 /* When not updating the rest, may need to move w_lines[]
1899 * entries. */
1900 if (mod_bot != MAXLNUM && i != j)
1901 {
1902 if (j < i)
1903 {
1904 int x = row + new_rows;
1905
1906 /* move entries in w_lines[] upwards */
1907 for (;;)
1908 {
1909 /* stop at last valid entry in w_lines[] */
1910 if (i >= wp->w_lines_valid)
1911 {
1912 wp->w_lines_valid = j;
1913 break;
1914 }
1915 wp->w_lines[j] = wp->w_lines[i];
1916 /* stop at a line that won't fit */
1917 if (x + (int)wp->w_lines[j].wl_size
1918 > wp->w_height)
1919 {
1920 wp->w_lines_valid = j + 1;
1921 break;
1922 }
1923 x += wp->w_lines[j++].wl_size;
1924 ++i;
1925 }
1926 if (bot_start > x)
1927 bot_start = x;
1928 }
1929 else /* j > i */
1930 {
1931 /* move entries in w_lines[] downwards */
1932 j -= i;
1933 wp->w_lines_valid += j;
1934 if (wp->w_lines_valid > wp->w_height)
1935 wp->w_lines_valid = wp->w_height;
1936 for (i = wp->w_lines_valid; i - j >= idx; --i)
1937 wp->w_lines[i] = wp->w_lines[i - j];
1938
1939 /* The w_lines[] entries for inserted lines are
1940 * now invalid, but wl_size may be used above.
1941 * Reset to zero. */
1942 while (i >= idx)
1943 {
1944 wp->w_lines[i].wl_size = 0;
1945 wp->w_lines[i--].wl_valid = FALSE;
1946 }
1947 }
1948 }
1949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
1951
1952#ifdef FEAT_FOLDING
1953 /*
1954 * When lines are folded, display one line for all of them.
1955 * Otherwise, display normally (can be several display lines when
1956 * 'wrap' is on).
1957 */
1958 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1959 if (fold_count != 0)
1960 {
1961 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1962 ++row;
1963 --fold_count;
1964 wp->w_lines[idx].wl_folded = TRUE;
1965 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1966# ifdef FEAT_SYN_HL
1967 did_update = DID_FOLD;
1968# endif
1969 }
1970 else
1971#endif
1972 if (idx < wp->w_lines_valid
1973 && wp->w_lines[idx].wl_valid
1974 && wp->w_lines[idx].wl_lnum == lnum
1975 && lnum > wp->w_topline
1976 && !(dy_flags & DY_LASTLINE)
1977 && srow + wp->w_lines[idx].wl_size > wp->w_height
1978#ifdef FEAT_DIFF
1979 && diff_check_fill(wp, lnum) == 0
1980#endif
1981 )
1982 {
1983 /* This line is not going to fit. Don't draw anything here,
1984 * will draw "@ " lines below. */
1985 row = wp->w_height + 1;
1986 }
1987 else
1988 {
1989#ifdef FEAT_SEARCH_EXTRA
1990 prepare_search_hl(wp, lnum);
1991#endif
1992#ifdef FEAT_SYN_HL
1993 /* Let the syntax stuff know we skipped a few lines. */
1994 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001995 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 syntax_end_parsing(syntax_last_parsed + 1);
1997#endif
1998
1999 /*
2000 * Display one line.
2001 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002002 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
2004#ifdef FEAT_FOLDING
2005 wp->w_lines[idx].wl_folded = FALSE;
2006 wp->w_lines[idx].wl_lastlnum = lnum;
2007#endif
2008#ifdef FEAT_SYN_HL
2009 did_update = DID_LINE;
2010 syntax_last_parsed = lnum;
2011#endif
2012 }
2013
2014 wp->w_lines[idx].wl_lnum = lnum;
2015 wp->w_lines[idx].wl_valid = TRUE;
2016 if (row > wp->w_height) /* past end of screen */
2017 {
2018 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002019 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2021 ++idx;
2022 break;
2023 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002024 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 wp->w_lines[idx].wl_size = row - srow;
2026 ++idx;
2027#ifdef FEAT_FOLDING
2028 lnum += fold_count + 1;
2029#else
2030 ++lnum;
2031#endif
2032 }
2033 else
2034 {
2035 /* This line does not need updating, advance to the next one */
2036 row += wp->w_lines[idx++].wl_size;
2037 if (row > wp->w_height) /* past end of screen */
2038 break;
2039#ifdef FEAT_FOLDING
2040 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2041#else
2042 ++lnum;
2043#endif
2044#ifdef FEAT_SYN_HL
2045 did_update = DID_NONE;
2046#endif
2047 }
2048
2049 if (lnum > buf->b_ml.ml_line_count)
2050 {
2051 eof = TRUE;
2052 break;
2053 }
2054 }
2055 /*
2056 * End of loop over all window lines.
2057 */
2058
2059
2060 if (idx > wp->w_lines_valid)
2061 wp->w_lines_valid = idx;
2062
2063#ifdef FEAT_SYN_HL
2064 /*
2065 * Let the syntax stuff know we stop parsing here.
2066 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002067 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 syntax_end_parsing(syntax_last_parsed + 1);
2069#endif
2070
2071 /*
2072 * If we didn't hit the end of the file, and we didn't finish the last
2073 * line we were working on, then the line didn't fit.
2074 */
2075 wp->w_empty_rows = 0;
2076#ifdef FEAT_DIFF
2077 wp->w_filler_rows = 0;
2078#endif
2079 if (!eof && !didline)
2080 {
2081 if (lnum == wp->w_topline)
2082 {
2083 /*
2084 * Single line that does not fit!
2085 * Don't overwrite it, it can be edited.
2086 */
2087 wp->w_botline = lnum + 1;
2088 }
2089#ifdef FEAT_DIFF
2090 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2091 {
2092 /* Window ends in filler lines. */
2093 wp->w_botline = lnum;
2094 wp->w_filler_rows = wp->w_height - srow;
2095 }
2096#endif
2097 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2098 {
2099 /*
2100 * Last line isn't finished: Display "@@@" at the end.
2101 */
2102 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2103 W_WINROW(wp) + wp->w_height,
2104 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2105 '@', '@', hl_attr(HLF_AT));
2106 set_empty_rows(wp, srow);
2107 wp->w_botline = lnum;
2108 }
2109 else
2110 {
2111 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2112 wp->w_botline = lnum;
2113 }
2114 }
2115 else
2116 {
2117#ifdef FEAT_VERTSPLIT
2118 draw_vsep_win(wp, row);
2119#endif
2120 if (eof) /* we hit the end of the file */
2121 {
2122 wp->w_botline = buf->b_ml.ml_line_count + 1;
2123#ifdef FEAT_DIFF
2124 j = diff_check_fill(wp, wp->w_botline);
2125 if (j > 0 && !wp->w_botfill)
2126 {
2127 /*
2128 * Display filler lines at the end of the file
2129 */
2130 if (char2cells(fill_diff) > 1)
2131 i = '-';
2132 else
2133 i = fill_diff;
2134 if (row + j > wp->w_height)
2135 j = wp->w_height - row;
2136 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2137 row += j;
2138 }
2139#endif
2140 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002141 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 wp->w_botline = lnum;
2143
2144 /* make sure the rest of the screen is blank */
2145 /* put '~'s on rows that aren't part of the file. */
2146 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2147 }
2148
2149 /* Reset the type of redrawing required, the window has been updated. */
2150 wp->w_redr_type = 0;
2151#ifdef FEAT_DIFF
2152 wp->w_old_topfill = wp->w_topfill;
2153 wp->w_old_botfill = wp->w_botfill;
2154#endif
2155
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002156 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 /*
2159 * There is a trick with w_botline. If we invalidate it on each
2160 * change that might modify it, this will cause a lot of expensive
2161 * calls to plines() in update_topline() each time. Therefore the
2162 * value of w_botline is often approximated, and this value is used to
2163 * compute the value of w_topline. If the value of w_botline was
2164 * wrong, check that the value of w_topline is correct (cursor is on
2165 * the visible part of the text). If it's not, we need to redraw
2166 * again. Mostly this just means scrolling up a few lines, so it
2167 * doesn't look too bad. Only do this for the current window (where
2168 * changes are relevant).
2169 */
2170 wp->w_valid |= VALID_BOTLINE;
2171 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2172 {
2173 recursive = TRUE;
2174 curwin->w_valid &= ~VALID_TOPLINE;
2175 update_topline(); /* may invalidate w_botline again */
2176 if (must_redraw != 0)
2177 {
2178 /* Don't update for changes in buffer again. */
2179 i = curbuf->b_mod_set;
2180 curbuf->b_mod_set = FALSE;
2181 win_update(curwin);
2182 must_redraw = 0;
2183 curbuf->b_mod_set = i;
2184 }
2185 recursive = FALSE;
2186 }
2187 }
2188
2189#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2190 /* restore got_int, unless CTRL-C was hit while redrawing */
2191 if (!got_int)
2192 got_int = save_got_int;
2193#endif
2194}
2195
2196#ifdef FEAT_SIGNS
2197static int draw_signcolumn __ARGS((win_T *wp));
2198
2199/*
2200 * Return TRUE when window "wp" has a column to draw signs in.
2201 */
2202 static int
2203draw_signcolumn(wp)
2204 win_T *wp;
2205{
2206 return (wp->w_buffer->b_signlist != NULL
2207# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002208 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209# endif
2210 );
2211}
2212#endif
2213
2214/*
2215 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2216 * as the filler character.
2217 */
2218 static void
2219win_draw_end(wp, c1, c2, row, endrow, hl)
2220 win_T *wp;
2221 int c1;
2222 int c2;
2223 int row;
2224 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002225 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2228 int n = 0;
2229# define FDC_OFF n
2230#else
2231# define FDC_OFF 0
2232#endif
2233
2234#ifdef FEAT_RIGHTLEFT
2235 if (wp->w_p_rl)
2236 {
2237 /* No check for cmdline window: should never be right-left. */
2238# ifdef FEAT_FOLDING
2239 n = wp->w_p_fdc;
2240
2241 if (n > 0)
2242 {
2243 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002244 if (n > W_WIDTH(wp))
2245 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2247 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2248 ' ', ' ', hl_attr(HLF_FC));
2249 }
2250# endif
2251# ifdef FEAT_SIGNS
2252 if (draw_signcolumn(wp))
2253 {
2254 int nn = n + 2;
2255
2256 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002257 if (nn > W_WIDTH(wp))
2258 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2260 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2261 ' ', ' ', hl_attr(HLF_SC));
2262 n = nn;
2263 }
2264# endif
2265 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2266 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2267 c2, c2, hl_attr(hl));
2268 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2269 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2270 c1, c2, hl_attr(hl));
2271 }
2272 else
2273#endif
2274 {
2275#ifdef FEAT_CMDWIN
2276 if (cmdwin_type != 0 && wp == curwin)
2277 {
2278 /* draw the cmdline character in the leftmost column */
2279 n = 1;
2280 if (n > wp->w_width)
2281 n = wp->w_width;
2282 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2283 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2284 cmdwin_type, ' ', hl_attr(HLF_AT));
2285 }
2286#endif
2287#ifdef FEAT_FOLDING
2288 if (wp->w_p_fdc > 0)
2289 {
2290 int nn = n + wp->w_p_fdc;
2291
2292 /* draw the fold column at the left */
2293 if (nn > W_WIDTH(wp))
2294 nn = W_WIDTH(wp);
2295 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2296 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2297 ' ', ' ', hl_attr(HLF_FC));
2298 n = nn;
2299 }
2300#endif
2301#ifdef FEAT_SIGNS
2302 if (draw_signcolumn(wp))
2303 {
2304 int nn = n + 2;
2305
2306 /* draw the sign column after the fold column */
2307 if (nn > W_WIDTH(wp))
2308 nn = W_WIDTH(wp);
2309 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2310 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2311 ' ', ' ', hl_attr(HLF_SC));
2312 n = nn;
2313 }
2314#endif
2315 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2316 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2317 c1, c2, hl_attr(hl));
2318 }
2319 set_empty_rows(wp, row);
2320}
2321
Bram Moolenaar1a384422010-07-14 19:53:30 +02002322#ifdef FEAT_SYN_HL
2323static int advance_color_col __ARGS((int vcol, int **color_cols));
2324
2325/*
2326 * Advance **color_cols and return TRUE when there are columns to draw.
2327 */
2328 static int
2329advance_color_col(vcol, color_cols)
2330 int vcol;
2331 int **color_cols;
2332{
2333 while (**color_cols >= 0 && vcol > **color_cols)
2334 ++*color_cols;
2335 return (**color_cols >= 0);
2336}
2337#endif
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339#ifdef FEAT_FOLDING
2340/*
2341 * Display one folded line.
2342 */
2343 static void
2344fold_line(wp, fold_count, foldinfo, lnum, row)
2345 win_T *wp;
2346 long fold_count;
2347 foldinfo_T *foldinfo;
2348 linenr_T lnum;
2349 int row;
2350{
2351 char_u buf[51];
2352 pos_T *top, *bot;
2353 linenr_T lnume = lnum + fold_count - 1;
2354 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002355 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int col;
2358 int txtcol;
2359 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002360 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 /* Build the fold line:
2363 * 1. Add the cmdwin_type for the command-line window
2364 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002365 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 * 4. Compose the text
2367 * 5. Add the text
2368 * 6. set highlighting for the Visual area an other text
2369 */
2370 col = 0;
2371
2372 /*
2373 * 1. Add the cmdwin_type for the command-line window
2374 * Ignores 'rightleft', this window is never right-left.
2375 */
2376#ifdef FEAT_CMDWIN
2377 if (cmdwin_type != 0 && wp == curwin)
2378 {
2379 ScreenLines[off] = cmdwin_type;
2380 ScreenAttrs[off] = hl_attr(HLF_AT);
2381#ifdef FEAT_MBYTE
2382 if (enc_utf8)
2383 ScreenLinesUC[off] = 0;
2384#endif
2385 ++col;
2386 }
2387#endif
2388
2389 /*
2390 * 2. Add the 'foldcolumn'
2391 */
2392 fdc = wp->w_p_fdc;
2393 if (fdc > W_WIDTH(wp) - col)
2394 fdc = W_WIDTH(wp) - col;
2395 if (fdc > 0)
2396 {
2397 fill_foldcolumn(buf, wp, TRUE, lnum);
2398#ifdef FEAT_RIGHTLEFT
2399 if (wp->w_p_rl)
2400 {
2401 int i;
2402
2403 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2404 hl_attr(HLF_FC));
2405 /* reverse the fold column */
2406 for (i = 0; i < fdc; ++i)
2407 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2408 }
2409 else
2410#endif
2411 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2412 col += fdc;
2413 }
2414
2415#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002416# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2417 for (ri = 0; ri < l; ++ri) \
2418 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2419 else \
2420 for (ri = 0; ri < l; ++ri) \
2421 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002423# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2424 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#endif
2426
Bram Moolenaar64486672010-05-16 15:46:46 +02002427 /* Set all attributes of the 'number' or 'relativenumber' column and the
2428 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002429 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431#ifdef FEAT_SIGNS
2432 /* If signs are being displayed, add two spaces. */
2433 if (draw_signcolumn(wp))
2434 {
2435 len = W_WIDTH(wp) - col;
2436 if (len > 0)
2437 {
2438 if (len > 2)
2439 len = 2;
2440# ifdef FEAT_RIGHTLEFT
2441 if (wp->w_p_rl)
2442 /* the line number isn't reversed */
2443 copy_text_attr(off + W_WIDTH(wp) - len - col,
2444 (char_u *)" ", len, hl_attr(HLF_FL));
2445 else
2446# endif
2447 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2448 col += len;
2449 }
2450 }
2451#endif
2452
2453 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002454 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002456 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 len = W_WIDTH(wp) - col;
2459 if (len > 0)
2460 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002461 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002462 long num;
2463 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002464
2465 if (len > w + 1)
2466 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002467
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002468 if (wp->w_p_nu && !wp->w_p_rnu)
2469 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002470 num = (long)lnum;
2471 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002472 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002473 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002474 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002475 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002476 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002477 /* 'number' + 'relativenumber': cursor line shows absolute
2478 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002479 num = lnum;
2480 fmt = "%-*ld ";
2481 }
2482 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002483
Bram Moolenaar700e7342013-01-30 12:31:36 +01002484 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485#ifdef FEAT_RIGHTLEFT
2486 if (wp->w_p_rl)
2487 /* the line number isn't reversed */
2488 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2489 hl_attr(HLF_FL));
2490 else
2491#endif
2492 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2493 col += len;
2494 }
2495 }
2496
2497 /*
2498 * 4. Compose the folded-line string with 'foldtext', if set.
2499 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002500 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 txtcol = col; /* remember where text starts */
2503
2504 /*
2505 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2506 * Right-left text is put in columns 0 - number-col, normal text is put
2507 * in columns number-col - window-width.
2508 */
2509#ifdef FEAT_MBYTE
2510 if (has_mbyte)
2511 {
2512 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002513 int u8c, u8cc[MAX_MCO];
2514 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 int idx;
2516 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002517 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518# ifdef FEAT_ARABIC
2519 int prev_c = 0; /* previous Arabic character */
2520 int prev_c1 = 0; /* first composing char for prev_c */
2521# endif
2522
2523# ifdef FEAT_RIGHTLEFT
2524 if (wp->w_p_rl)
2525 idx = off;
2526 else
2527# endif
2528 idx = off + col;
2529
2530 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2531 for (p = text; *p != NUL; )
2532 {
2533 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002534 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (col + cells > W_WIDTH(wp)
2536# ifdef FEAT_RIGHTLEFT
2537 - (wp->w_p_rl ? col : 0)
2538# endif
2539 )
2540 break;
2541 ScreenLines[idx] = *p;
2542 if (enc_utf8)
2543 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002544 u8c = utfc_ptr2char(p, u8cc);
2545 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 ScreenLinesUC[idx] = 0;
2548#ifdef FEAT_ARABIC
2549 prev_c = u8c;
2550#endif
2551 }
2552 else
2553 {
2554#ifdef FEAT_ARABIC
2555 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2556 {
2557 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002558 int pc, pc1, nc;
2559 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 int firstbyte = *p;
2561
2562 /* The idea of what is the previous and next
2563 * character depends on 'rightleft'. */
2564 if (wp->w_p_rl)
2565 {
2566 pc = prev_c;
2567 pc1 = prev_c1;
2568 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002569 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571 else
2572 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002575 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577 prev_c = u8c;
2578
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002579 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 pc, pc1, nc);
2581 ScreenLines[idx] = firstbyte;
2582 }
2583 else
2584 prev_c = u8c;
2585#endif
2586 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002587#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (u8c >= 0x10000)
2589 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2590 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002593 for (i = 0; i < Screen_mco; ++i)
2594 {
2595 ScreenLinesC[i][idx] = u8cc[i];
2596 if (u8cc[i] == 0)
2597 break;
2598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600 if (cells > 1)
2601 ScreenLines[idx + 1] = 0;
2602 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002603 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2604 /* double-byte single width character */
2605 ScreenLines2[idx] = p[1];
2606 else if (cells > 1)
2607 /* double-width character */
2608 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 col += cells;
2610 idx += cells;
2611 p += c_len;
2612 }
2613 }
2614 else
2615#endif
2616 {
2617 len = (int)STRLEN(text);
2618 if (len > W_WIDTH(wp) - col)
2619 len = W_WIDTH(wp) - col;
2620 if (len > 0)
2621 {
2622#ifdef FEAT_RIGHTLEFT
2623 if (wp->w_p_rl)
2624 STRNCPY(current_ScreenLine, text, len);
2625 else
2626#endif
2627 STRNCPY(current_ScreenLine + col, text, len);
2628 col += len;
2629 }
2630 }
2631
2632 /* Fill the rest of the line with the fold filler */
2633#ifdef FEAT_RIGHTLEFT
2634 if (wp->w_p_rl)
2635 col -= txtcol;
2636#endif
2637 while (col < W_WIDTH(wp)
2638#ifdef FEAT_RIGHTLEFT
2639 - (wp->w_p_rl ? txtcol : 0)
2640#endif
2641 )
2642 {
2643#ifdef FEAT_MBYTE
2644 if (enc_utf8)
2645 {
2646 if (fill_fold >= 0x80)
2647 {
2648 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002649 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
2651 else
2652 ScreenLinesUC[off + col] = 0;
2653 }
2654#endif
2655 ScreenLines[off + col++] = fill_fold;
2656 }
2657
2658 if (text != buf)
2659 vim_free(text);
2660
2661 /*
2662 * 6. set highlighting for the Visual area an other text.
2663 * If all folded lines are in the Visual area, highlight the line.
2664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2666 {
2667 if (ltoreq(curwin->w_cursor, VIsual))
2668 {
2669 /* Visual is after curwin->w_cursor */
2670 top = &curwin->w_cursor;
2671 bot = &VIsual;
2672 }
2673 else
2674 {
2675 /* Visual is before curwin->w_cursor */
2676 top = &VIsual;
2677 bot = &curwin->w_cursor;
2678 }
2679 if (lnum >= top->lnum
2680 && lnume <= bot->lnum
2681 && (VIsual_mode != 'v'
2682 || ((lnum > top->lnum
2683 || (lnum == top->lnum
2684 && top->col == 0))
2685 && (lnume < bot->lnum
2686 || (lnume == bot->lnum
2687 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002688 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
2690 if (VIsual_mode == Ctrl_V)
2691 {
2692 /* Visual block mode: highlight the chars part of the block */
2693 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2694 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002695 if (wp->w_old_cursor_lcol != MAXCOL
2696 && wp->w_old_cursor_lcol + txtcol
2697 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 len = wp->w_old_cursor_lcol;
2699 else
2700 len = W_WIDTH(wp) - txtcol;
2701 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002702 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 }
2704 }
2705 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002708 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002713#ifdef FEAT_SYN_HL
2714 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002715 if (wp->w_p_cuc)
2716 {
2717 txtcol += wp->w_virtcol;
2718 if (wp->w_p_wrap)
2719 txtcol -= wp->w_skipcol;
2720 else
2721 txtcol -= wp->w_leftcol;
2722 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2723 ScreenAttrs[off + txtcol] = hl_combine_attr(
2724 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2725 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2729 (int)W_WIDTH(wp), FALSE);
2730
2731 /*
2732 * Update w_cline_height and w_cline_folded if the cursor line was
2733 * updated (saves a call to plines() later).
2734 */
2735 if (wp == curwin
2736 && lnum <= curwin->w_cursor.lnum
2737 && lnume >= curwin->w_cursor.lnum)
2738 {
2739 curwin->w_cline_row = row;
2740 curwin->w_cline_height = 1;
2741 curwin->w_cline_folded = TRUE;
2742 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2743 }
2744}
2745
2746/*
2747 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2748 */
2749 static void
2750copy_text_attr(off, buf, len, attr)
2751 int off;
2752 char_u *buf;
2753 int len;
2754 int attr;
2755{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002756 int i;
2757
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 mch_memmove(ScreenLines + off, buf, (size_t)len);
2759# ifdef FEAT_MBYTE
2760 if (enc_utf8)
2761 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2762# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002763 for (i = 0; i < len; ++i)
2764 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765}
2766
2767/*
2768 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002769 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 */
2771 static void
2772fill_foldcolumn(p, wp, closed, lnum)
2773 char_u *p;
2774 win_T *wp;
2775 int closed; /* TRUE of FALSE */
2776 linenr_T lnum; /* current line number */
2777{
2778 int i = 0;
2779 int level;
2780 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002781 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 /* Init to all spaces. */
2784 copy_spaces(p, (size_t)wp->w_p_fdc);
2785
2786 level = win_foldinfo.fi_level;
2787 if (level > 0)
2788 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002789 /* If there is only one column put more info in it. */
2790 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 /* If the column is too narrow, we start at the lowest level that
2793 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002794 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (first_level < 1)
2796 first_level = 1;
2797
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002798 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
2800 if (win_foldinfo.fi_lnum == lnum
2801 && first_level + i >= win_foldinfo.fi_low_level)
2802 p[i] = '-';
2803 else if (first_level == 1)
2804 p[i] = '|';
2805 else if (first_level + i <= 9)
2806 p[i] = '0' + first_level + i;
2807 else
2808 p[i] = '>';
2809 if (first_level + i == level)
2810 break;
2811 }
2812 }
2813 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002814 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815}
2816#endif /* FEAT_FOLDING */
2817
2818/*
2819 * Display line "lnum" of window 'wp' on the screen.
2820 * Start at row "startrow", stop when "endrow" is reached.
2821 * wp->w_virtcol needs to be valid.
2822 *
2823 * Return the number of last row the line occupies.
2824 */
2825 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002826win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 win_T *wp;
2828 linenr_T lnum;
2829 int startrow;
2830 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002831 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
2833 int col; /* visual column on screen */
2834 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2835 int c = 0; /* init for GCC */
2836 long vcol = 0; /* virtual column (for tabs) */
2837 long vcol_prev = -1; /* "vcol" of previous character */
2838 char_u *line; /* current line */
2839 char_u *ptr; /* current position in "line" */
2840 int row; /* row in the window, excl w_winrow */
2841 int screen_row; /* row on the screen, incl w_winrow */
2842
2843 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2844 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002845 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 int c_extra = NUL; /* extra chars, all the same */
2847 int extra_attr = 0; /* attributes when n_extra != 0 */
2848 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2849 displaying lcs_eol at end-of-line */
2850 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2851 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2852
2853 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2854 int saved_n_extra = 0;
2855 char_u *saved_p_extra = NULL;
2856 int saved_c_extra = 0;
2857 int saved_char_attr = 0;
2858
2859 int n_attr = 0; /* chars with special attr */
2860 int saved_attr2 = 0; /* char_attr saved for n_attr */
2861 int n_attr3 = 0; /* chars with overruling special attr */
2862 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2863
2864 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2865
2866 int fromcol, tocol; /* start/end of inverting */
2867 int fromcol_prev = -2; /* start of inverting after cursor */
2868 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002870 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 pos_T pos;
2872 long v;
2873
2874 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002875 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2877 in this line */
2878 int attr = 0; /* attributes for area highlighting */
2879 int area_attr = 0; /* attributes desired by highlighting */
2880 int search_attr = 0; /* attributes desired by 'hlsearch' */
2881#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002882 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 int syntax_attr = 0; /* attributes desired by syntax */
2884 int has_syntax = FALSE; /* this buffer has syntax highl. */
2885 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002886 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002887 int draw_color_col = FALSE; /* highlight colorcolumn */
2888 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002889#endif
2890#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002891 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002892# define SPWORDLEN 150
2893 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002894 int nextlinecol = 0; /* column where nextline[] starts */
2895 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002896 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002897 int spell_attr = 0; /* attributes desired by spelling */
2898 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002899 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2900 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002901 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002902 static int cap_col = -1; /* column to check for Cap word */
2903 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002904 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905#endif
2906 int extra_check; /* has syntax or linebreak */
2907#ifdef FEAT_MBYTE
2908 int multi_attr = 0; /* attributes desired by multibyte */
2909 int mb_l = 1; /* multi-byte byte length */
2910 int mb_c = 0; /* decoded multi-byte character */
2911 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002912 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913#endif
2914#ifdef FEAT_DIFF
2915 int filler_lines; /* nr of filler lines to be drawn */
2916 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002917 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 int change_start = MAXCOL; /* first col of changed area */
2919 int change_end = -1; /* last col of changed area */
2920#endif
2921 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2922#ifdef FEAT_LINEBREAK
2923 int need_showbreak = FALSE;
2924#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002925#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2926 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002928 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929#endif
2930#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002931 matchitem_T *cur; /* points to the match list */
2932 match_T *shl; /* points to search_hl or a match */
2933 int shl_flag; /* flag to indicate whether search_hl
2934 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002935 int pos_inprogress; /* marks that position match search is
2936 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002937 int prevcol_hl_flag; /* flag to indicate whether prevcol
2938 equals startcol of search_hl or one
2939 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940#endif
2941#ifdef FEAT_ARABIC
2942 int prev_c = 0; /* previous Arabic character */
2943 int prev_c1 = 0; /* first composing char for prev_c */
2944#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002945#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002946 int did_line_attr = 0;
2947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 /* draw_state: items that are drawn in sequence: */
2950#define WL_START 0 /* nothing done yet */
2951#ifdef FEAT_CMDWIN
2952# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2953#else
2954# define WL_CMDLINE WL_START
2955#endif
2956#ifdef FEAT_FOLDING
2957# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2958#else
2959# define WL_FOLD WL_CMDLINE
2960#endif
2961#ifdef FEAT_SIGNS
2962# define WL_SIGN WL_FOLD + 1 /* column for signs */
2963#else
2964# define WL_SIGN WL_FOLD /* column for signs */
2965#endif
2966#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002967#ifdef FEAT_LINEBREAK
2968# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02002970# define WL_BRI WL_NR
2971#endif
2972#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2973# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
2974#else
2975# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976#endif
2977#define WL_LINE WL_SBR + 1 /* text in the line */
2978 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002979#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 int feedback_col = 0;
2981 int feedback_old_attr = -1;
2982#endif
2983
Bram Moolenaar860cae12010-06-05 23:22:07 +02002984#ifdef FEAT_CONCEAL
2985 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002986 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002987 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002988 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002989 int is_concealing = FALSE;
2990 int boguscols = 0; /* nonexistent columns added to force
2991 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002992 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002993 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002994# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02002995# define FIX_FOR_BOGUSCOLS \
2996 { \
2997 n_extra += vcol_off; \
2998 vcol -= vcol_off; \
2999 vcol_off = 0; \
3000 col -= boguscols; \
3001 boguscols = 0; \
3002 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003003#else
3004# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007 if (startrow > endrow) /* past the end already! */
3008 return startrow;
3009
3010 row = startrow;
3011 screen_row = row + W_WINROW(wp);
3012
3013 /*
3014 * To speed up the loop below, set extra_check when there is linebreak,
3015 * trailing white space and/or syntax processing to be done.
3016 */
3017#ifdef FEAT_LINEBREAK
3018 extra_check = wp->w_p_lbr;
3019#else
3020 extra_check = 0;
3021#endif
3022#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003023 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 {
3025 /* Prepare for syntax highlighting in this line. When there is an
3026 * error, stop syntax highlighting. */
3027 save_did_emsg = did_emsg;
3028 did_emsg = FALSE;
3029 syntax_start(wp, lnum);
3030 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003031 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else
3033 {
3034 did_emsg = save_did_emsg;
3035 has_syntax = TRUE;
3036 extra_check = TRUE;
3037 }
3038 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003039
3040 /* Check for columns to display for 'colorcolumn'. */
3041 color_cols = wp->w_p_cc_cols;
3042 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003043 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003044#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003045
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003046#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003047 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003048 && *wp->w_s->b_p_spl != NUL
3049 && wp->w_s->b_langp.ga_len > 0
3050 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003051 {
3052 /* Prepare for spell checking. */
3053 has_spell = TRUE;
3054 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003055
3056 /* Get the start of the next line, so that words that wrap to the next
3057 * line are found too: "et<line-break>al.".
3058 * Trick: skip a few chars for C/shell/Vim comments */
3059 nextline[SPWORDLEN] = NUL;
3060 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3061 {
3062 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3063 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3064 }
3065
3066 /* When a word wrapped from the previous line the start of the current
3067 * line is valid. */
3068 if (lnum == checked_lnum)
3069 cur_checked_col = checked_col;
3070 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003071
3072 /* When there was a sentence end in the previous line may require a
3073 * word starting with capital in this line. In line 1 always check
3074 * the first word. */
3075 if (lnum != capcol_lnum)
3076 cap_col = -1;
3077 if (lnum == 1)
3078 cap_col = 0;
3079 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081#endif
3082
3083 /*
3084 * handle visual active in this window
3085 */
3086 fromcol = -10;
3087 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3089 {
3090 /* Visual is after curwin->w_cursor */
3091 if (ltoreq(curwin->w_cursor, VIsual))
3092 {
3093 top = &curwin->w_cursor;
3094 bot = &VIsual;
3095 }
3096 else /* Visual is before curwin->w_cursor */
3097 {
3098 top = &VIsual;
3099 bot = &curwin->w_cursor;
3100 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003101 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (VIsual_mode == Ctrl_V) /* block mode */
3103 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003104 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
3106 fromcol = wp->w_old_cursor_fcol;
3107 tocol = wp->w_old_cursor_lcol;
3108 }
3109 }
3110 else /* non-block mode */
3111 {
3112 if (lnum > top->lnum && lnum <= bot->lnum)
3113 fromcol = 0;
3114 else if (lnum == top->lnum)
3115 {
3116 if (VIsual_mode == 'V') /* linewise */
3117 fromcol = 0;
3118 else
3119 {
3120 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3121 if (gchar_pos(top) == NUL)
3122 tocol = fromcol + 1;
3123 }
3124 }
3125 if (VIsual_mode != 'V' && lnum == bot->lnum)
3126 {
3127 if (*p_sel == 'e' && bot->col == 0
3128#ifdef FEAT_VIRTUALEDIT
3129 && bot->coladd == 0
3130#endif
3131 )
3132 {
3133 fromcol = -10;
3134 tocol = MAXCOL;
3135 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003136 else if (bot->col == MAXCOL)
3137 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 else
3139 {
3140 pos = *bot;
3141 if (*p_sel == 'e')
3142 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3143 else
3144 {
3145 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3146 ++tocol;
3147 }
3148 }
3149 }
3150 }
3151
3152#ifndef MSDOS
3153 /* Check if the character under the cursor should not be inverted */
3154 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3155# ifdef FEAT_GUI
3156 && !gui.in_use
3157# endif
3158 )
3159 noinvcur = TRUE;
3160#endif
3161
3162 /* if inverting in this line set area_highlighting */
3163 if (fromcol >= 0)
3164 {
3165 area_highlighting = TRUE;
3166 attr = hl_attr(HLF_V);
3167#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003168 if ((clip_star.available && !clip_star.owned
3169 && clip_isautosel_star())
3170 || (clip_plus.available && !clip_plus.owned
3171 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 attr = hl_attr(HLF_VNC);
3173#endif
3174 }
3175 }
3176
3177 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003178 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003180 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 && wp == curwin
3182 && lnum >= curwin->w_cursor.lnum
3183 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3184 {
3185 if (lnum == curwin->w_cursor.lnum)
3186 getvcol(curwin, &(curwin->w_cursor),
3187 (colnr_T *)&fromcol, NULL, NULL);
3188 else
3189 fromcol = 0;
3190 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3191 {
3192 pos.lnum = lnum;
3193 pos.col = search_match_endcol;
3194 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3195 }
3196 else
3197 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003198 /* do at least one character; happens when past end of line */
3199 if (fromcol == tocol)
3200 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 area_highlighting = TRUE;
3202 attr = hl_attr(HLF_I);
3203 }
3204
3205#ifdef FEAT_DIFF
3206 filler_lines = diff_check(wp, lnum);
3207 if (filler_lines < 0)
3208 {
3209 if (filler_lines == -1)
3210 {
3211 if (diff_find_change(wp, lnum, &change_start, &change_end))
3212 diff_hlf = HLF_ADD; /* added line */
3213 else if (change_start == 0)
3214 diff_hlf = HLF_TXD; /* changed text */
3215 else
3216 diff_hlf = HLF_CHD; /* changed line */
3217 }
3218 else
3219 diff_hlf = HLF_ADD; /* added line */
3220 filler_lines = 0;
3221 area_highlighting = TRUE;
3222 }
3223 if (lnum == wp->w_topline)
3224 filler_lines = wp->w_topfill;
3225 filler_todo = filler_lines;
3226#endif
3227
3228#ifdef LINE_ATTR
3229# ifdef FEAT_SIGNS
3230 /* If this line has a sign with line highlighting set line_attr. */
3231 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3232 if (v != 0)
3233 line_attr = sign_get_attr((int)v, TRUE);
3234# endif
3235# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3236 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003237 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 line_attr = hl_attr(HLF_L);
3239# endif
3240 if (line_attr != 0)
3241 area_highlighting = TRUE;
3242#endif
3243
3244 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3245 ptr = line;
3246
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003247#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003248 if (has_spell)
3249 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003250 /* For checking first word with a capital skip white space. */
3251 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003252 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003253
Bram Moolenaar30abd282005-06-22 22:35:10 +00003254 /* To be able to spell-check over line boundaries copy the end of the
3255 * current line into nextline[]. Above the start of the next line was
3256 * copied to nextline[SPWORDLEN]. */
3257 if (nextline[SPWORDLEN] == NUL)
3258 {
3259 /* No next line or it is empty. */
3260 nextlinecol = MAXCOL;
3261 nextline_idx = 0;
3262 }
3263 else
3264 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003265 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003266 if (v < SPWORDLEN)
3267 {
3268 /* Short line, use it completely and append the start of the
3269 * next line. */
3270 nextlinecol = 0;
3271 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003272 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003273 nextline_idx = v + 1;
3274 }
3275 else
3276 {
3277 /* Long line, use only the last SPWORDLEN bytes. */
3278 nextlinecol = v - SPWORDLEN;
3279 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3280 nextline_idx = SPWORDLEN + 1;
3281 }
3282 }
3283 }
3284#endif
3285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 /* find start of trailing whitespace */
3287 if (wp->w_p_list && lcs_trail)
3288 {
3289 trailcol = (colnr_T)STRLEN(ptr);
3290 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3291 --trailcol;
3292 trailcol += (colnr_T) (ptr - line);
3293 extra_check = TRUE;
3294 }
3295
3296 /*
3297 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3298 * first character to be displayed.
3299 */
3300 if (wp->w_p_wrap)
3301 v = wp->w_skipcol;
3302 else
3303 v = wp->w_leftcol;
3304 if (v > 0)
3305 {
3306#ifdef FEAT_MBYTE
3307 char_u *prev_ptr = ptr;
3308#endif
3309 while (vcol < v && *ptr != NUL)
3310 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003311 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 vcol += c;
3313#ifdef FEAT_MBYTE
3314 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003316 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003319 /* When:
3320 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003321 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003322 * - 'virtualedit' is set, or
3323 * - the visual mode is active,
3324 * the end of the line may be before the start of the displayed part.
3325 */
3326 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003327#ifdef FEAT_SYN_HL
3328 wp->w_p_cuc || draw_color_col ||
3329#endif
3330#ifdef FEAT_VIRTUALEDIT
3331 virtual_active() ||
3332#endif
3333 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338 /* Handle a character that's not completely on the screen: Put ptr at
3339 * that character but skip the first few screen characters. */
3340 if (vcol > v)
3341 {
3342 vcol -= c;
3343#ifdef FEAT_MBYTE
3344 ptr = prev_ptr;
3345#else
3346 --ptr;
3347#endif
3348 n_skip = v - vcol;
3349 }
3350
3351 /*
3352 * Adjust for when the inverted text is before the screen,
3353 * and when the start of the inverted text is before the screen.
3354 */
3355 if (tocol <= vcol)
3356 fromcol = 0;
3357 else if (fromcol >= 0 && fromcol < vcol)
3358 fromcol = vcol;
3359
3360#ifdef FEAT_LINEBREAK
3361 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3362 if (wp->w_p_wrap)
3363 need_showbreak = TRUE;
3364#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003365#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003366 /* When spell checking a word we need to figure out the start of the
3367 * word and if it's badly spelled or not. */
3368 if (has_spell)
3369 {
3370 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003371 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003372 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003373
3374 pos = wp->w_cursor;
3375 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003376 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003377 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003378
3379 /* spell_move_to() may call ml_get() and make "line" invalid */
3380 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3381 ptr = line + linecol;
3382
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003383 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003384 {
3385 /* no bad word found at line start, don't check until end of a
3386 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003387 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003388 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003389 }
3390 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003391 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003392 /* bad word found, use attributes until end of word */
3393 word_end = wp->w_cursor.col + len + 1;
3394
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003395 /* Turn index into actual attributes. */
3396 if (spell_hlf != HLF_COUNT)
3397 spell_attr = highlight_attr[spell_hlf];
3398 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003399 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003400
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003401# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003402 /* Need to restart syntax highlighting for this line. */
3403 if (has_syntax)
3404 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003405# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003406 }
3407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409
3410 /*
3411 * Correct highlighting for cursor that can't be disabled.
3412 * Avoids having to check this for each character.
3413 */
3414 if (fromcol >= 0)
3415 {
3416 if (noinvcur)
3417 {
3418 if ((colnr_T)fromcol == wp->w_virtcol)
3419 {
3420 /* highlighting starts at cursor, let it start just after the
3421 * cursor */
3422 fromcol_prev = fromcol;
3423 fromcol = -1;
3424 }
3425 else if ((colnr_T)fromcol < wp->w_virtcol)
3426 /* restart highlighting after the cursor */
3427 fromcol_prev = wp->w_virtcol;
3428 }
3429 if (fromcol >= tocol)
3430 fromcol = -1;
3431 }
3432
3433#ifdef FEAT_SEARCH_EXTRA
3434 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003435 * Handle highlighting the last used search pattern and matches.
3436 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003438 cur = wp->w_match_head;
3439 shl_flag = FALSE;
3440 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003442 if (shl_flag == FALSE)
3443 {
3444 shl = &search_hl;
3445 shl_flag = TRUE;
3446 }
3447 else
3448 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003449 shl->startcol = MAXCOL;
3450 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003452 v = (long)(ptr - line);
3453 if (cur != NULL)
3454 cur->pos.cur = 0;
3455 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3456
3457 /* Need to get the line again, a multi-line regexp may have made it
3458 * invalid. */
3459 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3460 ptr = line + v;
3461
3462 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003464 if (shl->lnum == lnum)
3465 shl->startcol = shl->rm.startpos[0].col;
3466 else
3467 shl->startcol = 0;
3468 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3469 - shl->rm.startpos[0].lnum)
3470 shl->endcol = shl->rm.endpos[0].col;
3471 else
3472 shl->endcol = MAXCOL;
3473 /* Highlight one character for an empty match. */
3474 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003477 if (has_mbyte && line[shl->endcol] != NUL)
3478 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3479 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003481 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003483 if ((long)shl->startcol < v) /* match at leftcol */
3484 {
3485 shl->attr_cur = shl->attr;
3486 search_attr = shl->attr;
3487 }
3488 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003490 if (shl != &search_hl && cur != NULL)
3491 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493#endif
3494
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003495#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003496 /* Cursor line highlighting for 'cursorline' in the current window. Not
3497 * when Visual mode is active, because it's not clear what is selected
3498 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003499 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003500 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003501 {
3502 line_attr = hl_attr(HLF_CUL);
3503 area_highlighting = TRUE;
3504 }
3505#endif
3506
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003507 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 col = 0;
3509#ifdef FEAT_RIGHTLEFT
3510 if (wp->w_p_rl)
3511 {
3512 /* Rightleft window: process the text in the normal direction, but put
3513 * it in current_ScreenLine[] from right to left. Start at the
3514 * rightmost column of the window. */
3515 col = W_WIDTH(wp) - 1;
3516 off += col;
3517 }
3518#endif
3519
3520 /*
3521 * Repeat for the whole displayed line.
3522 */
3523 for (;;)
3524 {
3525 /* Skip this quickly when working on the text. */
3526 if (draw_state != WL_LINE)
3527 {
3528#ifdef FEAT_CMDWIN
3529 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3530 {
3531 draw_state = WL_CMDLINE;
3532 if (cmdwin_type != 0 && wp == curwin)
3533 {
3534 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003536 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 char_attr = hl_attr(HLF_AT);
3538 }
3539 }
3540#endif
3541
3542#ifdef FEAT_FOLDING
3543 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3544 {
3545 draw_state = WL_FOLD;
3546 if (wp->w_p_fdc > 0)
3547 {
3548 /* Draw the 'foldcolumn'. */
3549 fill_foldcolumn(extra, wp, FALSE, lnum);
3550 n_extra = wp->w_p_fdc;
3551 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003552 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 c_extra = NUL;
3554 char_attr = hl_attr(HLF_FC);
3555 }
3556 }
3557#endif
3558
3559#ifdef FEAT_SIGNS
3560 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3561 {
3562 draw_state = WL_SIGN;
3563 /* Show the sign column when there are any signs in this
3564 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003565 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003567 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003569 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570# endif
3571
3572 /* Draw two cells with the sign value or blank. */
3573 c_extra = ' ';
3574 char_attr = hl_attr(HLF_SC);
3575 n_extra = 2;
3576
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003577 if (row == startrow
3578#ifdef FEAT_DIFF
3579 + filler_lines && filler_todo <= 0
3580#endif
3581 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3584 SIGN_TEXT);
3585# ifdef FEAT_SIGN_ICONS
3586 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3587 SIGN_ICON);
3588 if (gui.in_use && icon_sign != 0)
3589 {
3590 /* Use the image in this position. */
3591 c_extra = SIGN_BYTE;
3592# ifdef FEAT_NETBEANS_INTG
3593 if (buf_signcount(wp->w_buffer, lnum) > 1)
3594 c_extra = MULTISIGN_BYTE;
3595# endif
3596 char_attr = icon_sign;
3597 }
3598 else
3599# endif
3600 if (text_sign != 0)
3601 {
3602 p_extra = sign_get_text(text_sign);
3603 if (p_extra != NULL)
3604 {
3605 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003606 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 char_attr = sign_get_attr(text_sign, FALSE);
3609 }
3610 }
3611 }
3612 }
3613#endif
3614
3615 if (draw_state == WL_NR - 1 && n_extra == 0)
3616 {
3617 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003618 /* Display the absolute or relative line number. After the
3619 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3620 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 && (row == startrow
3622#ifdef FEAT_DIFF
3623 + filler_lines
3624#endif
3625 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3626 {
3627 /* Draw the line number (empty space after wrapping). */
3628 if (row == startrow
3629#ifdef FEAT_DIFF
3630 + filler_lines
3631#endif
3632 )
3633 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003634 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003635 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003636
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003637 if (wp->w_p_nu && !wp->w_p_rnu)
3638 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003639 num = (long)lnum;
3640 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003641 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003642 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003643 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003644 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003645 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003646 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003647 num = lnum;
3648 fmt = "%-*ld ";
3649 }
3650 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003651
Bram Moolenaar700e7342013-01-30 12:31:36 +01003652 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003653 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (wp->w_skipcol > 0)
3655 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3656 *p_extra = '-';
3657#ifdef FEAT_RIGHTLEFT
3658 if (wp->w_p_rl) /* reverse line numbers */
3659 rl_mirror(extra);
3660#endif
3661 p_extra = extra;
3662 c_extra = NUL;
3663 }
3664 else
3665 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003666 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003668#ifdef FEAT_SYN_HL
3669 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003670 * the current line differently.
3671 * TODO: Can we use CursorLine instead of CursorLineNr
3672 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003673 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003674 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003675 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 }
3679
Bram Moolenaar597a4222014-06-25 14:39:50 +02003680#ifdef FEAT_LINEBREAK
3681 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3682 && n_extra == 0 && *p_sbr != NUL)
3683 /* draw indent after showbreak value */
3684 draw_state = WL_BRI;
3685 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3686 /* After the showbreak, draw the breakindent */
3687 draw_state = WL_BRI - 1;
3688
3689 /* draw 'breakindent': indent wrapped text accordingly */
3690 if (draw_state == WL_BRI - 1 && n_extra == 0)
3691 {
3692 draw_state = WL_BRI;
3693# ifdef FEAT_DIFF
3694# endif
3695 if (wp->w_p_bri && n_extra == 0 && row != startrow
3696#ifdef FEAT_DIFF
3697 && filler_lines == 0
3698#endif
3699 )
3700 {
3701 char_attr = 0; /* was: hl_attr(HLF_AT); */
3702#ifdef FEAT_DIFF
3703 if (diff_hlf != (hlf_T)0)
3704 char_attr = hl_attr(diff_hlf);
3705#endif
3706 p_extra = NUL;
3707 c_extra = ' ';
3708 n_extra = get_breakindent_win(wp,
3709 ml_get_buf(wp->w_buffer, lnum, FALSE));
3710 /* Correct end of highlighted area for 'breakindent',
3711 * required when 'linebreak' is also set. */
3712 if (tocol == vcol)
3713 tocol += n_extra;
3714 }
3715 }
3716#endif
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3719 if (draw_state == WL_SBR - 1 && n_extra == 0)
3720 {
3721 draw_state = WL_SBR;
3722# ifdef FEAT_DIFF
3723 if (filler_todo > 0)
3724 {
3725 /* Draw "deleted" diff line(s). */
3726 if (char2cells(fill_diff) > 1)
3727 c_extra = '-';
3728 else
3729 c_extra = fill_diff;
3730# ifdef FEAT_RIGHTLEFT
3731 if (wp->w_p_rl)
3732 n_extra = col + 1;
3733 else
3734# endif
3735 n_extra = W_WIDTH(wp) - col;
3736 char_attr = hl_attr(HLF_DED);
3737 }
3738# endif
3739# ifdef FEAT_LINEBREAK
3740 if (*p_sbr != NUL && need_showbreak)
3741 {
3742 /* Draw 'showbreak' at the start of each broken line. */
3743 p_extra = p_sbr;
3744 c_extra = NUL;
3745 n_extra = (int)STRLEN(p_sbr);
3746 char_attr = hl_attr(HLF_AT);
3747 need_showbreak = FALSE;
3748 /* Correct end of highlighted area for 'showbreak',
3749 * required when 'linebreak' is also set. */
3750 if (tocol == vcol)
3751 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003752#ifdef FEAT_SYN_HL
3753 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003754 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003755 char_attr = hl_combine_attr(char_attr, HLF_CLN);
3756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758# endif
3759 }
3760#endif
3761
3762 if (draw_state == WL_LINE - 1 && n_extra == 0)
3763 {
3764 draw_state = WL_LINE;
3765 if (saved_n_extra)
3766 {
3767 /* Continue item from end of wrapped line. */
3768 n_extra = saved_n_extra;
3769 c_extra = saved_c_extra;
3770 p_extra = saved_p_extra;
3771 char_attr = saved_char_attr;
3772 }
3773 else
3774 char_attr = 0;
3775 }
3776 }
3777
3778 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003779 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003780 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781#ifdef FEAT_DIFF
3782 && filler_todo <= 0
3783#endif
3784 )
3785 {
3786 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3787 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003788 /* Pretend we have finished updating the window. Except when
3789 * 'cursorcolumn' is set. */
3790#ifdef FEAT_SYN_HL
3791 if (wp->w_p_cuc)
3792 row = wp->w_cline_row + wp->w_cline_height;
3793 else
3794#endif
3795 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 break;
3797 }
3798
3799 if (draw_state == WL_LINE && area_highlighting)
3800 {
3801 /* handle Visual or match highlighting in this line */
3802 if (vcol == fromcol
3803#ifdef FEAT_MBYTE
3804 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3805 && (*mb_ptr2cells)(ptr) > 1)
3806#endif
3807 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003808 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 && vcol < tocol))
3810 area_attr = attr; /* start highlighting */
3811 else if (area_attr != 0
3812 && (vcol == tocol
3813 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
3816#ifdef FEAT_SEARCH_EXTRA
3817 if (!n_extra)
3818 {
3819 /*
3820 * Check for start/end of search pattern match.
3821 * After end, check for start/end of next match.
3822 * When another match, have to check for start again.
3823 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003824 * Do this for 'search_hl' and the match list (ordered by
3825 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003827 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003828 cur = wp->w_match_head;
3829 shl_flag = FALSE;
3830 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003832 if (shl_flag == FALSE
3833 && ((cur != NULL
3834 && cur->priority > SEARCH_HL_PRIORITY)
3835 || cur == NULL))
3836 {
3837 shl = &search_hl;
3838 shl_flag = TRUE;
3839 }
3840 else
3841 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003842 if (cur != NULL)
3843 cur->pos.cur = 0;
3844 pos_inprogress = TRUE;
3845 while (shl->rm.regprog != NULL
3846 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003848 if (shl->startcol != MAXCOL
3849 && v >= (long)shl->startcol
3850 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
3852 shl->attr_cur = shl->attr;
3853 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003854 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
3856 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003857 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3858 pos_inprogress = cur == NULL || cur->pos.cur == 0
3859 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861 /* Need to get the line again, a multi-line regexp
3862 * may have made it invalid. */
3863 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3864 ptr = line + v;
3865
3866 if (shl->lnum == lnum)
3867 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003868 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003870 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003872 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003874 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
3876 /* highlight empty match, try again after
3877 * it */
3878#ifdef FEAT_MBYTE
3879 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003880 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003881 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 else
3883#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003884 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886
3887 /* Loop to check if the match starts at the
3888 * current position */
3889 continue;
3890 }
3891 }
3892 break;
3893 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003894 if (shl != &search_hl && cur != NULL)
3895 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003897
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003898 /* Use attributes from match with highest priority among
3899 * 'search_hl' and the match list. */
3900 search_attr = search_hl.attr_cur;
3901 cur = wp->w_match_head;
3902 shl_flag = FALSE;
3903 while (cur != NULL || shl_flag == FALSE)
3904 {
3905 if (shl_flag == FALSE
3906 && ((cur != NULL
3907 && cur->priority > SEARCH_HL_PRIORITY)
3908 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003909 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003910 shl = &search_hl;
3911 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003912 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003913 else
3914 shl = &cur->hl;
3915 if (shl->attr_cur != 0)
3916 search_attr = shl->attr_cur;
3917 if (shl != &search_hl && cur != NULL)
3918 cur = cur->next;
3919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921#endif
3922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003924 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003926 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3927 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003929 if (diff_hlf == HLF_TXD && ptr - line > change_end
3930 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003932 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003935
3936 /* Decide which of the highlight attributes to use. */
3937 attr_pri = TRUE;
3938 if (area_attr != 0)
3939 char_attr = area_attr;
3940 else if (search_attr != 0)
3941 char_attr = search_attr;
3942#ifdef LINE_ATTR
3943 /* Use line_attr when not in the Visual or 'incsearch' area
3944 * (area_attr may be 0 when "noinvcur" is set). */
3945 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003946 || vcol < fromcol || vcol_prev < fromcol_prev
3947 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003948 char_attr = line_attr;
3949#endif
3950 else
3951 {
3952 attr_pri = FALSE;
3953#ifdef FEAT_SYN_HL
3954 if (has_syntax)
3955 char_attr = syntax_attr;
3956 else
3957#endif
3958 char_attr = 0;
3959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961
3962 /*
3963 * Get the next character to put on the screen.
3964 */
3965 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003966 * The "p_extra" points to the extra stuff that is inserted to
3967 * represent special characters (non-printable stuff) and other
3968 * things. When all characters are the same, c_extra is used.
3969 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3970 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3972 */
3973 if (n_extra > 0)
3974 {
3975 if (c_extra != NUL)
3976 {
3977 c = c_extra;
3978#ifdef FEAT_MBYTE
3979 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3980 if (enc_utf8 && (*mb_char2len)(c) > 1)
3981 {
3982 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003983 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003984 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986 else
3987 mb_utf8 = FALSE;
3988#endif
3989 }
3990 else
3991 {
3992 c = *p_extra;
3993#ifdef FEAT_MBYTE
3994 if (has_mbyte)
3995 {
3996 mb_c = c;
3997 if (enc_utf8)
3998 {
3999 /* If the UTF-8 character is more than one byte:
4000 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004001 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 mb_utf8 = FALSE;
4003 if (mb_l > n_extra)
4004 mb_l = 1;
4005 else if (mb_l > 1)
4006 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004007 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004009 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011 }
4012 else
4013 {
4014 /* if this is a DBCS character, put it in "mb_c" */
4015 mb_l = MB_BYTE2LEN(c);
4016 if (mb_l >= n_extra)
4017 mb_l = 1;
4018 else if (mb_l > 1)
4019 mb_c = (c << 8) + p_extra[1];
4020 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004021 if (mb_l == 0) /* at the NUL at end-of-line */
4022 mb_l = 1;
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 /* If a double-width char doesn't fit display a '>' in the
4025 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004026 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027# ifdef FEAT_RIGHTLEFT
4028 wp->w_p_rl ? (col <= 0) :
4029# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004030 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 && (*mb_char2cells)(mb_c) == 2)
4032 {
4033 c = '>';
4034 mb_c = c;
4035 mb_l = 1;
4036 mb_utf8 = FALSE;
4037 multi_attr = hl_attr(HLF_AT);
4038 /* put the pointer back to output the double-width
4039 * character at the start of the next line. */
4040 ++n_extra;
4041 --p_extra;
4042 }
4043 else
4044 {
4045 n_extra -= mb_l - 1;
4046 p_extra += mb_l - 1;
4047 }
4048 }
4049#endif
4050 ++p_extra;
4051 }
4052 --n_extra;
4053 }
4054 else
4055 {
4056 /*
4057 * Get a character from the line itself.
4058 */
4059 c = *ptr;
4060#ifdef FEAT_MBYTE
4061 if (has_mbyte)
4062 {
4063 mb_c = c;
4064 if (enc_utf8)
4065 {
4066 /* If the UTF-8 character is more than one byte: Decode it
4067 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004068 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 mb_utf8 = FALSE;
4070 if (mb_l > 1)
4071 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004072 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 /* Overlong encoded ASCII or ASCII with composing char
4074 * is displayed normally, except a NUL. */
4075 if (mb_c < 0x80)
4076 c = mb_c;
4077 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004078
4079 /* At start of the line we can have a composing char.
4080 * Draw it as a space with a composing char. */
4081 if (utf_iscomposing(mb_c))
4082 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004083 int i;
4084
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004085 for (i = Screen_mco - 1; i > 0; --i)
4086 u8cc[i] = u8cc[i - 1];
4087 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004088 mb_c = ' ';
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091
4092 if ((mb_l == 1 && c >= 0x80)
4093 || (mb_l >= 1 && mb_c == 0)
4094 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004095# ifdef UNICODE16
4096 || mb_c >= 0x10000
4097# endif
4098 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
4100 /*
4101 * Illegal UTF-8 byte: display as <xx>.
4102 * Non-BMP character : display as ? or fullwidth ?.
4103 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004104# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
4108 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004109# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (wp->w_p_rl) /* reverse */
4111 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004114# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 else if (utf_char2cells(mb_c) != 2)
4116 STRCPY(extra, "?");
4117 else
4118 /* 0xff1f in UTF-8: full-width '?' */
4119 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004120# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
4122 p_extra = extra;
4123 c = *p_extra;
4124 mb_c = mb_ptr2char_adv(&p_extra);
4125 mb_utf8 = (c >= 0x80);
4126 n_extra = (int)STRLEN(p_extra);
4127 c_extra = NUL;
4128 if (area_attr == 0 && search_attr == 0)
4129 {
4130 n_attr = n_extra + 1;
4131 extra_attr = hl_attr(HLF_8);
4132 saved_attr2 = char_attr; /* save current attr */
4133 }
4134 }
4135 else if (mb_l == 0) /* at the NUL at end-of-line */
4136 mb_l = 1;
4137#ifdef FEAT_ARABIC
4138 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4139 {
4140 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004141 int pc, pc1, nc;
4142 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 /* The idea of what is the previous and next
4145 * character depends on 'rightleft'. */
4146 if (wp->w_p_rl)
4147 {
4148 pc = prev_c;
4149 pc1 = prev_c1;
4150 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004151 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153 else
4154 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004155 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004157 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159 prev_c = mb_c;
4160
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004161 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163 else
4164 prev_c = mb_c;
4165#endif
4166 }
4167 else /* enc_dbcs */
4168 {
4169 mb_l = MB_BYTE2LEN(c);
4170 if (mb_l == 0) /* at the NUL at end-of-line */
4171 mb_l = 1;
4172 else if (mb_l > 1)
4173 {
4174 /* We assume a second byte below 32 is illegal.
4175 * Hopefully this is OK for all double-byte encodings!
4176 */
4177 if (ptr[1] >= 32)
4178 mb_c = (c << 8) + ptr[1];
4179 else
4180 {
4181 if (ptr[1] == NUL)
4182 {
4183 /* head byte at end of line */
4184 mb_l = 1;
4185 transchar_nonprint(extra, c);
4186 }
4187 else
4188 {
4189 /* illegal tail byte */
4190 mb_l = 2;
4191 STRCPY(extra, "XX");
4192 }
4193 p_extra = extra;
4194 n_extra = (int)STRLEN(extra) - 1;
4195 c_extra = NUL;
4196 c = *p_extra++;
4197 if (area_attr == 0 && search_attr == 0)
4198 {
4199 n_attr = n_extra + 1;
4200 extra_attr = hl_attr(HLF_8);
4201 saved_attr2 = char_attr; /* save current attr */
4202 }
4203 mb_c = c;
4204 }
4205 }
4206 }
4207 /* If a double-width char doesn't fit display a '>' in the
4208 * last column; the character is displayed at the start of the
4209 * next line. */
4210 if ((
4211# ifdef FEAT_RIGHTLEFT
4212 wp->w_p_rl ? (col <= 0) :
4213# endif
4214 (col >= W_WIDTH(wp) - 1))
4215 && (*mb_char2cells)(mb_c) == 2)
4216 {
4217 c = '>';
4218 mb_c = c;
4219 mb_utf8 = FALSE;
4220 mb_l = 1;
4221 multi_attr = hl_attr(HLF_AT);
4222 /* Put pointer back so that the character will be
4223 * displayed at the start of the next line. */
4224 --ptr;
4225 }
4226 else if (*ptr != NUL)
4227 ptr += mb_l - 1;
4228
4229 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004230 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004231 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004232 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004235 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 c = ' ';
4237 if (area_attr == 0 && search_attr == 0)
4238 {
4239 n_attr = n_extra + 1;
4240 extra_attr = hl_attr(HLF_AT);
4241 saved_attr2 = char_attr; /* save current attr */
4242 }
4243 mb_c = c;
4244 mb_utf8 = FALSE;
4245 mb_l = 1;
4246 }
4247
4248 }
4249#endif
4250 ++ptr;
4251
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004252 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004253 if (wp->w_p_list && (c == 160
4254#ifdef FEAT_MBYTE
4255 || (mb_utf8 && mb_c == 160)
4256#endif
4257 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004258 {
4259 c = lcs_nbsp;
4260 if (area_attr == 0 && search_attr == 0)
4261 {
4262 n_attr = 1;
4263 extra_attr = hl_attr(HLF_8);
4264 saved_attr2 = char_attr; /* save current attr */
4265 }
4266#ifdef FEAT_MBYTE
4267 mb_c = c;
4268 if (enc_utf8 && (*mb_char2len)(c) > 1)
4269 {
4270 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004271 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004272 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004273 }
4274 else
4275 mb_utf8 = FALSE;
4276#endif
4277 }
4278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (extra_check)
4280 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004281#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004282 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004283#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004284
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004285#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 /* Get syntax attribute, unless still at the start of the line
4287 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004288 v = (long)(ptr - line);
4289 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 {
4291 /* Get the syntax attribute for the character. If there
4292 * is an error, disable syntax highlighting. */
4293 save_did_emsg = did_emsg;
4294 did_emsg = FALSE;
4295
Bram Moolenaar217ad922005-03-20 22:37:15 +00004296 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004297# ifdef FEAT_SPELL
4298 has_spell ? &can_spell :
4299# endif
4300 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
4302 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004303 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004304 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004305 has_syntax = FALSE;
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 else
4308 did_emsg = save_did_emsg;
4309
4310 /* Need to get the line again, a multi-line regexp may
4311 * have made it invalid. */
4312 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4313 ptr = line + v;
4314
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004315 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004317 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004318 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004319# ifdef FEAT_CONCEAL
4320 /* no concealing past the end of the line, it interferes
4321 * with line highlighting */
4322 if (c == NUL)
4323 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004324 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004325 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004328#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004329
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004330#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004331 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004332 * Only do this when there is no syntax highlighting, the
4333 * @Spell cluster is not used or the current syntax item
4334 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004335 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004336 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004337 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004338# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004339 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004340 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004341# endif
4342 if (c != 0 && (
4343# ifdef FEAT_SYN_HL
4344 !has_syntax ||
4345# endif
4346 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004347 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004348 char_u *prev_ptr, *p;
4349 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004350 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004351# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004352 if (has_mbyte)
4353 {
4354 prev_ptr = ptr - mb_l;
4355 v -= mb_l - 1;
4356 }
4357 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004358# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004359 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004360
4361 /* Use nextline[] if possible, it has the start of the
4362 * next line concatenated. */
4363 if ((prev_ptr - line) - nextlinecol >= 0)
4364 p = nextline + (prev_ptr - line) - nextlinecol;
4365 else
4366 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004367 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004368 len = spell_check(wp, p, &spell_hlf, &cap_col,
4369 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004370 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004371
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004372 /* In Insert mode only highlight a word that
4373 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004374 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004375 && (State & INSERT) != 0
4376 && wp->w_cursor.lnum == lnum
4377 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004378 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004379 && wp->w_cursor.col < (colnr_T)word_end)
4380 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004381 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004382 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004383 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004384
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004385 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004386 && (p - nextline) + len > nextline_idx)
4387 {
4388 /* Remember that the good word continues at the
4389 * start of the next line. */
4390 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004391 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004392 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004393
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004394 /* Turn index into actual attributes. */
4395 if (spell_hlf != HLF_COUNT)
4396 spell_attr = highlight_attr[spell_hlf];
4397
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004398 if (cap_col > 0)
4399 {
4400 if (p != prev_ptr
4401 && (p - nextline) + cap_col >= nextline_idx)
4402 {
4403 /* Remember that the word in the next line
4404 * must start with a capital. */
4405 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004406 cap_col = (int)((p - nextline) + cap_col
4407 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004408 }
4409 else
4410 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004411 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004412 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004413 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004414 }
4415 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004416 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004417 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004418 char_attr = hl_combine_attr(char_attr, spell_attr);
4419 else
4420 char_attr = hl_combine_attr(spell_attr, char_attr);
4421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422#endif
4423#ifdef FEAT_LINEBREAK
4424 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004425 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 */
4427 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004428 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004430 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431# ifdef FEAT_MBYTE
4432 has_mbyte ? mb_l :
4433# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004434 1);
4435 /* TODO: is passing p for start of the line OK? */
4436 n_extra = win_lbr_chartabsize(wp, p, p, (colnr_T)vcol,
4437 NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 c_extra = ' ';
4439 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004440 {
4441#ifdef FEAT_CONCEAL
4442 if (c == TAB)
4443 /* See "Tab alignment" below. */
4444 FIX_FOR_BOGUSCOLS;
4445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449#endif
4450
4451 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4452 {
4453 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004454 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
4456 n_attr = 1;
4457 extra_attr = hl_attr(HLF_8);
4458 saved_attr2 = char_attr; /* save current attr */
4459 }
4460#ifdef FEAT_MBYTE
4461 mb_c = c;
4462 if (enc_utf8 && (*mb_char2len)(c) > 1)
4463 {
4464 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004465 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004466 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 else
4469 mb_utf8 = FALSE;
4470#endif
4471 }
4472 }
4473
4474 /*
4475 * Handling of non-printable characters.
4476 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004477 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
4479 /*
4480 * when getting a character from the file, we may have to
4481 * turn it into something else on the way to putting it
4482 * into "ScreenLines".
4483 */
4484 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4485 {
4486 /* tab amount depends on current column */
4487 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004488 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4489#ifdef FEAT_CONCEAL
4490 /* Tab alignment should be identical regardless of
4491 * 'conceallevel' value. So tab compensates of all
4492 * previous concealed characters, and thus resets vcol_off
4493 * and boguscols accumulated so far in the line. Note that
4494 * the tab can be longer than 'tabstop' when there
4495 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004496 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498#ifdef FEAT_MBYTE
4499 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4500#endif
4501 if (wp->w_p_list)
4502 {
4503 c = lcs_tab1;
4504 c_extra = lcs_tab2;
4505 n_attr = n_extra + 1;
4506 extra_attr = hl_attr(HLF_8);
4507 saved_attr2 = char_attr; /* save current attr */
4508#ifdef FEAT_MBYTE
4509 mb_c = c;
4510 if (enc_utf8 && (*mb_char2len)(c) > 1)
4511 {
4512 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004513 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004514 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516#endif
4517 }
4518 else
4519 {
4520 c_extra = ' ';
4521 c = ' ';
4522 }
4523 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004524 else if (c == NUL
4525 && ((wp->w_p_list && lcs_eol > 0)
4526 || ((fromcol >= 0 || fromcol_prev >= 0)
4527 && tocol > vcol
4528 && VIsual_mode != Ctrl_V
4529 && (
4530# ifdef FEAT_RIGHTLEFT
4531 wp->w_p_rl ? (col >= 0) :
4532# endif
4533 (col < W_WIDTH(wp)))
4534 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004535 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004536 && (colnr_T)vcol == wp->w_virtcol)))
4537 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004539 /* Display a '$' after the line or highlight an extra
4540 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4542 /* For a diff line the highlighting continues after the
4543 * "$". */
4544 if (
4545# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004546 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547# ifdef LINE_ATTR
4548 &&
4549# endif
4550# endif
4551# ifdef LINE_ATTR
4552 line_attr == 0
4553# endif
4554 )
4555#endif
4556 {
4557#ifdef FEAT_VIRTUALEDIT
4558 /* In virtualedit, visual selections may extend
4559 * beyond end of line. */
4560 if (area_highlighting && virtual_active()
4561 && tocol != MAXCOL && vcol < tocol)
4562 n_extra = 0;
4563 else
4564#endif
4565 {
4566 p_extra = at_end_str;
4567 n_extra = 1;
4568 c_extra = NUL;
4569 }
4570 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004571 if (wp->w_p_list)
4572 c = lcs_eol;
4573 else
4574 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 lcs_eol_one = -1;
4576 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004577 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
4579 extra_attr = hl_attr(HLF_AT);
4580 n_attr = 1;
4581 }
4582#ifdef FEAT_MBYTE
4583 mb_c = c;
4584 if (enc_utf8 && (*mb_char2len)(c) > 1)
4585 {
4586 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004587 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004588 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 else
4591 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4592#endif
4593 }
4594 else if (c != NUL)
4595 {
4596 p_extra = transchar(c);
4597#ifdef FEAT_RIGHTLEFT
4598 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4599 rl_mirror(p_extra); /* reverse "<12>" */
4600#endif
4601 n_extra = byte2cells(c) - 1;
4602 c_extra = NUL;
4603 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004604 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 n_attr = n_extra + 1;
4607 extra_attr = hl_attr(HLF_8);
4608 saved_attr2 = char_attr; /* save current attr */
4609 }
4610#ifdef FEAT_MBYTE
4611 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4612#endif
4613 }
4614#ifdef FEAT_VIRTUALEDIT
4615 else if (VIsual_active
4616 && (VIsual_mode == Ctrl_V
4617 || VIsual_mode == 'v')
4618 && virtual_active()
4619 && tocol != MAXCOL
4620 && vcol < tocol
4621 && (
4622# ifdef FEAT_RIGHTLEFT
4623 wp->w_p_rl ? (col >= 0) :
4624# endif
4625 (col < W_WIDTH(wp))))
4626 {
4627 c = ' ';
4628 --ptr; /* put it back at the NUL */
4629 }
4630#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004631#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 else if ((
4633# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004634 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 ) && (
4638# ifdef FEAT_RIGHTLEFT
4639 wp->w_p_rl ? (col >= 0) :
4640# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004641 (col
4642# ifdef FEAT_CONCEAL
4643 - boguscols
4644# endif
4645 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 /* Highlight until the right side of the window */
4648 c = ' ';
4649 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004650
4651 /* Remember we do the char for line highlighting. */
4652 ++did_line_attr;
4653
4654 /* don't do search HL for the rest of the line */
4655 if (line_attr != 0 && char_attr == search_attr && col > 0)
4656 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657# ifdef FEAT_DIFF
4658 if (diff_hlf == HLF_TXD)
4659 {
4660 diff_hlf = HLF_CHD;
4661 if (attr == 0 || char_attr != attr)
4662 char_attr = hl_attr(diff_hlf);
4663 }
4664# endif
4665 }
4666#endif
4667 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004668
4669#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004670 if ( wp->w_p_cole > 0
4671 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4672 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004673 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004674 && !(lnum_in_visual_area
4675 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004676 {
4677 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004678 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004679 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4680 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004681 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004682 /* First time at this concealed item: display one
4683 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004684 if (syn_get_sub_char() != NUL)
4685 c = syn_get_sub_char();
4686 else if (lcs_conceal != NUL)
4687 c = lcs_conceal;
4688 else
4689 c = ' ';
4690
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004691 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004692
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004693 if (n_extra > 0)
4694 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004695 vcol += n_extra;
4696 if (wp->w_p_wrap && n_extra > 0)
4697 {
4698# ifdef FEAT_RIGHTLEFT
4699 if (wp->w_p_rl)
4700 {
4701 col -= n_extra;
4702 boguscols -= n_extra;
4703 }
4704 else
4705# endif
4706 {
4707 boguscols += n_extra;
4708 col += n_extra;
4709 }
4710 }
4711 n_extra = 0;
4712 n_attr = 0;
4713 }
4714 else if (n_skip == 0)
4715 {
4716 is_concealing = TRUE;
4717 n_skip = 1;
4718 }
4719# ifdef FEAT_MBYTE
4720 mb_c = c;
4721 if (enc_utf8 && (*mb_char2len)(c) > 1)
4722 {
4723 mb_utf8 = TRUE;
4724 u8cc[0] = 0;
4725 c = 0xc0;
4726 }
4727 else
4728 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4729# endif
4730 }
4731 else
4732 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004733 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004734 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004735 }
4736#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
4738
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004739#ifdef FEAT_CONCEAL
4740 /* In the cursor line and we may be concealing characters: correct
4741 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004742 if (!did_wcol && draw_state == WL_LINE
4743 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004744 && conceal_cursor_line(wp)
4745 && (int)wp->w_virtcol <= vcol + n_skip)
4746 {
4747 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004748 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004749 did_wcol = TRUE;
4750 }
4751#endif
4752
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 /* Don't override visual selection highlighting. */
4754 if (n_attr > 0
4755 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004756 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 char_attr = extra_attr;
4758
Bram Moolenaar81695252004-12-29 20:58:21 +00004759#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 /* XIM don't send preedit_start and preedit_end, but they send
4761 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4762 * im_is_preediting() here. */
4763 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004764 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 && (State & INSERT)
4766 && !p_imdisable
4767 && im_is_preediting()
4768 && draw_state == WL_LINE)
4769 {
4770 colnr_T tcol;
4771
4772 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004773 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 else
4775 tcol = preedit_end_col;
4776 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4777 {
4778 if (feedback_old_attr < 0)
4779 {
4780 feedback_col = 0;
4781 feedback_old_attr = char_attr;
4782 }
4783 char_attr = im_get_feedback_attr(feedback_col);
4784 if (char_attr < 0)
4785 char_attr = feedback_old_attr;
4786 feedback_col++;
4787 }
4788 else if (feedback_old_attr >= 0)
4789 {
4790 char_attr = feedback_old_attr;
4791 feedback_old_attr = -1;
4792 feedback_col = 0;
4793 }
4794 }
4795#endif
4796 /*
4797 * Handle the case where we are in column 0 but not on the first
4798 * character of the line and the user wants us to show us a
4799 * special character (via 'listchars' option "precedes:<char>".
4800 */
4801 if (lcs_prec_todo != NUL
4802 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4803#ifdef FEAT_DIFF
4804 && filler_todo <= 0
4805#endif
4806 && draw_state > WL_NR
4807 && c != NUL)
4808 {
4809 c = lcs_prec;
4810 lcs_prec_todo = NUL;
4811#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004812 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4813 {
4814 /* Double-width character being overwritten by the "precedes"
4815 * character, need to fill up half the character. */
4816 c_extra = MB_FILLER_CHAR;
4817 n_extra = 1;
4818 n_attr = 2;
4819 extra_attr = hl_attr(HLF_AT);
4820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 mb_c = c;
4822 if (enc_utf8 && (*mb_char2len)(c) > 1)
4823 {
4824 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004825 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004826 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828 else
4829 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4830#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004831 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
4833 saved_attr3 = char_attr; /* save current attr */
4834 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4835 n_attr3 = 1;
4836 }
4837 }
4838
4839 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004840 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004842 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004843#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004844 || did_line_attr == 1
4845#endif
4846 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004848#ifdef FEAT_SEARCH_EXTRA
4849 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004850
4851 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004852 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004853 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004854#endif
4855
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004856 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 * highlight match at end of line. If it's beyond the last
4858 * char on the screen, just overwrite that one (tricky!) Not
4859 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004860#ifdef FEAT_SEARCH_EXTRA
4861 prevcol_hl_flag = FALSE;
4862 if (prevcol == (long)search_hl.startcol)
4863 prevcol_hl_flag = TRUE;
4864 else
4865 {
4866 cur = wp->w_match_head;
4867 while (cur != NULL)
4868 {
4869 if (prevcol == (long)cur->hl.startcol)
4870 {
4871 prevcol_hl_flag = TRUE;
4872 break;
4873 }
4874 cur = cur->next;
4875 }
4876 }
4877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004879 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004880 && (VIsual_mode != Ctrl_V
4881 || lnum == VIsual.lnum
4882 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004883 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#ifdef FEAT_SEARCH_EXTRA
4885 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004886 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004887# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004888 && did_line_attr <= 1
4889# endif
4890 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#endif
4892 ))
4893 {
4894 int n = 0;
4895
4896#ifdef FEAT_RIGHTLEFT
4897 if (wp->w_p_rl)
4898 {
4899 if (col < 0)
4900 n = 1;
4901 }
4902 else
4903#endif
4904 {
4905 if (col >= W_WIDTH(wp))
4906 n = -1;
4907 }
4908 if (n != 0)
4909 {
4910 /* At the window boundary, highlight the last character
4911 * instead (better than nothing). */
4912 off += n;
4913 col += n;
4914 }
4915 else
4916 {
4917 /* Add a blank character to highlight. */
4918 ScreenLines[off] = ' ';
4919#ifdef FEAT_MBYTE
4920 if (enc_utf8)
4921 ScreenLinesUC[off] = 0;
4922#endif
4923 }
4924#ifdef FEAT_SEARCH_EXTRA
4925 if (area_attr == 0)
4926 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004927 /* Use attributes from match with highest priority among
4928 * 'search_hl' and the match list. */
4929 char_attr = search_hl.attr;
4930 cur = wp->w_match_head;
4931 shl_flag = FALSE;
4932 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004933 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004934 if (shl_flag == FALSE
4935 && ((cur != NULL
4936 && cur->priority > SEARCH_HL_PRIORITY)
4937 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004938 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004939 shl = &search_hl;
4940 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004941 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004942 else
4943 shl = &cur->hl;
4944 if ((ptr - line) - 1 == (long)shl->startcol)
4945 char_attr = shl->attr;
4946 if (shl != &search_hl && cur != NULL)
4947 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950#endif
4951 ScreenAttrs[off] = char_attr;
4952#ifdef FEAT_RIGHTLEFT
4953 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004956 --off;
4957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 else
4959#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004962 ++off;
4963 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004964 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004965#ifdef FEAT_SYN_HL
4966 eol_hl_off = 1;
4967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
Bram Moolenaar91170f82006-05-05 21:15:17 +00004971 /*
4972 * At end of the text line.
4973 */
4974 if (c == NUL)
4975 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004976#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004977 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4978 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004979 {
4980 /* highlight last char after line */
4981 --col;
4982 --off;
4983 --vcol;
4984 }
4985
Bram Moolenaar1a384422010-07-14 19:53:30 +02004986 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004987 if (wp->w_p_wrap)
4988 v = wp->w_skipcol;
4989 else
4990 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004991
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004992 /* check if line ends before left margin */
4993 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004994 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004995#ifdef FEAT_CONCEAL
4996 /* Get rid of the boguscols now, we want to draw until the right
4997 * edge for 'cursorcolumn'. */
4998 col -= boguscols;
4999 boguscols = 0;
5000#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005001
5002 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005003 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005004
5005 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005006 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5007 && (int)wp->w_virtcol <
5008 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005009 && lnum != wp->w_cursor.lnum)
5010 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005011# ifdef FEAT_RIGHTLEFT
5012 && !wp->w_p_rl
5013# endif
5014 )
5015 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005016 int rightmost_vcol = 0;
5017 int i;
5018
5019 if (wp->w_p_cuc)
5020 rightmost_vcol = wp->w_virtcol;
5021 if (draw_color_col)
5022 /* determine rightmost colorcolumn to possibly draw */
5023 for (i = 0; color_cols[i] >= 0; ++i)
5024 if (rightmost_vcol < color_cols[i])
5025 rightmost_vcol = color_cols[i];
5026
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005027 while (col < W_WIDTH(wp))
5028 {
5029 ScreenLines[off] = ' ';
5030#ifdef FEAT_MBYTE
5031 if (enc_utf8)
5032 ScreenLinesUC[off] = 0;
5033#endif
5034 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005035 if (draw_color_col)
5036 draw_color_col = advance_color_col(VCOL_HLC,
5037 &color_cols);
5038
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005039 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005040 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005041 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005042 ScreenAttrs[off++] = hl_attr(HLF_MC);
5043 else
5044 ScreenAttrs[off++] = 0;
5045
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005046 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005047 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005048
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005049 ++vcol;
5050 }
5051 }
5052#endif
5053
Bram Moolenaar860cae12010-06-05 23:22:07 +02005054 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5055 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 row++;
5057
5058 /*
5059 * Update w_cline_height and w_cline_folded if the cursor line was
5060 * updated (saves a call to plines() later).
5061 */
5062 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5063 {
5064 curwin->w_cline_row = startrow;
5065 curwin->w_cline_height = row - startrow;
5066#ifdef FEAT_FOLDING
5067 curwin->w_cline_folded = FALSE;
5068#endif
5069 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5070 }
5071
5072 break;
5073 }
5074
5075 /* line continues beyond line end */
5076 if (lcs_ext
5077 && !wp->w_p_wrap
5078#ifdef FEAT_DIFF
5079 && filler_todo <= 0
5080#endif
5081 && (
5082#ifdef FEAT_RIGHTLEFT
5083 wp->w_p_rl ? col == 0 :
5084#endif
5085 col == W_WIDTH(wp) - 1)
5086 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005087 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5089 {
5090 c = lcs_ext;
5091 char_attr = hl_attr(HLF_AT);
5092#ifdef FEAT_MBYTE
5093 mb_c = c;
5094 if (enc_utf8 && (*mb_char2len)(c) > 1)
5095 {
5096 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005097 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005098 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100 else
5101 mb_utf8 = FALSE;
5102#endif
5103 }
5104
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005105#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005106 /* advance to the next 'colorcolumn' */
5107 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005108 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005109
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005110 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005111 * highlight the cursor position itself.
5112 * Also highlight the 'colorcolumn' if it is different than
5113 * 'cursorcolumn' */
5114 vcol_save_attr = -1;
5115 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005116 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005117 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005118 && lnum != wp->w_cursor.lnum)
5119 {
5120 vcol_save_attr = char_attr;
5121 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5122 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005123 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005124 {
5125 vcol_save_attr = char_attr;
5126 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5127 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005128 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005129#endif
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 /*
5132 * Store character to be displayed.
5133 * Skip characters that are left of the screen for 'nowrap'.
5134 */
5135 vcol_prev = vcol;
5136 if (draw_state < WL_LINE || n_skip <= 0)
5137 {
5138 /*
5139 * Store the character.
5140 */
5141#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5142 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5143 {
5144 /* A double-wide character is: put first halve in left cell. */
5145 --off;
5146 --col;
5147 }
5148#endif
5149 ScreenLines[off] = c;
5150#ifdef FEAT_MBYTE
5151 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005152 {
5153 if ((mb_c & 0xff00) == 0x8e00)
5154 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 else if (enc_utf8)
5158 {
5159 if (mb_utf8)
5160 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005161 int i;
5162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005164 if ((c & 0xff) == 0)
5165 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005166 for (i = 0; i < Screen_mco; ++i)
5167 {
5168 ScreenLinesC[i][off] = u8cc[i];
5169 if (u8cc[i] == 0)
5170 break;
5171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173 else
5174 ScreenLinesUC[off] = 0;
5175 }
5176 if (multi_attr)
5177 {
5178 ScreenAttrs[off] = multi_attr;
5179 multi_attr = 0;
5180 }
5181 else
5182#endif
5183 ScreenAttrs[off] = char_attr;
5184
5185#ifdef FEAT_MBYTE
5186 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5187 {
5188 /* Need to fill two screen columns. */
5189 ++off;
5190 ++col;
5191 if (enc_utf8)
5192 /* UTF-8: Put a 0 in the second screen char. */
5193 ScreenLines[off] = 0;
5194 else
5195 /* DBCS: Put second byte in the second screen char. */
5196 ScreenLines[off] = mb_c & 0xff;
5197 ++vcol;
5198 /* When "tocol" is halfway a character, set it to the end of
5199 * the character, otherwise highlighting won't stop. */
5200 if (tocol == vcol)
5201 ++tocol;
5202#ifdef FEAT_RIGHTLEFT
5203 if (wp->w_p_rl)
5204 {
5205 /* now it's time to backup one cell */
5206 --off;
5207 --col;
5208 }
5209#endif
5210 }
5211#endif
5212#ifdef FEAT_RIGHTLEFT
5213 if (wp->w_p_rl)
5214 {
5215 --off;
5216 --col;
5217 }
5218 else
5219#endif
5220 {
5221 ++off;
5222 ++col;
5223 }
5224 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005225#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005226 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005227 {
5228 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005229 ++vcol_off;
5230 if (n_extra > 0)
5231 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005232 if (wp->w_p_wrap)
5233 {
5234 /*
5235 * Special voodoo required if 'wrap' is on.
5236 *
5237 * Advance the column indicator to force the line
5238 * drawing to wrap early. This will make the line
5239 * take up the same screen space when parts are concealed,
5240 * so that cursor line computations aren't messed up.
5241 *
5242 * To avoid the fictitious advance of 'col' causing
5243 * trailing junk to be written out of the screen line
5244 * we are building, 'boguscols' keeps track of the number
5245 * of bad columns we have advanced.
5246 */
5247 if (n_extra > 0)
5248 {
5249 vcol += n_extra;
5250# ifdef FEAT_RIGHTLEFT
5251 if (wp->w_p_rl)
5252 {
5253 col -= n_extra;
5254 boguscols -= n_extra;
5255 }
5256 else
5257# endif
5258 {
5259 col += n_extra;
5260 boguscols += n_extra;
5261 }
5262 n_extra = 0;
5263 n_attr = 0;
5264 }
5265
5266
5267# ifdef FEAT_MBYTE
5268 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5269 {
5270 /* Need to fill two screen columns. */
5271# ifdef FEAT_RIGHTLEFT
5272 if (wp->w_p_rl)
5273 {
5274 --boguscols;
5275 --col;
5276 }
5277 else
5278# endif
5279 {
5280 ++boguscols;
5281 ++col;
5282 }
5283 }
5284# endif
5285
5286# ifdef FEAT_RIGHTLEFT
5287 if (wp->w_p_rl)
5288 {
5289 --boguscols;
5290 --col;
5291 }
5292 else
5293# endif
5294 {
5295 ++boguscols;
5296 ++col;
5297 }
5298 }
5299 else
5300 {
5301 if (n_extra > 0)
5302 {
5303 vcol += n_extra;
5304 n_extra = 0;
5305 n_attr = 0;
5306 }
5307 }
5308
5309 }
5310#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 else
5312 --n_skip;
5313
Bram Moolenaar64486672010-05-16 15:46:46 +02005314 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5315 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005316 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#ifdef FEAT_DIFF
5318 && filler_todo <= 0
5319#endif
5320 )
5321 ++vcol;
5322
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005323#ifdef FEAT_SYN_HL
5324 if (vcol_save_attr >= 0)
5325 char_attr = vcol_save_attr;
5326#endif
5327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 /* restore attributes after "predeces" in 'listchars' */
5329 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5330 char_attr = saved_attr3;
5331
5332 /* restore attributes after last 'listchars' or 'number' char */
5333 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5334 char_attr = saved_attr2;
5335
5336 /*
5337 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005338 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 */
5340 if ((
5341#ifdef FEAT_RIGHTLEFT
5342 wp->w_p_rl ? (col < 0) :
5343#endif
5344 (col >= W_WIDTH(wp)))
5345 && (*ptr != NUL
5346#ifdef FEAT_DIFF
5347 || filler_todo > 0
5348#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005349 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5351 )
5352 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005353#ifdef FEAT_CONCEAL
5354 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5355 (int)W_WIDTH(wp), wp->w_p_rl);
5356 boguscols = 0;
5357#else
5358 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5359 (int)W_WIDTH(wp), wp->w_p_rl);
5360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 ++row;
5362 ++screen_row;
5363
5364 /* When not wrapping and finished diff lines, or when displayed
5365 * '$' and highlighting until last column, break here. */
5366 if ((!wp->w_p_wrap
5367#ifdef FEAT_DIFF
5368 && filler_todo <= 0
5369#endif
5370 ) || lcs_eol_one == -1)
5371 break;
5372
5373 /* When the window is too narrow draw all "@" lines. */
5374 if (draw_state != WL_LINE
5375#ifdef FEAT_DIFF
5376 && filler_todo <= 0
5377#endif
5378 )
5379 {
5380 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5381#ifdef FEAT_VERTSPLIT
5382 draw_vsep_win(wp, row);
5383#endif
5384 row = endrow;
5385 }
5386
5387 /* When line got too long for screen break here. */
5388 if (row == endrow)
5389 {
5390 ++row;
5391 break;
5392 }
5393
5394 if (screen_cur_row == screen_row - 1
5395#ifdef FEAT_DIFF
5396 && filler_todo <= 0
5397#endif
5398 && W_WIDTH(wp) == Columns)
5399 {
5400 /* Remember that the line wraps, used for modeless copy. */
5401 LineWraps[screen_row - 1] = TRUE;
5402
5403 /*
5404 * Special trick to make copy/paste of wrapped lines work with
5405 * xterm/screen: write an extra character beyond the end of
5406 * the line. This will work with all terminal types
5407 * (regardless of the xn,am settings).
5408 * Only do this on a fast tty.
5409 * Only do this if the cursor is on the current line
5410 * (something has been written in it).
5411 * Don't do this for the GUI.
5412 * Don't do this for double-width characters.
5413 * Don't do this for a window not at the right screen border.
5414 */
5415 if (p_tf
5416#ifdef FEAT_GUI
5417 && !gui.in_use
5418#endif
5419#ifdef FEAT_MBYTE
5420 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005421 && ((*mb_off2cells)(LineOffset[screen_row],
5422 LineOffset[screen_row] + screen_Columns)
5423 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005425 + (int)Columns - 2,
5426 LineOffset[screen_row] + screen_Columns)
5427 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#endif
5429 )
5430 {
5431 /* First make sure we are at the end of the screen line,
5432 * then output the same character again to let the
5433 * terminal know about the wrap. If the terminal doesn't
5434 * auto-wrap, we overwrite the character. */
5435 if (screen_cur_col != W_WIDTH(wp))
5436 screen_char(LineOffset[screen_row - 1]
5437 + (unsigned)Columns - 1,
5438 screen_row - 1, (int)(Columns - 1));
5439
5440#ifdef FEAT_MBYTE
5441 /* When there is a multi-byte character, just output a
5442 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005443 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5444 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 out_char(' ');
5446 else
5447#endif
5448 out_char(ScreenLines[LineOffset[screen_row - 1]
5449 + (Columns - 1)]);
5450 /* force a redraw of the first char on the next line */
5451 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5452 screen_start(); /* don't know where cursor is now */
5453 }
5454 }
5455
5456 col = 0;
5457 off = (unsigned)(current_ScreenLine - ScreenLines);
5458#ifdef FEAT_RIGHTLEFT
5459 if (wp->w_p_rl)
5460 {
5461 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5462 off += col;
5463 }
5464#endif
5465
5466 /* reset the drawing state for the start of a wrapped line */
5467 draw_state = WL_START;
5468 saved_n_extra = n_extra;
5469 saved_p_extra = p_extra;
5470 saved_c_extra = c_extra;
5471 saved_char_attr = char_attr;
5472 n_extra = 0;
5473 lcs_prec_todo = lcs_prec;
5474#ifdef FEAT_LINEBREAK
5475# ifdef FEAT_DIFF
5476 if (filler_todo <= 0)
5477# endif
5478 need_showbreak = TRUE;
5479#endif
5480#ifdef FEAT_DIFF
5481 --filler_todo;
5482 /* When the filler lines are actually below the last line of the
5483 * file, don't draw the line itself, break here. */
5484 if (filler_todo == 0 && wp->w_botfill)
5485 break;
5486#endif
5487 }
5488
5489 } /* for every character in the line */
5490
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005491#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005492 /* After an empty line check first word for capital. */
5493 if (*skipwhite(line) == NUL)
5494 {
5495 capcol_lnum = lnum + 1;
5496 cap_col = 0;
5497 }
5498#endif
5499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 return row;
5501}
5502
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005503#ifdef FEAT_MBYTE
5504static int comp_char_differs __ARGS((int, int));
5505
5506/*
5507 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005508 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005509 */
5510 static int
5511comp_char_differs(off_from, off_to)
5512 int off_from;
5513 int off_to;
5514{
5515 int i;
5516
5517 for (i = 0; i < Screen_mco; ++i)
5518 {
5519 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5520 return TRUE;
5521 if (ScreenLinesC[i][off_from] == 0)
5522 break;
5523 }
5524 return FALSE;
5525}
5526#endif
5527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528/*
5529 * Check whether the given character needs redrawing:
5530 * - the (first byte of the) character is different
5531 * - the attributes are different
5532 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005533 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 */
5535 static int
5536char_needs_redraw(off_from, off_to, cols)
5537 int off_from;
5538 int off_to;
5539 int cols;
5540{
5541 if (cols > 0
5542 && ((ScreenLines[off_from] != ScreenLines[off_to]
5543 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5544
5545#ifdef FEAT_MBYTE
5546 || (enc_dbcs != 0
5547 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5548 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5549 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5550 : (cols > 1 && ScreenLines[off_from + 1]
5551 != ScreenLines[off_to + 1])))
5552 || (enc_utf8
5553 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5554 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005555 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005556 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5557 && ScreenLines[off_from + 1]
5558 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559#endif
5560 ))
5561 return TRUE;
5562 return FALSE;
5563}
5564
5565/*
5566 * Move one "cooked" screen line to the screen, but only the characters that
5567 * have actually changed. Handle insert/delete character.
5568 * "coloff" gives the first column on the screen for this line.
5569 * "endcol" gives the columns where valid characters are.
5570 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5571 * needs to be cleared, negative otherwise.
5572 * "rlflag" is TRUE in a rightleft window:
5573 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5574 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5575 */
5576 static void
5577screen_line(row, coloff, endcol, clear_width
5578#ifdef FEAT_RIGHTLEFT
5579 , rlflag
5580#endif
5581 )
5582 int row;
5583 int coloff;
5584 int endcol;
5585 int clear_width;
5586#ifdef FEAT_RIGHTLEFT
5587 int rlflag;
5588#endif
5589{
5590 unsigned off_from;
5591 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005592#ifdef FEAT_MBYTE
5593 unsigned max_off_from;
5594 unsigned max_off_to;
5595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int col = 0;
5597#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5598 int hl;
5599#endif
5600 int force = FALSE; /* force update rest of the line */
5601 int redraw_this /* bool: does character need redraw? */
5602#ifdef FEAT_GUI
5603 = TRUE /* For GUI when while-loop empty */
5604#endif
5605 ;
5606 int redraw_next; /* redraw_this for next character */
5607#ifdef FEAT_MBYTE
5608 int clear_next = FALSE;
5609 int char_cells; /* 1: normal char */
5610 /* 2: occupies two display cells */
5611# define CHAR_CELLS char_cells
5612#else
5613# define CHAR_CELLS 1
5614#endif
5615
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005616 /* Check for illegal row and col, just in case. */
5617 if (row >= Rows)
5618 row = Rows - 1;
5619 if (endcol > Columns)
5620 endcol = Columns;
5621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622# ifdef FEAT_CLIPBOARD
5623 clip_may_clear_selection(row, row);
5624# endif
5625
5626 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5627 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005628#ifdef FEAT_MBYTE
5629 max_off_from = off_from + screen_Columns;
5630 max_off_to = LineOffset[row] + screen_Columns;
5631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
5633#ifdef FEAT_RIGHTLEFT
5634 if (rlflag)
5635 {
5636 /* Clear rest first, because it's left of the text. */
5637 if (clear_width > 0)
5638 {
5639 while (col <= endcol && ScreenLines[off_to] == ' '
5640 && ScreenAttrs[off_to] == 0
5641# ifdef FEAT_MBYTE
5642 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5643# endif
5644 )
5645 {
5646 ++off_to;
5647 ++col;
5648 }
5649 if (col <= endcol)
5650 screen_fill(row, row + 1, col + coloff,
5651 endcol + coloff + 1, ' ', ' ', 0);
5652 }
5653 col = endcol + 1;
5654 off_to = LineOffset[row] + col + coloff;
5655 off_from += col;
5656 endcol = (clear_width > 0 ? clear_width : -clear_width);
5657 }
5658#endif /* FEAT_RIGHTLEFT */
5659
5660 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5661
5662 while (col < endcol)
5663 {
5664#ifdef FEAT_MBYTE
5665 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005666 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 else
5668 char_cells = 1;
5669#endif
5670
5671 redraw_this = redraw_next;
5672 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5673 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5674
5675#ifdef FEAT_GUI
5676 /* If the next character was bold, then redraw the current character to
5677 * remove any pixels that might have spilt over into us. This only
5678 * happens in the GUI.
5679 */
5680 if (redraw_next && gui.in_use)
5681 {
5682 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005683 if (hl > HL_ALL)
5684 hl = syn_attr2attr(hl);
5685 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 redraw_this = TRUE;
5687 }
5688#endif
5689
5690 if (redraw_this)
5691 {
5692 /*
5693 * Special handling when 'xs' termcap flag set (hpterm):
5694 * Attributes for characters are stored at the position where the
5695 * cursor is when writing the highlighting code. The
5696 * start-highlighting code must be written with the cursor on the
5697 * first highlighted character. The stop-highlighting code must
5698 * be written with the cursor just after the last highlighted
5699 * character.
5700 * Overwriting a character doesn't remove it's highlighting. Need
5701 * to clear the rest of the line, and force redrawing it
5702 * completely.
5703 */
5704 if ( p_wiv
5705 && !force
5706#ifdef FEAT_GUI
5707 && !gui.in_use
5708#endif
5709 && ScreenAttrs[off_to] != 0
5710 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5711 {
5712 /*
5713 * Need to remove highlighting attributes here.
5714 */
5715 windgoto(row, col + coloff);
5716 out_str(T_CE); /* clear rest of this screen line */
5717 screen_start(); /* don't know where cursor is now */
5718 force = TRUE; /* force redraw of rest of the line */
5719 redraw_next = TRUE; /* or else next char would miss out */
5720
5721 /*
5722 * If the previous character was highlighted, need to stop
5723 * highlighting at this character.
5724 */
5725 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5726 {
5727 screen_attr = ScreenAttrs[off_to - 1];
5728 term_windgoto(row, col + coloff);
5729 screen_stop_highlight();
5730 }
5731 else
5732 screen_attr = 0; /* highlighting has stopped */
5733 }
5734#ifdef FEAT_MBYTE
5735 if (enc_dbcs != 0)
5736 {
5737 /* Check if overwriting a double-byte with a single-byte or
5738 * the other way around requires another character to be
5739 * redrawn. For UTF-8 this isn't needed, because comparing
5740 * ScreenLinesUC[] is sufficient. */
5741 if (char_cells == 1
5742 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005743 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
5745 /* Writing a single-cell character over a double-cell
5746 * character: need to redraw the next cell. */
5747 ScreenLines[off_to + 1] = 0;
5748 redraw_next = TRUE;
5749 }
5750 else if (char_cells == 2
5751 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005752 && (*mb_off2cells)(off_to, max_off_to) == 1
5753 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755 /* Writing the second half of a double-cell character over
5756 * a double-cell character: need to redraw the second
5757 * cell. */
5758 ScreenLines[off_to + 2] = 0;
5759 redraw_next = TRUE;
5760 }
5761
5762 if (enc_dbcs == DBCS_JPNU)
5763 ScreenLines2[off_to] = ScreenLines2[off_from];
5764 }
5765 /* When writing a single-width character over a double-width
5766 * character and at the end of the redrawn text, need to clear out
5767 * the right halve of the old character.
5768 * Also required when writing the right halve of a double-width
5769 * char over the left halve of an existing one. */
5770 if (has_mbyte && col + char_cells == endcol
5771 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005772 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005774 && (*mb_off2cells)(off_to, max_off_to) == 1
5775 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 clear_next = TRUE;
5777#endif
5778
5779 ScreenLines[off_to] = ScreenLines[off_from];
5780#ifdef FEAT_MBYTE
5781 if (enc_utf8)
5782 {
5783 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5784 if (ScreenLinesUC[off_from] != 0)
5785 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005786 int i;
5787
5788 for (i = 0; i < Screen_mco; ++i)
5789 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791 }
5792 if (char_cells == 2)
5793 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5794#endif
5795
5796#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005797 /* The bold trick makes a single column of pixels appear in the
5798 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 * character should be redrawn too. This happens for our own GUI
5800 * and for some xterms. */
5801 if (
5802# ifdef FEAT_GUI
5803 gui.in_use
5804# endif
5805# if defined(FEAT_GUI) && defined(UNIX)
5806 ||
5807# endif
5808# ifdef UNIX
5809 term_is_xterm
5810# endif
5811 )
5812 {
5813 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005814 if (hl > HL_ALL)
5815 hl = syn_attr2attr(hl);
5816 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 redraw_next = TRUE;
5818 }
5819#endif
5820 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5821#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005822 /* For simplicity set the attributes of second half of a
5823 * double-wide character equal to the first half. */
5824 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005826
5827 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 else
5830#endif
5831 screen_char(off_to, row, col + coloff);
5832 }
5833 else if ( p_wiv
5834#ifdef FEAT_GUI
5835 && !gui.in_use
5836#endif
5837 && col + coloff > 0)
5838 {
5839 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5840 {
5841 /*
5842 * Don't output stop-highlight when moving the cursor, it will
5843 * stop the highlighting when it should continue.
5844 */
5845 screen_attr = 0;
5846 }
5847 else if (screen_attr != 0)
5848 screen_stop_highlight();
5849 }
5850
5851 off_to += CHAR_CELLS;
5852 off_from += CHAR_CELLS;
5853 col += CHAR_CELLS;
5854 }
5855
5856#ifdef FEAT_MBYTE
5857 if (clear_next)
5858 {
5859 /* Clear the second half of a double-wide character of which the left
5860 * half was overwritten with a single-wide character. */
5861 ScreenLines[off_to] = ' ';
5862 if (enc_utf8)
5863 ScreenLinesUC[off_to] = 0;
5864 screen_char(off_to, row, col + coloff);
5865 }
5866#endif
5867
5868 if (clear_width > 0
5869#ifdef FEAT_RIGHTLEFT
5870 && !rlflag
5871#endif
5872 )
5873 {
5874#ifdef FEAT_GUI
5875 int startCol = col;
5876#endif
5877
5878 /* blank out the rest of the line */
5879 while (col < clear_width && ScreenLines[off_to] == ' '
5880 && ScreenAttrs[off_to] == 0
5881#ifdef FEAT_MBYTE
5882 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5883#endif
5884 )
5885 {
5886 ++off_to;
5887 ++col;
5888 }
5889 if (col < clear_width)
5890 {
5891#ifdef FEAT_GUI
5892 /*
5893 * In the GUI, clearing the rest of the line may leave pixels
5894 * behind if the first character cleared was bold. Some bold
5895 * fonts spill over the left. In this case we redraw the previous
5896 * character too. If we didn't skip any blanks above, then we
5897 * only redraw if the character wasn't already redrawn anyway.
5898 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005899 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 {
5901 hl = ScreenAttrs[off_to];
5902 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005903 {
5904 int prev_cells = 1;
5905# ifdef FEAT_MBYTE
5906 if (enc_utf8)
5907 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5908 * that its width is 2. */
5909 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5910 else if (enc_dbcs != 0)
5911 {
5912 /* find previous character by counting from first
5913 * column and get its width. */
5914 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005915 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005916
5917 while (off < off_to)
5918 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005919 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005920 off += prev_cells;
5921 }
5922 }
5923
5924 if (enc_dbcs != 0 && prev_cells > 1)
5925 screen_char_2(off_to - prev_cells, row,
5926 col + coloff - prev_cells);
5927 else
5928# endif
5929 screen_char(off_to - prev_cells, row,
5930 col + coloff - prev_cells);
5931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 }
5933#endif
5934 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5935 ' ', ' ', 0);
5936#ifdef FEAT_VERTSPLIT
5937 off_to += clear_width - col;
5938 col = clear_width;
5939#endif
5940 }
5941 }
5942
5943 if (clear_width > 0)
5944 {
5945#ifdef FEAT_VERTSPLIT
5946 /* For a window that's left of another, draw the separator char. */
5947 if (col + coloff < Columns)
5948 {
5949 int c;
5950
5951 c = fillchar_vsep(&hl);
5952 if (ScreenLines[off_to] != c
5953# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005954 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5955 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956# endif
5957 || ScreenAttrs[off_to] != hl)
5958 {
5959 ScreenLines[off_to] = c;
5960 ScreenAttrs[off_to] = hl;
5961# ifdef FEAT_MBYTE
5962 if (enc_utf8)
5963 {
5964 if (c >= 0x80)
5965 {
5966 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005967 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 }
5969 else
5970 ScreenLinesUC[off_to] = 0;
5971 }
5972# endif
5973 screen_char(off_to, row, col + coloff);
5974 }
5975 }
5976 else
5977#endif
5978 LineWraps[row] = FALSE;
5979 }
5980}
5981
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005982#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005984 * Mirror text "str" for right-left displaying.
5985 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005987 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988rl_mirror(str)
5989 char_u *str;
5990{
5991 char_u *p1, *p2;
5992 int t;
5993
5994 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5995 {
5996 t = *p1;
5997 *p1 = *p2;
5998 *p2 = t;
5999 }
6000}
6001#endif
6002
6003#if defined(FEAT_WINDOWS) || defined(PROTO)
6004/*
6005 * mark all status lines for redraw; used after first :cd
6006 */
6007 void
6008status_redraw_all()
6009{
6010 win_T *wp;
6011
6012 for (wp = firstwin; wp; wp = wp->w_next)
6013 if (wp->w_status_height)
6014 {
6015 wp->w_redr_status = TRUE;
6016 redraw_later(VALID);
6017 }
6018}
6019
6020/*
6021 * mark all status lines of the current buffer for redraw
6022 */
6023 void
6024status_redraw_curbuf()
6025{
6026 win_T *wp;
6027
6028 for (wp = firstwin; wp; wp = wp->w_next)
6029 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6030 {
6031 wp->w_redr_status = TRUE;
6032 redraw_later(VALID);
6033 }
6034}
6035
6036/*
6037 * Redraw all status lines that need to be redrawn.
6038 */
6039 void
6040redraw_statuslines()
6041{
6042 win_T *wp;
6043
6044 for (wp = firstwin; wp; wp = wp->w_next)
6045 if (wp->w_redr_status)
6046 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006047 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006048 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049}
6050#endif
6051
6052#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6053/*
6054 * Redraw all status lines at the bottom of frame "frp".
6055 */
6056 void
6057win_redraw_last_status(frp)
6058 frame_T *frp;
6059{
6060 if (frp->fr_layout == FR_LEAF)
6061 frp->fr_win->w_redr_status = TRUE;
6062 else if (frp->fr_layout == FR_ROW)
6063 {
6064 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6065 win_redraw_last_status(frp);
6066 }
6067 else /* frp->fr_layout == FR_COL */
6068 {
6069 frp = frp->fr_child;
6070 while (frp->fr_next != NULL)
6071 frp = frp->fr_next;
6072 win_redraw_last_status(frp);
6073 }
6074}
6075#endif
6076
6077#ifdef FEAT_VERTSPLIT
6078/*
6079 * Draw the verticap separator right of window "wp" starting with line "row".
6080 */
6081 static void
6082draw_vsep_win(wp, row)
6083 win_T *wp;
6084 int row;
6085{
6086 int hl;
6087 int c;
6088
6089 if (wp->w_vsep_width)
6090 {
6091 /* draw the vertical separator right of this window */
6092 c = fillchar_vsep(&hl);
6093 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6094 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6095 c, ' ', hl);
6096 }
6097}
6098#endif
6099
6100#ifdef FEAT_WILDMENU
6101static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006102static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006105 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 */
6107 static int
6108status_match_len(xp, s)
6109 expand_T *xp;
6110 char_u *s;
6111{
6112 int len = 0;
6113
6114#ifdef FEAT_MENU
6115 int emenu = (xp->xp_context == EXPAND_MENUS
6116 || xp->xp_context == EXPAND_MENUNAMES);
6117
6118 /* Check for menu separators - replace with '|'. */
6119 if (emenu && menu_is_separator(s))
6120 return 1;
6121#endif
6122
6123 while (*s != NUL)
6124 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006125 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006126 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006127 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 }
6129
6130 return len;
6131}
6132
6133/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006134 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006135 * These are backslashes used for escaping. Do show backslashes in help tags.
6136 */
6137 static int
6138skip_status_match_char(xp, s)
6139 expand_T *xp;
6140 char_u *s;
6141{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006142 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006143#ifdef FEAT_MENU
6144 || ((xp->xp_context == EXPAND_MENUS
6145 || xp->xp_context == EXPAND_MENUNAMES)
6146 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6147#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006148 )
6149 {
6150#ifndef BACKSLASH_IN_FILENAME
6151 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6152 return 2;
6153#endif
6154 return 1;
6155 }
6156 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006157}
6158
6159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 * Show wildchar matches in the status line.
6161 * Show at least the "match" item.
6162 * We start at item 'first_match' in the list and show all matches that fit.
6163 *
6164 * If inversion is possible we use it. Else '=' characters are used.
6165 */
6166 void
6167win_redr_status_matches(xp, num_matches, matches, match, showtail)
6168 expand_T *xp;
6169 int num_matches;
6170 char_u **matches; /* list of matches */
6171 int match;
6172 int showtail;
6173{
6174#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6175 int row;
6176 char_u *buf;
6177 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006178 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 int fillchar;
6180 int attr;
6181 int i;
6182 int highlight = TRUE;
6183 char_u *selstart = NULL;
6184 int selstart_col = 0;
6185 char_u *selend = NULL;
6186 static int first_match = 0;
6187 int add_left = FALSE;
6188 char_u *s;
6189#ifdef FEAT_MENU
6190 int emenu;
6191#endif
6192#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6193 int l;
6194#endif
6195
6196 if (matches == NULL) /* interrupted completion? */
6197 return;
6198
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006199#ifdef FEAT_MBYTE
6200 if (has_mbyte)
6201 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6202 else
6203#endif
6204 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 if (buf == NULL)
6206 return;
6207
6208 if (match == -1) /* don't show match but original text */
6209 {
6210 match = 0;
6211 highlight = FALSE;
6212 }
6213 /* count 1 for the ending ">" */
6214 clen = status_match_len(xp, L_MATCH(match)) + 3;
6215 if (match == 0)
6216 first_match = 0;
6217 else if (match < first_match)
6218 {
6219 /* jumping left, as far as we can go */
6220 first_match = match;
6221 add_left = TRUE;
6222 }
6223 else
6224 {
6225 /* check if match fits on the screen */
6226 for (i = first_match; i < match; ++i)
6227 clen += status_match_len(xp, L_MATCH(i)) + 2;
6228 if (first_match > 0)
6229 clen += 2;
6230 /* jumping right, put match at the left */
6231 if ((long)clen > Columns)
6232 {
6233 first_match = match;
6234 /* if showing the last match, we can add some on the left */
6235 clen = 2;
6236 for (i = match; i < num_matches; ++i)
6237 {
6238 clen += status_match_len(xp, L_MATCH(i)) + 2;
6239 if ((long)clen >= Columns)
6240 break;
6241 }
6242 if (i == num_matches)
6243 add_left = TRUE;
6244 }
6245 }
6246 if (add_left)
6247 while (first_match > 0)
6248 {
6249 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6250 if ((long)clen >= Columns)
6251 break;
6252 --first_match;
6253 }
6254
6255 fillchar = fillchar_status(&attr, TRUE);
6256
6257 if (first_match == 0)
6258 {
6259 *buf = NUL;
6260 len = 0;
6261 }
6262 else
6263 {
6264 STRCPY(buf, "< ");
6265 len = 2;
6266 }
6267 clen = len;
6268
6269 i = first_match;
6270 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6271 {
6272 if (i == match)
6273 {
6274 selstart = buf + len;
6275 selstart_col = clen;
6276 }
6277
6278 s = L_MATCH(i);
6279 /* Check for menu separators - replace with '|' */
6280#ifdef FEAT_MENU
6281 emenu = (xp->xp_context == EXPAND_MENUS
6282 || xp->xp_context == EXPAND_MENUNAMES);
6283 if (emenu && menu_is_separator(s))
6284 {
6285 STRCPY(buf + len, transchar('|'));
6286 l = (int)STRLEN(buf + len);
6287 len += l;
6288 clen += l;
6289 }
6290 else
6291#endif
6292 for ( ; *s != NUL; ++s)
6293 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006294 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 clen += ptr2cells(s);
6296#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006297 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 {
6299 STRNCPY(buf + len, s, l);
6300 s += l - 1;
6301 len += l;
6302 }
6303 else
6304#endif
6305 {
6306 STRCPY(buf + len, transchar_byte(*s));
6307 len += (int)STRLEN(buf + len);
6308 }
6309 }
6310 if (i == match)
6311 selend = buf + len;
6312
6313 *(buf + len++) = ' ';
6314 *(buf + len++) = ' ';
6315 clen += 2;
6316 if (++i == num_matches)
6317 break;
6318 }
6319
6320 if (i != num_matches)
6321 {
6322 *(buf + len++) = '>';
6323 ++clen;
6324 }
6325
6326 buf[len] = NUL;
6327
6328 row = cmdline_row - 1;
6329 if (row >= 0)
6330 {
6331 if (wild_menu_showing == 0)
6332 {
6333 if (msg_scrolled > 0)
6334 {
6335 /* Put the wildmenu just above the command line. If there is
6336 * no room, scroll the screen one line up. */
6337 if (cmdline_row == Rows - 1)
6338 {
6339 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6340 ++msg_scrolled;
6341 }
6342 else
6343 {
6344 ++cmdline_row;
6345 ++row;
6346 }
6347 wild_menu_showing = WM_SCROLLED;
6348 }
6349 else
6350 {
6351 /* Create status line if needed by setting 'laststatus' to 2.
6352 * Set 'winminheight' to zero to avoid that the window is
6353 * resized. */
6354 if (lastwin->w_status_height == 0)
6355 {
6356 save_p_ls = p_ls;
6357 save_p_wmh = p_wmh;
6358 p_ls = 2;
6359 p_wmh = 0;
6360 last_status(FALSE);
6361 }
6362 wild_menu_showing = WM_SHOWN;
6363 }
6364 }
6365
6366 screen_puts(buf, row, 0, attr);
6367 if (selstart != NULL && highlight)
6368 {
6369 *selend = NUL;
6370 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6371 }
6372
6373 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6374 }
6375
6376#ifdef FEAT_VERTSPLIT
6377 win_redraw_last_status(topframe);
6378#else
6379 lastwin->w_redr_status = TRUE;
6380#endif
6381 vim_free(buf);
6382}
6383#endif
6384
6385#if defined(FEAT_WINDOWS) || defined(PROTO)
6386/*
6387 * Redraw the status line of window wp.
6388 *
6389 * If inversion is possible we use it. Else '=' characters are used.
6390 */
6391 void
6392win_redr_status(wp)
6393 win_T *wp;
6394{
6395 int row;
6396 char_u *p;
6397 int len;
6398 int fillchar;
6399 int attr;
6400 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006401 static int busy = FALSE;
6402
6403 /* It's possible to get here recursively when 'statusline' (indirectly)
6404 * invokes ":redrawstatus". Simply ignore the call then. */
6405 if (busy)
6406 return;
6407 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408
6409 wp->w_redr_status = FALSE;
6410 if (wp->w_status_height == 0)
6411 {
6412 /* no status line, can only be last window */
6413 redraw_cmdline = TRUE;
6414 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006415 else if (!redrawing()
6416#ifdef FEAT_INS_EXPAND
6417 /* don't update status line when popup menu is visible and may be
6418 * drawn over it */
6419 || pum_visible()
6420#endif
6421 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
6423 /* Don't redraw right now, do it later. */
6424 wp->w_redr_status = TRUE;
6425 }
6426#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006427 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 {
6429 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006430 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 }
6432#endif
6433 else
6434 {
6435 fillchar = fillchar_status(&attr, wp == curwin);
6436
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006437 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 p = NameBuff;
6439 len = (int)STRLEN(p);
6440
6441 if (wp->w_buffer->b_help
6442#ifdef FEAT_QUICKFIX
6443 || wp->w_p_pvw
6444#endif
6445 || bufIsChanged(wp->w_buffer)
6446 || wp->w_buffer->b_p_ro)
6447 *(p + len++) = ' ';
6448 if (wp->w_buffer->b_help)
6449 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006450 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 len += (int)STRLEN(p + len);
6452 }
6453#ifdef FEAT_QUICKFIX
6454 if (wp->w_p_pvw)
6455 {
6456 STRCPY(p + len, _("[Preview]"));
6457 len += (int)STRLEN(p + len);
6458 }
6459#endif
6460 if (bufIsChanged(wp->w_buffer))
6461 {
6462 STRCPY(p + len, "[+]");
6463 len += 3;
6464 }
6465 if (wp->w_buffer->b_p_ro)
6466 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006467 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 len += 4;
6469 }
6470
6471#ifndef FEAT_VERTSPLIT
6472 this_ru_col = ru_col;
6473 if (this_ru_col < (Columns + 1) / 2)
6474 this_ru_col = (Columns + 1) / 2;
6475#else
6476 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6477 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6478 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6479 if (this_ru_col <= 1)
6480 {
6481 p = (char_u *)"<"; /* No room for file name! */
6482 len = 1;
6483 }
6484 else
6485#endif
6486#ifdef FEAT_MBYTE
6487 if (has_mbyte)
6488 {
6489 int clen = 0, i;
6490
6491 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006492 clen = mb_string2cells(p, -1);
6493
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 /* Find first character that will fit.
6495 * Going from start to end is much faster for DBCS. */
6496 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006497 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 clen -= (*mb_ptr2cells)(p + i);
6499 len = clen;
6500 if (i > 0)
6501 {
6502 p = p + i - 1;
6503 *p = '<';
6504 ++len;
6505 }
6506
6507 }
6508 else
6509#endif
6510 if (len > this_ru_col - 1)
6511 {
6512 p += len - (this_ru_col - 1);
6513 *p = '<';
6514 len = this_ru_col - 1;
6515 }
6516
6517 row = W_WINROW(wp) + wp->w_height;
6518 screen_puts(p, row, W_WINCOL(wp), attr);
6519 screen_fill(row, row + 1, len + W_WINCOL(wp),
6520 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6521
6522 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6523 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6524 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6525 - 1 + W_WINCOL(wp)), attr);
6526
6527#ifdef FEAT_CMDL_INFO
6528 win_redr_ruler(wp, TRUE);
6529#endif
6530 }
6531
6532#ifdef FEAT_VERTSPLIT
6533 /*
6534 * May need to draw the character below the vertical separator.
6535 */
6536 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6537 {
6538 if (stl_connected(wp))
6539 fillchar = fillchar_status(&attr, wp == curwin);
6540 else
6541 fillchar = fillchar_vsep(&attr);
6542 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6543 attr);
6544 }
6545#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006546 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547}
6548
Bram Moolenaar238a5642006-02-21 22:12:05 +00006549#ifdef FEAT_STL_OPT
6550/*
6551 * Redraw the status line according to 'statusline' and take care of any
6552 * errors encountered.
6553 */
6554 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006555redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006556 win_T *wp;
6557{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006558 static int entered = FALSE;
6559 int save_called_emsg = called_emsg;
6560
6561 /* When called recursively return. This can happen when the statusline
6562 * contains an expression that triggers a redraw. */
6563 if (entered)
6564 return;
6565 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006566
6567 called_emsg = FALSE;
6568 win_redr_custom(wp, FALSE);
6569 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006570 {
6571 /* When there is an error disable the statusline, otherwise the
6572 * display is messed up with errors and a redraw triggers the problem
6573 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006574 set_string_option_direct((char_u *)"statusline", -1,
6575 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006576 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006577 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006578 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006579 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006580}
6581#endif
6582
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583# ifdef FEAT_VERTSPLIT
6584/*
6585 * Return TRUE if the status line of window "wp" is connected to the status
6586 * line of the window right of it. If not, then it's a vertical separator.
6587 * Only call if (wp->w_vsep_width != 0).
6588 */
6589 int
6590stl_connected(wp)
6591 win_T *wp;
6592{
6593 frame_T *fr;
6594
6595 fr = wp->w_frame;
6596 while (fr->fr_parent != NULL)
6597 {
6598 if (fr->fr_parent->fr_layout == FR_COL)
6599 {
6600 if (fr->fr_next != NULL)
6601 break;
6602 }
6603 else
6604 {
6605 if (fr->fr_next != NULL)
6606 return TRUE;
6607 }
6608 fr = fr->fr_parent;
6609 }
6610 return FALSE;
6611}
6612# endif
6613
6614#endif /* FEAT_WINDOWS */
6615
6616#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6617/*
6618 * Get the value to show for the language mappings, active 'keymap'.
6619 */
6620 int
6621get_keymap_str(wp, buf, len)
6622 win_T *wp;
6623 char_u *buf; /* buffer for the result */
6624 int len; /* length of buffer */
6625{
6626 char_u *p;
6627
6628 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6629 return FALSE;
6630
6631 {
6632#ifdef FEAT_EVAL
6633 buf_T *old_curbuf = curbuf;
6634 win_T *old_curwin = curwin;
6635 char_u *s;
6636
6637 curbuf = wp->w_buffer;
6638 curwin = wp;
6639 STRCPY(buf, "b:keymap_name"); /* must be writable */
6640 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006641 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 --emsg_skip;
6643 curbuf = old_curbuf;
6644 curwin = old_curwin;
6645 if (p == NULL || *p == NUL)
6646#endif
6647 {
6648#ifdef FEAT_KEYMAP
6649 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6650 p = wp->w_buffer->b_p_keymap;
6651 else
6652#endif
6653 p = (char_u *)"lang";
6654 }
6655 if ((int)(STRLEN(p) + 3) < len)
6656 sprintf((char *)buf, "<%s>", p);
6657 else
6658 buf[0] = NUL;
6659#ifdef FEAT_EVAL
6660 vim_free(s);
6661#endif
6662 }
6663 return buf[0] != NUL;
6664}
6665#endif
6666
6667#if defined(FEAT_STL_OPT) || defined(PROTO)
6668/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006669 * Redraw the status line or ruler of window "wp".
6670 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 */
6672 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006673win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006675 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006677 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 int attr;
6679 int curattr;
6680 int row;
6681 int col = 0;
6682 int maxwidth;
6683 int width;
6684 int n;
6685 int len;
6686 int fillchar;
6687 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006688 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006690 struct stl_hlrec hltab[STL_MAX_ITEM];
6691 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006692 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006693 win_T *ewp;
6694 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
Bram Moolenaar1d633412013-12-11 15:52:01 +01006696 /* There is a tiny chance that this gets called recursively: When
6697 * redrawing a status line triggers redrawing the ruler or tabline.
6698 * Avoid trouble by not allowing recursion. */
6699 if (entered)
6700 return;
6701 entered = TRUE;
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006704 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006706 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006707 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006708 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006709 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006710 attr = hl_attr(HLF_TPF);
6711 maxwidth = Columns;
6712# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006713 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006716 else
6717 {
6718 row = W_WINROW(wp) + wp->w_height;
6719 fillchar = fillchar_status(&attr, wp == curwin);
6720 maxwidth = W_WIDTH(wp);
6721
6722 if (draw_ruler)
6723 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006724 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006725 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006726 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006727 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006728 if (*++stl == '-')
6729 stl++;
6730 if (atoi((char *)stl))
6731 while (VIM_ISDIGIT(*stl))
6732 stl++;
6733 if (*stl++ != '(')
6734 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006735 }
6736#ifdef FEAT_VERTSPLIT
6737 col = ru_col - (Columns - W_WIDTH(wp));
6738 if (col < (W_WIDTH(wp) + 1) / 2)
6739 col = (W_WIDTH(wp) + 1) / 2;
6740#else
6741 col = ru_col;
6742 if (col > (Columns + 1) / 2)
6743 col = (Columns + 1) / 2;
6744#endif
6745 maxwidth = W_WIDTH(wp) - col;
6746#ifdef FEAT_WINDOWS
6747 if (!wp->w_status_height)
6748#endif
6749 {
6750 row = Rows - 1;
6751 --maxwidth; /* writing in last column may cause scrolling */
6752 fillchar = ' ';
6753 attr = 0;
6754 }
6755
6756# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006757 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006758# endif
6759 }
6760 else
6761 {
6762 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006763 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006764 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006765 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006766# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006767 use_sandbox = was_set_insecurely((char_u *)"statusline",
6768 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006769# endif
6770 }
6771
6772#ifdef FEAT_VERTSPLIT
6773 col += W_WINCOL(wp);
6774#endif
6775 }
6776
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006778 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
Bram Moolenaar61452852011-02-01 18:01:11 +01006780 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6781 * the cursor away and back. */
6782 ewp = wp == NULL ? curwin : wp;
6783 p_crb_save = ewp->w_p_crb;
6784 ewp->w_p_crb = FALSE;
6785
Bram Moolenaar362f3562009-11-03 16:20:34 +00006786 /* Make a copy, because the statusline may include a function call that
6787 * might change the option value and free the memory. */
6788 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006789 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006790 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006791 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006792 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006793 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006795 /* Make all characters printable. */
6796 p = transstr(buf);
6797 if (p != NULL)
6798 {
6799 vim_strncpy(buf, p, sizeof(buf) - 1);
6800 vim_free(p);
6801 }
6802
6803 /* fill up with "fillchar" */
6804 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006805 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
6807#ifdef FEAT_MBYTE
6808 len += (*mb_char2bytes)(fillchar, buf + len);
6809#else
6810 buf[len++] = fillchar;
6811#endif
6812 ++width;
6813 }
6814 buf[len] = NUL;
6815
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006816 /*
6817 * Draw each snippet with the specified highlighting.
6818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 curattr = attr;
6820 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006821 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006823 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 screen_puts_len(p, len, row, col, curattr);
6825 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006826 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006828 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006830 else if (hltab[n].userhl < 0)
6831 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006833 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006834 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835#endif
6836 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006837 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 }
6839 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006840
6841 if (wp == NULL)
6842 {
6843 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6844 col = 0;
6845 len = 0;
6846 p = buf;
6847 fillchar = 0;
6848 for (n = 0; tabtab[n].start != NULL; n++)
6849 {
6850 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6851 while (col < len)
6852 TabPageIdxs[col++] = fillchar;
6853 p = tabtab[n].start;
6854 fillchar = tabtab[n].userhl;
6855 }
6856 while (col < Columns)
6857 TabPageIdxs[col++] = fillchar;
6858 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006859
6860theend:
6861 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862}
6863
6864#endif /* FEAT_STL_OPT */
6865
6866/*
6867 * Output a single character directly to the screen and update ScreenLines.
6868 */
6869 void
6870screen_putchar(c, row, col, attr)
6871 int c;
6872 int row, col;
6873 int attr;
6874{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 char_u buf[MB_MAXBYTES + 1];
6876
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006877#ifdef FEAT_MBYTE
6878 if (has_mbyte)
6879 buf[(*mb_char2bytes)(c, buf)] = NUL;
6880 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006882 {
6883 buf[0] = c;
6884 buf[1] = NUL;
6885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 screen_puts(buf, row, col, attr);
6887}
6888
6889/*
6890 * Get a single character directly from ScreenLines into "bytes[]".
6891 * Also return its attribute in *attrp;
6892 */
6893 void
6894screen_getbytes(row, col, bytes, attrp)
6895 int row, col;
6896 char_u *bytes;
6897 int *attrp;
6898{
6899 unsigned off;
6900
6901 /* safety check */
6902 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6903 {
6904 off = LineOffset[row] + col;
6905 *attrp = ScreenAttrs[off];
6906 bytes[0] = ScreenLines[off];
6907 bytes[1] = NUL;
6908
6909#ifdef FEAT_MBYTE
6910 if (enc_utf8 && ScreenLinesUC[off] != 0)
6911 bytes[utfc_char2bytes(off, bytes)] = NUL;
6912 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6913 {
6914 bytes[0] = ScreenLines[off];
6915 bytes[1] = ScreenLines2[off];
6916 bytes[2] = NUL;
6917 }
6918 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6919 {
6920 bytes[1] = ScreenLines[off + 1];
6921 bytes[2] = NUL;
6922 }
6923#endif
6924 }
6925}
6926
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006927#ifdef FEAT_MBYTE
6928static int screen_comp_differs __ARGS((int, int*));
6929
6930/*
6931 * Return TRUE if composing characters for screen posn "off" differs from
6932 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006933 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006934 */
6935 static int
6936screen_comp_differs(off, u8cc)
6937 int off;
6938 int *u8cc;
6939{
6940 int i;
6941
6942 for (i = 0; i < Screen_mco; ++i)
6943 {
6944 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6945 return TRUE;
6946 if (u8cc[i] == 0)
6947 break;
6948 }
6949 return FALSE;
6950}
6951#endif
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953/*
6954 * Put string '*text' on the screen at position 'row' and 'col', with
6955 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6956 * Note: only outputs within one row, message is truncated at screen boundary!
6957 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6958 */
6959 void
6960screen_puts(text, row, col, attr)
6961 char_u *text;
6962 int row;
6963 int col;
6964 int attr;
6965{
6966 screen_puts_len(text, -1, row, col, attr);
6967}
6968
6969/*
6970 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6971 * a NUL.
6972 */
6973 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006974screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006976 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 int row;
6978 int col;
6979 int attr;
6980{
6981 unsigned off;
6982 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006983 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 int c;
6985#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006986 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 int mbyte_blen = 1;
6988 int mbyte_cells = 1;
6989 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006990 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 int clear_next_cell = FALSE;
6992# ifdef FEAT_ARABIC
6993 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006994 int pc, nc, nc1;
6995 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996# endif
6997#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006998#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6999 int force_redraw_this;
7000 int force_redraw_next = FALSE;
7001#endif
7002 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7005 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007006 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
Bram Moolenaarc236c162008-07-13 17:41:49 +00007008#ifdef FEAT_MBYTE
7009 /* When drawing over the right halve of a double-wide char clear out the
7010 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007011 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007012# ifdef FEAT_GUI
7013 && !gui.in_use
7014# endif
7015 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007016 {
7017 ScreenLines[off - 1] = ' ';
7018 ScreenAttrs[off - 1] = 0;
7019 if (enc_utf8)
7020 {
7021 ScreenLinesUC[off - 1] = 0;
7022 ScreenLinesC[0][off - 1] = 0;
7023 }
7024 /* redraw the previous cell, make it empty */
7025 screen_char(off - 1, row, col - 1);
7026 /* force the cell at "col" to be redrawn */
7027 force_redraw_next = TRUE;
7028 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007029#endif
7030
Bram Moolenaar367329b2007-08-30 11:53:22 +00007031#ifdef FEAT_MBYTE
7032 max_off = LineOffset[row] + screen_Columns;
7033#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007034 while (col < screen_Columns
7035 && (len < 0 || (int)(ptr - text) < len)
7036 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
7038 c = *ptr;
7039#ifdef FEAT_MBYTE
7040 /* check if this is the first byte of a multibyte */
7041 if (has_mbyte)
7042 {
7043 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007044 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007046 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7048 mbyte_cells = 1;
7049 else if (enc_dbcs != 0)
7050 mbyte_cells = mbyte_blen;
7051 else /* enc_utf8 */
7052 {
7053 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007054 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 (int)((text + len) - ptr));
7056 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007057 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007059# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 /* Non-BMP character: display as ? or fullwidth ?. */
7061 if (u8c >= 0x10000)
7062 {
7063 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7064 if (attr == 0)
7065 attr = hl_attr(HLF_8);
7066 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068# ifdef FEAT_ARABIC
7069 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7070 {
7071 /* Do Arabic shaping. */
7072 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7073 {
7074 /* Past end of string to be displayed. */
7075 nc = NUL;
7076 nc1 = NUL;
7077 }
7078 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007079 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007080 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7081 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007082 nc1 = pcc[0];
7083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 pc = prev_c;
7085 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007086 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 }
7088 else
7089 prev_c = u8c;
7090# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007091 if (col + mbyte_cells > screen_Columns)
7092 {
7093 /* Only 1 cell left, but character requires 2 cells:
7094 * display a '>' in the last column to avoid wrapping. */
7095 c = '>';
7096 mbyte_cells = 1;
7097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 }
7099 }
7100#endif
7101
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007102#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7103 force_redraw_this = force_redraw_next;
7104 force_redraw_next = FALSE;
7105#endif
7106
7107 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108#ifdef FEAT_MBYTE
7109 || (mbyte_cells == 2
7110 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7111 || (enc_dbcs == DBCS_JPNU
7112 && c == 0x8e
7113 && ScreenLines2[off] != ptr[1])
7114 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007115 && (ScreenLinesUC[off] !=
7116 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7117 || (ScreenLinesUC[off] != 0
7118 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119#endif
7120 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007121 || exmode_active;
7122
7123 if (need_redraw
7124#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7125 || force_redraw_this
7126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 )
7128 {
7129#if defined(FEAT_GUI) || defined(UNIX)
7130 /* The bold trick makes a single row of pixels appear in the next
7131 * character. When a bold character is removed, the next
7132 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007133 * and for some xterms. */
7134 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135# ifdef FEAT_GUI
7136 gui.in_use
7137# endif
7138# if defined(FEAT_GUI) && defined(UNIX)
7139 ||
7140# endif
7141# ifdef UNIX
7142 term_is_xterm
7143# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007144 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007146 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007148 if (n > HL_ALL)
7149 n = syn_attr2attr(n);
7150 if (n & HL_BOLD)
7151 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
7153#endif
7154#ifdef FEAT_MBYTE
7155 /* When at the end of the text and overwriting a two-cell
7156 * character with a one-cell character, need to clear the next
7157 * cell. Also when overwriting the left halve of a two-cell char
7158 * with the right halve of a two-cell char. Do this only once
7159 * (mb_off2cells() may return 2 on the right halve). */
7160 if (clear_next_cell)
7161 clear_next_cell = FALSE;
7162 else if (has_mbyte
7163 && (len < 0 ? ptr[mbyte_blen] == NUL
7164 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007165 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007167 && (*mb_off2cells)(off, max_off) == 1
7168 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 clear_next_cell = TRUE;
7170
7171 /* Make sure we never leave a second byte of a double-byte behind,
7172 * it confuses mb_off2cells(). */
7173 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007174 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007176 && (*mb_off2cells)(off, max_off) == 1
7177 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 ScreenLines[off + mbyte_blen] = 0;
7179#endif
7180 ScreenLines[off] = c;
7181 ScreenAttrs[off] = attr;
7182#ifdef FEAT_MBYTE
7183 if (enc_utf8)
7184 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007185 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 ScreenLinesUC[off] = 0;
7187 else
7188 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007189 int i;
7190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007192 for (i = 0; i < Screen_mco; ++i)
7193 {
7194 ScreenLinesC[i][off] = u8cc[i];
7195 if (u8cc[i] == 0)
7196 break;
7197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
7199 if (mbyte_cells == 2)
7200 {
7201 ScreenLines[off + 1] = 0;
7202 ScreenAttrs[off + 1] = attr;
7203 }
7204 screen_char(off, row, col);
7205 }
7206 else if (mbyte_cells == 2)
7207 {
7208 ScreenLines[off + 1] = ptr[1];
7209 ScreenAttrs[off + 1] = attr;
7210 screen_char_2(off, row, col);
7211 }
7212 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7213 {
7214 ScreenLines2[off] = ptr[1];
7215 screen_char(off, row, col);
7216 }
7217 else
7218#endif
7219 screen_char(off, row, col);
7220 }
7221#ifdef FEAT_MBYTE
7222 if (has_mbyte)
7223 {
7224 off += mbyte_cells;
7225 col += mbyte_cells;
7226 ptr += mbyte_blen;
7227 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007228 {
7229 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007231 len = -1;
7232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 }
7234 else
7235#endif
7236 {
7237 ++off;
7238 ++col;
7239 ++ptr;
7240 }
7241 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007242
7243#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7244 /* If we detected the next character needs to be redrawn, but the text
7245 * doesn't extend up to there, update the character here. */
7246 if (force_redraw_next && col < screen_Columns)
7247 {
7248# ifdef FEAT_MBYTE
7249 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7250 screen_char_2(off, row, col);
7251 else
7252# endif
7253 screen_char(off, row, col);
7254 }
7255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256}
7257
7258#ifdef FEAT_SEARCH_EXTRA
7259/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007260 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 */
7262 static void
7263start_search_hl()
7264{
7265 if (p_hls && !no_hlsearch)
7266 {
7267 last_pat_prog(&search_hl.rm);
7268 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007269# ifdef FEAT_RELTIME
7270 /* Set the time limit to 'redrawtime'. */
7271 profile_setlimit(p_rdt, &search_hl.tm);
7272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274}
7275
7276/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007277 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 */
7279 static void
7280end_search_hl()
7281{
7282 if (search_hl.rm.regprog != NULL)
7283 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007284 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 search_hl.rm.regprog = NULL;
7286 }
7287}
7288
7289/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007290 * Init for calling prepare_search_hl().
7291 */
7292 static void
7293init_search_hl(wp)
7294 win_T *wp;
7295{
7296 matchitem_T *cur;
7297
7298 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7299 * match */
7300 cur = wp->w_match_head;
7301 while (cur != NULL)
7302 {
7303 cur->hl.rm = cur->match;
7304 if (cur->hlg_id == 0)
7305 cur->hl.attr = 0;
7306 else
7307 cur->hl.attr = syn_id2attr(cur->hlg_id);
7308 cur->hl.buf = wp->w_buffer;
7309 cur->hl.lnum = 0;
7310 cur->hl.first_lnum = 0;
7311# ifdef FEAT_RELTIME
7312 /* Set the time limit to 'redrawtime'. */
7313 profile_setlimit(p_rdt, &(cur->hl.tm));
7314# endif
7315 cur = cur->next;
7316 }
7317 search_hl.buf = wp->w_buffer;
7318 search_hl.lnum = 0;
7319 search_hl.first_lnum = 0;
7320 /* time limit is set at the toplevel, for all windows */
7321}
7322
7323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 * Advance to the match in window "wp" line "lnum" or past it.
7325 */
7326 static void
7327prepare_search_hl(wp, lnum)
7328 win_T *wp;
7329 linenr_T lnum;
7330{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007331 matchitem_T *cur; /* points to the match list */
7332 match_T *shl; /* points to search_hl or a match */
7333 int shl_flag; /* flag to indicate whether search_hl
7334 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007335 int pos_inprogress; /* marks that position match search is
7336 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 int n;
7338
7339 /*
7340 * When using a multi-line pattern, start searching at the top
7341 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007342 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007344 cur = wp->w_match_head;
7345 shl_flag = FALSE;
7346 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007348 if (shl_flag == FALSE)
7349 {
7350 shl = &search_hl;
7351 shl_flag = TRUE;
7352 }
7353 else
7354 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (shl->rm.regprog != NULL
7356 && shl->lnum == 0
7357 && re_multiline(shl->rm.regprog))
7358 {
7359 if (shl->first_lnum == 0)
7360 {
7361# ifdef FEAT_FOLDING
7362 for (shl->first_lnum = lnum;
7363 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7364 if (hasFoldingWin(wp, shl->first_lnum - 1,
7365 NULL, NULL, TRUE, NULL))
7366 break;
7367# else
7368 shl->first_lnum = wp->w_topline;
7369# endif
7370 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007371 if (cur != NULL)
7372 cur->pos.cur = 0;
7373 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007375 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7376 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007378 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7379 pos_inprogress = cur == NULL || cur->pos.cur == 0
7380 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 if (shl->lnum != 0)
7382 {
7383 shl->first_lnum = shl->lnum
7384 + shl->rm.endpos[0].lnum
7385 - shl->rm.startpos[0].lnum;
7386 n = shl->rm.endpos[0].col;
7387 }
7388 else
7389 {
7390 ++shl->first_lnum;
7391 n = 0;
7392 }
7393 }
7394 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007395 if (shl != &search_hl && cur != NULL)
7396 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 }
7398}
7399
7400/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007401 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 * Uses shl->buf.
7403 * Sets shl->lnum and shl->rm contents.
7404 * Note: Assumes a previous match is always before "lnum", unless
7405 * shl->lnum is zero.
7406 * Careful: Any pointers for buffer lines will become invalid.
7407 */
7408 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007409next_search_hl(win, shl, lnum, mincol, cur)
7410 win_T *win;
7411 match_T *shl; /* points to search_hl or a match */
7412 linenr_T lnum;
7413 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007414 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 linenr_T l;
7417 colnr_T matchcol;
7418 long nmatched;
7419
7420 if (shl->lnum != 0)
7421 {
7422 /* Check for three situations:
7423 * 1. If the "lnum" is below a previous match, start a new search.
7424 * 2. If the previous match includes "mincol", use it.
7425 * 3. Continue after the previous match.
7426 */
7427 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7428 if (lnum > l)
7429 shl->lnum = 0;
7430 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7431 return;
7432 }
7433
7434 /*
7435 * Repeat searching for a match until one is found that includes "mincol"
7436 * or none is found in this line.
7437 */
7438 called_emsg = FALSE;
7439 for (;;)
7440 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007441#ifdef FEAT_RELTIME
7442 /* Stop searching after passing the time limit. */
7443 if (profile_passed_limit(&(shl->tm)))
7444 {
7445 shl->lnum = 0; /* no match found in time */
7446 break;
7447 }
7448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 /* Three situations:
7450 * 1. No useful previous match: search from start of line.
7451 * 2. Not Vi compatible or empty match: continue at next character.
7452 * Break the loop if this is beyond the end of the line.
7453 * 3. Vi compatible searching: continue at end of previous match.
7454 */
7455 if (shl->lnum == 0)
7456 matchcol = 0;
7457 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7458 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007459 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007461 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007462
7463 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007464 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007465 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007467 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 shl->lnum = 0;
7469 break;
7470 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007471#ifdef FEAT_MBYTE
7472 if (has_mbyte)
7473 matchcol += mb_ptr2len(ml);
7474 else
7475#endif
7476 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
7478 else
7479 matchcol = shl->rm.endpos[0].col;
7480
7481 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007482 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007484 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7485 matchcol,
7486#ifdef FEAT_RELTIME
7487 &(shl->tm)
7488#else
7489 NULL
7490#endif
7491 );
7492 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007493 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007494 /* Error while handling regexp: stop using this regexp. */
7495 if (shl == &search_hl)
7496 {
7497 /* don't free regprog in the match list, it's a copy */
7498 vim_regfree(shl->rm.regprog);
7499 SET_NO_HLSEARCH(TRUE);
7500 }
7501 shl->rm.regprog = NULL;
7502 shl->lnum = 0;
7503 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7504 message */
7505 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007506 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007507 }
7508 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007509 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007510 else
7511 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 if (nmatched == 0)
7513 {
7514 shl->lnum = 0; /* no match found */
7515 break;
7516 }
7517 if (shl->rm.startpos[0].lnum > 0
7518 || shl->rm.startpos[0].col >= mincol
7519 || nmatched > 1
7520 || shl->rm.endpos[0].col > mincol)
7521 {
7522 shl->lnum += shl->rm.startpos[0].lnum;
7523 break; /* useful match found */
7524 }
7525 }
7526}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527
Bram Moolenaarb3414592014-06-17 17:48:32 +02007528 static int
7529next_search_hl_pos(shl, lnum, posmatch, mincol)
7530 match_T *shl; /* points to a match */
7531 linenr_T lnum;
7532 posmatch_T *posmatch; /* match positions */
7533 colnr_T mincol; /* minimal column for a match */
7534{
7535 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007536 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007537
7538 shl->lnum = 0;
7539 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7540 {
7541 if (posmatch->pos[i].lnum == 0)
7542 break;
7543 if (posmatch->pos[i].col < mincol)
7544 continue;
7545 if (posmatch->pos[i].lnum == lnum)
7546 {
7547 if (shl->lnum == lnum)
7548 {
7549 /* partially sort positions by column numbers
7550 * on the same line */
7551 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7552 {
7553 llpos_T tmp = posmatch->pos[i];
7554
7555 posmatch->pos[i] = posmatch->pos[bot];
7556 posmatch->pos[bot] = tmp;
7557 }
7558 }
7559 else
7560 {
7561 bot = i;
7562 shl->lnum = lnum;
7563 }
7564 }
7565 }
7566 posmatch->cur = 0;
7567 if (shl->lnum == lnum)
7568 {
7569 colnr_T start = posmatch->pos[bot].col == 0
7570 ? 0 : posmatch->pos[bot].col - 1;
7571 colnr_T end = posmatch->pos[bot].col == 0
7572 ? MAXCOL : start + posmatch->pos[bot].len;
7573
7574 shl->rm.startpos[0].lnum = 0;
7575 shl->rm.startpos[0].col = start;
7576 shl->rm.endpos[0].lnum = 0;
7577 shl->rm.endpos[0].col = end;
7578 posmatch->cur = bot + 1;
7579 return TRUE;
7580 }
7581 return FALSE;
7582}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007583#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007584
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 static void
7586screen_start_highlight(attr)
7587 int attr;
7588{
7589 attrentry_T *aep = NULL;
7590
7591 screen_attr = attr;
7592 if (full_screen
7593#ifdef WIN3264
7594 && termcap_active
7595#endif
7596 )
7597 {
7598#ifdef FEAT_GUI
7599 if (gui.in_use)
7600 {
7601 char buf[20];
7602
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007603 /* The GUI handles this internally. */
7604 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 OUT_STR(buf);
7606 }
7607 else
7608#endif
7609 {
7610 if (attr > HL_ALL) /* special HL attr. */
7611 {
7612 if (t_colors > 1)
7613 aep = syn_cterm_attr2entry(attr);
7614 else
7615 aep = syn_term_attr2entry(attr);
7616 if (aep == NULL) /* did ":syntax clear" */
7617 attr = 0;
7618 else
7619 attr = aep->ae_attr;
7620 }
7621 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7622 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007623 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7624 && cterm_normal_fg_bold)
7625 /* If the Normal FG color has BOLD attribute and the new HL
7626 * has a FG color defined, clear BOLD. */
7627 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7629 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007630 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7631 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 out_str(T_US);
7633 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7634 out_str(T_CZH);
7635 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7636 out_str(T_MR);
7637
7638 /*
7639 * Output the color or start string after bold etc., in case the
7640 * bold etc. override the color setting.
7641 */
7642 if (aep != NULL)
7643 {
7644 if (t_colors > 1)
7645 {
7646 if (aep->ae_u.cterm.fg_color)
7647 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7648 if (aep->ae_u.cterm.bg_color)
7649 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7650 }
7651 else
7652 {
7653 if (aep->ae_u.term.start != NULL)
7654 out_str(aep->ae_u.term.start);
7655 }
7656 }
7657 }
7658 }
7659}
7660
7661 void
7662screen_stop_highlight()
7663{
7664 int do_ME = FALSE; /* output T_ME code */
7665
7666 if (screen_attr != 0
7667#ifdef WIN3264
7668 && termcap_active
7669#endif
7670 )
7671 {
7672#ifdef FEAT_GUI
7673 if (gui.in_use)
7674 {
7675 char buf[20];
7676
7677 /* use internal GUI code */
7678 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7679 OUT_STR(buf);
7680 }
7681 else
7682#endif
7683 {
7684 if (screen_attr > HL_ALL) /* special HL attr. */
7685 {
7686 attrentry_T *aep;
7687
7688 if (t_colors > 1)
7689 {
7690 /*
7691 * Assume that t_me restores the original colors!
7692 */
7693 aep = syn_cterm_attr2entry(screen_attr);
7694 if (aep != NULL && (aep->ae_u.cterm.fg_color
7695 || aep->ae_u.cterm.bg_color))
7696 do_ME = TRUE;
7697 }
7698 else
7699 {
7700 aep = syn_term_attr2entry(screen_attr);
7701 if (aep != NULL && aep->ae_u.term.stop != NULL)
7702 {
7703 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7704 do_ME = TRUE;
7705 else
7706 out_str(aep->ae_u.term.stop);
7707 }
7708 }
7709 if (aep == NULL) /* did ":syntax clear" */
7710 screen_attr = 0;
7711 else
7712 screen_attr = aep->ae_attr;
7713 }
7714
7715 /*
7716 * Often all ending-codes are equal to T_ME. Avoid outputting the
7717 * same sequence several times.
7718 */
7719 if (screen_attr & HL_STANDOUT)
7720 {
7721 if (STRCMP(T_SE, T_ME) == 0)
7722 do_ME = TRUE;
7723 else
7724 out_str(T_SE);
7725 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007726 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {
7728 if (STRCMP(T_UE, T_ME) == 0)
7729 do_ME = TRUE;
7730 else
7731 out_str(T_UE);
7732 }
7733 if (screen_attr & HL_ITALIC)
7734 {
7735 if (STRCMP(T_CZR, T_ME) == 0)
7736 do_ME = TRUE;
7737 else
7738 out_str(T_CZR);
7739 }
7740 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7741 out_str(T_ME);
7742
7743 if (t_colors > 1)
7744 {
7745 /* set Normal cterm colors */
7746 if (cterm_normal_fg_color != 0)
7747 term_fg_color(cterm_normal_fg_color - 1);
7748 if (cterm_normal_bg_color != 0)
7749 term_bg_color(cterm_normal_bg_color - 1);
7750 if (cterm_normal_fg_bold)
7751 out_str(T_MD);
7752 }
7753 }
7754 }
7755 screen_attr = 0;
7756}
7757
7758/*
7759 * Reset the colors for a cterm. Used when leaving Vim.
7760 * The machine specific code may override this again.
7761 */
7762 void
7763reset_cterm_colors()
7764{
7765 if (t_colors > 1)
7766 {
7767 /* set Normal cterm colors */
7768 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7769 {
7770 out_str(T_OP);
7771 screen_attr = -1;
7772 }
7773 if (cterm_normal_fg_bold)
7774 {
7775 out_str(T_ME);
7776 screen_attr = -1;
7777 }
7778 }
7779}
7780
7781/*
7782 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7783 * using the attributes from ScreenAttrs["off"].
7784 */
7785 static void
7786screen_char(off, row, col)
7787 unsigned off;
7788 int row;
7789 int col;
7790{
7791 int attr;
7792
7793 /* Check for illegal values, just in case (could happen just after
7794 * resizing). */
7795 if (row >= screen_Rows || col >= screen_Columns)
7796 return;
7797
7798 /* Outputting the last character on the screen may scrollup the screen.
7799 * Don't to it! Mark the character invalid (update it when scrolled up) */
7800 if (row == screen_Rows - 1 && col == screen_Columns - 1
7801#ifdef FEAT_RIGHTLEFT
7802 /* account for first command-line character in rightleft mode */
7803 && !cmdmsg_rl
7804#endif
7805 )
7806 {
7807 ScreenAttrs[off] = (sattr_T)-1;
7808 return;
7809 }
7810
7811 /*
7812 * Stop highlighting first, so it's easier to move the cursor.
7813 */
7814#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7815 if (screen_char_attr != 0)
7816 attr = screen_char_attr;
7817 else
7818#endif
7819 attr = ScreenAttrs[off];
7820 if (screen_attr != attr)
7821 screen_stop_highlight();
7822
7823 windgoto(row, col);
7824
7825 if (screen_attr != attr)
7826 screen_start_highlight(attr);
7827
7828#ifdef FEAT_MBYTE
7829 if (enc_utf8 && ScreenLinesUC[off] != 0)
7830 {
7831 char_u buf[MB_MAXBYTES + 1];
7832
7833 /* Convert UTF-8 character to bytes and write it. */
7834
7835 buf[utfc_char2bytes(off, buf)] = NUL;
7836
7837 out_str(buf);
7838 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7839 ++screen_cur_col;
7840 }
7841 else
7842#endif
7843 {
7844#ifdef FEAT_MBYTE
7845 out_flush_check();
7846#endif
7847 out_char(ScreenLines[off]);
7848#ifdef FEAT_MBYTE
7849 /* double-byte character in single-width cell */
7850 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7851 out_char(ScreenLines2[off]);
7852#endif
7853 }
7854
7855 screen_cur_col++;
7856}
7857
7858#ifdef FEAT_MBYTE
7859
7860/*
7861 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7862 * on the screen at position 'row' and 'col'.
7863 * The attributes of the first byte is used for all. This is required to
7864 * output the two bytes of a double-byte character with nothing in between.
7865 */
7866 static void
7867screen_char_2(off, row, col)
7868 unsigned off;
7869 int row;
7870 int col;
7871{
7872 /* Check for illegal values (could be wrong when screen was resized). */
7873 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7874 return;
7875
7876 /* Outputting the last character on the screen may scrollup the screen.
7877 * Don't to it! Mark the character invalid (update it when scrolled up) */
7878 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7879 {
7880 ScreenAttrs[off] = (sattr_T)-1;
7881 return;
7882 }
7883
7884 /* Output the first byte normally (positions the cursor), then write the
7885 * second byte directly. */
7886 screen_char(off, row, col);
7887 out_char(ScreenLines[off + 1]);
7888 ++screen_cur_col;
7889}
7890#endif
7891
7892#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7893/*
7894 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7895 * This uses the contents of ScreenLines[] and doesn't change it.
7896 */
7897 void
7898screen_draw_rectangle(row, col, height, width, invert)
7899 int row;
7900 int col;
7901 int height;
7902 int width;
7903 int invert;
7904{
7905 int r, c;
7906 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007907#ifdef FEAT_MBYTE
7908 int max_off;
7909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007911 /* Can't use ScreenLines unless initialized */
7912 if (ScreenLines == NULL)
7913 return;
7914
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 if (invert)
7916 screen_char_attr = HL_INVERSE;
7917 for (r = row; r < row + height; ++r)
7918 {
7919 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007920#ifdef FEAT_MBYTE
7921 max_off = off + screen_Columns;
7922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 for (c = col; c < col + width; ++c)
7924 {
7925#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007926 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
7928 screen_char_2(off + c, r, c);
7929 ++c;
7930 }
7931 else
7932#endif
7933 {
7934 screen_char(off + c, r, c);
7935#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007936 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 ++c;
7938#endif
7939 }
7940 }
7941 }
7942 screen_char_attr = 0;
7943}
7944#endif
7945
7946#ifdef FEAT_VERTSPLIT
7947/*
7948 * Redraw the characters for a vertically split window.
7949 */
7950 static void
7951redraw_block(row, end, wp)
7952 int row;
7953 int end;
7954 win_T *wp;
7955{
7956 int col;
7957 int width;
7958
7959# ifdef FEAT_CLIPBOARD
7960 clip_may_clear_selection(row, end - 1);
7961# endif
7962
7963 if (wp == NULL)
7964 {
7965 col = 0;
7966 width = Columns;
7967 }
7968 else
7969 {
7970 col = wp->w_wincol;
7971 width = wp->w_width;
7972 }
7973 screen_draw_rectangle(row, col, end - row, width, FALSE);
7974}
7975#endif
7976
7977/*
7978 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7979 * with character 'c1' in first column followed by 'c2' in the other columns.
7980 * Use attributes 'attr'.
7981 */
7982 void
7983screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7984 int start_row, end_row;
7985 int start_col, end_col;
7986 int c1, c2;
7987 int attr;
7988{
7989 int row;
7990 int col;
7991 int off;
7992 int end_off;
7993 int did_delete;
7994 int c;
7995 int norm_term;
7996#if defined(FEAT_GUI) || defined(UNIX)
7997 int force_next = FALSE;
7998#endif
7999
8000 if (end_row > screen_Rows) /* safety check */
8001 end_row = screen_Rows;
8002 if (end_col > screen_Columns) /* safety check */
8003 end_col = screen_Columns;
8004 if (ScreenLines == NULL
8005 || start_row >= end_row
8006 || start_col >= end_col) /* nothing to do */
8007 return;
8008
8009 /* it's a "normal" terminal when not in a GUI or cterm */
8010 norm_term = (
8011#ifdef FEAT_GUI
8012 !gui.in_use &&
8013#endif
8014 t_colors <= 1);
8015 for (row = start_row; row < end_row; ++row)
8016 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008017#ifdef FEAT_MBYTE
8018 if (has_mbyte
8019# ifdef FEAT_GUI
8020 && !gui.in_use
8021# endif
8022 )
8023 {
8024 /* When drawing over the right halve of a double-wide char clear
8025 * out the left halve. When drawing over the left halve of a
8026 * double wide-char clear out the right halve. Only needed in a
8027 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008028 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008029 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008030 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008031 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008032 }
8033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 /*
8035 * Try to use delete-line termcap code, when no attributes or in a
8036 * "normal" terminal, where a bold/italic space is just a
8037 * space.
8038 */
8039 did_delete = FALSE;
8040 if (c2 == ' '
8041 && end_col == Columns
8042 && can_clear(T_CE)
8043 && (attr == 0
8044 || (norm_term
8045 && attr <= HL_ALL
8046 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8047 {
8048 /*
8049 * check if we really need to clear something
8050 */
8051 col = start_col;
8052 if (c1 != ' ') /* don't clear first char */
8053 ++col;
8054
8055 off = LineOffset[row] + col;
8056 end_off = LineOffset[row] + end_col;
8057
8058 /* skip blanks (used often, keep it fast!) */
8059#ifdef FEAT_MBYTE
8060 if (enc_utf8)
8061 while (off < end_off && ScreenLines[off] == ' '
8062 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8063 ++off;
8064 else
8065#endif
8066 while (off < end_off && ScreenLines[off] == ' '
8067 && ScreenAttrs[off] == 0)
8068 ++off;
8069 if (off < end_off) /* something to be cleared */
8070 {
8071 col = off - LineOffset[row];
8072 screen_stop_highlight();
8073 term_windgoto(row, col);/* clear rest of this screen line */
8074 out_str(T_CE);
8075 screen_start(); /* don't know where cursor is now */
8076 col = end_col - col;
8077 while (col--) /* clear chars in ScreenLines */
8078 {
8079 ScreenLines[off] = ' ';
8080#ifdef FEAT_MBYTE
8081 if (enc_utf8)
8082 ScreenLinesUC[off] = 0;
8083#endif
8084 ScreenAttrs[off] = 0;
8085 ++off;
8086 }
8087 }
8088 did_delete = TRUE; /* the chars are cleared now */
8089 }
8090
8091 off = LineOffset[row] + start_col;
8092 c = c1;
8093 for (col = start_col; col < end_col; ++col)
8094 {
8095 if (ScreenLines[off] != c
8096#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008097 || (enc_utf8 && (int)ScreenLinesUC[off]
8098 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099#endif
8100 || ScreenAttrs[off] != attr
8101#if defined(FEAT_GUI) || defined(UNIX)
8102 || force_next
8103#endif
8104 )
8105 {
8106#if defined(FEAT_GUI) || defined(UNIX)
8107 /* The bold trick may make a single row of pixels appear in
8108 * the next character. When a bold character is removed, the
8109 * next character should be redrawn too. This happens for our
8110 * own GUI and for some xterms. */
8111 if (
8112# ifdef FEAT_GUI
8113 gui.in_use
8114# endif
8115# if defined(FEAT_GUI) && defined(UNIX)
8116 ||
8117# endif
8118# ifdef UNIX
8119 term_is_xterm
8120# endif
8121 )
8122 {
8123 if (ScreenLines[off] != ' '
8124 && (ScreenAttrs[off] > HL_ALL
8125 || ScreenAttrs[off] & HL_BOLD))
8126 force_next = TRUE;
8127 else
8128 force_next = FALSE;
8129 }
8130#endif
8131 ScreenLines[off] = c;
8132#ifdef FEAT_MBYTE
8133 if (enc_utf8)
8134 {
8135 if (c >= 0x80)
8136 {
8137 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008138 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 }
8140 else
8141 ScreenLinesUC[off] = 0;
8142 }
8143#endif
8144 ScreenAttrs[off] = attr;
8145 if (!did_delete || c != ' ')
8146 screen_char(off, row, col);
8147 }
8148 ++off;
8149 if (col == start_col)
8150 {
8151 if (did_delete)
8152 break;
8153 c = c2;
8154 }
8155 }
8156 if (end_col == Columns)
8157 LineWraps[row] = FALSE;
8158 if (row == Rows - 1) /* overwritten the command line */
8159 {
8160 redraw_cmdline = TRUE;
8161 if (c1 == ' ' && c2 == ' ')
8162 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008163 if (start_col == 0)
8164 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 }
8166 }
8167}
8168
8169/*
8170 * Check if there should be a delay. Used before clearing or redrawing the
8171 * screen or the command line.
8172 */
8173 void
8174check_for_delay(check_msg_scroll)
8175 int check_msg_scroll;
8176{
8177 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8178 && !did_wait_return
8179 && emsg_silent == 0)
8180 {
8181 out_flush();
8182 ui_delay(1000L, TRUE);
8183 emsg_on_display = FALSE;
8184 if (check_msg_scroll)
8185 msg_scroll = FALSE;
8186 }
8187}
8188
8189/*
8190 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008191 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 * Returns TRUE if there is a valid screen to write to.
8193 * Returns FALSE when starting up and screen not initialized yet.
8194 */
8195 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008196screen_valid(doclear)
8197 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008199 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 return (ScreenLines != NULL);
8201}
8202
8203/*
8204 * Resize the shell to Rows and Columns.
8205 * Allocate ScreenLines[] and associated items.
8206 *
8207 * There may be some time between setting Rows and Columns and (re)allocating
8208 * ScreenLines[]. This happens when starting up and when (manually) changing
8209 * the shell size. Always use screen_Rows and screen_Columns to access items
8210 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8211 * final size of the shell is needed.
8212 */
8213 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008214screenalloc(doclear)
8215 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217 int new_row, old_row;
8218#ifdef FEAT_GUI
8219 int old_Rows;
8220#endif
8221 win_T *wp;
8222 int outofmem = FALSE;
8223 int len;
8224 schar_T *new_ScreenLines;
8225#ifdef FEAT_MBYTE
8226 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008227 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008229 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230#endif
8231 sattr_T *new_ScreenAttrs;
8232 unsigned *new_LineOffset;
8233 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008234#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008235 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008236 tabpage_T *tp;
8237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008239 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008240#ifdef FEAT_AUTOCMD
8241 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008243retry:
8244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 /*
8246 * Allocation of the screen buffers is done only when the size changes and
8247 * when Rows and Columns have been set and we have started doing full
8248 * screen stuff.
8249 */
8250 if ((ScreenLines != NULL
8251 && Rows == screen_Rows
8252 && Columns == screen_Columns
8253#ifdef FEAT_MBYTE
8254 && enc_utf8 == (ScreenLinesUC != NULL)
8255 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008256 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257#endif
8258 )
8259 || Rows == 0
8260 || Columns == 0
8261 || (!full_screen && ScreenLines == NULL))
8262 return;
8263
8264 /*
8265 * It's possible that we produce an out-of-memory message below, which
8266 * will cause this function to be called again. To break the loop, just
8267 * return here.
8268 */
8269 if (entered)
8270 return;
8271 entered = TRUE;
8272
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008273 /*
8274 * Note that the window sizes are updated before reallocating the arrays,
8275 * thus we must not redraw here!
8276 */
8277 ++RedrawingDisabled;
8278
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 win_new_shellsize(); /* fit the windows in the new sized shell */
8280
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 comp_col(); /* recompute columns for shown command and ruler */
8282
8283 /*
8284 * We're changing the size of the screen.
8285 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8286 * - Move lines from the old arrays into the new arrays, clear extra
8287 * lines (unless the screen is going to be cleared).
8288 * - Free the old arrays.
8289 *
8290 * If anything fails, make ScreenLines NULL, so we don't do anything!
8291 * Continuing with the old ScreenLines may result in a crash, because the
8292 * size is wrong.
8293 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008294 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008296#ifdef FEAT_AUTOCMD
8297 if (aucmd_win != NULL)
8298 win_free_lsize(aucmd_win);
8299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300
8301 new_ScreenLines = (schar_T *)lalloc((long_u)(
8302 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8303#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008304 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (enc_utf8)
8306 {
8307 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8308 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008309 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008310 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8312 }
8313 if (enc_dbcs == DBCS_JPNU)
8314 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8315 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8316#endif
8317 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8318 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8319 new_LineOffset = (unsigned *)lalloc((long_u)(
8320 Rows * sizeof(unsigned)), FALSE);
8321 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008322#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008323 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008326 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {
8328 if (win_alloc_lines(wp) == FAIL)
8329 {
8330 outofmem = TRUE;
8331#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008332 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333#endif
8334 }
8335 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008336#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008337 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8338 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008339 outofmem = TRUE;
8340#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008341#ifdef FEAT_WINDOWS
8342give_up:
8343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008345#ifdef FEAT_MBYTE
8346 for (i = 0; i < p_mco; ++i)
8347 if (new_ScreenLinesC[i] == NULL)
8348 break;
8349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 if (new_ScreenLines == NULL
8351#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008352 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8354#endif
8355 || new_ScreenAttrs == NULL
8356 || new_LineOffset == NULL
8357 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008358#ifdef FEAT_WINDOWS
8359 || new_TabPageIdxs == NULL
8360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 || outofmem)
8362 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008363 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008364 {
8365 /* guess the size */
8366 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8367
8368 /* Remember we did this to avoid getting outofmem messages over
8369 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008370 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 vim_free(new_ScreenLines);
8373 new_ScreenLines = NULL;
8374#ifdef FEAT_MBYTE
8375 vim_free(new_ScreenLinesUC);
8376 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008377 for (i = 0; i < p_mco; ++i)
8378 {
8379 vim_free(new_ScreenLinesC[i]);
8380 new_ScreenLinesC[i] = NULL;
8381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 vim_free(new_ScreenLines2);
8383 new_ScreenLines2 = NULL;
8384#endif
8385 vim_free(new_ScreenAttrs);
8386 new_ScreenAttrs = NULL;
8387 vim_free(new_LineOffset);
8388 new_LineOffset = NULL;
8389 vim_free(new_LineWraps);
8390 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008391#ifdef FEAT_WINDOWS
8392 vim_free(new_TabPageIdxs);
8393 new_TabPageIdxs = NULL;
8394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 }
8396 else
8397 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008398 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008399
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 for (new_row = 0; new_row < Rows; ++new_row)
8401 {
8402 new_LineOffset[new_row] = new_row * Columns;
8403 new_LineWraps[new_row] = FALSE;
8404
8405 /*
8406 * If the screen is not going to be cleared, copy as much as
8407 * possible from the old screen to the new one and clear the rest
8408 * (used when resizing the window at the "--more--" prompt or when
8409 * executing an external command, for the GUI).
8410 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008411 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {
8413 (void)vim_memset(new_ScreenLines + new_row * Columns,
8414 ' ', (size_t)Columns * sizeof(schar_T));
8415#ifdef FEAT_MBYTE
8416 if (enc_utf8)
8417 {
8418 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8419 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008420 for (i = 0; i < p_mco; ++i)
8421 (void)vim_memset(new_ScreenLinesC[i]
8422 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 0, (size_t)Columns * sizeof(u8char_T));
8424 }
8425 if (enc_dbcs == DBCS_JPNU)
8426 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8427 0, (size_t)Columns * sizeof(schar_T));
8428#endif
8429 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8430 0, (size_t)Columns * sizeof(sattr_T));
8431 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008432 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {
8434 if (screen_Columns < Columns)
8435 len = screen_Columns;
8436 else
8437 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008438#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008439 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008440 * may be invalid now. Also when p_mco changes. */
8441 if (!(enc_utf8 && ScreenLinesUC == NULL)
8442 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008443#endif
8444 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8445 ScreenLines + LineOffset[old_row],
8446 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008448 if (enc_utf8 && ScreenLinesUC != NULL
8449 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {
8451 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8452 ScreenLinesUC + LineOffset[old_row],
8453 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008454 for (i = 0; i < p_mco; ++i)
8455 mch_memmove(new_ScreenLinesC[i]
8456 + new_LineOffset[new_row],
8457 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 (size_t)len * sizeof(u8char_T));
8459 }
8460 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8461 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8462 ScreenLines2 + LineOffset[old_row],
8463 (size_t)len * sizeof(schar_T));
8464#endif
8465 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8466 ScreenAttrs + LineOffset[old_row],
8467 (size_t)len * sizeof(sattr_T));
8468 }
8469 }
8470 }
8471 /* Use the last line of the screen for the current line. */
8472 current_ScreenLine = new_ScreenLines + Rows * Columns;
8473 }
8474
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008475 free_screenlines();
8476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 ScreenLines = new_ScreenLines;
8478#ifdef FEAT_MBYTE
8479 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008480 for (i = 0; i < p_mco; ++i)
8481 ScreenLinesC[i] = new_ScreenLinesC[i];
8482 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 ScreenLines2 = new_ScreenLines2;
8484#endif
8485 ScreenAttrs = new_ScreenAttrs;
8486 LineOffset = new_LineOffset;
8487 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008488#ifdef FEAT_WINDOWS
8489 TabPageIdxs = new_TabPageIdxs;
8490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491
8492 /* It's important that screen_Rows and screen_Columns reflect the actual
8493 * size of ScreenLines[]. Set them before calling anything. */
8494#ifdef FEAT_GUI
8495 old_Rows = screen_Rows;
8496#endif
8497 screen_Rows = Rows;
8498 screen_Columns = Columns;
8499
8500 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008501 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 screenclear2();
8503
8504#ifdef FEAT_GUI
8505 else if (gui.in_use
8506 && !gui.starting
8507 && ScreenLines != NULL
8508 && old_Rows != Rows)
8509 {
8510 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8511 /*
8512 * Adjust the position of the cursor, for when executing an external
8513 * command.
8514 */
8515 if (msg_row >= Rows) /* Rows got smaller */
8516 msg_row = Rows - 1; /* put cursor at last row */
8517 else if (Rows > old_Rows) /* Rows got bigger */
8518 msg_row += Rows - old_Rows; /* put cursor in same place */
8519 if (msg_col >= Columns) /* Columns got smaller */
8520 msg_col = Columns - 1; /* put cursor at last column */
8521 }
8522#endif
8523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008525 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008526
8527#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008528 /*
8529 * Do not apply autocommands more than 3 times to avoid an endless loop
8530 * in case applying autocommands always changes Rows or Columns.
8531 */
8532 if (starting == 0 && ++retry_count <= 3)
8533 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008534 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008535 /* In rare cases, autocommands may have altered Rows or Columns,
8536 * jump back to check if we need to allocate the screen again. */
8537 goto retry;
8538 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540}
8541
8542 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008543free_screenlines()
8544{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008545#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008546 int i;
8547
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008548 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008549 for (i = 0; i < Screen_mco; ++i)
8550 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008551 vim_free(ScreenLines2);
8552#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008553 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008554 vim_free(ScreenAttrs);
8555 vim_free(LineOffset);
8556 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008557#ifdef FEAT_WINDOWS
8558 vim_free(TabPageIdxs);
8559#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008560}
8561
8562 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563screenclear()
8564{
8565 check_for_delay(FALSE);
8566 screenalloc(FALSE); /* allocate screen buffers if size changed */
8567 screenclear2(); /* clear the screen */
8568}
8569
8570 static void
8571screenclear2()
8572{
8573 int i;
8574
8575 if (starting == NO_SCREEN || ScreenLines == NULL
8576#ifdef FEAT_GUI
8577 || (gui.in_use && gui.starting)
8578#endif
8579 )
8580 return;
8581
8582#ifdef FEAT_GUI
8583 if (!gui.in_use)
8584#endif
8585 screen_attr = -1; /* force setting the Normal colors */
8586 screen_stop_highlight(); /* don't want highlighting here */
8587
8588#ifdef FEAT_CLIPBOARD
8589 /* disable selection without redrawing it */
8590 clip_scroll_selection(9999);
8591#endif
8592
8593 /* blank out ScreenLines */
8594 for (i = 0; i < Rows; ++i)
8595 {
8596 lineclear(LineOffset[i], (int)Columns);
8597 LineWraps[i] = FALSE;
8598 }
8599
8600 if (can_clear(T_CL))
8601 {
8602 out_str(T_CL); /* clear the display */
8603 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008604 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 }
8606 else
8607 {
8608 /* can't clear the screen, mark all chars with invalid attributes */
8609 for (i = 0; i < Rows; ++i)
8610 lineinvalid(LineOffset[i], (int)Columns);
8611 clear_cmdline = TRUE;
8612 }
8613
8614 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8615
8616 win_rest_invalid(firstwin);
8617 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008618#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008619 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 if (must_redraw == CLEAR) /* no need to clear again */
8622 must_redraw = NOT_VALID;
8623 compute_cmdrow();
8624 msg_row = cmdline_row; /* put cursor on last line for messages */
8625 msg_col = 0;
8626 screen_start(); /* don't know where cursor is now */
8627 msg_scrolled = 0; /* can't scroll back */
8628 msg_didany = FALSE;
8629 msg_didout = FALSE;
8630}
8631
8632/*
8633 * Clear one line in ScreenLines.
8634 */
8635 static void
8636lineclear(off, width)
8637 unsigned off;
8638 int width;
8639{
8640 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8641#ifdef FEAT_MBYTE
8642 if (enc_utf8)
8643 (void)vim_memset(ScreenLinesUC + off, 0,
8644 (size_t)width * sizeof(u8char_T));
8645#endif
8646 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8647}
8648
8649/*
8650 * Mark one line in ScreenLines invalid by setting the attributes to an
8651 * invalid value.
8652 */
8653 static void
8654lineinvalid(off, width)
8655 unsigned off;
8656 int width;
8657{
8658 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8659}
8660
8661#ifdef FEAT_VERTSPLIT
8662/*
8663 * Copy part of a Screenline for vertically split window "wp".
8664 */
8665 static void
8666linecopy(to, from, wp)
8667 int to;
8668 int from;
8669 win_T *wp;
8670{
8671 unsigned off_to = LineOffset[to] + wp->w_wincol;
8672 unsigned off_from = LineOffset[from] + wp->w_wincol;
8673
8674 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8675 wp->w_width * sizeof(schar_T));
8676# ifdef FEAT_MBYTE
8677 if (enc_utf8)
8678 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008679 int i;
8680
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8682 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008683 for (i = 0; i < p_mco; ++i)
8684 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8685 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 }
8687 if (enc_dbcs == DBCS_JPNU)
8688 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8689 wp->w_width * sizeof(schar_T));
8690# endif
8691 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8692 wp->w_width * sizeof(sattr_T));
8693}
8694#endif
8695
8696/*
8697 * Return TRUE if clearing with term string "p" would work.
8698 * It can't work when the string is empty or it won't set the right background.
8699 */
8700 int
8701can_clear(p)
8702 char_u *p;
8703{
8704 return (*p != NUL && (t_colors <= 1
8705#ifdef FEAT_GUI
8706 || gui.in_use
8707#endif
8708 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8709}
8710
8711/*
8712 * Reset cursor position. Use whenever cursor was moved because of outputting
8713 * something directly to the screen (shell commands) or a terminal control
8714 * code.
8715 */
8716 void
8717screen_start()
8718{
8719 screen_cur_row = screen_cur_col = 9999;
8720}
8721
8722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 * Move the cursor to position "row","col" in the screen.
8724 * This tries to find the most efficient way to move, minimizing the number of
8725 * characters sent to the terminal.
8726 */
8727 void
8728windgoto(row, col)
8729 int row;
8730 int col;
8731{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008732 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 int i;
8734 int plan;
8735 int cost;
8736 int wouldbe_col;
8737 int noinvcurs;
8738 char_u *bs;
8739 int goto_cost;
8740 int attr;
8741
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008742#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8744
8745#define PLAN_LE 1
8746#define PLAN_CR 2
8747#define PLAN_NL 3
8748#define PLAN_WRITE 4
8749 /* Can't use ScreenLines unless initialized */
8750 if (ScreenLines == NULL)
8751 return;
8752
8753 if (col != screen_cur_col || row != screen_cur_row)
8754 {
8755 /* Check for valid position. */
8756 if (row < 0) /* window without text lines? */
8757 row = 0;
8758 if (row >= screen_Rows)
8759 row = screen_Rows - 1;
8760 if (col >= screen_Columns)
8761 col = screen_Columns - 1;
8762
8763 /* check if no cursor movement is allowed in highlight mode */
8764 if (screen_attr && *T_MS == NUL)
8765 noinvcurs = HIGHL_COST;
8766 else
8767 noinvcurs = 0;
8768 goto_cost = GOTO_COST + noinvcurs;
8769
8770 /*
8771 * Plan how to do the positioning:
8772 * 1. Use CR to move it to column 0, same row.
8773 * 2. Use T_LE to move it a few columns to the left.
8774 * 3. Use NL to move a few lines down, column 0.
8775 * 4. Move a few columns to the right with T_ND or by writing chars.
8776 *
8777 * Don't do this if the cursor went beyond the last column, the cursor
8778 * position is unknown then (some terminals wrap, some don't )
8779 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008780 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 * characters to move the cursor to the right.
8782 */
8783 if (row >= screen_cur_row && screen_cur_col < Columns)
8784 {
8785 /*
8786 * If the cursor is in the same row, bigger col, we can use CR
8787 * or T_LE.
8788 */
8789 bs = NULL; /* init for GCC */
8790 attr = screen_attr;
8791 if (row == screen_cur_row && col < screen_cur_col)
8792 {
8793 /* "le" is preferred over "bc", because "bc" is obsolete */
8794 if (*T_LE)
8795 bs = T_LE; /* "cursor left" */
8796 else
8797 bs = T_BC; /* "backspace character (old) */
8798 if (*bs)
8799 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8800 else
8801 cost = 999;
8802 if (col + 1 < cost) /* using CR is less characters */
8803 {
8804 plan = PLAN_CR;
8805 wouldbe_col = 0;
8806 cost = 1; /* CR is just one character */
8807 }
8808 else
8809 {
8810 plan = PLAN_LE;
8811 wouldbe_col = col;
8812 }
8813 if (noinvcurs) /* will stop highlighting */
8814 {
8815 cost += noinvcurs;
8816 attr = 0;
8817 }
8818 }
8819
8820 /*
8821 * If the cursor is above where we want to be, we can use CR LF.
8822 */
8823 else if (row > screen_cur_row)
8824 {
8825 plan = PLAN_NL;
8826 wouldbe_col = 0;
8827 cost = (row - screen_cur_row) * 2; /* CR LF */
8828 if (noinvcurs) /* will stop highlighting */
8829 {
8830 cost += noinvcurs;
8831 attr = 0;
8832 }
8833 }
8834
8835 /*
8836 * If the cursor is in the same row, smaller col, just use write.
8837 */
8838 else
8839 {
8840 plan = PLAN_WRITE;
8841 wouldbe_col = screen_cur_col;
8842 cost = 0;
8843 }
8844
8845 /*
8846 * Check if any characters that need to be written have the
8847 * correct attributes. Also avoid UTF-8 characters.
8848 */
8849 i = col - wouldbe_col;
8850 if (i > 0)
8851 cost += i;
8852 if (cost < goto_cost && i > 0)
8853 {
8854 /*
8855 * Check if the attributes are correct without additionally
8856 * stopping highlighting.
8857 */
8858 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8859 while (i && *p++ == attr)
8860 --i;
8861 if (i != 0)
8862 {
8863 /*
8864 * Try if it works when highlighting is stopped here.
8865 */
8866 if (*--p == 0)
8867 {
8868 cost += noinvcurs;
8869 while (i && *p++ == 0)
8870 --i;
8871 }
8872 if (i != 0)
8873 cost = 999; /* different attributes, don't do it */
8874 }
8875#ifdef FEAT_MBYTE
8876 if (enc_utf8)
8877 {
8878 /* Don't use an UTF-8 char for positioning, it's slow. */
8879 for (i = wouldbe_col; i < col; ++i)
8880 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8881 {
8882 cost = 999;
8883 break;
8884 }
8885 }
8886#endif
8887 }
8888
8889 /*
8890 * We can do it without term_windgoto()!
8891 */
8892 if (cost < goto_cost)
8893 {
8894 if (plan == PLAN_LE)
8895 {
8896 if (noinvcurs)
8897 screen_stop_highlight();
8898 while (screen_cur_col > col)
8899 {
8900 out_str(bs);
8901 --screen_cur_col;
8902 }
8903 }
8904 else if (plan == PLAN_CR)
8905 {
8906 if (noinvcurs)
8907 screen_stop_highlight();
8908 out_char('\r');
8909 screen_cur_col = 0;
8910 }
8911 else if (plan == PLAN_NL)
8912 {
8913 if (noinvcurs)
8914 screen_stop_highlight();
8915 while (screen_cur_row < row)
8916 {
8917 out_char('\n');
8918 ++screen_cur_row;
8919 }
8920 screen_cur_col = 0;
8921 }
8922
8923 i = col - screen_cur_col;
8924 if (i > 0)
8925 {
8926 /*
8927 * Use cursor-right if it's one character only. Avoids
8928 * removing a line of pixels from the last bold char, when
8929 * using the bold trick in the GUI.
8930 */
8931 if (T_ND[0] != NUL && T_ND[1] == NUL)
8932 {
8933 while (i-- > 0)
8934 out_char(*T_ND);
8935 }
8936 else
8937 {
8938 int off;
8939
8940 off = LineOffset[row] + screen_cur_col;
8941 while (i-- > 0)
8942 {
8943 if (ScreenAttrs[off] != screen_attr)
8944 screen_stop_highlight();
8945#ifdef FEAT_MBYTE
8946 out_flush_check();
8947#endif
8948 out_char(ScreenLines[off]);
8949#ifdef FEAT_MBYTE
8950 if (enc_dbcs == DBCS_JPNU
8951 && ScreenLines[off] == 0x8e)
8952 out_char(ScreenLines2[off]);
8953#endif
8954 ++off;
8955 }
8956 }
8957 }
8958 }
8959 }
8960 else
8961 cost = 999;
8962
8963 if (cost >= goto_cost)
8964 {
8965 if (noinvcurs)
8966 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02008967 if (row == screen_cur_row && (col > screen_cur_col)
8968 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 term_cursor_right(col - screen_cur_col);
8970 else
8971 term_windgoto(row, col);
8972 }
8973 screen_cur_row = row;
8974 screen_cur_col = col;
8975 }
8976}
8977
8978/*
8979 * Set cursor to its position in the current window.
8980 */
8981 void
8982setcursor()
8983{
8984 if (redrawing())
8985 {
8986 validate_cursor();
8987 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8988 W_WINCOL(curwin) + (
8989#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008990 /* With 'rightleft' set and the cursor on a double-wide
8991 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8993# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008994 (has_mbyte
8995 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8996 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997# endif
8998 1)) :
8999#endif
9000 curwin->w_wcol));
9001 }
9002}
9003
9004
9005/*
9006 * insert 'line_count' lines at 'row' in window 'wp'
9007 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9008 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9009 * scrolling.
9010 * Returns FAIL if the lines are not inserted, OK for success.
9011 */
9012 int
9013win_ins_lines(wp, row, line_count, invalid, mayclear)
9014 win_T *wp;
9015 int row;
9016 int line_count;
9017 int invalid;
9018 int mayclear;
9019{
9020 int did_delete;
9021 int nextrow;
9022 int lastrow;
9023 int retval;
9024
9025 if (invalid)
9026 wp->w_lines_valid = 0;
9027
9028 if (wp->w_height < 5)
9029 return FAIL;
9030
9031 if (line_count > wp->w_height - row)
9032 line_count = wp->w_height - row;
9033
9034 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9035 if (retval != MAYBE)
9036 return retval;
9037
9038 /*
9039 * If there is a next window or a status line, we first try to delete the
9040 * lines at the bottom to avoid messing what is after the window.
9041 * If this fails and there are following windows, don't do anything to avoid
9042 * messing up those windows, better just redraw.
9043 */
9044 did_delete = FALSE;
9045#ifdef FEAT_WINDOWS
9046 if (wp->w_next != NULL || wp->w_status_height)
9047 {
9048 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9049 line_count, (int)Rows, FALSE, NULL) == OK)
9050 did_delete = TRUE;
9051 else if (wp->w_next)
9052 return FAIL;
9053 }
9054#endif
9055 /*
9056 * if no lines deleted, blank the lines that will end up below the window
9057 */
9058 if (!did_delete)
9059 {
9060#ifdef FEAT_WINDOWS
9061 wp->w_redr_status = TRUE;
9062#endif
9063 redraw_cmdline = TRUE;
9064 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9065 lastrow = nextrow + line_count;
9066 if (lastrow > Rows)
9067 lastrow = Rows;
9068 screen_fill(nextrow - line_count, lastrow - line_count,
9069 W_WINCOL(wp), (int)W_ENDCOL(wp),
9070 ' ', ' ', 0);
9071 }
9072
9073 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9074 == FAIL)
9075 {
9076 /* deletion will have messed up other windows */
9077 if (did_delete)
9078 {
9079#ifdef FEAT_WINDOWS
9080 wp->w_redr_status = TRUE;
9081#endif
9082 win_rest_invalid(W_NEXT(wp));
9083 }
9084 return FAIL;
9085 }
9086
9087 return OK;
9088}
9089
9090/*
9091 * delete "line_count" window lines at "row" in window "wp"
9092 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9093 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9094 * scrolling
9095 * Return OK for success, FAIL if the lines are not deleted.
9096 */
9097 int
9098win_del_lines(wp, row, line_count, invalid, mayclear)
9099 win_T *wp;
9100 int row;
9101 int line_count;
9102 int invalid;
9103 int mayclear;
9104{
9105 int retval;
9106
9107 if (invalid)
9108 wp->w_lines_valid = 0;
9109
9110 if (line_count > wp->w_height - row)
9111 line_count = wp->w_height - row;
9112
9113 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9114 if (retval != MAYBE)
9115 return retval;
9116
9117 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9118 (int)Rows, FALSE, NULL) == FAIL)
9119 return FAIL;
9120
9121#ifdef FEAT_WINDOWS
9122 /*
9123 * If there are windows or status lines below, try to put them at the
9124 * correct place. If we can't do that, they have to be redrawn.
9125 */
9126 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9127 {
9128 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9129 line_count, (int)Rows, NULL) == FAIL)
9130 {
9131 wp->w_redr_status = TRUE;
9132 win_rest_invalid(wp->w_next);
9133 }
9134 }
9135 /*
9136 * If this is the last window and there is no status line, redraw the
9137 * command line later.
9138 */
9139 else
9140#endif
9141 redraw_cmdline = TRUE;
9142 return OK;
9143}
9144
9145/*
9146 * Common code for win_ins_lines() and win_del_lines().
9147 * Returns OK or FAIL when the work has been done.
9148 * Returns MAYBE when not finished yet.
9149 */
9150 static int
9151win_do_lines(wp, row, line_count, mayclear, del)
9152 win_T *wp;
9153 int row;
9154 int line_count;
9155 int mayclear;
9156 int del;
9157{
9158 int retval;
9159
9160 if (!redrawing() || line_count <= 0)
9161 return FAIL;
9162
9163 /* only a few lines left: redraw is faster */
9164 if (mayclear && Rows - line_count < 5
9165#ifdef FEAT_VERTSPLIT
9166 && wp->w_width == Columns
9167#endif
9168 )
9169 {
9170 screenclear(); /* will set wp->w_lines_valid to 0 */
9171 return FAIL;
9172 }
9173
9174 /*
9175 * Delete all remaining lines
9176 */
9177 if (row + line_count >= wp->w_height)
9178 {
9179 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9180 W_WINCOL(wp), (int)W_ENDCOL(wp),
9181 ' ', ' ', 0);
9182 return OK;
9183 }
9184
9185 /*
9186 * when scrolling, the message on the command line should be cleared,
9187 * otherwise it will stay there forever.
9188 */
9189 clear_cmdline = TRUE;
9190
9191 /*
9192 * If the terminal can set a scroll region, use that.
9193 * Always do this in a vertically split window. This will redraw from
9194 * ScreenLines[] when t_CV isn't defined. That's faster than using
9195 * win_line().
9196 * Don't use a scroll region when we are going to redraw the text, writing
9197 * a character in the lower right corner of the scroll region causes a
9198 * scroll-up in the DJGPP version.
9199 */
9200 if (scroll_region
9201#ifdef FEAT_VERTSPLIT
9202 || W_WIDTH(wp) != Columns
9203#endif
9204 )
9205 {
9206#ifdef FEAT_VERTSPLIT
9207 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9208#endif
9209 scroll_region_set(wp, row);
9210 if (del)
9211 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9212 wp->w_height - row, FALSE, wp);
9213 else
9214 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9215 wp->w_height - row, wp);
9216#ifdef FEAT_VERTSPLIT
9217 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9218#endif
9219 scroll_region_reset();
9220 return retval;
9221 }
9222
9223#ifdef FEAT_WINDOWS
9224 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9225 return FAIL;
9226#endif
9227
9228 return MAYBE;
9229}
9230
9231/*
9232 * window 'wp' and everything after it is messed up, mark it for redraw
9233 */
9234 static void
9235win_rest_invalid(wp)
9236 win_T *wp;
9237{
9238#ifdef FEAT_WINDOWS
9239 while (wp != NULL)
9240#else
9241 if (wp != NULL)
9242#endif
9243 {
9244 redraw_win_later(wp, NOT_VALID);
9245#ifdef FEAT_WINDOWS
9246 wp->w_redr_status = TRUE;
9247 wp = wp->w_next;
9248#endif
9249 }
9250 redraw_cmdline = TRUE;
9251}
9252
9253/*
9254 * The rest of the routines in this file perform screen manipulations. The
9255 * given operation is performed physically on the screen. The corresponding
9256 * change is also made to the internal screen image. In this way, the editor
9257 * anticipates the effect of editing changes on the appearance of the screen.
9258 * That way, when we call screenupdate a complete redraw isn't usually
9259 * necessary. Another advantage is that we can keep adding code to anticipate
9260 * screen changes, and in the meantime, everything still works.
9261 */
9262
9263/*
9264 * types for inserting or deleting lines
9265 */
9266#define USE_T_CAL 1
9267#define USE_T_CDL 2
9268#define USE_T_AL 3
9269#define USE_T_CE 4
9270#define USE_T_DL 5
9271#define USE_T_SR 6
9272#define USE_NL 7
9273#define USE_T_CD 8
9274#define USE_REDRAW 9
9275
9276/*
9277 * insert lines on the screen and update ScreenLines[]
9278 * 'end' is the line after the scrolled part. Normally it is Rows.
9279 * When scrolling region used 'off' is the offset from the top for the region.
9280 * 'row' and 'end' are relative to the start of the region.
9281 *
9282 * return FAIL for failure, OK for success.
9283 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009284 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285screen_ins_lines(off, row, line_count, end, wp)
9286 int off;
9287 int row;
9288 int line_count;
9289 int end;
9290 win_T *wp; /* NULL or window to use width from */
9291{
9292 int i;
9293 int j;
9294 unsigned temp;
9295 int cursor_row;
9296 int type;
9297 int result_empty;
9298 int can_ce = can_clear(T_CE);
9299
9300 /*
9301 * FAIL if
9302 * - there is no valid screen
9303 * - the screen has to be redrawn completely
9304 * - the line count is less than one
9305 * - the line count is more than 'ttyscroll'
9306 */
9307 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9308 return FAIL;
9309
9310 /*
9311 * There are seven ways to insert lines:
9312 * 0. When in a vertically split window and t_CV isn't set, redraw the
9313 * characters from ScreenLines[].
9314 * 1. Use T_CD (clear to end of display) if it exists and the result of
9315 * the insert is just empty lines
9316 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9317 * present or line_count > 1. It looks better if we do all the inserts
9318 * at once.
9319 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9320 * insert is just empty lines and T_CE is not present or line_count >
9321 * 1.
9322 * 4. Use T_AL (insert line) if it exists.
9323 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9324 * just empty lines.
9325 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9326 * just empty lines.
9327 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9328 * the 'da' flag is not set or we have clear line capability.
9329 * 8. redraw the characters from ScreenLines[].
9330 *
9331 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9332 * the scrollbar for the window. It does have insert line, use that if it
9333 * exists.
9334 */
9335 result_empty = (row + line_count >= end);
9336#ifdef FEAT_VERTSPLIT
9337 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9338 type = USE_REDRAW;
9339 else
9340#endif
9341 if (can_clear(T_CD) && result_empty)
9342 type = USE_T_CD;
9343 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9344 type = USE_T_CAL;
9345 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9346 type = USE_T_CDL;
9347 else if (*T_AL != NUL)
9348 type = USE_T_AL;
9349 else if (can_ce && result_empty)
9350 type = USE_T_CE;
9351 else if (*T_DL != NUL && result_empty)
9352 type = USE_T_DL;
9353 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9354 type = USE_T_SR;
9355 else
9356 return FAIL;
9357
9358 /*
9359 * For clearing the lines screen_del_lines() is used. This will also take
9360 * care of t_db if necessary.
9361 */
9362 if (type == USE_T_CD || type == USE_T_CDL ||
9363 type == USE_T_CE || type == USE_T_DL)
9364 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9365
9366 /*
9367 * If text is retained below the screen, first clear or delete as many
9368 * lines at the bottom of the window as are about to be inserted so that
9369 * the deleted lines won't later surface during a screen_del_lines.
9370 */
9371 if (*T_DB)
9372 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9373
9374#ifdef FEAT_CLIPBOARD
9375 /* Remove a modeless selection when inserting lines halfway the screen
9376 * or not the full width of the screen. */
9377 if (off + row > 0
9378# ifdef FEAT_VERTSPLIT
9379 || (wp != NULL && wp->w_width != Columns)
9380# endif
9381 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009382 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 else
9384 clip_scroll_selection(-line_count);
9385#endif
9386
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387#ifdef FEAT_GUI
9388 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9389 * scrolling is actually carried out. */
9390 gui_dont_update_cursor();
9391#endif
9392
9393 if (*T_CCS != NUL) /* cursor relative to region */
9394 cursor_row = row;
9395 else
9396 cursor_row = row + off;
9397
9398 /*
9399 * Shift LineOffset[] line_count down to reflect the inserted lines.
9400 * Clear the inserted lines in ScreenLines[].
9401 */
9402 row += off;
9403 end += off;
9404 for (i = 0; i < line_count; ++i)
9405 {
9406#ifdef FEAT_VERTSPLIT
9407 if (wp != NULL && wp->w_width != Columns)
9408 {
9409 /* need to copy part of a line */
9410 j = end - 1 - i;
9411 while ((j -= line_count) >= row)
9412 linecopy(j + line_count, j, wp);
9413 j += line_count;
9414 if (can_clear((char_u *)" "))
9415 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9416 else
9417 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9418 LineWraps[j] = FALSE;
9419 }
9420 else
9421#endif
9422 {
9423 j = end - 1 - i;
9424 temp = LineOffset[j];
9425 while ((j -= line_count) >= row)
9426 {
9427 LineOffset[j + line_count] = LineOffset[j];
9428 LineWraps[j + line_count] = LineWraps[j];
9429 }
9430 LineOffset[j + line_count] = temp;
9431 LineWraps[j + line_count] = FALSE;
9432 if (can_clear((char_u *)" "))
9433 lineclear(temp, (int)Columns);
9434 else
9435 lineinvalid(temp, (int)Columns);
9436 }
9437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438
9439 screen_stop_highlight();
9440 windgoto(cursor_row, 0);
9441
9442#ifdef FEAT_VERTSPLIT
9443 /* redraw the characters */
9444 if (type == USE_REDRAW)
9445 redraw_block(row, end, wp);
9446 else
9447#endif
9448 if (type == USE_T_CAL)
9449 {
9450 term_append_lines(line_count);
9451 screen_start(); /* don't know where cursor is now */
9452 }
9453 else
9454 {
9455 for (i = 0; i < line_count; i++)
9456 {
9457 if (type == USE_T_AL)
9458 {
9459 if (i && cursor_row != 0)
9460 windgoto(cursor_row, 0);
9461 out_str(T_AL);
9462 }
9463 else /* type == USE_T_SR */
9464 out_str(T_SR);
9465 screen_start(); /* don't know where cursor is now */
9466 }
9467 }
9468
9469 /*
9470 * With scroll-reverse and 'da' flag set we need to clear the lines that
9471 * have been scrolled down into the region.
9472 */
9473 if (type == USE_T_SR && *T_DA)
9474 {
9475 for (i = 0; i < line_count; ++i)
9476 {
9477 windgoto(off + i, 0);
9478 out_str(T_CE);
9479 screen_start(); /* don't know where cursor is now */
9480 }
9481 }
9482
9483#ifdef FEAT_GUI
9484 gui_can_update_cursor();
9485 if (gui.in_use)
9486 out_flush(); /* always flush after a scroll */
9487#endif
9488 return OK;
9489}
9490
9491/*
9492 * delete lines on the screen and update ScreenLines[]
9493 * 'end' is the line after the scrolled part. Normally it is Rows.
9494 * When scrolling region used 'off' is the offset from the top for the region.
9495 * 'row' and 'end' are relative to the start of the region.
9496 *
9497 * Return OK for success, FAIL if the lines are not deleted.
9498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 int
9500screen_del_lines(off, row, line_count, end, force, wp)
9501 int off;
9502 int row;
9503 int line_count;
9504 int end;
9505 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009506 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507{
9508 int j;
9509 int i;
9510 unsigned temp;
9511 int cursor_row;
9512 int cursor_end;
9513 int result_empty; /* result is empty until end of region */
9514 int can_delete; /* deleting line codes can be used */
9515 int type;
9516
9517 /*
9518 * FAIL if
9519 * - there is no valid screen
9520 * - the screen has to be redrawn completely
9521 * - the line count is less than one
9522 * - the line count is more than 'ttyscroll'
9523 */
9524 if (!screen_valid(TRUE) || line_count <= 0 ||
9525 (!force && line_count > p_ttyscroll))
9526 return FAIL;
9527
9528 /*
9529 * Check if the rest of the current region will become empty.
9530 */
9531 result_empty = row + line_count >= end;
9532
9533 /*
9534 * We can delete lines only when 'db' flag not set or when 'ce' option
9535 * available.
9536 */
9537 can_delete = (*T_DB == NUL || can_clear(T_CE));
9538
9539 /*
9540 * There are six ways to delete lines:
9541 * 0. When in a vertically split window and t_CV isn't set, redraw the
9542 * characters from ScreenLines[].
9543 * 1. Use T_CD if it exists and the result is empty.
9544 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9545 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9546 * none of the other ways work.
9547 * 4. Use T_CE (erase line) if the result is empty.
9548 * 5. Use T_DL (delete line) if it exists.
9549 * 6. redraw the characters from ScreenLines[].
9550 */
9551#ifdef FEAT_VERTSPLIT
9552 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9553 type = USE_REDRAW;
9554 else
9555#endif
9556 if (can_clear(T_CD) && result_empty)
9557 type = USE_T_CD;
9558#if defined(__BEOS__) && defined(BEOS_DR8)
9559 /*
9560 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9561 * its internal termcap... this works okay for tests which test *T_DB !=
9562 * NUL. It has the disadvantage that the user cannot use any :set t_*
9563 * command to get T_DB (back) to empty_option, only :set term=... will do
9564 * the trick...
9565 * Anyway, this hack will hopefully go away with the next OS release.
9566 * (Olaf Seibert)
9567 */
9568 else if (row == 0 && T_DB == empty_option
9569 && (line_count == 1 || *T_CDL == NUL))
9570#else
9571 else if (row == 0 && (
9572#ifndef AMIGA
9573 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9574 * up, so use delete-line command */
9575 line_count == 1 ||
9576#endif
9577 *T_CDL == NUL))
9578#endif
9579 type = USE_NL;
9580 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9581 type = USE_T_CDL;
9582 else if (can_clear(T_CE) && result_empty
9583#ifdef FEAT_VERTSPLIT
9584 && (wp == NULL || wp->w_width == Columns)
9585#endif
9586 )
9587 type = USE_T_CE;
9588 else if (*T_DL != NUL && can_delete)
9589 type = USE_T_DL;
9590 else if (*T_CDL != NUL && can_delete)
9591 type = USE_T_CDL;
9592 else
9593 return FAIL;
9594
9595#ifdef FEAT_CLIPBOARD
9596 /* Remove a modeless selection when deleting lines halfway the screen or
9597 * not the full width of the screen. */
9598 if (off + row > 0
9599# ifdef FEAT_VERTSPLIT
9600 || (wp != NULL && wp->w_width != Columns)
9601# endif
9602 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009603 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 else
9605 clip_scroll_selection(line_count);
9606#endif
9607
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608#ifdef FEAT_GUI
9609 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9610 * scrolling is actually carried out. */
9611 gui_dont_update_cursor();
9612#endif
9613
9614 if (*T_CCS != NUL) /* cursor relative to region */
9615 {
9616 cursor_row = row;
9617 cursor_end = end;
9618 }
9619 else
9620 {
9621 cursor_row = row + off;
9622 cursor_end = end + off;
9623 }
9624
9625 /*
9626 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9627 * Clear the inserted lines in ScreenLines[].
9628 */
9629 row += off;
9630 end += off;
9631 for (i = 0; i < line_count; ++i)
9632 {
9633#ifdef FEAT_VERTSPLIT
9634 if (wp != NULL && wp->w_width != Columns)
9635 {
9636 /* need to copy part of a line */
9637 j = row + i;
9638 while ((j += line_count) <= end - 1)
9639 linecopy(j - line_count, j, wp);
9640 j -= line_count;
9641 if (can_clear((char_u *)" "))
9642 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9643 else
9644 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9645 LineWraps[j] = FALSE;
9646 }
9647 else
9648#endif
9649 {
9650 /* whole width, moving the line pointers is faster */
9651 j = row + i;
9652 temp = LineOffset[j];
9653 while ((j += line_count) <= end - 1)
9654 {
9655 LineOffset[j - line_count] = LineOffset[j];
9656 LineWraps[j - line_count] = LineWraps[j];
9657 }
9658 LineOffset[j - line_count] = temp;
9659 LineWraps[j - line_count] = FALSE;
9660 if (can_clear((char_u *)" "))
9661 lineclear(temp, (int)Columns);
9662 else
9663 lineinvalid(temp, (int)Columns);
9664 }
9665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666
9667 screen_stop_highlight();
9668
9669#ifdef FEAT_VERTSPLIT
9670 /* redraw the characters */
9671 if (type == USE_REDRAW)
9672 redraw_block(row, end, wp);
9673 else
9674#endif
9675 if (type == USE_T_CD) /* delete the lines */
9676 {
9677 windgoto(cursor_row, 0);
9678 out_str(T_CD);
9679 screen_start(); /* don't know where cursor is now */
9680 }
9681 else if (type == USE_T_CDL)
9682 {
9683 windgoto(cursor_row, 0);
9684 term_delete_lines(line_count);
9685 screen_start(); /* don't know where cursor is now */
9686 }
9687 /*
9688 * Deleting lines at top of the screen or scroll region: Just scroll
9689 * the whole screen (scroll region) up by outputting newlines on the
9690 * last line.
9691 */
9692 else if (type == USE_NL)
9693 {
9694 windgoto(cursor_end - 1, 0);
9695 for (i = line_count; --i >= 0; )
9696 out_char('\n'); /* cursor will remain on same line */
9697 }
9698 else
9699 {
9700 for (i = line_count; --i >= 0; )
9701 {
9702 if (type == USE_T_DL)
9703 {
9704 windgoto(cursor_row, 0);
9705 out_str(T_DL); /* delete a line */
9706 }
9707 else /* type == USE_T_CE */
9708 {
9709 windgoto(cursor_row + i, 0);
9710 out_str(T_CE); /* erase a line */
9711 }
9712 screen_start(); /* don't know where cursor is now */
9713 }
9714 }
9715
9716 /*
9717 * If the 'db' flag is set, we need to clear the lines that have been
9718 * scrolled up at the bottom of the region.
9719 */
9720 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9721 {
9722 for (i = line_count; i > 0; --i)
9723 {
9724 windgoto(cursor_end - i, 0);
9725 out_str(T_CE); /* erase a line */
9726 screen_start(); /* don't know where cursor is now */
9727 }
9728 }
9729
9730#ifdef FEAT_GUI
9731 gui_can_update_cursor();
9732 if (gui.in_use)
9733 out_flush(); /* always flush after a scroll */
9734#endif
9735
9736 return OK;
9737}
9738
9739/*
9740 * show the current mode and ruler
9741 *
9742 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9743 * If clear_cmdline is FALSE there may be a message there that needs to be
9744 * cleared only if a mode is shown.
9745 * Return the length of the message (0 if no message).
9746 */
9747 int
9748showmode()
9749{
9750 int need_clear;
9751 int length = 0;
9752 int do_mode;
9753 int attr;
9754 int nwr_save;
9755#ifdef FEAT_INS_EXPAND
9756 int sub_attr;
9757#endif
9758
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009759 do_mode = ((p_smd && msg_silent == 0)
9760 && ((State & INSERT)
9761 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009762 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 if (do_mode || Recording)
9764 {
9765 /*
9766 * Don't show mode right now, when not redrawing or inside a mapping.
9767 * Call char_avail() only when we are going to show something, because
9768 * it takes a bit of time.
9769 */
9770 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9771 {
9772 redraw_cmdline = TRUE; /* show mode later */
9773 return 0;
9774 }
9775
9776 nwr_save = need_wait_return;
9777
9778 /* wait a bit before overwriting an important message */
9779 check_for_delay(FALSE);
9780
9781 /* if the cmdline is more than one line high, erase top lines */
9782 need_clear = clear_cmdline;
9783 if (clear_cmdline && cmdline_row < Rows - 1)
9784 msg_clr_cmdline(); /* will reset clear_cmdline */
9785
9786 /* Position on the last line in the window, column 0 */
9787 msg_pos_mode();
9788 cursor_off();
9789 attr = hl_attr(HLF_CM); /* Highlight mode */
9790 if (do_mode)
9791 {
9792 MSG_PUTS_ATTR("--", attr);
9793#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009794 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009795# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009796 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009797# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009798 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009799# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009800 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009801# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 MSG_PUTS_ATTR(" IM", attr);
9803# else
9804 MSG_PUTS_ATTR(" XIM", attr);
9805# endif
9806#endif
9807#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9808 if (gui.in_use)
9809 {
9810 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009811 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 }
9813#endif
9814#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009815 /* CTRL-X in Insert mode */
9816 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 {
9818 /* These messages can get long, avoid a wrap in a narrow
9819 * window. Prefer showing edit_submode_extra. */
9820 length = (Rows - msg_row) * Columns - 3;
9821 if (edit_submode_extra != NULL)
9822 length -= vim_strsize(edit_submode_extra);
9823 if (length > 0)
9824 {
9825 if (edit_submode_pre != NULL)
9826 length -= vim_strsize(edit_submode_pre);
9827 if (length - vim_strsize(edit_submode) > 0)
9828 {
9829 if (edit_submode_pre != NULL)
9830 msg_puts_attr(edit_submode_pre, attr);
9831 msg_puts_attr(edit_submode, attr);
9832 }
9833 if (edit_submode_extra != NULL)
9834 {
9835 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9836 if ((int)edit_submode_highl < (int)HLF_COUNT)
9837 sub_attr = hl_attr(edit_submode_highl);
9838 else
9839 sub_attr = attr;
9840 msg_puts_attr(edit_submode_extra, sub_attr);
9841 }
9842 }
9843 length = 0;
9844 }
9845 else
9846#endif
9847 {
9848#ifdef FEAT_VREPLACE
9849 if (State & VREPLACE_FLAG)
9850 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9851 else
9852#endif
9853 if (State & REPLACE_FLAG)
9854 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9855 else if (State & INSERT)
9856 {
9857#ifdef FEAT_RIGHTLEFT
9858 if (p_ri)
9859 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9860#endif
9861 MSG_PUTS_ATTR(_(" INSERT"), attr);
9862 }
9863 else if (restart_edit == 'I')
9864 MSG_PUTS_ATTR(_(" (insert)"), attr);
9865 else if (restart_edit == 'R')
9866 MSG_PUTS_ATTR(_(" (replace)"), attr);
9867 else if (restart_edit == 'V')
9868 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9869#ifdef FEAT_RIGHTLEFT
9870 if (p_hkmap)
9871 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9872# ifdef FEAT_FKMAP
9873 if (p_fkmap)
9874 MSG_PUTS_ATTR(farsi_text_5, attr);
9875# endif
9876#endif
9877#ifdef FEAT_KEYMAP
9878 if (State & LANGMAP)
9879 {
9880# ifdef FEAT_ARABIC
9881 if (curwin->w_p_arab)
9882 MSG_PUTS_ATTR(_(" Arabic"), attr);
9883 else
9884# endif
9885 MSG_PUTS_ATTR(_(" (lang)"), attr);
9886 }
9887#endif
9888 if ((State & INSERT) && p_paste)
9889 MSG_PUTS_ATTR(_(" (paste)"), attr);
9890
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 if (VIsual_active)
9892 {
9893 char *p;
9894
9895 /* Don't concatenate separate words to avoid translation
9896 * problems. */
9897 switch ((VIsual_select ? 4 : 0)
9898 + (VIsual_mode == Ctrl_V) * 2
9899 + (VIsual_mode == 'V'))
9900 {
9901 case 0: p = N_(" VISUAL"); break;
9902 case 1: p = N_(" VISUAL LINE"); break;
9903 case 2: p = N_(" VISUAL BLOCK"); break;
9904 case 4: p = N_(" SELECT"); break;
9905 case 5: p = N_(" SELECT LINE"); break;
9906 default: p = N_(" SELECT BLOCK"); break;
9907 }
9908 MSG_PUTS_ATTR(_(p), attr);
9909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 MSG_PUTS_ATTR(" --", attr);
9911 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009912
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 need_clear = TRUE;
9914 }
9915 if (Recording
9916#ifdef FEAT_INS_EXPAND
9917 && edit_submode == NULL /* otherwise it gets too long */
9918#endif
9919 )
9920 {
9921 MSG_PUTS_ATTR(_("recording"), attr);
9922 need_clear = TRUE;
9923 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009924
9925 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 if (need_clear || clear_cmdline)
9927 msg_clr_eos();
9928 msg_didout = FALSE; /* overwrite this message */
9929 length = msg_col;
9930 msg_col = 0;
9931 need_wait_return = nwr_save; /* never ask for hit-return for this */
9932 }
9933 else if (clear_cmdline && msg_silent == 0)
9934 /* Clear the whole command line. Will reset "clear_cmdline". */
9935 msg_clr_cmdline();
9936
9937#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 /* In Visual mode the size of the selected area must be redrawn. */
9939 if (VIsual_active)
9940 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
9942 /* If the last window has no status line, the ruler is after the mode
9943 * message and must be redrawn */
9944 if (redrawing()
9945# ifdef FEAT_WINDOWS
9946 && lastwin->w_status_height == 0
9947# endif
9948 )
9949 win_redr_ruler(lastwin, TRUE);
9950#endif
9951 redraw_cmdline = FALSE;
9952 clear_cmdline = FALSE;
9953
9954 return length;
9955}
9956
9957/*
9958 * Position for a mode message.
9959 */
9960 static void
9961msg_pos_mode()
9962{
9963 msg_col = 0;
9964 msg_row = Rows - 1;
9965}
9966
9967/*
9968 * Delete mode message. Used when ESC is typed which is expected to end
9969 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009970 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 */
9972 void
9973unshowmode(force)
9974 int force;
9975{
9976 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009977 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 */
9979 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9980 redraw_cmdline = TRUE; /* delete mode later */
9981 else
9982 {
9983 msg_pos_mode();
9984 if (Recording)
9985 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9986 msg_clr_eos();
9987 }
9988}
9989
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009990#if defined(FEAT_WINDOWS)
9991/*
9992 * Draw the tab pages line at the top of the Vim window.
9993 */
9994 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009995draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009996{
9997 int tabcount = 0;
9998 tabpage_T *tp;
9999 int tabwidth;
10000 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010001 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010002 int attr;
10003 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010004 win_T *cwp;
10005 int wincount;
10006 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010007 int c;
10008 int len;
10009 int attr_sel = hl_attr(HLF_TPS);
10010 int attr_nosel = hl_attr(HLF_TP);
10011 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010012 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010013 int room;
10014 int use_sep_chars = (t_colors < 8
10015#ifdef FEAT_GUI
10016 && !gui.in_use
10017#endif
10018 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010019
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010020 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010021
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010022#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010023 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010024 if (gui_use_tabline())
10025 {
10026 gui_update_tabline();
10027 return;
10028 }
10029#endif
10030
10031 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010032 return;
10033
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010034#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010035
10036 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10037 for (scol = 0; scol < Columns; ++scol)
10038 TabPageIdxs[scol] = 0;
10039
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010040 /* Use the 'tabline' option if it's set. */
10041 if (*p_tal != NUL)
10042 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010043 int save_called_emsg = called_emsg;
10044
10045 /* Check for an error. If there is one we would loop in redrawing the
10046 * screen. Avoid that by making 'tabline' empty. */
10047 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010048 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010049 if (called_emsg)
10050 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010051 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010052 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010053 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010054 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010055#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010056 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010057 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10058 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010059
Bram Moolenaar238a5642006-02-21 22:12:05 +000010060 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10061 if (tabwidth < 6)
10062 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010063
Bram Moolenaar238a5642006-02-21 22:12:05 +000010064 attr = attr_nosel;
10065 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010066 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010067 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10068 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010069 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010070 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010071
Bram Moolenaar238a5642006-02-21 22:12:05 +000010072 if (tp->tp_topframe == topframe)
10073 attr = attr_sel;
10074 if (use_sep_chars && col > 0)
10075 screen_putchar('|', 0, col++, attr);
10076
10077 if (tp->tp_topframe != topframe)
10078 attr = attr_nosel;
10079
10080 screen_putchar(' ', 0, col++, attr);
10081
10082 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010083 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010084 cwp = curwin;
10085 wp = firstwin;
10086 }
10087 else
10088 {
10089 cwp = tp->tp_curwin;
10090 wp = tp->tp_firstwin;
10091 }
10092
10093 modified = FALSE;
10094 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10095 if (bufIsChanged(wp->w_buffer))
10096 modified = TRUE;
10097 if (modified || wincount > 1)
10098 {
10099 if (wincount > 1)
10100 {
10101 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010102 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010103 if (col + len >= Columns - 3)
10104 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010105 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010106#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010107 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010108#else
Bram Moolenaar238a5642006-02-21 22:12:05 +000010109 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010110#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010111 );
10112 col += len;
10113 }
10114 if (modified)
10115 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10116 screen_putchar(' ', 0, col++, attr);
10117 }
10118
10119 room = scol - col + tabwidth - 1;
10120 if (room > 0)
10121 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010122 /* Get buffer name in NameBuff[] */
10123 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010124 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010125 len = vim_strsize(NameBuff);
10126 p = NameBuff;
10127#ifdef FEAT_MBYTE
10128 if (has_mbyte)
10129 while (len > room)
10130 {
10131 len -= ptr2cells(p);
10132 mb_ptr_adv(p);
10133 }
10134 else
10135#endif
10136 if (len > room)
10137 {
10138 p += len - room;
10139 len = room;
10140 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010141 if (len > Columns - col - 1)
10142 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010144 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010145 col += len;
10146 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010147 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010148
10149 /* Store the tab page number in TabPageIdxs[], so that
10150 * jump_to_mouse() knows where each one is. */
10151 ++tabcount;
10152 while (scol < col)
10153 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010154 }
10155
Bram Moolenaar238a5642006-02-21 22:12:05 +000010156 if (use_sep_chars)
10157 c = '_';
10158 else
10159 c = ' ';
10160 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010161
10162 /* Put an "X" for closing the current tab if there are several. */
10163 if (first_tabpage->tp_next != NULL)
10164 {
10165 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10166 TabPageIdxs[Columns - 1] = -999;
10167 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010168 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010169
10170 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10171 * set. */
10172 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010173}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010174
10175/*
10176 * Get buffer name for "buf" into NameBuff[].
10177 * Takes care of special buffer names and translates special characters.
10178 */
10179 void
10180get_trans_bufname(buf)
10181 buf_T *buf;
10182{
10183 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010184 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010185 else
10186 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10187 trans_characters(NameBuff, MAXPATHL);
10188}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010189#endif
10190
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10192/*
10193 * Get the character to use in a status line. Get its attributes in "*attr".
10194 */
10195 static int
10196fillchar_status(attr, is_curwin)
10197 int *attr;
10198 int is_curwin;
10199{
10200 int fill;
10201 if (is_curwin)
10202 {
10203 *attr = hl_attr(HLF_S);
10204 fill = fill_stl;
10205 }
10206 else
10207 {
10208 *attr = hl_attr(HLF_SNC);
10209 fill = fill_stlnc;
10210 }
10211 /* Use fill when there is highlighting, and highlighting of current
10212 * window differs, or the fillchars differ, or this is not the
10213 * current window */
10214 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10215 || !is_curwin || firstwin == lastwin)
10216 || (fill_stl != fill_stlnc)))
10217 return fill;
10218 if (is_curwin)
10219 return '^';
10220 return '=';
10221}
10222#endif
10223
10224#ifdef FEAT_VERTSPLIT
10225/*
10226 * Get the character to use in a separator between vertically split windows.
10227 * Get its attributes in "*attr".
10228 */
10229 static int
10230fillchar_vsep(attr)
10231 int *attr;
10232{
10233 *attr = hl_attr(HLF_C);
10234 if (*attr == 0 && fill_vert == ' ')
10235 return '|';
10236 else
10237 return fill_vert;
10238}
10239#endif
10240
10241/*
10242 * Return TRUE if redrawing should currently be done.
10243 */
10244 int
10245redrawing()
10246{
10247 return (!RedrawingDisabled
10248 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10249}
10250
10251/*
10252 * Return TRUE if printing messages should currently be done.
10253 */
10254 int
10255messaging()
10256{
10257 return (!(p_lz && char_avail() && !KeyTyped));
10258}
10259
10260/*
10261 * Show current status info in ruler and various other places
10262 * If always is FALSE, only show ruler if position has changed.
10263 */
10264 void
10265showruler(always)
10266 int always;
10267{
10268 if (!always && !redrawing())
10269 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010270#ifdef FEAT_INS_EXPAND
10271 if (pum_visible())
10272 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010273# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010274 /* Don't redraw right now, do it later. */
10275 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010276# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010277 return;
10278 }
10279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010281 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010282 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010283 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 else
10286#endif
10287#ifdef FEAT_CMDL_INFO
10288 win_redr_ruler(curwin, always);
10289#endif
10290
10291#ifdef FEAT_TITLE
10292 if (need_maketitle
10293# ifdef FEAT_STL_OPT
10294 || (p_icon && (stl_syntax & STL_IN_ICON))
10295 || (p_title && (stl_syntax & STL_IN_TITLE))
10296# endif
10297 )
10298 maketitle();
10299#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010300#ifdef FEAT_WINDOWS
10301 /* Redraw the tab pages line if needed. */
10302 if (redraw_tabline)
10303 draw_tabline();
10304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305}
10306
10307#ifdef FEAT_CMDL_INFO
10308 static void
10309win_redr_ruler(wp, always)
10310 win_T *wp;
10311 int always;
10312{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010313#define RULER_BUF_LEN 70
10314 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 int row;
10316 int fillchar;
10317 int attr;
10318 int empty_line = FALSE;
10319 colnr_T virtcol;
10320 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010321 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 int o;
10323#ifdef FEAT_VERTSPLIT
10324 int this_ru_col;
10325 int off = 0;
10326 int width = Columns;
10327# define WITH_OFF(x) x
10328# define WITH_WIDTH(x) x
10329#else
10330# define WITH_OFF(x) 0
10331# define WITH_WIDTH(x) Columns
10332# define this_ru_col ru_col
10333#endif
10334
10335 /* If 'ruler' off or redrawing disabled, don't do anything */
10336 if (!p_ru)
10337 return;
10338
10339 /*
10340 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10341 * after deleting lines, before cursor.lnum is corrected.
10342 */
10343 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10344 return;
10345
10346#ifdef FEAT_INS_EXPAND
10347 /* Don't draw the ruler while doing insert-completion, it might overwrite
10348 * the (long) mode message. */
10349# ifdef FEAT_WINDOWS
10350 if (wp == lastwin && lastwin->w_status_height == 0)
10351# endif
10352 if (edit_submode != NULL)
10353 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010354 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10355 if (pum_visible())
10356 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357#endif
10358
10359#ifdef FEAT_STL_OPT
10360 if (*p_ruf)
10361 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010362 int save_called_emsg = called_emsg;
10363
10364 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010366 if (called_emsg)
10367 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010368 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010369 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 return;
10371 }
10372#endif
10373
10374 /*
10375 * Check if not in Insert mode and the line is empty (will show "0-1").
10376 */
10377 if (!(State & INSERT)
10378 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10379 empty_line = TRUE;
10380
10381 /*
10382 * Only draw the ruler when something changed.
10383 */
10384 validate_virtcol_win(wp);
10385 if ( redraw_cmdline
10386 || always
10387 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10388 || wp->w_cursor.col != wp->w_ru_cursor.col
10389 || wp->w_virtcol != wp->w_ru_virtcol
10390#ifdef FEAT_VIRTUALEDIT
10391 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10392#endif
10393 || wp->w_topline != wp->w_ru_topline
10394 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10395#ifdef FEAT_DIFF
10396 || wp->w_topfill != wp->w_ru_topfill
10397#endif
10398 || empty_line != wp->w_ru_empty)
10399 {
10400 cursor_off();
10401#ifdef FEAT_WINDOWS
10402 if (wp->w_status_height)
10403 {
10404 row = W_WINROW(wp) + wp->w_height;
10405 fillchar = fillchar_status(&attr, wp == curwin);
10406# ifdef FEAT_VERTSPLIT
10407 off = W_WINCOL(wp);
10408 width = W_WIDTH(wp);
10409# endif
10410 }
10411 else
10412#endif
10413 {
10414 row = Rows - 1;
10415 fillchar = ' ';
10416 attr = 0;
10417#ifdef FEAT_VERTSPLIT
10418 width = Columns;
10419 off = 0;
10420#endif
10421 }
10422
10423 /* In list mode virtcol needs to be recomputed */
10424 virtcol = wp->w_virtcol;
10425 if (wp->w_p_list && lcs_tab1 == NUL)
10426 {
10427 wp->w_p_list = FALSE;
10428 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10429 wp->w_p_list = TRUE;
10430 }
10431
10432 /*
10433 * Some sprintfs return the length, some return a pointer.
10434 * To avoid portability problems we use strlen() here.
10435 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010436 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10438 ? 0L
10439 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010440 len = STRLEN(buffer);
10441 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10443 (int)virtcol + 1);
10444
10445 /*
10446 * Add a "50%" if there is room for it.
10447 * On the last line, don't print in the last column (scrolls the
10448 * screen up on some terminals).
10449 */
10450 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010451 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 o = i + vim_strsize(buffer + i + 1);
10453#ifdef FEAT_WINDOWS
10454 if (wp->w_status_height == 0) /* can't use last char of screen */
10455#endif
10456 ++o;
10457#ifdef FEAT_VERTSPLIT
10458 this_ru_col = ru_col - (Columns - width);
10459 if (this_ru_col < 0)
10460 this_ru_col = 0;
10461#endif
10462 /* Never use more than half the window/screen width, leave the other
10463 * half for the filename. */
10464 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10465 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10466 if (this_ru_col + o < WITH_WIDTH(width))
10467 {
10468 while (this_ru_col + o < WITH_WIDTH(width))
10469 {
10470#ifdef FEAT_MBYTE
10471 if (has_mbyte)
10472 i += (*mb_char2bytes)(fillchar, buffer + i);
10473 else
10474#endif
10475 buffer[i++] = fillchar;
10476 ++o;
10477 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010478 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 }
10480 /* Truncate at window boundary. */
10481#ifdef FEAT_MBYTE
10482 if (has_mbyte)
10483 {
10484 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010485 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 {
10487 o += (*mb_ptr2cells)(buffer + i);
10488 if (this_ru_col + o > WITH_WIDTH(width))
10489 {
10490 buffer[i] = NUL;
10491 break;
10492 }
10493 }
10494 }
10495 else
10496#endif
10497 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10498 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10499
10500 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10501 i = redraw_cmdline;
10502 screen_fill(row, row + 1,
10503 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10504 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10505 fillchar, fillchar, attr);
10506 /* don't redraw the cmdline because of showing the ruler */
10507 redraw_cmdline = i;
10508 wp->w_ru_cursor = wp->w_cursor;
10509 wp->w_ru_virtcol = wp->w_virtcol;
10510 wp->w_ru_empty = empty_line;
10511 wp->w_ru_topline = wp->w_topline;
10512 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10513#ifdef FEAT_DIFF
10514 wp->w_ru_topfill = wp->w_topfill;
10515#endif
10516 }
10517}
10518#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010519
10520#if defined(FEAT_LINEBREAK) || defined(PROTO)
10521/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010522 * Return the width of the 'number' and 'relativenumber' column.
10523 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010524 * Otherwise it depends on 'numberwidth' and the line count.
10525 */
10526 int
10527number_width(wp)
10528 win_T *wp;
10529{
10530 int n;
10531 linenr_T lnum;
10532
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010533 if (wp->w_p_rnu && !wp->w_p_nu)
10534 /* cursor line shows "0" */
10535 lnum = wp->w_height;
10536 else
10537 /* cursor line shows absolute line number */
10538 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010539
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010540 if (lnum == wp->w_nrwidth_line_count)
10541 return wp->w_nrwidth_width;
10542 wp->w_nrwidth_line_count = lnum;
10543
10544 n = 0;
10545 do
10546 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010547 lnum /= 10;
10548 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010549 } while (lnum > 0);
10550
10551 /* 'numberwidth' gives the minimal width plus one */
10552 if (n < wp->w_p_nuw - 1)
10553 n = wp->w_p_nuw - 1;
10554
10555 wp->w_nrwidth_width = n;
10556 return n;
10557}
10558#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010559
10560/*
10561 * Return the current cursor column. This is the actual position on the
10562 * screen. First column is 0.
10563 */
10564 int
10565screen_screencol()
10566{
10567 return screen_cur_col;
10568}
10569
10570/*
10571 * Return the current cursor row. This is the actual position on the screen.
10572 * First row is 0.
10573 */
10574 int
10575screen_screenrow()
10576{
10577 return screen_cur_row;
10578}