blob: 43490fb8f675d40d3534fbef47e4716d5775e73e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(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
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static void win_draw_end(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
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int win_line(win_T *, linenr_T, int, int, int nochange);
128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_RIGHTLEFT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void screen_line(int row, int coloff, int endcol, int clear_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100136#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void redraw_custom_statusline(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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void start_search_hl(void);
145static void end_search_hl(void);
146static void init_search_hl(win_T *wp);
147static void prepare_search_hl(win_T *wp, linenr_T lnum);
148static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_start_highlight(int attr);
152static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void screenclear2(void);
157static void lineclear(unsigned off, int width);
158static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100159#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void linecopy(int to, int from, win_T *wp);
161static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164static void win_rest_invalid(win_T *wp);
165static void msg_pos_mode(void);
166static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100183#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
191 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
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
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
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
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 win_T *wp;
260
261 FOR_ALL_WINDOWS(wp)
262 {
263 if (wp->w_buffer == buf)
264 redraw_win_later(wp, type);
265 }
266}
267
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200268 void
269redraw_buf_and_status_later(buf_T *buf, int type)
270{
271 win_T *wp;
272
273 FOR_ALL_WINDOWS(wp)
274 {
275 if (wp->w_buffer == buf)
276 {
277 redraw_win_later(wp, type);
278 wp->w_redr_status = TRUE;
279 }
280 }
281}
282
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200284 * Redraw as soon as possible. When the command line is not scrolled redraw
285 * right away and restore what was on the command line.
286 * Return a code indicating what happened.
287 */
288 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100289redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200290{
291 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200292 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200293 int r;
294 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200295 schar_T *screenline; /* copy from ScreenLines[] */
296 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297#ifdef FEAT_MBYTE
298 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200299 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200301 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302#endif
303
304 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 return ret;
307
308 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200309 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 if (screenline == NULL || screenattr == NULL)
315 ret = 2;
316#ifdef FEAT_MBYTE
317 if (enc_utf8)
318 {
319 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200320 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321 if (screenlineUC == NULL)
322 ret = 2;
323 for (i = 0; i < p_mco; ++i)
324 {
325 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 if (screenlineC[i] == NULL)
328 ret = 2;
329 }
330 }
331 if (enc_dbcs == DBCS_JPNU)
332 {
333 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200334 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenline2 == NULL)
336 ret = 2;
337 }
338#endif
339
340 if (ret != 2)
341 {
342 /* Save the text displayed in the command line area. */
343 for (r = 0; r < rows; ++r)
344 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200345 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 (size_t)cols * sizeof(schar_T));
348 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200349 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351#ifdef FEAT_MBYTE
352 if (enc_utf8)
353 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 mch_memmove(screenlineC[i] + r * cols,
359 ScreenLinesC[i] + LineOffset[cmdline_row + r],
360 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 }
362 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366#endif
367 }
368
369 update_screen(0);
370 ret = 3;
371
372 if (must_redraw == 0)
373 {
374 int off = (int)(current_ScreenLine - ScreenLines);
375
376 /* Restore the text displayed in the command line area. */
377 for (r = 0; r < rows; ++r)
378 {
379 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200380 screenline + r * cols,
381 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200382 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 screenattr + r * cols,
384 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200385#ifdef FEAT_MBYTE
386 if (enc_utf8)
387 {
388 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200389 screenlineUC + r * cols,
390 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200391 for (i = 0; i < p_mco; ++i)
392 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200393 screenlineC[i] + r * cols,
394 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200395 }
396 if (enc_dbcs == DBCS_JPNU)
397 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200398 screenline2 + r * cols,
399 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200400#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 }
403 ret = 4;
404 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200405 }
406
407 vim_free(screenline);
408 vim_free(screenattr);
409#ifdef FEAT_MBYTE
410 if (enc_utf8)
411 {
412 vim_free(screenlineUC);
413 for (i = 0; i < p_mco; ++i)
414 vim_free(screenlineC[i]);
415 }
416 if (enc_dbcs == DBCS_JPNU)
417 vim_free(screenline2);
418#endif
419
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200420 /* Show the intro message when appropriate. */
421 maybe_intro_message();
422
423 setcursor();
424
Bram Moolenaar2951b772013-07-03 12:45:31 +0200425 return ret;
426}
427
428/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100429 * Invoked after an asynchronous callback is called.
430 * If an echo command was used the cursor needs to be put back where
431 * it belongs. If highlighting was changed a redraw is needed.
432 */
433 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200434redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100435{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100436 if (State == HITRETURN || State == ASKMORE)
437 ; /* do nothing */
438 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200439 {
440 /* Redrawing only works when the screen didn't scroll. */
441 if (msg_scrolled == 0)
442 {
443 update_screen(0);
444 compute_cmdrow();
445 }
446 else
447 {
448 /* Redraw in the same position, so that the user can continue
449 * editing the command. */
450 compute_cmdrow();
451 if (cmdline_row > msg_scrolled)
452 cmdline_row -= msg_scrolled;
453 else
454 cmdline_row = 0;
455 }
456 redrawcmdline_ex(FALSE);
457 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200458 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100459 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200460 /* keep the command line if possible */
461 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100462 setcursor();
463 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100464 cursor_on();
465 out_flush();
466#ifdef FEAT_GUI
467 if (gui.in_use)
468 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200469 /* Don't update the cursor when it is blinking and off to avoid
470 * flicker. */
471 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200472 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100473 gui_mch_flush();
474 }
475#endif
476}
477
478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 * Changed something in the current window, at buffer line "lnum", that
480 * requires that line and possibly other lines to be redrawn.
481 * Used when entering/leaving Insert mode with the cursor on a folded line.
482 * Used to remove the "$" from a change command.
483 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
484 * may become invalid and the whole window will have to be redrawn.
485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100487redrawWinline(
488 linenr_T lnum,
489 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490{
491#ifdef FEAT_FOLDING
492 int i;
493#endif
494
495 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
496 curwin->w_redraw_top = lnum;
497 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
498 curwin->w_redraw_bot = lnum;
499 redraw_later(VALID);
500
501#ifdef FEAT_FOLDING
502 if (invalid)
503 {
504 /* A w_lines[] entry for this lnum has become invalid. */
505 i = find_wl_entry(curwin, lnum);
506 if (i >= 0)
507 curwin->w_lines[i].wl_valid = FALSE;
508 }
509#endif
510}
511
512/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200513 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 */
515 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100516update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
518 redraw_curbuf_later(type);
519 update_screen(type);
520}
521
522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 * Based on the current value of curwin->w_topline, transfer a screenfull
524 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
525 */
526 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200527update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200529 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 win_T *wp;
531 static int did_intro = FALSE;
532#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
533 int did_one;
534#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200535#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200536 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200537 int gui_cursor_col;
538 int gui_cursor_row;
539#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200540 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000542 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 if (!screen_valid(TRUE))
544 return;
545
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 if (type == VALID_NO_UPDATE)
547 {
548 no_update = TRUE;
549 type = 0;
550 }
551
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (must_redraw)
553 {
554 if (type < must_redraw) /* use maximal type */
555 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000556
557 /* must_redraw is reset here, so that when we run into some weird
558 * reason to redraw while busy redrawing (e.g., asynchronous
559 * scrolling), or update_topline() in win_update() will cause a
560 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 must_redraw = 0;
562 }
563
564 /* Need to update w_lines[]. */
565 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
566 type = NOT_VALID;
567
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000568 /* Postpone the redrawing when it's not needed and when being called
569 * recursively. */
570 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 {
572 redraw_later(type); /* remember type for next time */
573 must_redraw = type;
574 if (type > INVERTED_ALL)
575 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
576 return;
577 }
578
579 updating_screen = TRUE;
580#ifdef FEAT_SYN_HL
581 ++display_tick; /* let syntax code know we're in a next round of
582 * display updating */
583#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200584 if (no_update)
585 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
587 /*
588 * if the screen was scrolled up when displaying a message, scroll it down
589 */
590 if (msg_scrolled)
591 {
592 clear_cmdline = TRUE;
593 if (msg_scrolled > Rows - 5) /* clearing is faster */
594 type = CLEAR;
595 else if (type != CLEAR)
596 {
597 check_for_delay(FALSE);
598 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
599 type = CLEAR;
600 FOR_ALL_WINDOWS(wp)
601 {
602 if (W_WINROW(wp) < msg_scrolled)
603 {
604 if (W_WINROW(wp) + wp->w_height > msg_scrolled
605 && wp->w_redr_type < REDRAW_TOP
606 && wp->w_lines_valid > 0
607 && wp->w_topline == wp->w_lines[0].wl_lnum)
608 {
609 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
610 wp->w_redr_type = REDRAW_TOP;
611 }
612 else
613 {
614 wp->w_redr_type = NOT_VALID;
615#ifdef FEAT_WINDOWS
616 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
617 <= msg_scrolled)
618 wp->w_redr_status = TRUE;
619#endif
620 }
621 }
622 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200623 if (!no_update)
624 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000625#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000626 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629 msg_scrolled = 0;
630 need_wait_return = FALSE;
631 }
632
633 /* reset cmdline_row now (may have been changed temporarily) */
634 compute_cmdrow();
635
636 /* Check for changed highlighting */
637 if (need_highlight_changed)
638 highlight_changed();
639
640 if (type == CLEAR) /* first clear screen */
641 {
642 screenclear(); /* will reset clear_cmdline */
643 type = NOT_VALID;
644 }
645
646 if (clear_cmdline) /* going to clear cmdline (done below) */
647 check_for_delay(FALSE);
648
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000649#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200650 /* Force redraw when width of 'number' or 'relativenumber' column
651 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000652 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200653 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
654 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000655 curwin->w_redr_type = NOT_VALID;
656#endif
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 /*
659 * Only start redrawing if there is really something to do.
660 */
661 if (type == INVERTED)
662 update_curswant();
663 if (curwin->w_redr_type < type
664 && !((type == VALID
665 && curwin->w_lines[0].wl_valid
666#ifdef FEAT_DIFF
667 && curwin->w_topfill == curwin->w_old_topfill
668 && curwin->w_botfill == curwin->w_old_botfill
669#endif
670 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000672 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
674 && curwin->w_old_visual_mode == VIsual_mode
675 && (curwin->w_valid & VALID_VIRTCOL)
676 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 ))
678 curwin->w_redr_type = type;
679
Bram Moolenaar5a305422006-04-28 22:38:25 +0000680#ifdef FEAT_WINDOWS
681 /* Redraw the tab pages line if needed. */
682 if (redraw_tabline || type >= NOT_VALID)
683 draw_tabline();
684#endif
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686#ifdef FEAT_SYN_HL
687 /*
688 * Correct stored syntax highlighting info for changes in each displayed
689 * buffer. Each buffer must only be done once.
690 */
691 FOR_ALL_WINDOWS(wp)
692 {
693 if (wp->w_buffer->b_mod_set)
694 {
695# ifdef FEAT_WINDOWS
696 win_T *wwp;
697
698 /* Check if we already did this buffer. */
699 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
700 if (wwp->w_buffer == wp->w_buffer)
701 break;
702# endif
703 if (
704# ifdef FEAT_WINDOWS
705 wwp == wp &&
706# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200707 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 syn_stack_apply_changes(wp->w_buffer);
709 }
710 }
711#endif
712
713 /*
714 * Go from top to bottom through the windows, redrawing the ones that need
715 * it.
716 */
717#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
718 did_one = FALSE;
719#endif
720#ifdef FEAT_SEARCH_EXTRA
721 search_hl.rm.regprog = NULL;
722#endif
723 FOR_ALL_WINDOWS(wp)
724 {
725 if (wp->w_redr_type != 0)
726 {
727 cursor_off();
728#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
729 if (!did_one)
730 {
731 did_one = TRUE;
732# ifdef FEAT_SEARCH_EXTRA
733 start_search_hl();
734# endif
735# ifdef FEAT_CLIPBOARD
736 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200737 if (clip_star.available && clip_isautosel_star())
738 clip_update_selection(&clip_star);
739 if (clip_plus.available && clip_isautosel_plus())
740 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741# endif
742#ifdef FEAT_GUI
743 /* Remove the cursor before starting to do anything, because
744 * scrolling may make it difficult to redraw the text under
745 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200746 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200747 {
748 gui_cursor_col = gui.cursor_col;
749 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200751 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#endif
754 }
755#endif
756 win_update(wp);
757 }
758
759#ifdef FEAT_WINDOWS
760 /* redraw status line after the window to minimize cursor movement */
761 if (wp->w_redr_status)
762 {
763 cursor_off();
764 win_redr_status(wp);
765 }
766#endif
767 }
768#if defined(FEAT_SEARCH_EXTRA)
769 end_search_hl();
770#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100771#ifdef FEAT_INS_EXPAND
772 /* May need to redraw the popup menu. */
773 if (pum_visible())
774 pum_redraw();
775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777#ifdef FEAT_WINDOWS
778 /* Reset b_mod_set flags. Going through all windows is probably faster
779 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200780 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 wp->w_buffer->b_mod_set = FALSE;
782#else
783 curbuf->b_mod_set = FALSE;
784#endif
785
786 updating_screen = FALSE;
787#ifdef FEAT_GUI
788 gui_may_resize_shell();
789#endif
790
791 /* Clear or redraw the command line. Done last, because scrolling may
792 * mess up the command line. */
793 if (clear_cmdline || redraw_cmdline)
794 showmode();
795
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200796 if (no_update)
797 --no_win_do_lines_ins;
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200800 if (!did_intro)
801 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 did_intro = TRUE;
803
804#ifdef FEAT_GUI
805 /* Redraw the cursor and update the scrollbars when all screen updating is
806 * done. */
807 if (gui.in_use)
808 {
809 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200810 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200811 {
812 /* Put the GUI position where the cursor was, gui_update_cursor()
813 * uses that. */
814 gui.col = gui_cursor_col;
815 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200816# ifdef FEAT_MBYTE
817 gui.col = mb_fix_col(gui.col, gui.row);
818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200820 screen_cur_col = gui.col;
821 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 gui_update_scrollbars(FALSE);
824 }
825#endif
826}
827
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100828#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
829/*
830 * Prepare for updating one or more windows.
831 * Caller must check for "updating_screen" already set to avoid recursiveness.
832 */
833 static void
834update_prepare(void)
835{
836 cursor_off();
837 updating_screen = TRUE;
838#ifdef FEAT_GUI
839 /* Remove the cursor before starting to do anything, because scrolling may
840 * make it difficult to redraw the text under it. */
841 if (gui.in_use)
842 gui_undraw_cursor();
843#endif
844#ifdef FEAT_SEARCH_EXTRA
845 start_search_hl();
846#endif
847}
848
849/*
850 * Finish updating one or more windows.
851 */
852 static void
853update_finish(void)
854{
855 if (redraw_cmdline)
856 showmode();
857
858# ifdef FEAT_SEARCH_EXTRA
859 end_search_hl();
860# endif
861
862 updating_screen = FALSE;
863
864# ifdef FEAT_GUI
865 gui_may_resize_shell();
866
867 /* Redraw the cursor and update the scrollbars when all screen updating is
868 * done. */
869 if (gui.in_use)
870 {
871 out_flush(); /* required before updating the cursor */
872 gui_update_cursor(FALSE, FALSE);
873 gui_update_scrollbars(FALSE);
874 }
875# endif
876}
877#endif
878
Bram Moolenaar860cae12010-06-05 23:22:07 +0200879#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200880/*
881 * Return TRUE if the cursor line in window "wp" may be concealed, according
882 * to the 'concealcursor' option.
883 */
884 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100885conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200886{
887 int c;
888
889 if (*wp->w_p_cocu == NUL)
890 return FALSE;
891 if (get_real_state() & VISUAL)
892 c = 'v';
893 else if (State & INSERT)
894 c = 'i';
895 else if (State & NORMAL)
896 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200897 else if (State & CMDLINE)
898 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200899 else
900 return FALSE;
901 return vim_strchr(wp->w_p_cocu, c) != NULL;
902}
903
904/*
905 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
906 */
907 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100908conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200909{
910 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
911 {
912 need_cursor_line_redraw = TRUE;
913 /* Need to recompute cursor column, e.g., when starting Visual mode
914 * without concealing. */
915 curs_columns(TRUE);
916 }
917}
918
Bram Moolenaar860cae12010-06-05 23:22:07 +0200919 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100920update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200921{
922 int row;
923 int j;
924
Bram Moolenaar908be432016-05-24 10:51:30 +0200925 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100926 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200927 return;
928
Bram Moolenaar860cae12010-06-05 23:22:07 +0200929 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200930 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200931 {
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100932 update_prepare();
933
Bram Moolenaar860cae12010-06-05 23:22:07 +0200934 row = 0;
935 for (j = 0; j < wp->w_lines_valid; ++j)
936 {
937 if (lnum == wp->w_lines[j].wl_lnum)
938 {
939 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200940# ifdef FEAT_SEARCH_EXTRA
941 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200942 start_search_hl();
943 prepare_search_hl(wp, lnum);
944# endif
945 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
946# if defined(FEAT_SEARCH_EXTRA)
947 end_search_hl();
948# endif
949 break;
950 }
951 row += wp->w_lines[j].wl_size;
952 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100953
954 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200955 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200956 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958#endif
959
960#if defined(FEAT_SIGNS) || defined(PROTO)
961 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100962update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964 win_T *wp;
965 int doit = FALSE;
966
967# ifdef FEAT_FOLDING
968 win_foldinfo.fi_level = 0;
969# endif
970
971 /* update/delete a specific mark */
972 FOR_ALL_WINDOWS(wp)
973 {
974 if (buf != NULL && lnum > 0)
975 {
976 if (wp->w_buffer == buf && lnum >= wp->w_topline
977 && lnum < wp->w_botline)
978 {
979 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
980 wp->w_redraw_top = lnum;
981 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
982 wp->w_redraw_bot = lnum;
983 redraw_win_later(wp, VALID);
984 }
985 }
986 else
987 redraw_win_later(wp, VALID);
988 if (wp->w_redr_type != 0)
989 doit = TRUE;
990 }
991
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100992 /* Return when there is nothing to do, screen updating is already
993 * happening (recursive call) or still starting up. */
994 if (!doit || updating_screen
995#ifdef FEAT_GUI
996 || gui.starting
997#endif
998 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 return;
1000
1001 /* update all windows that need updating */
1002 update_prepare();
1003
1004# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001005 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 {
1007 if (wp->w_redr_type != 0)
1008 win_update(wp);
1009 if (wp->w_redr_status)
1010 win_redr_status(wp);
1011 }
1012# else
1013 if (curwin->w_redr_type != 0)
1014 win_update(curwin);
1015# endif
1016
1017 update_finish();
1018}
1019#endif
1020
1021
1022#if defined(FEAT_GUI) || defined(PROTO)
1023/*
1024 * Update a single window, its status line and maybe the command line msg.
1025 * Used for the GUI scrollbar.
1026 */
1027 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001028updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001030 /* return if already busy updating */
1031 if (updating_screen)
1032 return;
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 update_prepare();
1035
1036#ifdef FEAT_CLIPBOARD
1037 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001038 if (clip_star.available && clip_isautosel_star())
1039 clip_update_selection(&clip_star);
1040 if (clip_plus.available && clip_isautosel_plus())
1041 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001047 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001048 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001049 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (wp->w_redr_status
1052# ifdef FEAT_CMDL_INFO
1053 || p_ru
1054# endif
1055# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001056 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057# endif
1058 )
1059 win_redr_status(wp);
1060#endif
1061
1062 update_finish();
1063}
1064#endif
1065
1066/*
1067 * Update a single window.
1068 *
1069 * This may cause the windows below it also to be redrawn (when clearing the
1070 * screen or scrolling lines).
1071 *
1072 * How the window is redrawn depends on wp->w_redr_type. Each type also
1073 * implies the one below it.
1074 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001075 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1077 * INVERTED redraw the changed part of the Visual area
1078 * INVERTED_ALL redraw the whole Visual area
1079 * VALID 1. scroll up/down to adjust for a changed w_topline
1080 * 2. update lines at the top when scrolled down
1081 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001082 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 * b_mod_top and b_mod_bot.
1084 * - if wp->w_redraw_top non-zero, redraw lines between
1085 * wp->w_redraw_top and wp->w_redr_bot.
1086 * - continue redrawing when syntax status is invalid.
1087 * 4. if scrolled up, update lines at the bottom.
1088 * This results in three areas that may need updating:
1089 * top: from first row to top_end (when scrolled down)
1090 * mid: from mid_start to mid_end (update inversion or changed text)
1091 * bot: from bot_start to last row (when scrolled up)
1092 */
1093 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001094win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095{
1096 buf_T *buf = wp->w_buffer;
1097 int type;
1098 int top_end = 0; /* Below last row of the top area that needs
1099 updating. 0 when no top area updating. */
1100 int mid_start = 999;/* first row of the mid area that needs
1101 updating. 999 when no mid area updating. */
1102 int mid_end = 0; /* Below last row of the mid area that needs
1103 updating. 0 when no mid area updating. */
1104 int bot_start = 999;/* first row of the bot area that needs
1105 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 int scrolled_down = FALSE; /* TRUE when scrolled down when
1107 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001109 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 int top_to_mod = FALSE; /* redraw above mod_top */
1111#endif
1112
1113 int row; /* current window row to display */
1114 linenr_T lnum; /* current buffer lnum to display */
1115 int idx; /* current index in w_lines[] */
1116 int srow; /* starting row of the current line */
1117
1118 int eof = FALSE; /* if TRUE, we hit the end of the file */
1119 int didline = FALSE; /* if TRUE, we finished the last line */
1120 int i;
1121 long j;
1122 static int recursive = FALSE; /* being called recursively */
1123 int old_botline = wp->w_botline;
1124#ifdef FEAT_FOLDING
1125 long fold_count;
1126#endif
1127#ifdef FEAT_SYN_HL
1128 /* remember what happened to the previous line, to know if
1129 * check_visual_highlight() can be used */
1130#define DID_NONE 1 /* didn't update a line */
1131#define DID_LINE 2 /* updated a normal line */
1132#define DID_FOLD 3 /* updated a folded line */
1133 int did_update = DID_NONE;
1134 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1135#endif
1136 linenr_T mod_top = 0;
1137 linenr_T mod_bot = 0;
1138#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1139 int save_got_int;
1140#endif
1141
1142 type = wp->w_redr_type;
1143
1144 if (type == NOT_VALID)
1145 {
1146#ifdef FEAT_WINDOWS
1147 wp->w_redr_status = TRUE;
1148#endif
1149 wp->w_lines_valid = 0;
1150 }
1151
1152 /* Window is zero-height: nothing to draw. */
1153 if (wp->w_height == 0)
1154 {
1155 wp->w_redr_type = 0;
1156 return;
1157 }
1158
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001159#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 /* Window is zero-width: Only need to draw the separator. */
1161 if (wp->w_width == 0)
1162 {
1163 /* draw the vertical separator right of this window */
1164 draw_vsep_win(wp, 0);
1165 wp->w_redr_type = 0;
1166 return;
1167 }
1168#endif
1169
1170#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001171 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#endif
1173
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001174#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001175 /* Force redraw when width of 'number' or 'relativenumber' column
1176 * changes. */
1177 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001178 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001179 {
1180 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001181 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001182 }
1183 else
1184#endif
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1187 {
1188 /*
1189 * When there are both inserted/deleted lines and specific lines to be
1190 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1191 * everything (only happens when redrawing is off for while).
1192 */
1193 type = NOT_VALID;
1194 }
1195 else
1196 {
1197 /*
1198 * Set mod_top to the first line that needs displaying because of
1199 * changes. Set mod_bot to the first line after the changes.
1200 */
1201 mod_top = wp->w_redraw_top;
1202 if (wp->w_redraw_bot != 0)
1203 mod_bot = wp->w_redraw_bot + 1;
1204 else
1205 mod_bot = 0;
1206 wp->w_redraw_top = 0; /* reset for next time */
1207 wp->w_redraw_bot = 0;
1208 if (buf->b_mod_set)
1209 {
1210 if (mod_top == 0 || mod_top > buf->b_mod_top)
1211 {
1212 mod_top = buf->b_mod_top;
1213#ifdef FEAT_SYN_HL
1214 /* Need to redraw lines above the change that may be included
1215 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001216 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001218 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (mod_top < 1)
1220 mod_top = 1;
1221 }
1222#endif
1223 }
1224 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1225 mod_bot = buf->b_mod_bot;
1226
1227#ifdef FEAT_SEARCH_EXTRA
1228 /* When 'hlsearch' is on and using a multi-line search pattern, a
1229 * change in one line may make the Search highlighting in a
1230 * previous line invalid. Simple solution: redraw all visible
1231 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001232 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001234 if (search_hl.rm.regprog != NULL
1235 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001237 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001238 {
1239 cur = wp->w_match_head;
1240 while (cur != NULL)
1241 {
1242 if (cur->match.regprog != NULL
1243 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001244 {
1245 top_to_mod = TRUE;
1246 break;
1247 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001248 cur = cur->next;
1249 }
1250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251#endif
1252 }
1253#ifdef FEAT_FOLDING
1254 if (mod_top != 0 && hasAnyFolding(wp))
1255 {
1256 linenr_T lnumt, lnumb;
1257
1258 /*
1259 * A change in a line can cause lines above it to become folded or
1260 * unfolded. Find the top most buffer line that may be affected.
1261 * If the line was previously folded and displayed, get the first
1262 * line of that fold. If the line is folded now, get the first
1263 * folded line. Use the minimum of these two.
1264 */
1265
1266 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1267 * the line below it. If there is no valid entry, use w_topline.
1268 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1269 * to this line. If there is no valid entry, use MAXLNUM. */
1270 lnumt = wp->w_topline;
1271 lnumb = MAXLNUM;
1272 for (i = 0; i < wp->w_lines_valid; ++i)
1273 if (wp->w_lines[i].wl_valid)
1274 {
1275 if (wp->w_lines[i].wl_lastlnum < mod_top)
1276 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1277 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1278 {
1279 lnumb = wp->w_lines[i].wl_lnum;
1280 /* When there is a fold column it might need updating
1281 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001282 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 ++lnumb;
1284 }
1285 }
1286
1287 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1288 if (mod_top > lnumt)
1289 mod_top = lnumt;
1290
1291 /* Now do the same for the bottom line (one above mod_bot). */
1292 --mod_bot;
1293 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1294 ++mod_bot;
1295 if (mod_bot < lnumb)
1296 mod_bot = lnumb;
1297 }
1298#endif
1299
1300 /* When a change starts above w_topline and the end is below
1301 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001302 * If the end of the change is above w_topline: do like no change was
1303 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (mod_top != 0 && mod_top < wp->w_topline)
1305 {
1306 if (mod_bot > wp->w_topline)
1307 mod_top = wp->w_topline;
1308#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001309 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 top_end = 1;
1311#endif
1312 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001313
1314 /* When line numbers are displayed need to redraw all lines below
1315 * inserted/deleted lines. */
1316 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1317 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319
1320 /*
1321 * When only displaying the lines at the top, set top_end. Used when
1322 * window has scrolled down for msg_scrolled.
1323 */
1324 if (type == REDRAW_TOP)
1325 {
1326 j = 0;
1327 for (i = 0; i < wp->w_lines_valid; ++i)
1328 {
1329 j += wp->w_lines[i].wl_size;
1330 if (j >= wp->w_upd_rows)
1331 {
1332 top_end = j;
1333 break;
1334 }
1335 }
1336 if (top_end == 0)
1337 /* not found (cannot happen?): redraw everything */
1338 type = NOT_VALID;
1339 else
1340 /* top area defined, the rest is VALID */
1341 type = VALID;
1342 }
1343
Bram Moolenaar367329b2007-08-30 11:53:22 +00001344 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001345 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1346 * non-zero and thus not FALSE) will indicate that screenclear() was not
1347 * called. */
1348 if (screen_cleared)
1349 screen_cleared = MAYBE;
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 /*
1352 * If there are no changes on the screen that require a complete redraw,
1353 * handle three cases:
1354 * 1: we are off the top of the screen by a few lines: scroll down
1355 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1356 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1357 * w_lines[] that needs updating.
1358 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001359 if ((type == VALID || type == SOME_VALID
1360 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#ifdef FEAT_DIFF
1362 && !wp->w_botfill && !wp->w_old_botfill
1363#endif
1364 )
1365 {
1366 if (mod_top != 0 && wp->w_topline == mod_top)
1367 {
1368 /*
1369 * w_topline is the first changed line, the scrolling will be done
1370 * further down.
1371 */
1372 }
1373 else if (wp->w_lines[0].wl_valid
1374 && (wp->w_topline < wp->w_lines[0].wl_lnum
1375#ifdef FEAT_DIFF
1376 || (wp->w_topline == wp->w_lines[0].wl_lnum
1377 && wp->w_topfill > wp->w_old_topfill)
1378#endif
1379 ))
1380 {
1381 /*
1382 * New topline is above old topline: May scroll down.
1383 */
1384#ifdef FEAT_FOLDING
1385 if (hasAnyFolding(wp))
1386 {
1387 linenr_T ln;
1388
1389 /* count the number of lines we are off, counting a sequence
1390 * of folded lines as one */
1391 j = 0;
1392 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1393 {
1394 ++j;
1395 if (j >= wp->w_height - 2)
1396 break;
1397 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1398 }
1399 }
1400 else
1401#endif
1402 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1403 if (j < wp->w_height - 2) /* not too far off */
1404 {
1405 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1406#ifdef FEAT_DIFF
1407 /* insert extra lines for previously invisible filler lines */
1408 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1409 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1410 - wp->w_old_topfill;
1411#endif
1412 if (i < wp->w_height - 2) /* less than a screen off */
1413 {
1414 /*
1415 * Try to insert the correct number of lines.
1416 * If not the last window, delete the lines at the bottom.
1417 * win_ins_lines may fail when the terminal can't do it.
1418 */
1419 if (i > 0)
1420 check_for_delay(FALSE);
1421 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1422 {
1423 if (wp->w_lines_valid != 0)
1424 {
1425 /* Need to update rows that are new, stop at the
1426 * first one that scrolled down. */
1427 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 /* Move the entries that were scrolled, disable
1431 * the entries for the lines to be redrawn. */
1432 if ((wp->w_lines_valid += j) > wp->w_height)
1433 wp->w_lines_valid = wp->w_height;
1434 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1435 wp->w_lines[idx] = wp->w_lines[idx - j];
1436 while (idx >= 0)
1437 wp->w_lines[idx--].wl_valid = FALSE;
1438 }
1439 }
1440 else
1441 mid_start = 0; /* redraw all lines */
1442 }
1443 else
1444 mid_start = 0; /* redraw all lines */
1445 }
1446 else
1447 mid_start = 0; /* redraw all lines */
1448 }
1449 else
1450 {
1451 /*
1452 * New topline is at or below old topline: May scroll up.
1453 * When topline didn't change, find first entry in w_lines[] that
1454 * needs updating.
1455 */
1456
1457 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1458 j = -1;
1459 row = 0;
1460 for (i = 0; i < wp->w_lines_valid; i++)
1461 {
1462 if (wp->w_lines[i].wl_valid
1463 && wp->w_lines[i].wl_lnum == wp->w_topline)
1464 {
1465 j = i;
1466 break;
1467 }
1468 row += wp->w_lines[i].wl_size;
1469 }
1470 if (j == -1)
1471 {
1472 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1473 * lines */
1474 mid_start = 0;
1475 }
1476 else
1477 {
1478 /*
1479 * Try to delete the correct number of lines.
1480 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1481 */
1482#ifdef FEAT_DIFF
1483 /* If the topline didn't change, delete old filler lines,
1484 * otherwise delete filler lines of the new topline... */
1485 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1486 row += wp->w_old_topfill;
1487 else
1488 row += diff_check_fill(wp, wp->w_topline);
1489 /* ... but don't delete new filler lines. */
1490 row -= wp->w_topfill;
1491#endif
1492 if (row > 0)
1493 {
1494 check_for_delay(FALSE);
1495 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1496 bot_start = wp->w_height - row;
1497 else
1498 mid_start = 0; /* redraw all lines */
1499 }
1500 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1501 {
1502 /*
1503 * Skip the lines (below the deleted lines) that are still
1504 * valid and don't need redrawing. Copy their info
1505 * upwards, to compensate for the deleted lines. Set
1506 * bot_start to the first row that needs redrawing.
1507 */
1508 bot_start = 0;
1509 idx = 0;
1510 for (;;)
1511 {
1512 wp->w_lines[idx] = wp->w_lines[j];
1513 /* stop at line that didn't fit, unless it is still
1514 * valid (no lines deleted) */
1515 if (row > 0 && bot_start + row
1516 + (int)wp->w_lines[j].wl_size > wp->w_height)
1517 {
1518 wp->w_lines_valid = idx + 1;
1519 break;
1520 }
1521 bot_start += wp->w_lines[idx++].wl_size;
1522
1523 /* stop at the last valid entry in w_lines[].wl_size */
1524 if (++j >= wp->w_lines_valid)
1525 {
1526 wp->w_lines_valid = idx;
1527 break;
1528 }
1529 }
1530#ifdef FEAT_DIFF
1531 /* Correct the first entry for filler lines at the top
1532 * when it won't get updated below. */
1533 if (wp->w_p_diff && bot_start > 0)
1534 wp->w_lines[0].wl_size =
1535 plines_win_nofill(wp, wp->w_topline, TRUE)
1536 + wp->w_topfill;
1537#endif
1538 }
1539 }
1540 }
1541
1542 /* When starting redraw in the first line, redraw all lines. When
1543 * there is only one window it's probably faster to clear the screen
1544 * first. */
1545 if (mid_start == 0)
1546 {
1547 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001548 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001549 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001550 /* Clear the screen when it was not done by win_del_lines() or
1551 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1552 * then. */
1553 if (screen_cleared != TRUE)
1554 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001555#ifdef FEAT_WINDOWS
1556 /* The screen was cleared, redraw the tab pages line. */
1557 if (redraw_tabline)
1558 draw_tabline();
1559#endif
1560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001562
1563 /* When win_del_lines() or win_ins_lines() caused the screen to be
1564 * cleared (only happens for the first window) or when screenclear()
1565 * was called directly above, "must_redraw" will have been set to
1566 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1567 if (screen_cleared == TRUE)
1568 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
1570 else
1571 {
1572 /* Not VALID or INVERTED: redraw all lines. */
1573 mid_start = 0;
1574 mid_end = wp->w_height;
1575 }
1576
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001577 if (type == SOME_VALID)
1578 {
1579 /* SOME_VALID: redraw all lines. */
1580 mid_start = 0;
1581 mid_end = wp->w_height;
1582 type = NOT_VALID;
1583 }
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 /* check if we are updating or removing the inverted part */
1586 if ((VIsual_active && buf == curwin->w_buffer)
1587 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1588 {
1589 linenr_T from, to;
1590
1591 if (VIsual_active)
1592 {
1593 if (VIsual_active
1594 && (VIsual_mode != wp->w_old_visual_mode
1595 || type == INVERTED_ALL))
1596 {
1597 /*
1598 * If the type of Visual selection changed, redraw the whole
1599 * selection. Also when the ownership of the X selection is
1600 * gained or lost.
1601 */
1602 if (curwin->w_cursor.lnum < VIsual.lnum)
1603 {
1604 from = curwin->w_cursor.lnum;
1605 to = VIsual.lnum;
1606 }
1607 else
1608 {
1609 from = VIsual.lnum;
1610 to = curwin->w_cursor.lnum;
1611 }
1612 /* redraw more when the cursor moved as well */
1613 if (wp->w_old_cursor_lnum < from)
1614 from = wp->w_old_cursor_lnum;
1615 if (wp->w_old_cursor_lnum > to)
1616 to = wp->w_old_cursor_lnum;
1617 if (wp->w_old_visual_lnum < from)
1618 from = wp->w_old_visual_lnum;
1619 if (wp->w_old_visual_lnum > to)
1620 to = wp->w_old_visual_lnum;
1621 }
1622 else
1623 {
1624 /*
1625 * Find the line numbers that need to be updated: The lines
1626 * between the old cursor position and the current cursor
1627 * position. Also check if the Visual position changed.
1628 */
1629 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1630 {
1631 from = curwin->w_cursor.lnum;
1632 to = wp->w_old_cursor_lnum;
1633 }
1634 else
1635 {
1636 from = wp->w_old_cursor_lnum;
1637 to = curwin->w_cursor.lnum;
1638 if (from == 0) /* Visual mode just started */
1639 from = to;
1640 }
1641
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001642 if (VIsual.lnum != wp->w_old_visual_lnum
1643 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 if (wp->w_old_visual_lnum < from
1646 && wp->w_old_visual_lnum != 0)
1647 from = wp->w_old_visual_lnum;
1648 if (wp->w_old_visual_lnum > to)
1649 to = wp->w_old_visual_lnum;
1650 if (VIsual.lnum < from)
1651 from = VIsual.lnum;
1652 if (VIsual.lnum > to)
1653 to = VIsual.lnum;
1654 }
1655 }
1656
1657 /*
1658 * If in block mode and changed column or curwin->w_curswant:
1659 * update all lines.
1660 * First compute the actual start and end column.
1661 */
1662 if (VIsual_mode == Ctrl_V)
1663 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001664 colnr_T fromc, toc;
1665#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1666 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaar404406a2014-10-09 13:24:43 +02001668 if (curwin->w_p_lbr)
1669 ve_flags = VE_ALL;
1670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001672#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1673 ve_flags = save_ve_flags;
1674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 ++toc;
1676 if (curwin->w_curswant == MAXCOL)
1677 toc = MAXCOL;
1678
1679 if (fromc != wp->w_old_cursor_fcol
1680 || toc != wp->w_old_cursor_lcol)
1681 {
1682 if (from > VIsual.lnum)
1683 from = VIsual.lnum;
1684 if (to < VIsual.lnum)
1685 to = VIsual.lnum;
1686 }
1687 wp->w_old_cursor_fcol = fromc;
1688 wp->w_old_cursor_lcol = toc;
1689 }
1690 }
1691 else
1692 {
1693 /* Use the line numbers of the old Visual area. */
1694 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1695 {
1696 from = wp->w_old_cursor_lnum;
1697 to = wp->w_old_visual_lnum;
1698 }
1699 else
1700 {
1701 from = wp->w_old_visual_lnum;
1702 to = wp->w_old_cursor_lnum;
1703 }
1704 }
1705
1706 /*
1707 * There is no need to update lines above the top of the window.
1708 */
1709 if (from < wp->w_topline)
1710 from = wp->w_topline;
1711
1712 /*
1713 * If we know the value of w_botline, use it to restrict the update to
1714 * the lines that are visible in the window.
1715 */
1716 if (wp->w_valid & VALID_BOTLINE)
1717 {
1718 if (from >= wp->w_botline)
1719 from = wp->w_botline - 1;
1720 if (to >= wp->w_botline)
1721 to = wp->w_botline - 1;
1722 }
1723
1724 /*
1725 * Find the minimal part to be updated.
1726 * Watch out for scrolling that made entries in w_lines[] invalid.
1727 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1728 * top_end; need to redraw from top_end to the "to" line.
1729 * A middle mouse click with a Visual selection may change the text
1730 * above the Visual area and reset wl_valid, do count these for
1731 * mid_end (in srow).
1732 */
1733 if (mid_start > 0)
1734 {
1735 lnum = wp->w_topline;
1736 idx = 0;
1737 srow = 0;
1738 if (scrolled_down)
1739 mid_start = top_end;
1740 else
1741 mid_start = 0;
1742 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1743 {
1744 if (wp->w_lines[idx].wl_valid)
1745 mid_start += wp->w_lines[idx].wl_size;
1746 else if (!scrolled_down)
1747 srow += wp->w_lines[idx].wl_size;
1748 ++idx;
1749# ifdef FEAT_FOLDING
1750 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1751 lnum = wp->w_lines[idx].wl_lnum;
1752 else
1753# endif
1754 ++lnum;
1755 }
1756 srow += mid_start;
1757 mid_end = wp->w_height;
1758 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1759 {
1760 if (wp->w_lines[idx].wl_valid
1761 && wp->w_lines[idx].wl_lnum >= to + 1)
1762 {
1763 /* Only update until first row of this line */
1764 mid_end = srow;
1765 break;
1766 }
1767 srow += wp->w_lines[idx].wl_size;
1768 }
1769 }
1770 }
1771
1772 if (VIsual_active && buf == curwin->w_buffer)
1773 {
1774 wp->w_old_visual_mode = VIsual_mode;
1775 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1776 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001777 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 wp->w_old_curswant = curwin->w_curswant;
1779 }
1780 else
1781 {
1782 wp->w_old_visual_mode = 0;
1783 wp->w_old_cursor_lnum = 0;
1784 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001785 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1789 /* reset got_int, otherwise regexp won't work */
1790 save_got_int = got_int;
1791 got_int = 0;
1792#endif
1793#ifdef FEAT_FOLDING
1794 win_foldinfo.fi_level = 0;
1795#endif
1796
1797 /*
1798 * Update all the window rows.
1799 */
1800 idx = 0; /* first entry in w_lines[].wl_size */
1801 row = 0;
1802 srow = 0;
1803 lnum = wp->w_topline; /* first line shown in window */
1804 for (;;)
1805 {
1806 /* stop updating when reached the end of the window (check for _past_
1807 * the end of the window is at the end of the loop) */
1808 if (row == wp->w_height)
1809 {
1810 didline = TRUE;
1811 break;
1812 }
1813
1814 /* stop updating when hit the end of the file */
1815 if (lnum > buf->b_ml.ml_line_count)
1816 {
1817 eof = TRUE;
1818 break;
1819 }
1820
1821 /* Remember the starting row of the line that is going to be dealt
1822 * with. It is used further down when the line doesn't fit. */
1823 srow = row;
1824
1825 /*
1826 * Update a line when it is in an area that needs updating, when it
1827 * has changes or w_lines[idx] is invalid.
1828 * bot_start may be halfway a wrapped line after using
1829 * win_del_lines(), check if the current line includes it.
1830 * When syntax folding is being used, the saved syntax states will
1831 * already have been updated, we can't see where the syntax state is
1832 * the same again, just update until the end of the window.
1833 */
1834 if (row < top_end
1835 || (row >= mid_start && row < mid_end)
1836#ifdef FEAT_SEARCH_EXTRA
1837 || top_to_mod
1838#endif
1839 || idx >= wp->w_lines_valid
1840 || (row + wp->w_lines[idx].wl_size > bot_start)
1841 || (mod_top != 0
1842 && (lnum == mod_top
1843 || (lnum >= mod_top
1844 && (lnum < mod_bot
1845#ifdef FEAT_SYN_HL
1846 || did_update == DID_FOLD
1847 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001848 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 && (
1850# ifdef FEAT_FOLDING
1851 (foldmethodIsSyntax(wp)
1852 && hasAnyFolding(wp)) ||
1853# endif
1854 syntax_check_changed(lnum)))
1855#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001856#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001857 /* match in fixed position might need redraw
1858 * if lines were inserted or deleted */
1859 || (wp->w_match_head != NULL
1860 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 )))))
1863 {
1864#ifdef FEAT_SEARCH_EXTRA
1865 if (lnum == mod_top)
1866 top_to_mod = FALSE;
1867#endif
1868
1869 /*
1870 * When at start of changed lines: May scroll following lines
1871 * up or down to minimize redrawing.
1872 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001873 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 */
1875 if (lnum == mod_top
1876 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001877 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
1879 int old_rows = 0;
1880 int new_rows = 0;
1881 int xtra_rows;
1882 linenr_T l;
1883
1884 /* Count the old number of window rows, using w_lines[], which
1885 * should still contain the sizes for the lines as they are
1886 * currently displayed. */
1887 for (i = idx; i < wp->w_lines_valid; ++i)
1888 {
1889 /* Only valid lines have a meaningful wl_lnum. Invalid
1890 * lines are part of the changed area. */
1891 if (wp->w_lines[i].wl_valid
1892 && wp->w_lines[i].wl_lnum == mod_bot)
1893 break;
1894 old_rows += wp->w_lines[i].wl_size;
1895#ifdef FEAT_FOLDING
1896 if (wp->w_lines[i].wl_valid
1897 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1898 {
1899 /* Must have found the last valid entry above mod_bot.
1900 * Add following invalid entries. */
1901 ++i;
1902 while (i < wp->w_lines_valid
1903 && !wp->w_lines[i].wl_valid)
1904 old_rows += wp->w_lines[i++].wl_size;
1905 break;
1906 }
1907#endif
1908 }
1909
1910 if (i >= wp->w_lines_valid)
1911 {
1912 /* We can't find a valid line below the changed lines,
1913 * need to redraw until the end of the window.
1914 * Inserting/deleting lines has no use. */
1915 bot_start = 0;
1916 }
1917 else
1918 {
1919 /* Able to count old number of rows: Count new window
1920 * rows, and may insert/delete lines */
1921 j = idx;
1922 for (l = lnum; l < mod_bot; ++l)
1923 {
1924#ifdef FEAT_FOLDING
1925 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1926 ++new_rows;
1927 else
1928#endif
1929#ifdef FEAT_DIFF
1930 if (l == wp->w_topline)
1931 new_rows += plines_win_nofill(wp, l, TRUE)
1932 + wp->w_topfill;
1933 else
1934#endif
1935 new_rows += plines_win(wp, l, TRUE);
1936 ++j;
1937 if (new_rows > wp->w_height - row - 2)
1938 {
1939 /* it's getting too much, must redraw the rest */
1940 new_rows = 9999;
1941 break;
1942 }
1943 }
1944 xtra_rows = new_rows - old_rows;
1945 if (xtra_rows < 0)
1946 {
1947 /* May scroll text up. If there is not enough
1948 * remaining text or scrolling fails, must redraw the
1949 * rest. If scrolling works, must redraw the text
1950 * below the scrolled text. */
1951 if (row - xtra_rows >= wp->w_height - 2)
1952 mod_bot = MAXLNUM;
1953 else
1954 {
1955 check_for_delay(FALSE);
1956 if (win_del_lines(wp, row,
1957 -xtra_rows, FALSE, FALSE) == FAIL)
1958 mod_bot = MAXLNUM;
1959 else
1960 bot_start = wp->w_height + xtra_rows;
1961 }
1962 }
1963 else if (xtra_rows > 0)
1964 {
1965 /* May scroll text down. If there is not enough
1966 * remaining text of scrolling fails, must redraw the
1967 * rest. */
1968 if (row + xtra_rows >= wp->w_height - 2)
1969 mod_bot = MAXLNUM;
1970 else
1971 {
1972 check_for_delay(FALSE);
1973 if (win_ins_lines(wp, row + old_rows,
1974 xtra_rows, FALSE, FALSE) == FAIL)
1975 mod_bot = MAXLNUM;
1976 else if (top_end > row + old_rows)
1977 /* Scrolled the part at the top that requires
1978 * updating down. */
1979 top_end += xtra_rows;
1980 }
1981 }
1982
1983 /* When not updating the rest, may need to move w_lines[]
1984 * entries. */
1985 if (mod_bot != MAXLNUM && i != j)
1986 {
1987 if (j < i)
1988 {
1989 int x = row + new_rows;
1990
1991 /* move entries in w_lines[] upwards */
1992 for (;;)
1993 {
1994 /* stop at last valid entry in w_lines[] */
1995 if (i >= wp->w_lines_valid)
1996 {
1997 wp->w_lines_valid = j;
1998 break;
1999 }
2000 wp->w_lines[j] = wp->w_lines[i];
2001 /* stop at a line that won't fit */
2002 if (x + (int)wp->w_lines[j].wl_size
2003 > wp->w_height)
2004 {
2005 wp->w_lines_valid = j + 1;
2006 break;
2007 }
2008 x += wp->w_lines[j++].wl_size;
2009 ++i;
2010 }
2011 if (bot_start > x)
2012 bot_start = x;
2013 }
2014 else /* j > i */
2015 {
2016 /* move entries in w_lines[] downwards */
2017 j -= i;
2018 wp->w_lines_valid += j;
2019 if (wp->w_lines_valid > wp->w_height)
2020 wp->w_lines_valid = wp->w_height;
2021 for (i = wp->w_lines_valid; i - j >= idx; --i)
2022 wp->w_lines[i] = wp->w_lines[i - j];
2023
2024 /* The w_lines[] entries for inserted lines are
2025 * now invalid, but wl_size may be used above.
2026 * Reset to zero. */
2027 while (i >= idx)
2028 {
2029 wp->w_lines[i].wl_size = 0;
2030 wp->w_lines[i--].wl_valid = FALSE;
2031 }
2032 }
2033 }
2034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036
2037#ifdef FEAT_FOLDING
2038 /*
2039 * When lines are folded, display one line for all of them.
2040 * Otherwise, display normally (can be several display lines when
2041 * 'wrap' is on).
2042 */
2043 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2044 if (fold_count != 0)
2045 {
2046 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2047 ++row;
2048 --fold_count;
2049 wp->w_lines[idx].wl_folded = TRUE;
2050 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2051# ifdef FEAT_SYN_HL
2052 did_update = DID_FOLD;
2053# endif
2054 }
2055 else
2056#endif
2057 if (idx < wp->w_lines_valid
2058 && wp->w_lines[idx].wl_valid
2059 && wp->w_lines[idx].wl_lnum == lnum
2060 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002061 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 && srow + wp->w_lines[idx].wl_size > wp->w_height
2063#ifdef FEAT_DIFF
2064 && diff_check_fill(wp, lnum) == 0
2065#endif
2066 )
2067 {
2068 /* This line is not going to fit. Don't draw anything here,
2069 * will draw "@ " lines below. */
2070 row = wp->w_height + 1;
2071 }
2072 else
2073 {
2074#ifdef FEAT_SEARCH_EXTRA
2075 prepare_search_hl(wp, lnum);
2076#endif
2077#ifdef FEAT_SYN_HL
2078 /* Let the syntax stuff know we skipped a few lines. */
2079 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002080 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 syntax_end_parsing(syntax_last_parsed + 1);
2082#endif
2083
2084 /*
2085 * Display one line.
2086 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002087 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089#ifdef FEAT_FOLDING
2090 wp->w_lines[idx].wl_folded = FALSE;
2091 wp->w_lines[idx].wl_lastlnum = lnum;
2092#endif
2093#ifdef FEAT_SYN_HL
2094 did_update = DID_LINE;
2095 syntax_last_parsed = lnum;
2096#endif
2097 }
2098
2099 wp->w_lines[idx].wl_lnum = lnum;
2100 wp->w_lines[idx].wl_valid = TRUE;
2101 if (row > wp->w_height) /* past end of screen */
2102 {
2103 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002104 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2106 ++idx;
2107 break;
2108 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002109 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 wp->w_lines[idx].wl_size = row - srow;
2111 ++idx;
2112#ifdef FEAT_FOLDING
2113 lnum += fold_count + 1;
2114#else
2115 ++lnum;
2116#endif
2117 }
2118 else
2119 {
2120 /* This line does not need updating, advance to the next one */
2121 row += wp->w_lines[idx++].wl_size;
2122 if (row > wp->w_height) /* past end of screen */
2123 break;
2124#ifdef FEAT_FOLDING
2125 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2126#else
2127 ++lnum;
2128#endif
2129#ifdef FEAT_SYN_HL
2130 did_update = DID_NONE;
2131#endif
2132 }
2133
2134 if (lnum > buf->b_ml.ml_line_count)
2135 {
2136 eof = TRUE;
2137 break;
2138 }
2139 }
2140 /*
2141 * End of loop over all window lines.
2142 */
2143
2144
2145 if (idx > wp->w_lines_valid)
2146 wp->w_lines_valid = idx;
2147
2148#ifdef FEAT_SYN_HL
2149 /*
2150 * Let the syntax stuff know we stop parsing here.
2151 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002152 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 syntax_end_parsing(syntax_last_parsed + 1);
2154#endif
2155
2156 /*
2157 * If we didn't hit the end of the file, and we didn't finish the last
2158 * line we were working on, then the line didn't fit.
2159 */
2160 wp->w_empty_rows = 0;
2161#ifdef FEAT_DIFF
2162 wp->w_filler_rows = 0;
2163#endif
2164 if (!eof && !didline)
2165 {
2166 if (lnum == wp->w_topline)
2167 {
2168 /*
2169 * Single line that does not fit!
2170 * Don't overwrite it, it can be edited.
2171 */
2172 wp->w_botline = lnum + 1;
2173 }
2174#ifdef FEAT_DIFF
2175 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2176 {
2177 /* Window ends in filler lines. */
2178 wp->w_botline = lnum;
2179 wp->w_filler_rows = wp->w_height - srow;
2180 }
2181#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002182 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2183 {
2184 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2185
2186 /*
2187 * Last line isn't finished: Display "@@@" in the last screen line.
2188 */
2189 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002190 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002191 screen_fill(scr_row, scr_row + 1,
2192 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002193 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002194 set_empty_rows(wp, srow);
2195 wp->w_botline = lnum;
2196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2198 {
2199 /*
2200 * Last line isn't finished: Display "@@@" at the end.
2201 */
2202 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2203 W_WINROW(wp) + wp->w_height,
2204 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002205 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 set_empty_rows(wp, srow);
2207 wp->w_botline = lnum;
2208 }
2209 else
2210 {
2211 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2212 wp->w_botline = lnum;
2213 }
2214 }
2215 else
2216 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002217#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 draw_vsep_win(wp, row);
2219#endif
2220 if (eof) /* we hit the end of the file */
2221 {
2222 wp->w_botline = buf->b_ml.ml_line_count + 1;
2223#ifdef FEAT_DIFF
2224 j = diff_check_fill(wp, wp->w_botline);
2225 if (j > 0 && !wp->w_botfill)
2226 {
2227 /*
2228 * Display filler lines at the end of the file
2229 */
2230 if (char2cells(fill_diff) > 1)
2231 i = '-';
2232 else
2233 i = fill_diff;
2234 if (row + j > wp->w_height)
2235 j = wp->w_height - row;
2236 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2237 row += j;
2238 }
2239#endif
2240 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002241 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 wp->w_botline = lnum;
2243
2244 /* make sure the rest of the screen is blank */
2245 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002246 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
2248
2249 /* Reset the type of redrawing required, the window has been updated. */
2250 wp->w_redr_type = 0;
2251#ifdef FEAT_DIFF
2252 wp->w_old_topfill = wp->w_topfill;
2253 wp->w_old_botfill = wp->w_botfill;
2254#endif
2255
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002256 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
2258 /*
2259 * There is a trick with w_botline. If we invalidate it on each
2260 * change that might modify it, this will cause a lot of expensive
2261 * calls to plines() in update_topline() each time. Therefore the
2262 * value of w_botline is often approximated, and this value is used to
2263 * compute the value of w_topline. If the value of w_botline was
2264 * wrong, check that the value of w_topline is correct (cursor is on
2265 * the visible part of the text). If it's not, we need to redraw
2266 * again. Mostly this just means scrolling up a few lines, so it
2267 * doesn't look too bad. Only do this for the current window (where
2268 * changes are relevant).
2269 */
2270 wp->w_valid |= VALID_BOTLINE;
2271 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2272 {
2273 recursive = TRUE;
2274 curwin->w_valid &= ~VALID_TOPLINE;
2275 update_topline(); /* may invalidate w_botline again */
2276 if (must_redraw != 0)
2277 {
2278 /* Don't update for changes in buffer again. */
2279 i = curbuf->b_mod_set;
2280 curbuf->b_mod_set = FALSE;
2281 win_update(curwin);
2282 must_redraw = 0;
2283 curbuf->b_mod_set = i;
2284 }
2285 recursive = FALSE;
2286 }
2287 }
2288
2289#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2290 /* restore got_int, unless CTRL-C was hit while redrawing */
2291 if (!got_int)
2292 got_int = save_got_int;
2293#endif
2294}
2295
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296/*
2297 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2298 * as the filler character.
2299 */
2300 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002301win_draw_end(
2302 win_T *wp,
2303 int c1,
2304 int c2,
2305 int row,
2306 int endrow,
2307 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2310 int n = 0;
2311# define FDC_OFF n
2312#else
2313# define FDC_OFF 0
2314#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002315#ifdef FEAT_FOLDING
2316 int fdc = compute_foldcolumn(wp, 0);
2317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
2319#ifdef FEAT_RIGHTLEFT
2320 if (wp->w_p_rl)
2321 {
2322 /* No check for cmdline window: should never be right-left. */
2323# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002324 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326 if (n > 0)
2327 {
2328 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002329 if (n > W_WIDTH(wp))
2330 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2332 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002333 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335# endif
2336# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002337 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 int nn = n + 2;
2340
2341 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002342 if (nn > W_WIDTH(wp))
2343 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2345 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002346 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 n = nn;
2348 }
2349# endif
2350 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2351 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002352 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2354 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002355 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357 else
2358#endif
2359 {
2360#ifdef FEAT_CMDWIN
2361 if (cmdwin_type != 0 && wp == curwin)
2362 {
2363 /* draw the cmdline character in the leftmost column */
2364 n = 1;
2365 if (n > wp->w_width)
2366 n = wp->w_width;
2367 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2368 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002369 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371#endif
2372#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002373 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002375 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
2377 /* draw the fold column at the left */
2378 if (nn > W_WIDTH(wp))
2379 nn = W_WIDTH(wp);
2380 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2381 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002382 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 n = nn;
2384 }
2385#endif
2386#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002387 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
2389 int nn = n + 2;
2390
2391 /* draw the sign column after the fold column */
2392 if (nn > W_WIDTH(wp))
2393 nn = W_WIDTH(wp);
2394 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2395 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002396 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 n = nn;
2398 }
2399#endif
2400 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2401 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002402 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 set_empty_rows(wp, row);
2405}
2406
Bram Moolenaar1a384422010-07-14 19:53:30 +02002407#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002408static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002409
2410/*
2411 * Advance **color_cols and return TRUE when there are columns to draw.
2412 */
2413 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002414advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002415{
2416 while (**color_cols >= 0 && vcol > **color_cols)
2417 ++*color_cols;
2418 return (**color_cols >= 0);
2419}
2420#endif
2421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#ifdef FEAT_FOLDING
2423/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002424 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2425 * space is available for window "wp", minus "col".
2426 */
2427 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002428compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002429{
2430 int fdc = wp->w_p_fdc;
2431 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2432 int wwidth = W_WIDTH(wp);
2433
2434 if (fdc > wwidth - (col + wmw))
2435 fdc = wwidth - (col + wmw);
2436 return fdc;
2437}
2438
2439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 * Display one folded line.
2441 */
2442 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002443fold_line(
2444 win_T *wp,
2445 long fold_count,
2446 foldinfo_T *foldinfo,
2447 linenr_T lnum,
2448 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002450 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 pos_T *top, *bot;
2452 linenr_T lnume = lnum + fold_count - 1;
2453 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002454 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 int col;
2457 int txtcol;
2458 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002459 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
2461 /* Build the fold line:
2462 * 1. Add the cmdwin_type for the command-line window
2463 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002464 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 * 4. Compose the text
2466 * 5. Add the text
2467 * 6. set highlighting for the Visual area an other text
2468 */
2469 col = 0;
2470
2471 /*
2472 * 1. Add the cmdwin_type for the command-line window
2473 * Ignores 'rightleft', this window is never right-left.
2474 */
2475#ifdef FEAT_CMDWIN
2476 if (cmdwin_type != 0 && wp == curwin)
2477 {
2478 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002479 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480#ifdef FEAT_MBYTE
2481 if (enc_utf8)
2482 ScreenLinesUC[off] = 0;
2483#endif
2484 ++col;
2485 }
2486#endif
2487
2488 /*
2489 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002490 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002492 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 if (fdc > 0)
2494 {
2495 fill_foldcolumn(buf, wp, TRUE, lnum);
2496#ifdef FEAT_RIGHTLEFT
2497 if (wp->w_p_rl)
2498 {
2499 int i;
2500
2501 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002502 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 /* reverse the fold column */
2504 for (i = 0; i < fdc; ++i)
2505 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2506 }
2507 else
2508#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002509 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 col += fdc;
2511 }
2512
2513#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002514# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2515 for (ri = 0; ri < l; ++ri) \
2516 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2517 else \
2518 for (ri = 0; ri < l; ++ri) \
2519 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002521# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2522 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523#endif
2524
Bram Moolenaar64486672010-05-16 15:46:46 +02002525 /* Set all attributes of the 'number' or 'relativenumber' column and the
2526 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002527 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529#ifdef FEAT_SIGNS
2530 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002531 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 len = W_WIDTH(wp) - col;
2534 if (len > 0)
2535 {
2536 if (len > 2)
2537 len = 2;
2538# ifdef FEAT_RIGHTLEFT
2539 if (wp->w_p_rl)
2540 /* the line number isn't reversed */
2541 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002542 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 else
2544# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002545 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 col += len;
2547 }
2548 }
2549#endif
2550
2551 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002552 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002554 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
2556 len = W_WIDTH(wp) - col;
2557 if (len > 0)
2558 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002559 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002560 long num;
2561 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002562
2563 if (len > w + 1)
2564 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002565
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002566 if (wp->w_p_nu && !wp->w_p_rnu)
2567 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002568 num = (long)lnum;
2569 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002570 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002571 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002572 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002573 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002574 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002575 /* 'number' + 'relativenumber': cursor line shows absolute
2576 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002577 num = lnum;
2578 fmt = "%-*ld ";
2579 }
2580 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002581
Bram Moolenaar700e7342013-01-30 12:31:36 +01002582 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583#ifdef FEAT_RIGHTLEFT
2584 if (wp->w_p_rl)
2585 /* the line number isn't reversed */
2586 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002587 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 else
2589#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002590 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 col += len;
2592 }
2593 }
2594
2595 /*
2596 * 4. Compose the folded-line string with 'foldtext', if set.
2597 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002598 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 txtcol = col; /* remember where text starts */
2601
2602 /*
2603 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2604 * Right-left text is put in columns 0 - number-col, normal text is put
2605 * in columns number-col - window-width.
2606 */
2607#ifdef FEAT_MBYTE
2608 if (has_mbyte)
2609 {
2610 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002611 int u8c, u8cc[MAX_MCO];
2612 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 int idx;
2614 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002615 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616# ifdef FEAT_ARABIC
2617 int prev_c = 0; /* previous Arabic character */
2618 int prev_c1 = 0; /* first composing char for prev_c */
2619# endif
2620
2621# ifdef FEAT_RIGHTLEFT
2622 if (wp->w_p_rl)
2623 idx = off;
2624 else
2625# endif
2626 idx = off + col;
2627
2628 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2629 for (p = text; *p != NUL; )
2630 {
2631 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002632 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (col + cells > W_WIDTH(wp)
2634# ifdef FEAT_RIGHTLEFT
2635 - (wp->w_p_rl ? col : 0)
2636# endif
2637 )
2638 break;
2639 ScreenLines[idx] = *p;
2640 if (enc_utf8)
2641 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002642 u8c = utfc_ptr2char(p, u8cc);
2643 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {
2645 ScreenLinesUC[idx] = 0;
2646#ifdef FEAT_ARABIC
2647 prev_c = u8c;
2648#endif
2649 }
2650 else
2651 {
2652#ifdef FEAT_ARABIC
2653 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2654 {
2655 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002656 int pc, pc1, nc;
2657 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 int firstbyte = *p;
2659
2660 /* The idea of what is the previous and next
2661 * character depends on 'rightleft'. */
2662 if (wp->w_p_rl)
2663 {
2664 pc = prev_c;
2665 pc1 = prev_c1;
2666 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002667 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
2669 else
2670 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002671 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002673 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675 prev_c = u8c;
2676
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002677 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 pc, pc1, nc);
2679 ScreenLines[idx] = firstbyte;
2680 }
2681 else
2682 prev_c = u8c;
2683#endif
2684 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002685#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (u8c >= 0x10000)
2687 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2688 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002691 for (i = 0; i < Screen_mco; ++i)
2692 {
2693 ScreenLinesC[i][idx] = u8cc[i];
2694 if (u8cc[i] == 0)
2695 break;
2696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698 if (cells > 1)
2699 ScreenLines[idx + 1] = 0;
2700 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002701 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2702 /* double-byte single width character */
2703 ScreenLines2[idx] = p[1];
2704 else if (cells > 1)
2705 /* double-width character */
2706 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 col += cells;
2708 idx += cells;
2709 p += c_len;
2710 }
2711 }
2712 else
2713#endif
2714 {
2715 len = (int)STRLEN(text);
2716 if (len > W_WIDTH(wp) - col)
2717 len = W_WIDTH(wp) - col;
2718 if (len > 0)
2719 {
2720#ifdef FEAT_RIGHTLEFT
2721 if (wp->w_p_rl)
2722 STRNCPY(current_ScreenLine, text, len);
2723 else
2724#endif
2725 STRNCPY(current_ScreenLine + col, text, len);
2726 col += len;
2727 }
2728 }
2729
2730 /* Fill the rest of the line with the fold filler */
2731#ifdef FEAT_RIGHTLEFT
2732 if (wp->w_p_rl)
2733 col -= txtcol;
2734#endif
2735 while (col < W_WIDTH(wp)
2736#ifdef FEAT_RIGHTLEFT
2737 - (wp->w_p_rl ? txtcol : 0)
2738#endif
2739 )
2740 {
2741#ifdef FEAT_MBYTE
2742 if (enc_utf8)
2743 {
2744 if (fill_fold >= 0x80)
2745 {
2746 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002747 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002748 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002753 ScreenLines[off + col] = fill_fold;
2754 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002755 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002759 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761
2762 if (text != buf)
2763 vim_free(text);
2764
2765 /*
2766 * 6. set highlighting for the Visual area an other text.
2767 * If all folded lines are in the Visual area, highlight the line.
2768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2770 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002771 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
2773 /* Visual is after curwin->w_cursor */
2774 top = &curwin->w_cursor;
2775 bot = &VIsual;
2776 }
2777 else
2778 {
2779 /* Visual is before curwin->w_cursor */
2780 top = &VIsual;
2781 bot = &curwin->w_cursor;
2782 }
2783 if (lnum >= top->lnum
2784 && lnume <= bot->lnum
2785 && (VIsual_mode != 'v'
2786 || ((lnum > top->lnum
2787 || (lnum == top->lnum
2788 && top->col == 0))
2789 && (lnume < bot->lnum
2790 || (lnume == bot->lnum
2791 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002792 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
2794 if (VIsual_mode == Ctrl_V)
2795 {
2796 /* Visual block mode: highlight the chars part of the block */
2797 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2798 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002799 if (wp->w_old_cursor_lcol != MAXCOL
2800 && wp->w_old_cursor_lcol + txtcol
2801 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 len = wp->w_old_cursor_lcol;
2803 else
2804 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002805 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002806 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808 }
2809 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002812 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002817#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002818 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2819 if (wp->w_p_cc_cols)
2820 {
2821 int i = 0;
2822 int j = wp->w_p_cc_cols[i];
2823 int old_txtcol = txtcol;
2824
2825 while (j > -1)
2826 {
2827 txtcol += j;
2828 if (wp->w_p_wrap)
2829 txtcol -= wp->w_skipcol;
2830 else
2831 txtcol -= wp->w_leftcol;
2832 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2833 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002834 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002835 txtcol = old_txtcol;
2836 j = wp->w_p_cc_cols[++i];
2837 }
2838 }
2839
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002840 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002841 if (wp->w_p_cuc)
2842 {
2843 txtcol += wp->w_virtcol;
2844 if (wp->w_p_wrap)
2845 txtcol -= wp->w_skipcol;
2846 else
2847 txtcol -= wp->w_leftcol;
2848 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2849 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002850 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002851 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2855 (int)W_WIDTH(wp), FALSE);
2856
2857 /*
2858 * Update w_cline_height and w_cline_folded if the cursor line was
2859 * updated (saves a call to plines() later).
2860 */
2861 if (wp == curwin
2862 && lnum <= curwin->w_cursor.lnum
2863 && lnume >= curwin->w_cursor.lnum)
2864 {
2865 curwin->w_cline_row = row;
2866 curwin->w_cline_height = 1;
2867 curwin->w_cline_folded = TRUE;
2868 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2869 }
2870}
2871
2872/*
2873 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2874 */
2875 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002876copy_text_attr(
2877 int off,
2878 char_u *buf,
2879 int len,
2880 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002882 int i;
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 mch_memmove(ScreenLines + off, buf, (size_t)len);
2885# ifdef FEAT_MBYTE
2886 if (enc_utf8)
2887 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2888# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002889 for (i = 0; i < len; ++i)
2890 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891}
2892
2893/*
2894 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002895 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 */
2897 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002898fill_foldcolumn(
2899 char_u *p,
2900 win_T *wp,
2901 int closed, /* TRUE of FALSE */
2902 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 int i = 0;
2905 int level;
2906 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002907 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002908 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002911 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
2913 level = win_foldinfo.fi_level;
2914 if (level > 0)
2915 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002916 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002917 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 /* If the column is too narrow, we start at the lowest level that
2920 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002921 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (first_level < 1)
2923 first_level = 1;
2924
Bram Moolenaar1c934292015-01-27 16:39:29 +01002925 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
2927 if (win_foldinfo.fi_lnum == lnum
2928 && first_level + i >= win_foldinfo.fi_low_level)
2929 p[i] = '-';
2930 else if (first_level == 1)
2931 p[i] = '|';
2932 else if (first_level + i <= 9)
2933 p[i] = '0' + first_level + i;
2934 else
2935 p[i] = '>';
2936 if (first_level + i == level)
2937 break;
2938 }
2939 }
2940 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002941 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942}
2943#endif /* FEAT_FOLDING */
2944
2945/*
2946 * Display line "lnum" of window 'wp' on the screen.
2947 * Start at row "startrow", stop when "endrow" is reached.
2948 * wp->w_virtcol needs to be valid.
2949 *
2950 * Return the number of last row the line occupies.
2951 */
2952 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002953win_line(
2954 win_T *wp,
2955 linenr_T lnum,
2956 int startrow,
2957 int endrow,
2958 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002960 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2962 int c = 0; /* init for GCC */
2963 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002964#ifdef FEAT_LINEBREAK
2965 long vcol_sbr = -1; /* virtual column after showbreak */
2966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 long vcol_prev = -1; /* "vcol" of previous character */
2968 char_u *line; /* current line */
2969 char_u *ptr; /* current position in "line" */
2970 int row; /* row in the window, excl w_winrow */
2971 int screen_row; /* row on the screen, incl w_winrow */
2972
2973 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2974 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002975 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002976 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 int c_extra = NUL; /* extra chars, all the same */
2978 int extra_attr = 0; /* attributes when n_extra != 0 */
2979 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2980 displaying lcs_eol at end-of-line */
2981 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2982 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2983
2984 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2985 int saved_n_extra = 0;
2986 char_u *saved_p_extra = NULL;
2987 int saved_c_extra = 0;
2988 int saved_char_attr = 0;
2989
2990 int n_attr = 0; /* chars with special attr */
2991 int saved_attr2 = 0; /* char_attr saved for n_attr */
2992 int n_attr3 = 0; /* chars with overruling special attr */
2993 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2994
2995 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2996
2997 int fromcol, tocol; /* start/end of inverting */
2998 int fromcol_prev = -2; /* start of inverting after cursor */
2999 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003001 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 pos_T pos;
3003 long v;
3004
3005 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003006 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3008 in this line */
3009 int attr = 0; /* attributes for area highlighting */
3010 int area_attr = 0; /* attributes desired by highlighting */
3011 int search_attr = 0; /* attributes desired by 'hlsearch' */
3012#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003013 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 int syntax_attr = 0; /* attributes desired by syntax */
3015 int has_syntax = FALSE; /* this buffer has syntax highl. */
3016 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003017 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003018 int draw_color_col = FALSE; /* highlight colorcolumn */
3019 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003020#endif
3021#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003022 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003023# define SPWORDLEN 150
3024 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003025 int nextlinecol = 0; /* column where nextline[] starts */
3026 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003027 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003028 int spell_attr = 0; /* attributes desired by spelling */
3029 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003030 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3031 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003032 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003033 static int cap_col = -1; /* column to check for Cap word */
3034 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003035 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#endif
3037 int extra_check; /* has syntax or linebreak */
3038#ifdef FEAT_MBYTE
3039 int multi_attr = 0; /* attributes desired by multibyte */
3040 int mb_l = 1; /* multi-byte byte length */
3041 int mb_c = 0; /* decoded multi-byte character */
3042 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003043 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#endif
3045#ifdef FEAT_DIFF
3046 int filler_lines; /* nr of filler lines to be drawn */
3047 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003048 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 int change_start = MAXCOL; /* first col of changed area */
3050 int change_end = -1; /* last col of changed area */
3051#endif
3052 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3053#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003054 int need_showbreak = FALSE; /* overlong line, skipping first x
3055 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003057#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3058 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003060 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061#endif
3062#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003063 matchitem_T *cur; /* points to the match list */
3064 match_T *shl; /* points to search_hl or a match */
3065 int shl_flag; /* flag to indicate whether search_hl
3066 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003067 int pos_inprogress; /* marks that position match search is
3068 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003069 int prevcol_hl_flag; /* flag to indicate whether prevcol
3070 equals startcol of search_hl or one
3071 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072#endif
3073#ifdef FEAT_ARABIC
3074 int prev_c = 0; /* previous Arabic character */
3075 int prev_c1 = 0; /* first composing char for prev_c */
3076#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003077#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003078 int did_line_attr = 0;
3079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081 /* draw_state: items that are drawn in sequence: */
3082#define WL_START 0 /* nothing done yet */
3083#ifdef FEAT_CMDWIN
3084# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3085#else
3086# define WL_CMDLINE WL_START
3087#endif
3088#ifdef FEAT_FOLDING
3089# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3090#else
3091# define WL_FOLD WL_CMDLINE
3092#endif
3093#ifdef FEAT_SIGNS
3094# define WL_SIGN WL_FOLD + 1 /* column for signs */
3095#else
3096# define WL_SIGN WL_FOLD /* column for signs */
3097#endif
3098#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003099#ifdef FEAT_LINEBREAK
3100# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003102# define WL_BRI WL_NR
3103#endif
3104#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3105# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3106#else
3107# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#endif
3109#define WL_LINE WL_SBR + 1 /* text in the line */
3110 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003111#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 int feedback_col = 0;
3113 int feedback_old_attr = -1;
3114#endif
3115
Bram Moolenaar860cae12010-06-05 23:22:07 +02003116#ifdef FEAT_CONCEAL
3117 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003118 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003119 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003120 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003121 int is_concealing = FALSE;
3122 int boguscols = 0; /* nonexistent columns added to force
3123 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003124 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003125 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003126 int match_conc = 0; /* cchar for match functions */
3127 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003128 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003129# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003130# define FIX_FOR_BOGUSCOLS \
3131 { \
3132 n_extra += vcol_off; \
3133 vcol -= vcol_off; \
3134 vcol_off = 0; \
3135 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003136 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003137 boguscols = 0; \
3138 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003139#else
3140# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143 if (startrow > endrow) /* past the end already! */
3144 return startrow;
3145
3146 row = startrow;
3147 screen_row = row + W_WINROW(wp);
3148
3149 /*
3150 * To speed up the loop below, set extra_check when there is linebreak,
3151 * trailing white space and/or syntax processing to be done.
3152 */
3153#ifdef FEAT_LINEBREAK
3154 extra_check = wp->w_p_lbr;
3155#else
3156 extra_check = 0;
3157#endif
3158#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003159 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
3161 /* Prepare for syntax highlighting in this line. When there is an
3162 * error, stop syntax highlighting. */
3163 save_did_emsg = did_emsg;
3164 did_emsg = FALSE;
3165 syntax_start(wp, lnum);
3166 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003167 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 else
3169 {
3170 did_emsg = save_did_emsg;
3171 has_syntax = TRUE;
3172 extra_check = TRUE;
3173 }
3174 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003175
3176 /* Check for columns to display for 'colorcolumn'. */
3177 color_cols = wp->w_p_cc_cols;
3178 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003179 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003180#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003181
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003182#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003183 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003184 && *wp->w_s->b_p_spl != NUL
3185 && wp->w_s->b_langp.ga_len > 0
3186 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003187 {
3188 /* Prepare for spell checking. */
3189 has_spell = TRUE;
3190 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003191
3192 /* Get the start of the next line, so that words that wrap to the next
3193 * line are found too: "et<line-break>al.".
3194 * Trick: skip a few chars for C/shell/Vim comments */
3195 nextline[SPWORDLEN] = NUL;
3196 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3197 {
3198 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3199 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3200 }
3201
3202 /* When a word wrapped from the previous line the start of the current
3203 * line is valid. */
3204 if (lnum == checked_lnum)
3205 cur_checked_col = checked_col;
3206 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003207
3208 /* When there was a sentence end in the previous line may require a
3209 * word starting with capital in this line. In line 1 always check
3210 * the first word. */
3211 if (lnum != capcol_lnum)
3212 cap_col = -1;
3213 if (lnum == 1)
3214 cap_col = 0;
3215 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#endif
3218
3219 /*
3220 * handle visual active in this window
3221 */
3222 fromcol = -10;
3223 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3225 {
3226 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003227 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
3229 top = &curwin->w_cursor;
3230 bot = &VIsual;
3231 }
3232 else /* Visual is before curwin->w_cursor */
3233 {
3234 top = &VIsual;
3235 bot = &curwin->w_cursor;
3236 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003237 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (VIsual_mode == Ctrl_V) /* block mode */
3239 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003240 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
3242 fromcol = wp->w_old_cursor_fcol;
3243 tocol = wp->w_old_cursor_lcol;
3244 }
3245 }
3246 else /* non-block mode */
3247 {
3248 if (lnum > top->lnum && lnum <= bot->lnum)
3249 fromcol = 0;
3250 else if (lnum == top->lnum)
3251 {
3252 if (VIsual_mode == 'V') /* linewise */
3253 fromcol = 0;
3254 else
3255 {
3256 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3257 if (gchar_pos(top) == NUL)
3258 tocol = fromcol + 1;
3259 }
3260 }
3261 if (VIsual_mode != 'V' && lnum == bot->lnum)
3262 {
3263 if (*p_sel == 'e' && bot->col == 0
3264#ifdef FEAT_VIRTUALEDIT
3265 && bot->coladd == 0
3266#endif
3267 )
3268 {
3269 fromcol = -10;
3270 tocol = MAXCOL;
3271 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003272 else if (bot->col == MAXCOL)
3273 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 else
3275 {
3276 pos = *bot;
3277 if (*p_sel == 'e')
3278 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3279 else
3280 {
3281 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3282 ++tocol;
3283 }
3284 }
3285 }
3286 }
3287
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 /* Check if the character under the cursor should not be inverted */
3289 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003290#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 )
3294 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
3296 /* if inverting in this line set area_highlighting */
3297 if (fromcol >= 0)
3298 {
3299 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003300 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003302 if ((clip_star.available && !clip_star.owned
3303 && clip_isautosel_star())
3304 || (clip_plus.available && !clip_plus.owned
3305 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003306 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#endif
3308 }
3309 }
3310
3311 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003312 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003314 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 && wp == curwin
3316 && lnum >= curwin->w_cursor.lnum
3317 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3318 {
3319 if (lnum == curwin->w_cursor.lnum)
3320 getvcol(curwin, &(curwin->w_cursor),
3321 (colnr_T *)&fromcol, NULL, NULL);
3322 else
3323 fromcol = 0;
3324 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3325 {
3326 pos.lnum = lnum;
3327 pos.col = search_match_endcol;
3328 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3329 }
3330 else
3331 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003332 /* do at least one character; happens when past end of line */
3333 if (fromcol == tocol)
3334 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003336 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338
3339#ifdef FEAT_DIFF
3340 filler_lines = diff_check(wp, lnum);
3341 if (filler_lines < 0)
3342 {
3343 if (filler_lines == -1)
3344 {
3345 if (diff_find_change(wp, lnum, &change_start, &change_end))
3346 diff_hlf = HLF_ADD; /* added line */
3347 else if (change_start == 0)
3348 diff_hlf = HLF_TXD; /* changed text */
3349 else
3350 diff_hlf = HLF_CHD; /* changed line */
3351 }
3352 else
3353 diff_hlf = HLF_ADD; /* added line */
3354 filler_lines = 0;
3355 area_highlighting = TRUE;
3356 }
3357 if (lnum == wp->w_topline)
3358 filler_lines = wp->w_topfill;
3359 filler_todo = filler_lines;
3360#endif
3361
3362#ifdef LINE_ATTR
3363# ifdef FEAT_SIGNS
3364 /* If this line has a sign with line highlighting set line_attr. */
3365 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3366 if (v != 0)
3367 line_attr = sign_get_attr((int)v, TRUE);
3368# endif
3369# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3370 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003371 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003372 line_attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373# endif
3374 if (line_attr != 0)
3375 area_highlighting = TRUE;
3376#endif
3377
3378 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3379 ptr = line;
3380
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003381#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003382 if (has_spell)
3383 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003384 /* For checking first word with a capital skip white space. */
3385 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003386 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003387
Bram Moolenaar30abd282005-06-22 22:35:10 +00003388 /* To be able to spell-check over line boundaries copy the end of the
3389 * current line into nextline[]. Above the start of the next line was
3390 * copied to nextline[SPWORDLEN]. */
3391 if (nextline[SPWORDLEN] == NUL)
3392 {
3393 /* No next line or it is empty. */
3394 nextlinecol = MAXCOL;
3395 nextline_idx = 0;
3396 }
3397 else
3398 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003399 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003400 if (v < SPWORDLEN)
3401 {
3402 /* Short line, use it completely and append the start of the
3403 * next line. */
3404 nextlinecol = 0;
3405 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003406 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003407 nextline_idx = v + 1;
3408 }
3409 else
3410 {
3411 /* Long line, use only the last SPWORDLEN bytes. */
3412 nextlinecol = v - SPWORDLEN;
3413 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3414 nextline_idx = SPWORDLEN + 1;
3415 }
3416 }
3417 }
3418#endif
3419
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003420 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003422 if (lcs_space || lcs_trail)
3423 extra_check = TRUE;
3424 /* find start of trailing whitespace */
3425 if (lcs_trail)
3426 {
3427 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003428 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003429 --trailcol;
3430 trailcol += (colnr_T) (ptr - line);
3431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433
3434 /*
3435 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3436 * first character to be displayed.
3437 */
3438 if (wp->w_p_wrap)
3439 v = wp->w_skipcol;
3440 else
3441 v = wp->w_leftcol;
3442 if (v > 0)
3443 {
3444#ifdef FEAT_MBYTE
3445 char_u *prev_ptr = ptr;
3446#endif
3447 while (vcol < v && *ptr != NUL)
3448 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003449 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 vcol += c;
3451#ifdef FEAT_MBYTE
3452 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003454 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003457 /* When:
3458 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003459 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003460 * - 'virtualedit' is set, or
3461 * - the visual mode is active,
3462 * the end of the line may be before the start of the displayed part.
3463 */
3464 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003465#ifdef FEAT_SYN_HL
3466 wp->w_p_cuc || draw_color_col ||
3467#endif
3468#ifdef FEAT_VIRTUALEDIT
3469 virtual_active() ||
3470#endif
3471 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476 /* Handle a character that's not completely on the screen: Put ptr at
3477 * that character but skip the first few screen characters. */
3478 if (vcol > v)
3479 {
3480 vcol -= c;
3481#ifdef FEAT_MBYTE
3482 ptr = prev_ptr;
3483#else
3484 --ptr;
3485#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003486 /* If the character fits on the screen, don't need to skip it.
3487 * Except for a TAB. */
3488 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003489#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003490 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003491#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003492 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003493 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495
3496 /*
3497 * Adjust for when the inverted text is before the screen,
3498 * and when the start of the inverted text is before the screen.
3499 */
3500 if (tocol <= vcol)
3501 fromcol = 0;
3502 else if (fromcol >= 0 && fromcol < vcol)
3503 fromcol = vcol;
3504
3505#ifdef FEAT_LINEBREAK
3506 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3507 if (wp->w_p_wrap)
3508 need_showbreak = TRUE;
3509#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003510#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003511 /* When spell checking a word we need to figure out the start of the
3512 * word and if it's badly spelled or not. */
3513 if (has_spell)
3514 {
3515 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003516 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003517 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003518
3519 pos = wp->w_cursor;
3520 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003521 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003522 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003523
3524 /* spell_move_to() may call ml_get() and make "line" invalid */
3525 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3526 ptr = line + linecol;
3527
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003528 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003529 {
3530 /* no bad word found at line start, don't check until end of a
3531 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003532 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003533 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003534 }
3535 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003536 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003537 /* bad word found, use attributes until end of word */
3538 word_end = wp->w_cursor.col + len + 1;
3539
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003540 /* Turn index into actual attributes. */
3541 if (spell_hlf != HLF_COUNT)
3542 spell_attr = highlight_attr[spell_hlf];
3543 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003544 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003545
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003546# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003547 /* Need to restart syntax highlighting for this line. */
3548 if (has_syntax)
3549 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003550# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003551 }
3552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554
3555 /*
3556 * Correct highlighting for cursor that can't be disabled.
3557 * Avoids having to check this for each character.
3558 */
3559 if (fromcol >= 0)
3560 {
3561 if (noinvcur)
3562 {
3563 if ((colnr_T)fromcol == wp->w_virtcol)
3564 {
3565 /* highlighting starts at cursor, let it start just after the
3566 * cursor */
3567 fromcol_prev = fromcol;
3568 fromcol = -1;
3569 }
3570 else if ((colnr_T)fromcol < wp->w_virtcol)
3571 /* restart highlighting after the cursor */
3572 fromcol_prev = wp->w_virtcol;
3573 }
3574 if (fromcol >= tocol)
3575 fromcol = -1;
3576 }
3577
3578#ifdef FEAT_SEARCH_EXTRA
3579 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003580 * Handle highlighting the last used search pattern and matches.
3581 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003583 cur = wp->w_match_head;
3584 shl_flag = FALSE;
3585 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003587 if (shl_flag == FALSE)
3588 {
3589 shl = &search_hl;
3590 shl_flag = TRUE;
3591 }
3592 else
3593 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003594 shl->startcol = MAXCOL;
3595 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003597 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003598 v = (long)(ptr - line);
3599 if (cur != NULL)
3600 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003601 next_search_hl(wp, shl, lnum, (colnr_T)v,
3602 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003603
3604 /* Need to get the line again, a multi-line regexp may have made it
3605 * invalid. */
3606 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3607 ptr = line + v;
3608
3609 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003611 if (shl->lnum == lnum)
3612 shl->startcol = shl->rm.startpos[0].col;
3613 else
3614 shl->startcol = 0;
3615 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3616 - shl->rm.startpos[0].lnum)
3617 shl->endcol = shl->rm.endpos[0].col;
3618 else
3619 shl->endcol = MAXCOL;
3620 /* Highlight one character for an empty match. */
3621 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003624 if (has_mbyte && line[shl->endcol] != NUL)
3625 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3626 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003628 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003630 if ((long)shl->startcol < v) /* match at leftcol */
3631 {
3632 shl->attr_cur = shl->attr;
3633 search_attr = shl->attr;
3634 }
3635 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003637 if (shl != &search_hl && cur != NULL)
3638 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640#endif
3641
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003642#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003643 /* Cursor line highlighting for 'cursorline' in the current window. Not
3644 * when Visual mode is active, because it's not clear what is selected
3645 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003646 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003647 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003648 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003649 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003650 area_highlighting = TRUE;
3651 }
3652#endif
3653
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003654 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 col = 0;
3656#ifdef FEAT_RIGHTLEFT
3657 if (wp->w_p_rl)
3658 {
3659 /* Rightleft window: process the text in the normal direction, but put
3660 * it in current_ScreenLine[] from right to left. Start at the
3661 * rightmost column of the window. */
3662 col = W_WIDTH(wp) - 1;
3663 off += col;
3664 }
3665#endif
3666
3667 /*
3668 * Repeat for the whole displayed line.
3669 */
3670 for (;;)
3671 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003672#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003673 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 /* Skip this quickly when working on the text. */
3676 if (draw_state != WL_LINE)
3677 {
3678#ifdef FEAT_CMDWIN
3679 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3680 {
3681 draw_state = WL_CMDLINE;
3682 if (cmdwin_type != 0 && wp == curwin)
3683 {
3684 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003686 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003687 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689 }
3690#endif
3691
3692#ifdef FEAT_FOLDING
3693 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3694 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003695 int fdc = compute_foldcolumn(wp, 0);
3696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003698 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003700 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003701 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003702 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003703 p_extra_free = alloc(12 + 1);
3704
3705 if (p_extra_free != NULL)
3706 {
3707 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3708 n_extra = fdc;
3709 p_extra_free[n_extra] = NUL;
3710 p_extra = p_extra_free;
3711 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003712 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715 }
3716#endif
3717
3718#ifdef FEAT_SIGNS
3719 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3720 {
3721 draw_state = WL_SIGN;
3722 /* Show the sign column when there are any signs in this
3723 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003724 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003726 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003728 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729# endif
3730
3731 /* Draw two cells with the sign value or blank. */
3732 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003733 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 n_extra = 2;
3735
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003736 if (row == startrow
3737#ifdef FEAT_DIFF
3738 + filler_lines && filler_todo <= 0
3739#endif
3740 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
3742 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3743 SIGN_TEXT);
3744# ifdef FEAT_SIGN_ICONS
3745 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3746 SIGN_ICON);
3747 if (gui.in_use && icon_sign != 0)
3748 {
3749 /* Use the image in this position. */
3750 c_extra = SIGN_BYTE;
3751# ifdef FEAT_NETBEANS_INTG
3752 if (buf_signcount(wp->w_buffer, lnum) > 1)
3753 c_extra = MULTISIGN_BYTE;
3754# endif
3755 char_attr = icon_sign;
3756 }
3757 else
3758# endif
3759 if (text_sign != 0)
3760 {
3761 p_extra = sign_get_text(text_sign);
3762 if (p_extra != NULL)
3763 {
3764 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003765 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767 char_attr = sign_get_attr(text_sign, FALSE);
3768 }
3769 }
3770 }
3771 }
3772#endif
3773
3774 if (draw_state == WL_NR - 1 && n_extra == 0)
3775 {
3776 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003777 /* Display the absolute or relative line number. After the
3778 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3779 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 && (row == startrow
3781#ifdef FEAT_DIFF
3782 + filler_lines
3783#endif
3784 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3785 {
3786 /* Draw the line number (empty space after wrapping). */
3787 if (row == startrow
3788#ifdef FEAT_DIFF
3789 + filler_lines
3790#endif
3791 )
3792 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003793 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003794 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003795
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003796 if (wp->w_p_nu && !wp->w_p_rnu)
3797 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003798 num = (long)lnum;
3799 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003800 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003801 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003802 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003803 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003804 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003805 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003806 num = lnum;
3807 fmt = "%-*ld ";
3808 }
3809 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003810
Bram Moolenaar700e7342013-01-30 12:31:36 +01003811 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003812 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (wp->w_skipcol > 0)
3814 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3815 *p_extra = '-';
3816#ifdef FEAT_RIGHTLEFT
3817 if (wp->w_p_rl) /* reverse line numbers */
3818 rl_mirror(extra);
3819#endif
3820 p_extra = extra;
3821 c_extra = NUL;
3822 }
3823 else
3824 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003825 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003826 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003827#ifdef FEAT_SYN_HL
3828 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003829 * the current line differently.
3830 * TODO: Can we use CursorLine instead of CursorLineNr
3831 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003832 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003833 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003834 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 }
3838
Bram Moolenaar597a4222014-06-25 14:39:50 +02003839#ifdef FEAT_LINEBREAK
3840 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3841 && n_extra == 0 && *p_sbr != NUL)
3842 /* draw indent after showbreak value */
3843 draw_state = WL_BRI;
3844 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3845 /* After the showbreak, draw the breakindent */
3846 draw_state = WL_BRI - 1;
3847
3848 /* draw 'breakindent': indent wrapped text accordingly */
3849 if (draw_state == WL_BRI - 1 && n_extra == 0)
3850 {
3851 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003852 /* if need_showbreak is set, breakindent also applies */
3853 if (wp->w_p_bri && n_extra == 0
3854 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003855# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003856 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003857# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003858 )
3859 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003860 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003861# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003862 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003863 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003864 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003865# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003866 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3867 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003868 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003869# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003870 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003871# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003872 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003873 c_extra = ' ';
3874 n_extra = get_breakindent_win(wp,
3875 ml_get_buf(wp->w_buffer, lnum, FALSE));
3876 /* Correct end of highlighted area for 'breakindent',
3877 * required when 'linebreak' is also set. */
3878 if (tocol == vcol)
3879 tocol += n_extra;
3880 }
3881 }
3882#endif
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3885 if (draw_state == WL_SBR - 1 && n_extra == 0)
3886 {
3887 draw_state = WL_SBR;
3888# ifdef FEAT_DIFF
3889 if (filler_todo > 0)
3890 {
3891 /* Draw "deleted" diff line(s). */
3892 if (char2cells(fill_diff) > 1)
3893 c_extra = '-';
3894 else
3895 c_extra = fill_diff;
3896# ifdef FEAT_RIGHTLEFT
3897 if (wp->w_p_rl)
3898 n_extra = col + 1;
3899 else
3900# endif
3901 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003902 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904# endif
3905# ifdef FEAT_LINEBREAK
3906 if (*p_sbr != NUL && need_showbreak)
3907 {
3908 /* Draw 'showbreak' at the start of each broken line. */
3909 p_extra = p_sbr;
3910 c_extra = NUL;
3911 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003912 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003914 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 /* Correct end of highlighted area for 'showbreak',
3916 * required when 'linebreak' is also set. */
3917 if (tocol == vcol)
3918 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003919#ifdef FEAT_SYN_HL
3920 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003921 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003922 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003923 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
3926# endif
3927 }
3928#endif
3929
3930 if (draw_state == WL_LINE - 1 && n_extra == 0)
3931 {
3932 draw_state = WL_LINE;
3933 if (saved_n_extra)
3934 {
3935 /* Continue item from end of wrapped line. */
3936 n_extra = saved_n_extra;
3937 c_extra = saved_c_extra;
3938 p_extra = saved_p_extra;
3939 char_attr = saved_char_attr;
3940 }
3941 else
3942 char_attr = 0;
3943 }
3944 }
3945
3946 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003947 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003948 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#ifdef FEAT_DIFF
3950 && filler_todo <= 0
3951#endif
3952 )
3953 {
3954 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3955 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003956 /* Pretend we have finished updating the window. Except when
3957 * 'cursorcolumn' is set. */
3958#ifdef FEAT_SYN_HL
3959 if (wp->w_p_cuc)
3960 row = wp->w_cline_row + wp->w_cline_height;
3961 else
3962#endif
3963 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 break;
3965 }
3966
3967 if (draw_state == WL_LINE && area_highlighting)
3968 {
3969 /* handle Visual or match highlighting in this line */
3970 if (vcol == fromcol
3971#ifdef FEAT_MBYTE
3972 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3973 && (*mb_ptr2cells)(ptr) > 1)
3974#endif
3975 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003976 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 && vcol < tocol))
3978 area_attr = attr; /* start highlighting */
3979 else if (area_attr != 0
3980 && (vcol == tocol
3981 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984#ifdef FEAT_SEARCH_EXTRA
3985 if (!n_extra)
3986 {
3987 /*
3988 * Check for start/end of search pattern match.
3989 * After end, check for start/end of next match.
3990 * When another match, have to check for start again.
3991 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003992 * Do this for 'search_hl' and the match list (ordered by
3993 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003995 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003996 cur = wp->w_match_head;
3997 shl_flag = FALSE;
3998 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004000 if (shl_flag == FALSE
4001 && ((cur != NULL
4002 && cur->priority > SEARCH_HL_PRIORITY)
4003 || cur == NULL))
4004 {
4005 shl = &search_hl;
4006 shl_flag = TRUE;
4007 }
4008 else
4009 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004010 if (cur != NULL)
4011 cur->pos.cur = 0;
4012 pos_inprogress = TRUE;
4013 while (shl->rm.regprog != NULL
4014 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004016 if (shl->startcol != MAXCOL
4017 && v >= (long)shl->startcol
4018 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004020#ifdef FEAT_MBYTE
4021 int tmp_col = v + MB_PTR2LEN(ptr);
4022
4023 if (shl->endcol < tmp_col)
4024 shl->endcol = tmp_col;
4025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004027#ifdef FEAT_CONCEAL
4028 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4029 == cur->hlg_id)
4030 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004031 has_match_conc =
4032 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004033 match_conc = cur->conceal_char;
4034 }
4035 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004036 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004039 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
4041 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004042 next_search_hl(wp, shl, lnum, (colnr_T)v,
4043 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004044 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004045 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047 /* Need to get the line again, a multi-line regexp
4048 * may have made it invalid. */
4049 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4050 ptr = line + v;
4051
4052 if (shl->lnum == lnum)
4053 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004054 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004056 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004058 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004060 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
4062 /* highlight empty match, try again after
4063 * it */
4064#ifdef FEAT_MBYTE
4065 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004066 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004067 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 else
4069#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004070 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072
4073 /* Loop to check if the match starts at the
4074 * current position */
4075 continue;
4076 }
4077 }
4078 break;
4079 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004080 if (shl != &search_hl && cur != NULL)
4081 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004083
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004084 /* Use attributes from match with highest priority among
4085 * 'search_hl' and the match list. */
4086 search_attr = search_hl.attr_cur;
4087 cur = wp->w_match_head;
4088 shl_flag = FALSE;
4089 while (cur != NULL || shl_flag == FALSE)
4090 {
4091 if (shl_flag == FALSE
4092 && ((cur != NULL
4093 && cur->priority > SEARCH_HL_PRIORITY)
4094 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004095 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004096 shl = &search_hl;
4097 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004098 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004099 else
4100 shl = &cur->hl;
4101 if (shl->attr_cur != 0)
4102 search_attr = shl->attr_cur;
4103 if (shl != &search_hl && cur != NULL)
4104 cur = cur->next;
4105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107#endif
4108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004110 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004112 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4113 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004115 if (diff_hlf == HLF_TXD && ptr - line > change_end
4116 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004118 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004119 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004120 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004123
4124 /* Decide which of the highlight attributes to use. */
4125 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004126#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004127 if (area_attr != 0)
4128 char_attr = hl_combine_attr(line_attr, area_attr);
4129 else if (search_attr != 0)
4130 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004131 /* Use line_attr when not in the Visual or 'incsearch' area
4132 * (area_attr may be 0 when "noinvcur" is set). */
4133 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004134 || vcol < fromcol || vcol_prev < fromcol_prev
4135 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004136 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004137#else
4138 if (area_attr != 0)
4139 char_attr = area_attr;
4140 else if (search_attr != 0)
4141 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004142#endif
4143 else
4144 {
4145 attr_pri = FALSE;
4146#ifdef FEAT_SYN_HL
4147 if (has_syntax)
4148 char_attr = syntax_attr;
4149 else
4150#endif
4151 char_attr = 0;
4152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154
4155 /*
4156 * Get the next character to put on the screen.
4157 */
4158 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004159 * The "p_extra" points to the extra stuff that is inserted to
4160 * represent special characters (non-printable stuff) and other
4161 * things. When all characters are the same, c_extra is used.
4162 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4163 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4165 */
4166 if (n_extra > 0)
4167 {
4168 if (c_extra != NUL)
4169 {
4170 c = c_extra;
4171#ifdef FEAT_MBYTE
4172 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004173 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004176 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004177 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 else
4180 mb_utf8 = FALSE;
4181#endif
4182 }
4183 else
4184 {
4185 c = *p_extra;
4186#ifdef FEAT_MBYTE
4187 if (has_mbyte)
4188 {
4189 mb_c = c;
4190 if (enc_utf8)
4191 {
4192 /* If the UTF-8 character is more than one byte:
4193 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004194 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 mb_utf8 = FALSE;
4196 if (mb_l > n_extra)
4197 mb_l = 1;
4198 else if (mb_l > 1)
4199 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004200 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004202 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 }
4205 else
4206 {
4207 /* if this is a DBCS character, put it in "mb_c" */
4208 mb_l = MB_BYTE2LEN(c);
4209 if (mb_l >= n_extra)
4210 mb_l = 1;
4211 else if (mb_l > 1)
4212 mb_c = (c << 8) + p_extra[1];
4213 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004214 if (mb_l == 0) /* at the NUL at end-of-line */
4215 mb_l = 1;
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 /* If a double-width char doesn't fit display a '>' in the
4218 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004219 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220# ifdef FEAT_RIGHTLEFT
4221 wp->w_p_rl ? (col <= 0) :
4222# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004223 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 && (*mb_char2cells)(mb_c) == 2)
4225 {
4226 c = '>';
4227 mb_c = c;
4228 mb_l = 1;
4229 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004230 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 /* put the pointer back to output the double-width
4232 * character at the start of the next line. */
4233 ++n_extra;
4234 --p_extra;
4235 }
4236 else
4237 {
4238 n_extra -= mb_l - 1;
4239 p_extra += mb_l - 1;
4240 }
4241 }
4242#endif
4243 ++p_extra;
4244 }
4245 --n_extra;
4246 }
4247 else
4248 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004249#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004250 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004251#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004252
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004253 if (p_extra_free != NULL)
4254 {
4255 vim_free(p_extra_free);
4256 p_extra_free = NULL;
4257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 /*
4259 * Get a character from the line itself.
4260 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004261 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004262#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004263 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265#ifdef FEAT_MBYTE
4266 if (has_mbyte)
4267 {
4268 mb_c = c;
4269 if (enc_utf8)
4270 {
4271 /* If the UTF-8 character is more than one byte: Decode it
4272 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004273 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 mb_utf8 = FALSE;
4275 if (mb_l > 1)
4276 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004277 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 /* Overlong encoded ASCII or ASCII with composing char
4279 * is displayed normally, except a NUL. */
4280 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004281 {
4282 c = mb_c;
4283# ifdef FEAT_LINEBREAK
4284 c0 = mb_c;
4285# endif
4286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004288
4289 /* At start of the line we can have a composing char.
4290 * Draw it as a space with a composing char. */
4291 if (utf_iscomposing(mb_c))
4292 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004293 int i;
4294
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004295 for (i = Screen_mco - 1; i > 0; --i)
4296 u8cc[i] = u8cc[i - 1];
4297 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004298 mb_c = ' ';
4299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301
4302 if ((mb_l == 1 && c >= 0x80)
4303 || (mb_l >= 1 && mb_c == 0)
4304 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004305# ifdef UNICODE16
4306 || mb_c >= 0x10000
4307# endif
4308 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
4310 /*
4311 * Illegal UTF-8 byte: display as <xx>.
4312 * Non-BMP character : display as ? or fullwidth ?.
4313 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004314# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 {
4318 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004319# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 if (wp->w_p_rl) /* reverse */
4321 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004324# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 else if (utf_char2cells(mb_c) != 2)
4326 STRCPY(extra, "?");
4327 else
4328 /* 0xff1f in UTF-8: full-width '?' */
4329 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 p_extra = extra;
4333 c = *p_extra;
4334 mb_c = mb_ptr2char_adv(&p_extra);
4335 mb_utf8 = (c >= 0x80);
4336 n_extra = (int)STRLEN(p_extra);
4337 c_extra = NUL;
4338 if (area_attr == 0 && search_attr == 0)
4339 {
4340 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004341 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 saved_attr2 = char_attr; /* save current attr */
4343 }
4344 }
4345 else if (mb_l == 0) /* at the NUL at end-of-line */
4346 mb_l = 1;
4347#ifdef FEAT_ARABIC
4348 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4349 {
4350 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004351 int pc, pc1, nc;
4352 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
4354 /* The idea of what is the previous and next
4355 * character depends on 'rightleft'. */
4356 if (wp->w_p_rl)
4357 {
4358 pc = prev_c;
4359 pc1 = prev_c1;
4360 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004361 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 else
4364 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004365 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004367 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 prev_c = mb_c;
4370
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004371 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 else
4374 prev_c = mb_c;
4375#endif
4376 }
4377 else /* enc_dbcs */
4378 {
4379 mb_l = MB_BYTE2LEN(c);
4380 if (mb_l == 0) /* at the NUL at end-of-line */
4381 mb_l = 1;
4382 else if (mb_l > 1)
4383 {
4384 /* We assume a second byte below 32 is illegal.
4385 * Hopefully this is OK for all double-byte encodings!
4386 */
4387 if (ptr[1] >= 32)
4388 mb_c = (c << 8) + ptr[1];
4389 else
4390 {
4391 if (ptr[1] == NUL)
4392 {
4393 /* head byte at end of line */
4394 mb_l = 1;
4395 transchar_nonprint(extra, c);
4396 }
4397 else
4398 {
4399 /* illegal tail byte */
4400 mb_l = 2;
4401 STRCPY(extra, "XX");
4402 }
4403 p_extra = extra;
4404 n_extra = (int)STRLEN(extra) - 1;
4405 c_extra = NUL;
4406 c = *p_extra++;
4407 if (area_attr == 0 && search_attr == 0)
4408 {
4409 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004410 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 saved_attr2 = char_attr; /* save current attr */
4412 }
4413 mb_c = c;
4414 }
4415 }
4416 }
4417 /* If a double-width char doesn't fit display a '>' in the
4418 * last column; the character is displayed at the start of the
4419 * next line. */
4420 if ((
4421# ifdef FEAT_RIGHTLEFT
4422 wp->w_p_rl ? (col <= 0) :
4423# endif
4424 (col >= W_WIDTH(wp) - 1))
4425 && (*mb_char2cells)(mb_c) == 2)
4426 {
4427 c = '>';
4428 mb_c = c;
4429 mb_utf8 = FALSE;
4430 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004431 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 /* Put pointer back so that the character will be
4433 * displayed at the start of the next line. */
4434 --ptr;
4435 }
4436 else if (*ptr != NUL)
4437 ptr += mb_l - 1;
4438
4439 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004440 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004441 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004442 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004445 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 c = ' ';
4447 if (area_attr == 0 && search_attr == 0)
4448 {
4449 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004450 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 saved_attr2 = char_attr; /* save current attr */
4452 }
4453 mb_c = c;
4454 mb_utf8 = FALSE;
4455 mb_l = 1;
4456 }
4457
4458 }
4459#endif
4460 ++ptr;
4461
4462 if (extra_check)
4463 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004464#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004465 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004466#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004467
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004468#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 /* Get syntax attribute, unless still at the start of the line
4470 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004471 v = (long)(ptr - line);
4472 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
4474 /* Get the syntax attribute for the character. If there
4475 * is an error, disable syntax highlighting. */
4476 save_did_emsg = did_emsg;
4477 did_emsg = FALSE;
4478
Bram Moolenaar217ad922005-03-20 22:37:15 +00004479 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004480# ifdef FEAT_SPELL
4481 has_spell ? &can_spell :
4482# endif
4483 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004486 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004487 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004488 has_syntax = FALSE;
4489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 else
4491 did_emsg = save_did_emsg;
4492
4493 /* Need to get the line again, a multi-line regexp may
4494 * have made it invalid. */
4495 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4496 ptr = line + v;
4497
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004498 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004500 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004501 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004502# ifdef FEAT_CONCEAL
4503 /* no concealing past the end of the line, it interferes
4504 * with line highlighting */
4505 if (c == NUL)
4506 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004507 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004508 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004511#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004512
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004513#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004514 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004515 * Only do this when there is no syntax highlighting, the
4516 * @Spell cluster is not used or the current syntax item
4517 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004518 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004519 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004520 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004521# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004522 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004523 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004524# endif
4525 if (c != 0 && (
4526# ifdef FEAT_SYN_HL
4527 !has_syntax ||
4528# endif
4529 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004530 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004531 char_u *prev_ptr, *p;
4532 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004533 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004534# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004535 if (has_mbyte)
4536 {
4537 prev_ptr = ptr - mb_l;
4538 v -= mb_l - 1;
4539 }
4540 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004541# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004542 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004543
4544 /* Use nextline[] if possible, it has the start of the
4545 * next line concatenated. */
4546 if ((prev_ptr - line) - nextlinecol >= 0)
4547 p = nextline + (prev_ptr - line) - nextlinecol;
4548 else
4549 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004550 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004551 len = spell_check(wp, p, &spell_hlf, &cap_col,
4552 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004553 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004554
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004555 /* In Insert mode only highlight a word that
4556 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004557 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004558 && (State & INSERT) != 0
4559 && wp->w_cursor.lnum == lnum
4560 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004561 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004562 && wp->w_cursor.col < (colnr_T)word_end)
4563 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004564 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004565 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004566 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004567
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004568 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004569 && (p - nextline) + len > nextline_idx)
4570 {
4571 /* Remember that the good word continues at the
4572 * start of the next line. */
4573 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004574 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004575 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004576
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004577 /* Turn index into actual attributes. */
4578 if (spell_hlf != HLF_COUNT)
4579 spell_attr = highlight_attr[spell_hlf];
4580
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004581 if (cap_col > 0)
4582 {
4583 if (p != prev_ptr
4584 && (p - nextline) + cap_col >= nextline_idx)
4585 {
4586 /* Remember that the word in the next line
4587 * must start with a capital. */
4588 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004589 cap_col = (int)((p - nextline) + cap_col
4590 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004591 }
4592 else
4593 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004594 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004595 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004596 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004597 }
4598 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004599 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004600 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004601 char_attr = hl_combine_attr(char_attr, spell_attr);
4602 else
4603 char_attr = hl_combine_attr(spell_attr, char_attr);
4604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605#endif
4606#ifdef FEAT_LINEBREAK
4607 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004608 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004610 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004611 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004613# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004614 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004615# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004616 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004618 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004620 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004621
Bram Moolenaar597a4222014-06-25 14:39:50 +02004622 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004623 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004624 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004625 if (c == TAB && n_extra + col > W_WIDTH(wp))
4626 n_extra = (int)wp->w_buffer->b_p_ts
4627 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4628
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004629# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004630 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004631# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004633# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004634 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004635 {
4636#ifdef FEAT_CONCEAL
4637 if (c == TAB)
4638 /* See "Tab alignment" below. */
4639 FIX_FOR_BOGUSCOLS;
4640#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004641 if (!wp->w_p_list)
4642 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645#endif
4646
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004647 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4648 */
4649 if (wp->w_p_list
4650 && (((c == 160
4651#ifdef FEAT_MBYTE
4652 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4653#endif
4654 ) && lcs_nbsp)
4655 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4656 {
4657 c = (c == ' ') ? lcs_space : lcs_nbsp;
4658 if (area_attr == 0 && search_attr == 0)
4659 {
4660 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004661 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004662 saved_attr2 = char_attr; /* save current attr */
4663 }
4664#ifdef FEAT_MBYTE
4665 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004666 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004667 {
4668 mb_utf8 = TRUE;
4669 u8cc[0] = 0;
4670 c = 0xc0;
4671 }
4672 else
4673 mb_utf8 = FALSE;
4674#endif
4675 }
4676
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4678 {
4679 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004680 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
4682 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004683 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 saved_attr2 = char_attr; /* save current attr */
4685 }
4686#ifdef FEAT_MBYTE
4687 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004688 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
4690 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004691 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004692 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694 else
4695 mb_utf8 = FALSE;
4696#endif
4697 }
4698 }
4699
4700 /*
4701 * Handling of non-printable characters.
4702 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004703 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
4705 /*
4706 * when getting a character from the file, we may have to
4707 * turn it into something else on the way to putting it
4708 * into "ScreenLines".
4709 */
4710 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4711 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004712 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004713 long vcol_adjusted = vcol; /* removed showbreak length */
4714#ifdef FEAT_LINEBREAK
4715 /* only adjust the tab_len, when at the first column
4716 * after the showbreak value was drawn */
4717 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4718 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004721 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004722 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4723
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004724#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004725 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004726#endif
4727 /* tab amount depends on current column */
4728 n_extra = tab_len;
4729#ifdef FEAT_LINEBREAK
4730 else
4731 {
4732 char_u *p;
4733 int len = n_extra;
4734 int i;
4735 int saved_nextra = n_extra;
4736
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004737#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004738 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004739 /* there are characters to conceal */
4740 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004741 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4742 */
4743 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4744 && n_extra > tab_len)
4745 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004746#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004747
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004748 /* if n_extra > 0, it gives the number of chars, to
4749 * use for a tab, else we need to calculate the width
4750 * for a tab */
4751#ifdef FEAT_MBYTE
4752 len = (tab_len * mb_char2len(lcs_tab2));
4753 if (n_extra > 0)
4754 len += n_extra - tab_len;
4755#endif
4756 c = lcs_tab1;
4757 p = alloc((unsigned)(len + 1));
4758 vim_memset(p, ' ', len);
4759 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004760 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004761 p_extra_free = p;
4762 for (i = 0; i < tab_len; i++)
4763 {
4764#ifdef FEAT_MBYTE
4765 mb_char2bytes(lcs_tab2, p);
4766 p += mb_char2len(lcs_tab2);
4767 n_extra += mb_char2len(lcs_tab2)
4768 - (saved_nextra > 0 ? 1 : 0);
4769#else
4770 p[i] = lcs_tab2;
4771#endif
4772 }
4773 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004774#ifdef FEAT_CONCEAL
4775 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4776 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004777 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004778 n_extra -= vcol_off;
4779#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004780 }
4781#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004782#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004783 {
4784 int vc_saved = vcol_off;
4785
4786 /* Tab alignment should be identical regardless of
4787 * 'conceallevel' value. So tab compensates of all
4788 * previous concealed characters, and thus resets
4789 * vcol_off and boguscols accumulated so far in the
4790 * line. Note that the tab can be longer than
4791 * 'tabstop' when there are concealed characters. */
4792 FIX_FOR_BOGUSCOLS;
4793
4794 /* Make sure, the highlighting for the tab char will be
4795 * correctly set further below (effectively reverts the
4796 * FIX_FOR_BOGSUCOLS macro */
4797 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004798 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004799 tab_len += vc_saved;
4800 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802#ifdef FEAT_MBYTE
4803 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4804#endif
4805 if (wp->w_p_list)
4806 {
4807 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004808#ifdef FEAT_LINEBREAK
4809 if (wp->w_p_lbr)
4810 c_extra = NUL; /* using p_extra from above */
4811 else
4812#endif
4813 c_extra = lcs_tab2;
4814 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004815 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 saved_attr2 = char_attr; /* save current attr */
4817#ifdef FEAT_MBYTE
4818 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004819 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004822 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004823 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825#endif
4826 }
4827 else
4828 {
4829 c_extra = ' ';
4830 c = ' ';
4831 }
4832 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004833 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004834 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004835 || ((fromcol >= 0 || fromcol_prev >= 0)
4836 && tocol > vcol
4837 && VIsual_mode != Ctrl_V
4838 && (
4839# ifdef FEAT_RIGHTLEFT
4840 wp->w_p_rl ? (col >= 0) :
4841# endif
4842 (col < W_WIDTH(wp)))
4843 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004844 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004845 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004846 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004848 /* Display a '$' after the line or highlight an extra
4849 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4851 /* For a diff line the highlighting continues after the
4852 * "$". */
4853 if (
4854# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004855 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856# ifdef LINE_ATTR
4857 &&
4858# endif
4859# endif
4860# ifdef LINE_ATTR
4861 line_attr == 0
4862# endif
4863 )
4864#endif
4865 {
4866#ifdef FEAT_VIRTUALEDIT
4867 /* In virtualedit, visual selections may extend
4868 * beyond end of line. */
4869 if (area_highlighting && virtual_active()
4870 && tocol != MAXCOL && vcol < tocol)
4871 n_extra = 0;
4872 else
4873#endif
4874 {
4875 p_extra = at_end_str;
4876 n_extra = 1;
4877 c_extra = NUL;
4878 }
4879 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004880 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004881 c = lcs_eol;
4882 else
4883 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 lcs_eol_one = -1;
4885 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004886 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004888 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 n_attr = 1;
4890 }
4891#ifdef FEAT_MBYTE
4892 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004893 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
4895 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004896 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004897 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 else
4900 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4901#endif
4902 }
4903 else if (c != NUL)
4904 {
4905 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004906 if (n_extra == 0)
4907 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908#ifdef FEAT_RIGHTLEFT
4909 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4910 rl_mirror(p_extra); /* reverse "<12>" */
4911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004913#ifdef FEAT_LINEBREAK
4914 if (wp->w_p_lbr)
4915 {
4916 char_u *p;
4917
4918 c = *p_extra;
4919 p = alloc((unsigned)n_extra + 1);
4920 vim_memset(p, ' ', n_extra);
4921 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4922 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004923 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004924 p_extra_free = p_extra = p;
4925 }
4926 else
4927#endif
4928 {
4929 n_extra = byte2cells(c) - 1;
4930 c = *p_extra++;
4931 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004932 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
4934 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004935 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 saved_attr2 = char_attr; /* save current attr */
4937 }
4938#ifdef FEAT_MBYTE
4939 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4940#endif
4941 }
4942#ifdef FEAT_VIRTUALEDIT
4943 else if (VIsual_active
4944 && (VIsual_mode == Ctrl_V
4945 || VIsual_mode == 'v')
4946 && virtual_active()
4947 && tocol != MAXCOL
4948 && vcol < tocol
4949 && (
4950# ifdef FEAT_RIGHTLEFT
4951 wp->w_p_rl ? (col >= 0) :
4952# endif
4953 (col < W_WIDTH(wp))))
4954 {
4955 c = ' ';
4956 --ptr; /* put it back at the NUL */
4957 }
4958#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004959#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 else if ((
4961# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004962 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 ) && (
4966# ifdef FEAT_RIGHTLEFT
4967 wp->w_p_rl ? (col >= 0) :
4968# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004969 (col
4970# ifdef FEAT_CONCEAL
4971 - boguscols
4972# endif
4973 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
4975 /* Highlight until the right side of the window */
4976 c = ' ';
4977 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004978
4979 /* Remember we do the char for line highlighting. */
4980 ++did_line_attr;
4981
4982 /* don't do search HL for the rest of the line */
4983 if (line_attr != 0 && char_attr == search_attr && col > 0)
4984 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985# ifdef FEAT_DIFF
4986 if (diff_hlf == HLF_TXD)
4987 {
4988 diff_hlf = HLF_CHD;
4989 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004990 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004991 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004992 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4993 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004994 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02004995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997# endif
4998 }
4999#endif
5000 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005001
5002#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005003 if ( wp->w_p_cole > 0
5004 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005005 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005006 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005007 && !(lnum_in_visual_area
5008 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005009 {
5010 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005011 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005012 && (syn_get_sub_char() != NUL || match_conc
5013 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005014 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005015 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005016 /* First time at this concealed item: display one
5017 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005018 if (match_conc)
5019 c = match_conc;
5020 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005021 c = syn_get_sub_char();
5022 else if (lcs_conceal != NUL)
5023 c = lcs_conceal;
5024 else
5025 c = ' ';
5026
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005027 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005028
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005029 if (n_extra > 0)
5030 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005031 vcol += n_extra;
5032 if (wp->w_p_wrap && n_extra > 0)
5033 {
5034# ifdef FEAT_RIGHTLEFT
5035 if (wp->w_p_rl)
5036 {
5037 col -= n_extra;
5038 boguscols -= n_extra;
5039 }
5040 else
5041# endif
5042 {
5043 boguscols += n_extra;
5044 col += n_extra;
5045 }
5046 }
5047 n_extra = 0;
5048 n_attr = 0;
5049 }
5050 else if (n_skip == 0)
5051 {
5052 is_concealing = TRUE;
5053 n_skip = 1;
5054 }
5055# ifdef FEAT_MBYTE
5056 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005057 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005058 {
5059 mb_utf8 = TRUE;
5060 u8cc[0] = 0;
5061 c = 0xc0;
5062 }
5063 else
5064 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5065# endif
5066 }
5067 else
5068 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005069 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005070 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005071 }
5072#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005075#ifdef FEAT_CONCEAL
5076 /* In the cursor line and we may be concealing characters: correct
5077 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005078 if (!did_wcol && draw_state == WL_LINE
5079 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005080 && conceal_cursor_line(wp)
5081 && (int)wp->w_virtcol <= vcol + n_skip)
5082 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005083# ifdef FEAT_RIGHTLEFT
5084 if (wp->w_p_rl)
5085 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5086 else
5087# endif
5088 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005089 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005090 did_wcol = TRUE;
5091 }
5092#endif
5093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 /* Don't override visual selection highlighting. */
5095 if (n_attr > 0
5096 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005097 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 char_attr = extra_attr;
5099
Bram Moolenaar81695252004-12-29 20:58:21 +00005100#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 /* XIM don't send preedit_start and preedit_end, but they send
5102 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5103 * im_is_preediting() here. */
5104 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005105 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 && (State & INSERT)
5107 && !p_imdisable
5108 && im_is_preediting()
5109 && draw_state == WL_LINE)
5110 {
5111 colnr_T tcol;
5112
5113 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005114 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 else
5116 tcol = preedit_end_col;
5117 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5118 {
5119 if (feedback_old_attr < 0)
5120 {
5121 feedback_col = 0;
5122 feedback_old_attr = char_attr;
5123 }
5124 char_attr = im_get_feedback_attr(feedback_col);
5125 if (char_attr < 0)
5126 char_attr = feedback_old_attr;
5127 feedback_col++;
5128 }
5129 else if (feedback_old_attr >= 0)
5130 {
5131 char_attr = feedback_old_attr;
5132 feedback_old_attr = -1;
5133 feedback_col = 0;
5134 }
5135 }
5136#endif
5137 /*
5138 * Handle the case where we are in column 0 but not on the first
5139 * character of the line and the user wants us to show us a
5140 * special character (via 'listchars' option "precedes:<char>".
5141 */
5142 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005143 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5145#ifdef FEAT_DIFF
5146 && filler_todo <= 0
5147#endif
5148 && draw_state > WL_NR
5149 && c != NUL)
5150 {
5151 c = lcs_prec;
5152 lcs_prec_todo = NUL;
5153#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005154 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5155 {
5156 /* Double-width character being overwritten by the "precedes"
5157 * character, need to fill up half the character. */
5158 c_extra = MB_FILLER_CHAR;
5159 n_extra = 1;
5160 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005161 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005164 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005167 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005168 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 else
5171 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5172#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005173 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
5175 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005176 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 n_attr3 = 1;
5178 }
5179 }
5180
5181 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005182 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005184 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005185#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005186 || did_line_attr == 1
5187#endif
5188 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005190#ifdef FEAT_SEARCH_EXTRA
5191 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005192
5193 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005194 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005195 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005196#endif
5197
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005198 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 * highlight match at end of line. If it's beyond the last
5200 * char on the screen, just overwrite that one (tricky!) Not
5201 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005202#ifdef FEAT_SEARCH_EXTRA
5203 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005204 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005205 prevcol_hl_flag = TRUE;
5206 else
5207 {
5208 cur = wp->w_match_head;
5209 while (cur != NULL)
5210 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005211 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005212 {
5213 prevcol_hl_flag = TRUE;
5214 break;
5215 }
5216 cur = cur->next;
5217 }
5218 }
5219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005221 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005222 && (VIsual_mode != Ctrl_V
5223 || lnum == VIsual.lnum
5224 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005225 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226#ifdef FEAT_SEARCH_EXTRA
5227 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005228 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005229# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005230 && did_line_attr <= 1
5231# endif
5232 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#endif
5234 ))
5235 {
5236 int n = 0;
5237
5238#ifdef FEAT_RIGHTLEFT
5239 if (wp->w_p_rl)
5240 {
5241 if (col < 0)
5242 n = 1;
5243 }
5244 else
5245#endif
5246 {
5247 if (col >= W_WIDTH(wp))
5248 n = -1;
5249 }
5250 if (n != 0)
5251 {
5252 /* At the window boundary, highlight the last character
5253 * instead (better than nothing). */
5254 off += n;
5255 col += n;
5256 }
5257 else
5258 {
5259 /* Add a blank character to highlight. */
5260 ScreenLines[off] = ' ';
5261#ifdef FEAT_MBYTE
5262 if (enc_utf8)
5263 ScreenLinesUC[off] = 0;
5264#endif
5265 }
5266#ifdef FEAT_SEARCH_EXTRA
5267 if (area_attr == 0)
5268 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005269 /* Use attributes from match with highest priority among
5270 * 'search_hl' and the match list. */
5271 char_attr = search_hl.attr;
5272 cur = wp->w_match_head;
5273 shl_flag = FALSE;
5274 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005275 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005276 if (shl_flag == FALSE
5277 && ((cur != NULL
5278 && cur->priority > SEARCH_HL_PRIORITY)
5279 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005280 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005281 shl = &search_hl;
5282 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005283 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005284 else
5285 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005286 if ((ptr - line) - 1 == (long)shl->startcol
5287 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005288 char_attr = shl->attr;
5289 if (shl != &search_hl && cur != NULL)
5290 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 }
5293#endif
5294 ScreenAttrs[off] = char_attr;
5295#ifdef FEAT_RIGHTLEFT
5296 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005299 --off;
5300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 else
5302#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005305 ++off;
5306 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005307 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005308#ifdef FEAT_SYN_HL
5309 eol_hl_off = 1;
5310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
Bram Moolenaar91170f82006-05-05 21:15:17 +00005314 /*
5315 * At end of the text line.
5316 */
5317 if (c == NUL)
5318 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005319#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005320 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5321 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005322 {
5323 /* highlight last char after line */
5324 --col;
5325 --off;
5326 --vcol;
5327 }
5328
Bram Moolenaar1a384422010-07-14 19:53:30 +02005329 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005330 if (wp->w_p_wrap)
5331 v = wp->w_skipcol;
5332 else
5333 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005334
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005335 /* check if line ends before left margin */
5336 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005337 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005338#ifdef FEAT_CONCEAL
5339 /* Get rid of the boguscols now, we want to draw until the right
5340 * edge for 'cursorcolumn'. */
5341 col -= boguscols;
5342 boguscols = 0;
5343#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005344
5345 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005346 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005347
5348 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005349 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5350 && (int)wp->w_virtcol <
5351 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005352 && lnum != wp->w_cursor.lnum)
5353 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005354# ifdef FEAT_RIGHTLEFT
5355 && !wp->w_p_rl
5356# endif
5357 )
5358 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005359 int rightmost_vcol = 0;
5360 int i;
5361
5362 if (wp->w_p_cuc)
5363 rightmost_vcol = wp->w_virtcol;
5364 if (draw_color_col)
5365 /* determine rightmost colorcolumn to possibly draw */
5366 for (i = 0; color_cols[i] >= 0; ++i)
5367 if (rightmost_vcol < color_cols[i])
5368 rightmost_vcol = color_cols[i];
5369
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005370 while (col < W_WIDTH(wp))
5371 {
5372 ScreenLines[off] = ' ';
5373#ifdef FEAT_MBYTE
5374 if (enc_utf8)
5375 ScreenLinesUC[off] = 0;
5376#endif
5377 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005378 if (draw_color_col)
5379 draw_color_col = advance_color_col(VCOL_HLC,
5380 &color_cols);
5381
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005382 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005383 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005384 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005385 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005386 else
5387 ScreenAttrs[off++] = 0;
5388
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005389 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005390 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005391
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005392 ++vcol;
5393 }
5394 }
5395#endif
5396
Bram Moolenaar860cae12010-06-05 23:22:07 +02005397 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5398 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 row++;
5400
5401 /*
5402 * Update w_cline_height and w_cline_folded if the cursor line was
5403 * updated (saves a call to plines() later).
5404 */
5405 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5406 {
5407 curwin->w_cline_row = startrow;
5408 curwin->w_cline_height = row - startrow;
5409#ifdef FEAT_FOLDING
5410 curwin->w_cline_folded = FALSE;
5411#endif
5412 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5413 }
5414
5415 break;
5416 }
5417
5418 /* line continues beyond line end */
5419 if (lcs_ext
5420 && !wp->w_p_wrap
5421#ifdef FEAT_DIFF
5422 && filler_todo <= 0
5423#endif
5424 && (
5425#ifdef FEAT_RIGHTLEFT
5426 wp->w_p_rl ? col == 0 :
5427#endif
5428 col == W_WIDTH(wp) - 1)
5429 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005430 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5432 {
5433 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005434 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435#ifdef FEAT_MBYTE
5436 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005437 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {
5439 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005440 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005441 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 }
5443 else
5444 mb_utf8 = FALSE;
5445#endif
5446 }
5447
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005448#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005449 /* advance to the next 'colorcolumn' */
5450 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005451 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005452
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005453 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005454 * highlight the cursor position itself.
5455 * Also highlight the 'colorcolumn' if it is different than
5456 * 'cursorcolumn' */
5457 vcol_save_attr = -1;
5458 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005459 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005460 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005461 && lnum != wp->w_cursor.lnum)
5462 {
5463 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005464 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005465 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005466 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005467 {
5468 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005469 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005470 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005471 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005472#endif
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 /*
5475 * Store character to be displayed.
5476 * Skip characters that are left of the screen for 'nowrap'.
5477 */
5478 vcol_prev = vcol;
5479 if (draw_state < WL_LINE || n_skip <= 0)
5480 {
5481 /*
5482 * Store the character.
5483 */
5484#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5485 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5486 {
5487 /* A double-wide character is: put first halve in left cell. */
5488 --off;
5489 --col;
5490 }
5491#endif
5492 ScreenLines[off] = c;
5493#ifdef FEAT_MBYTE
5494 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005495 {
5496 if ((mb_c & 0xff00) == 0x8e00)
5497 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 else if (enc_utf8)
5501 {
5502 if (mb_utf8)
5503 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005504 int i;
5505
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005507 if ((c & 0xff) == 0)
5508 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005509 for (i = 0; i < Screen_mco; ++i)
5510 {
5511 ScreenLinesC[i][off] = u8cc[i];
5512 if (u8cc[i] == 0)
5513 break;
5514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
5516 else
5517 ScreenLinesUC[off] = 0;
5518 }
5519 if (multi_attr)
5520 {
5521 ScreenAttrs[off] = multi_attr;
5522 multi_attr = 0;
5523 }
5524 else
5525#endif
5526 ScreenAttrs[off] = char_attr;
5527
5528#ifdef FEAT_MBYTE
5529 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5530 {
5531 /* Need to fill two screen columns. */
5532 ++off;
5533 ++col;
5534 if (enc_utf8)
5535 /* UTF-8: Put a 0 in the second screen char. */
5536 ScreenLines[off] = 0;
5537 else
5538 /* DBCS: Put second byte in the second screen char. */
5539 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005540 if (draw_state > WL_NR
5541#ifdef FEAT_DIFF
5542 && filler_todo <= 0
5543#endif
5544 )
5545 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 /* When "tocol" is halfway a character, set it to the end of
5547 * the character, otherwise highlighting won't stop. */
5548 if (tocol == vcol)
5549 ++tocol;
5550#ifdef FEAT_RIGHTLEFT
5551 if (wp->w_p_rl)
5552 {
5553 /* now it's time to backup one cell */
5554 --off;
5555 --col;
5556 }
5557#endif
5558 }
5559#endif
5560#ifdef FEAT_RIGHTLEFT
5561 if (wp->w_p_rl)
5562 {
5563 --off;
5564 --col;
5565 }
5566 else
5567#endif
5568 {
5569 ++off;
5570 ++col;
5571 }
5572 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005573#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005574 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005575 {
5576 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005577 ++vcol_off;
5578 if (n_extra > 0)
5579 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005580 if (wp->w_p_wrap)
5581 {
5582 /*
5583 * Special voodoo required if 'wrap' is on.
5584 *
5585 * Advance the column indicator to force the line
5586 * drawing to wrap early. This will make the line
5587 * take up the same screen space when parts are concealed,
5588 * so that cursor line computations aren't messed up.
5589 *
5590 * To avoid the fictitious advance of 'col' causing
5591 * trailing junk to be written out of the screen line
5592 * we are building, 'boguscols' keeps track of the number
5593 * of bad columns we have advanced.
5594 */
5595 if (n_extra > 0)
5596 {
5597 vcol += n_extra;
5598# ifdef FEAT_RIGHTLEFT
5599 if (wp->w_p_rl)
5600 {
5601 col -= n_extra;
5602 boguscols -= n_extra;
5603 }
5604 else
5605# endif
5606 {
5607 col += n_extra;
5608 boguscols += n_extra;
5609 }
5610 n_extra = 0;
5611 n_attr = 0;
5612 }
5613
5614
5615# ifdef FEAT_MBYTE
5616 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5617 {
5618 /* Need to fill two screen columns. */
5619# ifdef FEAT_RIGHTLEFT
5620 if (wp->w_p_rl)
5621 {
5622 --boguscols;
5623 --col;
5624 }
5625 else
5626# endif
5627 {
5628 ++boguscols;
5629 ++col;
5630 }
5631 }
5632# endif
5633
5634# ifdef FEAT_RIGHTLEFT
5635 if (wp->w_p_rl)
5636 {
5637 --boguscols;
5638 --col;
5639 }
5640 else
5641# endif
5642 {
5643 ++boguscols;
5644 ++col;
5645 }
5646 }
5647 else
5648 {
5649 if (n_extra > 0)
5650 {
5651 vcol += n_extra;
5652 n_extra = 0;
5653 n_attr = 0;
5654 }
5655 }
5656
5657 }
5658#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 else
5660 --n_skip;
5661
Bram Moolenaar64486672010-05-16 15:46:46 +02005662 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5663 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005664 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665#ifdef FEAT_DIFF
5666 && filler_todo <= 0
5667#endif
5668 )
5669 ++vcol;
5670
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005671#ifdef FEAT_SYN_HL
5672 if (vcol_save_attr >= 0)
5673 char_attr = vcol_save_attr;
5674#endif
5675
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 /* restore attributes after "predeces" in 'listchars' */
5677 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5678 char_attr = saved_attr3;
5679
5680 /* restore attributes after last 'listchars' or 'number' char */
5681 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5682 char_attr = saved_attr2;
5683
5684 /*
5685 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005686 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 */
5688 if ((
5689#ifdef FEAT_RIGHTLEFT
5690 wp->w_p_rl ? (col < 0) :
5691#endif
5692 (col >= W_WIDTH(wp)))
5693 && (*ptr != NUL
5694#ifdef FEAT_DIFF
5695 || filler_todo > 0
5696#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005697 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5699 )
5700 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005701#ifdef FEAT_CONCEAL
5702 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5703 (int)W_WIDTH(wp), wp->w_p_rl);
5704 boguscols = 0;
5705#else
5706 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5707 (int)W_WIDTH(wp), wp->w_p_rl);
5708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 ++row;
5710 ++screen_row;
5711
5712 /* When not wrapping and finished diff lines, or when displayed
5713 * '$' and highlighting until last column, break here. */
5714 if ((!wp->w_p_wrap
5715#ifdef FEAT_DIFF
5716 && filler_todo <= 0
5717#endif
5718 ) || lcs_eol_one == -1)
5719 break;
5720
5721 /* When the window is too narrow draw all "@" lines. */
5722 if (draw_state != WL_LINE
5723#ifdef FEAT_DIFF
5724 && filler_todo <= 0
5725#endif
5726 )
5727 {
5728 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005729#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 draw_vsep_win(wp, row);
5731#endif
5732 row = endrow;
5733 }
5734
5735 /* When line got too long for screen break here. */
5736 if (row == endrow)
5737 {
5738 ++row;
5739 break;
5740 }
5741
5742 if (screen_cur_row == screen_row - 1
5743#ifdef FEAT_DIFF
5744 && filler_todo <= 0
5745#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005746#ifdef FEAT_WINDOWS
5747 && W_WIDTH(wp) == Columns
5748#endif
5749 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 /* Remember that the line wraps, used for modeless copy. */
5752 LineWraps[screen_row - 1] = TRUE;
5753
5754 /*
5755 * Special trick to make copy/paste of wrapped lines work with
5756 * xterm/screen: write an extra character beyond the end of
5757 * the line. This will work with all terminal types
5758 * (regardless of the xn,am settings).
5759 * Only do this on a fast tty.
5760 * Only do this if the cursor is on the current line
5761 * (something has been written in it).
5762 * Don't do this for the GUI.
5763 * Don't do this for double-width characters.
5764 * Don't do this for a window not at the right screen border.
5765 */
5766 if (p_tf
5767#ifdef FEAT_GUI
5768 && !gui.in_use
5769#endif
5770#ifdef FEAT_MBYTE
5771 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005772 && ((*mb_off2cells)(LineOffset[screen_row],
5773 LineOffset[screen_row] + screen_Columns)
5774 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005776 + (int)Columns - 2,
5777 LineOffset[screen_row] + screen_Columns)
5778 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779#endif
5780 )
5781 {
5782 /* First make sure we are at the end of the screen line,
5783 * then output the same character again to let the
5784 * terminal know about the wrap. If the terminal doesn't
5785 * auto-wrap, we overwrite the character. */
5786 if (screen_cur_col != W_WIDTH(wp))
5787 screen_char(LineOffset[screen_row - 1]
5788 + (unsigned)Columns - 1,
5789 screen_row - 1, (int)(Columns - 1));
5790
5791#ifdef FEAT_MBYTE
5792 /* When there is a multi-byte character, just output a
5793 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005794 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5795 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 out_char(' ');
5797 else
5798#endif
5799 out_char(ScreenLines[LineOffset[screen_row - 1]
5800 + (Columns - 1)]);
5801 /* force a redraw of the first char on the next line */
5802 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5803 screen_start(); /* don't know where cursor is now */
5804 }
5805 }
5806
5807 col = 0;
5808 off = (unsigned)(current_ScreenLine - ScreenLines);
5809#ifdef FEAT_RIGHTLEFT
5810 if (wp->w_p_rl)
5811 {
5812 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5813 off += col;
5814 }
5815#endif
5816
5817 /* reset the drawing state for the start of a wrapped line */
5818 draw_state = WL_START;
5819 saved_n_extra = n_extra;
5820 saved_p_extra = p_extra;
5821 saved_c_extra = c_extra;
5822 saved_char_attr = char_attr;
5823 n_extra = 0;
5824 lcs_prec_todo = lcs_prec;
5825#ifdef FEAT_LINEBREAK
5826# ifdef FEAT_DIFF
5827 if (filler_todo <= 0)
5828# endif
5829 need_showbreak = TRUE;
5830#endif
5831#ifdef FEAT_DIFF
5832 --filler_todo;
5833 /* When the filler lines are actually below the last line of the
5834 * file, don't draw the line itself, break here. */
5835 if (filler_todo == 0 && wp->w_botfill)
5836 break;
5837#endif
5838 }
5839
5840 } /* for every character in the line */
5841
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005842#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005843 /* After an empty line check first word for capital. */
5844 if (*skipwhite(line) == NUL)
5845 {
5846 capcol_lnum = lnum + 1;
5847 cap_col = 0;
5848 }
5849#endif
5850
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005851 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 return row;
5853}
5854
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005855#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005856static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005857
5858/*
5859 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005860 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005861 */
5862 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005863comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005864{
5865 int i;
5866
5867 for (i = 0; i < Screen_mco; ++i)
5868 {
5869 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5870 return TRUE;
5871 if (ScreenLinesC[i][off_from] == 0)
5872 break;
5873 }
5874 return FALSE;
5875}
5876#endif
5877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878/*
5879 * Check whether the given character needs redrawing:
5880 * - the (first byte of the) character is different
5881 * - the attributes are different
5882 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005883 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 */
5885 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005886char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 if (cols > 0
5889 && ((ScreenLines[off_from] != ScreenLines[off_to]
5890 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5891
5892#ifdef FEAT_MBYTE
5893 || (enc_dbcs != 0
5894 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5895 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5896 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5897 : (cols > 1 && ScreenLines[off_from + 1]
5898 != ScreenLines[off_to + 1])))
5899 || (enc_utf8
5900 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5901 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005902 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005903 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5904 && ScreenLines[off_from + 1]
5905 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#endif
5907 ))
5908 return TRUE;
5909 return FALSE;
5910}
5911
5912/*
5913 * Move one "cooked" screen line to the screen, but only the characters that
5914 * have actually changed. Handle insert/delete character.
5915 * "coloff" gives the first column on the screen for this line.
5916 * "endcol" gives the columns where valid characters are.
5917 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5918 * needs to be cleared, negative otherwise.
5919 * "rlflag" is TRUE in a rightleft window:
5920 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5921 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5922 */
5923 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005924screen_line(
5925 int row,
5926 int coloff,
5927 int endcol,
5928 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005930 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005932 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933{
5934 unsigned off_from;
5935 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005936#ifdef FEAT_MBYTE
5937 unsigned max_off_from;
5938 unsigned max_off_to;
5939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005941#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 int hl;
5943#endif
5944 int force = FALSE; /* force update rest of the line */
5945 int redraw_this /* bool: does character need redraw? */
5946#ifdef FEAT_GUI
5947 = TRUE /* For GUI when while-loop empty */
5948#endif
5949 ;
5950 int redraw_next; /* redraw_this for next character */
5951#ifdef FEAT_MBYTE
5952 int clear_next = FALSE;
5953 int char_cells; /* 1: normal char */
5954 /* 2: occupies two display cells */
5955# define CHAR_CELLS char_cells
5956#else
5957# define CHAR_CELLS 1
5958#endif
5959
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005960 /* Check for illegal row and col, just in case. */
5961 if (row >= Rows)
5962 row = Rows - 1;
5963 if (endcol > Columns)
5964 endcol = Columns;
5965
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966# ifdef FEAT_CLIPBOARD
5967 clip_may_clear_selection(row, row);
5968# endif
5969
5970 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5971 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005972#ifdef FEAT_MBYTE
5973 max_off_from = off_from + screen_Columns;
5974 max_off_to = LineOffset[row] + screen_Columns;
5975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
5977#ifdef FEAT_RIGHTLEFT
5978 if (rlflag)
5979 {
5980 /* Clear rest first, because it's left of the text. */
5981 if (clear_width > 0)
5982 {
5983 while (col <= endcol && ScreenLines[off_to] == ' '
5984 && ScreenAttrs[off_to] == 0
5985# ifdef FEAT_MBYTE
5986 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5987# endif
5988 )
5989 {
5990 ++off_to;
5991 ++col;
5992 }
5993 if (col <= endcol)
5994 screen_fill(row, row + 1, col + coloff,
5995 endcol + coloff + 1, ' ', ' ', 0);
5996 }
5997 col = endcol + 1;
5998 off_to = LineOffset[row] + col + coloff;
5999 off_from += col;
6000 endcol = (clear_width > 0 ? clear_width : -clear_width);
6001 }
6002#endif /* FEAT_RIGHTLEFT */
6003
6004 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6005
6006 while (col < endcol)
6007 {
6008#ifdef FEAT_MBYTE
6009 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006010 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 else
6012 char_cells = 1;
6013#endif
6014
6015 redraw_this = redraw_next;
6016 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6017 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6018
6019#ifdef FEAT_GUI
6020 /* If the next character was bold, then redraw the current character to
6021 * remove any pixels that might have spilt over into us. This only
6022 * happens in the GUI.
6023 */
6024 if (redraw_next && gui.in_use)
6025 {
6026 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006027 if (hl > HL_ALL)
6028 hl = syn_attr2attr(hl);
6029 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 redraw_this = TRUE;
6031 }
6032#endif
6033
6034 if (redraw_this)
6035 {
6036 /*
6037 * Special handling when 'xs' termcap flag set (hpterm):
6038 * Attributes for characters are stored at the position where the
6039 * cursor is when writing the highlighting code. The
6040 * start-highlighting code must be written with the cursor on the
6041 * first highlighted character. The stop-highlighting code must
6042 * be written with the cursor just after the last highlighted
6043 * character.
6044 * Overwriting a character doesn't remove it's highlighting. Need
6045 * to clear the rest of the line, and force redrawing it
6046 * completely.
6047 */
6048 if ( p_wiv
6049 && !force
6050#ifdef FEAT_GUI
6051 && !gui.in_use
6052#endif
6053 && ScreenAttrs[off_to] != 0
6054 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6055 {
6056 /*
6057 * Need to remove highlighting attributes here.
6058 */
6059 windgoto(row, col + coloff);
6060 out_str(T_CE); /* clear rest of this screen line */
6061 screen_start(); /* don't know where cursor is now */
6062 force = TRUE; /* force redraw of rest of the line */
6063 redraw_next = TRUE; /* or else next char would miss out */
6064
6065 /*
6066 * If the previous character was highlighted, need to stop
6067 * highlighting at this character.
6068 */
6069 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6070 {
6071 screen_attr = ScreenAttrs[off_to - 1];
6072 term_windgoto(row, col + coloff);
6073 screen_stop_highlight();
6074 }
6075 else
6076 screen_attr = 0; /* highlighting has stopped */
6077 }
6078#ifdef FEAT_MBYTE
6079 if (enc_dbcs != 0)
6080 {
6081 /* Check if overwriting a double-byte with a single-byte or
6082 * the other way around requires another character to be
6083 * redrawn. For UTF-8 this isn't needed, because comparing
6084 * ScreenLinesUC[] is sufficient. */
6085 if (char_cells == 1
6086 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006087 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 {
6089 /* Writing a single-cell character over a double-cell
6090 * character: need to redraw the next cell. */
6091 ScreenLines[off_to + 1] = 0;
6092 redraw_next = TRUE;
6093 }
6094 else if (char_cells == 2
6095 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006096 && (*mb_off2cells)(off_to, max_off_to) == 1
6097 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
6099 /* Writing the second half of a double-cell character over
6100 * a double-cell character: need to redraw the second
6101 * cell. */
6102 ScreenLines[off_to + 2] = 0;
6103 redraw_next = TRUE;
6104 }
6105
6106 if (enc_dbcs == DBCS_JPNU)
6107 ScreenLines2[off_to] = ScreenLines2[off_from];
6108 }
6109 /* When writing a single-width character over a double-width
6110 * character and at the end of the redrawn text, need to clear out
6111 * the right halve of the old character.
6112 * Also required when writing the right halve of a double-width
6113 * char over the left halve of an existing one. */
6114 if (has_mbyte && col + char_cells == endcol
6115 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006116 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006118 && (*mb_off2cells)(off_to, max_off_to) == 1
6119 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 clear_next = TRUE;
6121#endif
6122
6123 ScreenLines[off_to] = ScreenLines[off_from];
6124#ifdef FEAT_MBYTE
6125 if (enc_utf8)
6126 {
6127 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6128 if (ScreenLinesUC[off_from] != 0)
6129 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006130 int i;
6131
6132 for (i = 0; i < Screen_mco; ++i)
6133 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135 }
6136 if (char_cells == 2)
6137 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6138#endif
6139
6140#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006141 /* The bold trick makes a single column of pixels appear in the
6142 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 * character should be redrawn too. This happens for our own GUI
6144 * and for some xterms. */
6145 if (
6146# ifdef FEAT_GUI
6147 gui.in_use
6148# endif
6149# if defined(FEAT_GUI) && defined(UNIX)
6150 ||
6151# endif
6152# ifdef UNIX
6153 term_is_xterm
6154# endif
6155 )
6156 {
6157 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006158 if (hl > HL_ALL)
6159 hl = syn_attr2attr(hl);
6160 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 redraw_next = TRUE;
6162 }
6163#endif
6164 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6165#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006166 /* For simplicity set the attributes of second half of a
6167 * double-wide character equal to the first half. */
6168 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006170
6171 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 else
6174#endif
6175 screen_char(off_to, row, col + coloff);
6176 }
6177 else if ( p_wiv
6178#ifdef FEAT_GUI
6179 && !gui.in_use
6180#endif
6181 && col + coloff > 0)
6182 {
6183 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6184 {
6185 /*
6186 * Don't output stop-highlight when moving the cursor, it will
6187 * stop the highlighting when it should continue.
6188 */
6189 screen_attr = 0;
6190 }
6191 else if (screen_attr != 0)
6192 screen_stop_highlight();
6193 }
6194
6195 off_to += CHAR_CELLS;
6196 off_from += CHAR_CELLS;
6197 col += CHAR_CELLS;
6198 }
6199
6200#ifdef FEAT_MBYTE
6201 if (clear_next)
6202 {
6203 /* Clear the second half of a double-wide character of which the left
6204 * half was overwritten with a single-wide character. */
6205 ScreenLines[off_to] = ' ';
6206 if (enc_utf8)
6207 ScreenLinesUC[off_to] = 0;
6208 screen_char(off_to, row, col + coloff);
6209 }
6210#endif
6211
6212 if (clear_width > 0
6213#ifdef FEAT_RIGHTLEFT
6214 && !rlflag
6215#endif
6216 )
6217 {
6218#ifdef FEAT_GUI
6219 int startCol = col;
6220#endif
6221
6222 /* blank out the rest of the line */
6223 while (col < clear_width && ScreenLines[off_to] == ' '
6224 && ScreenAttrs[off_to] == 0
6225#ifdef FEAT_MBYTE
6226 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6227#endif
6228 )
6229 {
6230 ++off_to;
6231 ++col;
6232 }
6233 if (col < clear_width)
6234 {
6235#ifdef FEAT_GUI
6236 /*
6237 * In the GUI, clearing the rest of the line may leave pixels
6238 * behind if the first character cleared was bold. Some bold
6239 * fonts spill over the left. In this case we redraw the previous
6240 * character too. If we didn't skip any blanks above, then we
6241 * only redraw if the character wasn't already redrawn anyway.
6242 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006243 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
6245 hl = ScreenAttrs[off_to];
6246 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006247 {
6248 int prev_cells = 1;
6249# ifdef FEAT_MBYTE
6250 if (enc_utf8)
6251 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6252 * that its width is 2. */
6253 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6254 else if (enc_dbcs != 0)
6255 {
6256 /* find previous character by counting from first
6257 * column and get its width. */
6258 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006259 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006260
6261 while (off < off_to)
6262 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006263 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006264 off += prev_cells;
6265 }
6266 }
6267
6268 if (enc_dbcs != 0 && prev_cells > 1)
6269 screen_char_2(off_to - prev_cells, row,
6270 col + coloff - prev_cells);
6271 else
6272# endif
6273 screen_char(off_to - prev_cells, row,
6274 col + coloff - prev_cells);
6275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
6277#endif
6278 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6279 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006280#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 off_to += clear_width - col;
6282 col = clear_width;
6283#endif
6284 }
6285 }
6286
6287 if (clear_width > 0)
6288 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006289#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 /* For a window that's left of another, draw the separator char. */
6291 if (col + coloff < Columns)
6292 {
6293 int c;
6294
6295 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006296 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006298 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6299 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300# endif
6301 || ScreenAttrs[off_to] != hl)
6302 {
6303 ScreenLines[off_to] = c;
6304 ScreenAttrs[off_to] = hl;
6305# ifdef FEAT_MBYTE
6306 if (enc_utf8)
6307 {
6308 if (c >= 0x80)
6309 {
6310 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006311 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 }
6313 else
6314 ScreenLinesUC[off_to] = 0;
6315 }
6316# endif
6317 screen_char(off_to, row, col + coloff);
6318 }
6319 }
6320 else
6321#endif
6322 LineWraps[row] = FALSE;
6323 }
6324}
6325
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006326#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006328 * Mirror text "str" for right-left displaying.
6329 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006331 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006332rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 char_u *p1, *p2;
6335 int t;
6336
6337 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6338 {
6339 t = *p1;
6340 *p1 = *p2;
6341 *p2 = t;
6342 }
6343}
6344#endif
6345
6346#if defined(FEAT_WINDOWS) || defined(PROTO)
6347/*
6348 * mark all status lines for redraw; used after first :cd
6349 */
6350 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006351status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353 win_T *wp;
6354
Bram Moolenaar29323592016-07-24 22:04:11 +02006355 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (wp->w_status_height)
6357 {
6358 wp->w_redr_status = TRUE;
6359 redraw_later(VALID);
6360 }
6361}
6362
6363/*
6364 * mark all status lines of the current buffer for redraw
6365 */
6366 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006367status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
6369 win_T *wp;
6370
Bram Moolenaar29323592016-07-24 22:04:11 +02006371 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6373 {
6374 wp->w_redr_status = TRUE;
6375 redraw_later(VALID);
6376 }
6377}
6378
6379/*
6380 * Redraw all status lines that need to be redrawn.
6381 */
6382 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006383redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 win_T *wp;
6386
Bram Moolenaar29323592016-07-24 22:04:11 +02006387 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 if (wp->w_redr_status)
6389 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006390 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006391 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392}
6393#endif
6394
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006395#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396/*
6397 * Redraw all status lines at the bottom of frame "frp".
6398 */
6399 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006400win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
6402 if (frp->fr_layout == FR_LEAF)
6403 frp->fr_win->w_redr_status = TRUE;
6404 else if (frp->fr_layout == FR_ROW)
6405 {
6406 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6407 win_redraw_last_status(frp);
6408 }
6409 else /* frp->fr_layout == FR_COL */
6410 {
6411 frp = frp->fr_child;
6412 while (frp->fr_next != NULL)
6413 frp = frp->fr_next;
6414 win_redraw_last_status(frp);
6415 }
6416}
6417#endif
6418
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006419#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420/*
6421 * Draw the verticap separator right of window "wp" starting with line "row".
6422 */
6423 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006424draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 int hl;
6427 int c;
6428
6429 if (wp->w_vsep_width)
6430 {
6431 /* draw the vertical separator right of this window */
6432 c = fillchar_vsep(&hl);
6433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6434 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6435 c, ' ', hl);
6436 }
6437}
6438#endif
6439
6440#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006441static int status_match_len(expand_T *xp, char_u *s);
6442static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443
6444/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006445 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 */
6447 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006448status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 int len = 0;
6451
6452#ifdef FEAT_MENU
6453 int emenu = (xp->xp_context == EXPAND_MENUS
6454 || xp->xp_context == EXPAND_MENUNAMES);
6455
6456 /* Check for menu separators - replace with '|'. */
6457 if (emenu && menu_is_separator(s))
6458 return 1;
6459#endif
6460
6461 while (*s != NUL)
6462 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006463 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006464 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006465 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 }
6467
6468 return len;
6469}
6470
6471/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006472 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006473 * These are backslashes used for escaping. Do show backslashes in help tags.
6474 */
6475 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006476skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006477{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006478 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006479#ifdef FEAT_MENU
6480 || ((xp->xp_context == EXPAND_MENUS
6481 || xp->xp_context == EXPAND_MENUNAMES)
6482 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6483#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006484 )
6485 {
6486#ifndef BACKSLASH_IN_FILENAME
6487 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6488 return 2;
6489#endif
6490 return 1;
6491 }
6492 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006493}
6494
6495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 * Show wildchar matches in the status line.
6497 * Show at least the "match" item.
6498 * We start at item 'first_match' in the list and show all matches that fit.
6499 *
6500 * If inversion is possible we use it. Else '=' characters are used.
6501 */
6502 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006503win_redr_status_matches(
6504 expand_T *xp,
6505 int num_matches,
6506 char_u **matches, /* list of matches */
6507 int match,
6508 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6511 int row;
6512 char_u *buf;
6513 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006514 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 int fillchar;
6516 int attr;
6517 int i;
6518 int highlight = TRUE;
6519 char_u *selstart = NULL;
6520 int selstart_col = 0;
6521 char_u *selend = NULL;
6522 static int first_match = 0;
6523 int add_left = FALSE;
6524 char_u *s;
6525#ifdef FEAT_MENU
6526 int emenu;
6527#endif
6528#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6529 int l;
6530#endif
6531
6532 if (matches == NULL) /* interrupted completion? */
6533 return;
6534
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006535#ifdef FEAT_MBYTE
6536 if (has_mbyte)
6537 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6538 else
6539#endif
6540 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (buf == NULL)
6542 return;
6543
6544 if (match == -1) /* don't show match but original text */
6545 {
6546 match = 0;
6547 highlight = FALSE;
6548 }
6549 /* count 1 for the ending ">" */
6550 clen = status_match_len(xp, L_MATCH(match)) + 3;
6551 if (match == 0)
6552 first_match = 0;
6553 else if (match < first_match)
6554 {
6555 /* jumping left, as far as we can go */
6556 first_match = match;
6557 add_left = TRUE;
6558 }
6559 else
6560 {
6561 /* check if match fits on the screen */
6562 for (i = first_match; i < match; ++i)
6563 clen += status_match_len(xp, L_MATCH(i)) + 2;
6564 if (first_match > 0)
6565 clen += 2;
6566 /* jumping right, put match at the left */
6567 if ((long)clen > Columns)
6568 {
6569 first_match = match;
6570 /* if showing the last match, we can add some on the left */
6571 clen = 2;
6572 for (i = match; i < num_matches; ++i)
6573 {
6574 clen += status_match_len(xp, L_MATCH(i)) + 2;
6575 if ((long)clen >= Columns)
6576 break;
6577 }
6578 if (i == num_matches)
6579 add_left = TRUE;
6580 }
6581 }
6582 if (add_left)
6583 while (first_match > 0)
6584 {
6585 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6586 if ((long)clen >= Columns)
6587 break;
6588 --first_match;
6589 }
6590
6591 fillchar = fillchar_status(&attr, TRUE);
6592
6593 if (first_match == 0)
6594 {
6595 *buf = NUL;
6596 len = 0;
6597 }
6598 else
6599 {
6600 STRCPY(buf, "< ");
6601 len = 2;
6602 }
6603 clen = len;
6604
6605 i = first_match;
6606 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6607 {
6608 if (i == match)
6609 {
6610 selstart = buf + len;
6611 selstart_col = clen;
6612 }
6613
6614 s = L_MATCH(i);
6615 /* Check for menu separators - replace with '|' */
6616#ifdef FEAT_MENU
6617 emenu = (xp->xp_context == EXPAND_MENUS
6618 || xp->xp_context == EXPAND_MENUNAMES);
6619 if (emenu && menu_is_separator(s))
6620 {
6621 STRCPY(buf + len, transchar('|'));
6622 l = (int)STRLEN(buf + len);
6623 len += l;
6624 clen += l;
6625 }
6626 else
6627#endif
6628 for ( ; *s != NUL; ++s)
6629 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006630 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 clen += ptr2cells(s);
6632#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006633 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 {
6635 STRNCPY(buf + len, s, l);
6636 s += l - 1;
6637 len += l;
6638 }
6639 else
6640#endif
6641 {
6642 STRCPY(buf + len, transchar_byte(*s));
6643 len += (int)STRLEN(buf + len);
6644 }
6645 }
6646 if (i == match)
6647 selend = buf + len;
6648
6649 *(buf + len++) = ' ';
6650 *(buf + len++) = ' ';
6651 clen += 2;
6652 if (++i == num_matches)
6653 break;
6654 }
6655
6656 if (i != num_matches)
6657 {
6658 *(buf + len++) = '>';
6659 ++clen;
6660 }
6661
6662 buf[len] = NUL;
6663
6664 row = cmdline_row - 1;
6665 if (row >= 0)
6666 {
6667 if (wild_menu_showing == 0)
6668 {
6669 if (msg_scrolled > 0)
6670 {
6671 /* Put the wildmenu just above the command line. If there is
6672 * no room, scroll the screen one line up. */
6673 if (cmdline_row == Rows - 1)
6674 {
6675 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6676 ++msg_scrolled;
6677 }
6678 else
6679 {
6680 ++cmdline_row;
6681 ++row;
6682 }
6683 wild_menu_showing = WM_SCROLLED;
6684 }
6685 else
6686 {
6687 /* Create status line if needed by setting 'laststatus' to 2.
6688 * Set 'winminheight' to zero to avoid that the window is
6689 * resized. */
6690 if (lastwin->w_status_height == 0)
6691 {
6692 save_p_ls = p_ls;
6693 save_p_wmh = p_wmh;
6694 p_ls = 2;
6695 p_wmh = 0;
6696 last_status(FALSE);
6697 }
6698 wild_menu_showing = WM_SHOWN;
6699 }
6700 }
6701
6702 screen_puts(buf, row, 0, attr);
6703 if (selstart != NULL && highlight)
6704 {
6705 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006706 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
6708
6709 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6710 }
6711
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006712#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 win_redraw_last_status(topframe);
6714#else
6715 lastwin->w_redr_status = TRUE;
6716#endif
6717 vim_free(buf);
6718}
6719#endif
6720
6721#if defined(FEAT_WINDOWS) || defined(PROTO)
6722/*
6723 * Redraw the status line of window wp.
6724 *
6725 * If inversion is possible we use it. Else '=' characters are used.
6726 */
6727 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006728win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 int row;
6731 char_u *p;
6732 int len;
6733 int fillchar;
6734 int attr;
6735 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006736 static int busy = FALSE;
6737
6738 /* It's possible to get here recursively when 'statusline' (indirectly)
6739 * invokes ":redrawstatus". Simply ignore the call then. */
6740 if (busy)
6741 return;
6742 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
6744 wp->w_redr_status = FALSE;
6745 if (wp->w_status_height == 0)
6746 {
6747 /* no status line, can only be last window */
6748 redraw_cmdline = TRUE;
6749 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006750 else if (!redrawing()
6751#ifdef FEAT_INS_EXPAND
6752 /* don't update status line when popup menu is visible and may be
6753 * drawn over it */
6754 || pum_visible()
6755#endif
6756 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 {
6758 /* Don't redraw right now, do it later. */
6759 wp->w_redr_status = TRUE;
6760 }
6761#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006762 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 {
6764 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006765 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 }
6767#endif
6768 else
6769 {
6770 fillchar = fillchar_status(&attr, wp == curwin);
6771
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006772 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 p = NameBuff;
6774 len = (int)STRLEN(p);
6775
6776 if (wp->w_buffer->b_help
6777#ifdef FEAT_QUICKFIX
6778 || wp->w_p_pvw
6779#endif
6780 || bufIsChanged(wp->w_buffer)
6781 || wp->w_buffer->b_p_ro)
6782 *(p + len++) = ' ';
6783 if (wp->w_buffer->b_help)
6784 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006785 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 len += (int)STRLEN(p + len);
6787 }
6788#ifdef FEAT_QUICKFIX
6789 if (wp->w_p_pvw)
6790 {
6791 STRCPY(p + len, _("[Preview]"));
6792 len += (int)STRLEN(p + len);
6793 }
6794#endif
6795 if (bufIsChanged(wp->w_buffer))
6796 {
6797 STRCPY(p + len, "[+]");
6798 len += 3;
6799 }
6800 if (wp->w_buffer->b_p_ro)
6801 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006802 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006803 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 }
6805
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6807 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6808 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6809 if (this_ru_col <= 1)
6810 {
6811 p = (char_u *)"<"; /* No room for file name! */
6812 len = 1;
6813 }
6814 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815#ifdef FEAT_MBYTE
6816 if (has_mbyte)
6817 {
6818 int clen = 0, i;
6819
6820 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006821 clen = mb_string2cells(p, -1);
6822
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 /* Find first character that will fit.
6824 * Going from start to end is much faster for DBCS. */
6825 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006826 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 clen -= (*mb_ptr2cells)(p + i);
6828 len = clen;
6829 if (i > 0)
6830 {
6831 p = p + i - 1;
6832 *p = '<';
6833 ++len;
6834 }
6835
6836 }
6837 else
6838#endif
6839 if (len > this_ru_col - 1)
6840 {
6841 p += len - (this_ru_col - 1);
6842 *p = '<';
6843 len = this_ru_col - 1;
6844 }
6845
6846 row = W_WINROW(wp) + wp->w_height;
6847 screen_puts(p, row, W_WINCOL(wp), attr);
6848 screen_fill(row, row + 1, len + W_WINCOL(wp),
6849 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6850
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006851 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6853 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6854 - 1 + W_WINCOL(wp)), attr);
6855
6856#ifdef FEAT_CMDL_INFO
6857 win_redr_ruler(wp, TRUE);
6858#endif
6859 }
6860
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 /*
6862 * May need to draw the character below the vertical separator.
6863 */
6864 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6865 {
6866 if (stl_connected(wp))
6867 fillchar = fillchar_status(&attr, wp == curwin);
6868 else
6869 fillchar = fillchar_vsep(&attr);
6870 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6871 attr);
6872 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006873 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874}
6875
Bram Moolenaar238a5642006-02-21 22:12:05 +00006876#ifdef FEAT_STL_OPT
6877/*
6878 * Redraw the status line according to 'statusline' and take care of any
6879 * errors encountered.
6880 */
6881 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006882redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006883{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006884 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006885 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006886
6887 /* When called recursively return. This can happen when the statusline
6888 * contains an expression that triggers a redraw. */
6889 if (entered)
6890 return;
6891 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006892
Bram Moolenaara742e082016-04-05 21:10:38 +02006893 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006894 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006895 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006896 {
6897 /* When there is an error disable the statusline, otherwise the
6898 * display is messed up with errors and a redraw triggers the problem
6899 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006900 set_string_option_direct((char_u *)"statusline", -1,
6901 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006902 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006903 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006904 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006905 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006906}
6907#endif
6908
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909/*
6910 * Return TRUE if the status line of window "wp" is connected to the status
6911 * line of the window right of it. If not, then it's a vertical separator.
6912 * Only call if (wp->w_vsep_width != 0).
6913 */
6914 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006915stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916{
6917 frame_T *fr;
6918
6919 fr = wp->w_frame;
6920 while (fr->fr_parent != NULL)
6921 {
6922 if (fr->fr_parent->fr_layout == FR_COL)
6923 {
6924 if (fr->fr_next != NULL)
6925 break;
6926 }
6927 else
6928 {
6929 if (fr->fr_next != NULL)
6930 return TRUE;
6931 }
6932 fr = fr->fr_parent;
6933 }
6934 return FALSE;
6935}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937#endif /* FEAT_WINDOWS */
6938
6939#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6940/*
6941 * Get the value to show for the language mappings, active 'keymap'.
6942 */
6943 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006944get_keymap_str(
6945 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006946 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006947 char_u *buf, /* buffer for the result */
6948 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 char_u *p;
6951
6952 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6953 return FALSE;
6954
6955 {
6956#ifdef FEAT_EVAL
6957 buf_T *old_curbuf = curbuf;
6958 win_T *old_curwin = curwin;
6959 char_u *s;
6960
6961 curbuf = wp->w_buffer;
6962 curwin = wp;
6963 STRCPY(buf, "b:keymap_name"); /* must be writable */
6964 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006965 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 --emsg_skip;
6967 curbuf = old_curbuf;
6968 curwin = old_curwin;
6969 if (p == NULL || *p == NUL)
6970#endif
6971 {
6972#ifdef FEAT_KEYMAP
6973 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6974 p = wp->w_buffer->b_p_keymap;
6975 else
6976#endif
6977 p = (char_u *)"lang";
6978 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006979 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 buf[0] = NUL;
6981#ifdef FEAT_EVAL
6982 vim_free(s);
6983#endif
6984 }
6985 return buf[0] != NUL;
6986}
6987#endif
6988
6989#if defined(FEAT_STL_OPT) || defined(PROTO)
6990/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006991 * Redraw the status line or ruler of window "wp".
6992 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 */
6994 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006995win_redr_custom(
6996 win_T *wp,
6997 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006999 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 int attr;
7001 int curattr;
7002 int row;
7003 int col = 0;
7004 int maxwidth;
7005 int width;
7006 int n;
7007 int len;
7008 int fillchar;
7009 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007010 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007012 struct stl_hlrec hltab[STL_MAX_ITEM];
7013 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007014 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007015 win_T *ewp;
7016 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
Bram Moolenaar1d633412013-12-11 15:52:01 +01007018 /* There is a tiny chance that this gets called recursively: When
7019 * redrawing a status line triggers redrawing the ruler or tabline.
7020 * Avoid trouble by not allowing recursion. */
7021 if (entered)
7022 return;
7023 entered = TRUE;
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007026 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007028 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007029 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007030 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007031 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007032 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007033 maxwidth = Columns;
7034# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007035 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007036# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007038 else
7039 {
7040 row = W_WINROW(wp) + wp->w_height;
7041 fillchar = fillchar_status(&attr, wp == curwin);
7042 maxwidth = W_WIDTH(wp);
7043
7044 if (draw_ruler)
7045 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007046 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007047 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007048 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007049 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007050 if (*++stl == '-')
7051 stl++;
7052 if (atoi((char *)stl))
7053 while (VIM_ISDIGIT(*stl))
7054 stl++;
7055 if (*stl++ != '(')
7056 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007057 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007058#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007059 col = ru_col - (Columns - W_WIDTH(wp));
7060 if (col < (W_WIDTH(wp) + 1) / 2)
7061 col = (W_WIDTH(wp) + 1) / 2;
7062#else
7063 col = ru_col;
7064 if (col > (Columns + 1) / 2)
7065 col = (Columns + 1) / 2;
7066#endif
7067 maxwidth = W_WIDTH(wp) - col;
7068#ifdef FEAT_WINDOWS
7069 if (!wp->w_status_height)
7070#endif
7071 {
7072 row = Rows - 1;
7073 --maxwidth; /* writing in last column may cause scrolling */
7074 fillchar = ' ';
7075 attr = 0;
7076 }
7077
7078# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007079 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007080# endif
7081 }
7082 else
7083 {
7084 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007085 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007086 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007087 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007088# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007089 use_sandbox = was_set_insecurely((char_u *)"statusline",
7090 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007091# endif
7092 }
7093
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007094#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007095 col += W_WINCOL(wp);
7096#endif
7097 }
7098
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007100 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101
Bram Moolenaar61452852011-02-01 18:01:11 +01007102 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7103 * the cursor away and back. */
7104 ewp = wp == NULL ? curwin : wp;
7105 p_crb_save = ewp->w_p_crb;
7106 ewp->w_p_crb = FALSE;
7107
Bram Moolenaar362f3562009-11-03 16:20:34 +00007108 /* Make a copy, because the statusline may include a function call that
7109 * might change the option value and free the memory. */
7110 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007111 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007112 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007113 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007114 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007115 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007117 /* Make all characters printable. */
7118 p = transstr(buf);
7119 if (p != NULL)
7120 {
7121 vim_strncpy(buf, p, sizeof(buf) - 1);
7122 vim_free(p);
7123 }
7124
7125 /* fill up with "fillchar" */
7126 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007127 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 {
7129#ifdef FEAT_MBYTE
7130 len += (*mb_char2bytes)(fillchar, buf + len);
7131#else
7132 buf[len++] = fillchar;
7133#endif
7134 ++width;
7135 }
7136 buf[len] = NUL;
7137
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007138 /*
7139 * Draw each snippet with the specified highlighting.
7140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 curattr = attr;
7142 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007143 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007145 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 screen_puts_len(p, len, row, col, curattr);
7147 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007148 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007150 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007152 else if (hltab[n].userhl < 0)
7153 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007155 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007156 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157#endif
7158 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007159 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 }
7161 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007162
7163 if (wp == NULL)
7164 {
7165 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7166 col = 0;
7167 len = 0;
7168 p = buf;
7169 fillchar = 0;
7170 for (n = 0; tabtab[n].start != NULL; n++)
7171 {
7172 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7173 while (col < len)
7174 TabPageIdxs[col++] = fillchar;
7175 p = tabtab[n].start;
7176 fillchar = tabtab[n].userhl;
7177 }
7178 while (col < Columns)
7179 TabPageIdxs[col++] = fillchar;
7180 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007181
7182theend:
7183 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184}
7185
7186#endif /* FEAT_STL_OPT */
7187
7188/*
7189 * Output a single character directly to the screen and update ScreenLines.
7190 */
7191 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007192screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 char_u buf[MB_MAXBYTES + 1];
7195
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007196#ifdef FEAT_MBYTE
7197 if (has_mbyte)
7198 buf[(*mb_char2bytes)(c, buf)] = NUL;
7199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007201 {
7202 buf[0] = c;
7203 buf[1] = NUL;
7204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 screen_puts(buf, row, col, attr);
7206}
7207
7208/*
7209 * Get a single character directly from ScreenLines into "bytes[]".
7210 * Also return its attribute in *attrp;
7211 */
7212 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007213screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 unsigned off;
7216
7217 /* safety check */
7218 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7219 {
7220 off = LineOffset[row] + col;
7221 *attrp = ScreenAttrs[off];
7222 bytes[0] = ScreenLines[off];
7223 bytes[1] = NUL;
7224
7225#ifdef FEAT_MBYTE
7226 if (enc_utf8 && ScreenLinesUC[off] != 0)
7227 bytes[utfc_char2bytes(off, bytes)] = NUL;
7228 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7229 {
7230 bytes[0] = ScreenLines[off];
7231 bytes[1] = ScreenLines2[off];
7232 bytes[2] = NUL;
7233 }
7234 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7235 {
7236 bytes[1] = ScreenLines[off + 1];
7237 bytes[2] = NUL;
7238 }
7239#endif
7240 }
7241}
7242
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007243#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007244static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007245
7246/*
7247 * Return TRUE if composing characters for screen posn "off" differs from
7248 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007249 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007250 */
7251 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007252screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007253{
7254 int i;
7255
7256 for (i = 0; i < Screen_mco; ++i)
7257 {
7258 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7259 return TRUE;
7260 if (u8cc[i] == 0)
7261 break;
7262 }
7263 return FALSE;
7264}
7265#endif
7266
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267/*
7268 * Put string '*text' on the screen at position 'row' and 'col', with
7269 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7270 * Note: only outputs within one row, message is truncated at screen boundary!
7271 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7272 */
7273 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007274screen_puts(
7275 char_u *text,
7276 int row,
7277 int col,
7278 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
7280 screen_puts_len(text, -1, row, col, attr);
7281}
7282
7283/*
7284 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7285 * a NUL.
7286 */
7287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007288screen_puts_len(
7289 char_u *text,
7290 int textlen,
7291 int row,
7292 int col,
7293 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
7295 unsigned off;
7296 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007297 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 int c;
7299#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007300 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 int mbyte_blen = 1;
7302 int mbyte_cells = 1;
7303 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007304 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 int clear_next_cell = FALSE;
7306# ifdef FEAT_ARABIC
7307 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007308 int pc, nc, nc1;
7309 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310# endif
7311#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007312#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7313 int force_redraw_this;
7314 int force_redraw_next = FALSE;
7315#endif
7316 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
7318 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7319 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007320 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
Bram Moolenaarc236c162008-07-13 17:41:49 +00007322#ifdef FEAT_MBYTE
7323 /* When drawing over the right halve of a double-wide char clear out the
7324 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007325 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007326# ifdef FEAT_GUI
7327 && !gui.in_use
7328# endif
7329 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007330 {
7331 ScreenLines[off - 1] = ' ';
7332 ScreenAttrs[off - 1] = 0;
7333 if (enc_utf8)
7334 {
7335 ScreenLinesUC[off - 1] = 0;
7336 ScreenLinesC[0][off - 1] = 0;
7337 }
7338 /* redraw the previous cell, make it empty */
7339 screen_char(off - 1, row, col - 1);
7340 /* force the cell at "col" to be redrawn */
7341 force_redraw_next = TRUE;
7342 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007343#endif
7344
Bram Moolenaar367329b2007-08-30 11:53:22 +00007345#ifdef FEAT_MBYTE
7346 max_off = LineOffset[row] + screen_Columns;
7347#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007348 while (col < screen_Columns
7349 && (len < 0 || (int)(ptr - text) < len)
7350 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 {
7352 c = *ptr;
7353#ifdef FEAT_MBYTE
7354 /* check if this is the first byte of a multibyte */
7355 if (has_mbyte)
7356 {
7357 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007358 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007360 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7362 mbyte_cells = 1;
7363 else if (enc_dbcs != 0)
7364 mbyte_cells = mbyte_blen;
7365 else /* enc_utf8 */
7366 {
7367 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007368 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 (int)((text + len) - ptr));
7370 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007371 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007373# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 /* Non-BMP character: display as ? or fullwidth ?. */
7375 if (u8c >= 0x10000)
7376 {
7377 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7378 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007379 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382# ifdef FEAT_ARABIC
7383 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7384 {
7385 /* Do Arabic shaping. */
7386 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7387 {
7388 /* Past end of string to be displayed. */
7389 nc = NUL;
7390 nc1 = NUL;
7391 }
7392 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007393 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007394 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7395 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007396 nc1 = pcc[0];
7397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 pc = prev_c;
7399 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007400 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 }
7402 else
7403 prev_c = u8c;
7404# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007405 if (col + mbyte_cells > screen_Columns)
7406 {
7407 /* Only 1 cell left, but character requires 2 cells:
7408 * display a '>' in the last column to avoid wrapping. */
7409 c = '>';
7410 mbyte_cells = 1;
7411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413 }
7414#endif
7415
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007416#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7417 force_redraw_this = force_redraw_next;
7418 force_redraw_next = FALSE;
7419#endif
7420
7421 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422#ifdef FEAT_MBYTE
7423 || (mbyte_cells == 2
7424 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7425 || (enc_dbcs == DBCS_JPNU
7426 && c == 0x8e
7427 && ScreenLines2[off] != ptr[1])
7428 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007429 && (ScreenLinesUC[off] !=
7430 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7431 || (ScreenLinesUC[off] != 0
7432 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433#endif
7434 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007435 || exmode_active;
7436
7437 if (need_redraw
7438#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7439 || force_redraw_this
7440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 )
7442 {
7443#if defined(FEAT_GUI) || defined(UNIX)
7444 /* The bold trick makes a single row of pixels appear in the next
7445 * character. When a bold character is removed, the next
7446 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007447 * and for some xterms. */
7448 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449# ifdef FEAT_GUI
7450 gui.in_use
7451# endif
7452# if defined(FEAT_GUI) && defined(UNIX)
7453 ||
7454# endif
7455# ifdef UNIX
7456 term_is_xterm
7457# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007458 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007460 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007462 if (n > HL_ALL)
7463 n = syn_attr2attr(n);
7464 if (n & HL_BOLD)
7465 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 }
7467#endif
7468#ifdef FEAT_MBYTE
7469 /* When at the end of the text and overwriting a two-cell
7470 * character with a one-cell character, need to clear the next
7471 * cell. Also when overwriting the left halve of a two-cell char
7472 * with the right halve of a two-cell char. Do this only once
7473 * (mb_off2cells() may return 2 on the right halve). */
7474 if (clear_next_cell)
7475 clear_next_cell = FALSE;
7476 else if (has_mbyte
7477 && (len < 0 ? ptr[mbyte_blen] == NUL
7478 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007479 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007481 && (*mb_off2cells)(off, max_off) == 1
7482 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 clear_next_cell = TRUE;
7484
7485 /* Make sure we never leave a second byte of a double-byte behind,
7486 * it confuses mb_off2cells(). */
7487 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007488 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007490 && (*mb_off2cells)(off, max_off) == 1
7491 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 ScreenLines[off + mbyte_blen] = 0;
7493#endif
7494 ScreenLines[off] = c;
7495 ScreenAttrs[off] = attr;
7496#ifdef FEAT_MBYTE
7497 if (enc_utf8)
7498 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007499 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 ScreenLinesUC[off] = 0;
7501 else
7502 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007503 int i;
7504
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007506 for (i = 0; i < Screen_mco; ++i)
7507 {
7508 ScreenLinesC[i][off] = u8cc[i];
7509 if (u8cc[i] == 0)
7510 break;
7511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
7513 if (mbyte_cells == 2)
7514 {
7515 ScreenLines[off + 1] = 0;
7516 ScreenAttrs[off + 1] = attr;
7517 }
7518 screen_char(off, row, col);
7519 }
7520 else if (mbyte_cells == 2)
7521 {
7522 ScreenLines[off + 1] = ptr[1];
7523 ScreenAttrs[off + 1] = attr;
7524 screen_char_2(off, row, col);
7525 }
7526 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7527 {
7528 ScreenLines2[off] = ptr[1];
7529 screen_char(off, row, col);
7530 }
7531 else
7532#endif
7533 screen_char(off, row, col);
7534 }
7535#ifdef FEAT_MBYTE
7536 if (has_mbyte)
7537 {
7538 off += mbyte_cells;
7539 col += mbyte_cells;
7540 ptr += mbyte_blen;
7541 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007542 {
7543 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007545 len = -1;
7546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 }
7548 else
7549#endif
7550 {
7551 ++off;
7552 ++col;
7553 ++ptr;
7554 }
7555 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007556
7557#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7558 /* If we detected the next character needs to be redrawn, but the text
7559 * doesn't extend up to there, update the character here. */
7560 if (force_redraw_next && col < screen_Columns)
7561 {
7562# ifdef FEAT_MBYTE
7563 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7564 screen_char_2(off, row, col);
7565 else
7566# endif
7567 screen_char(off, row, col);
7568 }
7569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570}
7571
7572#ifdef FEAT_SEARCH_EXTRA
7573/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007574 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 */
7576 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007577start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
7579 if (p_hls && !no_hlsearch)
7580 {
7581 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007582 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007583# ifdef FEAT_RELTIME
7584 /* Set the time limit to 'redrawtime'. */
7585 profile_setlimit(p_rdt, &search_hl.tm);
7586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588}
7589
7590/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007591 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 */
7593 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007594end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
7596 if (search_hl.rm.regprog != NULL)
7597 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007598 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 search_hl.rm.regprog = NULL;
7600 }
7601}
7602
7603/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007604 * Init for calling prepare_search_hl().
7605 */
7606 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007607init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007608{
7609 matchitem_T *cur;
7610
7611 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7612 * match */
7613 cur = wp->w_match_head;
7614 while (cur != NULL)
7615 {
7616 cur->hl.rm = cur->match;
7617 if (cur->hlg_id == 0)
7618 cur->hl.attr = 0;
7619 else
7620 cur->hl.attr = syn_id2attr(cur->hlg_id);
7621 cur->hl.buf = wp->w_buffer;
7622 cur->hl.lnum = 0;
7623 cur->hl.first_lnum = 0;
7624# ifdef FEAT_RELTIME
7625 /* Set the time limit to 'redrawtime'. */
7626 profile_setlimit(p_rdt, &(cur->hl.tm));
7627# endif
7628 cur = cur->next;
7629 }
7630 search_hl.buf = wp->w_buffer;
7631 search_hl.lnum = 0;
7632 search_hl.first_lnum = 0;
7633 /* time limit is set at the toplevel, for all windows */
7634}
7635
7636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 * Advance to the match in window "wp" line "lnum" or past it.
7638 */
7639 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007640prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007642 matchitem_T *cur; /* points to the match list */
7643 match_T *shl; /* points to search_hl or a match */
7644 int shl_flag; /* flag to indicate whether search_hl
7645 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007646 int pos_inprogress; /* marks that position match search is
7647 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 int n;
7649
7650 /*
7651 * When using a multi-line pattern, start searching at the top
7652 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007653 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007655 cur = wp->w_match_head;
7656 shl_flag = FALSE;
7657 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007659 if (shl_flag == FALSE)
7660 {
7661 shl = &search_hl;
7662 shl_flag = TRUE;
7663 }
7664 else
7665 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (shl->rm.regprog != NULL
7667 && shl->lnum == 0
7668 && re_multiline(shl->rm.regprog))
7669 {
7670 if (shl->first_lnum == 0)
7671 {
7672# ifdef FEAT_FOLDING
7673 for (shl->first_lnum = lnum;
7674 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7675 if (hasFoldingWin(wp, shl->first_lnum - 1,
7676 NULL, NULL, TRUE, NULL))
7677 break;
7678# else
7679 shl->first_lnum = wp->w_topline;
7680# endif
7681 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007682 if (cur != NULL)
7683 cur->pos.cur = 0;
7684 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007686 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7687 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007689 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7690 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007691 pos_inprogress = cur == NULL || cur->pos.cur == 0
7692 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 if (shl->lnum != 0)
7694 {
7695 shl->first_lnum = shl->lnum
7696 + shl->rm.endpos[0].lnum
7697 - shl->rm.startpos[0].lnum;
7698 n = shl->rm.endpos[0].col;
7699 }
7700 else
7701 {
7702 ++shl->first_lnum;
7703 n = 0;
7704 }
7705 }
7706 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007707 if (shl != &search_hl && cur != NULL)
7708 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710}
7711
7712/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007713 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 * Uses shl->buf.
7715 * Sets shl->lnum and shl->rm contents.
7716 * Note: Assumes a previous match is always before "lnum", unless
7717 * shl->lnum is zero.
7718 * Careful: Any pointers for buffer lines will become invalid.
7719 */
7720 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007721next_search_hl(
7722 win_T *win,
7723 match_T *shl, /* points to search_hl or a match */
7724 linenr_T lnum,
7725 colnr_T mincol, /* minimal column for a match */
7726 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 linenr_T l;
7729 colnr_T matchcol;
7730 long nmatched;
7731
7732 if (shl->lnum != 0)
7733 {
7734 /* Check for three situations:
7735 * 1. If the "lnum" is below a previous match, start a new search.
7736 * 2. If the previous match includes "mincol", use it.
7737 * 3. Continue after the previous match.
7738 */
7739 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7740 if (lnum > l)
7741 shl->lnum = 0;
7742 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7743 return;
7744 }
7745
7746 /*
7747 * Repeat searching for a match until one is found that includes "mincol"
7748 * or none is found in this line.
7749 */
7750 called_emsg = FALSE;
7751 for (;;)
7752 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007753#ifdef FEAT_RELTIME
7754 /* Stop searching after passing the time limit. */
7755 if (profile_passed_limit(&(shl->tm)))
7756 {
7757 shl->lnum = 0; /* no match found in time */
7758 break;
7759 }
7760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 /* Three situations:
7762 * 1. No useful previous match: search from start of line.
7763 * 2. Not Vi compatible or empty match: continue at next character.
7764 * Break the loop if this is beyond the end of the line.
7765 * 3. Vi compatible searching: continue at end of previous match.
7766 */
7767 if (shl->lnum == 0)
7768 matchcol = 0;
7769 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7770 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007771 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007773 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007774
7775 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007776 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007777 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007779 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 shl->lnum = 0;
7781 break;
7782 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007783#ifdef FEAT_MBYTE
7784 if (has_mbyte)
7785 matchcol += mb_ptr2len(ml);
7786 else
7787#endif
7788 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 }
7790 else
7791 matchcol = shl->rm.endpos[0].col;
7792
7793 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007794 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007796 /* Remember whether shl->rm is using a copy of the regprog in
7797 * cur->match. */
7798 int regprog_is_copy = (shl != &search_hl && cur != NULL
7799 && shl == &cur->hl
7800 && cur->match.regprog == cur->hl.rm.regprog);
7801
Bram Moolenaarb3414592014-06-17 17:48:32 +02007802 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7803 matchcol,
7804#ifdef FEAT_RELTIME
7805 &(shl->tm)
7806#else
7807 NULL
7808#endif
7809 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007810 /* Copy the regprog, in case it got freed and recompiled. */
7811 if (regprog_is_copy)
7812 cur->match.regprog = cur->hl.rm.regprog;
7813
Bram Moolenaarb3414592014-06-17 17:48:32 +02007814 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007815 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007816 /* Error while handling regexp: stop using this regexp. */
7817 if (shl == &search_hl)
7818 {
7819 /* don't free regprog in the match list, it's a copy */
7820 vim_regfree(shl->rm.regprog);
7821 SET_NO_HLSEARCH(TRUE);
7822 }
7823 shl->rm.regprog = NULL;
7824 shl->lnum = 0;
7825 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7826 message */
7827 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007828 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007829 }
7830 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007831 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007832 else
7833 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 if (nmatched == 0)
7835 {
7836 shl->lnum = 0; /* no match found */
7837 break;
7838 }
7839 if (shl->rm.startpos[0].lnum > 0
7840 || shl->rm.startpos[0].col >= mincol
7841 || nmatched > 1
7842 || shl->rm.endpos[0].col > mincol)
7843 {
7844 shl->lnum += shl->rm.startpos[0].lnum;
7845 break; /* useful match found */
7846 }
7847 }
7848}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849
Bram Moolenaar85077472016-10-16 14:35:48 +02007850/*
7851 * If there is a match fill "shl" and return one.
7852 * Return zero otherwise.
7853 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007854 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007855next_search_hl_pos(
7856 match_T *shl, /* points to a match */
7857 linenr_T lnum,
7858 posmatch_T *posmatch, /* match positions */
7859 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007860{
7861 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007862 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007863
Bram Moolenaarb3414592014-06-17 17:48:32 +02007864 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7865 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007866 llpos_T *pos = &posmatch->pos[i];
7867
7868 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007869 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007870 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007871 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007872 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007873 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007874 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007875 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007876 /* if this match comes before the one at "found" then swap
7877 * them */
7878 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007879 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007880 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007881
Bram Moolenaar85077472016-10-16 14:35:48 +02007882 *pos = posmatch->pos[found];
7883 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007884 }
7885 }
7886 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007887 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007888 }
7889 }
7890 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007891 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007892 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007893 colnr_T start = posmatch->pos[found].col == 0
7894 ? 0 : posmatch->pos[found].col - 1;
7895 colnr_T end = posmatch->pos[found].col == 0
7896 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007897
Bram Moolenaar85077472016-10-16 14:35:48 +02007898 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007899 shl->rm.startpos[0].lnum = 0;
7900 shl->rm.startpos[0].col = start;
7901 shl->rm.endpos[0].lnum = 0;
7902 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007903 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007904 posmatch->cur = found + 1;
7905 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007906 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007907 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007908}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007909#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007910
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007912screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914 attrentry_T *aep = NULL;
7915
7916 screen_attr = attr;
7917 if (full_screen
7918#ifdef WIN3264
7919 && termcap_active
7920#endif
7921 )
7922 {
7923#ifdef FEAT_GUI
7924 if (gui.in_use)
7925 {
7926 char buf[20];
7927
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007928 /* The GUI handles this internally. */
7929 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 OUT_STR(buf);
7931 }
7932 else
7933#endif
7934 {
7935 if (attr > HL_ALL) /* special HL attr. */
7936 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007937 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 aep = syn_cterm_attr2entry(attr);
7939 else
7940 aep = syn_term_attr2entry(attr);
7941 if (aep == NULL) /* did ":syntax clear" */
7942 attr = 0;
7943 else
7944 attr = aep->ae_attr;
7945 }
7946 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7947 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007948 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007949#ifdef FEAT_TERMGUICOLORS
7950 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007951 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007952#endif
7953 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007954#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007955 )
7956#endif
7957 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007958 /* If the Normal FG color has BOLD attribute and the new HL
7959 * has a FG color defined, clear BOLD. */
7960 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7962 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007963 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7964 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 out_str(T_US);
7966 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7967 out_str(T_CZH);
7968 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7969 out_str(T_MR);
7970
7971 /*
7972 * Output the color or start string after bold etc., in case the
7973 * bold etc. override the color setting.
7974 */
7975 if (aep != NULL)
7976 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007977#ifdef FEAT_TERMGUICOLORS
7978 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007980 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007981 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007982 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007983 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 }
7985 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007988 if (t_colors > 1)
7989 {
7990 if (aep->ae_u.cterm.fg_color)
7991 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7992 if (aep->ae_u.cterm.bg_color)
7993 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7994 }
7995 else
7996 {
7997 if (aep->ae_u.term.start != NULL)
7998 out_str(aep->ae_u.term.start);
7999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 }
8001 }
8002 }
8003 }
8004}
8005
8006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008007screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 int do_ME = FALSE; /* output T_ME code */
8010
8011 if (screen_attr != 0
8012#ifdef WIN3264
8013 && termcap_active
8014#endif
8015 )
8016 {
8017#ifdef FEAT_GUI
8018 if (gui.in_use)
8019 {
8020 char buf[20];
8021
8022 /* use internal GUI code */
8023 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8024 OUT_STR(buf);
8025 }
8026 else
8027#endif
8028 {
8029 if (screen_attr > HL_ALL) /* special HL attr. */
8030 {
8031 attrentry_T *aep;
8032
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008033 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 {
8035 /*
8036 * Assume that t_me restores the original colors!
8037 */
8038 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008039 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008040#ifdef FEAT_TERMGUICOLORS
8041 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008042 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8043 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008044#endif
8045 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008046#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008047 )
8048#endif
8049 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 do_ME = TRUE;
8051 }
8052 else
8053 {
8054 aep = syn_term_attr2entry(screen_attr);
8055 if (aep != NULL && aep->ae_u.term.stop != NULL)
8056 {
8057 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8058 do_ME = TRUE;
8059 else
8060 out_str(aep->ae_u.term.stop);
8061 }
8062 }
8063 if (aep == NULL) /* did ":syntax clear" */
8064 screen_attr = 0;
8065 else
8066 screen_attr = aep->ae_attr;
8067 }
8068
8069 /*
8070 * Often all ending-codes are equal to T_ME. Avoid outputting the
8071 * same sequence several times.
8072 */
8073 if (screen_attr & HL_STANDOUT)
8074 {
8075 if (STRCMP(T_SE, T_ME) == 0)
8076 do_ME = TRUE;
8077 else
8078 out_str(T_SE);
8079 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008080 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
8082 if (STRCMP(T_UE, T_ME) == 0)
8083 do_ME = TRUE;
8084 else
8085 out_str(T_UE);
8086 }
8087 if (screen_attr & HL_ITALIC)
8088 {
8089 if (STRCMP(T_CZR, T_ME) == 0)
8090 do_ME = TRUE;
8091 else
8092 out_str(T_CZR);
8093 }
8094 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8095 out_str(T_ME);
8096
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008097#ifdef FEAT_TERMGUICOLORS
8098 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008100 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008101 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008102 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008103 term_bg_rgb_color(cterm_normal_bg_gui_color);
8104 }
8105 else
8106#endif
8107 {
8108 if (t_colors > 1)
8109 {
8110 /* set Normal cterm colors */
8111 if (cterm_normal_fg_color != 0)
8112 term_fg_color(cterm_normal_fg_color - 1);
8113 if (cterm_normal_bg_color != 0)
8114 term_bg_color(cterm_normal_bg_color - 1);
8115 if (cterm_normal_fg_bold)
8116 out_str(T_MD);
8117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 }
8119 }
8120 }
8121 screen_attr = 0;
8122}
8123
8124/*
8125 * Reset the colors for a cterm. Used when leaving Vim.
8126 * The machine specific code may override this again.
8127 */
8128 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008129reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008131 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {
8133 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008134#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008135 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8136 || cterm_normal_bg_gui_color != INVALCOLOR)
8137 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008138#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {
8142 out_str(T_OP);
8143 screen_attr = -1;
8144 }
8145 if (cterm_normal_fg_bold)
8146 {
8147 out_str(T_ME);
8148 screen_attr = -1;
8149 }
8150 }
8151}
8152
8153/*
8154 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8155 * using the attributes from ScreenAttrs["off"].
8156 */
8157 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008158screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159{
8160 int attr;
8161
8162 /* Check for illegal values, just in case (could happen just after
8163 * resizing). */
8164 if (row >= screen_Rows || col >= screen_Columns)
8165 return;
8166
Bram Moolenaar494838a2015-02-10 19:20:37 +01008167 /* Outputting a character in the last cell on the screen may scroll the
8168 * screen up. Only do it when the "xn" termcap property is set, otherwise
8169 * mark the character invalid (update it when scrolled up). */
8170 if (*T_XN == NUL
8171 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172#ifdef FEAT_RIGHTLEFT
8173 /* account for first command-line character in rightleft mode */
8174 && !cmdmsg_rl
8175#endif
8176 )
8177 {
8178 ScreenAttrs[off] = (sattr_T)-1;
8179 return;
8180 }
8181
8182 /*
8183 * Stop highlighting first, so it's easier to move the cursor.
8184 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008185#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 if (screen_char_attr != 0)
8187 attr = screen_char_attr;
8188 else
8189#endif
8190 attr = ScreenAttrs[off];
8191 if (screen_attr != attr)
8192 screen_stop_highlight();
8193
8194 windgoto(row, col);
8195
8196 if (screen_attr != attr)
8197 screen_start_highlight(attr);
8198
8199#ifdef FEAT_MBYTE
8200 if (enc_utf8 && ScreenLinesUC[off] != 0)
8201 {
8202 char_u buf[MB_MAXBYTES + 1];
8203
8204 /* Convert UTF-8 character to bytes and write it. */
8205
8206 buf[utfc_char2bytes(off, buf)] = NUL;
8207
8208 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008209 if (utf_ambiguous_width(ScreenLinesUC[off]))
8210 screen_cur_col = 9999;
8211 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 ++screen_cur_col;
8213 }
8214 else
8215#endif
8216 {
8217#ifdef FEAT_MBYTE
8218 out_flush_check();
8219#endif
8220 out_char(ScreenLines[off]);
8221#ifdef FEAT_MBYTE
8222 /* double-byte character in single-width cell */
8223 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8224 out_char(ScreenLines2[off]);
8225#endif
8226 }
8227
8228 screen_cur_col++;
8229}
8230
8231#ifdef FEAT_MBYTE
8232
8233/*
8234 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8235 * on the screen at position 'row' and 'col'.
8236 * The attributes of the first byte is used for all. This is required to
8237 * output the two bytes of a double-byte character with nothing in between.
8238 */
8239 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008240screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242 /* Check for illegal values (could be wrong when screen was resized). */
8243 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8244 return;
8245
8246 /* Outputting the last character on the screen may scrollup the screen.
8247 * Don't to it! Mark the character invalid (update it when scrolled up) */
8248 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8249 {
8250 ScreenAttrs[off] = (sattr_T)-1;
8251 return;
8252 }
8253
8254 /* Output the first byte normally (positions the cursor), then write the
8255 * second byte directly. */
8256 screen_char(off, row, col);
8257 out_char(ScreenLines[off + 1]);
8258 ++screen_cur_col;
8259}
8260#endif
8261
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008262#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263/*
8264 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8265 * This uses the contents of ScreenLines[] and doesn't change it.
8266 */
8267 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008268screen_draw_rectangle(
8269 int row,
8270 int col,
8271 int height,
8272 int width,
8273 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275 int r, c;
8276 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008277#ifdef FEAT_MBYTE
8278 int max_off;
8279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008281 /* Can't use ScreenLines unless initialized */
8282 if (ScreenLines == NULL)
8283 return;
8284
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 if (invert)
8286 screen_char_attr = HL_INVERSE;
8287 for (r = row; r < row + height; ++r)
8288 {
8289 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008290#ifdef FEAT_MBYTE
8291 max_off = off + screen_Columns;
8292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 for (c = col; c < col + width; ++c)
8294 {
8295#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008296 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {
8298 screen_char_2(off + c, r, c);
8299 ++c;
8300 }
8301 else
8302#endif
8303 {
8304 screen_char(off + c, r, c);
8305#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008306 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 ++c;
8308#endif
8309 }
8310 }
8311 }
8312 screen_char_attr = 0;
8313}
8314#endif
8315
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008316#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317/*
8318 * Redraw the characters for a vertically split window.
8319 */
8320 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008321redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
8323 int col;
8324 int width;
8325
8326# ifdef FEAT_CLIPBOARD
8327 clip_may_clear_selection(row, end - 1);
8328# endif
8329
8330 if (wp == NULL)
8331 {
8332 col = 0;
8333 width = Columns;
8334 }
8335 else
8336 {
8337 col = wp->w_wincol;
8338 width = wp->w_width;
8339 }
8340 screen_draw_rectangle(row, col, end - row, width, FALSE);
8341}
8342#endif
8343
8344/*
8345 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8346 * with character 'c1' in first column followed by 'c2' in the other columns.
8347 * Use attributes 'attr'.
8348 */
8349 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008350screen_fill(
8351 int start_row,
8352 int end_row,
8353 int start_col,
8354 int end_col,
8355 int c1,
8356 int c2,
8357 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358{
8359 int row;
8360 int col;
8361 int off;
8362 int end_off;
8363 int did_delete;
8364 int c;
8365 int norm_term;
8366#if defined(FEAT_GUI) || defined(UNIX)
8367 int force_next = FALSE;
8368#endif
8369
8370 if (end_row > screen_Rows) /* safety check */
8371 end_row = screen_Rows;
8372 if (end_col > screen_Columns) /* safety check */
8373 end_col = screen_Columns;
8374 if (ScreenLines == NULL
8375 || start_row >= end_row
8376 || start_col >= end_col) /* nothing to do */
8377 return;
8378
8379 /* it's a "normal" terminal when not in a GUI or cterm */
8380 norm_term = (
8381#ifdef FEAT_GUI
8382 !gui.in_use &&
8383#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008384 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 for (row = start_row; row < end_row; ++row)
8386 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008387#ifdef FEAT_MBYTE
8388 if (has_mbyte
8389# ifdef FEAT_GUI
8390 && !gui.in_use
8391# endif
8392 )
8393 {
8394 /* When drawing over the right halve of a double-wide char clear
8395 * out the left halve. When drawing over the left halve of a
8396 * double wide-char clear out the right halve. Only needed in a
8397 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008398 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008399 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008400 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008401 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008402 }
8403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 /*
8405 * Try to use delete-line termcap code, when no attributes or in a
8406 * "normal" terminal, where a bold/italic space is just a
8407 * space.
8408 */
8409 did_delete = FALSE;
8410 if (c2 == ' '
8411 && end_col == Columns
8412 && can_clear(T_CE)
8413 && (attr == 0
8414 || (norm_term
8415 && attr <= HL_ALL
8416 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8417 {
8418 /*
8419 * check if we really need to clear something
8420 */
8421 col = start_col;
8422 if (c1 != ' ') /* don't clear first char */
8423 ++col;
8424
8425 off = LineOffset[row] + col;
8426 end_off = LineOffset[row] + end_col;
8427
8428 /* skip blanks (used often, keep it fast!) */
8429#ifdef FEAT_MBYTE
8430 if (enc_utf8)
8431 while (off < end_off && ScreenLines[off] == ' '
8432 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8433 ++off;
8434 else
8435#endif
8436 while (off < end_off && ScreenLines[off] == ' '
8437 && ScreenAttrs[off] == 0)
8438 ++off;
8439 if (off < end_off) /* something to be cleared */
8440 {
8441 col = off - LineOffset[row];
8442 screen_stop_highlight();
8443 term_windgoto(row, col);/* clear rest of this screen line */
8444 out_str(T_CE);
8445 screen_start(); /* don't know where cursor is now */
8446 col = end_col - col;
8447 while (col--) /* clear chars in ScreenLines */
8448 {
8449 ScreenLines[off] = ' ';
8450#ifdef FEAT_MBYTE
8451 if (enc_utf8)
8452 ScreenLinesUC[off] = 0;
8453#endif
8454 ScreenAttrs[off] = 0;
8455 ++off;
8456 }
8457 }
8458 did_delete = TRUE; /* the chars are cleared now */
8459 }
8460
8461 off = LineOffset[row] + start_col;
8462 c = c1;
8463 for (col = start_col; col < end_col; ++col)
8464 {
8465 if (ScreenLines[off] != c
8466#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008467 || (enc_utf8 && (int)ScreenLinesUC[off]
8468 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469#endif
8470 || ScreenAttrs[off] != attr
8471#if defined(FEAT_GUI) || defined(UNIX)
8472 || force_next
8473#endif
8474 )
8475 {
8476#if defined(FEAT_GUI) || defined(UNIX)
8477 /* The bold trick may make a single row of pixels appear in
8478 * the next character. When a bold character is removed, the
8479 * next character should be redrawn too. This happens for our
8480 * own GUI and for some xterms. */
8481 if (
8482# ifdef FEAT_GUI
8483 gui.in_use
8484# endif
8485# if defined(FEAT_GUI) && defined(UNIX)
8486 ||
8487# endif
8488# ifdef UNIX
8489 term_is_xterm
8490# endif
8491 )
8492 {
8493 if (ScreenLines[off] != ' '
8494 && (ScreenAttrs[off] > HL_ALL
8495 || ScreenAttrs[off] & HL_BOLD))
8496 force_next = TRUE;
8497 else
8498 force_next = FALSE;
8499 }
8500#endif
8501 ScreenLines[off] = c;
8502#ifdef FEAT_MBYTE
8503 if (enc_utf8)
8504 {
8505 if (c >= 0x80)
8506 {
8507 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008508 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 }
8510 else
8511 ScreenLinesUC[off] = 0;
8512 }
8513#endif
8514 ScreenAttrs[off] = attr;
8515 if (!did_delete || c != ' ')
8516 screen_char(off, row, col);
8517 }
8518 ++off;
8519 if (col == start_col)
8520 {
8521 if (did_delete)
8522 break;
8523 c = c2;
8524 }
8525 }
8526 if (end_col == Columns)
8527 LineWraps[row] = FALSE;
8528 if (row == Rows - 1) /* overwritten the command line */
8529 {
8530 redraw_cmdline = TRUE;
8531 if (c1 == ' ' && c2 == ' ')
8532 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008533 if (start_col == 0)
8534 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 }
8536 }
8537}
8538
8539/*
8540 * Check if there should be a delay. Used before clearing or redrawing the
8541 * screen or the command line.
8542 */
8543 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008544check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545{
8546 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8547 && !did_wait_return
8548 && emsg_silent == 0)
8549 {
8550 out_flush();
8551 ui_delay(1000L, TRUE);
8552 emsg_on_display = FALSE;
8553 if (check_msg_scroll)
8554 msg_scroll = FALSE;
8555 }
8556}
8557
8558/*
8559 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008560 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 * Returns TRUE if there is a valid screen to write to.
8562 * Returns FALSE when starting up and screen not initialized yet.
8563 */
8564 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008565screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008567 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 return (ScreenLines != NULL);
8569}
8570
8571/*
8572 * Resize the shell to Rows and Columns.
8573 * Allocate ScreenLines[] and associated items.
8574 *
8575 * There may be some time between setting Rows and Columns and (re)allocating
8576 * ScreenLines[]. This happens when starting up and when (manually) changing
8577 * the shell size. Always use screen_Rows and screen_Columns to access items
8578 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8579 * final size of the shell is needed.
8580 */
8581 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008582screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
8584 int new_row, old_row;
8585#ifdef FEAT_GUI
8586 int old_Rows;
8587#endif
8588 win_T *wp;
8589 int outofmem = FALSE;
8590 int len;
8591 schar_T *new_ScreenLines;
8592#ifdef FEAT_MBYTE
8593 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008594 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008596 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#endif
8598 sattr_T *new_ScreenAttrs;
8599 unsigned *new_LineOffset;
8600 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008601#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008602 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008603 tabpage_T *tp;
8604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008606 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008607#ifdef FEAT_AUTOCMD
8608 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008610retry:
8611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 /*
8613 * Allocation of the screen buffers is done only when the size changes and
8614 * when Rows and Columns have been set and we have started doing full
8615 * screen stuff.
8616 */
8617 if ((ScreenLines != NULL
8618 && Rows == screen_Rows
8619 && Columns == screen_Columns
8620#ifdef FEAT_MBYTE
8621 && enc_utf8 == (ScreenLinesUC != NULL)
8622 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008623 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624#endif
8625 )
8626 || Rows == 0
8627 || Columns == 0
8628 || (!full_screen && ScreenLines == NULL))
8629 return;
8630
8631 /*
8632 * It's possible that we produce an out-of-memory message below, which
8633 * will cause this function to be called again. To break the loop, just
8634 * return here.
8635 */
8636 if (entered)
8637 return;
8638 entered = TRUE;
8639
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008640 /*
8641 * Note that the window sizes are updated before reallocating the arrays,
8642 * thus we must not redraw here!
8643 */
8644 ++RedrawingDisabled;
8645
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 win_new_shellsize(); /* fit the windows in the new sized shell */
8647
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 comp_col(); /* recompute columns for shown command and ruler */
8649
8650 /*
8651 * We're changing the size of the screen.
8652 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8653 * - Move lines from the old arrays into the new arrays, clear extra
8654 * lines (unless the screen is going to be cleared).
8655 * - Free the old arrays.
8656 *
8657 * If anything fails, make ScreenLines NULL, so we don't do anything!
8658 * Continuing with the old ScreenLines may result in a crash, because the
8659 * size is wrong.
8660 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008661 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008663#ifdef FEAT_AUTOCMD
8664 if (aucmd_win != NULL)
8665 win_free_lsize(aucmd_win);
8666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
8668 new_ScreenLines = (schar_T *)lalloc((long_u)(
8669 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8670#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008671 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 if (enc_utf8)
8673 {
8674 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8675 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008676 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008677 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8679 }
8680 if (enc_dbcs == DBCS_JPNU)
8681 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8682 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8683#endif
8684 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8685 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8686 new_LineOffset = (unsigned *)lalloc((long_u)(
8687 Rows * sizeof(unsigned)), FALSE);
8688 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008689#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008690 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008693 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 {
8695 if (win_alloc_lines(wp) == FAIL)
8696 {
8697 outofmem = TRUE;
8698#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008699 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#endif
8701 }
8702 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008703#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008704 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8705 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008706 outofmem = TRUE;
8707#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008708#ifdef FEAT_WINDOWS
8709give_up:
8710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008712#ifdef FEAT_MBYTE
8713 for (i = 0; i < p_mco; ++i)
8714 if (new_ScreenLinesC[i] == NULL)
8715 break;
8716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 if (new_ScreenLines == NULL
8718#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008719 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8721#endif
8722 || new_ScreenAttrs == NULL
8723 || new_LineOffset == NULL
8724 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008725#ifdef FEAT_WINDOWS
8726 || new_TabPageIdxs == NULL
8727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 || outofmem)
8729 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008730 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008731 {
8732 /* guess the size */
8733 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8734
8735 /* Remember we did this to avoid getting outofmem messages over
8736 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008737 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 vim_free(new_ScreenLines);
8740 new_ScreenLines = NULL;
8741#ifdef FEAT_MBYTE
8742 vim_free(new_ScreenLinesUC);
8743 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008744 for (i = 0; i < p_mco; ++i)
8745 {
8746 vim_free(new_ScreenLinesC[i]);
8747 new_ScreenLinesC[i] = NULL;
8748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 vim_free(new_ScreenLines2);
8750 new_ScreenLines2 = NULL;
8751#endif
8752 vim_free(new_ScreenAttrs);
8753 new_ScreenAttrs = NULL;
8754 vim_free(new_LineOffset);
8755 new_LineOffset = NULL;
8756 vim_free(new_LineWraps);
8757 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008758#ifdef FEAT_WINDOWS
8759 vim_free(new_TabPageIdxs);
8760 new_TabPageIdxs = NULL;
8761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
8763 else
8764 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008765 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008766
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 for (new_row = 0; new_row < Rows; ++new_row)
8768 {
8769 new_LineOffset[new_row] = new_row * Columns;
8770 new_LineWraps[new_row] = FALSE;
8771
8772 /*
8773 * If the screen is not going to be cleared, copy as much as
8774 * possible from the old screen to the new one and clear the rest
8775 * (used when resizing the window at the "--more--" prompt or when
8776 * executing an external command, for the GUI).
8777 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008778 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 {
8780 (void)vim_memset(new_ScreenLines + new_row * Columns,
8781 ' ', (size_t)Columns * sizeof(schar_T));
8782#ifdef FEAT_MBYTE
8783 if (enc_utf8)
8784 {
8785 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8786 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008787 for (i = 0; i < p_mco; ++i)
8788 (void)vim_memset(new_ScreenLinesC[i]
8789 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 0, (size_t)Columns * sizeof(u8char_T));
8791 }
8792 if (enc_dbcs == DBCS_JPNU)
8793 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8794 0, (size_t)Columns * sizeof(schar_T));
8795#endif
8796 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8797 0, (size_t)Columns * sizeof(sattr_T));
8798 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008799 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
8801 if (screen_Columns < Columns)
8802 len = screen_Columns;
8803 else
8804 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008805#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008806 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008807 * may be invalid now. Also when p_mco changes. */
8808 if (!(enc_utf8 && ScreenLinesUC == NULL)
8809 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008810#endif
8811 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8812 ScreenLines + LineOffset[old_row],
8813 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008815 if (enc_utf8 && ScreenLinesUC != NULL
8816 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
8818 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8819 ScreenLinesUC + LineOffset[old_row],
8820 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008821 for (i = 0; i < p_mco; ++i)
8822 mch_memmove(new_ScreenLinesC[i]
8823 + new_LineOffset[new_row],
8824 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 (size_t)len * sizeof(u8char_T));
8826 }
8827 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8828 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8829 ScreenLines2 + LineOffset[old_row],
8830 (size_t)len * sizeof(schar_T));
8831#endif
8832 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8833 ScreenAttrs + LineOffset[old_row],
8834 (size_t)len * sizeof(sattr_T));
8835 }
8836 }
8837 }
8838 /* Use the last line of the screen for the current line. */
8839 current_ScreenLine = new_ScreenLines + Rows * Columns;
8840 }
8841
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008842 free_screenlines();
8843
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 ScreenLines = new_ScreenLines;
8845#ifdef FEAT_MBYTE
8846 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008847 for (i = 0; i < p_mco; ++i)
8848 ScreenLinesC[i] = new_ScreenLinesC[i];
8849 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 ScreenLines2 = new_ScreenLines2;
8851#endif
8852 ScreenAttrs = new_ScreenAttrs;
8853 LineOffset = new_LineOffset;
8854 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008855#ifdef FEAT_WINDOWS
8856 TabPageIdxs = new_TabPageIdxs;
8857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858
8859 /* It's important that screen_Rows and screen_Columns reflect the actual
8860 * size of ScreenLines[]. Set them before calling anything. */
8861#ifdef FEAT_GUI
8862 old_Rows = screen_Rows;
8863#endif
8864 screen_Rows = Rows;
8865 screen_Columns = Columns;
8866
8867 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008868 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 screenclear2();
8870
8871#ifdef FEAT_GUI
8872 else if (gui.in_use
8873 && !gui.starting
8874 && ScreenLines != NULL
8875 && old_Rows != Rows)
8876 {
8877 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8878 /*
8879 * Adjust the position of the cursor, for when executing an external
8880 * command.
8881 */
8882 if (msg_row >= Rows) /* Rows got smaller */
8883 msg_row = Rows - 1; /* put cursor at last row */
8884 else if (Rows > old_Rows) /* Rows got bigger */
8885 msg_row += Rows - old_Rows; /* put cursor in same place */
8886 if (msg_col >= Columns) /* Columns got smaller */
8887 msg_col = Columns - 1; /* put cursor at last column */
8888 }
8889#endif
8890
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008892 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008893
8894#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008895 /*
8896 * Do not apply autocommands more than 3 times to avoid an endless loop
8897 * in case applying autocommands always changes Rows or Columns.
8898 */
8899 if (starting == 0 && ++retry_count <= 3)
8900 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008901 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008902 /* In rare cases, autocommands may have altered Rows or Columns,
8903 * jump back to check if we need to allocate the screen again. */
8904 goto retry;
8905 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907}
8908
8909 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008910free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008911{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008912#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008913 int i;
8914
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008915 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008916 for (i = 0; i < Screen_mco; ++i)
8917 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008918 vim_free(ScreenLines2);
8919#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008920 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008921 vim_free(ScreenAttrs);
8922 vim_free(LineOffset);
8923 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008924#ifdef FEAT_WINDOWS
8925 vim_free(TabPageIdxs);
8926#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008927}
8928
8929 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008930screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932 check_for_delay(FALSE);
8933 screenalloc(FALSE); /* allocate screen buffers if size changed */
8934 screenclear2(); /* clear the screen */
8935}
8936
8937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008938screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
8940 int i;
8941
8942 if (starting == NO_SCREEN || ScreenLines == NULL
8943#ifdef FEAT_GUI
8944 || (gui.in_use && gui.starting)
8945#endif
8946 )
8947 return;
8948
8949#ifdef FEAT_GUI
8950 if (!gui.in_use)
8951#endif
8952 screen_attr = -1; /* force setting the Normal colors */
8953 screen_stop_highlight(); /* don't want highlighting here */
8954
8955#ifdef FEAT_CLIPBOARD
8956 /* disable selection without redrawing it */
8957 clip_scroll_selection(9999);
8958#endif
8959
8960 /* blank out ScreenLines */
8961 for (i = 0; i < Rows; ++i)
8962 {
8963 lineclear(LineOffset[i], (int)Columns);
8964 LineWraps[i] = FALSE;
8965 }
8966
8967 if (can_clear(T_CL))
8968 {
8969 out_str(T_CL); /* clear the display */
8970 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008971 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 }
8973 else
8974 {
8975 /* can't clear the screen, mark all chars with invalid attributes */
8976 for (i = 0; i < Rows; ++i)
8977 lineinvalid(LineOffset[i], (int)Columns);
8978 clear_cmdline = TRUE;
8979 }
8980
8981 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8982
8983 win_rest_invalid(firstwin);
8984 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008985#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008986 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 if (must_redraw == CLEAR) /* no need to clear again */
8989 must_redraw = NOT_VALID;
8990 compute_cmdrow();
8991 msg_row = cmdline_row; /* put cursor on last line for messages */
8992 msg_col = 0;
8993 screen_start(); /* don't know where cursor is now */
8994 msg_scrolled = 0; /* can't scroll back */
8995 msg_didany = FALSE;
8996 msg_didout = FALSE;
8997}
8998
8999/*
9000 * Clear one line in ScreenLines.
9001 */
9002 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009003lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
9005 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9006#ifdef FEAT_MBYTE
9007 if (enc_utf8)
9008 (void)vim_memset(ScreenLinesUC + off, 0,
9009 (size_t)width * sizeof(u8char_T));
9010#endif
9011 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9012}
9013
9014/*
9015 * Mark one line in ScreenLines invalid by setting the attributes to an
9016 * invalid value.
9017 */
9018 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009019lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
9021 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9022}
9023
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009024#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025/*
9026 * Copy part of a Screenline for vertically split window "wp".
9027 */
9028 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009029linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
9031 unsigned off_to = LineOffset[to] + wp->w_wincol;
9032 unsigned off_from = LineOffset[from] + wp->w_wincol;
9033
9034 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9035 wp->w_width * sizeof(schar_T));
9036# ifdef FEAT_MBYTE
9037 if (enc_utf8)
9038 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009039 int i;
9040
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9042 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009043 for (i = 0; i < p_mco; ++i)
9044 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9045 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 }
9047 if (enc_dbcs == DBCS_JPNU)
9048 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9049 wp->w_width * sizeof(schar_T));
9050# endif
9051 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9052 wp->w_width * sizeof(sattr_T));
9053}
9054#endif
9055
9056/*
9057 * Return TRUE if clearing with term string "p" would work.
9058 * It can't work when the string is empty or it won't set the right background.
9059 */
9060 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009061can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063 return (*p != NUL && (t_colors <= 1
9064#ifdef FEAT_GUI
9065 || gui.in_use
9066#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009067#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009068 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009069 || (!p_tgc && cterm_normal_bg_color == 0)
9070#else
9071 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009072#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009073 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074}
9075
9076/*
9077 * Reset cursor position. Use whenever cursor was moved because of outputting
9078 * something directly to the screen (shell commands) or a terminal control
9079 * code.
9080 */
9081 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009082screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
9084 screen_cur_row = screen_cur_col = 9999;
9085}
9086
9087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 * Move the cursor to position "row","col" in the screen.
9089 * This tries to find the most efficient way to move, minimizing the number of
9090 * characters sent to the terminal.
9091 */
9092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009093windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009095 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 int i;
9097 int plan;
9098 int cost;
9099 int wouldbe_col;
9100 int noinvcurs;
9101 char_u *bs;
9102 int goto_cost;
9103 int attr;
9104
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009105#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9107
9108#define PLAN_LE 1
9109#define PLAN_CR 2
9110#define PLAN_NL 3
9111#define PLAN_WRITE 4
9112 /* Can't use ScreenLines unless initialized */
9113 if (ScreenLines == NULL)
9114 return;
9115
9116 if (col != screen_cur_col || row != screen_cur_row)
9117 {
9118 /* Check for valid position. */
9119 if (row < 0) /* window without text lines? */
9120 row = 0;
9121 if (row >= screen_Rows)
9122 row = screen_Rows - 1;
9123 if (col >= screen_Columns)
9124 col = screen_Columns - 1;
9125
9126 /* check if no cursor movement is allowed in highlight mode */
9127 if (screen_attr && *T_MS == NUL)
9128 noinvcurs = HIGHL_COST;
9129 else
9130 noinvcurs = 0;
9131 goto_cost = GOTO_COST + noinvcurs;
9132
9133 /*
9134 * Plan how to do the positioning:
9135 * 1. Use CR to move it to column 0, same row.
9136 * 2. Use T_LE to move it a few columns to the left.
9137 * 3. Use NL to move a few lines down, column 0.
9138 * 4. Move a few columns to the right with T_ND or by writing chars.
9139 *
9140 * Don't do this if the cursor went beyond the last column, the cursor
9141 * position is unknown then (some terminals wrap, some don't )
9142 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009143 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 * characters to move the cursor to the right.
9145 */
9146 if (row >= screen_cur_row && screen_cur_col < Columns)
9147 {
9148 /*
9149 * If the cursor is in the same row, bigger col, we can use CR
9150 * or T_LE.
9151 */
9152 bs = NULL; /* init for GCC */
9153 attr = screen_attr;
9154 if (row == screen_cur_row && col < screen_cur_col)
9155 {
9156 /* "le" is preferred over "bc", because "bc" is obsolete */
9157 if (*T_LE)
9158 bs = T_LE; /* "cursor left" */
9159 else
9160 bs = T_BC; /* "backspace character (old) */
9161 if (*bs)
9162 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9163 else
9164 cost = 999;
9165 if (col + 1 < cost) /* using CR is less characters */
9166 {
9167 plan = PLAN_CR;
9168 wouldbe_col = 0;
9169 cost = 1; /* CR is just one character */
9170 }
9171 else
9172 {
9173 plan = PLAN_LE;
9174 wouldbe_col = col;
9175 }
9176 if (noinvcurs) /* will stop highlighting */
9177 {
9178 cost += noinvcurs;
9179 attr = 0;
9180 }
9181 }
9182
9183 /*
9184 * If the cursor is above where we want to be, we can use CR LF.
9185 */
9186 else if (row > screen_cur_row)
9187 {
9188 plan = PLAN_NL;
9189 wouldbe_col = 0;
9190 cost = (row - screen_cur_row) * 2; /* CR LF */
9191 if (noinvcurs) /* will stop highlighting */
9192 {
9193 cost += noinvcurs;
9194 attr = 0;
9195 }
9196 }
9197
9198 /*
9199 * If the cursor is in the same row, smaller col, just use write.
9200 */
9201 else
9202 {
9203 plan = PLAN_WRITE;
9204 wouldbe_col = screen_cur_col;
9205 cost = 0;
9206 }
9207
9208 /*
9209 * Check if any characters that need to be written have the
9210 * correct attributes. Also avoid UTF-8 characters.
9211 */
9212 i = col - wouldbe_col;
9213 if (i > 0)
9214 cost += i;
9215 if (cost < goto_cost && i > 0)
9216 {
9217 /*
9218 * Check if the attributes are correct without additionally
9219 * stopping highlighting.
9220 */
9221 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9222 while (i && *p++ == attr)
9223 --i;
9224 if (i != 0)
9225 {
9226 /*
9227 * Try if it works when highlighting is stopped here.
9228 */
9229 if (*--p == 0)
9230 {
9231 cost += noinvcurs;
9232 while (i && *p++ == 0)
9233 --i;
9234 }
9235 if (i != 0)
9236 cost = 999; /* different attributes, don't do it */
9237 }
9238#ifdef FEAT_MBYTE
9239 if (enc_utf8)
9240 {
9241 /* Don't use an UTF-8 char for positioning, it's slow. */
9242 for (i = wouldbe_col; i < col; ++i)
9243 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9244 {
9245 cost = 999;
9246 break;
9247 }
9248 }
9249#endif
9250 }
9251
9252 /*
9253 * We can do it without term_windgoto()!
9254 */
9255 if (cost < goto_cost)
9256 {
9257 if (plan == PLAN_LE)
9258 {
9259 if (noinvcurs)
9260 screen_stop_highlight();
9261 while (screen_cur_col > col)
9262 {
9263 out_str(bs);
9264 --screen_cur_col;
9265 }
9266 }
9267 else if (plan == PLAN_CR)
9268 {
9269 if (noinvcurs)
9270 screen_stop_highlight();
9271 out_char('\r');
9272 screen_cur_col = 0;
9273 }
9274 else if (plan == PLAN_NL)
9275 {
9276 if (noinvcurs)
9277 screen_stop_highlight();
9278 while (screen_cur_row < row)
9279 {
9280 out_char('\n');
9281 ++screen_cur_row;
9282 }
9283 screen_cur_col = 0;
9284 }
9285
9286 i = col - screen_cur_col;
9287 if (i > 0)
9288 {
9289 /*
9290 * Use cursor-right if it's one character only. Avoids
9291 * removing a line of pixels from the last bold char, when
9292 * using the bold trick in the GUI.
9293 */
9294 if (T_ND[0] != NUL && T_ND[1] == NUL)
9295 {
9296 while (i-- > 0)
9297 out_char(*T_ND);
9298 }
9299 else
9300 {
9301 int off;
9302
9303 off = LineOffset[row] + screen_cur_col;
9304 while (i-- > 0)
9305 {
9306 if (ScreenAttrs[off] != screen_attr)
9307 screen_stop_highlight();
9308#ifdef FEAT_MBYTE
9309 out_flush_check();
9310#endif
9311 out_char(ScreenLines[off]);
9312#ifdef FEAT_MBYTE
9313 if (enc_dbcs == DBCS_JPNU
9314 && ScreenLines[off] == 0x8e)
9315 out_char(ScreenLines2[off]);
9316#endif
9317 ++off;
9318 }
9319 }
9320 }
9321 }
9322 }
9323 else
9324 cost = 999;
9325
9326 if (cost >= goto_cost)
9327 {
9328 if (noinvcurs)
9329 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009330 if (row == screen_cur_row && (col > screen_cur_col)
9331 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 term_cursor_right(col - screen_cur_col);
9333 else
9334 term_windgoto(row, col);
9335 }
9336 screen_cur_row = row;
9337 screen_cur_col = col;
9338 }
9339}
9340
9341/*
9342 * Set cursor to its position in the current window.
9343 */
9344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009345setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 if (redrawing())
9348 {
9349 validate_cursor();
9350 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9351 W_WINCOL(curwin) + (
9352#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009353 /* With 'rightleft' set and the cursor on a double-wide
9354 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9356# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009357 (has_mbyte
9358 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9359 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360# endif
9361 1)) :
9362#endif
9363 curwin->w_wcol));
9364 }
9365}
9366
9367
9368/*
9369 * insert 'line_count' lines at 'row' in window 'wp'
9370 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9371 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9372 * scrolling.
9373 * Returns FAIL if the lines are not inserted, OK for success.
9374 */
9375 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009376win_ins_lines(
9377 win_T *wp,
9378 int row,
9379 int line_count,
9380 int invalid,
9381 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 int did_delete;
9384 int nextrow;
9385 int lastrow;
9386 int retval;
9387
9388 if (invalid)
9389 wp->w_lines_valid = 0;
9390
9391 if (wp->w_height < 5)
9392 return FAIL;
9393
9394 if (line_count > wp->w_height - row)
9395 line_count = wp->w_height - row;
9396
9397 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9398 if (retval != MAYBE)
9399 return retval;
9400
9401 /*
9402 * If there is a next window or a status line, we first try to delete the
9403 * lines at the bottom to avoid messing what is after the window.
9404 * If this fails and there are following windows, don't do anything to avoid
9405 * messing up those windows, better just redraw.
9406 */
9407 did_delete = FALSE;
9408#ifdef FEAT_WINDOWS
9409 if (wp->w_next != NULL || wp->w_status_height)
9410 {
9411 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9412 line_count, (int)Rows, FALSE, NULL) == OK)
9413 did_delete = TRUE;
9414 else if (wp->w_next)
9415 return FAIL;
9416 }
9417#endif
9418 /*
9419 * if no lines deleted, blank the lines that will end up below the window
9420 */
9421 if (!did_delete)
9422 {
9423#ifdef FEAT_WINDOWS
9424 wp->w_redr_status = TRUE;
9425#endif
9426 redraw_cmdline = TRUE;
9427 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9428 lastrow = nextrow + line_count;
9429 if (lastrow > Rows)
9430 lastrow = Rows;
9431 screen_fill(nextrow - line_count, lastrow - line_count,
9432 W_WINCOL(wp), (int)W_ENDCOL(wp),
9433 ' ', ' ', 0);
9434 }
9435
9436 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9437 == FAIL)
9438 {
9439 /* deletion will have messed up other windows */
9440 if (did_delete)
9441 {
9442#ifdef FEAT_WINDOWS
9443 wp->w_redr_status = TRUE;
9444#endif
9445 win_rest_invalid(W_NEXT(wp));
9446 }
9447 return FAIL;
9448 }
9449
9450 return OK;
9451}
9452
9453/*
9454 * delete "line_count" window lines at "row" in window "wp"
9455 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9456 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9457 * scrolling
9458 * Return OK for success, FAIL if the lines are not deleted.
9459 */
9460 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009461win_del_lines(
9462 win_T *wp,
9463 int row,
9464 int line_count,
9465 int invalid,
9466 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
9468 int retval;
9469
9470 if (invalid)
9471 wp->w_lines_valid = 0;
9472
9473 if (line_count > wp->w_height - row)
9474 line_count = wp->w_height - row;
9475
9476 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9477 if (retval != MAYBE)
9478 return retval;
9479
9480 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9481 (int)Rows, FALSE, NULL) == FAIL)
9482 return FAIL;
9483
9484#ifdef FEAT_WINDOWS
9485 /*
9486 * If there are windows or status lines below, try to put them at the
9487 * correct place. If we can't do that, they have to be redrawn.
9488 */
9489 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9490 {
9491 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9492 line_count, (int)Rows, NULL) == FAIL)
9493 {
9494 wp->w_redr_status = TRUE;
9495 win_rest_invalid(wp->w_next);
9496 }
9497 }
9498 /*
9499 * If this is the last window and there is no status line, redraw the
9500 * command line later.
9501 */
9502 else
9503#endif
9504 redraw_cmdline = TRUE;
9505 return OK;
9506}
9507
9508/*
9509 * Common code for win_ins_lines() and win_del_lines().
9510 * Returns OK or FAIL when the work has been done.
9511 * Returns MAYBE when not finished yet.
9512 */
9513 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009514win_do_lines(
9515 win_T *wp,
9516 int row,
9517 int line_count,
9518 int mayclear,
9519 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 int retval;
9522
9523 if (!redrawing() || line_count <= 0)
9524 return FAIL;
9525
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009526 /* When inserting lines would result in loss of command output, just redraw
9527 * the lines. */
9528 if (no_win_do_lines_ins && !del)
9529 return FAIL;
9530
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 /* only a few lines left: redraw is faster */
9532 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009533#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 && wp->w_width == Columns
9535#endif
9536 )
9537 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009538 if (!no_win_do_lines_ins)
9539 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 return FAIL;
9541 }
9542
9543 /*
9544 * Delete all remaining lines
9545 */
9546 if (row + line_count >= wp->w_height)
9547 {
9548 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9549 W_WINCOL(wp), (int)W_ENDCOL(wp),
9550 ' ', ' ', 0);
9551 return OK;
9552 }
9553
9554 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009555 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009557 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009559 if (!no_win_do_lines_ins)
9560 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561
9562 /*
9563 * If the terminal can set a scroll region, use that.
9564 * Always do this in a vertically split window. This will redraw from
9565 * ScreenLines[] when t_CV isn't defined. That's faster than using
9566 * win_line().
9567 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009568 * a character in the lower right corner of the scroll region may cause a
9569 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 */
9571 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009572#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 || W_WIDTH(wp) != Columns
9574#endif
9575 )
9576 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009577#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9579#endif
9580 scroll_region_set(wp, row);
9581 if (del)
9582 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9583 wp->w_height - row, FALSE, wp);
9584 else
9585 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9586 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009587#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9589#endif
9590 scroll_region_reset();
9591 return retval;
9592 }
9593
9594#ifdef FEAT_WINDOWS
9595 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9596 return FAIL;
9597#endif
9598
9599 return MAYBE;
9600}
9601
9602/*
9603 * window 'wp' and everything after it is messed up, mark it for redraw
9604 */
9605 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009606win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607{
9608#ifdef FEAT_WINDOWS
9609 while (wp != NULL)
9610#else
9611 if (wp != NULL)
9612#endif
9613 {
9614 redraw_win_later(wp, NOT_VALID);
9615#ifdef FEAT_WINDOWS
9616 wp->w_redr_status = TRUE;
9617 wp = wp->w_next;
9618#endif
9619 }
9620 redraw_cmdline = TRUE;
9621}
9622
9623/*
9624 * The rest of the routines in this file perform screen manipulations. The
9625 * given operation is performed physically on the screen. The corresponding
9626 * change is also made to the internal screen image. In this way, the editor
9627 * anticipates the effect of editing changes on the appearance of the screen.
9628 * That way, when we call screenupdate a complete redraw isn't usually
9629 * necessary. Another advantage is that we can keep adding code to anticipate
9630 * screen changes, and in the meantime, everything still works.
9631 */
9632
9633/*
9634 * types for inserting or deleting lines
9635 */
9636#define USE_T_CAL 1
9637#define USE_T_CDL 2
9638#define USE_T_AL 3
9639#define USE_T_CE 4
9640#define USE_T_DL 5
9641#define USE_T_SR 6
9642#define USE_NL 7
9643#define USE_T_CD 8
9644#define USE_REDRAW 9
9645
9646/*
9647 * insert lines on the screen and update ScreenLines[]
9648 * 'end' is the line after the scrolled part. Normally it is Rows.
9649 * When scrolling region used 'off' is the offset from the top for the region.
9650 * 'row' and 'end' are relative to the start of the region.
9651 *
9652 * return FAIL for failure, OK for success.
9653 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009654 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009655screen_ins_lines(
9656 int off,
9657 int row,
9658 int line_count,
9659 int end,
9660 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662 int i;
9663 int j;
9664 unsigned temp;
9665 int cursor_row;
9666 int type;
9667 int result_empty;
9668 int can_ce = can_clear(T_CE);
9669
9670 /*
9671 * FAIL if
9672 * - there is no valid screen
9673 * - the screen has to be redrawn completely
9674 * - the line count is less than one
9675 * - the line count is more than 'ttyscroll'
9676 */
9677 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9678 return FAIL;
9679
9680 /*
9681 * There are seven ways to insert lines:
9682 * 0. When in a vertically split window and t_CV isn't set, redraw the
9683 * characters from ScreenLines[].
9684 * 1. Use T_CD (clear to end of display) if it exists and the result of
9685 * the insert is just empty lines
9686 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9687 * present or line_count > 1. It looks better if we do all the inserts
9688 * at once.
9689 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9690 * insert is just empty lines and T_CE is not present or line_count >
9691 * 1.
9692 * 4. Use T_AL (insert line) if it exists.
9693 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9694 * just empty lines.
9695 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9696 * just empty lines.
9697 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9698 * the 'da' flag is not set or we have clear line capability.
9699 * 8. redraw the characters from ScreenLines[].
9700 *
9701 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9702 * the scrollbar for the window. It does have insert line, use that if it
9703 * exists.
9704 */
9705 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009706#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9708 type = USE_REDRAW;
9709 else
9710#endif
9711 if (can_clear(T_CD) && result_empty)
9712 type = USE_T_CD;
9713 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9714 type = USE_T_CAL;
9715 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9716 type = USE_T_CDL;
9717 else if (*T_AL != NUL)
9718 type = USE_T_AL;
9719 else if (can_ce && result_empty)
9720 type = USE_T_CE;
9721 else if (*T_DL != NUL && result_empty)
9722 type = USE_T_DL;
9723 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9724 type = USE_T_SR;
9725 else
9726 return FAIL;
9727
9728 /*
9729 * For clearing the lines screen_del_lines() is used. This will also take
9730 * care of t_db if necessary.
9731 */
9732 if (type == USE_T_CD || type == USE_T_CDL ||
9733 type == USE_T_CE || type == USE_T_DL)
9734 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9735
9736 /*
9737 * If text is retained below the screen, first clear or delete as many
9738 * lines at the bottom of the window as are about to be inserted so that
9739 * the deleted lines won't later surface during a screen_del_lines.
9740 */
9741 if (*T_DB)
9742 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9743
9744#ifdef FEAT_CLIPBOARD
9745 /* Remove a modeless selection when inserting lines halfway the screen
9746 * or not the full width of the screen. */
9747 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009748# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 || (wp != NULL && wp->w_width != Columns)
9750# endif
9751 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009752 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 else
9754 clip_scroll_selection(-line_count);
9755#endif
9756
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757#ifdef FEAT_GUI
9758 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9759 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009760 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761#endif
9762
9763 if (*T_CCS != NUL) /* cursor relative to region */
9764 cursor_row = row;
9765 else
9766 cursor_row = row + off;
9767
9768 /*
9769 * Shift LineOffset[] line_count down to reflect the inserted lines.
9770 * Clear the inserted lines in ScreenLines[].
9771 */
9772 row += off;
9773 end += off;
9774 for (i = 0; i < line_count; ++i)
9775 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009776#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 if (wp != NULL && wp->w_width != Columns)
9778 {
9779 /* need to copy part of a line */
9780 j = end - 1 - i;
9781 while ((j -= line_count) >= row)
9782 linecopy(j + line_count, j, wp);
9783 j += line_count;
9784 if (can_clear((char_u *)" "))
9785 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9786 else
9787 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9788 LineWraps[j] = FALSE;
9789 }
9790 else
9791#endif
9792 {
9793 j = end - 1 - i;
9794 temp = LineOffset[j];
9795 while ((j -= line_count) >= row)
9796 {
9797 LineOffset[j + line_count] = LineOffset[j];
9798 LineWraps[j + line_count] = LineWraps[j];
9799 }
9800 LineOffset[j + line_count] = temp;
9801 LineWraps[j + line_count] = FALSE;
9802 if (can_clear((char_u *)" "))
9803 lineclear(temp, (int)Columns);
9804 else
9805 lineinvalid(temp, (int)Columns);
9806 }
9807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808
9809 screen_stop_highlight();
9810 windgoto(cursor_row, 0);
9811
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009812#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 /* redraw the characters */
9814 if (type == USE_REDRAW)
9815 redraw_block(row, end, wp);
9816 else
9817#endif
9818 if (type == USE_T_CAL)
9819 {
9820 term_append_lines(line_count);
9821 screen_start(); /* don't know where cursor is now */
9822 }
9823 else
9824 {
9825 for (i = 0; i < line_count; i++)
9826 {
9827 if (type == USE_T_AL)
9828 {
9829 if (i && cursor_row != 0)
9830 windgoto(cursor_row, 0);
9831 out_str(T_AL);
9832 }
9833 else /* type == USE_T_SR */
9834 out_str(T_SR);
9835 screen_start(); /* don't know where cursor is now */
9836 }
9837 }
9838
9839 /*
9840 * With scroll-reverse and 'da' flag set we need to clear the lines that
9841 * have been scrolled down into the region.
9842 */
9843 if (type == USE_T_SR && *T_DA)
9844 {
9845 for (i = 0; i < line_count; ++i)
9846 {
9847 windgoto(off + i, 0);
9848 out_str(T_CE);
9849 screen_start(); /* don't know where cursor is now */
9850 }
9851 }
9852
9853#ifdef FEAT_GUI
9854 gui_can_update_cursor();
9855 if (gui.in_use)
9856 out_flush(); /* always flush after a scroll */
9857#endif
9858 return OK;
9859}
9860
9861/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009862 * Delete lines on the screen and update ScreenLines[].
9863 * "end" is the line after the scrolled part. Normally it is Rows.
9864 * When scrolling region used "off" is the offset from the top for the region.
9865 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 *
9867 * Return OK for success, FAIL if the lines are not deleted.
9868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009870screen_del_lines(
9871 int off,
9872 int row,
9873 int line_count,
9874 int end,
9875 int force, /* even when line_count > p_ttyscroll */
9876 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877{
9878 int j;
9879 int i;
9880 unsigned temp;
9881 int cursor_row;
9882 int cursor_end;
9883 int result_empty; /* result is empty until end of region */
9884 int can_delete; /* deleting line codes can be used */
9885 int type;
9886
9887 /*
9888 * FAIL if
9889 * - there is no valid screen
9890 * - the screen has to be redrawn completely
9891 * - the line count is less than one
9892 * - the line count is more than 'ttyscroll'
9893 */
9894 if (!screen_valid(TRUE) || line_count <= 0 ||
9895 (!force && line_count > p_ttyscroll))
9896 return FAIL;
9897
9898 /*
9899 * Check if the rest of the current region will become empty.
9900 */
9901 result_empty = row + line_count >= end;
9902
9903 /*
9904 * We can delete lines only when 'db' flag not set or when 'ce' option
9905 * available.
9906 */
9907 can_delete = (*T_DB == NUL || can_clear(T_CE));
9908
9909 /*
9910 * There are six ways to delete lines:
9911 * 0. When in a vertically split window and t_CV isn't set, redraw the
9912 * characters from ScreenLines[].
9913 * 1. Use T_CD if it exists and the result is empty.
9914 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9915 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9916 * none of the other ways work.
9917 * 4. Use T_CE (erase line) if the result is empty.
9918 * 5. Use T_DL (delete line) if it exists.
9919 * 6. redraw the characters from ScreenLines[].
9920 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009921#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9923 type = USE_REDRAW;
9924 else
9925#endif
9926 if (can_clear(T_CD) && result_empty)
9927 type = USE_T_CD;
9928#if defined(__BEOS__) && defined(BEOS_DR8)
9929 /*
9930 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9931 * its internal termcap... this works okay for tests which test *T_DB !=
9932 * NUL. It has the disadvantage that the user cannot use any :set t_*
9933 * command to get T_DB (back) to empty_option, only :set term=... will do
9934 * the trick...
9935 * Anyway, this hack will hopefully go away with the next OS release.
9936 * (Olaf Seibert)
9937 */
9938 else if (row == 0 && T_DB == empty_option
9939 && (line_count == 1 || *T_CDL == NUL))
9940#else
9941 else if (row == 0 && (
9942#ifndef AMIGA
9943 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9944 * up, so use delete-line command */
9945 line_count == 1 ||
9946#endif
9947 *T_CDL == NUL))
9948#endif
9949 type = USE_NL;
9950 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9951 type = USE_T_CDL;
9952 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009953#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 && (wp == NULL || wp->w_width == Columns)
9955#endif
9956 )
9957 type = USE_T_CE;
9958 else if (*T_DL != NUL && can_delete)
9959 type = USE_T_DL;
9960 else if (*T_CDL != NUL && can_delete)
9961 type = USE_T_CDL;
9962 else
9963 return FAIL;
9964
9965#ifdef FEAT_CLIPBOARD
9966 /* Remove a modeless selection when deleting lines halfway the screen or
9967 * not the full width of the screen. */
9968 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009969# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 || (wp != NULL && wp->w_width != Columns)
9971# endif
9972 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009973 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 else
9975 clip_scroll_selection(line_count);
9976#endif
9977
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978#ifdef FEAT_GUI
9979 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9980 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009981 gui_dont_update_cursor(gui.cursor_row >= row + off
9982 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983#endif
9984
9985 if (*T_CCS != NUL) /* cursor relative to region */
9986 {
9987 cursor_row = row;
9988 cursor_end = end;
9989 }
9990 else
9991 {
9992 cursor_row = row + off;
9993 cursor_end = end + off;
9994 }
9995
9996 /*
9997 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9998 * Clear the inserted lines in ScreenLines[].
9999 */
10000 row += off;
10001 end += off;
10002 for (i = 0; i < line_count; ++i)
10003 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010004#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 if (wp != NULL && wp->w_width != Columns)
10006 {
10007 /* need to copy part of a line */
10008 j = row + i;
10009 while ((j += line_count) <= end - 1)
10010 linecopy(j - line_count, j, wp);
10011 j -= line_count;
10012 if (can_clear((char_u *)" "))
10013 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10014 else
10015 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10016 LineWraps[j] = FALSE;
10017 }
10018 else
10019#endif
10020 {
10021 /* whole width, moving the line pointers is faster */
10022 j = row + i;
10023 temp = LineOffset[j];
10024 while ((j += line_count) <= end - 1)
10025 {
10026 LineOffset[j - line_count] = LineOffset[j];
10027 LineWraps[j - line_count] = LineWraps[j];
10028 }
10029 LineOffset[j - line_count] = temp;
10030 LineWraps[j - line_count] = FALSE;
10031 if (can_clear((char_u *)" "))
10032 lineclear(temp, (int)Columns);
10033 else
10034 lineinvalid(temp, (int)Columns);
10035 }
10036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037
10038 screen_stop_highlight();
10039
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010040#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 /* redraw the characters */
10042 if (type == USE_REDRAW)
10043 redraw_block(row, end, wp);
10044 else
10045#endif
10046 if (type == USE_T_CD) /* delete the lines */
10047 {
10048 windgoto(cursor_row, 0);
10049 out_str(T_CD);
10050 screen_start(); /* don't know where cursor is now */
10051 }
10052 else if (type == USE_T_CDL)
10053 {
10054 windgoto(cursor_row, 0);
10055 term_delete_lines(line_count);
10056 screen_start(); /* don't know where cursor is now */
10057 }
10058 /*
10059 * Deleting lines at top of the screen or scroll region: Just scroll
10060 * the whole screen (scroll region) up by outputting newlines on the
10061 * last line.
10062 */
10063 else if (type == USE_NL)
10064 {
10065 windgoto(cursor_end - 1, 0);
10066 for (i = line_count; --i >= 0; )
10067 out_char('\n'); /* cursor will remain on same line */
10068 }
10069 else
10070 {
10071 for (i = line_count; --i >= 0; )
10072 {
10073 if (type == USE_T_DL)
10074 {
10075 windgoto(cursor_row, 0);
10076 out_str(T_DL); /* delete a line */
10077 }
10078 else /* type == USE_T_CE */
10079 {
10080 windgoto(cursor_row + i, 0);
10081 out_str(T_CE); /* erase a line */
10082 }
10083 screen_start(); /* don't know where cursor is now */
10084 }
10085 }
10086
10087 /*
10088 * If the 'db' flag is set, we need to clear the lines that have been
10089 * scrolled up at the bottom of the region.
10090 */
10091 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10092 {
10093 for (i = line_count; i > 0; --i)
10094 {
10095 windgoto(cursor_end - i, 0);
10096 out_str(T_CE); /* erase a line */
10097 screen_start(); /* don't know where cursor is now */
10098 }
10099 }
10100
10101#ifdef FEAT_GUI
10102 gui_can_update_cursor();
10103 if (gui.in_use)
10104 out_flush(); /* always flush after a scroll */
10105#endif
10106
10107 return OK;
10108}
10109
10110/*
10111 * show the current mode and ruler
10112 *
10113 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10114 * If clear_cmdline is FALSE there may be a message there that needs to be
10115 * cleared only if a mode is shown.
10116 * Return the length of the message (0 if no message).
10117 */
10118 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010119showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
10121 int need_clear;
10122 int length = 0;
10123 int do_mode;
10124 int attr;
10125 int nwr_save;
10126#ifdef FEAT_INS_EXPAND
10127 int sub_attr;
10128#endif
10129
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010130 do_mode = ((p_smd && msg_silent == 0)
10131 && ((State & INSERT)
10132 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010133 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 if (do_mode || Recording)
10135 {
10136 /*
10137 * Don't show mode right now, when not redrawing or inside a mapping.
10138 * Call char_avail() only when we are going to show something, because
10139 * it takes a bit of time.
10140 */
10141 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10142 {
10143 redraw_cmdline = TRUE; /* show mode later */
10144 return 0;
10145 }
10146
10147 nwr_save = need_wait_return;
10148
10149 /* wait a bit before overwriting an important message */
10150 check_for_delay(FALSE);
10151
10152 /* if the cmdline is more than one line high, erase top lines */
10153 need_clear = clear_cmdline;
10154 if (clear_cmdline && cmdline_row < Rows - 1)
10155 msg_clr_cmdline(); /* will reset clear_cmdline */
10156
10157 /* Position on the last line in the window, column 0 */
10158 msg_pos_mode();
10159 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010160 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 if (do_mode)
10162 {
10163 MSG_PUTS_ATTR("--", attr);
10164#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010165 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010166# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010167 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010168# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010169 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010170# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010171 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010172# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 MSG_PUTS_ATTR(" IM", attr);
10174# else
10175 MSG_PUTS_ATTR(" XIM", attr);
10176# endif
10177#endif
10178#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10179 if (gui.in_use)
10180 {
10181 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010182 {
10183 /* HANGUL */
10184 if (enc_utf8)
10185 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10186 else
10187 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 }
10190#endif
10191#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010192 /* CTRL-X in Insert mode */
10193 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 {
10195 /* These messages can get long, avoid a wrap in a narrow
10196 * window. Prefer showing edit_submode_extra. */
10197 length = (Rows - msg_row) * Columns - 3;
10198 if (edit_submode_extra != NULL)
10199 length -= vim_strsize(edit_submode_extra);
10200 if (length > 0)
10201 {
10202 if (edit_submode_pre != NULL)
10203 length -= vim_strsize(edit_submode_pre);
10204 if (length - vim_strsize(edit_submode) > 0)
10205 {
10206 if (edit_submode_pre != NULL)
10207 msg_puts_attr(edit_submode_pre, attr);
10208 msg_puts_attr(edit_submode, attr);
10209 }
10210 if (edit_submode_extra != NULL)
10211 {
10212 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10213 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010214 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 else
10216 sub_attr = attr;
10217 msg_puts_attr(edit_submode_extra, sub_attr);
10218 }
10219 }
10220 length = 0;
10221 }
10222 else
10223#endif
10224 {
10225#ifdef FEAT_VREPLACE
10226 if (State & VREPLACE_FLAG)
10227 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10228 else
10229#endif
10230 if (State & REPLACE_FLAG)
10231 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10232 else if (State & INSERT)
10233 {
10234#ifdef FEAT_RIGHTLEFT
10235 if (p_ri)
10236 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10237#endif
10238 MSG_PUTS_ATTR(_(" INSERT"), attr);
10239 }
10240 else if (restart_edit == 'I')
10241 MSG_PUTS_ATTR(_(" (insert)"), attr);
10242 else if (restart_edit == 'R')
10243 MSG_PUTS_ATTR(_(" (replace)"), attr);
10244 else if (restart_edit == 'V')
10245 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10246#ifdef FEAT_RIGHTLEFT
10247 if (p_hkmap)
10248 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10249# ifdef FEAT_FKMAP
10250 if (p_fkmap)
10251 MSG_PUTS_ATTR(farsi_text_5, attr);
10252# endif
10253#endif
10254#ifdef FEAT_KEYMAP
10255 if (State & LANGMAP)
10256 {
10257# ifdef FEAT_ARABIC
10258 if (curwin->w_p_arab)
10259 MSG_PUTS_ATTR(_(" Arabic"), attr);
10260 else
10261# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010262 if (get_keymap_str(curwin, (char_u *)" (%s)",
10263 NameBuff, MAXPATHL))
10264 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 }
10266#endif
10267 if ((State & INSERT) && p_paste)
10268 MSG_PUTS_ATTR(_(" (paste)"), attr);
10269
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 if (VIsual_active)
10271 {
10272 char *p;
10273
10274 /* Don't concatenate separate words to avoid translation
10275 * problems. */
10276 switch ((VIsual_select ? 4 : 0)
10277 + (VIsual_mode == Ctrl_V) * 2
10278 + (VIsual_mode == 'V'))
10279 {
10280 case 0: p = N_(" VISUAL"); break;
10281 case 1: p = N_(" VISUAL LINE"); break;
10282 case 2: p = N_(" VISUAL BLOCK"); break;
10283 case 4: p = N_(" SELECT"); break;
10284 case 5: p = N_(" SELECT LINE"); break;
10285 default: p = N_(" SELECT BLOCK"); break;
10286 }
10287 MSG_PUTS_ATTR(_(p), attr);
10288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 MSG_PUTS_ATTR(" --", attr);
10290 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010291
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 need_clear = TRUE;
10293 }
10294 if (Recording
10295#ifdef FEAT_INS_EXPAND
10296 && edit_submode == NULL /* otherwise it gets too long */
10297#endif
10298 )
10299 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010300 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 need_clear = TRUE;
10302 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010303
10304 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 if (need_clear || clear_cmdline)
10306 msg_clr_eos();
10307 msg_didout = FALSE; /* overwrite this message */
10308 length = msg_col;
10309 msg_col = 0;
10310 need_wait_return = nwr_save; /* never ask for hit-return for this */
10311 }
10312 else if (clear_cmdline && msg_silent == 0)
10313 /* Clear the whole command line. Will reset "clear_cmdline". */
10314 msg_clr_cmdline();
10315
10316#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 /* In Visual mode the size of the selected area must be redrawn. */
10318 if (VIsual_active)
10319 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320
10321 /* If the last window has no status line, the ruler is after the mode
10322 * message and must be redrawn */
10323 if (redrawing()
10324# ifdef FEAT_WINDOWS
10325 && lastwin->w_status_height == 0
10326# endif
10327 )
10328 win_redr_ruler(lastwin, TRUE);
10329#endif
10330 redraw_cmdline = FALSE;
10331 clear_cmdline = FALSE;
10332
10333 return length;
10334}
10335
10336/*
10337 * Position for a mode message.
10338 */
10339 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010340msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341{
10342 msg_col = 0;
10343 msg_row = Rows - 1;
10344}
10345
10346/*
10347 * Delete mode message. Used when ESC is typed which is expected to end
10348 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010349 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 */
10351 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010352unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
10354 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010355 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 */
10357 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10358 redraw_cmdline = TRUE; /* delete mode later */
10359 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010360 clearmode();
10361}
10362
10363/*
10364 * Clear the mode message.
10365 */
10366 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010367clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010368{
10369 msg_pos_mode();
10370 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010371 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010372 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373}
10374
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010375 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010376recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010377{
10378 MSG_PUTS_ATTR(_("recording"), attr);
10379 if (!shortmess(SHM_RECORDING))
10380 {
10381 char_u s[4];
10382 sprintf((char *)s, " @%c", Recording);
10383 MSG_PUTS_ATTR(s, attr);
10384 }
10385}
10386
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010387#if defined(FEAT_WINDOWS)
10388/*
10389 * Draw the tab pages line at the top of the Vim window.
10390 */
10391 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010392draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010393{
10394 int tabcount = 0;
10395 tabpage_T *tp;
10396 int tabwidth;
10397 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010398 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010399 int attr;
10400 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010401 win_T *cwp;
10402 int wincount;
10403 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010404 int c;
10405 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010406 int attr_sel = HL_ATTR(HLF_TPS);
10407 int attr_nosel = HL_ATTR(HLF_TP);
10408 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010409 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010410 int room;
10411 int use_sep_chars = (t_colors < 8
10412#ifdef FEAT_GUI
10413 && !gui.in_use
10414#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010415#ifdef FEAT_TERMGUICOLORS
10416 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010417#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010418 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010419
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010420 if (ScreenLines == NULL)
10421 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010422 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010423
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010424#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010425 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010426 if (gui_use_tabline())
10427 {
10428 gui_update_tabline();
10429 return;
10430 }
10431#endif
10432
10433 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010434 return;
10435
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010436#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010437
10438 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10439 for (scol = 0; scol < Columns; ++scol)
10440 TabPageIdxs[scol] = 0;
10441
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010442 /* Use the 'tabline' option if it's set. */
10443 if (*p_tal != NUL)
10444 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010445 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010446
10447 /* Check for an error. If there is one we would loop in redrawing the
10448 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010449 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010450 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010451 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010452 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010453 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010454 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010455 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010456 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010457#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010458 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010459 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010460 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010461
Bram Moolenaar238a5642006-02-21 22:12:05 +000010462 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10463 if (tabwidth < 6)
10464 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010465
Bram Moolenaar238a5642006-02-21 22:12:05 +000010466 attr = attr_nosel;
10467 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010468 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010469 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10470 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010471 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010472 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010473
Bram Moolenaar238a5642006-02-21 22:12:05 +000010474 if (tp->tp_topframe == topframe)
10475 attr = attr_sel;
10476 if (use_sep_chars && col > 0)
10477 screen_putchar('|', 0, col++, attr);
10478
10479 if (tp->tp_topframe != topframe)
10480 attr = attr_nosel;
10481
10482 screen_putchar(' ', 0, col++, attr);
10483
10484 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010485 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010486 cwp = curwin;
10487 wp = firstwin;
10488 }
10489 else
10490 {
10491 cwp = tp->tp_curwin;
10492 wp = tp->tp_firstwin;
10493 }
10494
10495 modified = FALSE;
10496 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10497 if (bufIsChanged(wp->w_buffer))
10498 modified = TRUE;
10499 if (modified || wincount > 1)
10500 {
10501 if (wincount > 1)
10502 {
10503 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010504 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010505 if (col + len >= Columns - 3)
10506 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010507 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010508#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010509 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010510#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010511 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010512#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010513 );
10514 col += len;
10515 }
10516 if (modified)
10517 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10518 screen_putchar(' ', 0, col++, attr);
10519 }
10520
10521 room = scol - col + tabwidth - 1;
10522 if (room > 0)
10523 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010524 /* Get buffer name in NameBuff[] */
10525 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010526 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010527 len = vim_strsize(NameBuff);
10528 p = NameBuff;
10529#ifdef FEAT_MBYTE
10530 if (has_mbyte)
10531 while (len > room)
10532 {
10533 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010534 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010535 }
10536 else
10537#endif
10538 if (len > room)
10539 {
10540 p += len - room;
10541 len = room;
10542 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010543 if (len > Columns - col - 1)
10544 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010545
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010546 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010547 col += len;
10548 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010549 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010550
10551 /* Store the tab page number in TabPageIdxs[], so that
10552 * jump_to_mouse() knows where each one is. */
10553 ++tabcount;
10554 while (scol < col)
10555 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010556 }
10557
Bram Moolenaar238a5642006-02-21 22:12:05 +000010558 if (use_sep_chars)
10559 c = '_';
10560 else
10561 c = ' ';
10562 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010563
10564 /* Put an "X" for closing the current tab if there are several. */
10565 if (first_tabpage->tp_next != NULL)
10566 {
10567 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10568 TabPageIdxs[Columns - 1] = -999;
10569 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010570 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010571
10572 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10573 * set. */
10574 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010575}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010576
10577/*
10578 * Get buffer name for "buf" into NameBuff[].
10579 * Takes care of special buffer names and translates special characters.
10580 */
10581 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010582get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010583{
10584 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010585 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010586 else
10587 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10588 trans_characters(NameBuff, MAXPATHL);
10589}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010590#endif
10591
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10593/*
10594 * Get the character to use in a status line. Get its attributes in "*attr".
10595 */
10596 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010597fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598{
10599 int fill;
10600 if (is_curwin)
10601 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010602 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 fill = fill_stl;
10604 }
10605 else
10606 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010607 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 fill = fill_stlnc;
10609 }
10610 /* Use fill when there is highlighting, and highlighting of current
10611 * window differs, or the fillchars differ, or this is not the
10612 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010613 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010614 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 || (fill_stl != fill_stlnc)))
10616 return fill;
10617 if (is_curwin)
10618 return '^';
10619 return '=';
10620}
10621#endif
10622
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010623#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624/*
10625 * Get the character to use in a separator between vertically split windows.
10626 * Get its attributes in "*attr".
10627 */
10628 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010629fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010631 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 if (*attr == 0 && fill_vert == ' ')
10633 return '|';
10634 else
10635 return fill_vert;
10636}
10637#endif
10638
10639/*
10640 * Return TRUE if redrawing should currently be done.
10641 */
10642 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010643redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010645#ifdef FEAT_EVAL
10646 if (disable_redraw_for_testing)
10647 return 0;
10648 else
10649#endif
10650 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10652}
10653
10654/*
10655 * Return TRUE if printing messages should currently be done.
10656 */
10657 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010658messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659{
10660 return (!(p_lz && char_avail() && !KeyTyped));
10661}
10662
10663/*
10664 * Show current status info in ruler and various other places
10665 * If always is FALSE, only show ruler if position has changed.
10666 */
10667 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010668showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669{
10670 if (!always && !redrawing())
10671 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010672#ifdef FEAT_INS_EXPAND
10673 if (pum_visible())
10674 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010675# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010676 /* Don't redraw right now, do it later. */
10677 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010678# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010679 return;
10680 }
10681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010683 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010684 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010685 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 else
10688#endif
10689#ifdef FEAT_CMDL_INFO
10690 win_redr_ruler(curwin, always);
10691#endif
10692
10693#ifdef FEAT_TITLE
10694 if (need_maketitle
10695# ifdef FEAT_STL_OPT
10696 || (p_icon && (stl_syntax & STL_IN_ICON))
10697 || (p_title && (stl_syntax & STL_IN_TITLE))
10698# endif
10699 )
10700 maketitle();
10701#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010702#ifdef FEAT_WINDOWS
10703 /* Redraw the tab pages line if needed. */
10704 if (redraw_tabline)
10705 draw_tabline();
10706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707}
10708
10709#ifdef FEAT_CMDL_INFO
10710 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010711win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010713#define RULER_BUF_LEN 70
10714 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 int row;
10716 int fillchar;
10717 int attr;
10718 int empty_line = FALSE;
10719 colnr_T virtcol;
10720 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010721 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010723#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 int this_ru_col;
10725 int off = 0;
10726 int width = Columns;
10727# define WITH_OFF(x) x
10728# define WITH_WIDTH(x) x
10729#else
10730# define WITH_OFF(x) 0
10731# define WITH_WIDTH(x) Columns
10732# define this_ru_col ru_col
10733#endif
10734
10735 /* If 'ruler' off or redrawing disabled, don't do anything */
10736 if (!p_ru)
10737 return;
10738
10739 /*
10740 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10741 * after deleting lines, before cursor.lnum is corrected.
10742 */
10743 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10744 return;
10745
10746#ifdef FEAT_INS_EXPAND
10747 /* Don't draw the ruler while doing insert-completion, it might overwrite
10748 * the (long) mode message. */
10749# ifdef FEAT_WINDOWS
10750 if (wp == lastwin && lastwin->w_status_height == 0)
10751# endif
10752 if (edit_submode != NULL)
10753 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010754 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10755 if (pum_visible())
10756 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757#endif
10758
10759#ifdef FEAT_STL_OPT
10760 if (*p_ruf)
10761 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010762 int save_called_emsg = called_emsg;
10763
10764 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010766 if (called_emsg)
10767 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010768 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010769 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 return;
10771 }
10772#endif
10773
10774 /*
10775 * Check if not in Insert mode and the line is empty (will show "0-1").
10776 */
10777 if (!(State & INSERT)
10778 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10779 empty_line = TRUE;
10780
10781 /*
10782 * Only draw the ruler when something changed.
10783 */
10784 validate_virtcol_win(wp);
10785 if ( redraw_cmdline
10786 || always
10787 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10788 || wp->w_cursor.col != wp->w_ru_cursor.col
10789 || wp->w_virtcol != wp->w_ru_virtcol
10790#ifdef FEAT_VIRTUALEDIT
10791 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10792#endif
10793 || wp->w_topline != wp->w_ru_topline
10794 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10795#ifdef FEAT_DIFF
10796 || wp->w_topfill != wp->w_ru_topfill
10797#endif
10798 || empty_line != wp->w_ru_empty)
10799 {
10800 cursor_off();
10801#ifdef FEAT_WINDOWS
10802 if (wp->w_status_height)
10803 {
10804 row = W_WINROW(wp) + wp->w_height;
10805 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 off = W_WINCOL(wp);
10807 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 }
10809 else
10810#endif
10811 {
10812 row = Rows - 1;
10813 fillchar = ' ';
10814 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010815#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 width = Columns;
10817 off = 0;
10818#endif
10819 }
10820
10821 /* In list mode virtcol needs to be recomputed */
10822 virtcol = wp->w_virtcol;
10823 if (wp->w_p_list && lcs_tab1 == NUL)
10824 {
10825 wp->w_p_list = FALSE;
10826 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10827 wp->w_p_list = TRUE;
10828 }
10829
10830 /*
10831 * Some sprintfs return the length, some return a pointer.
10832 * To avoid portability problems we use strlen() here.
10833 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010834 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10836 ? 0L
10837 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010838 len = STRLEN(buffer);
10839 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10841 (int)virtcol + 1);
10842
10843 /*
10844 * Add a "50%" if there is room for it.
10845 * On the last line, don't print in the last column (scrolls the
10846 * screen up on some terminals).
10847 */
10848 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010849 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 o = i + vim_strsize(buffer + i + 1);
10851#ifdef FEAT_WINDOWS
10852 if (wp->w_status_height == 0) /* can't use last char of screen */
10853#endif
10854 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010855#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 this_ru_col = ru_col - (Columns - width);
10857 if (this_ru_col < 0)
10858 this_ru_col = 0;
10859#endif
10860 /* Never use more than half the window/screen width, leave the other
10861 * half for the filename. */
10862 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10863 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10864 if (this_ru_col + o < WITH_WIDTH(width))
10865 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010866 /* need at least 3 chars left for get_rel_pos() + NUL */
10867 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
10869#ifdef FEAT_MBYTE
10870 if (has_mbyte)
10871 i += (*mb_char2bytes)(fillchar, buffer + i);
10872 else
10873#endif
10874 buffer[i++] = fillchar;
10875 ++o;
10876 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010877 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 }
10879 /* Truncate at window boundary. */
10880#ifdef FEAT_MBYTE
10881 if (has_mbyte)
10882 {
10883 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010884 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 {
10886 o += (*mb_ptr2cells)(buffer + i);
10887 if (this_ru_col + o > WITH_WIDTH(width))
10888 {
10889 buffer[i] = NUL;
10890 break;
10891 }
10892 }
10893 }
10894 else
10895#endif
10896 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10897 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10898
10899 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10900 i = redraw_cmdline;
10901 screen_fill(row, row + 1,
10902 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10903 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10904 fillchar, fillchar, attr);
10905 /* don't redraw the cmdline because of showing the ruler */
10906 redraw_cmdline = i;
10907 wp->w_ru_cursor = wp->w_cursor;
10908 wp->w_ru_virtcol = wp->w_virtcol;
10909 wp->w_ru_empty = empty_line;
10910 wp->w_ru_topline = wp->w_topline;
10911 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10912#ifdef FEAT_DIFF
10913 wp->w_ru_topfill = wp->w_topfill;
10914#endif
10915 }
10916}
10917#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010918
10919#if defined(FEAT_LINEBREAK) || defined(PROTO)
10920/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010921 * Return the width of the 'number' and 'relativenumber' column.
10922 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010923 * Otherwise it depends on 'numberwidth' and the line count.
10924 */
10925 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010926number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010927{
10928 int n;
10929 linenr_T lnum;
10930
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010931 if (wp->w_p_rnu && !wp->w_p_nu)
10932 /* cursor line shows "0" */
10933 lnum = wp->w_height;
10934 else
10935 /* cursor line shows absolute line number */
10936 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010937
Bram Moolenaar6b314672015-03-20 15:42:10 +010010938 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010939 return wp->w_nrwidth_width;
10940 wp->w_nrwidth_line_count = lnum;
10941
10942 n = 0;
10943 do
10944 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010945 lnum /= 10;
10946 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010947 } while (lnum > 0);
10948
10949 /* 'numberwidth' gives the minimal width plus one */
10950 if (n < wp->w_p_nuw - 1)
10951 n = wp->w_p_nuw - 1;
10952
10953 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010954 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010955 return n;
10956}
10957#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010958
10959/*
10960 * Return the current cursor column. This is the actual position on the
10961 * screen. First column is 0.
10962 */
10963 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010964screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010965{
10966 return screen_cur_col;
10967}
10968
10969/*
10970 * Return the current cursor row. This is the actual position on the screen.
10971 * First row is 0.
10972 */
10973 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010974screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010975{
10976 return screen_cur_row;
10977}