blob: e1547624caa1011d30b81402bf2f6a95dd8566a9 [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' */
Bram Moolenaar1c934292015-01-27 16:39:29 +0100112static int compute_foldcolumn __ARGS((win_T *wp, int col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
120static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000121static 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 +0000122#ifdef FEAT_FOLDING
123static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
124static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
125static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
126#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000127static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
129#ifdef FEAT_RIGHTLEFT
130static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
133static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#ifdef FEAT_VERTSPLIT
137static void draw_vsep_win __ARGS((win_T *wp, int row));
138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000140static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static void start_search_hl __ARGS((void));
145static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200146static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200148static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur));
149static 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 +0000150#endif
151static void screen_start_highlight __ARGS((int attr));
152static void screen_char __ARGS((unsigned off, int row, int col));
153#ifdef FEAT_MBYTE
154static void screen_char_2 __ARGS((unsigned off, int row, int col));
155#endif
156static void screenclear2 __ARGS((void));
157static void lineclear __ARGS((unsigned off, int width));
158static void lineinvalid __ARGS((unsigned off, int width));
159#ifdef FEAT_VERTSPLIT
160static void linecopy __ARGS((int to, int from, win_T *wp));
161static void redraw_block __ARGS((int row, int end, win_T *wp));
162#endif
163static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
164static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000167static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
170static int fillchar_status __ARGS((int *attr, int is_curwin));
171#endif
172#ifdef FEAT_VERTSPLIT
173static int fillchar_vsep __ARGS((int *attr));
174#endif
175#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000176static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177#endif
178#ifdef FEAT_CMDL_INFO
179static void win_redr_ruler __ARGS((win_T *wp, int always));
180#endif
181
182#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
183/* Ugly global: overrule attribute used by screen_char() */
184static int screen_char_attr = 0;
185#endif
186
187/*
188 * Redraw the current window later, with update_screen(type).
189 * Set must_redraw only if not already set to a higher value.
190 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
191 */
192 void
193redraw_later(type)
194 int type;
195{
196 redraw_win_later(curwin, type);
197}
198
199 void
200redraw_win_later(wp, type)
201 win_T *wp;
202 int type;
203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
219redraw_later_clear()
220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
237redraw_all_later(type)
238 int type;
239{
240 win_T *wp;
241
242 FOR_ALL_WINDOWS(wp)
243 {
244 redraw_win_later(wp, type);
245 }
246}
247
248/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000249 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 */
251 void
252redraw_curbuf_later(type)
253 int type;
254{
255 redraw_buf_later(curbuf, type);
256}
257
258 void
259redraw_buf_later(buf, type)
260 buf_T *buf;
261 int type;
262{
263 win_T *wp;
264
265 FOR_ALL_WINDOWS(wp)
266 {
267 if (wp->w_buffer == buf)
268 redraw_win_later(wp, type);
269 }
270}
271
272/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200273 * Redraw as soon as possible. When the command line is not scrolled redraw
274 * right away and restore what was on the command line.
275 * Return a code indicating what happened.
276 */
277 int
278redraw_asap(type)
279 int type;
280{
281 int rows;
282 int r;
283 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200284 schar_T *screenline; /* copy from ScreenLines[] */
285 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200286#ifdef FEAT_MBYTE
287 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200288 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200289 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200290 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291#endif
292
293 redraw_later(type);
294 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY))
295 return ret;
296
297 /* Allocate space to save the text displayed in the command line area. */
298 rows = Rows - cmdline_row;
299 screenline = (schar_T *)lalloc(
300 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
301 screenattr = (sattr_T *)lalloc(
302 (long_u)(rows * Columns * sizeof(sattr_T)), FALSE);
303 if (screenline == NULL || screenattr == NULL)
304 ret = 2;
305#ifdef FEAT_MBYTE
306 if (enc_utf8)
307 {
308 screenlineUC = (u8char_T *)lalloc(
309 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
310 if (screenlineUC == NULL)
311 ret = 2;
312 for (i = 0; i < p_mco; ++i)
313 {
314 screenlineC[i] = (u8char_T *)lalloc(
315 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
316 if (screenlineC[i] == NULL)
317 ret = 2;
318 }
319 }
320 if (enc_dbcs == DBCS_JPNU)
321 {
322 screenline2 = (schar_T *)lalloc(
323 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
324 if (screenline2 == NULL)
325 ret = 2;
326 }
327#endif
328
329 if (ret != 2)
330 {
331 /* Save the text displayed in the command line area. */
332 for (r = 0; r < rows; ++r)
333 {
334 mch_memmove(screenline + r * Columns,
335 ScreenLines + LineOffset[cmdline_row + r],
336 (size_t)Columns * sizeof(schar_T));
337 mch_memmove(screenattr + r * Columns,
338 ScreenAttrs + LineOffset[cmdline_row + r],
339 (size_t)Columns * sizeof(sattr_T));
340#ifdef FEAT_MBYTE
341 if (enc_utf8)
342 {
343 mch_memmove(screenlineUC + r * Columns,
344 ScreenLinesUC + LineOffset[cmdline_row + r],
345 (size_t)Columns * sizeof(u8char_T));
346 for (i = 0; i < p_mco; ++i)
347 mch_memmove(screenlineC[i] + r * Columns,
348 ScreenLinesC[r] + LineOffset[cmdline_row + r],
349 (size_t)Columns * sizeof(u8char_T));
350 }
351 if (enc_dbcs == DBCS_JPNU)
352 mch_memmove(screenline2 + r * Columns,
353 ScreenLines2 + LineOffset[cmdline_row + r],
354 (size_t)Columns * sizeof(schar_T));
355#endif
356 }
357
358 update_screen(0);
359 ret = 3;
360
361 if (must_redraw == 0)
362 {
363 int off = (int)(current_ScreenLine - ScreenLines);
364
365 /* Restore the text displayed in the command line area. */
366 for (r = 0; r < rows; ++r)
367 {
368 mch_memmove(current_ScreenLine,
369 screenline + r * Columns,
370 (size_t)Columns * sizeof(schar_T));
371 mch_memmove(ScreenAttrs + off,
372 screenattr + r * Columns,
373 (size_t)Columns * sizeof(sattr_T));
374#ifdef FEAT_MBYTE
375 if (enc_utf8)
376 {
377 mch_memmove(ScreenLinesUC + off,
378 screenlineUC + r * Columns,
379 (size_t)Columns * sizeof(u8char_T));
380 for (i = 0; i < p_mco; ++i)
381 mch_memmove(ScreenLinesC[i] + off,
382 screenlineC[i] + r * Columns,
383 (size_t)Columns * sizeof(u8char_T));
384 }
385 if (enc_dbcs == DBCS_JPNU)
386 mch_memmove(ScreenLines2 + off,
387 screenline2 + r * Columns,
388 (size_t)Columns * sizeof(schar_T));
389#endif
390 SCREEN_LINE(cmdline_row + r, 0, Columns, Columns, FALSE);
391 }
392 ret = 4;
393 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 }
395
396 vim_free(screenline);
397 vim_free(screenattr);
398#ifdef FEAT_MBYTE
399 if (enc_utf8)
400 {
401 vim_free(screenlineUC);
402 for (i = 0; i < p_mco; ++i)
403 vim_free(screenlineC[i]);
404 }
405 if (enc_dbcs == DBCS_JPNU)
406 vim_free(screenline2);
407#endif
408
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200409 /* Show the intro message when appropriate. */
410 maybe_intro_message();
411
412 setcursor();
413
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414 return ret;
415}
416
417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 * Changed something in the current window, at buffer line "lnum", that
419 * requires that line and possibly other lines to be redrawn.
420 * Used when entering/leaving Insert mode with the cursor on a folded line.
421 * Used to remove the "$" from a change command.
422 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
423 * may become invalid and the whole window will have to be redrawn.
424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 void
426redrawWinline(lnum, invalid)
427 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000428 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430#ifdef FEAT_FOLDING
431 int i;
432#endif
433
434 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
435 curwin->w_redraw_top = lnum;
436 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
437 curwin->w_redraw_bot = lnum;
438 redraw_later(VALID);
439
440#ifdef FEAT_FOLDING
441 if (invalid)
442 {
443 /* A w_lines[] entry for this lnum has become invalid. */
444 i = find_wl_entry(curwin, lnum);
445 if (i >= 0)
446 curwin->w_lines[i].wl_valid = FALSE;
447 }
448#endif
449}
450
451/*
452 * update all windows that are editing the current buffer
453 */
454 void
455update_curbuf(type)
456 int type;
457{
458 redraw_curbuf_later(type);
459 update_screen(type);
460}
461
462/*
463 * update_screen()
464 *
465 * Based on the current value of curwin->w_topline, transfer a screenfull
466 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
467 */
468 void
469update_screen(type)
470 int type;
471{
472 win_T *wp;
473 static int did_intro = FALSE;
474#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
475 int did_one;
476#endif
477
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000478 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 if (!screen_valid(TRUE))
480 return;
481
482 if (must_redraw)
483 {
484 if (type < must_redraw) /* use maximal type */
485 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000486
487 /* must_redraw is reset here, so that when we run into some weird
488 * reason to redraw while busy redrawing (e.g., asynchronous
489 * scrolling), or update_topline() in win_update() will cause a
490 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 must_redraw = 0;
492 }
493
494 /* Need to update w_lines[]. */
495 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
496 type = NOT_VALID;
497
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000498 /* Postpone the redrawing when it's not needed and when being called
499 * recursively. */
500 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 {
502 redraw_later(type); /* remember type for next time */
503 must_redraw = type;
504 if (type > INVERTED_ALL)
505 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
506 return;
507 }
508
509 updating_screen = TRUE;
510#ifdef FEAT_SYN_HL
511 ++display_tick; /* let syntax code know we're in a next round of
512 * display updating */
513#endif
514
515 /*
516 * if the screen was scrolled up when displaying a message, scroll it down
517 */
518 if (msg_scrolled)
519 {
520 clear_cmdline = TRUE;
521 if (msg_scrolled > Rows - 5) /* clearing is faster */
522 type = CLEAR;
523 else if (type != CLEAR)
524 {
525 check_for_delay(FALSE);
526 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
527 type = CLEAR;
528 FOR_ALL_WINDOWS(wp)
529 {
530 if (W_WINROW(wp) < msg_scrolled)
531 {
532 if (W_WINROW(wp) + wp->w_height > msg_scrolled
533 && wp->w_redr_type < REDRAW_TOP
534 && wp->w_lines_valid > 0
535 && wp->w_topline == wp->w_lines[0].wl_lnum)
536 {
537 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
538 wp->w_redr_type = REDRAW_TOP;
539 }
540 else
541 {
542 wp->w_redr_type = NOT_VALID;
543#ifdef FEAT_WINDOWS
544 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
545 <= msg_scrolled)
546 wp->w_redr_status = TRUE;
547#endif
548 }
549 }
550 }
551 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000552#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000553 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556 msg_scrolled = 0;
557 need_wait_return = FALSE;
558 }
559
560 /* reset cmdline_row now (may have been changed temporarily) */
561 compute_cmdrow();
562
563 /* Check for changed highlighting */
564 if (need_highlight_changed)
565 highlight_changed();
566
567 if (type == CLEAR) /* first clear screen */
568 {
569 screenclear(); /* will reset clear_cmdline */
570 type = NOT_VALID;
571 }
572
573 if (clear_cmdline) /* going to clear cmdline (done below) */
574 check_for_delay(FALSE);
575
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000576#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200577 /* Force redraw when width of 'number' or 'relativenumber' column
578 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000579 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200580 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
581 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000582 curwin->w_redr_type = NOT_VALID;
583#endif
584
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 /*
586 * Only start redrawing if there is really something to do.
587 */
588 if (type == INVERTED)
589 update_curswant();
590 if (curwin->w_redr_type < type
591 && !((type == VALID
592 && curwin->w_lines[0].wl_valid
593#ifdef FEAT_DIFF
594 && curwin->w_topfill == curwin->w_old_topfill
595 && curwin->w_botfill == curwin->w_old_botfill
596#endif
597 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000599 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
601 && curwin->w_old_visual_mode == VIsual_mode
602 && (curwin->w_valid & VALID_VIRTCOL)
603 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 ))
605 curwin->w_redr_type = type;
606
Bram Moolenaar5a305422006-04-28 22:38:25 +0000607#ifdef FEAT_WINDOWS
608 /* Redraw the tab pages line if needed. */
609 if (redraw_tabline || type >= NOT_VALID)
610 draw_tabline();
611#endif
612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#ifdef FEAT_SYN_HL
614 /*
615 * Correct stored syntax highlighting info for changes in each displayed
616 * buffer. Each buffer must only be done once.
617 */
618 FOR_ALL_WINDOWS(wp)
619 {
620 if (wp->w_buffer->b_mod_set)
621 {
622# ifdef FEAT_WINDOWS
623 win_T *wwp;
624
625 /* Check if we already did this buffer. */
626 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
627 if (wwp->w_buffer == wp->w_buffer)
628 break;
629# endif
630 if (
631# ifdef FEAT_WINDOWS
632 wwp == wp &&
633# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200634 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 syn_stack_apply_changes(wp->w_buffer);
636 }
637 }
638#endif
639
640 /*
641 * Go from top to bottom through the windows, redrawing the ones that need
642 * it.
643 */
644#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
645 did_one = FALSE;
646#endif
647#ifdef FEAT_SEARCH_EXTRA
648 search_hl.rm.regprog = NULL;
649#endif
650 FOR_ALL_WINDOWS(wp)
651 {
652 if (wp->w_redr_type != 0)
653 {
654 cursor_off();
655#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
656 if (!did_one)
657 {
658 did_one = TRUE;
659# ifdef FEAT_SEARCH_EXTRA
660 start_search_hl();
661# endif
662# ifdef FEAT_CLIPBOARD
663 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200664 if (clip_star.available && clip_isautosel_star())
665 clip_update_selection(&clip_star);
666 if (clip_plus.available && clip_isautosel_plus())
667 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668# endif
669#ifdef FEAT_GUI
670 /* Remove the cursor before starting to do anything, because
671 * scrolling may make it difficult to redraw the text under
672 * it. */
673 if (gui.in_use)
674 gui_undraw_cursor();
675#endif
676 }
677#endif
678 win_update(wp);
679 }
680
681#ifdef FEAT_WINDOWS
682 /* redraw status line after the window to minimize cursor movement */
683 if (wp->w_redr_status)
684 {
685 cursor_off();
686 win_redr_status(wp);
687 }
688#endif
689 }
690#if defined(FEAT_SEARCH_EXTRA)
691 end_search_hl();
692#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100693#ifdef FEAT_INS_EXPAND
694 /* May need to redraw the popup menu. */
695 if (pum_visible())
696 pum_redraw();
697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
699#ifdef FEAT_WINDOWS
700 /* Reset b_mod_set flags. Going through all windows is probably faster
701 * than going through all buffers (there could be many buffers). */
702 for (wp = firstwin; wp != NULL; wp = wp->w_next)
703 wp->w_buffer->b_mod_set = FALSE;
704#else
705 curbuf->b_mod_set = FALSE;
706#endif
707
708 updating_screen = FALSE;
709#ifdef FEAT_GUI
710 gui_may_resize_shell();
711#endif
712
713 /* Clear or redraw the command line. Done last, because scrolling may
714 * mess up the command line. */
715 if (clear_cmdline || redraw_cmdline)
716 showmode();
717
718 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200719 if (!did_intro)
720 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 did_intro = TRUE;
722
723#ifdef FEAT_GUI
724 /* Redraw the cursor and update the scrollbars when all screen updating is
725 * done. */
726 if (gui.in_use)
727 {
728 out_flush(); /* required before updating the cursor */
729 if (did_one)
730 gui_update_cursor(FALSE, FALSE);
731 gui_update_scrollbars(FALSE);
732 }
733#endif
734}
735
Bram Moolenaar860cae12010-06-05 23:22:07 +0200736#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200737/*
738 * Return TRUE if the cursor line in window "wp" may be concealed, according
739 * to the 'concealcursor' option.
740 */
741 int
742conceal_cursor_line(wp)
743 win_T *wp;
744{
745 int c;
746
747 if (*wp->w_p_cocu == NUL)
748 return FALSE;
749 if (get_real_state() & VISUAL)
750 c = 'v';
751 else if (State & INSERT)
752 c = 'i';
753 else if (State & NORMAL)
754 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200755 else if (State & CMDLINE)
756 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200757 else
758 return FALSE;
759 return vim_strchr(wp->w_p_cocu, c) != NULL;
760}
761
762/*
763 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
764 */
765 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200766conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200767{
768 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
769 {
770 need_cursor_line_redraw = TRUE;
771 /* Need to recompute cursor column, e.g., when starting Visual mode
772 * without concealing. */
773 curs_columns(TRUE);
774 }
775}
776
Bram Moolenaar860cae12010-06-05 23:22:07 +0200777 void
778update_single_line(wp, lnum)
779 win_T *wp;
780 linenr_T lnum;
781{
782 int row;
783 int j;
784
785 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200786 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200787 {
788# ifdef FEAT_GUI
789 /* Remove the cursor before starting to do anything, because scrolling
790 * may make it difficult to redraw the text under it. */
791 if (gui.in_use)
792 gui_undraw_cursor();
793# endif
794 row = 0;
795 for (j = 0; j < wp->w_lines_valid; ++j)
796 {
797 if (lnum == wp->w_lines[j].wl_lnum)
798 {
799 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200800# ifdef FEAT_SEARCH_EXTRA
801 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200802 start_search_hl();
803 prepare_search_hl(wp, lnum);
804# endif
805 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
806# if defined(FEAT_SEARCH_EXTRA)
807 end_search_hl();
808# endif
809 break;
810 }
811 row += wp->w_lines[j].wl_size;
812 }
813# ifdef FEAT_GUI
814 /* Redraw the cursor */
815 if (gui.in_use)
816 {
817 out_flush(); /* required before updating the cursor */
818 gui_update_cursor(FALSE, FALSE);
819 }
820# endif
821 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200822 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200823}
824#endif
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
827static void update_prepare __ARGS((void));
828static void update_finish __ARGS((void));
829
830/*
831 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000832 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
834 static void
835update_prepare()
836{
837 cursor_off();
838 updating_screen = TRUE;
839#ifdef FEAT_GUI
840 /* Remove the cursor before starting to do anything, because scrolling may
841 * make it difficult to redraw the text under it. */
842 if (gui.in_use)
843 gui_undraw_cursor();
844#endif
845#ifdef FEAT_SEARCH_EXTRA
846 start_search_hl();
847#endif
848}
849
850/*
851 * Finish updating one or more windows.
852 */
853 static void
854update_finish()
855{
856 if (redraw_cmdline)
857 showmode();
858
859# ifdef FEAT_SEARCH_EXTRA
860 end_search_hl();
861# endif
862
863 updating_screen = FALSE;
864
865# ifdef FEAT_GUI
866 gui_may_resize_shell();
867
868 /* Redraw the cursor and update the scrollbars when all screen updating is
869 * done. */
870 if (gui.in_use)
871 {
872 out_flush(); /* required before updating the cursor */
873 gui_update_cursor(FALSE, FALSE);
874 gui_update_scrollbars(FALSE);
875 }
876# endif
877}
878#endif
879
880#if defined(FEAT_SIGNS) || defined(PROTO)
881 void
882update_debug_sign(buf, lnum)
883 buf_T *buf;
884 linenr_T lnum;
885{
886 win_T *wp;
887 int doit = FALSE;
888
889# ifdef FEAT_FOLDING
890 win_foldinfo.fi_level = 0;
891# endif
892
893 /* update/delete a specific mark */
894 FOR_ALL_WINDOWS(wp)
895 {
896 if (buf != NULL && lnum > 0)
897 {
898 if (wp->w_buffer == buf && lnum >= wp->w_topline
899 && lnum < wp->w_botline)
900 {
901 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
902 wp->w_redraw_top = lnum;
903 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
904 wp->w_redraw_bot = lnum;
905 redraw_win_later(wp, VALID);
906 }
907 }
908 else
909 redraw_win_later(wp, VALID);
910 if (wp->w_redr_type != 0)
911 doit = TRUE;
912 }
913
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100914 /* Return when there is nothing to do, screen updating is already
915 * happening (recursive call) or still starting up. */
916 if (!doit || updating_screen
917#ifdef FEAT_GUI
918 || gui.starting
919#endif
920 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 return;
922
923 /* update all windows that need updating */
924 update_prepare();
925
926# ifdef FEAT_WINDOWS
927 for (wp = firstwin; wp; wp = wp->w_next)
928 {
929 if (wp->w_redr_type != 0)
930 win_update(wp);
931 if (wp->w_redr_status)
932 win_redr_status(wp);
933 }
934# else
935 if (curwin->w_redr_type != 0)
936 win_update(curwin);
937# endif
938
939 update_finish();
940}
941#endif
942
943
944#if defined(FEAT_GUI) || defined(PROTO)
945/*
946 * Update a single window, its status line and maybe the command line msg.
947 * Used for the GUI scrollbar.
948 */
949 void
950updateWindow(wp)
951 win_T *wp;
952{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000953 /* return if already busy updating */
954 if (updating_screen)
955 return;
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 update_prepare();
958
959#ifdef FEAT_CLIPBOARD
960 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200961 if (clip_star.available && clip_isautosel_star())
962 clip_update_selection(&clip_star);
963 if (clip_plus.available && clip_isautosel_plus())
964 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000970 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000971 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000972 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (wp->w_redr_status
975# ifdef FEAT_CMDL_INFO
976 || p_ru
977# endif
978# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000979 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980# endif
981 )
982 win_redr_status(wp);
983#endif
984
985 update_finish();
986}
987#endif
988
989/*
990 * Update a single window.
991 *
992 * This may cause the windows below it also to be redrawn (when clearing the
993 * screen or scrolling lines).
994 *
995 * How the window is redrawn depends on wp->w_redr_type. Each type also
996 * implies the one below it.
997 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000998 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1000 * INVERTED redraw the changed part of the Visual area
1001 * INVERTED_ALL redraw the whole Visual area
1002 * VALID 1. scroll up/down to adjust for a changed w_topline
1003 * 2. update lines at the top when scrolled down
1004 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001005 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * b_mod_top and b_mod_bot.
1007 * - if wp->w_redraw_top non-zero, redraw lines between
1008 * wp->w_redraw_top and wp->w_redr_bot.
1009 * - continue redrawing when syntax status is invalid.
1010 * 4. if scrolled up, update lines at the bottom.
1011 * This results in three areas that may need updating:
1012 * top: from first row to top_end (when scrolled down)
1013 * mid: from mid_start to mid_end (update inversion or changed text)
1014 * bot: from bot_start to last row (when scrolled up)
1015 */
1016 static void
1017win_update(wp)
1018 win_T *wp;
1019{
1020 buf_T *buf = wp->w_buffer;
1021 int type;
1022 int top_end = 0; /* Below last row of the top area that needs
1023 updating. 0 when no top area updating. */
1024 int mid_start = 999;/* first row of the mid area that needs
1025 updating. 999 when no mid area updating. */
1026 int mid_end = 0; /* Below last row of the mid area that needs
1027 updating. 0 when no mid area updating. */
1028 int bot_start = 999;/* first row of the bot area that needs
1029 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 int scrolled_down = FALSE; /* TRUE when scrolled down when
1031 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001033 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 int top_to_mod = FALSE; /* redraw above mod_top */
1035#endif
1036
1037 int row; /* current window row to display */
1038 linenr_T lnum; /* current buffer lnum to display */
1039 int idx; /* current index in w_lines[] */
1040 int srow; /* starting row of the current line */
1041
1042 int eof = FALSE; /* if TRUE, we hit the end of the file */
1043 int didline = FALSE; /* if TRUE, we finished the last line */
1044 int i;
1045 long j;
1046 static int recursive = FALSE; /* being called recursively */
1047 int old_botline = wp->w_botline;
1048#ifdef FEAT_FOLDING
1049 long fold_count;
1050#endif
1051#ifdef FEAT_SYN_HL
1052 /* remember what happened to the previous line, to know if
1053 * check_visual_highlight() can be used */
1054#define DID_NONE 1 /* didn't update a line */
1055#define DID_LINE 2 /* updated a normal line */
1056#define DID_FOLD 3 /* updated a folded line */
1057 int did_update = DID_NONE;
1058 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1059#endif
1060 linenr_T mod_top = 0;
1061 linenr_T mod_bot = 0;
1062#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1063 int save_got_int;
1064#endif
1065
1066 type = wp->w_redr_type;
1067
1068 if (type == NOT_VALID)
1069 {
1070#ifdef FEAT_WINDOWS
1071 wp->w_redr_status = TRUE;
1072#endif
1073 wp->w_lines_valid = 0;
1074 }
1075
1076 /* Window is zero-height: nothing to draw. */
1077 if (wp->w_height == 0)
1078 {
1079 wp->w_redr_type = 0;
1080 return;
1081 }
1082
1083#ifdef FEAT_VERTSPLIT
1084 /* Window is zero-width: Only need to draw the separator. */
1085 if (wp->w_width == 0)
1086 {
1087 /* draw the vertical separator right of this window */
1088 draw_vsep_win(wp, 0);
1089 wp->w_redr_type = 0;
1090 return;
1091 }
1092#endif
1093
1094#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001095 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096#endif
1097
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001098#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001099 /* Force redraw when width of 'number' or 'relativenumber' column
1100 * changes. */
1101 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001102 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001103 {
1104 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001105 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001106 }
1107 else
1108#endif
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1111 {
1112 /*
1113 * When there are both inserted/deleted lines and specific lines to be
1114 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1115 * everything (only happens when redrawing is off for while).
1116 */
1117 type = NOT_VALID;
1118 }
1119 else
1120 {
1121 /*
1122 * Set mod_top to the first line that needs displaying because of
1123 * changes. Set mod_bot to the first line after the changes.
1124 */
1125 mod_top = wp->w_redraw_top;
1126 if (wp->w_redraw_bot != 0)
1127 mod_bot = wp->w_redraw_bot + 1;
1128 else
1129 mod_bot = 0;
1130 wp->w_redraw_top = 0; /* reset for next time */
1131 wp->w_redraw_bot = 0;
1132 if (buf->b_mod_set)
1133 {
1134 if (mod_top == 0 || mod_top > buf->b_mod_top)
1135 {
1136 mod_top = buf->b_mod_top;
1137#ifdef FEAT_SYN_HL
1138 /* Need to redraw lines above the change that may be included
1139 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001140 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001142 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 if (mod_top < 1)
1144 mod_top = 1;
1145 }
1146#endif
1147 }
1148 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1149 mod_bot = buf->b_mod_bot;
1150
1151#ifdef FEAT_SEARCH_EXTRA
1152 /* When 'hlsearch' is on and using a multi-line search pattern, a
1153 * change in one line may make the Search highlighting in a
1154 * previous line invalid. Simple solution: redraw all visible
1155 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001156 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001158 if (search_hl.rm.regprog != NULL
1159 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001161 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001162 {
1163 cur = wp->w_match_head;
1164 while (cur != NULL)
1165 {
1166 if (cur->match.regprog != NULL
1167 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001168 {
1169 top_to_mod = TRUE;
1170 break;
1171 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001172 cur = cur->next;
1173 }
1174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#endif
1176 }
1177#ifdef FEAT_FOLDING
1178 if (mod_top != 0 && hasAnyFolding(wp))
1179 {
1180 linenr_T lnumt, lnumb;
1181
1182 /*
1183 * A change in a line can cause lines above it to become folded or
1184 * unfolded. Find the top most buffer line that may be affected.
1185 * If the line was previously folded and displayed, get the first
1186 * line of that fold. If the line is folded now, get the first
1187 * folded line. Use the minimum of these two.
1188 */
1189
1190 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1191 * the line below it. If there is no valid entry, use w_topline.
1192 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1193 * to this line. If there is no valid entry, use MAXLNUM. */
1194 lnumt = wp->w_topline;
1195 lnumb = MAXLNUM;
1196 for (i = 0; i < wp->w_lines_valid; ++i)
1197 if (wp->w_lines[i].wl_valid)
1198 {
1199 if (wp->w_lines[i].wl_lastlnum < mod_top)
1200 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1201 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1202 {
1203 lnumb = wp->w_lines[i].wl_lnum;
1204 /* When there is a fold column it might need updating
1205 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001206 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 ++lnumb;
1208 }
1209 }
1210
1211 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1212 if (mod_top > lnumt)
1213 mod_top = lnumt;
1214
1215 /* Now do the same for the bottom line (one above mod_bot). */
1216 --mod_bot;
1217 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1218 ++mod_bot;
1219 if (mod_bot < lnumb)
1220 mod_bot = lnumb;
1221 }
1222#endif
1223
1224 /* When a change starts above w_topline and the end is below
1225 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001226 * If the end of the change is above w_topline: do like no change was
1227 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (mod_top != 0 && mod_top < wp->w_topline)
1229 {
1230 if (mod_bot > wp->w_topline)
1231 mod_top = wp->w_topline;
1232#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001233 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 top_end = 1;
1235#endif
1236 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001237
1238 /* When line numbers are displayed need to redraw all lines below
1239 * inserted/deleted lines. */
1240 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1241 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243
1244 /*
1245 * When only displaying the lines at the top, set top_end. Used when
1246 * window has scrolled down for msg_scrolled.
1247 */
1248 if (type == REDRAW_TOP)
1249 {
1250 j = 0;
1251 for (i = 0; i < wp->w_lines_valid; ++i)
1252 {
1253 j += wp->w_lines[i].wl_size;
1254 if (j >= wp->w_upd_rows)
1255 {
1256 top_end = j;
1257 break;
1258 }
1259 }
1260 if (top_end == 0)
1261 /* not found (cannot happen?): redraw everything */
1262 type = NOT_VALID;
1263 else
1264 /* top area defined, the rest is VALID */
1265 type = VALID;
1266 }
1267
Bram Moolenaar367329b2007-08-30 11:53:22 +00001268 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001269 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1270 * non-zero and thus not FALSE) will indicate that screenclear() was not
1271 * called. */
1272 if (screen_cleared)
1273 screen_cleared = MAYBE;
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /*
1276 * If there are no changes on the screen that require a complete redraw,
1277 * handle three cases:
1278 * 1: we are off the top of the screen by a few lines: scroll down
1279 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1280 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1281 * w_lines[] that needs updating.
1282 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001283 if ((type == VALID || type == SOME_VALID
1284 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285#ifdef FEAT_DIFF
1286 && !wp->w_botfill && !wp->w_old_botfill
1287#endif
1288 )
1289 {
1290 if (mod_top != 0 && wp->w_topline == mod_top)
1291 {
1292 /*
1293 * w_topline is the first changed line, the scrolling will be done
1294 * further down.
1295 */
1296 }
1297 else if (wp->w_lines[0].wl_valid
1298 && (wp->w_topline < wp->w_lines[0].wl_lnum
1299#ifdef FEAT_DIFF
1300 || (wp->w_topline == wp->w_lines[0].wl_lnum
1301 && wp->w_topfill > wp->w_old_topfill)
1302#endif
1303 ))
1304 {
1305 /*
1306 * New topline is above old topline: May scroll down.
1307 */
1308#ifdef FEAT_FOLDING
1309 if (hasAnyFolding(wp))
1310 {
1311 linenr_T ln;
1312
1313 /* count the number of lines we are off, counting a sequence
1314 * of folded lines as one */
1315 j = 0;
1316 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1317 {
1318 ++j;
1319 if (j >= wp->w_height - 2)
1320 break;
1321 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1322 }
1323 }
1324 else
1325#endif
1326 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1327 if (j < wp->w_height - 2) /* not too far off */
1328 {
1329 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1330#ifdef FEAT_DIFF
1331 /* insert extra lines for previously invisible filler lines */
1332 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1333 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1334 - wp->w_old_topfill;
1335#endif
1336 if (i < wp->w_height - 2) /* less than a screen off */
1337 {
1338 /*
1339 * Try to insert the correct number of lines.
1340 * If not the last window, delete the lines at the bottom.
1341 * win_ins_lines may fail when the terminal can't do it.
1342 */
1343 if (i > 0)
1344 check_for_delay(FALSE);
1345 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1346 {
1347 if (wp->w_lines_valid != 0)
1348 {
1349 /* Need to update rows that are new, stop at the
1350 * first one that scrolled down. */
1351 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 /* Move the entries that were scrolled, disable
1355 * the entries for the lines to be redrawn. */
1356 if ((wp->w_lines_valid += j) > wp->w_height)
1357 wp->w_lines_valid = wp->w_height;
1358 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1359 wp->w_lines[idx] = wp->w_lines[idx - j];
1360 while (idx >= 0)
1361 wp->w_lines[idx--].wl_valid = FALSE;
1362 }
1363 }
1364 else
1365 mid_start = 0; /* redraw all lines */
1366 }
1367 else
1368 mid_start = 0; /* redraw all lines */
1369 }
1370 else
1371 mid_start = 0; /* redraw all lines */
1372 }
1373 else
1374 {
1375 /*
1376 * New topline is at or below old topline: May scroll up.
1377 * When topline didn't change, find first entry in w_lines[] that
1378 * needs updating.
1379 */
1380
1381 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1382 j = -1;
1383 row = 0;
1384 for (i = 0; i < wp->w_lines_valid; i++)
1385 {
1386 if (wp->w_lines[i].wl_valid
1387 && wp->w_lines[i].wl_lnum == wp->w_topline)
1388 {
1389 j = i;
1390 break;
1391 }
1392 row += wp->w_lines[i].wl_size;
1393 }
1394 if (j == -1)
1395 {
1396 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1397 * lines */
1398 mid_start = 0;
1399 }
1400 else
1401 {
1402 /*
1403 * Try to delete the correct number of lines.
1404 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1405 */
1406#ifdef FEAT_DIFF
1407 /* If the topline didn't change, delete old filler lines,
1408 * otherwise delete filler lines of the new topline... */
1409 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1410 row += wp->w_old_topfill;
1411 else
1412 row += diff_check_fill(wp, wp->w_topline);
1413 /* ... but don't delete new filler lines. */
1414 row -= wp->w_topfill;
1415#endif
1416 if (row > 0)
1417 {
1418 check_for_delay(FALSE);
1419 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1420 bot_start = wp->w_height - row;
1421 else
1422 mid_start = 0; /* redraw all lines */
1423 }
1424 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1425 {
1426 /*
1427 * Skip the lines (below the deleted lines) that are still
1428 * valid and don't need redrawing. Copy their info
1429 * upwards, to compensate for the deleted lines. Set
1430 * bot_start to the first row that needs redrawing.
1431 */
1432 bot_start = 0;
1433 idx = 0;
1434 for (;;)
1435 {
1436 wp->w_lines[idx] = wp->w_lines[j];
1437 /* stop at line that didn't fit, unless it is still
1438 * valid (no lines deleted) */
1439 if (row > 0 && bot_start + row
1440 + (int)wp->w_lines[j].wl_size > wp->w_height)
1441 {
1442 wp->w_lines_valid = idx + 1;
1443 break;
1444 }
1445 bot_start += wp->w_lines[idx++].wl_size;
1446
1447 /* stop at the last valid entry in w_lines[].wl_size */
1448 if (++j >= wp->w_lines_valid)
1449 {
1450 wp->w_lines_valid = idx;
1451 break;
1452 }
1453 }
1454#ifdef FEAT_DIFF
1455 /* Correct the first entry for filler lines at the top
1456 * when it won't get updated below. */
1457 if (wp->w_p_diff && bot_start > 0)
1458 wp->w_lines[0].wl_size =
1459 plines_win_nofill(wp, wp->w_topline, TRUE)
1460 + wp->w_topfill;
1461#endif
1462 }
1463 }
1464 }
1465
1466 /* When starting redraw in the first line, redraw all lines. When
1467 * there is only one window it's probably faster to clear the screen
1468 * first. */
1469 if (mid_start == 0)
1470 {
1471 mid_end = wp->w_height;
1472 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001473 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001474 /* Clear the screen when it was not done by win_del_lines() or
1475 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1476 * then. */
1477 if (screen_cleared != TRUE)
1478 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001479#ifdef FEAT_WINDOWS
1480 /* The screen was cleared, redraw the tab pages line. */
1481 if (redraw_tabline)
1482 draw_tabline();
1483#endif
1484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001486
1487 /* When win_del_lines() or win_ins_lines() caused the screen to be
1488 * cleared (only happens for the first window) or when screenclear()
1489 * was called directly above, "must_redraw" will have been set to
1490 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1491 if (screen_cleared == TRUE)
1492 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494 else
1495 {
1496 /* Not VALID or INVERTED: redraw all lines. */
1497 mid_start = 0;
1498 mid_end = wp->w_height;
1499 }
1500
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001501 if (type == SOME_VALID)
1502 {
1503 /* SOME_VALID: redraw all lines. */
1504 mid_start = 0;
1505 mid_end = wp->w_height;
1506 type = NOT_VALID;
1507 }
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 /* check if we are updating or removing the inverted part */
1510 if ((VIsual_active && buf == curwin->w_buffer)
1511 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1512 {
1513 linenr_T from, to;
1514
1515 if (VIsual_active)
1516 {
1517 if (VIsual_active
1518 && (VIsual_mode != wp->w_old_visual_mode
1519 || type == INVERTED_ALL))
1520 {
1521 /*
1522 * If the type of Visual selection changed, redraw the whole
1523 * selection. Also when the ownership of the X selection is
1524 * gained or lost.
1525 */
1526 if (curwin->w_cursor.lnum < VIsual.lnum)
1527 {
1528 from = curwin->w_cursor.lnum;
1529 to = VIsual.lnum;
1530 }
1531 else
1532 {
1533 from = VIsual.lnum;
1534 to = curwin->w_cursor.lnum;
1535 }
1536 /* redraw more when the cursor moved as well */
1537 if (wp->w_old_cursor_lnum < from)
1538 from = wp->w_old_cursor_lnum;
1539 if (wp->w_old_cursor_lnum > to)
1540 to = wp->w_old_cursor_lnum;
1541 if (wp->w_old_visual_lnum < from)
1542 from = wp->w_old_visual_lnum;
1543 if (wp->w_old_visual_lnum > to)
1544 to = wp->w_old_visual_lnum;
1545 }
1546 else
1547 {
1548 /*
1549 * Find the line numbers that need to be updated: The lines
1550 * between the old cursor position and the current cursor
1551 * position. Also check if the Visual position changed.
1552 */
1553 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1554 {
1555 from = curwin->w_cursor.lnum;
1556 to = wp->w_old_cursor_lnum;
1557 }
1558 else
1559 {
1560 from = wp->w_old_cursor_lnum;
1561 to = curwin->w_cursor.lnum;
1562 if (from == 0) /* Visual mode just started */
1563 from = to;
1564 }
1565
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001566 if (VIsual.lnum != wp->w_old_visual_lnum
1567 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
1569 if (wp->w_old_visual_lnum < from
1570 && wp->w_old_visual_lnum != 0)
1571 from = wp->w_old_visual_lnum;
1572 if (wp->w_old_visual_lnum > to)
1573 to = wp->w_old_visual_lnum;
1574 if (VIsual.lnum < from)
1575 from = VIsual.lnum;
1576 if (VIsual.lnum > to)
1577 to = VIsual.lnum;
1578 }
1579 }
1580
1581 /*
1582 * If in block mode and changed column or curwin->w_curswant:
1583 * update all lines.
1584 * First compute the actual start and end column.
1585 */
1586 if (VIsual_mode == Ctrl_V)
1587 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001588 colnr_T fromc, toc;
1589#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1590 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
Bram Moolenaar404406a2014-10-09 13:24:43 +02001592 if (curwin->w_p_lbr)
1593 ve_flags = VE_ALL;
1594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001596#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1597 ve_flags = save_ve_flags;
1598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 ++toc;
1600 if (curwin->w_curswant == MAXCOL)
1601 toc = MAXCOL;
1602
1603 if (fromc != wp->w_old_cursor_fcol
1604 || toc != wp->w_old_cursor_lcol)
1605 {
1606 if (from > VIsual.lnum)
1607 from = VIsual.lnum;
1608 if (to < VIsual.lnum)
1609 to = VIsual.lnum;
1610 }
1611 wp->w_old_cursor_fcol = fromc;
1612 wp->w_old_cursor_lcol = toc;
1613 }
1614 }
1615 else
1616 {
1617 /* Use the line numbers of the old Visual area. */
1618 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1619 {
1620 from = wp->w_old_cursor_lnum;
1621 to = wp->w_old_visual_lnum;
1622 }
1623 else
1624 {
1625 from = wp->w_old_visual_lnum;
1626 to = wp->w_old_cursor_lnum;
1627 }
1628 }
1629
1630 /*
1631 * There is no need to update lines above the top of the window.
1632 */
1633 if (from < wp->w_topline)
1634 from = wp->w_topline;
1635
1636 /*
1637 * If we know the value of w_botline, use it to restrict the update to
1638 * the lines that are visible in the window.
1639 */
1640 if (wp->w_valid & VALID_BOTLINE)
1641 {
1642 if (from >= wp->w_botline)
1643 from = wp->w_botline - 1;
1644 if (to >= wp->w_botline)
1645 to = wp->w_botline - 1;
1646 }
1647
1648 /*
1649 * Find the minimal part to be updated.
1650 * Watch out for scrolling that made entries in w_lines[] invalid.
1651 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1652 * top_end; need to redraw from top_end to the "to" line.
1653 * A middle mouse click with a Visual selection may change the text
1654 * above the Visual area and reset wl_valid, do count these for
1655 * mid_end (in srow).
1656 */
1657 if (mid_start > 0)
1658 {
1659 lnum = wp->w_topline;
1660 idx = 0;
1661 srow = 0;
1662 if (scrolled_down)
1663 mid_start = top_end;
1664 else
1665 mid_start = 0;
1666 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1667 {
1668 if (wp->w_lines[idx].wl_valid)
1669 mid_start += wp->w_lines[idx].wl_size;
1670 else if (!scrolled_down)
1671 srow += wp->w_lines[idx].wl_size;
1672 ++idx;
1673# ifdef FEAT_FOLDING
1674 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1675 lnum = wp->w_lines[idx].wl_lnum;
1676 else
1677# endif
1678 ++lnum;
1679 }
1680 srow += mid_start;
1681 mid_end = wp->w_height;
1682 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1683 {
1684 if (wp->w_lines[idx].wl_valid
1685 && wp->w_lines[idx].wl_lnum >= to + 1)
1686 {
1687 /* Only update until first row of this line */
1688 mid_end = srow;
1689 break;
1690 }
1691 srow += wp->w_lines[idx].wl_size;
1692 }
1693 }
1694 }
1695
1696 if (VIsual_active && buf == curwin->w_buffer)
1697 {
1698 wp->w_old_visual_mode = VIsual_mode;
1699 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1700 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001701 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 wp->w_old_curswant = curwin->w_curswant;
1703 }
1704 else
1705 {
1706 wp->w_old_visual_mode = 0;
1707 wp->w_old_cursor_lnum = 0;
1708 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001709 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1713 /* reset got_int, otherwise regexp won't work */
1714 save_got_int = got_int;
1715 got_int = 0;
1716#endif
1717#ifdef FEAT_FOLDING
1718 win_foldinfo.fi_level = 0;
1719#endif
1720
1721 /*
1722 * Update all the window rows.
1723 */
1724 idx = 0; /* first entry in w_lines[].wl_size */
1725 row = 0;
1726 srow = 0;
1727 lnum = wp->w_topline; /* first line shown in window */
1728 for (;;)
1729 {
1730 /* stop updating when reached the end of the window (check for _past_
1731 * the end of the window is at the end of the loop) */
1732 if (row == wp->w_height)
1733 {
1734 didline = TRUE;
1735 break;
1736 }
1737
1738 /* stop updating when hit the end of the file */
1739 if (lnum > buf->b_ml.ml_line_count)
1740 {
1741 eof = TRUE;
1742 break;
1743 }
1744
1745 /* Remember the starting row of the line that is going to be dealt
1746 * with. It is used further down when the line doesn't fit. */
1747 srow = row;
1748
1749 /*
1750 * Update a line when it is in an area that needs updating, when it
1751 * has changes or w_lines[idx] is invalid.
1752 * bot_start may be halfway a wrapped line after using
1753 * win_del_lines(), check if the current line includes it.
1754 * When syntax folding is being used, the saved syntax states will
1755 * already have been updated, we can't see where the syntax state is
1756 * the same again, just update until the end of the window.
1757 */
1758 if (row < top_end
1759 || (row >= mid_start && row < mid_end)
1760#ifdef FEAT_SEARCH_EXTRA
1761 || top_to_mod
1762#endif
1763 || idx >= wp->w_lines_valid
1764 || (row + wp->w_lines[idx].wl_size > bot_start)
1765 || (mod_top != 0
1766 && (lnum == mod_top
1767 || (lnum >= mod_top
1768 && (lnum < mod_bot
1769#ifdef FEAT_SYN_HL
1770 || did_update == DID_FOLD
1771 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001772 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 && (
1774# ifdef FEAT_FOLDING
1775 (foldmethodIsSyntax(wp)
1776 && hasAnyFolding(wp)) ||
1777# endif
1778 syntax_check_changed(lnum)))
1779#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001780#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001781 /* match in fixed position might need redraw
1782 * if lines were inserted or deleted */
1783 || (wp->w_match_head != NULL
1784 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 )))))
1787 {
1788#ifdef FEAT_SEARCH_EXTRA
1789 if (lnum == mod_top)
1790 top_to_mod = FALSE;
1791#endif
1792
1793 /*
1794 * When at start of changed lines: May scroll following lines
1795 * up or down to minimize redrawing.
1796 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001797 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 */
1799 if (lnum == mod_top
1800 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001801 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803 int old_rows = 0;
1804 int new_rows = 0;
1805 int xtra_rows;
1806 linenr_T l;
1807
1808 /* Count the old number of window rows, using w_lines[], which
1809 * should still contain the sizes for the lines as they are
1810 * currently displayed. */
1811 for (i = idx; i < wp->w_lines_valid; ++i)
1812 {
1813 /* Only valid lines have a meaningful wl_lnum. Invalid
1814 * lines are part of the changed area. */
1815 if (wp->w_lines[i].wl_valid
1816 && wp->w_lines[i].wl_lnum == mod_bot)
1817 break;
1818 old_rows += wp->w_lines[i].wl_size;
1819#ifdef FEAT_FOLDING
1820 if (wp->w_lines[i].wl_valid
1821 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1822 {
1823 /* Must have found the last valid entry above mod_bot.
1824 * Add following invalid entries. */
1825 ++i;
1826 while (i < wp->w_lines_valid
1827 && !wp->w_lines[i].wl_valid)
1828 old_rows += wp->w_lines[i++].wl_size;
1829 break;
1830 }
1831#endif
1832 }
1833
1834 if (i >= wp->w_lines_valid)
1835 {
1836 /* We can't find a valid line below the changed lines,
1837 * need to redraw until the end of the window.
1838 * Inserting/deleting lines has no use. */
1839 bot_start = 0;
1840 }
1841 else
1842 {
1843 /* Able to count old number of rows: Count new window
1844 * rows, and may insert/delete lines */
1845 j = idx;
1846 for (l = lnum; l < mod_bot; ++l)
1847 {
1848#ifdef FEAT_FOLDING
1849 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1850 ++new_rows;
1851 else
1852#endif
1853#ifdef FEAT_DIFF
1854 if (l == wp->w_topline)
1855 new_rows += plines_win_nofill(wp, l, TRUE)
1856 + wp->w_topfill;
1857 else
1858#endif
1859 new_rows += plines_win(wp, l, TRUE);
1860 ++j;
1861 if (new_rows > wp->w_height - row - 2)
1862 {
1863 /* it's getting too much, must redraw the rest */
1864 new_rows = 9999;
1865 break;
1866 }
1867 }
1868 xtra_rows = new_rows - old_rows;
1869 if (xtra_rows < 0)
1870 {
1871 /* May scroll text up. If there is not enough
1872 * remaining text or scrolling fails, must redraw the
1873 * rest. If scrolling works, must redraw the text
1874 * below the scrolled text. */
1875 if (row - xtra_rows >= wp->w_height - 2)
1876 mod_bot = MAXLNUM;
1877 else
1878 {
1879 check_for_delay(FALSE);
1880 if (win_del_lines(wp, row,
1881 -xtra_rows, FALSE, FALSE) == FAIL)
1882 mod_bot = MAXLNUM;
1883 else
1884 bot_start = wp->w_height + xtra_rows;
1885 }
1886 }
1887 else if (xtra_rows > 0)
1888 {
1889 /* May scroll text down. If there is not enough
1890 * remaining text of scrolling fails, must redraw the
1891 * rest. */
1892 if (row + xtra_rows >= wp->w_height - 2)
1893 mod_bot = MAXLNUM;
1894 else
1895 {
1896 check_for_delay(FALSE);
1897 if (win_ins_lines(wp, row + old_rows,
1898 xtra_rows, FALSE, FALSE) == FAIL)
1899 mod_bot = MAXLNUM;
1900 else if (top_end > row + old_rows)
1901 /* Scrolled the part at the top that requires
1902 * updating down. */
1903 top_end += xtra_rows;
1904 }
1905 }
1906
1907 /* When not updating the rest, may need to move w_lines[]
1908 * entries. */
1909 if (mod_bot != MAXLNUM && i != j)
1910 {
1911 if (j < i)
1912 {
1913 int x = row + new_rows;
1914
1915 /* move entries in w_lines[] upwards */
1916 for (;;)
1917 {
1918 /* stop at last valid entry in w_lines[] */
1919 if (i >= wp->w_lines_valid)
1920 {
1921 wp->w_lines_valid = j;
1922 break;
1923 }
1924 wp->w_lines[j] = wp->w_lines[i];
1925 /* stop at a line that won't fit */
1926 if (x + (int)wp->w_lines[j].wl_size
1927 > wp->w_height)
1928 {
1929 wp->w_lines_valid = j + 1;
1930 break;
1931 }
1932 x += wp->w_lines[j++].wl_size;
1933 ++i;
1934 }
1935 if (bot_start > x)
1936 bot_start = x;
1937 }
1938 else /* j > i */
1939 {
1940 /* move entries in w_lines[] downwards */
1941 j -= i;
1942 wp->w_lines_valid += j;
1943 if (wp->w_lines_valid > wp->w_height)
1944 wp->w_lines_valid = wp->w_height;
1945 for (i = wp->w_lines_valid; i - j >= idx; --i)
1946 wp->w_lines[i] = wp->w_lines[i - j];
1947
1948 /* The w_lines[] entries for inserted lines are
1949 * now invalid, but wl_size may be used above.
1950 * Reset to zero. */
1951 while (i >= idx)
1952 {
1953 wp->w_lines[i].wl_size = 0;
1954 wp->w_lines[i--].wl_valid = FALSE;
1955 }
1956 }
1957 }
1958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960
1961#ifdef FEAT_FOLDING
1962 /*
1963 * When lines are folded, display one line for all of them.
1964 * Otherwise, display normally (can be several display lines when
1965 * 'wrap' is on).
1966 */
1967 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1968 if (fold_count != 0)
1969 {
1970 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1971 ++row;
1972 --fold_count;
1973 wp->w_lines[idx].wl_folded = TRUE;
1974 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1975# ifdef FEAT_SYN_HL
1976 did_update = DID_FOLD;
1977# endif
1978 }
1979 else
1980#endif
1981 if (idx < wp->w_lines_valid
1982 && wp->w_lines[idx].wl_valid
1983 && wp->w_lines[idx].wl_lnum == lnum
1984 && lnum > wp->w_topline
1985 && !(dy_flags & DY_LASTLINE)
1986 && srow + wp->w_lines[idx].wl_size > wp->w_height
1987#ifdef FEAT_DIFF
1988 && diff_check_fill(wp, lnum) == 0
1989#endif
1990 )
1991 {
1992 /* This line is not going to fit. Don't draw anything here,
1993 * will draw "@ " lines below. */
1994 row = wp->w_height + 1;
1995 }
1996 else
1997 {
1998#ifdef FEAT_SEARCH_EXTRA
1999 prepare_search_hl(wp, lnum);
2000#endif
2001#ifdef FEAT_SYN_HL
2002 /* Let the syntax stuff know we skipped a few lines. */
2003 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002004 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 syntax_end_parsing(syntax_last_parsed + 1);
2006#endif
2007
2008 /*
2009 * Display one line.
2010 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002011 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013#ifdef FEAT_FOLDING
2014 wp->w_lines[idx].wl_folded = FALSE;
2015 wp->w_lines[idx].wl_lastlnum = lnum;
2016#endif
2017#ifdef FEAT_SYN_HL
2018 did_update = DID_LINE;
2019 syntax_last_parsed = lnum;
2020#endif
2021 }
2022
2023 wp->w_lines[idx].wl_lnum = lnum;
2024 wp->w_lines[idx].wl_valid = TRUE;
2025 if (row > wp->w_height) /* past end of screen */
2026 {
2027 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002028 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2030 ++idx;
2031 break;
2032 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002033 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 wp->w_lines[idx].wl_size = row - srow;
2035 ++idx;
2036#ifdef FEAT_FOLDING
2037 lnum += fold_count + 1;
2038#else
2039 ++lnum;
2040#endif
2041 }
2042 else
2043 {
2044 /* This line does not need updating, advance to the next one */
2045 row += wp->w_lines[idx++].wl_size;
2046 if (row > wp->w_height) /* past end of screen */
2047 break;
2048#ifdef FEAT_FOLDING
2049 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2050#else
2051 ++lnum;
2052#endif
2053#ifdef FEAT_SYN_HL
2054 did_update = DID_NONE;
2055#endif
2056 }
2057
2058 if (lnum > buf->b_ml.ml_line_count)
2059 {
2060 eof = TRUE;
2061 break;
2062 }
2063 }
2064 /*
2065 * End of loop over all window lines.
2066 */
2067
2068
2069 if (idx > wp->w_lines_valid)
2070 wp->w_lines_valid = idx;
2071
2072#ifdef FEAT_SYN_HL
2073 /*
2074 * Let the syntax stuff know we stop parsing here.
2075 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002076 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 syntax_end_parsing(syntax_last_parsed + 1);
2078#endif
2079
2080 /*
2081 * If we didn't hit the end of the file, and we didn't finish the last
2082 * line we were working on, then the line didn't fit.
2083 */
2084 wp->w_empty_rows = 0;
2085#ifdef FEAT_DIFF
2086 wp->w_filler_rows = 0;
2087#endif
2088 if (!eof && !didline)
2089 {
2090 if (lnum == wp->w_topline)
2091 {
2092 /*
2093 * Single line that does not fit!
2094 * Don't overwrite it, it can be edited.
2095 */
2096 wp->w_botline = lnum + 1;
2097 }
2098#ifdef FEAT_DIFF
2099 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2100 {
2101 /* Window ends in filler lines. */
2102 wp->w_botline = lnum;
2103 wp->w_filler_rows = wp->w_height - srow;
2104 }
2105#endif
2106 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2107 {
2108 /*
2109 * Last line isn't finished: Display "@@@" at the end.
2110 */
2111 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2112 W_WINROW(wp) + wp->w_height,
2113 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2114 '@', '@', hl_attr(HLF_AT));
2115 set_empty_rows(wp, srow);
2116 wp->w_botline = lnum;
2117 }
2118 else
2119 {
2120 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2121 wp->w_botline = lnum;
2122 }
2123 }
2124 else
2125 {
2126#ifdef FEAT_VERTSPLIT
2127 draw_vsep_win(wp, row);
2128#endif
2129 if (eof) /* we hit the end of the file */
2130 {
2131 wp->w_botline = buf->b_ml.ml_line_count + 1;
2132#ifdef FEAT_DIFF
2133 j = diff_check_fill(wp, wp->w_botline);
2134 if (j > 0 && !wp->w_botfill)
2135 {
2136 /*
2137 * Display filler lines at the end of the file
2138 */
2139 if (char2cells(fill_diff) > 1)
2140 i = '-';
2141 else
2142 i = fill_diff;
2143 if (row + j > wp->w_height)
2144 j = wp->w_height - row;
2145 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2146 row += j;
2147 }
2148#endif
2149 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002150 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 wp->w_botline = lnum;
2152
2153 /* make sure the rest of the screen is blank */
2154 /* put '~'s on rows that aren't part of the file. */
2155 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2156 }
2157
2158 /* Reset the type of redrawing required, the window has been updated. */
2159 wp->w_redr_type = 0;
2160#ifdef FEAT_DIFF
2161 wp->w_old_topfill = wp->w_topfill;
2162 wp->w_old_botfill = wp->w_botfill;
2163#endif
2164
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002165 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
2167 /*
2168 * There is a trick with w_botline. If we invalidate it on each
2169 * change that might modify it, this will cause a lot of expensive
2170 * calls to plines() in update_topline() each time. Therefore the
2171 * value of w_botline is often approximated, and this value is used to
2172 * compute the value of w_topline. If the value of w_botline was
2173 * wrong, check that the value of w_topline is correct (cursor is on
2174 * the visible part of the text). If it's not, we need to redraw
2175 * again. Mostly this just means scrolling up a few lines, so it
2176 * doesn't look too bad. Only do this for the current window (where
2177 * changes are relevant).
2178 */
2179 wp->w_valid |= VALID_BOTLINE;
2180 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2181 {
2182 recursive = TRUE;
2183 curwin->w_valid &= ~VALID_TOPLINE;
2184 update_topline(); /* may invalidate w_botline again */
2185 if (must_redraw != 0)
2186 {
2187 /* Don't update for changes in buffer again. */
2188 i = curbuf->b_mod_set;
2189 curbuf->b_mod_set = FALSE;
2190 win_update(curwin);
2191 must_redraw = 0;
2192 curbuf->b_mod_set = i;
2193 }
2194 recursive = FALSE;
2195 }
2196 }
2197
2198#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2199 /* restore got_int, unless CTRL-C was hit while redrawing */
2200 if (!got_int)
2201 got_int = save_got_int;
2202#endif
2203}
2204
2205#ifdef FEAT_SIGNS
2206static int draw_signcolumn __ARGS((win_T *wp));
2207
2208/*
2209 * Return TRUE when window "wp" has a column to draw signs in.
2210 */
2211 static int
2212draw_signcolumn(wp)
2213 win_T *wp;
2214{
2215 return (wp->w_buffer->b_signlist != NULL
2216# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002217 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218# endif
2219 );
2220}
2221#endif
2222
2223/*
2224 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2225 * as the filler character.
2226 */
2227 static void
2228win_draw_end(wp, c1, c2, row, endrow, hl)
2229 win_T *wp;
2230 int c1;
2231 int c2;
2232 int row;
2233 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002234 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2237 int n = 0;
2238# define FDC_OFF n
2239#else
2240# define FDC_OFF 0
2241#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002242#ifdef FEAT_FOLDING
2243 int fdc = compute_foldcolumn(wp, 0);
2244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246#ifdef FEAT_RIGHTLEFT
2247 if (wp->w_p_rl)
2248 {
2249 /* No check for cmdline window: should never be right-left. */
2250# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002251 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 if (n > 0)
2254 {
2255 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002256 if (n > W_WIDTH(wp))
2257 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2259 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2260 ' ', ' ', hl_attr(HLF_FC));
2261 }
2262# endif
2263# ifdef FEAT_SIGNS
2264 if (draw_signcolumn(wp))
2265 {
2266 int nn = n + 2;
2267
2268 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002269 if (nn > W_WIDTH(wp))
2270 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2272 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2273 ' ', ' ', hl_attr(HLF_SC));
2274 n = nn;
2275 }
2276# endif
2277 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2278 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2279 c2, c2, hl_attr(hl));
2280 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2281 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2282 c1, c2, hl_attr(hl));
2283 }
2284 else
2285#endif
2286 {
2287#ifdef FEAT_CMDWIN
2288 if (cmdwin_type != 0 && wp == curwin)
2289 {
2290 /* draw the cmdline character in the leftmost column */
2291 n = 1;
2292 if (n > wp->w_width)
2293 n = wp->w_width;
2294 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2295 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2296 cmdwin_type, ' ', hl_attr(HLF_AT));
2297 }
2298#endif
2299#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002300 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002302 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 /* draw the fold column at the left */
2305 if (nn > W_WIDTH(wp))
2306 nn = W_WIDTH(wp);
2307 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2308 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2309 ' ', ' ', hl_attr(HLF_FC));
2310 n = nn;
2311 }
2312#endif
2313#ifdef FEAT_SIGNS
2314 if (draw_signcolumn(wp))
2315 {
2316 int nn = n + 2;
2317
2318 /* draw the sign column after the fold column */
2319 if (nn > W_WIDTH(wp))
2320 nn = W_WIDTH(wp);
2321 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2322 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2323 ' ', ' ', hl_attr(HLF_SC));
2324 n = nn;
2325 }
2326#endif
2327 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2328 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2329 c1, c2, hl_attr(hl));
2330 }
2331 set_empty_rows(wp, row);
2332}
2333
Bram Moolenaar1a384422010-07-14 19:53:30 +02002334#ifdef FEAT_SYN_HL
2335static int advance_color_col __ARGS((int vcol, int **color_cols));
2336
2337/*
2338 * Advance **color_cols and return TRUE when there are columns to draw.
2339 */
2340 static int
2341advance_color_col(vcol, color_cols)
2342 int vcol;
2343 int **color_cols;
2344{
2345 while (**color_cols >= 0 && vcol > **color_cols)
2346 ++*color_cols;
2347 return (**color_cols >= 0);
2348}
2349#endif
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#ifdef FEAT_FOLDING
2352/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002353 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2354 * space is available for window "wp", minus "col".
2355 */
2356 static int
2357compute_foldcolumn(wp, col)
2358 win_T *wp;
2359 int col;
2360{
2361 int fdc = wp->w_p_fdc;
2362 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2363 int wwidth = W_WIDTH(wp);
2364
2365 if (fdc > wwidth - (col + wmw))
2366 fdc = wwidth - (col + wmw);
2367 return fdc;
2368}
2369
2370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 * Display one folded line.
2372 */
2373 static void
2374fold_line(wp, fold_count, foldinfo, lnum, row)
2375 win_T *wp;
2376 long fold_count;
2377 foldinfo_T *foldinfo;
2378 linenr_T lnum;
2379 int row;
2380{
2381 char_u buf[51];
2382 pos_T *top, *bot;
2383 linenr_T lnume = lnum + fold_count - 1;
2384 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002385 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 int col;
2388 int txtcol;
2389 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002390 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392 /* Build the fold line:
2393 * 1. Add the cmdwin_type for the command-line window
2394 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002395 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 * 4. Compose the text
2397 * 5. Add the text
2398 * 6. set highlighting for the Visual area an other text
2399 */
2400 col = 0;
2401
2402 /*
2403 * 1. Add the cmdwin_type for the command-line window
2404 * Ignores 'rightleft', this window is never right-left.
2405 */
2406#ifdef FEAT_CMDWIN
2407 if (cmdwin_type != 0 && wp == curwin)
2408 {
2409 ScreenLines[off] = cmdwin_type;
2410 ScreenAttrs[off] = hl_attr(HLF_AT);
2411#ifdef FEAT_MBYTE
2412 if (enc_utf8)
2413 ScreenLinesUC[off] = 0;
2414#endif
2415 ++col;
2416 }
2417#endif
2418
2419 /*
2420 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002421 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002423 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (fdc > 0)
2425 {
2426 fill_foldcolumn(buf, wp, TRUE, lnum);
2427#ifdef FEAT_RIGHTLEFT
2428 if (wp->w_p_rl)
2429 {
2430 int i;
2431
2432 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2433 hl_attr(HLF_FC));
2434 /* reverse the fold column */
2435 for (i = 0; i < fdc; ++i)
2436 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2437 }
2438 else
2439#endif
2440 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2441 col += fdc;
2442 }
2443
2444#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002445# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2446 for (ri = 0; ri < l; ++ri) \
2447 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2448 else \
2449 for (ri = 0; ri < l; ++ri) \
2450 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002452# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2453 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#endif
2455
Bram Moolenaar64486672010-05-16 15:46:46 +02002456 /* Set all attributes of the 'number' or 'relativenumber' column and the
2457 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002458 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460#ifdef FEAT_SIGNS
2461 /* If signs are being displayed, add two spaces. */
2462 if (draw_signcolumn(wp))
2463 {
2464 len = W_WIDTH(wp) - col;
2465 if (len > 0)
2466 {
2467 if (len > 2)
2468 len = 2;
2469# ifdef FEAT_RIGHTLEFT
2470 if (wp->w_p_rl)
2471 /* the line number isn't reversed */
2472 copy_text_attr(off + W_WIDTH(wp) - len - col,
2473 (char_u *)" ", len, hl_attr(HLF_FL));
2474 else
2475# endif
2476 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2477 col += len;
2478 }
2479 }
2480#endif
2481
2482 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002483 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002485 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 len = W_WIDTH(wp) - col;
2488 if (len > 0)
2489 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002490 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002491 long num;
2492 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002493
2494 if (len > w + 1)
2495 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002496
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002497 if (wp->w_p_nu && !wp->w_p_rnu)
2498 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002499 num = (long)lnum;
2500 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002501 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002502 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002503 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002504 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002505 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002506 /* 'number' + 'relativenumber': cursor line shows absolute
2507 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002508 num = lnum;
2509 fmt = "%-*ld ";
2510 }
2511 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002512
Bram Moolenaar700e7342013-01-30 12:31:36 +01002513 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514#ifdef FEAT_RIGHTLEFT
2515 if (wp->w_p_rl)
2516 /* the line number isn't reversed */
2517 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2518 hl_attr(HLF_FL));
2519 else
2520#endif
2521 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2522 col += len;
2523 }
2524 }
2525
2526 /*
2527 * 4. Compose the folded-line string with 'foldtext', if set.
2528 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002529 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 txtcol = col; /* remember where text starts */
2532
2533 /*
2534 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2535 * Right-left text is put in columns 0 - number-col, normal text is put
2536 * in columns number-col - window-width.
2537 */
2538#ifdef FEAT_MBYTE
2539 if (has_mbyte)
2540 {
2541 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002542 int u8c, u8cc[MAX_MCO];
2543 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 int idx;
2545 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002546 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547# ifdef FEAT_ARABIC
2548 int prev_c = 0; /* previous Arabic character */
2549 int prev_c1 = 0; /* first composing char for prev_c */
2550# endif
2551
2552# ifdef FEAT_RIGHTLEFT
2553 if (wp->w_p_rl)
2554 idx = off;
2555 else
2556# endif
2557 idx = off + col;
2558
2559 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2560 for (p = text; *p != NUL; )
2561 {
2562 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002563 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (col + cells > W_WIDTH(wp)
2565# ifdef FEAT_RIGHTLEFT
2566 - (wp->w_p_rl ? col : 0)
2567# endif
2568 )
2569 break;
2570 ScreenLines[idx] = *p;
2571 if (enc_utf8)
2572 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 u8c = utfc_ptr2char(p, u8cc);
2574 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 ScreenLinesUC[idx] = 0;
2577#ifdef FEAT_ARABIC
2578 prev_c = u8c;
2579#endif
2580 }
2581 else
2582 {
2583#ifdef FEAT_ARABIC
2584 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2585 {
2586 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002587 int pc, pc1, nc;
2588 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 int firstbyte = *p;
2590
2591 /* The idea of what is the previous and next
2592 * character depends on 'rightleft'. */
2593 if (wp->w_p_rl)
2594 {
2595 pc = prev_c;
2596 pc1 = prev_c1;
2597 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002598 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600 else
2601 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002602 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002604 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606 prev_c = u8c;
2607
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002608 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 pc, pc1, nc);
2610 ScreenLines[idx] = firstbyte;
2611 }
2612 else
2613 prev_c = u8c;
2614#endif
2615 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002616#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 if (u8c >= 0x10000)
2618 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2619 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002622 for (i = 0; i < Screen_mco; ++i)
2623 {
2624 ScreenLinesC[i][idx] = u8cc[i];
2625 if (u8cc[i] == 0)
2626 break;
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629 if (cells > 1)
2630 ScreenLines[idx + 1] = 0;
2631 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002632 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2633 /* double-byte single width character */
2634 ScreenLines2[idx] = p[1];
2635 else if (cells > 1)
2636 /* double-width character */
2637 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 col += cells;
2639 idx += cells;
2640 p += c_len;
2641 }
2642 }
2643 else
2644#endif
2645 {
2646 len = (int)STRLEN(text);
2647 if (len > W_WIDTH(wp) - col)
2648 len = W_WIDTH(wp) - col;
2649 if (len > 0)
2650 {
2651#ifdef FEAT_RIGHTLEFT
2652 if (wp->w_p_rl)
2653 STRNCPY(current_ScreenLine, text, len);
2654 else
2655#endif
2656 STRNCPY(current_ScreenLine + col, text, len);
2657 col += len;
2658 }
2659 }
2660
2661 /* Fill the rest of the line with the fold filler */
2662#ifdef FEAT_RIGHTLEFT
2663 if (wp->w_p_rl)
2664 col -= txtcol;
2665#endif
2666 while (col < W_WIDTH(wp)
2667#ifdef FEAT_RIGHTLEFT
2668 - (wp->w_p_rl ? txtcol : 0)
2669#endif
2670 )
2671 {
2672#ifdef FEAT_MBYTE
2673 if (enc_utf8)
2674 {
2675 if (fill_fold >= 0x80)
2676 {
2677 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002678 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
2680 else
2681 ScreenLinesUC[off + col] = 0;
2682 }
2683#endif
2684 ScreenLines[off + col++] = fill_fold;
2685 }
2686
2687 if (text != buf)
2688 vim_free(text);
2689
2690 /*
2691 * 6. set highlighting for the Visual area an other text.
2692 * If all folded lines are in the Visual area, highlight the line.
2693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2695 {
2696 if (ltoreq(curwin->w_cursor, VIsual))
2697 {
2698 /* Visual is after curwin->w_cursor */
2699 top = &curwin->w_cursor;
2700 bot = &VIsual;
2701 }
2702 else
2703 {
2704 /* Visual is before curwin->w_cursor */
2705 top = &VIsual;
2706 bot = &curwin->w_cursor;
2707 }
2708 if (lnum >= top->lnum
2709 && lnume <= bot->lnum
2710 && (VIsual_mode != 'v'
2711 || ((lnum > top->lnum
2712 || (lnum == top->lnum
2713 && top->col == 0))
2714 && (lnume < bot->lnum
2715 || (lnume == bot->lnum
2716 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002717 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
2719 if (VIsual_mode == Ctrl_V)
2720 {
2721 /* Visual block mode: highlight the chars part of the block */
2722 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2723 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002724 if (wp->w_old_cursor_lcol != MAXCOL
2725 && wp->w_old_cursor_lcol + txtcol
2726 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 len = wp->w_old_cursor_lcol;
2728 else
2729 len = W_WIDTH(wp) - txtcol;
2730 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002731 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733 }
2734 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002737 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
2740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002742#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002743 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2744 if (wp->w_p_cc_cols)
2745 {
2746 int i = 0;
2747 int j = wp->w_p_cc_cols[i];
2748 int old_txtcol = txtcol;
2749
2750 while (j > -1)
2751 {
2752 txtcol += j;
2753 if (wp->w_p_wrap)
2754 txtcol -= wp->w_skipcol;
2755 else
2756 txtcol -= wp->w_leftcol;
2757 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2758 ScreenAttrs[off + txtcol] = hl_combine_attr(
2759 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2760 txtcol = old_txtcol;
2761 j = wp->w_p_cc_cols[++i];
2762 }
2763 }
2764
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002765 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002766 if (wp->w_p_cuc)
2767 {
2768 txtcol += wp->w_virtcol;
2769 if (wp->w_p_wrap)
2770 txtcol -= wp->w_skipcol;
2771 else
2772 txtcol -= wp->w_leftcol;
2773 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2774 ScreenAttrs[off + txtcol] = hl_combine_attr(
2775 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2776 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2780 (int)W_WIDTH(wp), FALSE);
2781
2782 /*
2783 * Update w_cline_height and w_cline_folded if the cursor line was
2784 * updated (saves a call to plines() later).
2785 */
2786 if (wp == curwin
2787 && lnum <= curwin->w_cursor.lnum
2788 && lnume >= curwin->w_cursor.lnum)
2789 {
2790 curwin->w_cline_row = row;
2791 curwin->w_cline_height = 1;
2792 curwin->w_cline_folded = TRUE;
2793 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2794 }
2795}
2796
2797/*
2798 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2799 */
2800 static void
2801copy_text_attr(off, buf, len, attr)
2802 int off;
2803 char_u *buf;
2804 int len;
2805 int attr;
2806{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002807 int i;
2808
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 mch_memmove(ScreenLines + off, buf, (size_t)len);
2810# ifdef FEAT_MBYTE
2811 if (enc_utf8)
2812 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2813# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002814 for (i = 0; i < len; ++i)
2815 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816}
2817
2818/*
2819 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002820 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 */
2822 static void
2823fill_foldcolumn(p, wp, closed, lnum)
2824 char_u *p;
2825 win_T *wp;
2826 int closed; /* TRUE of FALSE */
2827 linenr_T lnum; /* current line number */
2828{
2829 int i = 0;
2830 int level;
2831 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002832 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002833 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002836 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 level = win_foldinfo.fi_level;
2839 if (level > 0)
2840 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002841 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002842 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002843
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 /* If the column is too narrow, we start at the lowest level that
2845 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002846 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (first_level < 1)
2848 first_level = 1;
2849
Bram Moolenaar1c934292015-01-27 16:39:29 +01002850 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 if (win_foldinfo.fi_lnum == lnum
2853 && first_level + i >= win_foldinfo.fi_low_level)
2854 p[i] = '-';
2855 else if (first_level == 1)
2856 p[i] = '|';
2857 else if (first_level + i <= 9)
2858 p[i] = '0' + first_level + i;
2859 else
2860 p[i] = '>';
2861 if (first_level + i == level)
2862 break;
2863 }
2864 }
2865 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002866 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867}
2868#endif /* FEAT_FOLDING */
2869
2870/*
2871 * Display line "lnum" of window 'wp' on the screen.
2872 * Start at row "startrow", stop when "endrow" is reached.
2873 * wp->w_virtcol needs to be valid.
2874 *
2875 * Return the number of last row the line occupies.
2876 */
2877 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002878win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 win_T *wp;
2880 linenr_T lnum;
2881 int startrow;
2882 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002883 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884{
2885 int col; /* visual column on screen */
2886 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2887 int c = 0; /* init for GCC */
2888 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002889#ifdef FEAT_LINEBREAK
2890 long vcol_sbr = -1; /* virtual column after showbreak */
2891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 long vcol_prev = -1; /* "vcol" of previous character */
2893 char_u *line; /* current line */
2894 char_u *ptr; /* current position in "line" */
2895 int row; /* row in the window, excl w_winrow */
2896 int screen_row; /* row on the screen, incl w_winrow */
2897
2898 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2899 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002900 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002901 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 int c_extra = NUL; /* extra chars, all the same */
2903 int extra_attr = 0; /* attributes when n_extra != 0 */
2904 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2905 displaying lcs_eol at end-of-line */
2906 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2907 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2908
2909 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2910 int saved_n_extra = 0;
2911 char_u *saved_p_extra = NULL;
2912 int saved_c_extra = 0;
2913 int saved_char_attr = 0;
2914
2915 int n_attr = 0; /* chars with special attr */
2916 int saved_attr2 = 0; /* char_attr saved for n_attr */
2917 int n_attr3 = 0; /* chars with overruling special attr */
2918 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2919
2920 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2921
2922 int fromcol, tocol; /* start/end of inverting */
2923 int fromcol_prev = -2; /* start of inverting after cursor */
2924 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002926 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 pos_T pos;
2928 long v;
2929
2930 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002931 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2933 in this line */
2934 int attr = 0; /* attributes for area highlighting */
2935 int area_attr = 0; /* attributes desired by highlighting */
2936 int search_attr = 0; /* attributes desired by 'hlsearch' */
2937#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002938 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 int syntax_attr = 0; /* attributes desired by syntax */
2940 int has_syntax = FALSE; /* this buffer has syntax highl. */
2941 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002942 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002943 int draw_color_col = FALSE; /* highlight colorcolumn */
2944 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002945#endif
2946#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002947 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002948# define SPWORDLEN 150
2949 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002950 int nextlinecol = 0; /* column where nextline[] starts */
2951 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002952 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002953 int spell_attr = 0; /* attributes desired by spelling */
2954 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002955 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2956 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002957 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002958 static int cap_col = -1; /* column to check for Cap word */
2959 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002960 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961#endif
2962 int extra_check; /* has syntax or linebreak */
2963#ifdef FEAT_MBYTE
2964 int multi_attr = 0; /* attributes desired by multibyte */
2965 int mb_l = 1; /* multi-byte byte length */
2966 int mb_c = 0; /* decoded multi-byte character */
2967 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002968 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#endif
2970#ifdef FEAT_DIFF
2971 int filler_lines; /* nr of filler lines to be drawn */
2972 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002973 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 int change_start = MAXCOL; /* first col of changed area */
2975 int change_end = -1; /* last col of changed area */
2976#endif
2977 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2978#ifdef FEAT_LINEBREAK
2979 int need_showbreak = FALSE;
2980#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002981#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2982 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002984 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#endif
2986#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002987 matchitem_T *cur; /* points to the match list */
2988 match_T *shl; /* points to search_hl or a match */
2989 int shl_flag; /* flag to indicate whether search_hl
2990 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002991 int pos_inprogress; /* marks that position match search is
2992 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002993 int prevcol_hl_flag; /* flag to indicate whether prevcol
2994 equals startcol of search_hl or one
2995 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996#endif
2997#ifdef FEAT_ARABIC
2998 int prev_c = 0; /* previous Arabic character */
2999 int prev_c1 = 0; /* first composing char for prev_c */
3000#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003001#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003002 int did_line_attr = 0;
3003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 /* draw_state: items that are drawn in sequence: */
3006#define WL_START 0 /* nothing done yet */
3007#ifdef FEAT_CMDWIN
3008# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3009#else
3010# define WL_CMDLINE WL_START
3011#endif
3012#ifdef FEAT_FOLDING
3013# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3014#else
3015# define WL_FOLD WL_CMDLINE
3016#endif
3017#ifdef FEAT_SIGNS
3018# define WL_SIGN WL_FOLD + 1 /* column for signs */
3019#else
3020# define WL_SIGN WL_FOLD /* column for signs */
3021#endif
3022#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003023#ifdef FEAT_LINEBREAK
3024# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003026# define WL_BRI WL_NR
3027#endif
3028#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3029# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3030#else
3031# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032#endif
3033#define WL_LINE WL_SBR + 1 /* text in the line */
3034 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003035#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 int feedback_col = 0;
3037 int feedback_old_attr = -1;
3038#endif
3039
Bram Moolenaar860cae12010-06-05 23:22:07 +02003040#ifdef FEAT_CONCEAL
3041 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003042 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003043 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003044 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003045 int is_concealing = FALSE;
3046 int boguscols = 0; /* nonexistent columns added to force
3047 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003048 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003049 int did_wcol = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003050 int match_conc = FALSE; /* cchar for match functions */
3051 int has_match_conc = FALSE; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003052 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003053# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003054# define FIX_FOR_BOGUSCOLS \
3055 { \
3056 n_extra += vcol_off; \
3057 vcol -= vcol_off; \
3058 vcol_off = 0; \
3059 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003060 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003061 boguscols = 0; \
3062 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003063#else
3064# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
3067 if (startrow > endrow) /* past the end already! */
3068 return startrow;
3069
3070 row = startrow;
3071 screen_row = row + W_WINROW(wp);
3072
3073 /*
3074 * To speed up the loop below, set extra_check when there is linebreak,
3075 * trailing white space and/or syntax processing to be done.
3076 */
3077#ifdef FEAT_LINEBREAK
3078 extra_check = wp->w_p_lbr;
3079#else
3080 extra_check = 0;
3081#endif
3082#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003083 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 {
3085 /* Prepare for syntax highlighting in this line. When there is an
3086 * error, stop syntax highlighting. */
3087 save_did_emsg = did_emsg;
3088 did_emsg = FALSE;
3089 syntax_start(wp, lnum);
3090 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003091 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 else
3093 {
3094 did_emsg = save_did_emsg;
3095 has_syntax = TRUE;
3096 extra_check = TRUE;
3097 }
3098 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003099
3100 /* Check for columns to display for 'colorcolumn'. */
3101 color_cols = wp->w_p_cc_cols;
3102 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003103 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003104#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003105
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003106#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003107 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003108 && *wp->w_s->b_p_spl != NUL
3109 && wp->w_s->b_langp.ga_len > 0
3110 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003111 {
3112 /* Prepare for spell checking. */
3113 has_spell = TRUE;
3114 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003115
3116 /* Get the start of the next line, so that words that wrap to the next
3117 * line are found too: "et<line-break>al.".
3118 * Trick: skip a few chars for C/shell/Vim comments */
3119 nextline[SPWORDLEN] = NUL;
3120 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3121 {
3122 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3123 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3124 }
3125
3126 /* When a word wrapped from the previous line the start of the current
3127 * line is valid. */
3128 if (lnum == checked_lnum)
3129 cur_checked_col = checked_col;
3130 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003131
3132 /* When there was a sentence end in the previous line may require a
3133 * word starting with capital in this line. In line 1 always check
3134 * the first word. */
3135 if (lnum != capcol_lnum)
3136 cap_col = -1;
3137 if (lnum == 1)
3138 cap_col = 0;
3139 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141#endif
3142
3143 /*
3144 * handle visual active in this window
3145 */
3146 fromcol = -10;
3147 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3149 {
3150 /* Visual is after curwin->w_cursor */
3151 if (ltoreq(curwin->w_cursor, VIsual))
3152 {
3153 top = &curwin->w_cursor;
3154 bot = &VIsual;
3155 }
3156 else /* Visual is before curwin->w_cursor */
3157 {
3158 top = &VIsual;
3159 bot = &curwin->w_cursor;
3160 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003161 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (VIsual_mode == Ctrl_V) /* block mode */
3163 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003164 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 fromcol = wp->w_old_cursor_fcol;
3167 tocol = wp->w_old_cursor_lcol;
3168 }
3169 }
3170 else /* non-block mode */
3171 {
3172 if (lnum > top->lnum && lnum <= bot->lnum)
3173 fromcol = 0;
3174 else if (lnum == top->lnum)
3175 {
3176 if (VIsual_mode == 'V') /* linewise */
3177 fromcol = 0;
3178 else
3179 {
3180 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3181 if (gchar_pos(top) == NUL)
3182 tocol = fromcol + 1;
3183 }
3184 }
3185 if (VIsual_mode != 'V' && lnum == bot->lnum)
3186 {
3187 if (*p_sel == 'e' && bot->col == 0
3188#ifdef FEAT_VIRTUALEDIT
3189 && bot->coladd == 0
3190#endif
3191 )
3192 {
3193 fromcol = -10;
3194 tocol = MAXCOL;
3195 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003196 else if (bot->col == MAXCOL)
3197 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 else
3199 {
3200 pos = *bot;
3201 if (*p_sel == 'e')
3202 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3203 else
3204 {
3205 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3206 ++tocol;
3207 }
3208 }
3209 }
3210 }
3211
3212#ifndef MSDOS
3213 /* Check if the character under the cursor should not be inverted */
3214 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3215# ifdef FEAT_GUI
3216 && !gui.in_use
3217# endif
3218 )
3219 noinvcur = TRUE;
3220#endif
3221
3222 /* if inverting in this line set area_highlighting */
3223 if (fromcol >= 0)
3224 {
3225 area_highlighting = TRUE;
3226 attr = hl_attr(HLF_V);
3227#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003228 if ((clip_star.available && !clip_star.owned
3229 && clip_isautosel_star())
3230 || (clip_plus.available && !clip_plus.owned
3231 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 attr = hl_attr(HLF_VNC);
3233#endif
3234 }
3235 }
3236
3237 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003238 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003240 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 && wp == curwin
3242 && lnum >= curwin->w_cursor.lnum
3243 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3244 {
3245 if (lnum == curwin->w_cursor.lnum)
3246 getvcol(curwin, &(curwin->w_cursor),
3247 (colnr_T *)&fromcol, NULL, NULL);
3248 else
3249 fromcol = 0;
3250 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3251 {
3252 pos.lnum = lnum;
3253 pos.col = search_match_endcol;
3254 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3255 }
3256 else
3257 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003258 /* do at least one character; happens when past end of line */
3259 if (fromcol == tocol)
3260 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 area_highlighting = TRUE;
3262 attr = hl_attr(HLF_I);
3263 }
3264
3265#ifdef FEAT_DIFF
3266 filler_lines = diff_check(wp, lnum);
3267 if (filler_lines < 0)
3268 {
3269 if (filler_lines == -1)
3270 {
3271 if (diff_find_change(wp, lnum, &change_start, &change_end))
3272 diff_hlf = HLF_ADD; /* added line */
3273 else if (change_start == 0)
3274 diff_hlf = HLF_TXD; /* changed text */
3275 else
3276 diff_hlf = HLF_CHD; /* changed line */
3277 }
3278 else
3279 diff_hlf = HLF_ADD; /* added line */
3280 filler_lines = 0;
3281 area_highlighting = TRUE;
3282 }
3283 if (lnum == wp->w_topline)
3284 filler_lines = wp->w_topfill;
3285 filler_todo = filler_lines;
3286#endif
3287
3288#ifdef LINE_ATTR
3289# ifdef FEAT_SIGNS
3290 /* If this line has a sign with line highlighting set line_attr. */
3291 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3292 if (v != 0)
3293 line_attr = sign_get_attr((int)v, TRUE);
3294# endif
3295# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3296 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003297 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 line_attr = hl_attr(HLF_L);
3299# endif
3300 if (line_attr != 0)
3301 area_highlighting = TRUE;
3302#endif
3303
3304 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3305 ptr = line;
3306
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003307#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003308 if (has_spell)
3309 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003310 /* For checking first word with a capital skip white space. */
3311 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003312 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003313
Bram Moolenaar30abd282005-06-22 22:35:10 +00003314 /* To be able to spell-check over line boundaries copy the end of the
3315 * current line into nextline[]. Above the start of the next line was
3316 * copied to nextline[SPWORDLEN]. */
3317 if (nextline[SPWORDLEN] == NUL)
3318 {
3319 /* No next line or it is empty. */
3320 nextlinecol = MAXCOL;
3321 nextline_idx = 0;
3322 }
3323 else
3324 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003325 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003326 if (v < SPWORDLEN)
3327 {
3328 /* Short line, use it completely and append the start of the
3329 * next line. */
3330 nextlinecol = 0;
3331 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003332 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003333 nextline_idx = v + 1;
3334 }
3335 else
3336 {
3337 /* Long line, use only the last SPWORDLEN bytes. */
3338 nextlinecol = v - SPWORDLEN;
3339 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3340 nextline_idx = SPWORDLEN + 1;
3341 }
3342 }
3343 }
3344#endif
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 /* find start of trailing whitespace */
3347 if (wp->w_p_list && lcs_trail)
3348 {
3349 trailcol = (colnr_T)STRLEN(ptr);
3350 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3351 --trailcol;
3352 trailcol += (colnr_T) (ptr - line);
3353 extra_check = TRUE;
3354 }
3355
3356 /*
3357 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3358 * first character to be displayed.
3359 */
3360 if (wp->w_p_wrap)
3361 v = wp->w_skipcol;
3362 else
3363 v = wp->w_leftcol;
3364 if (v > 0)
3365 {
3366#ifdef FEAT_MBYTE
3367 char_u *prev_ptr = ptr;
3368#endif
3369 while (vcol < v && *ptr != NUL)
3370 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003371 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 vcol += c;
3373#ifdef FEAT_MBYTE
3374 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003376 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003379 /* When:
3380 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003381 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003382 * - 'virtualedit' is set, or
3383 * - the visual mode is active,
3384 * the end of the line may be before the start of the displayed part.
3385 */
3386 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003387#ifdef FEAT_SYN_HL
3388 wp->w_p_cuc || draw_color_col ||
3389#endif
3390#ifdef FEAT_VIRTUALEDIT
3391 virtual_active() ||
3392#endif
3393 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
3398 /* Handle a character that's not completely on the screen: Put ptr at
3399 * that character but skip the first few screen characters. */
3400 if (vcol > v)
3401 {
3402 vcol -= c;
3403#ifdef FEAT_MBYTE
3404 ptr = prev_ptr;
3405#else
3406 --ptr;
3407#endif
3408 n_skip = v - vcol;
3409 }
3410
3411 /*
3412 * Adjust for when the inverted text is before the screen,
3413 * and when the start of the inverted text is before the screen.
3414 */
3415 if (tocol <= vcol)
3416 fromcol = 0;
3417 else if (fromcol >= 0 && fromcol < vcol)
3418 fromcol = vcol;
3419
3420#ifdef FEAT_LINEBREAK
3421 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3422 if (wp->w_p_wrap)
3423 need_showbreak = TRUE;
3424#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003425#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003426 /* When spell checking a word we need to figure out the start of the
3427 * word and if it's badly spelled or not. */
3428 if (has_spell)
3429 {
3430 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003431 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003432 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003433
3434 pos = wp->w_cursor;
3435 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003436 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003437 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003438
3439 /* spell_move_to() may call ml_get() and make "line" invalid */
3440 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3441 ptr = line + linecol;
3442
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003443 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003444 {
3445 /* no bad word found at line start, don't check until end of a
3446 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003447 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003448 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003449 }
3450 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003451 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003452 /* bad word found, use attributes until end of word */
3453 word_end = wp->w_cursor.col + len + 1;
3454
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003455 /* Turn index into actual attributes. */
3456 if (spell_hlf != HLF_COUNT)
3457 spell_attr = highlight_attr[spell_hlf];
3458 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003459 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003460
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003461# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003462 /* Need to restart syntax highlighting for this line. */
3463 if (has_syntax)
3464 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003465# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003466 }
3467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469
3470 /*
3471 * Correct highlighting for cursor that can't be disabled.
3472 * Avoids having to check this for each character.
3473 */
3474 if (fromcol >= 0)
3475 {
3476 if (noinvcur)
3477 {
3478 if ((colnr_T)fromcol == wp->w_virtcol)
3479 {
3480 /* highlighting starts at cursor, let it start just after the
3481 * cursor */
3482 fromcol_prev = fromcol;
3483 fromcol = -1;
3484 }
3485 else if ((colnr_T)fromcol < wp->w_virtcol)
3486 /* restart highlighting after the cursor */
3487 fromcol_prev = wp->w_virtcol;
3488 }
3489 if (fromcol >= tocol)
3490 fromcol = -1;
3491 }
3492
3493#ifdef FEAT_SEARCH_EXTRA
3494 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003495 * Handle highlighting the last used search pattern and matches.
3496 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003498 cur = wp->w_match_head;
3499 shl_flag = FALSE;
3500 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003502 if (shl_flag == FALSE)
3503 {
3504 shl = &search_hl;
3505 shl_flag = TRUE;
3506 }
3507 else
3508 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003509 shl->startcol = MAXCOL;
3510 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003512 v = (long)(ptr - line);
3513 if (cur != NULL)
3514 cur->pos.cur = 0;
3515 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3516
3517 /* Need to get the line again, a multi-line regexp may have made it
3518 * invalid. */
3519 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3520 ptr = line + v;
3521
3522 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003524 if (shl->lnum == lnum)
3525 shl->startcol = shl->rm.startpos[0].col;
3526 else
3527 shl->startcol = 0;
3528 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3529 - shl->rm.startpos[0].lnum)
3530 shl->endcol = shl->rm.endpos[0].col;
3531 else
3532 shl->endcol = MAXCOL;
3533 /* Highlight one character for an empty match. */
3534 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003537 if (has_mbyte && line[shl->endcol] != NUL)
3538 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003541 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003543 if ((long)shl->startcol < v) /* match at leftcol */
3544 {
3545 shl->attr_cur = shl->attr;
3546 search_attr = shl->attr;
3547 }
3548 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003550 if (shl != &search_hl && cur != NULL)
3551 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
3553#endif
3554
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003555#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003556 /* Cursor line highlighting for 'cursorline' in the current window. Not
3557 * when Visual mode is active, because it's not clear what is selected
3558 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003559 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003560 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003561 {
3562 line_attr = hl_attr(HLF_CUL);
3563 area_highlighting = TRUE;
3564 }
3565#endif
3566
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003567 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 col = 0;
3569#ifdef FEAT_RIGHTLEFT
3570 if (wp->w_p_rl)
3571 {
3572 /* Rightleft window: process the text in the normal direction, but put
3573 * it in current_ScreenLine[] from right to left. Start at the
3574 * rightmost column of the window. */
3575 col = W_WIDTH(wp) - 1;
3576 off += col;
3577 }
3578#endif
3579
3580 /*
3581 * Repeat for the whole displayed line.
3582 */
3583 for (;;)
3584 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003585#ifdef FEAT_CONCEAL
3586 has_match_conc = FALSE;
3587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 /* Skip this quickly when working on the text. */
3589 if (draw_state != WL_LINE)
3590 {
3591#ifdef FEAT_CMDWIN
3592 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3593 {
3594 draw_state = WL_CMDLINE;
3595 if (cmdwin_type != 0 && wp == curwin)
3596 {
3597 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003599 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 char_attr = hl_attr(HLF_AT);
3601 }
3602 }
3603#endif
3604
3605#ifdef FEAT_FOLDING
3606 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3607 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003608 int fdc = compute_foldcolumn(wp, 0);
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003611 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
3613 /* Draw the 'foldcolumn'. */
3614 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003615 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003617 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 c_extra = NUL;
3619 char_attr = hl_attr(HLF_FC);
3620 }
3621 }
3622#endif
3623
3624#ifdef FEAT_SIGNS
3625 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3626 {
3627 draw_state = WL_SIGN;
3628 /* Show the sign column when there are any signs in this
3629 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003630 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003632 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003634 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635# endif
3636
3637 /* Draw two cells with the sign value or blank. */
3638 c_extra = ' ';
3639 char_attr = hl_attr(HLF_SC);
3640 n_extra = 2;
3641
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003642 if (row == startrow
3643#ifdef FEAT_DIFF
3644 + filler_lines && filler_todo <= 0
3645#endif
3646 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
3648 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3649 SIGN_TEXT);
3650# ifdef FEAT_SIGN_ICONS
3651 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3652 SIGN_ICON);
3653 if (gui.in_use && icon_sign != 0)
3654 {
3655 /* Use the image in this position. */
3656 c_extra = SIGN_BYTE;
3657# ifdef FEAT_NETBEANS_INTG
3658 if (buf_signcount(wp->w_buffer, lnum) > 1)
3659 c_extra = MULTISIGN_BYTE;
3660# endif
3661 char_attr = icon_sign;
3662 }
3663 else
3664# endif
3665 if (text_sign != 0)
3666 {
3667 p_extra = sign_get_text(text_sign);
3668 if (p_extra != NULL)
3669 {
3670 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003671 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 char_attr = sign_get_attr(text_sign, FALSE);
3674 }
3675 }
3676 }
3677 }
3678#endif
3679
3680 if (draw_state == WL_NR - 1 && n_extra == 0)
3681 {
3682 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003683 /* Display the absolute or relative line number. After the
3684 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3685 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 && (row == startrow
3687#ifdef FEAT_DIFF
3688 + filler_lines
3689#endif
3690 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3691 {
3692 /* Draw the line number (empty space after wrapping). */
3693 if (row == startrow
3694#ifdef FEAT_DIFF
3695 + filler_lines
3696#endif
3697 )
3698 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003699 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003700 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003701
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003702 if (wp->w_p_nu && !wp->w_p_rnu)
3703 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003704 num = (long)lnum;
3705 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003706 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003707 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003708 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003709 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003710 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003711 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003712 num = lnum;
3713 fmt = "%-*ld ";
3714 }
3715 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003716
Bram Moolenaar700e7342013-01-30 12:31:36 +01003717 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003718 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (wp->w_skipcol > 0)
3720 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3721 *p_extra = '-';
3722#ifdef FEAT_RIGHTLEFT
3723 if (wp->w_p_rl) /* reverse line numbers */
3724 rl_mirror(extra);
3725#endif
3726 p_extra = extra;
3727 c_extra = NUL;
3728 }
3729 else
3730 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003731 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003733#ifdef FEAT_SYN_HL
3734 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003735 * the current line differently.
3736 * TODO: Can we use CursorLine instead of CursorLineNr
3737 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003738 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003739 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003740 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743 }
3744
Bram Moolenaar597a4222014-06-25 14:39:50 +02003745#ifdef FEAT_LINEBREAK
3746 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3747 && n_extra == 0 && *p_sbr != NUL)
3748 /* draw indent after showbreak value */
3749 draw_state = WL_BRI;
3750 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3751 /* After the showbreak, draw the breakindent */
3752 draw_state = WL_BRI - 1;
3753
3754 /* draw 'breakindent': indent wrapped text accordingly */
3755 if (draw_state == WL_BRI - 1 && n_extra == 0)
3756 {
3757 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003758 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003759# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003760 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003761# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003762 )
3763 {
3764 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003765# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003766 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003767 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003768 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003769# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003770 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3771 char_attr = hl_combine_attr(char_attr,
3772 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003773# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003774 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003775# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003776 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003777 c_extra = ' ';
3778 n_extra = get_breakindent_win(wp,
3779 ml_get_buf(wp->w_buffer, lnum, FALSE));
3780 /* Correct end of highlighted area for 'breakindent',
3781 * required when 'linebreak' is also set. */
3782 if (tocol == vcol)
3783 tocol += n_extra;
3784 }
3785 }
3786#endif
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3789 if (draw_state == WL_SBR - 1 && n_extra == 0)
3790 {
3791 draw_state = WL_SBR;
3792# ifdef FEAT_DIFF
3793 if (filler_todo > 0)
3794 {
3795 /* Draw "deleted" diff line(s). */
3796 if (char2cells(fill_diff) > 1)
3797 c_extra = '-';
3798 else
3799 c_extra = fill_diff;
3800# ifdef FEAT_RIGHTLEFT
3801 if (wp->w_p_rl)
3802 n_extra = col + 1;
3803 else
3804# endif
3805 n_extra = W_WIDTH(wp) - col;
3806 char_attr = hl_attr(HLF_DED);
3807 }
3808# endif
3809# ifdef FEAT_LINEBREAK
3810 if (*p_sbr != NUL && need_showbreak)
3811 {
3812 /* Draw 'showbreak' at the start of each broken line. */
3813 p_extra = p_sbr;
3814 c_extra = NUL;
3815 n_extra = (int)STRLEN(p_sbr);
3816 char_attr = hl_attr(HLF_AT);
3817 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003818 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 /* Correct end of highlighted area for 'showbreak',
3820 * required when 'linebreak' is also set. */
3821 if (tocol == vcol)
3822 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003823#ifdef FEAT_SYN_HL
3824 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003825 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003826 char_attr = hl_combine_attr(char_attr,
3827 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830# endif
3831 }
3832#endif
3833
3834 if (draw_state == WL_LINE - 1 && n_extra == 0)
3835 {
3836 draw_state = WL_LINE;
3837 if (saved_n_extra)
3838 {
3839 /* Continue item from end of wrapped line. */
3840 n_extra = saved_n_extra;
3841 c_extra = saved_c_extra;
3842 p_extra = saved_p_extra;
3843 char_attr = saved_char_attr;
3844 }
3845 else
3846 char_attr = 0;
3847 }
3848 }
3849
3850 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003851 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003852 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853#ifdef FEAT_DIFF
3854 && filler_todo <= 0
3855#endif
3856 )
3857 {
3858 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3859 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003860 /* Pretend we have finished updating the window. Except when
3861 * 'cursorcolumn' is set. */
3862#ifdef FEAT_SYN_HL
3863 if (wp->w_p_cuc)
3864 row = wp->w_cline_row + wp->w_cline_height;
3865 else
3866#endif
3867 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 break;
3869 }
3870
3871 if (draw_state == WL_LINE && area_highlighting)
3872 {
3873 /* handle Visual or match highlighting in this line */
3874 if (vcol == fromcol
3875#ifdef FEAT_MBYTE
3876 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3877 && (*mb_ptr2cells)(ptr) > 1)
3878#endif
3879 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003880 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 && vcol < tocol))
3882 area_attr = attr; /* start highlighting */
3883 else if (area_attr != 0
3884 && (vcol == tocol
3885 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888#ifdef FEAT_SEARCH_EXTRA
3889 if (!n_extra)
3890 {
3891 /*
3892 * Check for start/end of search pattern match.
3893 * After end, check for start/end of next match.
3894 * When another match, have to check for start again.
3895 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003896 * Do this for 'search_hl' and the match list (ordered by
3897 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003899 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003900 cur = wp->w_match_head;
3901 shl_flag = FALSE;
3902 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003904 if (shl_flag == FALSE
3905 && ((cur != NULL
3906 && cur->priority > SEARCH_HL_PRIORITY)
3907 || cur == NULL))
3908 {
3909 shl = &search_hl;
3910 shl_flag = TRUE;
3911 }
3912 else
3913 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003914 if (cur != NULL)
3915 cur->pos.cur = 0;
3916 pos_inprogress = TRUE;
3917 while (shl->rm.regprog != NULL
3918 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003920 if (shl->startcol != MAXCOL
3921 && v >= (long)shl->startcol
3922 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003924#ifdef FEAT_MBYTE
3925 int tmp_col = v + MB_PTR2LEN(ptr);
3926
3927 if (shl->endcol < tmp_col)
3928 shl->endcol = tmp_col;
3929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003931#ifdef FEAT_CONCEAL
3932 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3933 == cur->hlg_id)
3934 {
3935 has_match_conc = TRUE;
3936 match_conc = cur->conceal_char;
3937 }
3938 else
3939 has_match_conc = match_conc = FALSE;
3940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003942 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
3944 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003945#ifdef FEAT_CONCEAL
3946 prev_syntax_id = 0;
3947#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003948 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3949 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003950 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
3952 /* Need to get the line again, a multi-line regexp
3953 * may have made it invalid. */
3954 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3955 ptr = line + v;
3956
3957 if (shl->lnum == lnum)
3958 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003959 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003961 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003963 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003965 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
3967 /* highlight empty match, try again after
3968 * it */
3969#ifdef FEAT_MBYTE
3970 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003971 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003972 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 else
3974#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003975 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
3977
3978 /* Loop to check if the match starts at the
3979 * current position */
3980 continue;
3981 }
3982 }
3983 break;
3984 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003985 if (shl != &search_hl && cur != NULL)
3986 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003988
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003989 /* Use attributes from match with highest priority among
3990 * 'search_hl' and the match list. */
3991 search_attr = search_hl.attr_cur;
3992 cur = wp->w_match_head;
3993 shl_flag = FALSE;
3994 while (cur != NULL || shl_flag == FALSE)
3995 {
3996 if (shl_flag == FALSE
3997 && ((cur != NULL
3998 && cur->priority > SEARCH_HL_PRIORITY)
3999 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004000 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004001 shl = &search_hl;
4002 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004003 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004004 else
4005 shl = &cur->hl;
4006 if (shl->attr_cur != 0)
4007 search_attr = shl->attr_cur;
4008 if (shl != &search_hl && cur != NULL)
4009 cur = cur->next;
4010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012#endif
4013
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004015 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004017 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4018 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004020 if (diff_hlf == HLF_TXD && ptr - line > change_end
4021 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004023 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004024 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4025 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004028
4029 /* Decide which of the highlight attributes to use. */
4030 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004031#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004032 if (area_attr != 0)
4033 char_attr = hl_combine_attr(line_attr, area_attr);
4034 else if (search_attr != 0)
4035 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004036 /* Use line_attr when not in the Visual or 'incsearch' area
4037 * (area_attr may be 0 when "noinvcur" is set). */
4038 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004039 || vcol < fromcol || vcol_prev < fromcol_prev
4040 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004041 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004042#else
4043 if (area_attr != 0)
4044 char_attr = area_attr;
4045 else if (search_attr != 0)
4046 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004047#endif
4048 else
4049 {
4050 attr_pri = FALSE;
4051#ifdef FEAT_SYN_HL
4052 if (has_syntax)
4053 char_attr = syntax_attr;
4054 else
4055#endif
4056 char_attr = 0;
4057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059
4060 /*
4061 * Get the next character to put on the screen.
4062 */
4063 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004064 * The "p_extra" points to the extra stuff that is inserted to
4065 * represent special characters (non-printable stuff) and other
4066 * things. When all characters are the same, c_extra is used.
4067 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4068 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4070 */
4071 if (n_extra > 0)
4072 {
4073 if (c_extra != NUL)
4074 {
4075 c = c_extra;
4076#ifdef FEAT_MBYTE
4077 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4078 if (enc_utf8 && (*mb_char2len)(c) > 1)
4079 {
4080 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004081 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004082 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 else
4085 mb_utf8 = FALSE;
4086#endif
4087 }
4088 else
4089 {
4090 c = *p_extra;
4091#ifdef FEAT_MBYTE
4092 if (has_mbyte)
4093 {
4094 mb_c = c;
4095 if (enc_utf8)
4096 {
4097 /* If the UTF-8 character is more than one byte:
4098 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004099 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 mb_utf8 = FALSE;
4101 if (mb_l > n_extra)
4102 mb_l = 1;
4103 else if (mb_l > 1)
4104 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004105 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004107 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109 }
4110 else
4111 {
4112 /* if this is a DBCS character, put it in "mb_c" */
4113 mb_l = MB_BYTE2LEN(c);
4114 if (mb_l >= n_extra)
4115 mb_l = 1;
4116 else if (mb_l > 1)
4117 mb_c = (c << 8) + p_extra[1];
4118 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004119 if (mb_l == 0) /* at the NUL at end-of-line */
4120 mb_l = 1;
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 /* If a double-width char doesn't fit display a '>' in the
4123 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004124 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125# ifdef FEAT_RIGHTLEFT
4126 wp->w_p_rl ? (col <= 0) :
4127# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004128 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 && (*mb_char2cells)(mb_c) == 2)
4130 {
4131 c = '>';
4132 mb_c = c;
4133 mb_l = 1;
4134 mb_utf8 = FALSE;
4135 multi_attr = hl_attr(HLF_AT);
4136 /* put the pointer back to output the double-width
4137 * character at the start of the next line. */
4138 ++n_extra;
4139 --p_extra;
4140 }
4141 else
4142 {
4143 n_extra -= mb_l - 1;
4144 p_extra += mb_l - 1;
4145 }
4146 }
4147#endif
4148 ++p_extra;
4149 }
4150 --n_extra;
4151 }
4152 else
4153 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004154 if (p_extra_free != NULL)
4155 {
4156 vim_free(p_extra_free);
4157 p_extra_free = NULL;
4158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 /*
4160 * Get a character from the line itself.
4161 */
4162 c = *ptr;
4163#ifdef FEAT_MBYTE
4164 if (has_mbyte)
4165 {
4166 mb_c = c;
4167 if (enc_utf8)
4168 {
4169 /* If the UTF-8 character is more than one byte: Decode it
4170 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004171 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 mb_utf8 = FALSE;
4173 if (mb_l > 1)
4174 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004175 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 /* Overlong encoded ASCII or ASCII with composing char
4177 * is displayed normally, except a NUL. */
4178 if (mb_c < 0x80)
4179 c = mb_c;
4180 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004181
4182 /* At start of the line we can have a composing char.
4183 * Draw it as a space with a composing char. */
4184 if (utf_iscomposing(mb_c))
4185 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004186 int i;
4187
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004188 for (i = Screen_mco - 1; i > 0; --i)
4189 u8cc[i] = u8cc[i - 1];
4190 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004191 mb_c = ' ';
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194
4195 if ((mb_l == 1 && c >= 0x80)
4196 || (mb_l >= 1 && mb_c == 0)
4197 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004198# ifdef UNICODE16
4199 || mb_c >= 0x10000
4200# endif
4201 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 /*
4204 * Illegal UTF-8 byte: display as <xx>.
4205 * Non-BMP character : display as ? or fullwidth ?.
4206 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004207# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
4211 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004212# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (wp->w_p_rl) /* reverse */
4214 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004217# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 else if (utf_char2cells(mb_c) != 2)
4219 STRCPY(extra, "?");
4220 else
4221 /* 0xff1f in UTF-8: full-width '?' */
4222 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225 p_extra = extra;
4226 c = *p_extra;
4227 mb_c = mb_ptr2char_adv(&p_extra);
4228 mb_utf8 = (c >= 0x80);
4229 n_extra = (int)STRLEN(p_extra);
4230 c_extra = NUL;
4231 if (area_attr == 0 && search_attr == 0)
4232 {
4233 n_attr = n_extra + 1;
4234 extra_attr = hl_attr(HLF_8);
4235 saved_attr2 = char_attr; /* save current attr */
4236 }
4237 }
4238 else if (mb_l == 0) /* at the NUL at end-of-line */
4239 mb_l = 1;
4240#ifdef FEAT_ARABIC
4241 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4242 {
4243 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004244 int pc, pc1, nc;
4245 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
4247 /* The idea of what is the previous and next
4248 * character depends on 'rightleft'. */
4249 if (wp->w_p_rl)
4250 {
4251 pc = prev_c;
4252 pc1 = prev_c1;
4253 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004254 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 else
4257 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004258 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004260 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 prev_c = mb_c;
4263
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004264 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 else
4267 prev_c = mb_c;
4268#endif
4269 }
4270 else /* enc_dbcs */
4271 {
4272 mb_l = MB_BYTE2LEN(c);
4273 if (mb_l == 0) /* at the NUL at end-of-line */
4274 mb_l = 1;
4275 else if (mb_l > 1)
4276 {
4277 /* We assume a second byte below 32 is illegal.
4278 * Hopefully this is OK for all double-byte encodings!
4279 */
4280 if (ptr[1] >= 32)
4281 mb_c = (c << 8) + ptr[1];
4282 else
4283 {
4284 if (ptr[1] == NUL)
4285 {
4286 /* head byte at end of line */
4287 mb_l = 1;
4288 transchar_nonprint(extra, c);
4289 }
4290 else
4291 {
4292 /* illegal tail byte */
4293 mb_l = 2;
4294 STRCPY(extra, "XX");
4295 }
4296 p_extra = extra;
4297 n_extra = (int)STRLEN(extra) - 1;
4298 c_extra = NUL;
4299 c = *p_extra++;
4300 if (area_attr == 0 && search_attr == 0)
4301 {
4302 n_attr = n_extra + 1;
4303 extra_attr = hl_attr(HLF_8);
4304 saved_attr2 = char_attr; /* save current attr */
4305 }
4306 mb_c = c;
4307 }
4308 }
4309 }
4310 /* If a double-width char doesn't fit display a '>' in the
4311 * last column; the character is displayed at the start of the
4312 * next line. */
4313 if ((
4314# ifdef FEAT_RIGHTLEFT
4315 wp->w_p_rl ? (col <= 0) :
4316# endif
4317 (col >= W_WIDTH(wp) - 1))
4318 && (*mb_char2cells)(mb_c) == 2)
4319 {
4320 c = '>';
4321 mb_c = c;
4322 mb_utf8 = FALSE;
4323 mb_l = 1;
4324 multi_attr = hl_attr(HLF_AT);
4325 /* Put pointer back so that the character will be
4326 * displayed at the start of the next line. */
4327 --ptr;
4328 }
4329 else if (*ptr != NUL)
4330 ptr += mb_l - 1;
4331
4332 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004333 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004334 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004335 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004338 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 c = ' ';
4340 if (area_attr == 0 && search_attr == 0)
4341 {
4342 n_attr = n_extra + 1;
4343 extra_attr = hl_attr(HLF_AT);
4344 saved_attr2 = char_attr; /* save current attr */
4345 }
4346 mb_c = c;
4347 mb_utf8 = FALSE;
4348 mb_l = 1;
4349 }
4350
4351 }
4352#endif
4353 ++ptr;
4354
Bram Moolenaar79278362015-04-21 18:33:48 +02004355 /* 'list': change char 160 to lcs_nbsp and space to lcs_space. */
4356 if (wp->w_p_list
4357 && (((c == 160
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004358#ifdef FEAT_MBYTE
Bram Moolenaar73284b92015-05-04 17:28:22 +02004359 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004360#endif
Bram Moolenaar79278362015-04-21 18:33:48 +02004361 ) && lcs_nbsp)
Bram Moolenaarc4dc2862015-05-04 16:10:26 +02004362 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004363 {
Bram Moolenaar79278362015-04-21 18:33:48 +02004364 c = (c == ' ') ? lcs_space : lcs_nbsp;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004365 if (area_attr == 0 && search_attr == 0)
4366 {
4367 n_attr = 1;
4368 extra_attr = hl_attr(HLF_8);
4369 saved_attr2 = char_attr; /* save current attr */
4370 }
4371#ifdef FEAT_MBYTE
4372 mb_c = c;
4373 if (enc_utf8 && (*mb_char2len)(c) > 1)
4374 {
4375 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004376 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004377 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004378 }
4379 else
4380 mb_utf8 = FALSE;
4381#endif
4382 }
4383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (extra_check)
4385 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004386#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004387 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004388#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004389
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004390#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 /* Get syntax attribute, unless still at the start of the line
4392 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004393 v = (long)(ptr - line);
4394 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
4396 /* Get the syntax attribute for the character. If there
4397 * is an error, disable syntax highlighting. */
4398 save_did_emsg = did_emsg;
4399 did_emsg = FALSE;
4400
Bram Moolenaar217ad922005-03-20 22:37:15 +00004401 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004402# ifdef FEAT_SPELL
4403 has_spell ? &can_spell :
4404# endif
4405 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
4407 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004408 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004409 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004410 has_syntax = FALSE;
4411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 else
4413 did_emsg = save_did_emsg;
4414
4415 /* Need to get the line again, a multi-line regexp may
4416 * have made it invalid. */
4417 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4418 ptr = line + v;
4419
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004420 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004422 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004423 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004424# ifdef FEAT_CONCEAL
4425 /* no concealing past the end of the line, it interferes
4426 * with line highlighting */
4427 if (c == NUL)
4428 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004429 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004430 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004433#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004434
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004435#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004436 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004437 * Only do this when there is no syntax highlighting, the
4438 * @Spell cluster is not used or the current syntax item
4439 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004440 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004441 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004442 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004443# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004444 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004445 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004446# endif
4447 if (c != 0 && (
4448# ifdef FEAT_SYN_HL
4449 !has_syntax ||
4450# endif
4451 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004452 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004453 char_u *prev_ptr, *p;
4454 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004455 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004456# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004457 if (has_mbyte)
4458 {
4459 prev_ptr = ptr - mb_l;
4460 v -= mb_l - 1;
4461 }
4462 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004463# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004464 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004465
4466 /* Use nextline[] if possible, it has the start of the
4467 * next line concatenated. */
4468 if ((prev_ptr - line) - nextlinecol >= 0)
4469 p = nextline + (prev_ptr - line) - nextlinecol;
4470 else
4471 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004472 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004473 len = spell_check(wp, p, &spell_hlf, &cap_col,
4474 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004475 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004476
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004477 /* In Insert mode only highlight a word that
4478 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004479 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004480 && (State & INSERT) != 0
4481 && wp->w_cursor.lnum == lnum
4482 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004483 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004484 && wp->w_cursor.col < (colnr_T)word_end)
4485 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004486 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004487 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004488 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004489
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004490 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004491 && (p - nextline) + len > nextline_idx)
4492 {
4493 /* Remember that the good word continues at the
4494 * start of the next line. */
4495 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004496 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004497 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004498
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004499 /* Turn index into actual attributes. */
4500 if (spell_hlf != HLF_COUNT)
4501 spell_attr = highlight_attr[spell_hlf];
4502
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004503 if (cap_col > 0)
4504 {
4505 if (p != prev_ptr
4506 && (p - nextline) + cap_col >= nextline_idx)
4507 {
4508 /* Remember that the word in the next line
4509 * must start with a capital. */
4510 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004511 cap_col = (int)((p - nextline) + cap_col
4512 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004513 }
4514 else
4515 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004516 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004517 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004518 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004519 }
4520 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004521 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004522 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004523 char_attr = hl_combine_attr(char_attr, spell_attr);
4524 else
4525 char_attr = hl_combine_attr(spell_attr, char_attr);
4526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#endif
4528#ifdef FEAT_LINEBREAK
4529 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004530 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004532 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004534# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004535 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004536# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004537 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004539 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004541 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004542
Bram Moolenaar597a4222014-06-25 14:39:50 +02004543 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004544 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004545 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004546 if (c == TAB && n_extra + col > W_WIDTH(wp))
4547 n_extra = (int)wp->w_buffer->b_p_ts
4548 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4549
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004550# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004551 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004552# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004556 {
4557#ifdef FEAT_CONCEAL
4558 if (c == TAB)
4559 /* See "Tab alignment" below. */
4560 FIX_FOR_BOGUSCOLS;
4561#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004562 if (!wp->w_p_list)
4563 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566#endif
4567
4568 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4569 {
4570 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004571 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
4573 n_attr = 1;
4574 extra_attr = hl_attr(HLF_8);
4575 saved_attr2 = char_attr; /* save current attr */
4576 }
4577#ifdef FEAT_MBYTE
4578 mb_c = c;
4579 if (enc_utf8 && (*mb_char2len)(c) > 1)
4580 {
4581 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004582 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004583 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 else
4586 mb_utf8 = FALSE;
4587#endif
4588 }
4589 }
4590
4591 /*
4592 * Handling of non-printable characters.
4593 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004594 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
4596 /*
4597 * when getting a character from the file, we may have to
4598 * turn it into something else on the way to putting it
4599 * into "ScreenLines".
4600 */
4601 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4602 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004603 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004604 long vcol_adjusted = vcol; /* removed showbreak length */
4605#ifdef FEAT_LINEBREAK
4606 /* only adjust the tab_len, when at the first column
4607 * after the showbreak value was drawn */
4608 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4609 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004612 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004613 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4614
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004615#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004616 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004617#endif
4618 /* tab amount depends on current column */
4619 n_extra = tab_len;
4620#ifdef FEAT_LINEBREAK
4621 else
4622 {
4623 char_u *p;
4624 int len = n_extra;
4625 int i;
4626 int saved_nextra = n_extra;
4627
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004628#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004629 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004630 /* there are characters to conceal */
4631 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004632 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4633 */
4634 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4635 && n_extra > tab_len)
4636 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004637#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004638
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004639 /* if n_extra > 0, it gives the number of chars, to
4640 * use for a tab, else we need to calculate the width
4641 * for a tab */
4642#ifdef FEAT_MBYTE
4643 len = (tab_len * mb_char2len(lcs_tab2));
4644 if (n_extra > 0)
4645 len += n_extra - tab_len;
4646#endif
4647 c = lcs_tab1;
4648 p = alloc((unsigned)(len + 1));
4649 vim_memset(p, ' ', len);
4650 p[len] = NUL;
4651 p_extra_free = p;
4652 for (i = 0; i < tab_len; i++)
4653 {
4654#ifdef FEAT_MBYTE
4655 mb_char2bytes(lcs_tab2, p);
4656 p += mb_char2len(lcs_tab2);
4657 n_extra += mb_char2len(lcs_tab2)
4658 - (saved_nextra > 0 ? 1 : 0);
4659#else
4660 p[i] = lcs_tab2;
4661#endif
4662 }
4663 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004664#ifdef FEAT_CONCEAL
4665 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4666 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004667 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004668 n_extra -= vcol_off;
4669#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004670 }
4671#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004672#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004673 {
4674 int vc_saved = vcol_off;
4675
4676 /* Tab alignment should be identical regardless of
4677 * 'conceallevel' value. So tab compensates of all
4678 * previous concealed characters, and thus resets
4679 * vcol_off and boguscols accumulated so far in the
4680 * line. Note that the tab can be longer than
4681 * 'tabstop' when there are concealed characters. */
4682 FIX_FOR_BOGUSCOLS;
4683
4684 /* Make sure, the highlighting for the tab char will be
4685 * correctly set further below (effectively reverts the
4686 * FIX_FOR_BOGSUCOLS macro */
4687 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004688 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004689 tab_len += vc_saved;
4690 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692#ifdef FEAT_MBYTE
4693 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4694#endif
4695 if (wp->w_p_list)
4696 {
4697 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004698#ifdef FEAT_LINEBREAK
4699 if (wp->w_p_lbr)
4700 c_extra = NUL; /* using p_extra from above */
4701 else
4702#endif
4703 c_extra = lcs_tab2;
4704 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 extra_attr = hl_attr(HLF_8);
4706 saved_attr2 = char_attr; /* save current attr */
4707#ifdef FEAT_MBYTE
4708 mb_c = c;
4709 if (enc_utf8 && (*mb_char2len)(c) > 1)
4710 {
4711 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004712 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004713 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
4715#endif
4716 }
4717 else
4718 {
4719 c_extra = ' ';
4720 c = ' ';
4721 }
4722 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004723 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004724 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004725 || ((fromcol >= 0 || fromcol_prev >= 0)
4726 && tocol > vcol
4727 && VIsual_mode != Ctrl_V
4728 && (
4729# ifdef FEAT_RIGHTLEFT
4730 wp->w_p_rl ? (col >= 0) :
4731# endif
4732 (col < W_WIDTH(wp)))
4733 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004734 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004735 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004736 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004738 /* Display a '$' after the line or highlight an extra
4739 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4741 /* For a diff line the highlighting continues after the
4742 * "$". */
4743 if (
4744# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004745 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746# ifdef LINE_ATTR
4747 &&
4748# endif
4749# endif
4750# ifdef LINE_ATTR
4751 line_attr == 0
4752# endif
4753 )
4754#endif
4755 {
4756#ifdef FEAT_VIRTUALEDIT
4757 /* In virtualedit, visual selections may extend
4758 * beyond end of line. */
4759 if (area_highlighting && virtual_active()
4760 && tocol != MAXCOL && vcol < tocol)
4761 n_extra = 0;
4762 else
4763#endif
4764 {
4765 p_extra = at_end_str;
4766 n_extra = 1;
4767 c_extra = NUL;
4768 }
4769 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004770 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004771 c = lcs_eol;
4772 else
4773 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 lcs_eol_one = -1;
4775 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004776 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 extra_attr = hl_attr(HLF_AT);
4779 n_attr = 1;
4780 }
4781#ifdef FEAT_MBYTE
4782 mb_c = c;
4783 if (enc_utf8 && (*mb_char2len)(c) > 1)
4784 {
4785 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004786 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004787 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 else
4790 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4791#endif
4792 }
4793 else if (c != NUL)
4794 {
4795 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004796 if (n_extra == 0)
4797 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798#ifdef FEAT_RIGHTLEFT
4799 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4800 rl_mirror(p_extra); /* reverse "<12>" */
4801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004803#ifdef FEAT_LINEBREAK
4804 if (wp->w_p_lbr)
4805 {
4806 char_u *p;
4807
4808 c = *p_extra;
4809 p = alloc((unsigned)n_extra + 1);
4810 vim_memset(p, ' ', n_extra);
4811 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4812 p[n_extra] = NUL;
4813 p_extra_free = p_extra = p;
4814 }
4815 else
4816#endif
4817 {
4818 n_extra = byte2cells(c) - 1;
4819 c = *p_extra++;
4820 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004821 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
4823 n_attr = n_extra + 1;
4824 extra_attr = hl_attr(HLF_8);
4825 saved_attr2 = char_attr; /* save current attr */
4826 }
4827#ifdef FEAT_MBYTE
4828 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4829#endif
4830 }
4831#ifdef FEAT_VIRTUALEDIT
4832 else if (VIsual_active
4833 && (VIsual_mode == Ctrl_V
4834 || VIsual_mode == 'v')
4835 && virtual_active()
4836 && tocol != MAXCOL
4837 && vcol < tocol
4838 && (
4839# ifdef FEAT_RIGHTLEFT
4840 wp->w_p_rl ? (col >= 0) :
4841# endif
4842 (col < W_WIDTH(wp))))
4843 {
4844 c = ' ';
4845 --ptr; /* put it back at the NUL */
4846 }
4847#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004848#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 else if ((
4850# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004851 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 ) && (
4855# ifdef FEAT_RIGHTLEFT
4856 wp->w_p_rl ? (col >= 0) :
4857# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004858 (col
4859# ifdef FEAT_CONCEAL
4860 - boguscols
4861# endif
4862 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 /* Highlight until the right side of the window */
4865 c = ' ';
4866 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004867
4868 /* Remember we do the char for line highlighting. */
4869 ++did_line_attr;
4870
4871 /* don't do search HL for the rest of the line */
4872 if (line_attr != 0 && char_attr == search_attr && col > 0)
4873 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874# ifdef FEAT_DIFF
4875 if (diff_hlf == HLF_TXD)
4876 {
4877 diff_hlf = HLF_CHD;
4878 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004881 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4882 char_attr = hl_combine_attr(char_attr,
4883 hl_attr(HLF_CUL));
4884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886# endif
4887 }
4888#endif
4889 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004890
4891#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004892 if ( wp->w_p_cole > 0
4893 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004894 conceal_cursor_line(wp) )
4895 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004896 && !(lnum_in_visual_area
4897 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004898 {
4899 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004900 if (prev_syntax_id != syntax_seqnr
Bram Moolenaar6561d522015-07-21 15:48:27 +02004901 && (syn_get_sub_char() != NUL || match_conc
4902 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004903 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004904 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004905 /* First time at this concealed item: display one
4906 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004907 if (match_conc)
4908 c = match_conc;
4909 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004910 c = syn_get_sub_char();
4911 else if (lcs_conceal != NUL)
4912 c = lcs_conceal;
4913 else
4914 c = ' ';
4915
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004916 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004917
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004918 if (n_extra > 0)
4919 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004920 vcol += n_extra;
4921 if (wp->w_p_wrap && n_extra > 0)
4922 {
4923# ifdef FEAT_RIGHTLEFT
4924 if (wp->w_p_rl)
4925 {
4926 col -= n_extra;
4927 boguscols -= n_extra;
4928 }
4929 else
4930# endif
4931 {
4932 boguscols += n_extra;
4933 col += n_extra;
4934 }
4935 }
4936 n_extra = 0;
4937 n_attr = 0;
4938 }
4939 else if (n_skip == 0)
4940 {
4941 is_concealing = TRUE;
4942 n_skip = 1;
4943 }
4944# ifdef FEAT_MBYTE
4945 mb_c = c;
4946 if (enc_utf8 && (*mb_char2len)(c) > 1)
4947 {
4948 mb_utf8 = TRUE;
4949 u8cc[0] = 0;
4950 c = 0xc0;
4951 }
4952 else
4953 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4954# endif
4955 }
4956 else
4957 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004958 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004959 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004960 }
4961#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004964#ifdef FEAT_CONCEAL
4965 /* In the cursor line and we may be concealing characters: correct
4966 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004967 if (!did_wcol && draw_state == WL_LINE
4968 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004969 && conceal_cursor_line(wp)
4970 && (int)wp->w_virtcol <= vcol + n_skip)
4971 {
4972 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004973 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004974 did_wcol = TRUE;
4975 }
4976#endif
4977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 /* Don't override visual selection highlighting. */
4979 if (n_attr > 0
4980 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004981 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 char_attr = extra_attr;
4983
Bram Moolenaar81695252004-12-29 20:58:21 +00004984#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 /* XIM don't send preedit_start and preedit_end, but they send
4986 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4987 * im_is_preediting() here. */
4988 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004989 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 && (State & INSERT)
4991 && !p_imdisable
4992 && im_is_preediting()
4993 && draw_state == WL_LINE)
4994 {
4995 colnr_T tcol;
4996
4997 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004998 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 else
5000 tcol = preedit_end_col;
5001 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5002 {
5003 if (feedback_old_attr < 0)
5004 {
5005 feedback_col = 0;
5006 feedback_old_attr = char_attr;
5007 }
5008 char_attr = im_get_feedback_attr(feedback_col);
5009 if (char_attr < 0)
5010 char_attr = feedback_old_attr;
5011 feedback_col++;
5012 }
5013 else if (feedback_old_attr >= 0)
5014 {
5015 char_attr = feedback_old_attr;
5016 feedback_old_attr = -1;
5017 feedback_col = 0;
5018 }
5019 }
5020#endif
5021 /*
5022 * Handle the case where we are in column 0 but not on the first
5023 * character of the line and the user wants us to show us a
5024 * special character (via 'listchars' option "precedes:<char>".
5025 */
5026 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005027 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5029#ifdef FEAT_DIFF
5030 && filler_todo <= 0
5031#endif
5032 && draw_state > WL_NR
5033 && c != NUL)
5034 {
5035 c = lcs_prec;
5036 lcs_prec_todo = NUL;
5037#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005038 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5039 {
5040 /* Double-width character being overwritten by the "precedes"
5041 * character, need to fill up half the character. */
5042 c_extra = MB_FILLER_CHAR;
5043 n_extra = 1;
5044 n_attr = 2;
5045 extra_attr = hl_attr(HLF_AT);
5046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 mb_c = c;
5048 if (enc_utf8 && (*mb_char2len)(c) > 1)
5049 {
5050 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005051 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005052 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054 else
5055 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5056#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005057 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 saved_attr3 = char_attr; /* save current attr */
5060 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5061 n_attr3 = 1;
5062 }
5063 }
5064
5065 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005066 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005068 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005069#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005070 || did_line_attr == 1
5071#endif
5072 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005074#ifdef FEAT_SEARCH_EXTRA
5075 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005076
5077 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005078 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005079 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005080#endif
5081
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005082 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 * highlight match at end of line. If it's beyond the last
5084 * char on the screen, just overwrite that one (tricky!) Not
5085 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005086#ifdef FEAT_SEARCH_EXTRA
5087 prevcol_hl_flag = FALSE;
5088 if (prevcol == (long)search_hl.startcol)
5089 prevcol_hl_flag = TRUE;
5090 else
5091 {
5092 cur = wp->w_match_head;
5093 while (cur != NULL)
5094 {
5095 if (prevcol == (long)cur->hl.startcol)
5096 {
5097 prevcol_hl_flag = TRUE;
5098 break;
5099 }
5100 cur = cur->next;
5101 }
5102 }
5103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005105 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005106 && (VIsual_mode != Ctrl_V
5107 || lnum == VIsual.lnum
5108 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005109 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110#ifdef FEAT_SEARCH_EXTRA
5111 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005112 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005113# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005114 && did_line_attr <= 1
5115# endif
5116 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#endif
5118 ))
5119 {
5120 int n = 0;
5121
5122#ifdef FEAT_RIGHTLEFT
5123 if (wp->w_p_rl)
5124 {
5125 if (col < 0)
5126 n = 1;
5127 }
5128 else
5129#endif
5130 {
5131 if (col >= W_WIDTH(wp))
5132 n = -1;
5133 }
5134 if (n != 0)
5135 {
5136 /* At the window boundary, highlight the last character
5137 * instead (better than nothing). */
5138 off += n;
5139 col += n;
5140 }
5141 else
5142 {
5143 /* Add a blank character to highlight. */
5144 ScreenLines[off] = ' ';
5145#ifdef FEAT_MBYTE
5146 if (enc_utf8)
5147 ScreenLinesUC[off] = 0;
5148#endif
5149 }
5150#ifdef FEAT_SEARCH_EXTRA
5151 if (area_attr == 0)
5152 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005153 /* Use attributes from match with highest priority among
5154 * 'search_hl' and the match list. */
5155 char_attr = search_hl.attr;
5156 cur = wp->w_match_head;
5157 shl_flag = FALSE;
5158 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005159 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005160 if (shl_flag == FALSE
5161 && ((cur != NULL
5162 && cur->priority > SEARCH_HL_PRIORITY)
5163 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005164 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005165 shl = &search_hl;
5166 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005167 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005168 else
5169 shl = &cur->hl;
5170 if ((ptr - line) - 1 == (long)shl->startcol)
5171 char_attr = shl->attr;
5172 if (shl != &search_hl && cur != NULL)
5173 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176#endif
5177 ScreenAttrs[off] = char_attr;
5178#ifdef FEAT_RIGHTLEFT
5179 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005182 --off;
5183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 else
5185#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005188 ++off;
5189 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005190 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005191#ifdef FEAT_SYN_HL
5192 eol_hl_off = 1;
5193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
Bram Moolenaar91170f82006-05-05 21:15:17 +00005197 /*
5198 * At end of the text line.
5199 */
5200 if (c == NUL)
5201 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005202#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005203 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5204 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005205 {
5206 /* highlight last char after line */
5207 --col;
5208 --off;
5209 --vcol;
5210 }
5211
Bram Moolenaar1a384422010-07-14 19:53:30 +02005212 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005213 if (wp->w_p_wrap)
5214 v = wp->w_skipcol;
5215 else
5216 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005217
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005218 /* check if line ends before left margin */
5219 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005220 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005221#ifdef FEAT_CONCEAL
5222 /* Get rid of the boguscols now, we want to draw until the right
5223 * edge for 'cursorcolumn'. */
5224 col -= boguscols;
5225 boguscols = 0;
5226#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005227
5228 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005229 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005230
5231 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005232 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5233 && (int)wp->w_virtcol <
5234 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005235 && lnum != wp->w_cursor.lnum)
5236 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005237# ifdef FEAT_RIGHTLEFT
5238 && !wp->w_p_rl
5239# endif
5240 )
5241 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005242 int rightmost_vcol = 0;
5243 int i;
5244
5245 if (wp->w_p_cuc)
5246 rightmost_vcol = wp->w_virtcol;
5247 if (draw_color_col)
5248 /* determine rightmost colorcolumn to possibly draw */
5249 for (i = 0; color_cols[i] >= 0; ++i)
5250 if (rightmost_vcol < color_cols[i])
5251 rightmost_vcol = color_cols[i];
5252
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005253 while (col < W_WIDTH(wp))
5254 {
5255 ScreenLines[off] = ' ';
5256#ifdef FEAT_MBYTE
5257 if (enc_utf8)
5258 ScreenLinesUC[off] = 0;
5259#endif
5260 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005261 if (draw_color_col)
5262 draw_color_col = advance_color_col(VCOL_HLC,
5263 &color_cols);
5264
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005265 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005266 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005267 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005268 ScreenAttrs[off++] = hl_attr(HLF_MC);
5269 else
5270 ScreenAttrs[off++] = 0;
5271
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005272 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005273 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005274
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005275 ++vcol;
5276 }
5277 }
5278#endif
5279
Bram Moolenaar860cae12010-06-05 23:22:07 +02005280 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5281 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 row++;
5283
5284 /*
5285 * Update w_cline_height and w_cline_folded if the cursor line was
5286 * updated (saves a call to plines() later).
5287 */
5288 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5289 {
5290 curwin->w_cline_row = startrow;
5291 curwin->w_cline_height = row - startrow;
5292#ifdef FEAT_FOLDING
5293 curwin->w_cline_folded = FALSE;
5294#endif
5295 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5296 }
5297
5298 break;
5299 }
5300
5301 /* line continues beyond line end */
5302 if (lcs_ext
5303 && !wp->w_p_wrap
5304#ifdef FEAT_DIFF
5305 && filler_todo <= 0
5306#endif
5307 && (
5308#ifdef FEAT_RIGHTLEFT
5309 wp->w_p_rl ? col == 0 :
5310#endif
5311 col == W_WIDTH(wp) - 1)
5312 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005313 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5315 {
5316 c = lcs_ext;
5317 char_attr = hl_attr(HLF_AT);
5318#ifdef FEAT_MBYTE
5319 mb_c = c;
5320 if (enc_utf8 && (*mb_char2len)(c) > 1)
5321 {
5322 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005323 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005324 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
5326 else
5327 mb_utf8 = FALSE;
5328#endif
5329 }
5330
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005331#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005332 /* advance to the next 'colorcolumn' */
5333 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005334 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005335
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005336 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005337 * highlight the cursor position itself.
5338 * Also highlight the 'colorcolumn' if it is different than
5339 * 'cursorcolumn' */
5340 vcol_save_attr = -1;
5341 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005342 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005343 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005344 && lnum != wp->w_cursor.lnum)
5345 {
5346 vcol_save_attr = char_attr;
5347 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5348 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005349 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005350 {
5351 vcol_save_attr = char_attr;
5352 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5353 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005354 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005355#endif
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 /*
5358 * Store character to be displayed.
5359 * Skip characters that are left of the screen for 'nowrap'.
5360 */
5361 vcol_prev = vcol;
5362 if (draw_state < WL_LINE || n_skip <= 0)
5363 {
5364 /*
5365 * Store the character.
5366 */
5367#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5368 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5369 {
5370 /* A double-wide character is: put first halve in left cell. */
5371 --off;
5372 --col;
5373 }
5374#endif
5375 ScreenLines[off] = c;
5376#ifdef FEAT_MBYTE
5377 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005378 {
5379 if ((mb_c & 0xff00) == 0x8e00)
5380 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 else if (enc_utf8)
5384 {
5385 if (mb_utf8)
5386 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005387 int i;
5388
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005390 if ((c & 0xff) == 0)
5391 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005392 for (i = 0; i < Screen_mco; ++i)
5393 {
5394 ScreenLinesC[i][off] = u8cc[i];
5395 if (u8cc[i] == 0)
5396 break;
5397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 else
5400 ScreenLinesUC[off] = 0;
5401 }
5402 if (multi_attr)
5403 {
5404 ScreenAttrs[off] = multi_attr;
5405 multi_attr = 0;
5406 }
5407 else
5408#endif
5409 ScreenAttrs[off] = char_attr;
5410
5411#ifdef FEAT_MBYTE
5412 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5413 {
5414 /* Need to fill two screen columns. */
5415 ++off;
5416 ++col;
5417 if (enc_utf8)
5418 /* UTF-8: Put a 0 in the second screen char. */
5419 ScreenLines[off] = 0;
5420 else
5421 /* DBCS: Put second byte in the second screen char. */
5422 ScreenLines[off] = mb_c & 0xff;
5423 ++vcol;
5424 /* When "tocol" is halfway a character, set it to the end of
5425 * the character, otherwise highlighting won't stop. */
5426 if (tocol == vcol)
5427 ++tocol;
5428#ifdef FEAT_RIGHTLEFT
5429 if (wp->w_p_rl)
5430 {
5431 /* now it's time to backup one cell */
5432 --off;
5433 --col;
5434 }
5435#endif
5436 }
5437#endif
5438#ifdef FEAT_RIGHTLEFT
5439 if (wp->w_p_rl)
5440 {
5441 --off;
5442 --col;
5443 }
5444 else
5445#endif
5446 {
5447 ++off;
5448 ++col;
5449 }
5450 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005451#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005452 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005453 {
5454 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005455 ++vcol_off;
5456 if (n_extra > 0)
5457 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005458 if (wp->w_p_wrap)
5459 {
5460 /*
5461 * Special voodoo required if 'wrap' is on.
5462 *
5463 * Advance the column indicator to force the line
5464 * drawing to wrap early. This will make the line
5465 * take up the same screen space when parts are concealed,
5466 * so that cursor line computations aren't messed up.
5467 *
5468 * To avoid the fictitious advance of 'col' causing
5469 * trailing junk to be written out of the screen line
5470 * we are building, 'boguscols' keeps track of the number
5471 * of bad columns we have advanced.
5472 */
5473 if (n_extra > 0)
5474 {
5475 vcol += n_extra;
5476# ifdef FEAT_RIGHTLEFT
5477 if (wp->w_p_rl)
5478 {
5479 col -= n_extra;
5480 boguscols -= n_extra;
5481 }
5482 else
5483# endif
5484 {
5485 col += n_extra;
5486 boguscols += n_extra;
5487 }
5488 n_extra = 0;
5489 n_attr = 0;
5490 }
5491
5492
5493# ifdef FEAT_MBYTE
5494 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5495 {
5496 /* Need to fill two screen columns. */
5497# ifdef FEAT_RIGHTLEFT
5498 if (wp->w_p_rl)
5499 {
5500 --boguscols;
5501 --col;
5502 }
5503 else
5504# endif
5505 {
5506 ++boguscols;
5507 ++col;
5508 }
5509 }
5510# endif
5511
5512# ifdef FEAT_RIGHTLEFT
5513 if (wp->w_p_rl)
5514 {
5515 --boguscols;
5516 --col;
5517 }
5518 else
5519# endif
5520 {
5521 ++boguscols;
5522 ++col;
5523 }
5524 }
5525 else
5526 {
5527 if (n_extra > 0)
5528 {
5529 vcol += n_extra;
5530 n_extra = 0;
5531 n_attr = 0;
5532 }
5533 }
5534
5535 }
5536#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 else
5538 --n_skip;
5539
Bram Moolenaar64486672010-05-16 15:46:46 +02005540 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5541 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005542 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543#ifdef FEAT_DIFF
5544 && filler_todo <= 0
5545#endif
5546 )
5547 ++vcol;
5548
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005549#ifdef FEAT_SYN_HL
5550 if (vcol_save_attr >= 0)
5551 char_attr = vcol_save_attr;
5552#endif
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 /* restore attributes after "predeces" in 'listchars' */
5555 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5556 char_attr = saved_attr3;
5557
5558 /* restore attributes after last 'listchars' or 'number' char */
5559 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5560 char_attr = saved_attr2;
5561
5562 /*
5563 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005564 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 */
5566 if ((
5567#ifdef FEAT_RIGHTLEFT
5568 wp->w_p_rl ? (col < 0) :
5569#endif
5570 (col >= W_WIDTH(wp)))
5571 && (*ptr != NUL
5572#ifdef FEAT_DIFF
5573 || filler_todo > 0
5574#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005575 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5577 )
5578 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005579#ifdef FEAT_CONCEAL
5580 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5581 (int)W_WIDTH(wp), wp->w_p_rl);
5582 boguscols = 0;
5583#else
5584 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5585 (int)W_WIDTH(wp), wp->w_p_rl);
5586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 ++row;
5588 ++screen_row;
5589
5590 /* When not wrapping and finished diff lines, or when displayed
5591 * '$' and highlighting until last column, break here. */
5592 if ((!wp->w_p_wrap
5593#ifdef FEAT_DIFF
5594 && filler_todo <= 0
5595#endif
5596 ) || lcs_eol_one == -1)
5597 break;
5598
5599 /* When the window is too narrow draw all "@" lines. */
5600 if (draw_state != WL_LINE
5601#ifdef FEAT_DIFF
5602 && filler_todo <= 0
5603#endif
5604 )
5605 {
5606 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5607#ifdef FEAT_VERTSPLIT
5608 draw_vsep_win(wp, row);
5609#endif
5610 row = endrow;
5611 }
5612
5613 /* When line got too long for screen break here. */
5614 if (row == endrow)
5615 {
5616 ++row;
5617 break;
5618 }
5619
5620 if (screen_cur_row == screen_row - 1
5621#ifdef FEAT_DIFF
5622 && filler_todo <= 0
5623#endif
5624 && W_WIDTH(wp) == Columns)
5625 {
5626 /* Remember that the line wraps, used for modeless copy. */
5627 LineWraps[screen_row - 1] = TRUE;
5628
5629 /*
5630 * Special trick to make copy/paste of wrapped lines work with
5631 * xterm/screen: write an extra character beyond the end of
5632 * the line. This will work with all terminal types
5633 * (regardless of the xn,am settings).
5634 * Only do this on a fast tty.
5635 * Only do this if the cursor is on the current line
5636 * (something has been written in it).
5637 * Don't do this for the GUI.
5638 * Don't do this for double-width characters.
5639 * Don't do this for a window not at the right screen border.
5640 */
5641 if (p_tf
5642#ifdef FEAT_GUI
5643 && !gui.in_use
5644#endif
5645#ifdef FEAT_MBYTE
5646 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005647 && ((*mb_off2cells)(LineOffset[screen_row],
5648 LineOffset[screen_row] + screen_Columns)
5649 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005651 + (int)Columns - 2,
5652 LineOffset[screen_row] + screen_Columns)
5653 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654#endif
5655 )
5656 {
5657 /* First make sure we are at the end of the screen line,
5658 * then output the same character again to let the
5659 * terminal know about the wrap. If the terminal doesn't
5660 * auto-wrap, we overwrite the character. */
5661 if (screen_cur_col != W_WIDTH(wp))
5662 screen_char(LineOffset[screen_row - 1]
5663 + (unsigned)Columns - 1,
5664 screen_row - 1, (int)(Columns - 1));
5665
5666#ifdef FEAT_MBYTE
5667 /* When there is a multi-byte character, just output a
5668 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005669 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5670 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 out_char(' ');
5672 else
5673#endif
5674 out_char(ScreenLines[LineOffset[screen_row - 1]
5675 + (Columns - 1)]);
5676 /* force a redraw of the first char on the next line */
5677 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5678 screen_start(); /* don't know where cursor is now */
5679 }
5680 }
5681
5682 col = 0;
5683 off = (unsigned)(current_ScreenLine - ScreenLines);
5684#ifdef FEAT_RIGHTLEFT
5685 if (wp->w_p_rl)
5686 {
5687 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5688 off += col;
5689 }
5690#endif
5691
5692 /* reset the drawing state for the start of a wrapped line */
5693 draw_state = WL_START;
5694 saved_n_extra = n_extra;
5695 saved_p_extra = p_extra;
5696 saved_c_extra = c_extra;
5697 saved_char_attr = char_attr;
5698 n_extra = 0;
5699 lcs_prec_todo = lcs_prec;
5700#ifdef FEAT_LINEBREAK
5701# ifdef FEAT_DIFF
5702 if (filler_todo <= 0)
5703# endif
5704 need_showbreak = TRUE;
5705#endif
5706#ifdef FEAT_DIFF
5707 --filler_todo;
5708 /* When the filler lines are actually below the last line of the
5709 * file, don't draw the line itself, break here. */
5710 if (filler_todo == 0 && wp->w_botfill)
5711 break;
5712#endif
5713 }
5714
5715 } /* for every character in the line */
5716
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005717#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005718 /* After an empty line check first word for capital. */
5719 if (*skipwhite(line) == NUL)
5720 {
5721 capcol_lnum = lnum + 1;
5722 cap_col = 0;
5723 }
5724#endif
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 return row;
5727}
5728
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005729#ifdef FEAT_MBYTE
5730static int comp_char_differs __ARGS((int, int));
5731
5732/*
5733 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005734 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005735 */
5736 static int
5737comp_char_differs(off_from, off_to)
5738 int off_from;
5739 int off_to;
5740{
5741 int i;
5742
5743 for (i = 0; i < Screen_mco; ++i)
5744 {
5745 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5746 return TRUE;
5747 if (ScreenLinesC[i][off_from] == 0)
5748 break;
5749 }
5750 return FALSE;
5751}
5752#endif
5753
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754/*
5755 * Check whether the given character needs redrawing:
5756 * - the (first byte of the) character is different
5757 * - the attributes are different
5758 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005759 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 */
5761 static int
5762char_needs_redraw(off_from, off_to, cols)
5763 int off_from;
5764 int off_to;
5765 int cols;
5766{
5767 if (cols > 0
5768 && ((ScreenLines[off_from] != ScreenLines[off_to]
5769 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5770
5771#ifdef FEAT_MBYTE
5772 || (enc_dbcs != 0
5773 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5774 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5775 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5776 : (cols > 1 && ScreenLines[off_from + 1]
5777 != ScreenLines[off_to + 1])))
5778 || (enc_utf8
5779 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5780 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005781 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005782 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5783 && ScreenLines[off_from + 1]
5784 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#endif
5786 ))
5787 return TRUE;
5788 return FALSE;
5789}
5790
5791/*
5792 * Move one "cooked" screen line to the screen, but only the characters that
5793 * have actually changed. Handle insert/delete character.
5794 * "coloff" gives the first column on the screen for this line.
5795 * "endcol" gives the columns where valid characters are.
5796 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5797 * needs to be cleared, negative otherwise.
5798 * "rlflag" is TRUE in a rightleft window:
5799 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5800 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5801 */
5802 static void
5803screen_line(row, coloff, endcol, clear_width
5804#ifdef FEAT_RIGHTLEFT
5805 , rlflag
5806#endif
5807 )
5808 int row;
5809 int coloff;
5810 int endcol;
5811 int clear_width;
5812#ifdef FEAT_RIGHTLEFT
5813 int rlflag;
5814#endif
5815{
5816 unsigned off_from;
5817 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005818#ifdef FEAT_MBYTE
5819 unsigned max_off_from;
5820 unsigned max_off_to;
5821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 int col = 0;
5823#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5824 int hl;
5825#endif
5826 int force = FALSE; /* force update rest of the line */
5827 int redraw_this /* bool: does character need redraw? */
5828#ifdef FEAT_GUI
5829 = TRUE /* For GUI when while-loop empty */
5830#endif
5831 ;
5832 int redraw_next; /* redraw_this for next character */
5833#ifdef FEAT_MBYTE
5834 int clear_next = FALSE;
5835 int char_cells; /* 1: normal char */
5836 /* 2: occupies two display cells */
5837# define CHAR_CELLS char_cells
5838#else
5839# define CHAR_CELLS 1
5840#endif
5841
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005842 /* Check for illegal row and col, just in case. */
5843 if (row >= Rows)
5844 row = Rows - 1;
5845 if (endcol > Columns)
5846 endcol = Columns;
5847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848# ifdef FEAT_CLIPBOARD
5849 clip_may_clear_selection(row, row);
5850# endif
5851
5852 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5853 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005854#ifdef FEAT_MBYTE
5855 max_off_from = off_from + screen_Columns;
5856 max_off_to = LineOffset[row] + screen_Columns;
5857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
5859#ifdef FEAT_RIGHTLEFT
5860 if (rlflag)
5861 {
5862 /* Clear rest first, because it's left of the text. */
5863 if (clear_width > 0)
5864 {
5865 while (col <= endcol && ScreenLines[off_to] == ' '
5866 && ScreenAttrs[off_to] == 0
5867# ifdef FEAT_MBYTE
5868 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5869# endif
5870 )
5871 {
5872 ++off_to;
5873 ++col;
5874 }
5875 if (col <= endcol)
5876 screen_fill(row, row + 1, col + coloff,
5877 endcol + coloff + 1, ' ', ' ', 0);
5878 }
5879 col = endcol + 1;
5880 off_to = LineOffset[row] + col + coloff;
5881 off_from += col;
5882 endcol = (clear_width > 0 ? clear_width : -clear_width);
5883 }
5884#endif /* FEAT_RIGHTLEFT */
5885
5886 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5887
5888 while (col < endcol)
5889 {
5890#ifdef FEAT_MBYTE
5891 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005892 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 else
5894 char_cells = 1;
5895#endif
5896
5897 redraw_this = redraw_next;
5898 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5899 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5900
5901#ifdef FEAT_GUI
5902 /* If the next character was bold, then redraw the current character to
5903 * remove any pixels that might have spilt over into us. This only
5904 * happens in the GUI.
5905 */
5906 if (redraw_next && gui.in_use)
5907 {
5908 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005909 if (hl > HL_ALL)
5910 hl = syn_attr2attr(hl);
5911 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 redraw_this = TRUE;
5913 }
5914#endif
5915
5916 if (redraw_this)
5917 {
5918 /*
5919 * Special handling when 'xs' termcap flag set (hpterm):
5920 * Attributes for characters are stored at the position where the
5921 * cursor is when writing the highlighting code. The
5922 * start-highlighting code must be written with the cursor on the
5923 * first highlighted character. The stop-highlighting code must
5924 * be written with the cursor just after the last highlighted
5925 * character.
5926 * Overwriting a character doesn't remove it's highlighting. Need
5927 * to clear the rest of the line, and force redrawing it
5928 * completely.
5929 */
5930 if ( p_wiv
5931 && !force
5932#ifdef FEAT_GUI
5933 && !gui.in_use
5934#endif
5935 && ScreenAttrs[off_to] != 0
5936 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5937 {
5938 /*
5939 * Need to remove highlighting attributes here.
5940 */
5941 windgoto(row, col + coloff);
5942 out_str(T_CE); /* clear rest of this screen line */
5943 screen_start(); /* don't know where cursor is now */
5944 force = TRUE; /* force redraw of rest of the line */
5945 redraw_next = TRUE; /* or else next char would miss out */
5946
5947 /*
5948 * If the previous character was highlighted, need to stop
5949 * highlighting at this character.
5950 */
5951 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5952 {
5953 screen_attr = ScreenAttrs[off_to - 1];
5954 term_windgoto(row, col + coloff);
5955 screen_stop_highlight();
5956 }
5957 else
5958 screen_attr = 0; /* highlighting has stopped */
5959 }
5960#ifdef FEAT_MBYTE
5961 if (enc_dbcs != 0)
5962 {
5963 /* Check if overwriting a double-byte with a single-byte or
5964 * the other way around requires another character to be
5965 * redrawn. For UTF-8 this isn't needed, because comparing
5966 * ScreenLinesUC[] is sufficient. */
5967 if (char_cells == 1
5968 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005969 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 {
5971 /* Writing a single-cell character over a double-cell
5972 * character: need to redraw the next cell. */
5973 ScreenLines[off_to + 1] = 0;
5974 redraw_next = TRUE;
5975 }
5976 else if (char_cells == 2
5977 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005978 && (*mb_off2cells)(off_to, max_off_to) == 1
5979 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
5981 /* Writing the second half of a double-cell character over
5982 * a double-cell character: need to redraw the second
5983 * cell. */
5984 ScreenLines[off_to + 2] = 0;
5985 redraw_next = TRUE;
5986 }
5987
5988 if (enc_dbcs == DBCS_JPNU)
5989 ScreenLines2[off_to] = ScreenLines2[off_from];
5990 }
5991 /* When writing a single-width character over a double-width
5992 * character and at the end of the redrawn text, need to clear out
5993 * the right halve of the old character.
5994 * Also required when writing the right halve of a double-width
5995 * char over the left halve of an existing one. */
5996 if (has_mbyte && col + char_cells == endcol
5997 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005998 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006000 && (*mb_off2cells)(off_to, max_off_to) == 1
6001 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 clear_next = TRUE;
6003#endif
6004
6005 ScreenLines[off_to] = ScreenLines[off_from];
6006#ifdef FEAT_MBYTE
6007 if (enc_utf8)
6008 {
6009 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6010 if (ScreenLinesUC[off_from] != 0)
6011 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006012 int i;
6013
6014 for (i = 0; i < Screen_mco; ++i)
6015 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 }
6017 }
6018 if (char_cells == 2)
6019 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6020#endif
6021
6022#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006023 /* The bold trick makes a single column of pixels appear in the
6024 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 * character should be redrawn too. This happens for our own GUI
6026 * and for some xterms. */
6027 if (
6028# ifdef FEAT_GUI
6029 gui.in_use
6030# endif
6031# if defined(FEAT_GUI) && defined(UNIX)
6032 ||
6033# endif
6034# ifdef UNIX
6035 term_is_xterm
6036# endif
6037 )
6038 {
6039 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006040 if (hl > HL_ALL)
6041 hl = syn_attr2attr(hl);
6042 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 redraw_next = TRUE;
6044 }
6045#endif
6046 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6047#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006048 /* For simplicity set the attributes of second half of a
6049 * double-wide character equal to the first half. */
6050 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006052
6053 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 else
6056#endif
6057 screen_char(off_to, row, col + coloff);
6058 }
6059 else if ( p_wiv
6060#ifdef FEAT_GUI
6061 && !gui.in_use
6062#endif
6063 && col + coloff > 0)
6064 {
6065 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6066 {
6067 /*
6068 * Don't output stop-highlight when moving the cursor, it will
6069 * stop the highlighting when it should continue.
6070 */
6071 screen_attr = 0;
6072 }
6073 else if (screen_attr != 0)
6074 screen_stop_highlight();
6075 }
6076
6077 off_to += CHAR_CELLS;
6078 off_from += CHAR_CELLS;
6079 col += CHAR_CELLS;
6080 }
6081
6082#ifdef FEAT_MBYTE
6083 if (clear_next)
6084 {
6085 /* Clear the second half of a double-wide character of which the left
6086 * half was overwritten with a single-wide character. */
6087 ScreenLines[off_to] = ' ';
6088 if (enc_utf8)
6089 ScreenLinesUC[off_to] = 0;
6090 screen_char(off_to, row, col + coloff);
6091 }
6092#endif
6093
6094 if (clear_width > 0
6095#ifdef FEAT_RIGHTLEFT
6096 && !rlflag
6097#endif
6098 )
6099 {
6100#ifdef FEAT_GUI
6101 int startCol = col;
6102#endif
6103
6104 /* blank out the rest of the line */
6105 while (col < clear_width && ScreenLines[off_to] == ' '
6106 && ScreenAttrs[off_to] == 0
6107#ifdef FEAT_MBYTE
6108 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6109#endif
6110 )
6111 {
6112 ++off_to;
6113 ++col;
6114 }
6115 if (col < clear_width)
6116 {
6117#ifdef FEAT_GUI
6118 /*
6119 * In the GUI, clearing the rest of the line may leave pixels
6120 * behind if the first character cleared was bold. Some bold
6121 * fonts spill over the left. In this case we redraw the previous
6122 * character too. If we didn't skip any blanks above, then we
6123 * only redraw if the character wasn't already redrawn anyway.
6124 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006125 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
6127 hl = ScreenAttrs[off_to];
6128 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006129 {
6130 int prev_cells = 1;
6131# ifdef FEAT_MBYTE
6132 if (enc_utf8)
6133 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6134 * that its width is 2. */
6135 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6136 else if (enc_dbcs != 0)
6137 {
6138 /* find previous character by counting from first
6139 * column and get its width. */
6140 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006141 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006142
6143 while (off < off_to)
6144 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006145 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006146 off += prev_cells;
6147 }
6148 }
6149
6150 if (enc_dbcs != 0 && prev_cells > 1)
6151 screen_char_2(off_to - prev_cells, row,
6152 col + coloff - prev_cells);
6153 else
6154# endif
6155 screen_char(off_to - prev_cells, row,
6156 col + coloff - prev_cells);
6157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
6159#endif
6160 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6161 ' ', ' ', 0);
6162#ifdef FEAT_VERTSPLIT
6163 off_to += clear_width - col;
6164 col = clear_width;
6165#endif
6166 }
6167 }
6168
6169 if (clear_width > 0)
6170 {
6171#ifdef FEAT_VERTSPLIT
6172 /* For a window that's left of another, draw the separator char. */
6173 if (col + coloff < Columns)
6174 {
6175 int c;
6176
6177 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006178 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006180 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6181 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182# endif
6183 || ScreenAttrs[off_to] != hl)
6184 {
6185 ScreenLines[off_to] = c;
6186 ScreenAttrs[off_to] = hl;
6187# ifdef FEAT_MBYTE
6188 if (enc_utf8)
6189 {
6190 if (c >= 0x80)
6191 {
6192 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006193 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 }
6195 else
6196 ScreenLinesUC[off_to] = 0;
6197 }
6198# endif
6199 screen_char(off_to, row, col + coloff);
6200 }
6201 }
6202 else
6203#endif
6204 LineWraps[row] = FALSE;
6205 }
6206}
6207
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006208#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006210 * Mirror text "str" for right-left displaying.
6211 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006213 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214rl_mirror(str)
6215 char_u *str;
6216{
6217 char_u *p1, *p2;
6218 int t;
6219
6220 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6221 {
6222 t = *p1;
6223 *p1 = *p2;
6224 *p2 = t;
6225 }
6226}
6227#endif
6228
6229#if defined(FEAT_WINDOWS) || defined(PROTO)
6230/*
6231 * mark all status lines for redraw; used after first :cd
6232 */
6233 void
6234status_redraw_all()
6235{
6236 win_T *wp;
6237
6238 for (wp = firstwin; wp; wp = wp->w_next)
6239 if (wp->w_status_height)
6240 {
6241 wp->w_redr_status = TRUE;
6242 redraw_later(VALID);
6243 }
6244}
6245
6246/*
6247 * mark all status lines of the current buffer for redraw
6248 */
6249 void
6250status_redraw_curbuf()
6251{
6252 win_T *wp;
6253
6254 for (wp = firstwin; wp; wp = wp->w_next)
6255 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6256 {
6257 wp->w_redr_status = TRUE;
6258 redraw_later(VALID);
6259 }
6260}
6261
6262/*
6263 * Redraw all status lines that need to be redrawn.
6264 */
6265 void
6266redraw_statuslines()
6267{
6268 win_T *wp;
6269
6270 for (wp = firstwin; wp; wp = wp->w_next)
6271 if (wp->w_redr_status)
6272 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006273 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006274 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275}
6276#endif
6277
6278#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6279/*
6280 * Redraw all status lines at the bottom of frame "frp".
6281 */
6282 void
6283win_redraw_last_status(frp)
6284 frame_T *frp;
6285{
6286 if (frp->fr_layout == FR_LEAF)
6287 frp->fr_win->w_redr_status = TRUE;
6288 else if (frp->fr_layout == FR_ROW)
6289 {
6290 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6291 win_redraw_last_status(frp);
6292 }
6293 else /* frp->fr_layout == FR_COL */
6294 {
6295 frp = frp->fr_child;
6296 while (frp->fr_next != NULL)
6297 frp = frp->fr_next;
6298 win_redraw_last_status(frp);
6299 }
6300}
6301#endif
6302
6303#ifdef FEAT_VERTSPLIT
6304/*
6305 * Draw the verticap separator right of window "wp" starting with line "row".
6306 */
6307 static void
6308draw_vsep_win(wp, row)
6309 win_T *wp;
6310 int row;
6311{
6312 int hl;
6313 int c;
6314
6315 if (wp->w_vsep_width)
6316 {
6317 /* draw the vertical separator right of this window */
6318 c = fillchar_vsep(&hl);
6319 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6320 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6321 c, ' ', hl);
6322 }
6323}
6324#endif
6325
6326#ifdef FEAT_WILDMENU
6327static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006328static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
6330/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006331 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 */
6333 static int
6334status_match_len(xp, s)
6335 expand_T *xp;
6336 char_u *s;
6337{
6338 int len = 0;
6339
6340#ifdef FEAT_MENU
6341 int emenu = (xp->xp_context == EXPAND_MENUS
6342 || xp->xp_context == EXPAND_MENUNAMES);
6343
6344 /* Check for menu separators - replace with '|'. */
6345 if (emenu && menu_is_separator(s))
6346 return 1;
6347#endif
6348
6349 while (*s != NUL)
6350 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006351 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006352 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006353 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 }
6355
6356 return len;
6357}
6358
6359/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006360 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006361 * These are backslashes used for escaping. Do show backslashes in help tags.
6362 */
6363 static int
6364skip_status_match_char(xp, s)
6365 expand_T *xp;
6366 char_u *s;
6367{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006368 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006369#ifdef FEAT_MENU
6370 || ((xp->xp_context == EXPAND_MENUS
6371 || xp->xp_context == EXPAND_MENUNAMES)
6372 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6373#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006374 )
6375 {
6376#ifndef BACKSLASH_IN_FILENAME
6377 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6378 return 2;
6379#endif
6380 return 1;
6381 }
6382 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006383}
6384
6385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 * Show wildchar matches in the status line.
6387 * Show at least the "match" item.
6388 * We start at item 'first_match' in the list and show all matches that fit.
6389 *
6390 * If inversion is possible we use it. Else '=' characters are used.
6391 */
6392 void
6393win_redr_status_matches(xp, num_matches, matches, match, showtail)
6394 expand_T *xp;
6395 int num_matches;
6396 char_u **matches; /* list of matches */
6397 int match;
6398 int showtail;
6399{
6400#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6401 int row;
6402 char_u *buf;
6403 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006404 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 int fillchar;
6406 int attr;
6407 int i;
6408 int highlight = TRUE;
6409 char_u *selstart = NULL;
6410 int selstart_col = 0;
6411 char_u *selend = NULL;
6412 static int first_match = 0;
6413 int add_left = FALSE;
6414 char_u *s;
6415#ifdef FEAT_MENU
6416 int emenu;
6417#endif
6418#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6419 int l;
6420#endif
6421
6422 if (matches == NULL) /* interrupted completion? */
6423 return;
6424
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006425#ifdef FEAT_MBYTE
6426 if (has_mbyte)
6427 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6428 else
6429#endif
6430 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (buf == NULL)
6432 return;
6433
6434 if (match == -1) /* don't show match but original text */
6435 {
6436 match = 0;
6437 highlight = FALSE;
6438 }
6439 /* count 1 for the ending ">" */
6440 clen = status_match_len(xp, L_MATCH(match)) + 3;
6441 if (match == 0)
6442 first_match = 0;
6443 else if (match < first_match)
6444 {
6445 /* jumping left, as far as we can go */
6446 first_match = match;
6447 add_left = TRUE;
6448 }
6449 else
6450 {
6451 /* check if match fits on the screen */
6452 for (i = first_match; i < match; ++i)
6453 clen += status_match_len(xp, L_MATCH(i)) + 2;
6454 if (first_match > 0)
6455 clen += 2;
6456 /* jumping right, put match at the left */
6457 if ((long)clen > Columns)
6458 {
6459 first_match = match;
6460 /* if showing the last match, we can add some on the left */
6461 clen = 2;
6462 for (i = match; i < num_matches; ++i)
6463 {
6464 clen += status_match_len(xp, L_MATCH(i)) + 2;
6465 if ((long)clen >= Columns)
6466 break;
6467 }
6468 if (i == num_matches)
6469 add_left = TRUE;
6470 }
6471 }
6472 if (add_left)
6473 while (first_match > 0)
6474 {
6475 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6476 if ((long)clen >= Columns)
6477 break;
6478 --first_match;
6479 }
6480
6481 fillchar = fillchar_status(&attr, TRUE);
6482
6483 if (first_match == 0)
6484 {
6485 *buf = NUL;
6486 len = 0;
6487 }
6488 else
6489 {
6490 STRCPY(buf, "< ");
6491 len = 2;
6492 }
6493 clen = len;
6494
6495 i = first_match;
6496 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6497 {
6498 if (i == match)
6499 {
6500 selstart = buf + len;
6501 selstart_col = clen;
6502 }
6503
6504 s = L_MATCH(i);
6505 /* Check for menu separators - replace with '|' */
6506#ifdef FEAT_MENU
6507 emenu = (xp->xp_context == EXPAND_MENUS
6508 || xp->xp_context == EXPAND_MENUNAMES);
6509 if (emenu && menu_is_separator(s))
6510 {
6511 STRCPY(buf + len, transchar('|'));
6512 l = (int)STRLEN(buf + len);
6513 len += l;
6514 clen += l;
6515 }
6516 else
6517#endif
6518 for ( ; *s != NUL; ++s)
6519 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006520 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 clen += ptr2cells(s);
6522#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006523 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
6525 STRNCPY(buf + len, s, l);
6526 s += l - 1;
6527 len += l;
6528 }
6529 else
6530#endif
6531 {
6532 STRCPY(buf + len, transchar_byte(*s));
6533 len += (int)STRLEN(buf + len);
6534 }
6535 }
6536 if (i == match)
6537 selend = buf + len;
6538
6539 *(buf + len++) = ' ';
6540 *(buf + len++) = ' ';
6541 clen += 2;
6542 if (++i == num_matches)
6543 break;
6544 }
6545
6546 if (i != num_matches)
6547 {
6548 *(buf + len++) = '>';
6549 ++clen;
6550 }
6551
6552 buf[len] = NUL;
6553
6554 row = cmdline_row - 1;
6555 if (row >= 0)
6556 {
6557 if (wild_menu_showing == 0)
6558 {
6559 if (msg_scrolled > 0)
6560 {
6561 /* Put the wildmenu just above the command line. If there is
6562 * no room, scroll the screen one line up. */
6563 if (cmdline_row == Rows - 1)
6564 {
6565 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6566 ++msg_scrolled;
6567 }
6568 else
6569 {
6570 ++cmdline_row;
6571 ++row;
6572 }
6573 wild_menu_showing = WM_SCROLLED;
6574 }
6575 else
6576 {
6577 /* Create status line if needed by setting 'laststatus' to 2.
6578 * Set 'winminheight' to zero to avoid that the window is
6579 * resized. */
6580 if (lastwin->w_status_height == 0)
6581 {
6582 save_p_ls = p_ls;
6583 save_p_wmh = p_wmh;
6584 p_ls = 2;
6585 p_wmh = 0;
6586 last_status(FALSE);
6587 }
6588 wild_menu_showing = WM_SHOWN;
6589 }
6590 }
6591
6592 screen_puts(buf, row, 0, attr);
6593 if (selstart != NULL && highlight)
6594 {
6595 *selend = NUL;
6596 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6597 }
6598
6599 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6600 }
6601
6602#ifdef FEAT_VERTSPLIT
6603 win_redraw_last_status(topframe);
6604#else
6605 lastwin->w_redr_status = TRUE;
6606#endif
6607 vim_free(buf);
6608}
6609#endif
6610
6611#if defined(FEAT_WINDOWS) || defined(PROTO)
6612/*
6613 * Redraw the status line of window wp.
6614 *
6615 * If inversion is possible we use it. Else '=' characters are used.
6616 */
6617 void
6618win_redr_status(wp)
6619 win_T *wp;
6620{
6621 int row;
6622 char_u *p;
6623 int len;
6624 int fillchar;
6625 int attr;
6626 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006627 static int busy = FALSE;
6628
6629 /* It's possible to get here recursively when 'statusline' (indirectly)
6630 * invokes ":redrawstatus". Simply ignore the call then. */
6631 if (busy)
6632 return;
6633 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635 wp->w_redr_status = FALSE;
6636 if (wp->w_status_height == 0)
6637 {
6638 /* no status line, can only be last window */
6639 redraw_cmdline = TRUE;
6640 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006641 else if (!redrawing()
6642#ifdef FEAT_INS_EXPAND
6643 /* don't update status line when popup menu is visible and may be
6644 * drawn over it */
6645 || pum_visible()
6646#endif
6647 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
6649 /* Don't redraw right now, do it later. */
6650 wp->w_redr_status = TRUE;
6651 }
6652#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006653 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
6655 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006656 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 }
6658#endif
6659 else
6660 {
6661 fillchar = fillchar_status(&attr, wp == curwin);
6662
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006663 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 p = NameBuff;
6665 len = (int)STRLEN(p);
6666
6667 if (wp->w_buffer->b_help
6668#ifdef FEAT_QUICKFIX
6669 || wp->w_p_pvw
6670#endif
6671 || bufIsChanged(wp->w_buffer)
6672 || wp->w_buffer->b_p_ro)
6673 *(p + len++) = ' ';
6674 if (wp->w_buffer->b_help)
6675 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006676 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 len += (int)STRLEN(p + len);
6678 }
6679#ifdef FEAT_QUICKFIX
6680 if (wp->w_p_pvw)
6681 {
6682 STRCPY(p + len, _("[Preview]"));
6683 len += (int)STRLEN(p + len);
6684 }
6685#endif
6686 if (bufIsChanged(wp->w_buffer))
6687 {
6688 STRCPY(p + len, "[+]");
6689 len += 3;
6690 }
6691 if (wp->w_buffer->b_p_ro)
6692 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006693 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 len += 4;
6695 }
6696
6697#ifndef FEAT_VERTSPLIT
6698 this_ru_col = ru_col;
6699 if (this_ru_col < (Columns + 1) / 2)
6700 this_ru_col = (Columns + 1) / 2;
6701#else
6702 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6703 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6704 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6705 if (this_ru_col <= 1)
6706 {
6707 p = (char_u *)"<"; /* No room for file name! */
6708 len = 1;
6709 }
6710 else
6711#endif
6712#ifdef FEAT_MBYTE
6713 if (has_mbyte)
6714 {
6715 int clen = 0, i;
6716
6717 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006718 clen = mb_string2cells(p, -1);
6719
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 /* Find first character that will fit.
6721 * Going from start to end is much faster for DBCS. */
6722 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006723 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 clen -= (*mb_ptr2cells)(p + i);
6725 len = clen;
6726 if (i > 0)
6727 {
6728 p = p + i - 1;
6729 *p = '<';
6730 ++len;
6731 }
6732
6733 }
6734 else
6735#endif
6736 if (len > this_ru_col - 1)
6737 {
6738 p += len - (this_ru_col - 1);
6739 *p = '<';
6740 len = this_ru_col - 1;
6741 }
6742
6743 row = W_WINROW(wp) + wp->w_height;
6744 screen_puts(p, row, W_WINCOL(wp), attr);
6745 screen_fill(row, row + 1, len + W_WINCOL(wp),
6746 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6747
6748 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6749 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6750 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6751 - 1 + W_WINCOL(wp)), attr);
6752
6753#ifdef FEAT_CMDL_INFO
6754 win_redr_ruler(wp, TRUE);
6755#endif
6756 }
6757
6758#ifdef FEAT_VERTSPLIT
6759 /*
6760 * May need to draw the character below the vertical separator.
6761 */
6762 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6763 {
6764 if (stl_connected(wp))
6765 fillchar = fillchar_status(&attr, wp == curwin);
6766 else
6767 fillchar = fillchar_vsep(&attr);
6768 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6769 attr);
6770 }
6771#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006772 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773}
6774
Bram Moolenaar238a5642006-02-21 22:12:05 +00006775#ifdef FEAT_STL_OPT
6776/*
6777 * Redraw the status line according to 'statusline' and take care of any
6778 * errors encountered.
6779 */
6780 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006781redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006782 win_T *wp;
6783{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006784 static int entered = FALSE;
6785 int save_called_emsg = called_emsg;
6786
6787 /* When called recursively return. This can happen when the statusline
6788 * contains an expression that triggers a redraw. */
6789 if (entered)
6790 return;
6791 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006792
6793 called_emsg = FALSE;
6794 win_redr_custom(wp, FALSE);
6795 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006796 {
6797 /* When there is an error disable the statusline, otherwise the
6798 * display is messed up with errors and a redraw triggers the problem
6799 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006800 set_string_option_direct((char_u *)"statusline", -1,
6801 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006802 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006803 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006804 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006805 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006806}
6807#endif
6808
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809# ifdef FEAT_VERTSPLIT
6810/*
6811 * Return TRUE if the status line of window "wp" is connected to the status
6812 * line of the window right of it. If not, then it's a vertical separator.
6813 * Only call if (wp->w_vsep_width != 0).
6814 */
6815 int
6816stl_connected(wp)
6817 win_T *wp;
6818{
6819 frame_T *fr;
6820
6821 fr = wp->w_frame;
6822 while (fr->fr_parent != NULL)
6823 {
6824 if (fr->fr_parent->fr_layout == FR_COL)
6825 {
6826 if (fr->fr_next != NULL)
6827 break;
6828 }
6829 else
6830 {
6831 if (fr->fr_next != NULL)
6832 return TRUE;
6833 }
6834 fr = fr->fr_parent;
6835 }
6836 return FALSE;
6837}
6838# endif
6839
6840#endif /* FEAT_WINDOWS */
6841
6842#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6843/*
6844 * Get the value to show for the language mappings, active 'keymap'.
6845 */
6846 int
6847get_keymap_str(wp, buf, len)
6848 win_T *wp;
6849 char_u *buf; /* buffer for the result */
6850 int len; /* length of buffer */
6851{
6852 char_u *p;
6853
6854 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6855 return FALSE;
6856
6857 {
6858#ifdef FEAT_EVAL
6859 buf_T *old_curbuf = curbuf;
6860 win_T *old_curwin = curwin;
6861 char_u *s;
6862
6863 curbuf = wp->w_buffer;
6864 curwin = wp;
6865 STRCPY(buf, "b:keymap_name"); /* must be writable */
6866 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006867 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 --emsg_skip;
6869 curbuf = old_curbuf;
6870 curwin = old_curwin;
6871 if (p == NULL || *p == NUL)
6872#endif
6873 {
6874#ifdef FEAT_KEYMAP
6875 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6876 p = wp->w_buffer->b_p_keymap;
6877 else
6878#endif
6879 p = (char_u *)"lang";
6880 }
6881 if ((int)(STRLEN(p) + 3) < len)
6882 sprintf((char *)buf, "<%s>", p);
6883 else
6884 buf[0] = NUL;
6885#ifdef FEAT_EVAL
6886 vim_free(s);
6887#endif
6888 }
6889 return buf[0] != NUL;
6890}
6891#endif
6892
6893#if defined(FEAT_STL_OPT) || defined(PROTO)
6894/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006895 * Redraw the status line or ruler of window "wp".
6896 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 */
6898 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006899win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006901 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006903 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 int attr;
6905 int curattr;
6906 int row;
6907 int col = 0;
6908 int maxwidth;
6909 int width;
6910 int n;
6911 int len;
6912 int fillchar;
6913 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006914 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006916 struct stl_hlrec hltab[STL_MAX_ITEM];
6917 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006918 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006919 win_T *ewp;
6920 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
Bram Moolenaar1d633412013-12-11 15:52:01 +01006922 /* There is a tiny chance that this gets called recursively: When
6923 * redrawing a status line triggers redrawing the ruler or tabline.
6924 * Avoid trouble by not allowing recursion. */
6925 if (entered)
6926 return;
6927 entered = TRUE;
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006930 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006932 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006933 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006934 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006935 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006936 attr = hl_attr(HLF_TPF);
6937 maxwidth = Columns;
6938# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006939 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006942 else
6943 {
6944 row = W_WINROW(wp) + wp->w_height;
6945 fillchar = fillchar_status(&attr, wp == curwin);
6946 maxwidth = W_WIDTH(wp);
6947
6948 if (draw_ruler)
6949 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006950 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006951 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006952 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006953 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006954 if (*++stl == '-')
6955 stl++;
6956 if (atoi((char *)stl))
6957 while (VIM_ISDIGIT(*stl))
6958 stl++;
6959 if (*stl++ != '(')
6960 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006961 }
6962#ifdef FEAT_VERTSPLIT
6963 col = ru_col - (Columns - W_WIDTH(wp));
6964 if (col < (W_WIDTH(wp) + 1) / 2)
6965 col = (W_WIDTH(wp) + 1) / 2;
6966#else
6967 col = ru_col;
6968 if (col > (Columns + 1) / 2)
6969 col = (Columns + 1) / 2;
6970#endif
6971 maxwidth = W_WIDTH(wp) - col;
6972#ifdef FEAT_WINDOWS
6973 if (!wp->w_status_height)
6974#endif
6975 {
6976 row = Rows - 1;
6977 --maxwidth; /* writing in last column may cause scrolling */
6978 fillchar = ' ';
6979 attr = 0;
6980 }
6981
6982# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006983 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006984# endif
6985 }
6986 else
6987 {
6988 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006989 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006990 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006991 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006992# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006993 use_sandbox = was_set_insecurely((char_u *)"statusline",
6994 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006995# endif
6996 }
6997
6998#ifdef FEAT_VERTSPLIT
6999 col += W_WINCOL(wp);
7000#endif
7001 }
7002
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007004 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
Bram Moolenaar61452852011-02-01 18:01:11 +01007006 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7007 * the cursor away and back. */
7008 ewp = wp == NULL ? curwin : wp;
7009 p_crb_save = ewp->w_p_crb;
7010 ewp->w_p_crb = FALSE;
7011
Bram Moolenaar362f3562009-11-03 16:20:34 +00007012 /* Make a copy, because the statusline may include a function call that
7013 * might change the option value and free the memory. */
7014 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007015 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007016 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007017 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007018 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007019 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007021 /* Make all characters printable. */
7022 p = transstr(buf);
7023 if (p != NULL)
7024 {
7025 vim_strncpy(buf, p, sizeof(buf) - 1);
7026 vim_free(p);
7027 }
7028
7029 /* fill up with "fillchar" */
7030 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007031 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {
7033#ifdef FEAT_MBYTE
7034 len += (*mb_char2bytes)(fillchar, buf + len);
7035#else
7036 buf[len++] = fillchar;
7037#endif
7038 ++width;
7039 }
7040 buf[len] = NUL;
7041
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007042 /*
7043 * Draw each snippet with the specified highlighting.
7044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 curattr = attr;
7046 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007047 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007049 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 screen_puts_len(p, len, row, col, curattr);
7051 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007052 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007054 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007056 else if (hltab[n].userhl < 0)
7057 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007059 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007060 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061#endif
7062 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007063 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007066
7067 if (wp == NULL)
7068 {
7069 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7070 col = 0;
7071 len = 0;
7072 p = buf;
7073 fillchar = 0;
7074 for (n = 0; tabtab[n].start != NULL; n++)
7075 {
7076 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7077 while (col < len)
7078 TabPageIdxs[col++] = fillchar;
7079 p = tabtab[n].start;
7080 fillchar = tabtab[n].userhl;
7081 }
7082 while (col < Columns)
7083 TabPageIdxs[col++] = fillchar;
7084 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007085
7086theend:
7087 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088}
7089
7090#endif /* FEAT_STL_OPT */
7091
7092/*
7093 * Output a single character directly to the screen and update ScreenLines.
7094 */
7095 void
7096screen_putchar(c, row, col, attr)
7097 int c;
7098 int row, col;
7099 int attr;
7100{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 char_u buf[MB_MAXBYTES + 1];
7102
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007103#ifdef FEAT_MBYTE
7104 if (has_mbyte)
7105 buf[(*mb_char2bytes)(c, buf)] = NUL;
7106 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007108 {
7109 buf[0] = c;
7110 buf[1] = NUL;
7111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 screen_puts(buf, row, col, attr);
7113}
7114
7115/*
7116 * Get a single character directly from ScreenLines into "bytes[]".
7117 * Also return its attribute in *attrp;
7118 */
7119 void
7120screen_getbytes(row, col, bytes, attrp)
7121 int row, col;
7122 char_u *bytes;
7123 int *attrp;
7124{
7125 unsigned off;
7126
7127 /* safety check */
7128 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7129 {
7130 off = LineOffset[row] + col;
7131 *attrp = ScreenAttrs[off];
7132 bytes[0] = ScreenLines[off];
7133 bytes[1] = NUL;
7134
7135#ifdef FEAT_MBYTE
7136 if (enc_utf8 && ScreenLinesUC[off] != 0)
7137 bytes[utfc_char2bytes(off, bytes)] = NUL;
7138 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7139 {
7140 bytes[0] = ScreenLines[off];
7141 bytes[1] = ScreenLines2[off];
7142 bytes[2] = NUL;
7143 }
7144 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7145 {
7146 bytes[1] = ScreenLines[off + 1];
7147 bytes[2] = NUL;
7148 }
7149#endif
7150 }
7151}
7152
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007153#ifdef FEAT_MBYTE
7154static int screen_comp_differs __ARGS((int, int*));
7155
7156/*
7157 * Return TRUE if composing characters for screen posn "off" differs from
7158 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007159 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007160 */
7161 static int
7162screen_comp_differs(off, u8cc)
7163 int off;
7164 int *u8cc;
7165{
7166 int i;
7167
7168 for (i = 0; i < Screen_mco; ++i)
7169 {
7170 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7171 return TRUE;
7172 if (u8cc[i] == 0)
7173 break;
7174 }
7175 return FALSE;
7176}
7177#endif
7178
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179/*
7180 * Put string '*text' on the screen at position 'row' and 'col', with
7181 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7182 * Note: only outputs within one row, message is truncated at screen boundary!
7183 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7184 */
7185 void
7186screen_puts(text, row, col, attr)
7187 char_u *text;
7188 int row;
7189 int col;
7190 int attr;
7191{
7192 screen_puts_len(text, -1, row, col, attr);
7193}
7194
7195/*
7196 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7197 * a NUL.
7198 */
7199 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007200screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007202 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 int row;
7204 int col;
7205 int attr;
7206{
7207 unsigned off;
7208 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007209 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 int c;
7211#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007212 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 int mbyte_blen = 1;
7214 int mbyte_cells = 1;
7215 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007216 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 int clear_next_cell = FALSE;
7218# ifdef FEAT_ARABIC
7219 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007220 int pc, nc, nc1;
7221 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222# endif
7223#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007224#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7225 int force_redraw_this;
7226 int force_redraw_next = FALSE;
7227#endif
7228 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
7230 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7231 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007232 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
Bram Moolenaarc236c162008-07-13 17:41:49 +00007234#ifdef FEAT_MBYTE
7235 /* When drawing over the right halve of a double-wide char clear out the
7236 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007237 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007238# ifdef FEAT_GUI
7239 && !gui.in_use
7240# endif
7241 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007242 {
7243 ScreenLines[off - 1] = ' ';
7244 ScreenAttrs[off - 1] = 0;
7245 if (enc_utf8)
7246 {
7247 ScreenLinesUC[off - 1] = 0;
7248 ScreenLinesC[0][off - 1] = 0;
7249 }
7250 /* redraw the previous cell, make it empty */
7251 screen_char(off - 1, row, col - 1);
7252 /* force the cell at "col" to be redrawn */
7253 force_redraw_next = TRUE;
7254 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007255#endif
7256
Bram Moolenaar367329b2007-08-30 11:53:22 +00007257#ifdef FEAT_MBYTE
7258 max_off = LineOffset[row] + screen_Columns;
7259#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007260 while (col < screen_Columns
7261 && (len < 0 || (int)(ptr - text) < len)
7262 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {
7264 c = *ptr;
7265#ifdef FEAT_MBYTE
7266 /* check if this is the first byte of a multibyte */
7267 if (has_mbyte)
7268 {
7269 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007270 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007272 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7274 mbyte_cells = 1;
7275 else if (enc_dbcs != 0)
7276 mbyte_cells = mbyte_blen;
7277 else /* enc_utf8 */
7278 {
7279 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007280 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 (int)((text + len) - ptr));
7282 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007283 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007285# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 /* Non-BMP character: display as ? or fullwidth ?. */
7287 if (u8c >= 0x10000)
7288 {
7289 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7290 if (attr == 0)
7291 attr = hl_attr(HLF_8);
7292 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294# ifdef FEAT_ARABIC
7295 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7296 {
7297 /* Do Arabic shaping. */
7298 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7299 {
7300 /* Past end of string to be displayed. */
7301 nc = NUL;
7302 nc1 = NUL;
7303 }
7304 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007305 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007306 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7307 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007308 nc1 = pcc[0];
7309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 pc = prev_c;
7311 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007312 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 }
7314 else
7315 prev_c = u8c;
7316# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007317 if (col + mbyte_cells > screen_Columns)
7318 {
7319 /* Only 1 cell left, but character requires 2 cells:
7320 * display a '>' in the last column to avoid wrapping. */
7321 c = '>';
7322 mbyte_cells = 1;
7323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 }
7325 }
7326#endif
7327
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007328#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7329 force_redraw_this = force_redraw_next;
7330 force_redraw_next = FALSE;
7331#endif
7332
7333 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334#ifdef FEAT_MBYTE
7335 || (mbyte_cells == 2
7336 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7337 || (enc_dbcs == DBCS_JPNU
7338 && c == 0x8e
7339 && ScreenLines2[off] != ptr[1])
7340 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007341 && (ScreenLinesUC[off] !=
7342 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7343 || (ScreenLinesUC[off] != 0
7344 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345#endif
7346 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007347 || exmode_active;
7348
7349 if (need_redraw
7350#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7351 || force_redraw_this
7352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 )
7354 {
7355#if defined(FEAT_GUI) || defined(UNIX)
7356 /* The bold trick makes a single row of pixels appear in the next
7357 * character. When a bold character is removed, the next
7358 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007359 * and for some xterms. */
7360 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361# ifdef FEAT_GUI
7362 gui.in_use
7363# endif
7364# if defined(FEAT_GUI) && defined(UNIX)
7365 ||
7366# endif
7367# ifdef UNIX
7368 term_is_xterm
7369# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007370 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007372 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007374 if (n > HL_ALL)
7375 n = syn_attr2attr(n);
7376 if (n & HL_BOLD)
7377 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
7379#endif
7380#ifdef FEAT_MBYTE
7381 /* When at the end of the text and overwriting a two-cell
7382 * character with a one-cell character, need to clear the next
7383 * cell. Also when overwriting the left halve of a two-cell char
7384 * with the right halve of a two-cell char. Do this only once
7385 * (mb_off2cells() may return 2 on the right halve). */
7386 if (clear_next_cell)
7387 clear_next_cell = FALSE;
7388 else if (has_mbyte
7389 && (len < 0 ? ptr[mbyte_blen] == NUL
7390 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007391 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007393 && (*mb_off2cells)(off, max_off) == 1
7394 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 clear_next_cell = TRUE;
7396
7397 /* Make sure we never leave a second byte of a double-byte behind,
7398 * it confuses mb_off2cells(). */
7399 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007400 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007402 && (*mb_off2cells)(off, max_off) == 1
7403 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 ScreenLines[off + mbyte_blen] = 0;
7405#endif
7406 ScreenLines[off] = c;
7407 ScreenAttrs[off] = attr;
7408#ifdef FEAT_MBYTE
7409 if (enc_utf8)
7410 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007411 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 ScreenLinesUC[off] = 0;
7413 else
7414 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007415 int i;
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007418 for (i = 0; i < Screen_mco; ++i)
7419 {
7420 ScreenLinesC[i][off] = u8cc[i];
7421 if (u8cc[i] == 0)
7422 break;
7423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
7425 if (mbyte_cells == 2)
7426 {
7427 ScreenLines[off + 1] = 0;
7428 ScreenAttrs[off + 1] = attr;
7429 }
7430 screen_char(off, row, col);
7431 }
7432 else if (mbyte_cells == 2)
7433 {
7434 ScreenLines[off + 1] = ptr[1];
7435 ScreenAttrs[off + 1] = attr;
7436 screen_char_2(off, row, col);
7437 }
7438 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7439 {
7440 ScreenLines2[off] = ptr[1];
7441 screen_char(off, row, col);
7442 }
7443 else
7444#endif
7445 screen_char(off, row, col);
7446 }
7447#ifdef FEAT_MBYTE
7448 if (has_mbyte)
7449 {
7450 off += mbyte_cells;
7451 col += mbyte_cells;
7452 ptr += mbyte_blen;
7453 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007454 {
7455 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007457 len = -1;
7458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 }
7460 else
7461#endif
7462 {
7463 ++off;
7464 ++col;
7465 ++ptr;
7466 }
7467 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007468
7469#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7470 /* If we detected the next character needs to be redrawn, but the text
7471 * doesn't extend up to there, update the character here. */
7472 if (force_redraw_next && col < screen_Columns)
7473 {
7474# ifdef FEAT_MBYTE
7475 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7476 screen_char_2(off, row, col);
7477 else
7478# endif
7479 screen_char(off, row, col);
7480 }
7481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482}
7483
7484#ifdef FEAT_SEARCH_EXTRA
7485/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007486 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 */
7488 static void
7489start_search_hl()
7490{
7491 if (p_hls && !no_hlsearch)
7492 {
7493 last_pat_prog(&search_hl.rm);
7494 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007495# ifdef FEAT_RELTIME
7496 /* Set the time limit to 'redrawtime'. */
7497 profile_setlimit(p_rdt, &search_hl.tm);
7498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 }
7500}
7501
7502/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007503 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 */
7505 static void
7506end_search_hl()
7507{
7508 if (search_hl.rm.regprog != NULL)
7509 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007510 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 search_hl.rm.regprog = NULL;
7512 }
7513}
7514
7515/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007516 * Init for calling prepare_search_hl().
7517 */
7518 static void
7519init_search_hl(wp)
7520 win_T *wp;
7521{
7522 matchitem_T *cur;
7523
7524 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7525 * match */
7526 cur = wp->w_match_head;
7527 while (cur != NULL)
7528 {
7529 cur->hl.rm = cur->match;
7530 if (cur->hlg_id == 0)
7531 cur->hl.attr = 0;
7532 else
7533 cur->hl.attr = syn_id2attr(cur->hlg_id);
7534 cur->hl.buf = wp->w_buffer;
7535 cur->hl.lnum = 0;
7536 cur->hl.first_lnum = 0;
7537# ifdef FEAT_RELTIME
7538 /* Set the time limit to 'redrawtime'. */
7539 profile_setlimit(p_rdt, &(cur->hl.tm));
7540# endif
7541 cur = cur->next;
7542 }
7543 search_hl.buf = wp->w_buffer;
7544 search_hl.lnum = 0;
7545 search_hl.first_lnum = 0;
7546 /* time limit is set at the toplevel, for all windows */
7547}
7548
7549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 * Advance to the match in window "wp" line "lnum" or past it.
7551 */
7552 static void
7553prepare_search_hl(wp, lnum)
7554 win_T *wp;
7555 linenr_T lnum;
7556{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007557 matchitem_T *cur; /* points to the match list */
7558 match_T *shl; /* points to search_hl or a match */
7559 int shl_flag; /* flag to indicate whether search_hl
7560 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007561 int pos_inprogress; /* marks that position match search is
7562 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 int n;
7564
7565 /*
7566 * When using a multi-line pattern, start searching at the top
7567 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007568 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007570 cur = wp->w_match_head;
7571 shl_flag = FALSE;
7572 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007574 if (shl_flag == FALSE)
7575 {
7576 shl = &search_hl;
7577 shl_flag = TRUE;
7578 }
7579 else
7580 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 if (shl->rm.regprog != NULL
7582 && shl->lnum == 0
7583 && re_multiline(shl->rm.regprog))
7584 {
7585 if (shl->first_lnum == 0)
7586 {
7587# ifdef FEAT_FOLDING
7588 for (shl->first_lnum = lnum;
7589 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7590 if (hasFoldingWin(wp, shl->first_lnum - 1,
7591 NULL, NULL, TRUE, NULL))
7592 break;
7593# else
7594 shl->first_lnum = wp->w_topline;
7595# endif
7596 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007597 if (cur != NULL)
7598 cur->pos.cur = 0;
7599 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007601 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7602 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007604 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7605 pos_inprogress = cur == NULL || cur->pos.cur == 0
7606 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 if (shl->lnum != 0)
7608 {
7609 shl->first_lnum = shl->lnum
7610 + shl->rm.endpos[0].lnum
7611 - shl->rm.startpos[0].lnum;
7612 n = shl->rm.endpos[0].col;
7613 }
7614 else
7615 {
7616 ++shl->first_lnum;
7617 n = 0;
7618 }
7619 }
7620 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007621 if (shl != &search_hl && cur != NULL)
7622 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 }
7624}
7625
7626/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007627 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 * Uses shl->buf.
7629 * Sets shl->lnum and shl->rm contents.
7630 * Note: Assumes a previous match is always before "lnum", unless
7631 * shl->lnum is zero.
7632 * Careful: Any pointers for buffer lines will become invalid.
7633 */
7634 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007635next_search_hl(win, shl, lnum, mincol, cur)
7636 win_T *win;
7637 match_T *shl; /* points to search_hl or a match */
7638 linenr_T lnum;
7639 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007640 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
7642 linenr_T l;
7643 colnr_T matchcol;
7644 long nmatched;
7645
7646 if (shl->lnum != 0)
7647 {
7648 /* Check for three situations:
7649 * 1. If the "lnum" is below a previous match, start a new search.
7650 * 2. If the previous match includes "mincol", use it.
7651 * 3. Continue after the previous match.
7652 */
7653 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7654 if (lnum > l)
7655 shl->lnum = 0;
7656 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7657 return;
7658 }
7659
7660 /*
7661 * Repeat searching for a match until one is found that includes "mincol"
7662 * or none is found in this line.
7663 */
7664 called_emsg = FALSE;
7665 for (;;)
7666 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007667#ifdef FEAT_RELTIME
7668 /* Stop searching after passing the time limit. */
7669 if (profile_passed_limit(&(shl->tm)))
7670 {
7671 shl->lnum = 0; /* no match found in time */
7672 break;
7673 }
7674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 /* Three situations:
7676 * 1. No useful previous match: search from start of line.
7677 * 2. Not Vi compatible or empty match: continue at next character.
7678 * Break the loop if this is beyond the end of the line.
7679 * 3. Vi compatible searching: continue at end of previous match.
7680 */
7681 if (shl->lnum == 0)
7682 matchcol = 0;
7683 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7684 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007685 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007687 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007688
7689 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007690 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007691 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007693 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 shl->lnum = 0;
7695 break;
7696 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007697#ifdef FEAT_MBYTE
7698 if (has_mbyte)
7699 matchcol += mb_ptr2len(ml);
7700 else
7701#endif
7702 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704 else
7705 matchcol = shl->rm.endpos[0].col;
7706
7707 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007708 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007710 /* Remember whether shl->rm is using a copy of the regprog in
7711 * cur->match. */
7712 int regprog_is_copy = (shl != &search_hl && cur != NULL
7713 && shl == &cur->hl
7714 && cur->match.regprog == cur->hl.rm.regprog);
7715
Bram Moolenaarb3414592014-06-17 17:48:32 +02007716 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7717 matchcol,
7718#ifdef FEAT_RELTIME
7719 &(shl->tm)
7720#else
7721 NULL
7722#endif
7723 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007724 /* Copy the regprog, in case it got freed and recompiled. */
7725 if (regprog_is_copy)
7726 cur->match.regprog = cur->hl.rm.regprog;
7727
Bram Moolenaarb3414592014-06-17 17:48:32 +02007728 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007729 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007730 /* Error while handling regexp: stop using this regexp. */
7731 if (shl == &search_hl)
7732 {
7733 /* don't free regprog in the match list, it's a copy */
7734 vim_regfree(shl->rm.regprog);
7735 SET_NO_HLSEARCH(TRUE);
7736 }
7737 shl->rm.regprog = NULL;
7738 shl->lnum = 0;
7739 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7740 message */
7741 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007742 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007743 }
7744 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007745 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007746 else
7747 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (nmatched == 0)
7749 {
7750 shl->lnum = 0; /* no match found */
7751 break;
7752 }
7753 if (shl->rm.startpos[0].lnum > 0
7754 || shl->rm.startpos[0].col >= mincol
7755 || nmatched > 1
7756 || shl->rm.endpos[0].col > mincol)
7757 {
7758 shl->lnum += shl->rm.startpos[0].lnum;
7759 break; /* useful match found */
7760 }
7761 }
7762}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763
Bram Moolenaarb3414592014-06-17 17:48:32 +02007764 static int
7765next_search_hl_pos(shl, lnum, posmatch, mincol)
7766 match_T *shl; /* points to a match */
7767 linenr_T lnum;
7768 posmatch_T *posmatch; /* match positions */
7769 colnr_T mincol; /* minimal column for a match */
7770{
7771 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007772 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007773
7774 shl->lnum = 0;
7775 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7776 {
7777 if (posmatch->pos[i].lnum == 0)
7778 break;
7779 if (posmatch->pos[i].col < mincol)
7780 continue;
7781 if (posmatch->pos[i].lnum == lnum)
7782 {
7783 if (shl->lnum == lnum)
7784 {
7785 /* partially sort positions by column numbers
7786 * on the same line */
7787 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7788 {
7789 llpos_T tmp = posmatch->pos[i];
7790
7791 posmatch->pos[i] = posmatch->pos[bot];
7792 posmatch->pos[bot] = tmp;
7793 }
7794 }
7795 else
7796 {
7797 bot = i;
7798 shl->lnum = lnum;
7799 }
7800 }
7801 }
7802 posmatch->cur = 0;
7803 if (shl->lnum == lnum)
7804 {
7805 colnr_T start = posmatch->pos[bot].col == 0
7806 ? 0 : posmatch->pos[bot].col - 1;
7807 colnr_T end = posmatch->pos[bot].col == 0
7808 ? MAXCOL : start + posmatch->pos[bot].len;
7809
7810 shl->rm.startpos[0].lnum = 0;
7811 shl->rm.startpos[0].col = start;
7812 shl->rm.endpos[0].lnum = 0;
7813 shl->rm.endpos[0].col = end;
7814 posmatch->cur = bot + 1;
7815 return TRUE;
7816 }
7817 return FALSE;
7818}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007819#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007820
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 static void
7822screen_start_highlight(attr)
7823 int attr;
7824{
7825 attrentry_T *aep = NULL;
7826
7827 screen_attr = attr;
7828 if (full_screen
7829#ifdef WIN3264
7830 && termcap_active
7831#endif
7832 )
7833 {
7834#ifdef FEAT_GUI
7835 if (gui.in_use)
7836 {
7837 char buf[20];
7838
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007839 /* The GUI handles this internally. */
7840 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 OUT_STR(buf);
7842 }
7843 else
7844#endif
7845 {
7846 if (attr > HL_ALL) /* special HL attr. */
7847 {
7848 if (t_colors > 1)
7849 aep = syn_cterm_attr2entry(attr);
7850 else
7851 aep = syn_term_attr2entry(attr);
7852 if (aep == NULL) /* did ":syntax clear" */
7853 attr = 0;
7854 else
7855 attr = aep->ae_attr;
7856 }
7857 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7858 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007859 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7860 && cterm_normal_fg_bold)
7861 /* If the Normal FG color has BOLD attribute and the new HL
7862 * has a FG color defined, clear BOLD. */
7863 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7865 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007866 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7867 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 out_str(T_US);
7869 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7870 out_str(T_CZH);
7871 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7872 out_str(T_MR);
7873
7874 /*
7875 * Output the color or start string after bold etc., in case the
7876 * bold etc. override the color setting.
7877 */
7878 if (aep != NULL)
7879 {
7880 if (t_colors > 1)
7881 {
7882 if (aep->ae_u.cterm.fg_color)
7883 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7884 if (aep->ae_u.cterm.bg_color)
7885 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7886 }
7887 else
7888 {
7889 if (aep->ae_u.term.start != NULL)
7890 out_str(aep->ae_u.term.start);
7891 }
7892 }
7893 }
7894 }
7895}
7896
7897 void
7898screen_stop_highlight()
7899{
7900 int do_ME = FALSE; /* output T_ME code */
7901
7902 if (screen_attr != 0
7903#ifdef WIN3264
7904 && termcap_active
7905#endif
7906 )
7907 {
7908#ifdef FEAT_GUI
7909 if (gui.in_use)
7910 {
7911 char buf[20];
7912
7913 /* use internal GUI code */
7914 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7915 OUT_STR(buf);
7916 }
7917 else
7918#endif
7919 {
7920 if (screen_attr > HL_ALL) /* special HL attr. */
7921 {
7922 attrentry_T *aep;
7923
7924 if (t_colors > 1)
7925 {
7926 /*
7927 * Assume that t_me restores the original colors!
7928 */
7929 aep = syn_cterm_attr2entry(screen_attr);
7930 if (aep != NULL && (aep->ae_u.cterm.fg_color
7931 || aep->ae_u.cterm.bg_color))
7932 do_ME = TRUE;
7933 }
7934 else
7935 {
7936 aep = syn_term_attr2entry(screen_attr);
7937 if (aep != NULL && aep->ae_u.term.stop != NULL)
7938 {
7939 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7940 do_ME = TRUE;
7941 else
7942 out_str(aep->ae_u.term.stop);
7943 }
7944 }
7945 if (aep == NULL) /* did ":syntax clear" */
7946 screen_attr = 0;
7947 else
7948 screen_attr = aep->ae_attr;
7949 }
7950
7951 /*
7952 * Often all ending-codes are equal to T_ME. Avoid outputting the
7953 * same sequence several times.
7954 */
7955 if (screen_attr & HL_STANDOUT)
7956 {
7957 if (STRCMP(T_SE, T_ME) == 0)
7958 do_ME = TRUE;
7959 else
7960 out_str(T_SE);
7961 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007962 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {
7964 if (STRCMP(T_UE, T_ME) == 0)
7965 do_ME = TRUE;
7966 else
7967 out_str(T_UE);
7968 }
7969 if (screen_attr & HL_ITALIC)
7970 {
7971 if (STRCMP(T_CZR, T_ME) == 0)
7972 do_ME = TRUE;
7973 else
7974 out_str(T_CZR);
7975 }
7976 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7977 out_str(T_ME);
7978
7979 if (t_colors > 1)
7980 {
7981 /* set Normal cterm colors */
7982 if (cterm_normal_fg_color != 0)
7983 term_fg_color(cterm_normal_fg_color - 1);
7984 if (cterm_normal_bg_color != 0)
7985 term_bg_color(cterm_normal_bg_color - 1);
7986 if (cterm_normal_fg_bold)
7987 out_str(T_MD);
7988 }
7989 }
7990 }
7991 screen_attr = 0;
7992}
7993
7994/*
7995 * Reset the colors for a cterm. Used when leaving Vim.
7996 * The machine specific code may override this again.
7997 */
7998 void
7999reset_cterm_colors()
8000{
8001 if (t_colors > 1)
8002 {
8003 /* set Normal cterm colors */
8004 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
8005 {
8006 out_str(T_OP);
8007 screen_attr = -1;
8008 }
8009 if (cterm_normal_fg_bold)
8010 {
8011 out_str(T_ME);
8012 screen_attr = -1;
8013 }
8014 }
8015}
8016
8017/*
8018 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8019 * using the attributes from ScreenAttrs["off"].
8020 */
8021 static void
8022screen_char(off, row, col)
8023 unsigned off;
8024 int row;
8025 int col;
8026{
8027 int attr;
8028
8029 /* Check for illegal values, just in case (could happen just after
8030 * resizing). */
8031 if (row >= screen_Rows || col >= screen_Columns)
8032 return;
8033
Bram Moolenaar494838a2015-02-10 19:20:37 +01008034 /* Outputting a character in the last cell on the screen may scroll the
8035 * screen up. Only do it when the "xn" termcap property is set, otherwise
8036 * mark the character invalid (update it when scrolled up). */
8037 if (*T_XN == NUL
8038 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039#ifdef FEAT_RIGHTLEFT
8040 /* account for first command-line character in rightleft mode */
8041 && !cmdmsg_rl
8042#endif
8043 )
8044 {
8045 ScreenAttrs[off] = (sattr_T)-1;
8046 return;
8047 }
8048
8049 /*
8050 * Stop highlighting first, so it's easier to move the cursor.
8051 */
8052#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
8053 if (screen_char_attr != 0)
8054 attr = screen_char_attr;
8055 else
8056#endif
8057 attr = ScreenAttrs[off];
8058 if (screen_attr != attr)
8059 screen_stop_highlight();
8060
8061 windgoto(row, col);
8062
8063 if (screen_attr != attr)
8064 screen_start_highlight(attr);
8065
8066#ifdef FEAT_MBYTE
8067 if (enc_utf8 && ScreenLinesUC[off] != 0)
8068 {
8069 char_u buf[MB_MAXBYTES + 1];
8070
8071 /* Convert UTF-8 character to bytes and write it. */
8072
8073 buf[utfc_char2bytes(off, buf)] = NUL;
8074
8075 out_str(buf);
8076 if (utf_char2cells(ScreenLinesUC[off]) > 1)
8077 ++screen_cur_col;
8078 }
8079 else
8080#endif
8081 {
8082#ifdef FEAT_MBYTE
8083 out_flush_check();
8084#endif
8085 out_char(ScreenLines[off]);
8086#ifdef FEAT_MBYTE
8087 /* double-byte character in single-width cell */
8088 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8089 out_char(ScreenLines2[off]);
8090#endif
8091 }
8092
8093 screen_cur_col++;
8094}
8095
8096#ifdef FEAT_MBYTE
8097
8098/*
8099 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8100 * on the screen at position 'row' and 'col'.
8101 * The attributes of the first byte is used for all. This is required to
8102 * output the two bytes of a double-byte character with nothing in between.
8103 */
8104 static void
8105screen_char_2(off, row, col)
8106 unsigned off;
8107 int row;
8108 int col;
8109{
8110 /* Check for illegal values (could be wrong when screen was resized). */
8111 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8112 return;
8113
8114 /* Outputting the last character on the screen may scrollup the screen.
8115 * Don't to it! Mark the character invalid (update it when scrolled up) */
8116 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8117 {
8118 ScreenAttrs[off] = (sattr_T)-1;
8119 return;
8120 }
8121
8122 /* Output the first byte normally (positions the cursor), then write the
8123 * second byte directly. */
8124 screen_char(off, row, col);
8125 out_char(ScreenLines[off + 1]);
8126 ++screen_cur_col;
8127}
8128#endif
8129
8130#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8131/*
8132 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8133 * This uses the contents of ScreenLines[] and doesn't change it.
8134 */
8135 void
8136screen_draw_rectangle(row, col, height, width, invert)
8137 int row;
8138 int col;
8139 int height;
8140 int width;
8141 int invert;
8142{
8143 int r, c;
8144 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008145#ifdef FEAT_MBYTE
8146 int max_off;
8147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008149 /* Can't use ScreenLines unless initialized */
8150 if (ScreenLines == NULL)
8151 return;
8152
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 if (invert)
8154 screen_char_attr = HL_INVERSE;
8155 for (r = row; r < row + height; ++r)
8156 {
8157 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008158#ifdef FEAT_MBYTE
8159 max_off = off + screen_Columns;
8160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 for (c = col; c < col + width; ++c)
8162 {
8163#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008164 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {
8166 screen_char_2(off + c, r, c);
8167 ++c;
8168 }
8169 else
8170#endif
8171 {
8172 screen_char(off + c, r, c);
8173#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008174 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 ++c;
8176#endif
8177 }
8178 }
8179 }
8180 screen_char_attr = 0;
8181}
8182#endif
8183
8184#ifdef FEAT_VERTSPLIT
8185/*
8186 * Redraw the characters for a vertically split window.
8187 */
8188 static void
8189redraw_block(row, end, wp)
8190 int row;
8191 int end;
8192 win_T *wp;
8193{
8194 int col;
8195 int width;
8196
8197# ifdef FEAT_CLIPBOARD
8198 clip_may_clear_selection(row, end - 1);
8199# endif
8200
8201 if (wp == NULL)
8202 {
8203 col = 0;
8204 width = Columns;
8205 }
8206 else
8207 {
8208 col = wp->w_wincol;
8209 width = wp->w_width;
8210 }
8211 screen_draw_rectangle(row, col, end - row, width, FALSE);
8212}
8213#endif
8214
8215/*
8216 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8217 * with character 'c1' in first column followed by 'c2' in the other columns.
8218 * Use attributes 'attr'.
8219 */
8220 void
8221screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8222 int start_row, end_row;
8223 int start_col, end_col;
8224 int c1, c2;
8225 int attr;
8226{
8227 int row;
8228 int col;
8229 int off;
8230 int end_off;
8231 int did_delete;
8232 int c;
8233 int norm_term;
8234#if defined(FEAT_GUI) || defined(UNIX)
8235 int force_next = FALSE;
8236#endif
8237
8238 if (end_row > screen_Rows) /* safety check */
8239 end_row = screen_Rows;
8240 if (end_col > screen_Columns) /* safety check */
8241 end_col = screen_Columns;
8242 if (ScreenLines == NULL
8243 || start_row >= end_row
8244 || start_col >= end_col) /* nothing to do */
8245 return;
8246
8247 /* it's a "normal" terminal when not in a GUI or cterm */
8248 norm_term = (
8249#ifdef FEAT_GUI
8250 !gui.in_use &&
8251#endif
8252 t_colors <= 1);
8253 for (row = start_row; row < end_row; ++row)
8254 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008255#ifdef FEAT_MBYTE
8256 if (has_mbyte
8257# ifdef FEAT_GUI
8258 && !gui.in_use
8259# endif
8260 )
8261 {
8262 /* When drawing over the right halve of a double-wide char clear
8263 * out the left halve. When drawing over the left halve of a
8264 * double wide-char clear out the right halve. Only needed in a
8265 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008266 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008267 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008268 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008269 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008270 }
8271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 /*
8273 * Try to use delete-line termcap code, when no attributes or in a
8274 * "normal" terminal, where a bold/italic space is just a
8275 * space.
8276 */
8277 did_delete = FALSE;
8278 if (c2 == ' '
8279 && end_col == Columns
8280 && can_clear(T_CE)
8281 && (attr == 0
8282 || (norm_term
8283 && attr <= HL_ALL
8284 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8285 {
8286 /*
8287 * check if we really need to clear something
8288 */
8289 col = start_col;
8290 if (c1 != ' ') /* don't clear first char */
8291 ++col;
8292
8293 off = LineOffset[row] + col;
8294 end_off = LineOffset[row] + end_col;
8295
8296 /* skip blanks (used often, keep it fast!) */
8297#ifdef FEAT_MBYTE
8298 if (enc_utf8)
8299 while (off < end_off && ScreenLines[off] == ' '
8300 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8301 ++off;
8302 else
8303#endif
8304 while (off < end_off && ScreenLines[off] == ' '
8305 && ScreenAttrs[off] == 0)
8306 ++off;
8307 if (off < end_off) /* something to be cleared */
8308 {
8309 col = off - LineOffset[row];
8310 screen_stop_highlight();
8311 term_windgoto(row, col);/* clear rest of this screen line */
8312 out_str(T_CE);
8313 screen_start(); /* don't know where cursor is now */
8314 col = end_col - col;
8315 while (col--) /* clear chars in ScreenLines */
8316 {
8317 ScreenLines[off] = ' ';
8318#ifdef FEAT_MBYTE
8319 if (enc_utf8)
8320 ScreenLinesUC[off] = 0;
8321#endif
8322 ScreenAttrs[off] = 0;
8323 ++off;
8324 }
8325 }
8326 did_delete = TRUE; /* the chars are cleared now */
8327 }
8328
8329 off = LineOffset[row] + start_col;
8330 c = c1;
8331 for (col = start_col; col < end_col; ++col)
8332 {
8333 if (ScreenLines[off] != c
8334#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008335 || (enc_utf8 && (int)ScreenLinesUC[off]
8336 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337#endif
8338 || ScreenAttrs[off] != attr
8339#if defined(FEAT_GUI) || defined(UNIX)
8340 || force_next
8341#endif
8342 )
8343 {
8344#if defined(FEAT_GUI) || defined(UNIX)
8345 /* The bold trick may make a single row of pixels appear in
8346 * the next character. When a bold character is removed, the
8347 * next character should be redrawn too. This happens for our
8348 * own GUI and for some xterms. */
8349 if (
8350# ifdef FEAT_GUI
8351 gui.in_use
8352# endif
8353# if defined(FEAT_GUI) && defined(UNIX)
8354 ||
8355# endif
8356# ifdef UNIX
8357 term_is_xterm
8358# endif
8359 )
8360 {
8361 if (ScreenLines[off] != ' '
8362 && (ScreenAttrs[off] > HL_ALL
8363 || ScreenAttrs[off] & HL_BOLD))
8364 force_next = TRUE;
8365 else
8366 force_next = FALSE;
8367 }
8368#endif
8369 ScreenLines[off] = c;
8370#ifdef FEAT_MBYTE
8371 if (enc_utf8)
8372 {
8373 if (c >= 0x80)
8374 {
8375 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008376 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 }
8378 else
8379 ScreenLinesUC[off] = 0;
8380 }
8381#endif
8382 ScreenAttrs[off] = attr;
8383 if (!did_delete || c != ' ')
8384 screen_char(off, row, col);
8385 }
8386 ++off;
8387 if (col == start_col)
8388 {
8389 if (did_delete)
8390 break;
8391 c = c2;
8392 }
8393 }
8394 if (end_col == Columns)
8395 LineWraps[row] = FALSE;
8396 if (row == Rows - 1) /* overwritten the command line */
8397 {
8398 redraw_cmdline = TRUE;
8399 if (c1 == ' ' && c2 == ' ')
8400 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008401 if (start_col == 0)
8402 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 }
8404 }
8405}
8406
8407/*
8408 * Check if there should be a delay. Used before clearing or redrawing the
8409 * screen or the command line.
8410 */
8411 void
8412check_for_delay(check_msg_scroll)
8413 int check_msg_scroll;
8414{
8415 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8416 && !did_wait_return
8417 && emsg_silent == 0)
8418 {
8419 out_flush();
8420 ui_delay(1000L, TRUE);
8421 emsg_on_display = FALSE;
8422 if (check_msg_scroll)
8423 msg_scroll = FALSE;
8424 }
8425}
8426
8427/*
8428 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008429 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 * Returns TRUE if there is a valid screen to write to.
8431 * Returns FALSE when starting up and screen not initialized yet.
8432 */
8433 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008434screen_valid(doclear)
8435 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008437 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 return (ScreenLines != NULL);
8439}
8440
8441/*
8442 * Resize the shell to Rows and Columns.
8443 * Allocate ScreenLines[] and associated items.
8444 *
8445 * There may be some time between setting Rows and Columns and (re)allocating
8446 * ScreenLines[]. This happens when starting up and when (manually) changing
8447 * the shell size. Always use screen_Rows and screen_Columns to access items
8448 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8449 * final size of the shell is needed.
8450 */
8451 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008452screenalloc(doclear)
8453 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 int new_row, old_row;
8456#ifdef FEAT_GUI
8457 int old_Rows;
8458#endif
8459 win_T *wp;
8460 int outofmem = FALSE;
8461 int len;
8462 schar_T *new_ScreenLines;
8463#ifdef FEAT_MBYTE
8464 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008465 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008467 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#endif
8469 sattr_T *new_ScreenAttrs;
8470 unsigned *new_LineOffset;
8471 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008472#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008473 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008474 tabpage_T *tp;
8475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008477 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008478#ifdef FEAT_AUTOCMD
8479 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008481retry:
8482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 /*
8484 * Allocation of the screen buffers is done only when the size changes and
8485 * when Rows and Columns have been set and we have started doing full
8486 * screen stuff.
8487 */
8488 if ((ScreenLines != NULL
8489 && Rows == screen_Rows
8490 && Columns == screen_Columns
8491#ifdef FEAT_MBYTE
8492 && enc_utf8 == (ScreenLinesUC != NULL)
8493 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008494 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495#endif
8496 )
8497 || Rows == 0
8498 || Columns == 0
8499 || (!full_screen && ScreenLines == NULL))
8500 return;
8501
8502 /*
8503 * It's possible that we produce an out-of-memory message below, which
8504 * will cause this function to be called again. To break the loop, just
8505 * return here.
8506 */
8507 if (entered)
8508 return;
8509 entered = TRUE;
8510
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008511 /*
8512 * Note that the window sizes are updated before reallocating the arrays,
8513 * thus we must not redraw here!
8514 */
8515 ++RedrawingDisabled;
8516
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 win_new_shellsize(); /* fit the windows in the new sized shell */
8518
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 comp_col(); /* recompute columns for shown command and ruler */
8520
8521 /*
8522 * We're changing the size of the screen.
8523 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8524 * - Move lines from the old arrays into the new arrays, clear extra
8525 * lines (unless the screen is going to be cleared).
8526 * - Free the old arrays.
8527 *
8528 * If anything fails, make ScreenLines NULL, so we don't do anything!
8529 * Continuing with the old ScreenLines may result in a crash, because the
8530 * size is wrong.
8531 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008532 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008534#ifdef FEAT_AUTOCMD
8535 if (aucmd_win != NULL)
8536 win_free_lsize(aucmd_win);
8537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538
8539 new_ScreenLines = (schar_T *)lalloc((long_u)(
8540 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8541#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008542 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (enc_utf8)
8544 {
8545 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8546 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008547 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008548 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8550 }
8551 if (enc_dbcs == DBCS_JPNU)
8552 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8553 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8554#endif
8555 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8556 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8557 new_LineOffset = (unsigned *)lalloc((long_u)(
8558 Rows * sizeof(unsigned)), FALSE);
8559 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008560#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008561 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008564 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {
8566 if (win_alloc_lines(wp) == FAIL)
8567 {
8568 outofmem = TRUE;
8569#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008570 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571#endif
8572 }
8573 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008574#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008575 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8576 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008577 outofmem = TRUE;
8578#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008579#ifdef FEAT_WINDOWS
8580give_up:
8581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008583#ifdef FEAT_MBYTE
8584 for (i = 0; i < p_mco; ++i)
8585 if (new_ScreenLinesC[i] == NULL)
8586 break;
8587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 if (new_ScreenLines == NULL
8589#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008590 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8592#endif
8593 || new_ScreenAttrs == NULL
8594 || new_LineOffset == NULL
8595 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008596#ifdef FEAT_WINDOWS
8597 || new_TabPageIdxs == NULL
8598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 || outofmem)
8600 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008601 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008602 {
8603 /* guess the size */
8604 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8605
8606 /* Remember we did this to avoid getting outofmem messages over
8607 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008608 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 vim_free(new_ScreenLines);
8611 new_ScreenLines = NULL;
8612#ifdef FEAT_MBYTE
8613 vim_free(new_ScreenLinesUC);
8614 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008615 for (i = 0; i < p_mco; ++i)
8616 {
8617 vim_free(new_ScreenLinesC[i]);
8618 new_ScreenLinesC[i] = NULL;
8619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 vim_free(new_ScreenLines2);
8621 new_ScreenLines2 = NULL;
8622#endif
8623 vim_free(new_ScreenAttrs);
8624 new_ScreenAttrs = NULL;
8625 vim_free(new_LineOffset);
8626 new_LineOffset = NULL;
8627 vim_free(new_LineWraps);
8628 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008629#ifdef FEAT_WINDOWS
8630 vim_free(new_TabPageIdxs);
8631 new_TabPageIdxs = NULL;
8632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 }
8634 else
8635 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008636 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008637
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 for (new_row = 0; new_row < Rows; ++new_row)
8639 {
8640 new_LineOffset[new_row] = new_row * Columns;
8641 new_LineWraps[new_row] = FALSE;
8642
8643 /*
8644 * If the screen is not going to be cleared, copy as much as
8645 * possible from the old screen to the new one and clear the rest
8646 * (used when resizing the window at the "--more--" prompt or when
8647 * executing an external command, for the GUI).
8648 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008649 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {
8651 (void)vim_memset(new_ScreenLines + new_row * Columns,
8652 ' ', (size_t)Columns * sizeof(schar_T));
8653#ifdef FEAT_MBYTE
8654 if (enc_utf8)
8655 {
8656 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8657 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008658 for (i = 0; i < p_mco; ++i)
8659 (void)vim_memset(new_ScreenLinesC[i]
8660 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 0, (size_t)Columns * sizeof(u8char_T));
8662 }
8663 if (enc_dbcs == DBCS_JPNU)
8664 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8665 0, (size_t)Columns * sizeof(schar_T));
8666#endif
8667 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8668 0, (size_t)Columns * sizeof(sattr_T));
8669 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008670 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 {
8672 if (screen_Columns < Columns)
8673 len = screen_Columns;
8674 else
8675 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008676#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008677 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008678 * may be invalid now. Also when p_mco changes. */
8679 if (!(enc_utf8 && ScreenLinesUC == NULL)
8680 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008681#endif
8682 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8683 ScreenLines + LineOffset[old_row],
8684 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008686 if (enc_utf8 && ScreenLinesUC != NULL
8687 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 {
8689 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8690 ScreenLinesUC + LineOffset[old_row],
8691 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008692 for (i = 0; i < p_mco; ++i)
8693 mch_memmove(new_ScreenLinesC[i]
8694 + new_LineOffset[new_row],
8695 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 (size_t)len * sizeof(u8char_T));
8697 }
8698 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8699 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8700 ScreenLines2 + LineOffset[old_row],
8701 (size_t)len * sizeof(schar_T));
8702#endif
8703 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8704 ScreenAttrs + LineOffset[old_row],
8705 (size_t)len * sizeof(sattr_T));
8706 }
8707 }
8708 }
8709 /* Use the last line of the screen for the current line. */
8710 current_ScreenLine = new_ScreenLines + Rows * Columns;
8711 }
8712
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008713 free_screenlines();
8714
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 ScreenLines = new_ScreenLines;
8716#ifdef FEAT_MBYTE
8717 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008718 for (i = 0; i < p_mco; ++i)
8719 ScreenLinesC[i] = new_ScreenLinesC[i];
8720 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 ScreenLines2 = new_ScreenLines2;
8722#endif
8723 ScreenAttrs = new_ScreenAttrs;
8724 LineOffset = new_LineOffset;
8725 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008726#ifdef FEAT_WINDOWS
8727 TabPageIdxs = new_TabPageIdxs;
8728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729
8730 /* It's important that screen_Rows and screen_Columns reflect the actual
8731 * size of ScreenLines[]. Set them before calling anything. */
8732#ifdef FEAT_GUI
8733 old_Rows = screen_Rows;
8734#endif
8735 screen_Rows = Rows;
8736 screen_Columns = Columns;
8737
8738 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008739 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 screenclear2();
8741
8742#ifdef FEAT_GUI
8743 else if (gui.in_use
8744 && !gui.starting
8745 && ScreenLines != NULL
8746 && old_Rows != Rows)
8747 {
8748 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8749 /*
8750 * Adjust the position of the cursor, for when executing an external
8751 * command.
8752 */
8753 if (msg_row >= Rows) /* Rows got smaller */
8754 msg_row = Rows - 1; /* put cursor at last row */
8755 else if (Rows > old_Rows) /* Rows got bigger */
8756 msg_row += Rows - old_Rows; /* put cursor in same place */
8757 if (msg_col >= Columns) /* Columns got smaller */
8758 msg_col = Columns - 1; /* put cursor at last column */
8759 }
8760#endif
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008763 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008764
8765#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008766 /*
8767 * Do not apply autocommands more than 3 times to avoid an endless loop
8768 * in case applying autocommands always changes Rows or Columns.
8769 */
8770 if (starting == 0 && ++retry_count <= 3)
8771 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008772 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008773 /* In rare cases, autocommands may have altered Rows or Columns,
8774 * jump back to check if we need to allocate the screen again. */
8775 goto retry;
8776 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778}
8779
8780 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008781free_screenlines()
8782{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008783#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008784 int i;
8785
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008786 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008787 for (i = 0; i < Screen_mco; ++i)
8788 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008789 vim_free(ScreenLines2);
8790#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008791 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008792 vim_free(ScreenAttrs);
8793 vim_free(LineOffset);
8794 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008795#ifdef FEAT_WINDOWS
8796 vim_free(TabPageIdxs);
8797#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008798}
8799
8800 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801screenclear()
8802{
8803 check_for_delay(FALSE);
8804 screenalloc(FALSE); /* allocate screen buffers if size changed */
8805 screenclear2(); /* clear the screen */
8806}
8807
8808 static void
8809screenclear2()
8810{
8811 int i;
8812
8813 if (starting == NO_SCREEN || ScreenLines == NULL
8814#ifdef FEAT_GUI
8815 || (gui.in_use && gui.starting)
8816#endif
8817 )
8818 return;
8819
8820#ifdef FEAT_GUI
8821 if (!gui.in_use)
8822#endif
8823 screen_attr = -1; /* force setting the Normal colors */
8824 screen_stop_highlight(); /* don't want highlighting here */
8825
8826#ifdef FEAT_CLIPBOARD
8827 /* disable selection without redrawing it */
8828 clip_scroll_selection(9999);
8829#endif
8830
8831 /* blank out ScreenLines */
8832 for (i = 0; i < Rows; ++i)
8833 {
8834 lineclear(LineOffset[i], (int)Columns);
8835 LineWraps[i] = FALSE;
8836 }
8837
8838 if (can_clear(T_CL))
8839 {
8840 out_str(T_CL); /* clear the display */
8841 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008842 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 }
8844 else
8845 {
8846 /* can't clear the screen, mark all chars with invalid attributes */
8847 for (i = 0; i < Rows; ++i)
8848 lineinvalid(LineOffset[i], (int)Columns);
8849 clear_cmdline = TRUE;
8850 }
8851
8852 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8853
8854 win_rest_invalid(firstwin);
8855 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008856#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008857 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 if (must_redraw == CLEAR) /* no need to clear again */
8860 must_redraw = NOT_VALID;
8861 compute_cmdrow();
8862 msg_row = cmdline_row; /* put cursor on last line for messages */
8863 msg_col = 0;
8864 screen_start(); /* don't know where cursor is now */
8865 msg_scrolled = 0; /* can't scroll back */
8866 msg_didany = FALSE;
8867 msg_didout = FALSE;
8868}
8869
8870/*
8871 * Clear one line in ScreenLines.
8872 */
8873 static void
8874lineclear(off, width)
8875 unsigned off;
8876 int width;
8877{
8878 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8879#ifdef FEAT_MBYTE
8880 if (enc_utf8)
8881 (void)vim_memset(ScreenLinesUC + off, 0,
8882 (size_t)width * sizeof(u8char_T));
8883#endif
8884 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8885}
8886
8887/*
8888 * Mark one line in ScreenLines invalid by setting the attributes to an
8889 * invalid value.
8890 */
8891 static void
8892lineinvalid(off, width)
8893 unsigned off;
8894 int width;
8895{
8896 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8897}
8898
8899#ifdef FEAT_VERTSPLIT
8900/*
8901 * Copy part of a Screenline for vertically split window "wp".
8902 */
8903 static void
8904linecopy(to, from, wp)
8905 int to;
8906 int from;
8907 win_T *wp;
8908{
8909 unsigned off_to = LineOffset[to] + wp->w_wincol;
8910 unsigned off_from = LineOffset[from] + wp->w_wincol;
8911
8912 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8913 wp->w_width * sizeof(schar_T));
8914# ifdef FEAT_MBYTE
8915 if (enc_utf8)
8916 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008917 int i;
8918
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8920 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008921 for (i = 0; i < p_mco; ++i)
8922 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8923 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 }
8925 if (enc_dbcs == DBCS_JPNU)
8926 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8927 wp->w_width * sizeof(schar_T));
8928# endif
8929 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8930 wp->w_width * sizeof(sattr_T));
8931}
8932#endif
8933
8934/*
8935 * Return TRUE if clearing with term string "p" would work.
8936 * It can't work when the string is empty or it won't set the right background.
8937 */
8938 int
8939can_clear(p)
8940 char_u *p;
8941{
8942 return (*p != NUL && (t_colors <= 1
8943#ifdef FEAT_GUI
8944 || gui.in_use
8945#endif
8946 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8947}
8948
8949/*
8950 * Reset cursor position. Use whenever cursor was moved because of outputting
8951 * something directly to the screen (shell commands) or a terminal control
8952 * code.
8953 */
8954 void
8955screen_start()
8956{
8957 screen_cur_row = screen_cur_col = 9999;
8958}
8959
8960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 * Move the cursor to position "row","col" in the screen.
8962 * This tries to find the most efficient way to move, minimizing the number of
8963 * characters sent to the terminal.
8964 */
8965 void
8966windgoto(row, col)
8967 int row;
8968 int col;
8969{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008970 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 int i;
8972 int plan;
8973 int cost;
8974 int wouldbe_col;
8975 int noinvcurs;
8976 char_u *bs;
8977 int goto_cost;
8978 int attr;
8979
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008980#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8982
8983#define PLAN_LE 1
8984#define PLAN_CR 2
8985#define PLAN_NL 3
8986#define PLAN_WRITE 4
8987 /* Can't use ScreenLines unless initialized */
8988 if (ScreenLines == NULL)
8989 return;
8990
8991 if (col != screen_cur_col || row != screen_cur_row)
8992 {
8993 /* Check for valid position. */
8994 if (row < 0) /* window without text lines? */
8995 row = 0;
8996 if (row >= screen_Rows)
8997 row = screen_Rows - 1;
8998 if (col >= screen_Columns)
8999 col = screen_Columns - 1;
9000
9001 /* check if no cursor movement is allowed in highlight mode */
9002 if (screen_attr && *T_MS == NUL)
9003 noinvcurs = HIGHL_COST;
9004 else
9005 noinvcurs = 0;
9006 goto_cost = GOTO_COST + noinvcurs;
9007
9008 /*
9009 * Plan how to do the positioning:
9010 * 1. Use CR to move it to column 0, same row.
9011 * 2. Use T_LE to move it a few columns to the left.
9012 * 3. Use NL to move a few lines down, column 0.
9013 * 4. Move a few columns to the right with T_ND or by writing chars.
9014 *
9015 * Don't do this if the cursor went beyond the last column, the cursor
9016 * position is unknown then (some terminals wrap, some don't )
9017 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009018 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 * characters to move the cursor to the right.
9020 */
9021 if (row >= screen_cur_row && screen_cur_col < Columns)
9022 {
9023 /*
9024 * If the cursor is in the same row, bigger col, we can use CR
9025 * or T_LE.
9026 */
9027 bs = NULL; /* init for GCC */
9028 attr = screen_attr;
9029 if (row == screen_cur_row && col < screen_cur_col)
9030 {
9031 /* "le" is preferred over "bc", because "bc" is obsolete */
9032 if (*T_LE)
9033 bs = T_LE; /* "cursor left" */
9034 else
9035 bs = T_BC; /* "backspace character (old) */
9036 if (*bs)
9037 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9038 else
9039 cost = 999;
9040 if (col + 1 < cost) /* using CR is less characters */
9041 {
9042 plan = PLAN_CR;
9043 wouldbe_col = 0;
9044 cost = 1; /* CR is just one character */
9045 }
9046 else
9047 {
9048 plan = PLAN_LE;
9049 wouldbe_col = col;
9050 }
9051 if (noinvcurs) /* will stop highlighting */
9052 {
9053 cost += noinvcurs;
9054 attr = 0;
9055 }
9056 }
9057
9058 /*
9059 * If the cursor is above where we want to be, we can use CR LF.
9060 */
9061 else if (row > screen_cur_row)
9062 {
9063 plan = PLAN_NL;
9064 wouldbe_col = 0;
9065 cost = (row - screen_cur_row) * 2; /* CR LF */
9066 if (noinvcurs) /* will stop highlighting */
9067 {
9068 cost += noinvcurs;
9069 attr = 0;
9070 }
9071 }
9072
9073 /*
9074 * If the cursor is in the same row, smaller col, just use write.
9075 */
9076 else
9077 {
9078 plan = PLAN_WRITE;
9079 wouldbe_col = screen_cur_col;
9080 cost = 0;
9081 }
9082
9083 /*
9084 * Check if any characters that need to be written have the
9085 * correct attributes. Also avoid UTF-8 characters.
9086 */
9087 i = col - wouldbe_col;
9088 if (i > 0)
9089 cost += i;
9090 if (cost < goto_cost && i > 0)
9091 {
9092 /*
9093 * Check if the attributes are correct without additionally
9094 * stopping highlighting.
9095 */
9096 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9097 while (i && *p++ == attr)
9098 --i;
9099 if (i != 0)
9100 {
9101 /*
9102 * Try if it works when highlighting is stopped here.
9103 */
9104 if (*--p == 0)
9105 {
9106 cost += noinvcurs;
9107 while (i && *p++ == 0)
9108 --i;
9109 }
9110 if (i != 0)
9111 cost = 999; /* different attributes, don't do it */
9112 }
9113#ifdef FEAT_MBYTE
9114 if (enc_utf8)
9115 {
9116 /* Don't use an UTF-8 char for positioning, it's slow. */
9117 for (i = wouldbe_col; i < col; ++i)
9118 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9119 {
9120 cost = 999;
9121 break;
9122 }
9123 }
9124#endif
9125 }
9126
9127 /*
9128 * We can do it without term_windgoto()!
9129 */
9130 if (cost < goto_cost)
9131 {
9132 if (plan == PLAN_LE)
9133 {
9134 if (noinvcurs)
9135 screen_stop_highlight();
9136 while (screen_cur_col > col)
9137 {
9138 out_str(bs);
9139 --screen_cur_col;
9140 }
9141 }
9142 else if (plan == PLAN_CR)
9143 {
9144 if (noinvcurs)
9145 screen_stop_highlight();
9146 out_char('\r');
9147 screen_cur_col = 0;
9148 }
9149 else if (plan == PLAN_NL)
9150 {
9151 if (noinvcurs)
9152 screen_stop_highlight();
9153 while (screen_cur_row < row)
9154 {
9155 out_char('\n');
9156 ++screen_cur_row;
9157 }
9158 screen_cur_col = 0;
9159 }
9160
9161 i = col - screen_cur_col;
9162 if (i > 0)
9163 {
9164 /*
9165 * Use cursor-right if it's one character only. Avoids
9166 * removing a line of pixels from the last bold char, when
9167 * using the bold trick in the GUI.
9168 */
9169 if (T_ND[0] != NUL && T_ND[1] == NUL)
9170 {
9171 while (i-- > 0)
9172 out_char(*T_ND);
9173 }
9174 else
9175 {
9176 int off;
9177
9178 off = LineOffset[row] + screen_cur_col;
9179 while (i-- > 0)
9180 {
9181 if (ScreenAttrs[off] != screen_attr)
9182 screen_stop_highlight();
9183#ifdef FEAT_MBYTE
9184 out_flush_check();
9185#endif
9186 out_char(ScreenLines[off]);
9187#ifdef FEAT_MBYTE
9188 if (enc_dbcs == DBCS_JPNU
9189 && ScreenLines[off] == 0x8e)
9190 out_char(ScreenLines2[off]);
9191#endif
9192 ++off;
9193 }
9194 }
9195 }
9196 }
9197 }
9198 else
9199 cost = 999;
9200
9201 if (cost >= goto_cost)
9202 {
9203 if (noinvcurs)
9204 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009205 if (row == screen_cur_row && (col > screen_cur_col)
9206 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 term_cursor_right(col - screen_cur_col);
9208 else
9209 term_windgoto(row, col);
9210 }
9211 screen_cur_row = row;
9212 screen_cur_col = col;
9213 }
9214}
9215
9216/*
9217 * Set cursor to its position in the current window.
9218 */
9219 void
9220setcursor()
9221{
9222 if (redrawing())
9223 {
9224 validate_cursor();
9225 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9226 W_WINCOL(curwin) + (
9227#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009228 /* With 'rightleft' set and the cursor on a double-wide
9229 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9231# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009232 (has_mbyte
9233 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9234 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235# endif
9236 1)) :
9237#endif
9238 curwin->w_wcol));
9239 }
9240}
9241
9242
9243/*
9244 * insert 'line_count' lines at 'row' in window 'wp'
9245 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9246 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9247 * scrolling.
9248 * Returns FAIL if the lines are not inserted, OK for success.
9249 */
9250 int
9251win_ins_lines(wp, row, line_count, invalid, mayclear)
9252 win_T *wp;
9253 int row;
9254 int line_count;
9255 int invalid;
9256 int mayclear;
9257{
9258 int did_delete;
9259 int nextrow;
9260 int lastrow;
9261 int retval;
9262
9263 if (invalid)
9264 wp->w_lines_valid = 0;
9265
9266 if (wp->w_height < 5)
9267 return FAIL;
9268
9269 if (line_count > wp->w_height - row)
9270 line_count = wp->w_height - row;
9271
9272 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9273 if (retval != MAYBE)
9274 return retval;
9275
9276 /*
9277 * If there is a next window or a status line, we first try to delete the
9278 * lines at the bottom to avoid messing what is after the window.
9279 * If this fails and there are following windows, don't do anything to avoid
9280 * messing up those windows, better just redraw.
9281 */
9282 did_delete = FALSE;
9283#ifdef FEAT_WINDOWS
9284 if (wp->w_next != NULL || wp->w_status_height)
9285 {
9286 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9287 line_count, (int)Rows, FALSE, NULL) == OK)
9288 did_delete = TRUE;
9289 else if (wp->w_next)
9290 return FAIL;
9291 }
9292#endif
9293 /*
9294 * if no lines deleted, blank the lines that will end up below the window
9295 */
9296 if (!did_delete)
9297 {
9298#ifdef FEAT_WINDOWS
9299 wp->w_redr_status = TRUE;
9300#endif
9301 redraw_cmdline = TRUE;
9302 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9303 lastrow = nextrow + line_count;
9304 if (lastrow > Rows)
9305 lastrow = Rows;
9306 screen_fill(nextrow - line_count, lastrow - line_count,
9307 W_WINCOL(wp), (int)W_ENDCOL(wp),
9308 ' ', ' ', 0);
9309 }
9310
9311 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9312 == FAIL)
9313 {
9314 /* deletion will have messed up other windows */
9315 if (did_delete)
9316 {
9317#ifdef FEAT_WINDOWS
9318 wp->w_redr_status = TRUE;
9319#endif
9320 win_rest_invalid(W_NEXT(wp));
9321 }
9322 return FAIL;
9323 }
9324
9325 return OK;
9326}
9327
9328/*
9329 * delete "line_count" window lines at "row" in window "wp"
9330 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9331 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9332 * scrolling
9333 * Return OK for success, FAIL if the lines are not deleted.
9334 */
9335 int
9336win_del_lines(wp, row, line_count, invalid, mayclear)
9337 win_T *wp;
9338 int row;
9339 int line_count;
9340 int invalid;
9341 int mayclear;
9342{
9343 int retval;
9344
9345 if (invalid)
9346 wp->w_lines_valid = 0;
9347
9348 if (line_count > wp->w_height - row)
9349 line_count = wp->w_height - row;
9350
9351 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9352 if (retval != MAYBE)
9353 return retval;
9354
9355 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9356 (int)Rows, FALSE, NULL) == FAIL)
9357 return FAIL;
9358
9359#ifdef FEAT_WINDOWS
9360 /*
9361 * If there are windows or status lines below, try to put them at the
9362 * correct place. If we can't do that, they have to be redrawn.
9363 */
9364 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9365 {
9366 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9367 line_count, (int)Rows, NULL) == FAIL)
9368 {
9369 wp->w_redr_status = TRUE;
9370 win_rest_invalid(wp->w_next);
9371 }
9372 }
9373 /*
9374 * If this is the last window and there is no status line, redraw the
9375 * command line later.
9376 */
9377 else
9378#endif
9379 redraw_cmdline = TRUE;
9380 return OK;
9381}
9382
9383/*
9384 * Common code for win_ins_lines() and win_del_lines().
9385 * Returns OK or FAIL when the work has been done.
9386 * Returns MAYBE when not finished yet.
9387 */
9388 static int
9389win_do_lines(wp, row, line_count, mayclear, del)
9390 win_T *wp;
9391 int row;
9392 int line_count;
9393 int mayclear;
9394 int del;
9395{
9396 int retval;
9397
9398 if (!redrawing() || line_count <= 0)
9399 return FAIL;
9400
9401 /* only a few lines left: redraw is faster */
9402 if (mayclear && Rows - line_count < 5
9403#ifdef FEAT_VERTSPLIT
9404 && wp->w_width == Columns
9405#endif
9406 )
9407 {
9408 screenclear(); /* will set wp->w_lines_valid to 0 */
9409 return FAIL;
9410 }
9411
9412 /*
9413 * Delete all remaining lines
9414 */
9415 if (row + line_count >= wp->w_height)
9416 {
9417 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9418 W_WINCOL(wp), (int)W_ENDCOL(wp),
9419 ' ', ' ', 0);
9420 return OK;
9421 }
9422
9423 /*
9424 * when scrolling, the message on the command line should be cleared,
9425 * otherwise it will stay there forever.
9426 */
9427 clear_cmdline = TRUE;
9428
9429 /*
9430 * If the terminal can set a scroll region, use that.
9431 * Always do this in a vertically split window. This will redraw from
9432 * ScreenLines[] when t_CV isn't defined. That's faster than using
9433 * win_line().
9434 * Don't use a scroll region when we are going to redraw the text, writing
9435 * a character in the lower right corner of the scroll region causes a
9436 * scroll-up in the DJGPP version.
9437 */
9438 if (scroll_region
9439#ifdef FEAT_VERTSPLIT
9440 || W_WIDTH(wp) != Columns
9441#endif
9442 )
9443 {
9444#ifdef FEAT_VERTSPLIT
9445 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9446#endif
9447 scroll_region_set(wp, row);
9448 if (del)
9449 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9450 wp->w_height - row, FALSE, wp);
9451 else
9452 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9453 wp->w_height - row, wp);
9454#ifdef FEAT_VERTSPLIT
9455 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9456#endif
9457 scroll_region_reset();
9458 return retval;
9459 }
9460
9461#ifdef FEAT_WINDOWS
9462 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9463 return FAIL;
9464#endif
9465
9466 return MAYBE;
9467}
9468
9469/*
9470 * window 'wp' and everything after it is messed up, mark it for redraw
9471 */
9472 static void
9473win_rest_invalid(wp)
9474 win_T *wp;
9475{
9476#ifdef FEAT_WINDOWS
9477 while (wp != NULL)
9478#else
9479 if (wp != NULL)
9480#endif
9481 {
9482 redraw_win_later(wp, NOT_VALID);
9483#ifdef FEAT_WINDOWS
9484 wp->w_redr_status = TRUE;
9485 wp = wp->w_next;
9486#endif
9487 }
9488 redraw_cmdline = TRUE;
9489}
9490
9491/*
9492 * The rest of the routines in this file perform screen manipulations. The
9493 * given operation is performed physically on the screen. The corresponding
9494 * change is also made to the internal screen image. In this way, the editor
9495 * anticipates the effect of editing changes on the appearance of the screen.
9496 * That way, when we call screenupdate a complete redraw isn't usually
9497 * necessary. Another advantage is that we can keep adding code to anticipate
9498 * screen changes, and in the meantime, everything still works.
9499 */
9500
9501/*
9502 * types for inserting or deleting lines
9503 */
9504#define USE_T_CAL 1
9505#define USE_T_CDL 2
9506#define USE_T_AL 3
9507#define USE_T_CE 4
9508#define USE_T_DL 5
9509#define USE_T_SR 6
9510#define USE_NL 7
9511#define USE_T_CD 8
9512#define USE_REDRAW 9
9513
9514/*
9515 * insert lines on the screen and update ScreenLines[]
9516 * 'end' is the line after the scrolled part. Normally it is Rows.
9517 * When scrolling region used 'off' is the offset from the top for the region.
9518 * 'row' and 'end' are relative to the start of the region.
9519 *
9520 * return FAIL for failure, OK for success.
9521 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009522 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523screen_ins_lines(off, row, line_count, end, wp)
9524 int off;
9525 int row;
9526 int line_count;
9527 int end;
9528 win_T *wp; /* NULL or window to use width from */
9529{
9530 int i;
9531 int j;
9532 unsigned temp;
9533 int cursor_row;
9534 int type;
9535 int result_empty;
9536 int can_ce = can_clear(T_CE);
9537
9538 /*
9539 * FAIL if
9540 * - there is no valid screen
9541 * - the screen has to be redrawn completely
9542 * - the line count is less than one
9543 * - the line count is more than 'ttyscroll'
9544 */
9545 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9546 return FAIL;
9547
9548 /*
9549 * There are seven ways to insert lines:
9550 * 0. When in a vertically split window and t_CV isn't set, redraw the
9551 * characters from ScreenLines[].
9552 * 1. Use T_CD (clear to end of display) if it exists and the result of
9553 * the insert is just empty lines
9554 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9555 * present or line_count > 1. It looks better if we do all the inserts
9556 * at once.
9557 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9558 * insert is just empty lines and T_CE is not present or line_count >
9559 * 1.
9560 * 4. Use T_AL (insert line) if it exists.
9561 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9562 * just empty lines.
9563 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9564 * just empty lines.
9565 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9566 * the 'da' flag is not set or we have clear line capability.
9567 * 8. redraw the characters from ScreenLines[].
9568 *
9569 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9570 * the scrollbar for the window. It does have insert line, use that if it
9571 * exists.
9572 */
9573 result_empty = (row + line_count >= end);
9574#ifdef FEAT_VERTSPLIT
9575 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9576 type = USE_REDRAW;
9577 else
9578#endif
9579 if (can_clear(T_CD) && result_empty)
9580 type = USE_T_CD;
9581 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9582 type = USE_T_CAL;
9583 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9584 type = USE_T_CDL;
9585 else if (*T_AL != NUL)
9586 type = USE_T_AL;
9587 else if (can_ce && result_empty)
9588 type = USE_T_CE;
9589 else if (*T_DL != NUL && result_empty)
9590 type = USE_T_DL;
9591 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9592 type = USE_T_SR;
9593 else
9594 return FAIL;
9595
9596 /*
9597 * For clearing the lines screen_del_lines() is used. This will also take
9598 * care of t_db if necessary.
9599 */
9600 if (type == USE_T_CD || type == USE_T_CDL ||
9601 type == USE_T_CE || type == USE_T_DL)
9602 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9603
9604 /*
9605 * If text is retained below the screen, first clear or delete as many
9606 * lines at the bottom of the window as are about to be inserted so that
9607 * the deleted lines won't later surface during a screen_del_lines.
9608 */
9609 if (*T_DB)
9610 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9611
9612#ifdef FEAT_CLIPBOARD
9613 /* Remove a modeless selection when inserting lines halfway the screen
9614 * or not the full width of the screen. */
9615 if (off + row > 0
9616# ifdef FEAT_VERTSPLIT
9617 || (wp != NULL && wp->w_width != Columns)
9618# endif
9619 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009620 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 else
9622 clip_scroll_selection(-line_count);
9623#endif
9624
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625#ifdef FEAT_GUI
9626 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9627 * scrolling is actually carried out. */
9628 gui_dont_update_cursor();
9629#endif
9630
9631 if (*T_CCS != NUL) /* cursor relative to region */
9632 cursor_row = row;
9633 else
9634 cursor_row = row + off;
9635
9636 /*
9637 * Shift LineOffset[] line_count down to reflect the inserted lines.
9638 * Clear the inserted lines in ScreenLines[].
9639 */
9640 row += off;
9641 end += off;
9642 for (i = 0; i < line_count; ++i)
9643 {
9644#ifdef FEAT_VERTSPLIT
9645 if (wp != NULL && wp->w_width != Columns)
9646 {
9647 /* need to copy part of a line */
9648 j = end - 1 - i;
9649 while ((j -= line_count) >= row)
9650 linecopy(j + line_count, j, wp);
9651 j += line_count;
9652 if (can_clear((char_u *)" "))
9653 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9654 else
9655 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9656 LineWraps[j] = FALSE;
9657 }
9658 else
9659#endif
9660 {
9661 j = end - 1 - i;
9662 temp = LineOffset[j];
9663 while ((j -= line_count) >= row)
9664 {
9665 LineOffset[j + line_count] = LineOffset[j];
9666 LineWraps[j + line_count] = LineWraps[j];
9667 }
9668 LineOffset[j + line_count] = temp;
9669 LineWraps[j + line_count] = FALSE;
9670 if (can_clear((char_u *)" "))
9671 lineclear(temp, (int)Columns);
9672 else
9673 lineinvalid(temp, (int)Columns);
9674 }
9675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676
9677 screen_stop_highlight();
9678 windgoto(cursor_row, 0);
9679
9680#ifdef FEAT_VERTSPLIT
9681 /* redraw the characters */
9682 if (type == USE_REDRAW)
9683 redraw_block(row, end, wp);
9684 else
9685#endif
9686 if (type == USE_T_CAL)
9687 {
9688 term_append_lines(line_count);
9689 screen_start(); /* don't know where cursor is now */
9690 }
9691 else
9692 {
9693 for (i = 0; i < line_count; i++)
9694 {
9695 if (type == USE_T_AL)
9696 {
9697 if (i && cursor_row != 0)
9698 windgoto(cursor_row, 0);
9699 out_str(T_AL);
9700 }
9701 else /* type == USE_T_SR */
9702 out_str(T_SR);
9703 screen_start(); /* don't know where cursor is now */
9704 }
9705 }
9706
9707 /*
9708 * With scroll-reverse and 'da' flag set we need to clear the lines that
9709 * have been scrolled down into the region.
9710 */
9711 if (type == USE_T_SR && *T_DA)
9712 {
9713 for (i = 0; i < line_count; ++i)
9714 {
9715 windgoto(off + i, 0);
9716 out_str(T_CE);
9717 screen_start(); /* don't know where cursor is now */
9718 }
9719 }
9720
9721#ifdef FEAT_GUI
9722 gui_can_update_cursor();
9723 if (gui.in_use)
9724 out_flush(); /* always flush after a scroll */
9725#endif
9726 return OK;
9727}
9728
9729/*
9730 * delete lines on the screen and update ScreenLines[]
9731 * 'end' is the line after the scrolled part. Normally it is Rows.
9732 * When scrolling region used 'off' is the offset from the top for the region.
9733 * 'row' and 'end' are relative to the start of the region.
9734 *
9735 * Return OK for success, FAIL if the lines are not deleted.
9736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 int
9738screen_del_lines(off, row, line_count, end, force, wp)
9739 int off;
9740 int row;
9741 int line_count;
9742 int end;
9743 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009744 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745{
9746 int j;
9747 int i;
9748 unsigned temp;
9749 int cursor_row;
9750 int cursor_end;
9751 int result_empty; /* result is empty until end of region */
9752 int can_delete; /* deleting line codes can be used */
9753 int type;
9754
9755 /*
9756 * FAIL if
9757 * - there is no valid screen
9758 * - the screen has to be redrawn completely
9759 * - the line count is less than one
9760 * - the line count is more than 'ttyscroll'
9761 */
9762 if (!screen_valid(TRUE) || line_count <= 0 ||
9763 (!force && line_count > p_ttyscroll))
9764 return FAIL;
9765
9766 /*
9767 * Check if the rest of the current region will become empty.
9768 */
9769 result_empty = row + line_count >= end;
9770
9771 /*
9772 * We can delete lines only when 'db' flag not set or when 'ce' option
9773 * available.
9774 */
9775 can_delete = (*T_DB == NUL || can_clear(T_CE));
9776
9777 /*
9778 * There are six ways to delete lines:
9779 * 0. When in a vertically split window and t_CV isn't set, redraw the
9780 * characters from ScreenLines[].
9781 * 1. Use T_CD if it exists and the result is empty.
9782 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9783 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9784 * none of the other ways work.
9785 * 4. Use T_CE (erase line) if the result is empty.
9786 * 5. Use T_DL (delete line) if it exists.
9787 * 6. redraw the characters from ScreenLines[].
9788 */
9789#ifdef FEAT_VERTSPLIT
9790 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9791 type = USE_REDRAW;
9792 else
9793#endif
9794 if (can_clear(T_CD) && result_empty)
9795 type = USE_T_CD;
9796#if defined(__BEOS__) && defined(BEOS_DR8)
9797 /*
9798 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9799 * its internal termcap... this works okay for tests which test *T_DB !=
9800 * NUL. It has the disadvantage that the user cannot use any :set t_*
9801 * command to get T_DB (back) to empty_option, only :set term=... will do
9802 * the trick...
9803 * Anyway, this hack will hopefully go away with the next OS release.
9804 * (Olaf Seibert)
9805 */
9806 else if (row == 0 && T_DB == empty_option
9807 && (line_count == 1 || *T_CDL == NUL))
9808#else
9809 else if (row == 0 && (
9810#ifndef AMIGA
9811 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9812 * up, so use delete-line command */
9813 line_count == 1 ||
9814#endif
9815 *T_CDL == NUL))
9816#endif
9817 type = USE_NL;
9818 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9819 type = USE_T_CDL;
9820 else if (can_clear(T_CE) && result_empty
9821#ifdef FEAT_VERTSPLIT
9822 && (wp == NULL || wp->w_width == Columns)
9823#endif
9824 )
9825 type = USE_T_CE;
9826 else if (*T_DL != NUL && can_delete)
9827 type = USE_T_DL;
9828 else if (*T_CDL != NUL && can_delete)
9829 type = USE_T_CDL;
9830 else
9831 return FAIL;
9832
9833#ifdef FEAT_CLIPBOARD
9834 /* Remove a modeless selection when deleting lines halfway the screen or
9835 * not the full width of the screen. */
9836 if (off + row > 0
9837# ifdef FEAT_VERTSPLIT
9838 || (wp != NULL && wp->w_width != Columns)
9839# endif
9840 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009841 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 else
9843 clip_scroll_selection(line_count);
9844#endif
9845
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846#ifdef FEAT_GUI
9847 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9848 * scrolling is actually carried out. */
9849 gui_dont_update_cursor();
9850#endif
9851
9852 if (*T_CCS != NUL) /* cursor relative to region */
9853 {
9854 cursor_row = row;
9855 cursor_end = end;
9856 }
9857 else
9858 {
9859 cursor_row = row + off;
9860 cursor_end = end + off;
9861 }
9862
9863 /*
9864 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9865 * Clear the inserted lines in ScreenLines[].
9866 */
9867 row += off;
9868 end += off;
9869 for (i = 0; i < line_count; ++i)
9870 {
9871#ifdef FEAT_VERTSPLIT
9872 if (wp != NULL && wp->w_width != Columns)
9873 {
9874 /* need to copy part of a line */
9875 j = row + i;
9876 while ((j += line_count) <= end - 1)
9877 linecopy(j - line_count, j, wp);
9878 j -= line_count;
9879 if (can_clear((char_u *)" "))
9880 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9881 else
9882 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9883 LineWraps[j] = FALSE;
9884 }
9885 else
9886#endif
9887 {
9888 /* whole width, moving the line pointers is faster */
9889 j = row + i;
9890 temp = LineOffset[j];
9891 while ((j += line_count) <= end - 1)
9892 {
9893 LineOffset[j - line_count] = LineOffset[j];
9894 LineWraps[j - line_count] = LineWraps[j];
9895 }
9896 LineOffset[j - line_count] = temp;
9897 LineWraps[j - line_count] = FALSE;
9898 if (can_clear((char_u *)" "))
9899 lineclear(temp, (int)Columns);
9900 else
9901 lineinvalid(temp, (int)Columns);
9902 }
9903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
9905 screen_stop_highlight();
9906
9907#ifdef FEAT_VERTSPLIT
9908 /* redraw the characters */
9909 if (type == USE_REDRAW)
9910 redraw_block(row, end, wp);
9911 else
9912#endif
9913 if (type == USE_T_CD) /* delete the lines */
9914 {
9915 windgoto(cursor_row, 0);
9916 out_str(T_CD);
9917 screen_start(); /* don't know where cursor is now */
9918 }
9919 else if (type == USE_T_CDL)
9920 {
9921 windgoto(cursor_row, 0);
9922 term_delete_lines(line_count);
9923 screen_start(); /* don't know where cursor is now */
9924 }
9925 /*
9926 * Deleting lines at top of the screen or scroll region: Just scroll
9927 * the whole screen (scroll region) up by outputting newlines on the
9928 * last line.
9929 */
9930 else if (type == USE_NL)
9931 {
9932 windgoto(cursor_end - 1, 0);
9933 for (i = line_count; --i >= 0; )
9934 out_char('\n'); /* cursor will remain on same line */
9935 }
9936 else
9937 {
9938 for (i = line_count; --i >= 0; )
9939 {
9940 if (type == USE_T_DL)
9941 {
9942 windgoto(cursor_row, 0);
9943 out_str(T_DL); /* delete a line */
9944 }
9945 else /* type == USE_T_CE */
9946 {
9947 windgoto(cursor_row + i, 0);
9948 out_str(T_CE); /* erase a line */
9949 }
9950 screen_start(); /* don't know where cursor is now */
9951 }
9952 }
9953
9954 /*
9955 * If the 'db' flag is set, we need to clear the lines that have been
9956 * scrolled up at the bottom of the region.
9957 */
9958 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9959 {
9960 for (i = line_count; i > 0; --i)
9961 {
9962 windgoto(cursor_end - i, 0);
9963 out_str(T_CE); /* erase a line */
9964 screen_start(); /* don't know where cursor is now */
9965 }
9966 }
9967
9968#ifdef FEAT_GUI
9969 gui_can_update_cursor();
9970 if (gui.in_use)
9971 out_flush(); /* always flush after a scroll */
9972#endif
9973
9974 return OK;
9975}
9976
9977/*
9978 * show the current mode and ruler
9979 *
9980 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9981 * If clear_cmdline is FALSE there may be a message there that needs to be
9982 * cleared only if a mode is shown.
9983 * Return the length of the message (0 if no message).
9984 */
9985 int
9986showmode()
9987{
9988 int need_clear;
9989 int length = 0;
9990 int do_mode;
9991 int attr;
9992 int nwr_save;
9993#ifdef FEAT_INS_EXPAND
9994 int sub_attr;
9995#endif
9996
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009997 do_mode = ((p_smd && msg_silent == 0)
9998 && ((State & INSERT)
9999 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010000 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 if (do_mode || Recording)
10002 {
10003 /*
10004 * Don't show mode right now, when not redrawing or inside a mapping.
10005 * Call char_avail() only when we are going to show something, because
10006 * it takes a bit of time.
10007 */
10008 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10009 {
10010 redraw_cmdline = TRUE; /* show mode later */
10011 return 0;
10012 }
10013
10014 nwr_save = need_wait_return;
10015
10016 /* wait a bit before overwriting an important message */
10017 check_for_delay(FALSE);
10018
10019 /* if the cmdline is more than one line high, erase top lines */
10020 need_clear = clear_cmdline;
10021 if (clear_cmdline && cmdline_row < Rows - 1)
10022 msg_clr_cmdline(); /* will reset clear_cmdline */
10023
10024 /* Position on the last line in the window, column 0 */
10025 msg_pos_mode();
10026 cursor_off();
10027 attr = hl_attr(HLF_CM); /* Highlight mode */
10028 if (do_mode)
10029 {
10030 MSG_PUTS_ATTR("--", attr);
10031#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010032 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010033# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010034 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010035# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010036 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010037# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010038 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010039# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 MSG_PUTS_ATTR(" IM", attr);
10041# else
10042 MSG_PUTS_ATTR(" XIM", attr);
10043# endif
10044#endif
10045#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10046 if (gui.in_use)
10047 {
10048 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010049 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 }
10051#endif
10052#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010053 /* CTRL-X in Insert mode */
10054 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 {
10056 /* These messages can get long, avoid a wrap in a narrow
10057 * window. Prefer showing edit_submode_extra. */
10058 length = (Rows - msg_row) * Columns - 3;
10059 if (edit_submode_extra != NULL)
10060 length -= vim_strsize(edit_submode_extra);
10061 if (length > 0)
10062 {
10063 if (edit_submode_pre != NULL)
10064 length -= vim_strsize(edit_submode_pre);
10065 if (length - vim_strsize(edit_submode) > 0)
10066 {
10067 if (edit_submode_pre != NULL)
10068 msg_puts_attr(edit_submode_pre, attr);
10069 msg_puts_attr(edit_submode, attr);
10070 }
10071 if (edit_submode_extra != NULL)
10072 {
10073 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10074 if ((int)edit_submode_highl < (int)HLF_COUNT)
10075 sub_attr = hl_attr(edit_submode_highl);
10076 else
10077 sub_attr = attr;
10078 msg_puts_attr(edit_submode_extra, sub_attr);
10079 }
10080 }
10081 length = 0;
10082 }
10083 else
10084#endif
10085 {
10086#ifdef FEAT_VREPLACE
10087 if (State & VREPLACE_FLAG)
10088 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10089 else
10090#endif
10091 if (State & REPLACE_FLAG)
10092 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10093 else if (State & INSERT)
10094 {
10095#ifdef FEAT_RIGHTLEFT
10096 if (p_ri)
10097 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10098#endif
10099 MSG_PUTS_ATTR(_(" INSERT"), attr);
10100 }
10101 else if (restart_edit == 'I')
10102 MSG_PUTS_ATTR(_(" (insert)"), attr);
10103 else if (restart_edit == 'R')
10104 MSG_PUTS_ATTR(_(" (replace)"), attr);
10105 else if (restart_edit == 'V')
10106 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10107#ifdef FEAT_RIGHTLEFT
10108 if (p_hkmap)
10109 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10110# ifdef FEAT_FKMAP
10111 if (p_fkmap)
10112 MSG_PUTS_ATTR(farsi_text_5, attr);
10113# endif
10114#endif
10115#ifdef FEAT_KEYMAP
10116 if (State & LANGMAP)
10117 {
10118# ifdef FEAT_ARABIC
10119 if (curwin->w_p_arab)
10120 MSG_PUTS_ATTR(_(" Arabic"), attr);
10121 else
10122# endif
10123 MSG_PUTS_ATTR(_(" (lang)"), attr);
10124 }
10125#endif
10126 if ((State & INSERT) && p_paste)
10127 MSG_PUTS_ATTR(_(" (paste)"), attr);
10128
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 if (VIsual_active)
10130 {
10131 char *p;
10132
10133 /* Don't concatenate separate words to avoid translation
10134 * problems. */
10135 switch ((VIsual_select ? 4 : 0)
10136 + (VIsual_mode == Ctrl_V) * 2
10137 + (VIsual_mode == 'V'))
10138 {
10139 case 0: p = N_(" VISUAL"); break;
10140 case 1: p = N_(" VISUAL LINE"); break;
10141 case 2: p = N_(" VISUAL BLOCK"); break;
10142 case 4: p = N_(" SELECT"); break;
10143 case 5: p = N_(" SELECT LINE"); break;
10144 default: p = N_(" SELECT BLOCK"); break;
10145 }
10146 MSG_PUTS_ATTR(_(p), attr);
10147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 MSG_PUTS_ATTR(" --", attr);
10149 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010150
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 need_clear = TRUE;
10152 }
10153 if (Recording
10154#ifdef FEAT_INS_EXPAND
10155 && edit_submode == NULL /* otherwise it gets too long */
10156#endif
10157 )
10158 {
10159 MSG_PUTS_ATTR(_("recording"), attr);
10160 need_clear = TRUE;
10161 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010162
10163 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 if (need_clear || clear_cmdline)
10165 msg_clr_eos();
10166 msg_didout = FALSE; /* overwrite this message */
10167 length = msg_col;
10168 msg_col = 0;
10169 need_wait_return = nwr_save; /* never ask for hit-return for this */
10170 }
10171 else if (clear_cmdline && msg_silent == 0)
10172 /* Clear the whole command line. Will reset "clear_cmdline". */
10173 msg_clr_cmdline();
10174
10175#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 /* In Visual mode the size of the selected area must be redrawn. */
10177 if (VIsual_active)
10178 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179
10180 /* If the last window has no status line, the ruler is after the mode
10181 * message and must be redrawn */
10182 if (redrawing()
10183# ifdef FEAT_WINDOWS
10184 && lastwin->w_status_height == 0
10185# endif
10186 )
10187 win_redr_ruler(lastwin, TRUE);
10188#endif
10189 redraw_cmdline = FALSE;
10190 clear_cmdline = FALSE;
10191
10192 return length;
10193}
10194
10195/*
10196 * Position for a mode message.
10197 */
10198 static void
10199msg_pos_mode()
10200{
10201 msg_col = 0;
10202 msg_row = Rows - 1;
10203}
10204
10205/*
10206 * Delete mode message. Used when ESC is typed which is expected to end
10207 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010208 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 */
10210 void
10211unshowmode(force)
10212 int force;
10213{
10214 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010215 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 */
10217 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10218 redraw_cmdline = TRUE; /* delete mode later */
10219 else
10220 {
10221 msg_pos_mode();
10222 if (Recording)
10223 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
10224 msg_clr_eos();
10225 }
10226}
10227
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010228#if defined(FEAT_WINDOWS)
10229/*
10230 * Draw the tab pages line at the top of the Vim window.
10231 */
10232 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010233draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010234{
10235 int tabcount = 0;
10236 tabpage_T *tp;
10237 int tabwidth;
10238 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010239 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010240 int attr;
10241 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010242 win_T *cwp;
10243 int wincount;
10244 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010245 int c;
10246 int len;
10247 int attr_sel = hl_attr(HLF_TPS);
10248 int attr_nosel = hl_attr(HLF_TP);
10249 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010250 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010251 int room;
10252 int use_sep_chars = (t_colors < 8
10253#ifdef FEAT_GUI
10254 && !gui.in_use
10255#endif
10256 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010257
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010258 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010259
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010260#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010261 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010262 if (gui_use_tabline())
10263 {
10264 gui_update_tabline();
10265 return;
10266 }
10267#endif
10268
10269 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010270 return;
10271
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010272#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010273
10274 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10275 for (scol = 0; scol < Columns; ++scol)
10276 TabPageIdxs[scol] = 0;
10277
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010278 /* Use the 'tabline' option if it's set. */
10279 if (*p_tal != NUL)
10280 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010281 int save_called_emsg = called_emsg;
10282
10283 /* Check for an error. If there is one we would loop in redrawing the
10284 * screen. Avoid that by making 'tabline' empty. */
10285 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010286 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010287 if (called_emsg)
10288 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010289 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010290 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010291 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010292 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010293#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010294 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010295 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10296 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010297
Bram Moolenaar238a5642006-02-21 22:12:05 +000010298 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10299 if (tabwidth < 6)
10300 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010301
Bram Moolenaar238a5642006-02-21 22:12:05 +000010302 attr = attr_nosel;
10303 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010304 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010305 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10306 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010307 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010308 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010309
Bram Moolenaar238a5642006-02-21 22:12:05 +000010310 if (tp->tp_topframe == topframe)
10311 attr = attr_sel;
10312 if (use_sep_chars && col > 0)
10313 screen_putchar('|', 0, col++, attr);
10314
10315 if (tp->tp_topframe != topframe)
10316 attr = attr_nosel;
10317
10318 screen_putchar(' ', 0, col++, attr);
10319
10320 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010321 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010322 cwp = curwin;
10323 wp = firstwin;
10324 }
10325 else
10326 {
10327 cwp = tp->tp_curwin;
10328 wp = tp->tp_firstwin;
10329 }
10330
10331 modified = FALSE;
10332 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10333 if (bufIsChanged(wp->w_buffer))
10334 modified = TRUE;
10335 if (modified || wincount > 1)
10336 {
10337 if (wincount > 1)
10338 {
10339 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010340 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010341 if (col + len >= Columns - 3)
10342 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010343 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010344#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010345 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010346#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010347 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010348#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010349 );
10350 col += len;
10351 }
10352 if (modified)
10353 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10354 screen_putchar(' ', 0, col++, attr);
10355 }
10356
10357 room = scol - col + tabwidth - 1;
10358 if (room > 0)
10359 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010360 /* Get buffer name in NameBuff[] */
10361 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010362 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010363 len = vim_strsize(NameBuff);
10364 p = NameBuff;
10365#ifdef FEAT_MBYTE
10366 if (has_mbyte)
10367 while (len > room)
10368 {
10369 len -= ptr2cells(p);
10370 mb_ptr_adv(p);
10371 }
10372 else
10373#endif
10374 if (len > room)
10375 {
10376 p += len - room;
10377 len = room;
10378 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010379 if (len > Columns - col - 1)
10380 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010381
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010382 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010383 col += len;
10384 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010385 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010386
10387 /* Store the tab page number in TabPageIdxs[], so that
10388 * jump_to_mouse() knows where each one is. */
10389 ++tabcount;
10390 while (scol < col)
10391 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010392 }
10393
Bram Moolenaar238a5642006-02-21 22:12:05 +000010394 if (use_sep_chars)
10395 c = '_';
10396 else
10397 c = ' ';
10398 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010399
10400 /* Put an "X" for closing the current tab if there are several. */
10401 if (first_tabpage->tp_next != NULL)
10402 {
10403 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10404 TabPageIdxs[Columns - 1] = -999;
10405 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010406 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010407
10408 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10409 * set. */
10410 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010411}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010412
10413/*
10414 * Get buffer name for "buf" into NameBuff[].
10415 * Takes care of special buffer names and translates special characters.
10416 */
10417 void
10418get_trans_bufname(buf)
10419 buf_T *buf;
10420{
10421 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010422 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010423 else
10424 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10425 trans_characters(NameBuff, MAXPATHL);
10426}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010427#endif
10428
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10430/*
10431 * Get the character to use in a status line. Get its attributes in "*attr".
10432 */
10433 static int
10434fillchar_status(attr, is_curwin)
10435 int *attr;
10436 int is_curwin;
10437{
10438 int fill;
10439 if (is_curwin)
10440 {
10441 *attr = hl_attr(HLF_S);
10442 fill = fill_stl;
10443 }
10444 else
10445 {
10446 *attr = hl_attr(HLF_SNC);
10447 fill = fill_stlnc;
10448 }
10449 /* Use fill when there is highlighting, and highlighting of current
10450 * window differs, or the fillchars differ, or this is not the
10451 * current window */
10452 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10453 || !is_curwin || firstwin == lastwin)
10454 || (fill_stl != fill_stlnc)))
10455 return fill;
10456 if (is_curwin)
10457 return '^';
10458 return '=';
10459}
10460#endif
10461
10462#ifdef FEAT_VERTSPLIT
10463/*
10464 * Get the character to use in a separator between vertically split windows.
10465 * Get its attributes in "*attr".
10466 */
10467 static int
10468fillchar_vsep(attr)
10469 int *attr;
10470{
10471 *attr = hl_attr(HLF_C);
10472 if (*attr == 0 && fill_vert == ' ')
10473 return '|';
10474 else
10475 return fill_vert;
10476}
10477#endif
10478
10479/*
10480 * Return TRUE if redrawing should currently be done.
10481 */
10482 int
10483redrawing()
10484{
10485 return (!RedrawingDisabled
10486 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10487}
10488
10489/*
10490 * Return TRUE if printing messages should currently be done.
10491 */
10492 int
10493messaging()
10494{
10495 return (!(p_lz && char_avail() && !KeyTyped));
10496}
10497
10498/*
10499 * Show current status info in ruler and various other places
10500 * If always is FALSE, only show ruler if position has changed.
10501 */
10502 void
10503showruler(always)
10504 int always;
10505{
10506 if (!always && !redrawing())
10507 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010508#ifdef FEAT_INS_EXPAND
10509 if (pum_visible())
10510 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010511# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010512 /* Don't redraw right now, do it later. */
10513 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010514# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010515 return;
10516 }
10517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010519 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010520 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010521 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 else
10524#endif
10525#ifdef FEAT_CMDL_INFO
10526 win_redr_ruler(curwin, always);
10527#endif
10528
10529#ifdef FEAT_TITLE
10530 if (need_maketitle
10531# ifdef FEAT_STL_OPT
10532 || (p_icon && (stl_syntax & STL_IN_ICON))
10533 || (p_title && (stl_syntax & STL_IN_TITLE))
10534# endif
10535 )
10536 maketitle();
10537#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010538#ifdef FEAT_WINDOWS
10539 /* Redraw the tab pages line if needed. */
10540 if (redraw_tabline)
10541 draw_tabline();
10542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543}
10544
10545#ifdef FEAT_CMDL_INFO
10546 static void
10547win_redr_ruler(wp, always)
10548 win_T *wp;
10549 int always;
10550{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010551#define RULER_BUF_LEN 70
10552 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 int row;
10554 int fillchar;
10555 int attr;
10556 int empty_line = FALSE;
10557 colnr_T virtcol;
10558 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010559 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 int o;
10561#ifdef FEAT_VERTSPLIT
10562 int this_ru_col;
10563 int off = 0;
10564 int width = Columns;
10565# define WITH_OFF(x) x
10566# define WITH_WIDTH(x) x
10567#else
10568# define WITH_OFF(x) 0
10569# define WITH_WIDTH(x) Columns
10570# define this_ru_col ru_col
10571#endif
10572
10573 /* If 'ruler' off or redrawing disabled, don't do anything */
10574 if (!p_ru)
10575 return;
10576
10577 /*
10578 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10579 * after deleting lines, before cursor.lnum is corrected.
10580 */
10581 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10582 return;
10583
10584#ifdef FEAT_INS_EXPAND
10585 /* Don't draw the ruler while doing insert-completion, it might overwrite
10586 * the (long) mode message. */
10587# ifdef FEAT_WINDOWS
10588 if (wp == lastwin && lastwin->w_status_height == 0)
10589# endif
10590 if (edit_submode != NULL)
10591 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010592 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10593 if (pum_visible())
10594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#endif
10596
10597#ifdef FEAT_STL_OPT
10598 if (*p_ruf)
10599 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010600 int save_called_emsg = called_emsg;
10601
10602 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010604 if (called_emsg)
10605 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010606 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010607 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 return;
10609 }
10610#endif
10611
10612 /*
10613 * Check if not in Insert mode and the line is empty (will show "0-1").
10614 */
10615 if (!(State & INSERT)
10616 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10617 empty_line = TRUE;
10618
10619 /*
10620 * Only draw the ruler when something changed.
10621 */
10622 validate_virtcol_win(wp);
10623 if ( redraw_cmdline
10624 || always
10625 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10626 || wp->w_cursor.col != wp->w_ru_cursor.col
10627 || wp->w_virtcol != wp->w_ru_virtcol
10628#ifdef FEAT_VIRTUALEDIT
10629 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10630#endif
10631 || wp->w_topline != wp->w_ru_topline
10632 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10633#ifdef FEAT_DIFF
10634 || wp->w_topfill != wp->w_ru_topfill
10635#endif
10636 || empty_line != wp->w_ru_empty)
10637 {
10638 cursor_off();
10639#ifdef FEAT_WINDOWS
10640 if (wp->w_status_height)
10641 {
10642 row = W_WINROW(wp) + wp->w_height;
10643 fillchar = fillchar_status(&attr, wp == curwin);
10644# ifdef FEAT_VERTSPLIT
10645 off = W_WINCOL(wp);
10646 width = W_WIDTH(wp);
10647# endif
10648 }
10649 else
10650#endif
10651 {
10652 row = Rows - 1;
10653 fillchar = ' ';
10654 attr = 0;
10655#ifdef FEAT_VERTSPLIT
10656 width = Columns;
10657 off = 0;
10658#endif
10659 }
10660
10661 /* In list mode virtcol needs to be recomputed */
10662 virtcol = wp->w_virtcol;
10663 if (wp->w_p_list && lcs_tab1 == NUL)
10664 {
10665 wp->w_p_list = FALSE;
10666 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10667 wp->w_p_list = TRUE;
10668 }
10669
10670 /*
10671 * Some sprintfs return the length, some return a pointer.
10672 * To avoid portability problems we use strlen() here.
10673 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010674 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10676 ? 0L
10677 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010678 len = STRLEN(buffer);
10679 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10681 (int)virtcol + 1);
10682
10683 /*
10684 * Add a "50%" if there is room for it.
10685 * On the last line, don't print in the last column (scrolls the
10686 * screen up on some terminals).
10687 */
10688 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010689 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 o = i + vim_strsize(buffer + i + 1);
10691#ifdef FEAT_WINDOWS
10692 if (wp->w_status_height == 0) /* can't use last char of screen */
10693#endif
10694 ++o;
10695#ifdef FEAT_VERTSPLIT
10696 this_ru_col = ru_col - (Columns - width);
10697 if (this_ru_col < 0)
10698 this_ru_col = 0;
10699#endif
10700 /* Never use more than half the window/screen width, leave the other
10701 * half for the filename. */
10702 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10703 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10704 if (this_ru_col + o < WITH_WIDTH(width))
10705 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010706 /* need at least 3 chars left for get_rel_pos() + NUL */
10707 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 {
10709#ifdef FEAT_MBYTE
10710 if (has_mbyte)
10711 i += (*mb_char2bytes)(fillchar, buffer + i);
10712 else
10713#endif
10714 buffer[i++] = fillchar;
10715 ++o;
10716 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010717 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 }
10719 /* Truncate at window boundary. */
10720#ifdef FEAT_MBYTE
10721 if (has_mbyte)
10722 {
10723 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010724 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 {
10726 o += (*mb_ptr2cells)(buffer + i);
10727 if (this_ru_col + o > WITH_WIDTH(width))
10728 {
10729 buffer[i] = NUL;
10730 break;
10731 }
10732 }
10733 }
10734 else
10735#endif
10736 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10737 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10738
10739 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10740 i = redraw_cmdline;
10741 screen_fill(row, row + 1,
10742 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10743 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10744 fillchar, fillchar, attr);
10745 /* don't redraw the cmdline because of showing the ruler */
10746 redraw_cmdline = i;
10747 wp->w_ru_cursor = wp->w_cursor;
10748 wp->w_ru_virtcol = wp->w_virtcol;
10749 wp->w_ru_empty = empty_line;
10750 wp->w_ru_topline = wp->w_topline;
10751 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10752#ifdef FEAT_DIFF
10753 wp->w_ru_topfill = wp->w_topfill;
10754#endif
10755 }
10756}
10757#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010758
10759#if defined(FEAT_LINEBREAK) || defined(PROTO)
10760/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010761 * Return the width of the 'number' and 'relativenumber' column.
10762 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010763 * Otherwise it depends on 'numberwidth' and the line count.
10764 */
10765 int
10766number_width(wp)
10767 win_T *wp;
10768{
10769 int n;
10770 linenr_T lnum;
10771
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010772 if (wp->w_p_rnu && !wp->w_p_nu)
10773 /* cursor line shows "0" */
10774 lnum = wp->w_height;
10775 else
10776 /* cursor line shows absolute line number */
10777 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010778
Bram Moolenaar6b314672015-03-20 15:42:10 +010010779 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010780 return wp->w_nrwidth_width;
10781 wp->w_nrwidth_line_count = lnum;
10782
10783 n = 0;
10784 do
10785 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010786 lnum /= 10;
10787 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010788 } while (lnum > 0);
10789
10790 /* 'numberwidth' gives the minimal width plus one */
10791 if (n < wp->w_p_nuw - 1)
10792 n = wp->w_p_nuw - 1;
10793
10794 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010795 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010796 return n;
10797}
10798#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010799
10800/*
10801 * Return the current cursor column. This is the actual position on the
10802 * screen. First column is 0.
10803 */
10804 int
10805screen_screencol()
10806{
10807 return screen_cur_col;
10808}
10809
10810/*
10811 * Return the current cursor row. This is the actual position on the screen.
10812 * First row is 0.
10813 */
10814 int
10815screen_screenrow()
10816{
10817 return screen_cur_row;
10818}