blob: e0e9d3b7e2d3775aeed136b6943b61b75c292470 [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
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static 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 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200153static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void win_rest_invalid(win_T *wp);
155static void msg_pos_mode(void);
156static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Redraw the current window later, with update_screen(type).
186 * Set must_redraw only if not already set to a higher value.
187 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
188 */
189 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100190redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
192 redraw_win_later(curwin, type);
193}
194
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_win_later(
197 win_T *wp,
198 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200200 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {
202 wp->w_redr_type = type;
203 if (type >= NOT_VALID)
204 wp->w_lines_valid = 0;
205 if (must_redraw < type) /* must_redraw is the maximum of all windows */
206 must_redraw = type;
207 }
208}
209
210/*
211 * Force a complete redraw later. Also resets the highlighting. To be used
212 * after executing a shell command that messes up the screen.
213 */
214 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100215redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
217 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000218#ifdef FEAT_GUI
219 if (gui.in_use)
220 /* Use a code that will reset gui.highlight_mask in
221 * gui_stop_highlight(). */
222 screen_attr = HL_ALL + 1;
223 else
224#endif
225 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200226 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227}
228
229/*
230 * Mark all windows to be redrawn later.
231 */
232 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100233redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100247redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 redraw_buf_later(curbuf, type);
250}
251
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 win_T *wp;
256
257 FOR_ALL_WINDOWS(wp)
258 {
259 if (wp->w_buffer == buf)
260 redraw_win_later(wp, type);
261 }
262}
263
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200264 void
265redraw_buf_and_status_later(buf_T *buf, int type)
266{
267 win_T *wp;
268
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200269#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200270 if (wild_menu_showing != 0)
271 /* Don't redraw while the command line completion is displayed, it
272 * would disappear. */
273 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200274#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200275 FOR_ALL_WINDOWS(wp)
276 {
277 if (wp->w_buffer == buf)
278 {
279 redraw_win_later(wp, type);
280 wp->w_redr_status = TRUE;
281 }
282 }
283}
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200286 * Redraw as soon as possible. When the command line is not scrolled redraw
287 * right away and restore what was on the command line.
288 * Return a code indicating what happened.
289 */
290 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100291redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292{
293 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 int r;
296 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200297 schar_T *screenline; /* copy from ScreenLines[] */
298 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299#ifdef FEAT_MBYTE
300 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200301 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304#endif
305
306 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200307 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 return ret;
309
310 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 if (screenline == NULL || screenattr == NULL)
317 ret = 2;
318#ifdef FEAT_MBYTE
319 if (enc_utf8)
320 {
321 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 if (screenlineUC == NULL)
324 ret = 2;
325 for (i = 0; i < p_mco; ++i)
326 {
327 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineC[i] == NULL)
330 ret = 2;
331 }
332 }
333 if (enc_dbcs == DBCS_JPNU)
334 {
335 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenline2 == NULL)
338 ret = 2;
339 }
340#endif
341
342 if (ret != 2)
343 {
344 /* Save the text displayed in the command line area. */
345 for (r = 0; r < rows; ++r)
346 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200348 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (size_t)cols * sizeof(schar_T));
350 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200352 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353#ifdef FEAT_MBYTE
354 if (enc_utf8)
355 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenlineC[i] + r * cols,
361 ScreenLinesC[i] + LineOffset[cmdline_row + r],
362 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 }
364 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368#endif
369 }
370
371 update_screen(0);
372 ret = 3;
373
374 if (must_redraw == 0)
375 {
376 int off = (int)(current_ScreenLine - ScreenLines);
377
378 /* Restore the text displayed in the command line area. */
379 for (r = 0; r < rows; ++r)
380 {
381 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenline + r * cols,
383 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200385 screenattr + r * cols,
386 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387#ifdef FEAT_MBYTE
388 if (enc_utf8)
389 {
390 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenlineUC + r * cols,
392 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 for (i = 0; i < p_mco; ++i)
394 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenlineC[i] + r * cols,
396 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 }
398 if (enc_dbcs == DBCS_JPNU)
399 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenline2 + r * cols,
401 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200403 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 ret = 4;
406 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408
409 vim_free(screenline);
410 vim_free(screenattr);
411#ifdef FEAT_MBYTE
412 if (enc_utf8)
413 {
414 vim_free(screenlineUC);
415 for (i = 0; i < p_mco; ++i)
416 vim_free(screenlineC[i]);
417 }
418 if (enc_dbcs == DBCS_JPNU)
419 vim_free(screenline2);
420#endif
421
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200422 /* Show the intro message when appropriate. */
423 maybe_intro_message();
424
425 setcursor();
426
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427 return ret;
428}
429
430/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100431 * Invoked after an asynchronous callback is called.
432 * If an echo command was used the cursor needs to be put back where
433 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200434 * If "call_update_screen" is FALSE don't call update_screen() when at the
435 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100436 */
437 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200438redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200440 ++redrawing_for_callback;
441
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100442 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200443 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100444 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200445 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200446 // Don't redraw when in prompt_for_number().
447 if (cmdline_row > 0)
448 {
449 // Redrawing only works when the screen didn't scroll. Don't clear
450 // wildmenu entries.
451 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200452#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 && call_update_screen)
456 update_screen(0);
457
458 // Redraw in the same position, so that the user can continue
459 // editing the command.
460 redrawcmdline_ex(FALSE);
461 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200463 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100464 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200465 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100467 setcursor();
468 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100469 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100471 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // Don't update the cursor when it is blinking and off to avoid
473 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100474 out_flush_cursor(FALSE, FALSE);
475 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100477 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200478
479 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100480}
481
482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 * Changed something in the current window, at buffer line "lnum", that
484 * requires that line and possibly other lines to be redrawn.
485 * Used when entering/leaving Insert mode with the cursor on a folded line.
486 * Used to remove the "$" from a change command.
487 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
488 * may become invalid and the whole window will have to be redrawn.
489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100491redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200492 win_T *wp,
Bram Moolenaar05540972016-01-30 20:31:25 +0100493 linenr_T lnum,
494 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496#ifdef FEAT_FOLDING
497 int i;
498#endif
499
Bram Moolenaar90a99792018-09-12 21:52:18 +0200500 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
501 wp->w_redraw_top = lnum;
502 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
503 wp->w_redraw_bot = lnum;
504 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
506#ifdef FEAT_FOLDING
507 if (invalid)
508 {
509 /* A w_lines[] entry for this lnum has become invalid. */
Bram Moolenaar90a99792018-09-12 21:52:18 +0200510 i = find_wl_entry(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (i >= 0)
Bram Moolenaar90a99792018-09-12 21:52:18 +0200512 wp->w_lines[i].wl_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 }
514#endif
515}
516
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200517 void
518reset_updating_screen(int may_resize_shell UNUSED)
519{
520 updating_screen = FALSE;
521#ifdef FEAT_GUI
522 if (may_resize_shell)
523 gui_may_resize_shell();
524#endif
525#ifdef FEAT_TERMINAL
526 term_check_channel_closed_recently();
527#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200528
529#ifdef HAVE_DROP_FILE
530 // If handle_drop() was called while updating_screen was TRUE need to
531 // handle the drop now.
532 handle_any_postponed_drop();
533#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200534}
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200537 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100540update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 redraw_curbuf_later(type);
543 update_screen(type);
544}
545
546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * Based on the current value of curwin->w_topline, transfer a screenfull
548 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200549 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200551 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 win_T *wp;
556 static int did_intro = FALSE;
557#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
558 int did_one;
559#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200561 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200562 int gui_cursor_col;
563 int gui_cursor_row;
564#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200565 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000567 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200571 if (type == VALID_NO_UPDATE)
572 {
573 no_update = TRUE;
574 type = 0;
575 }
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (must_redraw)
578 {
579 if (type < must_redraw) /* use maximal type */
580 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000581
582 /* must_redraw is reset here, so that when we run into some weird
583 * reason to redraw while busy redrawing (e.g., asynchronous
584 * scrolling), or update_topline() in win_update() will cause a
585 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 must_redraw = 0;
587 }
588
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200589 /* May need to update w_lines[]. */
590 if (curwin->w_lines_valid == 0 && type < NOT_VALID
591#ifdef FEAT_TERMINAL
592 && !term_do_update_window(curwin)
593#endif
594 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 type = NOT_VALID;
596
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000597 /* Postpone the redrawing when it's not needed and when being called
598 * recursively. */
599 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {
601 redraw_later(type); /* remember type for next time */
602 must_redraw = type;
603 if (type > INVERTED_ALL)
604 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
607
608 updating_screen = TRUE;
609#ifdef FEAT_SYN_HL
610 ++display_tick; /* let syntax code know we're in a next round of
611 * display updating */
612#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200613 if (no_update)
614 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 /*
617 * if the screen was scrolled up when displaying a message, scroll it down
618 */
619 if (msg_scrolled)
620 {
621 clear_cmdline = TRUE;
622 if (msg_scrolled > Rows - 5) /* clearing is faster */
623 type = CLEAR;
624 else if (type != CLEAR)
625 {
626 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200627 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
628 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 type = CLEAR;
630 FOR_ALL_WINDOWS(wp)
631 {
632 if (W_WINROW(wp) < msg_scrolled)
633 {
634 if (W_WINROW(wp) + wp->w_height > msg_scrolled
635 && wp->w_redr_type < REDRAW_TOP
636 && wp->w_lines_valid > 0
637 && wp->w_topline == wp->w_lines[0].wl_lnum)
638 {
639 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
640 wp->w_redr_type = REDRAW_TOP;
641 }
642 else
643 {
644 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200645 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
646 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
649 }
650 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200651 if (!no_update)
652 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000653 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 msg_scrolled = 0;
656 need_wait_return = FALSE;
657 }
658
659 /* reset cmdline_row now (may have been changed temporarily) */
660 compute_cmdrow();
661
662 /* Check for changed highlighting */
663 if (need_highlight_changed)
664 highlight_changed();
665
666 if (type == CLEAR) /* first clear screen */
667 {
668 screenclear(); /* will reset clear_cmdline */
669 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200670 /* must_redraw may be set indirectly, avoid another redraw later */
671 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673
674 if (clear_cmdline) /* going to clear cmdline (done below) */
675 check_for_delay(FALSE);
676
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000677#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200678 /* Force redraw when width of 'number' or 'relativenumber' column
679 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000680 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200681 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
682 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000683 curwin->w_redr_type = NOT_VALID;
684#endif
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 /*
687 * Only start redrawing if there is really something to do.
688 */
689 if (type == INVERTED)
690 update_curswant();
691 if (curwin->w_redr_type < type
692 && !((type == VALID
693 && curwin->w_lines[0].wl_valid
694#ifdef FEAT_DIFF
695 && curwin->w_topfill == curwin->w_old_topfill
696 && curwin->w_botfill == curwin->w_old_botfill
697#endif
698 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000700 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
702 && curwin->w_old_visual_mode == VIsual_mode
703 && (curwin->w_valid & VALID_VIRTCOL)
704 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ))
706 curwin->w_redr_type = type;
707
Bram Moolenaar5a305422006-04-28 22:38:25 +0000708 /* Redraw the tab pages line if needed. */
709 if (redraw_tabline || type >= NOT_VALID)
710 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000711
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifdef FEAT_SYN_HL
713 /*
714 * Correct stored syntax highlighting info for changes in each displayed
715 * buffer. Each buffer must only be done once.
716 */
717 FOR_ALL_WINDOWS(wp)
718 {
719 if (wp->w_buffer->b_mod_set)
720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 win_T *wwp;
722
723 /* Check if we already did this buffer. */
724 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
725 if (wwp->w_buffer == wp->w_buffer)
726 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200727 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 syn_stack_apply_changes(wp->w_buffer);
729 }
730 }
731#endif
732
733 /*
734 * Go from top to bottom through the windows, redrawing the ones that need
735 * it.
736 */
737#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
738 did_one = FALSE;
739#endif
740#ifdef FEAT_SEARCH_EXTRA
741 search_hl.rm.regprog = NULL;
742#endif
743 FOR_ALL_WINDOWS(wp)
744 {
745 if (wp->w_redr_type != 0)
746 {
747 cursor_off();
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 if (!did_one)
750 {
751 did_one = TRUE;
752# ifdef FEAT_SEARCH_EXTRA
753 start_search_hl();
754# endif
755# ifdef FEAT_CLIPBOARD
756 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200757 if (clip_star.available && clip_isautosel_star())
758 clip_update_selection(&clip_star);
759 if (clip_plus.available && clip_isautosel_plus())
760 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761# endif
762#ifdef FEAT_GUI
763 /* Remove the cursor before starting to do anything, because
764 * scrolling may make it difficult to redraw the text under
765 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200766 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 {
768 gui_cursor_col = gui.cursor_col;
769 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200771 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
774 }
775#endif
776 win_update(wp);
777 }
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 /* redraw status line after the window to minimize cursor movement */
780 if (wp->w_redr_status)
781 {
782 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200783 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786#if defined(FEAT_SEARCH_EXTRA)
787 end_search_hl();
788#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100789#ifdef FEAT_INS_EXPAND
790 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200791 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 /* Reset b_mod_set flags. Going through all windows is probably faster
795 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200796 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200799 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /* Clear or redraw the command line. Done last, because scrolling may
802 * mess up the command line. */
803 if (clear_cmdline || redraw_cmdline)
804 showmode();
805
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200806 if (no_update)
807 --no_win_do_lines_ins;
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200810 if (!did_intro)
811 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 did_intro = TRUE;
813
814#ifdef FEAT_GUI
815 /* Redraw the cursor and update the scrollbars when all screen updating is
816 * done. */
817 if (gui.in_use)
818 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200819 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100821 mch_disable_flush();
822 out_flush(); /* required before updating the cursor */
823 mch_enable_flush();
824
Bram Moolenaar144445d2016-07-08 21:41:54 +0200825 /* Put the GUI position where the cursor was, gui_update_cursor()
826 * uses that. */
827 gui.col = gui_cursor_col;
828 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200829# ifdef FEAT_MBYTE
830 gui.col = mb_fix_col(gui.col, gui.row);
831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100833 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200834 screen_cur_col = gui.col;
835 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 else
838 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_scrollbars(FALSE);
840 }
841#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200842 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100845#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
846/*
847 * Prepare for updating one or more windows.
848 * Caller must check for "updating_screen" already set to avoid recursiveness.
849 */
850 static void
851update_prepare(void)
852{
853 cursor_off();
854 updating_screen = TRUE;
855#ifdef FEAT_GUI
856 /* Remove the cursor before starting to do anything, because scrolling may
857 * make it difficult to redraw the text under it. */
858 if (gui.in_use)
859 gui_undraw_cursor();
860#endif
861#ifdef FEAT_SEARCH_EXTRA
862 start_search_hl();
863#endif
864}
865
866/*
867 * Finish updating one or more windows.
868 */
869 static void
870update_finish(void)
871{
872 if (redraw_cmdline)
873 showmode();
874
875# ifdef FEAT_SEARCH_EXTRA
876 end_search_hl();
877# endif
878
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200879 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880
881# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100882 /* Redraw the cursor and update the scrollbars when all screen updating is
883 * done. */
884 if (gui.in_use)
885 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100886 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887 gui_update_scrollbars(FALSE);
888 }
889# endif
890}
891#endif
892
Bram Moolenaar860cae12010-06-05 23:22:07 +0200893#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200894/*
895 * Return TRUE if the cursor line in window "wp" may be concealed, according
896 * to the 'concealcursor' option.
897 */
898 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100899conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900{
901 int c;
902
903 if (*wp->w_p_cocu == NUL)
904 return FALSE;
905 if (get_real_state() & VISUAL)
906 c = 'v';
907 else if (State & INSERT)
908 c = 'i';
909 else if (State & NORMAL)
910 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200911 else if (State & CMDLINE)
912 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913 else
914 return FALSE;
915 return vim_strchr(wp->w_p_cocu, c) != NULL;
916}
917
918/*
919 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
920 */
921 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200922conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200923{
924 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
925 {
926 need_cursor_line_redraw = TRUE;
927 /* Need to recompute cursor column, e.g., when starting Visual mode
928 * without concealing. */
929 curs_columns(TRUE);
930 }
931}
932
Bram Moolenaar860cae12010-06-05 23:22:07 +0200933 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935{
936 int row;
937 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200938#ifdef SYN_TIME_LIMIT
939 proftime_T syntax_tm;
940#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941
Bram Moolenaar908be432016-05-24 10:51:30 +0200942 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100943 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200944 return;
945
Bram Moolenaar860cae12010-06-05 23:22:07 +0200946 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200947 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200949#ifdef SYN_TIME_LIMIT
950 /* Set the time limit to 'redrawtime'. */
951 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200952 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200953#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100954 update_prepare();
955
Bram Moolenaar860cae12010-06-05 23:22:07 +0200956 row = 0;
957 for (j = 0; j < wp->w_lines_valid; ++j)
958 {
959 if (lnum == wp->w_lines[j].wl_lnum)
960 {
961 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200962# ifdef FEAT_SEARCH_EXTRA
963 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200964 prepare_search_hl(wp, lnum);
965# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200966 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
967 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200968 break;
969 }
970 row += wp->w_lines[j].wl_size;
971 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100972
973 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200974
975#ifdef SYN_TIME_LIMIT
976 syn_set_timeout(NULL);
977#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200978 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200979 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981#endif
982
983#if defined(FEAT_SIGNS) || defined(PROTO)
984 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100985update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
987 win_T *wp;
988 int doit = FALSE;
989
990# ifdef FEAT_FOLDING
991 win_foldinfo.fi_level = 0;
992# endif
993
994 /* update/delete a specific mark */
995 FOR_ALL_WINDOWS(wp)
996 {
997 if (buf != NULL && lnum > 0)
998 {
999 if (wp->w_buffer == buf && lnum >= wp->w_topline
1000 && lnum < wp->w_botline)
1001 {
1002 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1003 wp->w_redraw_top = lnum;
1004 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1005 wp->w_redraw_bot = lnum;
1006 redraw_win_later(wp, VALID);
1007 }
1008 }
1009 else
1010 redraw_win_later(wp, VALID);
1011 if (wp->w_redr_type != 0)
1012 doit = TRUE;
1013 }
1014
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001015 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001016 * happening (recursive call), messages on the screen or still starting up.
1017 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001018 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001019 || State == ASKMORE || State == HITRETURN
1020 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001021#ifdef FEAT_GUI
1022 || gui.starting
1023#endif
1024 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 return;
1026
1027 /* update all windows that need updating */
1028 update_prepare();
1029
Bram Moolenaar29323592016-07-24 22:04:11 +02001030 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
1032 if (wp->w_redr_type != 0)
1033 win_update(wp);
1034 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001035 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 update_finish();
1039}
1040#endif
1041
1042
1043#if defined(FEAT_GUI) || defined(PROTO)
1044/*
1045 * Update a single window, its status line and maybe the command line msg.
1046 * Used for the GUI scrollbar.
1047 */
1048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001049updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001051 /* return if already busy updating */
1052 if (updating_screen)
1053 return;
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 update_prepare();
1056
1057#ifdef FEAT_CLIPBOARD
1058 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001059 if (clip_star.available && clip_isautosel_star())
1060 clip_update_selection(&clip_star);
1061 if (clip_plus.available && clip_isautosel_plus())
1062 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001066
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001067 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001068 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001069 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (wp->w_redr_status
1072# ifdef FEAT_CMDL_INFO
1073 || p_ru
1074# endif
1075# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001076 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077# endif
1078 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001079 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
1081 update_finish();
1082}
1083#endif
1084
1085/*
1086 * Update a single window.
1087 *
1088 * This may cause the windows below it also to be redrawn (when clearing the
1089 * screen or scrolling lines).
1090 *
1091 * How the window is redrawn depends on wp->w_redr_type. Each type also
1092 * implies the one below it.
1093 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001094 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1096 * INVERTED redraw the changed part of the Visual area
1097 * INVERTED_ALL redraw the whole Visual area
1098 * VALID 1. scroll up/down to adjust for a changed w_topline
1099 * 2. update lines at the top when scrolled down
1100 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001101 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 * b_mod_top and b_mod_bot.
1103 * - if wp->w_redraw_top non-zero, redraw lines between
1104 * wp->w_redraw_top and wp->w_redr_bot.
1105 * - continue redrawing when syntax status is invalid.
1106 * 4. if scrolled up, update lines at the bottom.
1107 * This results in three areas that may need updating:
1108 * top: from first row to top_end (when scrolled down)
1109 * mid: from mid_start to mid_end (update inversion or changed text)
1110 * bot: from bot_start to last row (when scrolled up)
1111 */
1112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001113win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 buf_T *buf = wp->w_buffer;
1116 int type;
1117 int top_end = 0; /* Below last row of the top area that needs
1118 updating. 0 when no top area updating. */
1119 int mid_start = 999;/* first row of the mid area that needs
1120 updating. 999 when no mid area updating. */
1121 int mid_end = 0; /* Below last row of the mid area that needs
1122 updating. 0 when no mid area updating. */
1123 int bot_start = 999;/* first row of the bot area that needs
1124 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 int scrolled_down = FALSE; /* TRUE when scrolled down when
1126 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001128 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int top_to_mod = FALSE; /* redraw above mod_top */
1130#endif
1131
1132 int row; /* current window row to display */
1133 linenr_T lnum; /* current buffer lnum to display */
1134 int idx; /* current index in w_lines[] */
1135 int srow; /* starting row of the current line */
1136
1137 int eof = FALSE; /* if TRUE, we hit the end of the file */
1138 int didline = FALSE; /* if TRUE, we finished the last line */
1139 int i;
1140 long j;
1141 static int recursive = FALSE; /* being called recursively */
1142 int old_botline = wp->w_botline;
1143#ifdef FEAT_FOLDING
1144 long fold_count;
1145#endif
1146#ifdef FEAT_SYN_HL
1147 /* remember what happened to the previous line, to know if
1148 * check_visual_highlight() can be used */
1149#define DID_NONE 1 /* didn't update a line */
1150#define DID_LINE 2 /* updated a normal line */
1151#define DID_FOLD 3 /* updated a folded line */
1152 int did_update = DID_NONE;
1153 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1154#endif
1155 linenr_T mod_top = 0;
1156 linenr_T mod_bot = 0;
1157#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1158 int save_got_int;
1159#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001160#ifdef SYN_TIME_LIMIT
1161 proftime_T syntax_tm;
1162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 type = wp->w_redr_type;
1165
1166 if (type == NOT_VALID)
1167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 wp->w_lines_valid = 0;
1170 }
1171
1172 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001173 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
1175 wp->w_redr_type = 0;
1176 return;
1177 }
1178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 /* Window is zero-width: Only need to draw the separator. */
1180 if (wp->w_width == 0)
1181 {
1182 /* draw the vertical separator right of this window */
1183 draw_vsep_win(wp, 0);
1184 wp->w_redr_type = 0;
1185 return;
1186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001188#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001189 // If this window contains a terminal, redraw works completely differently.
1190 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001191 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001192 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001193# ifdef FEAT_MENU
1194 /* Draw the window toolbar, if there is one. */
1195 if (winbar_height(wp) > 0)
1196 redraw_win_toolbar(wp);
1197# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001198 wp->w_redr_type = 0;
1199 return;
1200 }
1201#endif
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001204 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#endif
1206
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001207#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001208 /* Force redraw when width of 'number' or 'relativenumber' column
1209 * changes. */
1210 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001211 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001212 {
1213 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001214 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001215 }
1216 else
1217#endif
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1220 {
1221 /*
1222 * When there are both inserted/deleted lines and specific lines to be
1223 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1224 * everything (only happens when redrawing is off for while).
1225 */
1226 type = NOT_VALID;
1227 }
1228 else
1229 {
1230 /*
1231 * Set mod_top to the first line that needs displaying because of
1232 * changes. Set mod_bot to the first line after the changes.
1233 */
1234 mod_top = wp->w_redraw_top;
1235 if (wp->w_redraw_bot != 0)
1236 mod_bot = wp->w_redraw_bot + 1;
1237 else
1238 mod_bot = 0;
1239 wp->w_redraw_top = 0; /* reset for next time */
1240 wp->w_redraw_bot = 0;
1241 if (buf->b_mod_set)
1242 {
1243 if (mod_top == 0 || mod_top > buf->b_mod_top)
1244 {
1245 mod_top = buf->b_mod_top;
1246#ifdef FEAT_SYN_HL
1247 /* Need to redraw lines above the change that may be included
1248 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001249 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001251 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (mod_top < 1)
1253 mod_top = 1;
1254 }
1255#endif
1256 }
1257 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1258 mod_bot = buf->b_mod_bot;
1259
1260#ifdef FEAT_SEARCH_EXTRA
1261 /* When 'hlsearch' is on and using a multi-line search pattern, a
1262 * change in one line may make the Search highlighting in a
1263 * previous line invalid. Simple solution: redraw all visible
1264 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001265 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001267 if (search_hl.rm.regprog != NULL
1268 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001270 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001271 {
1272 cur = wp->w_match_head;
1273 while (cur != NULL)
1274 {
1275 if (cur->match.regprog != NULL
1276 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001277 {
1278 top_to_mod = TRUE;
1279 break;
1280 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001281 cur = cur->next;
1282 }
1283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284#endif
1285 }
1286#ifdef FEAT_FOLDING
1287 if (mod_top != 0 && hasAnyFolding(wp))
1288 {
1289 linenr_T lnumt, lnumb;
1290
1291 /*
1292 * A change in a line can cause lines above it to become folded or
1293 * unfolded. Find the top most buffer line that may be affected.
1294 * If the line was previously folded and displayed, get the first
1295 * line of that fold. If the line is folded now, get the first
1296 * folded line. Use the minimum of these two.
1297 */
1298
1299 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1300 * the line below it. If there is no valid entry, use w_topline.
1301 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1302 * to this line. If there is no valid entry, use MAXLNUM. */
1303 lnumt = wp->w_topline;
1304 lnumb = MAXLNUM;
1305 for (i = 0; i < wp->w_lines_valid; ++i)
1306 if (wp->w_lines[i].wl_valid)
1307 {
1308 if (wp->w_lines[i].wl_lastlnum < mod_top)
1309 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1310 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1311 {
1312 lnumb = wp->w_lines[i].wl_lnum;
1313 /* When there is a fold column it might need updating
1314 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001315 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 ++lnumb;
1317 }
1318 }
1319
1320 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1321 if (mod_top > lnumt)
1322 mod_top = lnumt;
1323
1324 /* Now do the same for the bottom line (one above mod_bot). */
1325 --mod_bot;
1326 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1327 ++mod_bot;
1328 if (mod_bot < lnumb)
1329 mod_bot = lnumb;
1330 }
1331#endif
1332
1333 /* When a change starts above w_topline and the end is below
1334 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001335 * If the end of the change is above w_topline: do like no change was
1336 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (mod_top != 0 && mod_top < wp->w_topline)
1338 {
1339 if (mod_bot > wp->w_topline)
1340 mod_top = wp->w_topline;
1341#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001342 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 top_end = 1;
1344#endif
1345 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001346
1347 /* When line numbers are displayed need to redraw all lines below
1348 * inserted/deleted lines. */
1349 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1350 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352
1353 /*
1354 * When only displaying the lines at the top, set top_end. Used when
1355 * window has scrolled down for msg_scrolled.
1356 */
1357 if (type == REDRAW_TOP)
1358 {
1359 j = 0;
1360 for (i = 0; i < wp->w_lines_valid; ++i)
1361 {
1362 j += wp->w_lines[i].wl_size;
1363 if (j >= wp->w_upd_rows)
1364 {
1365 top_end = j;
1366 break;
1367 }
1368 }
1369 if (top_end == 0)
1370 /* not found (cannot happen?): redraw everything */
1371 type = NOT_VALID;
1372 else
1373 /* top area defined, the rest is VALID */
1374 type = VALID;
1375 }
1376
Bram Moolenaar367329b2007-08-30 11:53:22 +00001377 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001378 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1379 * non-zero and thus not FALSE) will indicate that screenclear() was not
1380 * called. */
1381 if (screen_cleared)
1382 screen_cleared = MAYBE;
1383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 /*
1385 * If there are no changes on the screen that require a complete redraw,
1386 * handle three cases:
1387 * 1: we are off the top of the screen by a few lines: scroll down
1388 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1389 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1390 * w_lines[] that needs updating.
1391 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001392 if ((type == VALID || type == SOME_VALID
1393 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#ifdef FEAT_DIFF
1395 && !wp->w_botfill && !wp->w_old_botfill
1396#endif
1397 )
1398 {
1399 if (mod_top != 0 && wp->w_topline == mod_top)
1400 {
1401 /*
1402 * w_topline is the first changed line, the scrolling will be done
1403 * further down.
1404 */
1405 }
1406 else if (wp->w_lines[0].wl_valid
1407 && (wp->w_topline < wp->w_lines[0].wl_lnum
1408#ifdef FEAT_DIFF
1409 || (wp->w_topline == wp->w_lines[0].wl_lnum
1410 && wp->w_topfill > wp->w_old_topfill)
1411#endif
1412 ))
1413 {
1414 /*
1415 * New topline is above old topline: May scroll down.
1416 */
1417#ifdef FEAT_FOLDING
1418 if (hasAnyFolding(wp))
1419 {
1420 linenr_T ln;
1421
1422 /* count the number of lines we are off, counting a sequence
1423 * of folded lines as one */
1424 j = 0;
1425 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1426 {
1427 ++j;
1428 if (j >= wp->w_height - 2)
1429 break;
1430 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1431 }
1432 }
1433 else
1434#endif
1435 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1436 if (j < wp->w_height - 2) /* not too far off */
1437 {
1438 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1439#ifdef FEAT_DIFF
1440 /* insert extra lines for previously invisible filler lines */
1441 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1442 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1443 - wp->w_old_topfill;
1444#endif
1445 if (i < wp->w_height - 2) /* less than a screen off */
1446 {
1447 /*
1448 * Try to insert the correct number of lines.
1449 * If not the last window, delete the lines at the bottom.
1450 * win_ins_lines may fail when the terminal can't do it.
1451 */
1452 if (i > 0)
1453 check_for_delay(FALSE);
1454 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1455 {
1456 if (wp->w_lines_valid != 0)
1457 {
1458 /* Need to update rows that are new, stop at the
1459 * first one that scrolled down. */
1460 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
1463 /* Move the entries that were scrolled, disable
1464 * the entries for the lines to be redrawn. */
1465 if ((wp->w_lines_valid += j) > wp->w_height)
1466 wp->w_lines_valid = wp->w_height;
1467 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1468 wp->w_lines[idx] = wp->w_lines[idx - j];
1469 while (idx >= 0)
1470 wp->w_lines[idx--].wl_valid = FALSE;
1471 }
1472 }
1473 else
1474 mid_start = 0; /* redraw all lines */
1475 }
1476 else
1477 mid_start = 0; /* redraw all lines */
1478 }
1479 else
1480 mid_start = 0; /* redraw all lines */
1481 }
1482 else
1483 {
1484 /*
1485 * New topline is at or below old topline: May scroll up.
1486 * When topline didn't change, find first entry in w_lines[] that
1487 * needs updating.
1488 */
1489
1490 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1491 j = -1;
1492 row = 0;
1493 for (i = 0; i < wp->w_lines_valid; i++)
1494 {
1495 if (wp->w_lines[i].wl_valid
1496 && wp->w_lines[i].wl_lnum == wp->w_topline)
1497 {
1498 j = i;
1499 break;
1500 }
1501 row += wp->w_lines[i].wl_size;
1502 }
1503 if (j == -1)
1504 {
1505 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1506 * lines */
1507 mid_start = 0;
1508 }
1509 else
1510 {
1511 /*
1512 * Try to delete the correct number of lines.
1513 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1514 */
1515#ifdef FEAT_DIFF
1516 /* If the topline didn't change, delete old filler lines,
1517 * otherwise delete filler lines of the new topline... */
1518 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1519 row += wp->w_old_topfill;
1520 else
1521 row += diff_check_fill(wp, wp->w_topline);
1522 /* ... but don't delete new filler lines. */
1523 row -= wp->w_topfill;
1524#endif
1525 if (row > 0)
1526 {
1527 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001528 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1529 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 bot_start = wp->w_height - row;
1531 else
1532 mid_start = 0; /* redraw all lines */
1533 }
1534 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1535 {
1536 /*
1537 * Skip the lines (below the deleted lines) that are still
1538 * valid and don't need redrawing. Copy their info
1539 * upwards, to compensate for the deleted lines. Set
1540 * bot_start to the first row that needs redrawing.
1541 */
1542 bot_start = 0;
1543 idx = 0;
1544 for (;;)
1545 {
1546 wp->w_lines[idx] = wp->w_lines[j];
1547 /* stop at line that didn't fit, unless it is still
1548 * valid (no lines deleted) */
1549 if (row > 0 && bot_start + row
1550 + (int)wp->w_lines[j].wl_size > wp->w_height)
1551 {
1552 wp->w_lines_valid = idx + 1;
1553 break;
1554 }
1555 bot_start += wp->w_lines[idx++].wl_size;
1556
1557 /* stop at the last valid entry in w_lines[].wl_size */
1558 if (++j >= wp->w_lines_valid)
1559 {
1560 wp->w_lines_valid = idx;
1561 break;
1562 }
1563 }
1564#ifdef FEAT_DIFF
1565 /* Correct the first entry for filler lines at the top
1566 * when it won't get updated below. */
1567 if (wp->w_p_diff && bot_start > 0)
1568 wp->w_lines[0].wl_size =
1569 plines_win_nofill(wp, wp->w_topline, TRUE)
1570 + wp->w_topfill;
1571#endif
1572 }
1573 }
1574 }
1575
1576 /* When starting redraw in the first line, redraw all lines. When
1577 * there is only one window it's probably faster to clear the screen
1578 * first. */
1579 if (mid_start == 0)
1580 {
1581 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001582 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001583 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001584 /* Clear the screen when it was not done by win_del_lines() or
1585 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1586 * then. */
1587 if (screen_cleared != TRUE)
1588 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001589 /* The screen was cleared, redraw the tab pages line. */
1590 if (redraw_tabline)
1591 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001594
1595 /* When win_del_lines() or win_ins_lines() caused the screen to be
1596 * cleared (only happens for the first window) or when screenclear()
1597 * was called directly above, "must_redraw" will have been set to
1598 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1599 if (screen_cleared == TRUE)
1600 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 else
1603 {
1604 /* Not VALID or INVERTED: redraw all lines. */
1605 mid_start = 0;
1606 mid_end = wp->w_height;
1607 }
1608
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001609 if (type == SOME_VALID)
1610 {
1611 /* SOME_VALID: redraw all lines. */
1612 mid_start = 0;
1613 mid_end = wp->w_height;
1614 type = NOT_VALID;
1615 }
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 /* check if we are updating or removing the inverted part */
1618 if ((VIsual_active && buf == curwin->w_buffer)
1619 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1620 {
1621 linenr_T from, to;
1622
1623 if (VIsual_active)
1624 {
1625 if (VIsual_active
1626 && (VIsual_mode != wp->w_old_visual_mode
1627 || type == INVERTED_ALL))
1628 {
1629 /*
1630 * If the type of Visual selection changed, redraw the whole
1631 * selection. Also when the ownership of the X selection is
1632 * gained or lost.
1633 */
1634 if (curwin->w_cursor.lnum < VIsual.lnum)
1635 {
1636 from = curwin->w_cursor.lnum;
1637 to = VIsual.lnum;
1638 }
1639 else
1640 {
1641 from = VIsual.lnum;
1642 to = curwin->w_cursor.lnum;
1643 }
1644 /* redraw more when the cursor moved as well */
1645 if (wp->w_old_cursor_lnum < from)
1646 from = wp->w_old_cursor_lnum;
1647 if (wp->w_old_cursor_lnum > to)
1648 to = wp->w_old_cursor_lnum;
1649 if (wp->w_old_visual_lnum < from)
1650 from = wp->w_old_visual_lnum;
1651 if (wp->w_old_visual_lnum > to)
1652 to = wp->w_old_visual_lnum;
1653 }
1654 else
1655 {
1656 /*
1657 * Find the line numbers that need to be updated: The lines
1658 * between the old cursor position and the current cursor
1659 * position. Also check if the Visual position changed.
1660 */
1661 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1662 {
1663 from = curwin->w_cursor.lnum;
1664 to = wp->w_old_cursor_lnum;
1665 }
1666 else
1667 {
1668 from = wp->w_old_cursor_lnum;
1669 to = curwin->w_cursor.lnum;
1670 if (from == 0) /* Visual mode just started */
1671 from = to;
1672 }
1673
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001674 if (VIsual.lnum != wp->w_old_visual_lnum
1675 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
1677 if (wp->w_old_visual_lnum < from
1678 && wp->w_old_visual_lnum != 0)
1679 from = wp->w_old_visual_lnum;
1680 if (wp->w_old_visual_lnum > to)
1681 to = wp->w_old_visual_lnum;
1682 if (VIsual.lnum < from)
1683 from = VIsual.lnum;
1684 if (VIsual.lnum > to)
1685 to = VIsual.lnum;
1686 }
1687 }
1688
1689 /*
1690 * If in block mode and changed column or curwin->w_curswant:
1691 * update all lines.
1692 * First compute the actual start and end column.
1693 */
1694 if (VIsual_mode == Ctrl_V)
1695 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001696 colnr_T fromc, toc;
1697#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1698 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
Bram Moolenaar404406a2014-10-09 13:24:43 +02001700 if (curwin->w_p_lbr)
1701 ve_flags = VE_ALL;
1702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001704#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1705 ve_flags = save_ve_flags;
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 ++toc;
1708 if (curwin->w_curswant == MAXCOL)
1709 toc = MAXCOL;
1710
1711 if (fromc != wp->w_old_cursor_fcol
1712 || toc != wp->w_old_cursor_lcol)
1713 {
1714 if (from > VIsual.lnum)
1715 from = VIsual.lnum;
1716 if (to < VIsual.lnum)
1717 to = VIsual.lnum;
1718 }
1719 wp->w_old_cursor_fcol = fromc;
1720 wp->w_old_cursor_lcol = toc;
1721 }
1722 }
1723 else
1724 {
1725 /* Use the line numbers of the old Visual area. */
1726 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1727 {
1728 from = wp->w_old_cursor_lnum;
1729 to = wp->w_old_visual_lnum;
1730 }
1731 else
1732 {
1733 from = wp->w_old_visual_lnum;
1734 to = wp->w_old_cursor_lnum;
1735 }
1736 }
1737
1738 /*
1739 * There is no need to update lines above the top of the window.
1740 */
1741 if (from < wp->w_topline)
1742 from = wp->w_topline;
1743
1744 /*
1745 * If we know the value of w_botline, use it to restrict the update to
1746 * the lines that are visible in the window.
1747 */
1748 if (wp->w_valid & VALID_BOTLINE)
1749 {
1750 if (from >= wp->w_botline)
1751 from = wp->w_botline - 1;
1752 if (to >= wp->w_botline)
1753 to = wp->w_botline - 1;
1754 }
1755
1756 /*
1757 * Find the minimal part to be updated.
1758 * Watch out for scrolling that made entries in w_lines[] invalid.
1759 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1760 * top_end; need to redraw from top_end to the "to" line.
1761 * A middle mouse click with a Visual selection may change the text
1762 * above the Visual area and reset wl_valid, do count these for
1763 * mid_end (in srow).
1764 */
1765 if (mid_start > 0)
1766 {
1767 lnum = wp->w_topline;
1768 idx = 0;
1769 srow = 0;
1770 if (scrolled_down)
1771 mid_start = top_end;
1772 else
1773 mid_start = 0;
1774 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1775 {
1776 if (wp->w_lines[idx].wl_valid)
1777 mid_start += wp->w_lines[idx].wl_size;
1778 else if (!scrolled_down)
1779 srow += wp->w_lines[idx].wl_size;
1780 ++idx;
1781# ifdef FEAT_FOLDING
1782 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1783 lnum = wp->w_lines[idx].wl_lnum;
1784 else
1785# endif
1786 ++lnum;
1787 }
1788 srow += mid_start;
1789 mid_end = wp->w_height;
1790 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1791 {
1792 if (wp->w_lines[idx].wl_valid
1793 && wp->w_lines[idx].wl_lnum >= to + 1)
1794 {
1795 /* Only update until first row of this line */
1796 mid_end = srow;
1797 break;
1798 }
1799 srow += wp->w_lines[idx].wl_size;
1800 }
1801 }
1802 }
1803
1804 if (VIsual_active && buf == curwin->w_buffer)
1805 {
1806 wp->w_old_visual_mode = VIsual_mode;
1807 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1808 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001809 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 wp->w_old_curswant = curwin->w_curswant;
1811 }
1812 else
1813 {
1814 wp->w_old_visual_mode = 0;
1815 wp->w_old_cursor_lnum = 0;
1816 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001817 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
1820#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1821 /* reset got_int, otherwise regexp won't work */
1822 save_got_int = got_int;
1823 got_int = 0;
1824#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001825#ifdef SYN_TIME_LIMIT
1826 /* Set the time limit to 'redrawtime'. */
1827 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001828 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830#ifdef FEAT_FOLDING
1831 win_foldinfo.fi_level = 0;
1832#endif
1833
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001834#ifdef FEAT_MENU
1835 /*
1836 * Draw the window toolbar, if there is one.
1837 * TODO: only when needed.
1838 */
1839 if (winbar_height(wp) > 0)
1840 redraw_win_toolbar(wp);
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 /*
1844 * Update all the window rows.
1845 */
1846 idx = 0; /* first entry in w_lines[].wl_size */
1847 row = 0;
1848 srow = 0;
1849 lnum = wp->w_topline; /* first line shown in window */
1850 for (;;)
1851 {
1852 /* stop updating when reached the end of the window (check for _past_
1853 * the end of the window is at the end of the loop) */
1854 if (row == wp->w_height)
1855 {
1856 didline = TRUE;
1857 break;
1858 }
1859
1860 /* stop updating when hit the end of the file */
1861 if (lnum > buf->b_ml.ml_line_count)
1862 {
1863 eof = TRUE;
1864 break;
1865 }
1866
1867 /* Remember the starting row of the line that is going to be dealt
1868 * with. It is used further down when the line doesn't fit. */
1869 srow = row;
1870
1871 /*
1872 * Update a line when it is in an area that needs updating, when it
1873 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001874 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 * win_del_lines(), check if the current line includes it.
1876 * When syntax folding is being used, the saved syntax states will
1877 * already have been updated, we can't see where the syntax state is
1878 * the same again, just update until the end of the window.
1879 */
1880 if (row < top_end
1881 || (row >= mid_start && row < mid_end)
1882#ifdef FEAT_SEARCH_EXTRA
1883 || top_to_mod
1884#endif
1885 || idx >= wp->w_lines_valid
1886 || (row + wp->w_lines[idx].wl_size > bot_start)
1887 || (mod_top != 0
1888 && (lnum == mod_top
1889 || (lnum >= mod_top
1890 && (lnum < mod_bot
1891#ifdef FEAT_SYN_HL
1892 || did_update == DID_FOLD
1893 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001894 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 && (
1896# ifdef FEAT_FOLDING
1897 (foldmethodIsSyntax(wp)
1898 && hasAnyFolding(wp)) ||
1899# endif
1900 syntax_check_changed(lnum)))
1901#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001902#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001903 /* match in fixed position might need redraw
1904 * if lines were inserted or deleted */
1905 || (wp->w_match_head != NULL
1906 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 )))))
1909 {
1910#ifdef FEAT_SEARCH_EXTRA
1911 if (lnum == mod_top)
1912 top_to_mod = FALSE;
1913#endif
1914
1915 /*
1916 * When at start of changed lines: May scroll following lines
1917 * up or down to minimize redrawing.
1918 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001919 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 */
1921 if (lnum == mod_top
1922 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001923 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 int old_rows = 0;
1926 int new_rows = 0;
1927 int xtra_rows;
1928 linenr_T l;
1929
1930 /* Count the old number of window rows, using w_lines[], which
1931 * should still contain the sizes for the lines as they are
1932 * currently displayed. */
1933 for (i = idx; i < wp->w_lines_valid; ++i)
1934 {
1935 /* Only valid lines have a meaningful wl_lnum. Invalid
1936 * lines are part of the changed area. */
1937 if (wp->w_lines[i].wl_valid
1938 && wp->w_lines[i].wl_lnum == mod_bot)
1939 break;
1940 old_rows += wp->w_lines[i].wl_size;
1941#ifdef FEAT_FOLDING
1942 if (wp->w_lines[i].wl_valid
1943 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1944 {
1945 /* Must have found the last valid entry above mod_bot.
1946 * Add following invalid entries. */
1947 ++i;
1948 while (i < wp->w_lines_valid
1949 && !wp->w_lines[i].wl_valid)
1950 old_rows += wp->w_lines[i++].wl_size;
1951 break;
1952 }
1953#endif
1954 }
1955
1956 if (i >= wp->w_lines_valid)
1957 {
1958 /* We can't find a valid line below the changed lines,
1959 * need to redraw until the end of the window.
1960 * Inserting/deleting lines has no use. */
1961 bot_start = 0;
1962 }
1963 else
1964 {
1965 /* Able to count old number of rows: Count new window
1966 * rows, and may insert/delete lines */
1967 j = idx;
1968 for (l = lnum; l < mod_bot; ++l)
1969 {
1970#ifdef FEAT_FOLDING
1971 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1972 ++new_rows;
1973 else
1974#endif
1975#ifdef FEAT_DIFF
1976 if (l == wp->w_topline)
1977 new_rows += plines_win_nofill(wp, l, TRUE)
1978 + wp->w_topfill;
1979 else
1980#endif
1981 new_rows += plines_win(wp, l, TRUE);
1982 ++j;
1983 if (new_rows > wp->w_height - row - 2)
1984 {
1985 /* it's getting too much, must redraw the rest */
1986 new_rows = 9999;
1987 break;
1988 }
1989 }
1990 xtra_rows = new_rows - old_rows;
1991 if (xtra_rows < 0)
1992 {
1993 /* May scroll text up. If there is not enough
1994 * remaining text or scrolling fails, must redraw the
1995 * rest. If scrolling works, must redraw the text
1996 * below the scrolled text. */
1997 if (row - xtra_rows >= wp->w_height - 2)
1998 mod_bot = MAXLNUM;
1999 else
2000 {
2001 check_for_delay(FALSE);
2002 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002003 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 mod_bot = MAXLNUM;
2005 else
2006 bot_start = wp->w_height + xtra_rows;
2007 }
2008 }
2009 else if (xtra_rows > 0)
2010 {
2011 /* May scroll text down. If there is not enough
2012 * remaining text of scrolling fails, must redraw the
2013 * rest. */
2014 if (row + xtra_rows >= wp->w_height - 2)
2015 mod_bot = MAXLNUM;
2016 else
2017 {
2018 check_for_delay(FALSE);
2019 if (win_ins_lines(wp, row + old_rows,
2020 xtra_rows, FALSE, FALSE) == FAIL)
2021 mod_bot = MAXLNUM;
2022 else if (top_end > row + old_rows)
2023 /* Scrolled the part at the top that requires
2024 * updating down. */
2025 top_end += xtra_rows;
2026 }
2027 }
2028
2029 /* When not updating the rest, may need to move w_lines[]
2030 * entries. */
2031 if (mod_bot != MAXLNUM && i != j)
2032 {
2033 if (j < i)
2034 {
2035 int x = row + new_rows;
2036
2037 /* move entries in w_lines[] upwards */
2038 for (;;)
2039 {
2040 /* stop at last valid entry in w_lines[] */
2041 if (i >= wp->w_lines_valid)
2042 {
2043 wp->w_lines_valid = j;
2044 break;
2045 }
2046 wp->w_lines[j] = wp->w_lines[i];
2047 /* stop at a line that won't fit */
2048 if (x + (int)wp->w_lines[j].wl_size
2049 > wp->w_height)
2050 {
2051 wp->w_lines_valid = j + 1;
2052 break;
2053 }
2054 x += wp->w_lines[j++].wl_size;
2055 ++i;
2056 }
2057 if (bot_start > x)
2058 bot_start = x;
2059 }
2060 else /* j > i */
2061 {
2062 /* move entries in w_lines[] downwards */
2063 j -= i;
2064 wp->w_lines_valid += j;
2065 if (wp->w_lines_valid > wp->w_height)
2066 wp->w_lines_valid = wp->w_height;
2067 for (i = wp->w_lines_valid; i - j >= idx; --i)
2068 wp->w_lines[i] = wp->w_lines[i - j];
2069
2070 /* The w_lines[] entries for inserted lines are
2071 * now invalid, but wl_size may be used above.
2072 * Reset to zero. */
2073 while (i >= idx)
2074 {
2075 wp->w_lines[i].wl_size = 0;
2076 wp->w_lines[i--].wl_valid = FALSE;
2077 }
2078 }
2079 }
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082
2083#ifdef FEAT_FOLDING
2084 /*
2085 * When lines are folded, display one line for all of them.
2086 * Otherwise, display normally (can be several display lines when
2087 * 'wrap' is on).
2088 */
2089 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2090 if (fold_count != 0)
2091 {
2092 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2093 ++row;
2094 --fold_count;
2095 wp->w_lines[idx].wl_folded = TRUE;
2096 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2097# ifdef FEAT_SYN_HL
2098 did_update = DID_FOLD;
2099# endif
2100 }
2101 else
2102#endif
2103 if (idx < wp->w_lines_valid
2104 && wp->w_lines[idx].wl_valid
2105 && wp->w_lines[idx].wl_lnum == lnum
2106 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002107 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 && srow + wp->w_lines[idx].wl_size > wp->w_height
2109#ifdef FEAT_DIFF
2110 && diff_check_fill(wp, lnum) == 0
2111#endif
2112 )
2113 {
2114 /* This line is not going to fit. Don't draw anything here,
2115 * will draw "@ " lines below. */
2116 row = wp->w_height + 1;
2117 }
2118 else
2119 {
2120#ifdef FEAT_SEARCH_EXTRA
2121 prepare_search_hl(wp, lnum);
2122#endif
2123#ifdef FEAT_SYN_HL
2124 /* Let the syntax stuff know we skipped a few lines. */
2125 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002126 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 syntax_end_parsing(syntax_last_parsed + 1);
2128#endif
2129
2130 /*
2131 * Display one line.
2132 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002133 row = win_line(wp, lnum, srow, wp->w_height,
2134 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136#ifdef FEAT_FOLDING
2137 wp->w_lines[idx].wl_folded = FALSE;
2138 wp->w_lines[idx].wl_lastlnum = lnum;
2139#endif
2140#ifdef FEAT_SYN_HL
2141 did_update = DID_LINE;
2142 syntax_last_parsed = lnum;
2143#endif
2144 }
2145
2146 wp->w_lines[idx].wl_lnum = lnum;
2147 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002148
2149 /* Past end of the window or end of the screen. Note that after
2150 * resizing wp->w_height may be end up too big. That's a problem
2151 * elsewhere, but prevent a crash here. */
2152 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002155 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2157 ++idx;
2158 break;
2159 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002160 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 wp->w_lines[idx].wl_size = row - srow;
2162 ++idx;
2163#ifdef FEAT_FOLDING
2164 lnum += fold_count + 1;
2165#else
2166 ++lnum;
2167#endif
2168 }
2169 else
2170 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002171 if (wp->w_p_rnu)
2172 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002173#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002174 // 'relativenumber' set: The text doesn't need to be drawn, but
2175 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002176 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2177 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002178 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002179 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002180#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002181 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002182 }
2183
2184 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 row += wp->w_lines[idx++].wl_size;
2186 if (row > wp->w_height) /* past end of screen */
2187 break;
2188#ifdef FEAT_FOLDING
2189 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2190#else
2191 ++lnum;
2192#endif
2193#ifdef FEAT_SYN_HL
2194 did_update = DID_NONE;
2195#endif
2196 }
2197
2198 if (lnum > buf->b_ml.ml_line_count)
2199 {
2200 eof = TRUE;
2201 break;
2202 }
2203 }
2204 /*
2205 * End of loop over all window lines.
2206 */
2207
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002208#ifdef FEAT_VTP
2209 /* Rewrite the character at the end of the screen line. */
2210 if (use_vtp())
2211 {
2212 int i;
2213
2214 for (i = 0; i < Rows; ++i)
2215# ifdef FEAT_MBYTE
2216 if (enc_utf8)
2217 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2218 LineOffset[i] + screen_Columns) > 1)
2219 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2220 else
2221 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2222 else
2223# endif
2224 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2225 }
2226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228 if (idx > wp->w_lines_valid)
2229 wp->w_lines_valid = idx;
2230
2231#ifdef FEAT_SYN_HL
2232 /*
2233 * Let the syntax stuff know we stop parsing here.
2234 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002235 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 syntax_end_parsing(syntax_last_parsed + 1);
2237#endif
2238
2239 /*
2240 * If we didn't hit the end of the file, and we didn't finish the last
2241 * line we were working on, then the line didn't fit.
2242 */
2243 wp->w_empty_rows = 0;
2244#ifdef FEAT_DIFF
2245 wp->w_filler_rows = 0;
2246#endif
2247 if (!eof && !didline)
2248 {
2249 if (lnum == wp->w_topline)
2250 {
2251 /*
2252 * Single line that does not fit!
2253 * Don't overwrite it, it can be edited.
2254 */
2255 wp->w_botline = lnum + 1;
2256 }
2257#ifdef FEAT_DIFF
2258 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2259 {
2260 /* Window ends in filler lines. */
2261 wp->w_botline = lnum;
2262 wp->w_filler_rows = wp->w_height - srow;
2263 }
2264#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002265 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2266 {
2267 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2268
2269 /*
2270 * Last line isn't finished: Display "@@@" in the last screen line.
2271 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002272 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002273 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002274 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002275 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002276 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002277 set_empty_rows(wp, srow);
2278 wp->w_botline = lnum;
2279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2281 {
2282 /*
2283 * Last line isn't finished: Display "@@@" at the end.
2284 */
2285 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2286 W_WINROW(wp) + wp->w_height,
2287 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002288 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 set_empty_rows(wp, srow);
2290 wp->w_botline = lnum;
2291 }
2292 else
2293 {
2294 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2295 wp->w_botline = lnum;
2296 }
2297 }
2298 else
2299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (eof) /* we hit the end of the file */
2302 {
2303 wp->w_botline = buf->b_ml.ml_line_count + 1;
2304#ifdef FEAT_DIFF
2305 j = diff_check_fill(wp, wp->w_botline);
2306 if (j > 0 && !wp->w_botfill)
2307 {
2308 /*
2309 * Display filler lines at the end of the file
2310 */
2311 if (char2cells(fill_diff) > 1)
2312 i = '-';
2313 else
2314 i = fill_diff;
2315 if (row + j > wp->w_height)
2316 j = wp->w_height - row;
2317 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2318 row += j;
2319 }
2320#endif
2321 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002322 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 wp->w_botline = lnum;
2324
2325 /* make sure the rest of the screen is blank */
2326 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002327 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002330#ifdef SYN_TIME_LIMIT
2331 syn_set_timeout(NULL);
2332#endif
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 /* Reset the type of redrawing required, the window has been updated. */
2335 wp->w_redr_type = 0;
2336#ifdef FEAT_DIFF
2337 wp->w_old_topfill = wp->w_topfill;
2338 wp->w_old_botfill = wp->w_botfill;
2339#endif
2340
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002341 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
2343 /*
2344 * There is a trick with w_botline. If we invalidate it on each
2345 * change that might modify it, this will cause a lot of expensive
2346 * calls to plines() in update_topline() each time. Therefore the
2347 * value of w_botline is often approximated, and this value is used to
2348 * compute the value of w_topline. If the value of w_botline was
2349 * wrong, check that the value of w_topline is correct (cursor is on
2350 * the visible part of the text). If it's not, we need to redraw
2351 * again. Mostly this just means scrolling up a few lines, so it
2352 * doesn't look too bad. Only do this for the current window (where
2353 * changes are relevant).
2354 */
2355 wp->w_valid |= VALID_BOTLINE;
2356 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2357 {
2358 recursive = TRUE;
2359 curwin->w_valid &= ~VALID_TOPLINE;
2360 update_topline(); /* may invalidate w_botline again */
2361 if (must_redraw != 0)
2362 {
2363 /* Don't update for changes in buffer again. */
2364 i = curbuf->b_mod_set;
2365 curbuf->b_mod_set = FALSE;
2366 win_update(curwin);
2367 must_redraw = 0;
2368 curbuf->b_mod_set = i;
2369 }
2370 recursive = FALSE;
2371 }
2372 }
2373
2374#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2375 /* restore got_int, unless CTRL-C was hit while redrawing */
2376 if (!got_int)
2377 got_int = save_got_int;
2378#endif
2379}
2380
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381/*
2382 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2383 * as the filler character.
2384 */
2385 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002386win_draw_end(
2387 win_T *wp,
2388 int c1,
2389 int c2,
2390 int row,
2391 int endrow,
2392 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2395 int n = 0;
2396# define FDC_OFF n
2397#else
2398# define FDC_OFF 0
2399#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002400#ifdef FEAT_FOLDING
2401 int fdc = compute_foldcolumn(wp, 0);
2402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404#ifdef FEAT_RIGHTLEFT
2405 if (wp->w_p_rl)
2406 {
2407 /* No check for cmdline window: should never be right-left. */
2408# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002409 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 if (n > 0)
2412 {
2413 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002414 if (n > wp->w_width)
2415 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2417 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002418 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420# endif
2421# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002422 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
2424 int nn = n + 2;
2425
2426 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002427 if (nn > wp->w_width)
2428 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2430 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002431 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 n = nn;
2433 }
2434# endif
2435 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002436 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002437 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2439 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002440 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442 else
2443#endif
2444 {
2445#ifdef FEAT_CMDWIN
2446 if (cmdwin_type != 0 && wp == curwin)
2447 {
2448 /* draw the cmdline character in the leftmost column */
2449 n = 1;
2450 if (n > wp->w_width)
2451 n = wp->w_width;
2452 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002453 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002454 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 }
2456#endif
2457#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002458 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002460 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002463 if (nn > wp->w_width)
2464 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002466 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002467 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 n = nn;
2469 }
2470#endif
2471#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002472 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 int nn = n + 2;
2475
2476 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002477 if (nn > wp->w_width)
2478 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002480 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002481 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 n = nn;
2483 }
2484#endif
2485 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002486 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002487 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489 set_empty_rows(wp, row);
2490}
2491
Bram Moolenaar1a384422010-07-14 19:53:30 +02002492#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002493/*
2494 * Advance **color_cols and return TRUE when there are columns to draw.
2495 */
2496 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002497advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002498{
2499 while (**color_cols >= 0 && vcol > **color_cols)
2500 ++*color_cols;
2501 return (**color_cols >= 0);
2502}
2503#endif
2504
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002505#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2506/*
2507 * Copy "text" to ScreenLines using "attr".
2508 * Returns the next screen column.
2509 */
2510 static int
2511text_to_screenline(win_T *wp, char_u *text, int col)
2512{
2513 int off = (int)(current_ScreenLine - ScreenLines);
2514
2515#ifdef FEAT_MBYTE
2516 if (has_mbyte)
2517 {
2518 int cells;
2519 int u8c, u8cc[MAX_MCO];
2520 int i;
2521 int idx;
2522 int c_len;
2523 char_u *p;
2524# ifdef FEAT_ARABIC
2525 int prev_c = 0; /* previous Arabic character */
2526 int prev_c1 = 0; /* first composing char for prev_c */
2527# endif
2528
2529# ifdef FEAT_RIGHTLEFT
2530 if (wp->w_p_rl)
2531 idx = off;
2532 else
2533# endif
2534 idx = off + col;
2535
2536 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2537 for (p = text; *p != NUL; )
2538 {
2539 cells = (*mb_ptr2cells)(p);
2540 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002541 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002542# ifdef FEAT_RIGHTLEFT
2543 - (wp->w_p_rl ? col : 0)
2544# endif
2545 )
2546 break;
2547 ScreenLines[idx] = *p;
2548 if (enc_utf8)
2549 {
2550 u8c = utfc_ptr2char(p, u8cc);
2551 if (*p < 0x80 && u8cc[0] == 0)
2552 {
2553 ScreenLinesUC[idx] = 0;
2554#ifdef FEAT_ARABIC
2555 prev_c = u8c;
2556#endif
2557 }
2558 else
2559 {
2560#ifdef FEAT_ARABIC
2561 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2562 {
2563 /* Do Arabic shaping. */
2564 int pc, pc1, nc;
2565 int pcc[MAX_MCO];
2566 int firstbyte = *p;
2567
2568 /* The idea of what is the previous and next
2569 * character depends on 'rightleft'. */
2570 if (wp->w_p_rl)
2571 {
2572 pc = prev_c;
2573 pc1 = prev_c1;
2574 nc = utf_ptr2char(p + c_len);
2575 prev_c1 = u8cc[0];
2576 }
2577 else
2578 {
2579 pc = utfc_ptr2char(p + c_len, pcc);
2580 nc = prev_c;
2581 pc1 = pcc[0];
2582 }
2583 prev_c = u8c;
2584
2585 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2586 pc, pc1, nc);
2587 ScreenLines[idx] = firstbyte;
2588 }
2589 else
2590 prev_c = u8c;
2591#endif
2592 /* Non-BMP character: display as ? or fullwidth ?. */
2593#ifdef UNICODE16
2594 if (u8c >= 0x10000)
2595 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2596 else
2597#endif
2598 ScreenLinesUC[idx] = u8c;
2599 for (i = 0; i < Screen_mco; ++i)
2600 {
2601 ScreenLinesC[i][idx] = u8cc[i];
2602 if (u8cc[i] == 0)
2603 break;
2604 }
2605 }
2606 if (cells > 1)
2607 ScreenLines[idx + 1] = 0;
2608 }
2609 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2610 /* double-byte single width character */
2611 ScreenLines2[idx] = p[1];
2612 else if (cells > 1)
2613 /* double-width character */
2614 ScreenLines[idx + 1] = p[1];
2615 col += cells;
2616 idx += cells;
2617 p += c_len;
2618 }
2619 }
2620 else
2621#endif
2622 {
2623 int len = (int)STRLEN(text);
2624
Bram Moolenaar02631462017-09-22 15:20:32 +02002625 if (len > wp->w_width - col)
2626 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002627 if (len > 0)
2628 {
2629#ifdef FEAT_RIGHTLEFT
2630 if (wp->w_p_rl)
2631 STRNCPY(current_ScreenLine, text, len);
2632 else
2633#endif
2634 STRNCPY(current_ScreenLine + col, text, len);
2635 col += len;
2636 }
2637 }
2638 return col;
2639}
2640#endif
2641
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#ifdef FEAT_FOLDING
2643/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002644 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2645 * space is available for window "wp", minus "col".
2646 */
2647 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002648compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002649{
2650 int fdc = wp->w_p_fdc;
2651 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002652 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002653
2654 if (fdc > wwidth - (col + wmw))
2655 fdc = wwidth - (col + wmw);
2656 return fdc;
2657}
2658
2659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 * Display one folded line.
2661 */
2662 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002663fold_line(
2664 win_T *wp,
2665 long fold_count,
2666 foldinfo_T *foldinfo,
2667 linenr_T lnum,
2668 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002670 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 pos_T *top, *bot;
2672 linenr_T lnume = lnum + fold_count - 1;
2673 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002674 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 int col;
2677 int txtcol;
2678 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002679 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 /* Build the fold line:
2682 * 1. Add the cmdwin_type for the command-line window
2683 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002684 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 * 4. Compose the text
2686 * 5. Add the text
2687 * 6. set highlighting for the Visual area an other text
2688 */
2689 col = 0;
2690
2691 /*
2692 * 1. Add the cmdwin_type for the command-line window
2693 * Ignores 'rightleft', this window is never right-left.
2694 */
2695#ifdef FEAT_CMDWIN
2696 if (cmdwin_type != 0 && wp == curwin)
2697 {
2698 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002699 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700#ifdef FEAT_MBYTE
2701 if (enc_utf8)
2702 ScreenLinesUC[off] = 0;
2703#endif
2704 ++col;
2705 }
2706#endif
2707
2708 /*
2709 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002710 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002712 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (fdc > 0)
2714 {
2715 fill_foldcolumn(buf, wp, TRUE, lnum);
2716#ifdef FEAT_RIGHTLEFT
2717 if (wp->w_p_rl)
2718 {
2719 int i;
2720
Bram Moolenaar02631462017-09-22 15:20:32 +02002721 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002722 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 /* reverse the fold column */
2724 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002725 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
2727 else
2728#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002729 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 col += fdc;
2731 }
2732
2733#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002734# define RL_MEMSET(p, v, l) \
2735 do { \
2736 if (wp->w_p_rl) \
2737 for (ri = 0; ri < l; ++ri) \
2738 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2739 else \
2740 for (ri = 0; ri < l; ++ri) \
2741 ScreenAttrs[off + (p) + ri] = v; \
2742 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002744# define RL_MEMSET(p, v, l) \
2745 do { \
2746 for (ri = 0; ri < l; ++ri) \
2747 ScreenAttrs[off + (p) + ri] = v; \
2748 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749#endif
2750
Bram Moolenaar64486672010-05-16 15:46:46 +02002751 /* Set all attributes of the 'number' or 'relativenumber' column and the
2752 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002753 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755#ifdef FEAT_SIGNS
2756 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002757 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002759 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (len > 0)
2761 {
2762 if (len > 2)
2763 len = 2;
2764# ifdef FEAT_RIGHTLEFT
2765 if (wp->w_p_rl)
2766 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002767 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002768 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else
2770# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002771 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 col += len;
2773 }
2774 }
2775#endif
2776
2777 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002778 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002780 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002782 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (len > 0)
2784 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002785 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002786 long num;
2787 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002788
2789 if (len > w + 1)
2790 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002791
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002792 if (wp->w_p_nu && !wp->w_p_rnu)
2793 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002794 num = (long)lnum;
2795 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002796 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002797 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002798 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002799 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002800 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002801 /* 'number' + 'relativenumber': cursor line shows absolute
2802 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002803 num = lnum;
2804 fmt = "%-*ld ";
2805 }
2806 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002807
Bram Moolenaar700e7342013-01-30 12:31:36 +01002808 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809#ifdef FEAT_RIGHTLEFT
2810 if (wp->w_p_rl)
2811 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002812 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002813 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 else
2815#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002816 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 col += len;
2818 }
2819 }
2820
2821 /*
2822 * 4. Compose the folded-line string with 'foldtext', if set.
2823 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002824 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
2826 txtcol = col; /* remember where text starts */
2827
2828 /*
2829 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2830 * Right-left text is put in columns 0 - number-col, normal text is put
2831 * in columns number-col - window-width.
2832 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002833 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 /* Fill the rest of the line with the fold filler */
2836#ifdef FEAT_RIGHTLEFT
2837 if (wp->w_p_rl)
2838 col -= txtcol;
2839#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002840 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841#ifdef FEAT_RIGHTLEFT
2842 - (wp->w_p_rl ? txtcol : 0)
2843#endif
2844 )
2845 {
2846#ifdef FEAT_MBYTE
2847 if (enc_utf8)
2848 {
2849 if (fill_fold >= 0x80)
2850 {
2851 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002852 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002853 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 }
2855 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002858 ScreenLines[off + col] = fill_fold;
2859 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002860 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002862 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002864 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
2866
2867 if (text != buf)
2868 vim_free(text);
2869
2870 /*
2871 * 6. set highlighting for the Visual area an other text.
2872 * If all folded lines are in the Visual area, highlight the line.
2873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2875 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002876 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 {
2878 /* Visual is after curwin->w_cursor */
2879 top = &curwin->w_cursor;
2880 bot = &VIsual;
2881 }
2882 else
2883 {
2884 /* Visual is before curwin->w_cursor */
2885 top = &VIsual;
2886 bot = &curwin->w_cursor;
2887 }
2888 if (lnum >= top->lnum
2889 && lnume <= bot->lnum
2890 && (VIsual_mode != 'v'
2891 || ((lnum > top->lnum
2892 || (lnum == top->lnum
2893 && top->col == 0))
2894 && (lnume < bot->lnum
2895 || (lnume == bot->lnum
2896 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002897 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
2899 if (VIsual_mode == Ctrl_V)
2900 {
2901 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002902 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002904 if (wp->w_old_cursor_lcol != MAXCOL
2905 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002906 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 len = wp->w_old_cursor_lcol;
2908 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002910 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002911 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913 }
2914 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002917 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002922#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002923 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2924 if (wp->w_p_cc_cols)
2925 {
2926 int i = 0;
2927 int j = wp->w_p_cc_cols[i];
2928 int old_txtcol = txtcol;
2929
2930 while (j > -1)
2931 {
2932 txtcol += j;
2933 if (wp->w_p_wrap)
2934 txtcol -= wp->w_skipcol;
2935 else
2936 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002937 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002938 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002939 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002940 txtcol = old_txtcol;
2941 j = wp->w_p_cc_cols[++i];
2942 }
2943 }
2944
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002945 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002946 if (wp->w_p_cuc)
2947 {
2948 txtcol += wp->w_virtcol;
2949 if (wp->w_p_wrap)
2950 txtcol -= wp->w_skipcol;
2951 else
2952 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002953 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002954 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002955 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002956 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
Bram Moolenaar02631462017-09-22 15:20:32 +02002959 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2960 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 /*
2963 * Update w_cline_height and w_cline_folded if the cursor line was
2964 * updated (saves a call to plines() later).
2965 */
2966 if (wp == curwin
2967 && lnum <= curwin->w_cursor.lnum
2968 && lnume >= curwin->w_cursor.lnum)
2969 {
2970 curwin->w_cline_row = row;
2971 curwin->w_cline_height = 1;
2972 curwin->w_cline_folded = TRUE;
2973 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2974 }
2975}
2976
2977/*
2978 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2979 */
2980 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002981copy_text_attr(
2982 int off,
2983 char_u *buf,
2984 int len,
2985 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002987 int i;
2988
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 mch_memmove(ScreenLines + off, buf, (size_t)len);
2990# ifdef FEAT_MBYTE
2991 if (enc_utf8)
2992 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2993# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002994 for (i = 0; i < len; ++i)
2995 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996}
2997
2998/*
2999 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003000 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 */
3002 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003003fill_foldcolumn(
3004 char_u *p,
3005 win_T *wp,
3006 int closed, /* TRUE of FALSE */
3007 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 int i = 0;
3010 int level;
3011 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003012 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003013 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003016 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
3018 level = win_foldinfo.fi_level;
3019 if (level > 0)
3020 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003021 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003022 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 /* If the column is too narrow, we start at the lowest level that
3025 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003026 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (first_level < 1)
3028 first_level = 1;
3029
Bram Moolenaar1c934292015-01-27 16:39:29 +01003030 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 {
3032 if (win_foldinfo.fi_lnum == lnum
3033 && first_level + i >= win_foldinfo.fi_low_level)
3034 p[i] = '-';
3035 else if (first_level == 1)
3036 p[i] = '|';
3037 else if (first_level + i <= 9)
3038 p[i] = '0' + first_level + i;
3039 else
3040 p[i] = '>';
3041 if (first_level + i == level)
3042 break;
3043 }
3044 }
3045 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003046 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047}
3048#endif /* FEAT_FOLDING */
3049
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003050#ifdef FEAT_TEXT_PROP
3051static textprop_T *current_text_props = NULL;
3052static buf_T *current_buf = NULL;
3053
3054 static int
3055#ifdef __BORLANDC__
3056_RTLENTRYF
3057#endif
3058text_prop_compare(const void *s1, const void *s2)
3059{
3060 int idx1, idx2;
3061 proptype_T *pt1, *pt2;
3062 colnr_T col1, col2;
3063
3064 idx1 = *(int *)s1;
3065 idx2 = *(int *)s2;
3066 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3067 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3068 if (pt1 == pt2)
3069 return 0;
3070 if (pt1 == NULL)
3071 return -1;
3072 if (pt2 == NULL)
3073 return 1;
3074 if (pt1->pt_priority != pt2->pt_priority)
3075 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3076 col1 = current_text_props[idx1].tp_col;
3077 col2 = current_text_props[idx2].tp_col;
3078 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3079}
3080#endif
3081
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082/*
3083 * Display line "lnum" of window 'wp' on the screen.
3084 * Start at row "startrow", stop when "endrow" is reached.
3085 * wp->w_virtcol needs to be valid.
3086 *
3087 * Return the number of last row the line occupies.
3088 */
3089 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003090win_line(
3091 win_T *wp,
3092 linenr_T lnum,
3093 int startrow,
3094 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003095 int nochange UNUSED, // not updating for changed text
3096 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003098 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3100 int c = 0; /* init for GCC */
3101 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003102#ifdef FEAT_LINEBREAK
3103 long vcol_sbr = -1; /* virtual column after showbreak */
3104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 long vcol_prev = -1; /* "vcol" of previous character */
3106 char_u *line; /* current line */
3107 char_u *ptr; /* current position in "line" */
3108 int row; /* row in the window, excl w_winrow */
3109 int screen_row; /* row on the screen, incl w_winrow */
3110
3111 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3112 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003113 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003114 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 int c_extra = NUL; /* extra chars, all the same */
3116 int extra_attr = 0; /* attributes when n_extra != 0 */
3117 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3118 displaying lcs_eol at end-of-line */
3119 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3120 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3121
3122 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3123 int saved_n_extra = 0;
3124 char_u *saved_p_extra = NULL;
3125 int saved_c_extra = 0;
3126 int saved_char_attr = 0;
3127
3128 int n_attr = 0; /* chars with special attr */
3129 int saved_attr2 = 0; /* char_attr saved for n_attr */
3130 int n_attr3 = 0; /* chars with overruling special attr */
3131 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3132
3133 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3134
3135 int fromcol, tocol; /* start/end of inverting */
3136 int fromcol_prev = -2; /* start of inverting after cursor */
3137 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003139 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 pos_T pos;
3141 long v;
3142
3143 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003144 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3146 in this line */
3147 int attr = 0; /* attributes for area highlighting */
3148 int area_attr = 0; /* attributes desired by highlighting */
3149 int search_attr = 0; /* attributes desired by 'hlsearch' */
3150#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003151 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 int syntax_attr = 0; /* attributes desired by syntax */
3153 int has_syntax = FALSE; /* this buffer has syntax highl. */
3154 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003155 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003156 int draw_color_col = FALSE; /* highlight colorcolumn */
3157 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003158#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003159#ifdef FEAT_TEXT_PROP
3160 int text_prop_count;
3161 int text_prop_next = 0; // next text property to use
3162 textprop_T *text_props = NULL;
3163 int *text_prop_idxs = NULL;
3164 int text_props_active = 0;
3165 proptype_T *text_prop_type = NULL;
3166 int text_prop_attr = 0;
3167#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003168#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003169 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003170# define SPWORDLEN 150
3171 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003172 int nextlinecol = 0; /* column where nextline[] starts */
3173 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003174 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003175 int spell_attr = 0; /* attributes desired by spelling */
3176 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003177 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3178 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003179 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003180 static int cap_col = -1; /* column to check for Cap word */
3181 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003182 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003184 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185#ifdef FEAT_MBYTE
3186 int multi_attr = 0; /* attributes desired by multibyte */
3187 int mb_l = 1; /* multi-byte byte length */
3188 int mb_c = 0; /* decoded multi-byte character */
3189 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003190 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191#endif
3192#ifdef FEAT_DIFF
3193 int filler_lines; /* nr of filler lines to be drawn */
3194 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003195 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 int change_start = MAXCOL; /* first col of changed area */
3197 int change_end = -1; /* last col of changed area */
3198#endif
3199 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3200#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003201 int need_showbreak = FALSE; /* overlong line, skipping first x
3202 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003204#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003205 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003207 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#endif
3209#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003210 matchitem_T *cur; /* points to the match list */
3211 match_T *shl; /* points to search_hl or a match */
3212 int shl_flag; /* flag to indicate whether search_hl
3213 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003214 int pos_inprogress; /* marks that position match search is
3215 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003216 int prevcol_hl_flag; /* flag to indicate whether prevcol
3217 equals startcol of search_hl or one
3218 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#endif
3220#ifdef FEAT_ARABIC
3221 int prev_c = 0; /* previous Arabic character */
3222 int prev_c1 = 0; /* first composing char for prev_c */
3223#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003224#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003225 int did_line_attr = 0;
3226#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003227#ifdef FEAT_TERMINAL
3228 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003229 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
3232 /* draw_state: items that are drawn in sequence: */
3233#define WL_START 0 /* nothing done yet */
3234#ifdef FEAT_CMDWIN
3235# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3236#else
3237# define WL_CMDLINE WL_START
3238#endif
3239#ifdef FEAT_FOLDING
3240# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3241#else
3242# define WL_FOLD WL_CMDLINE
3243#endif
3244#ifdef FEAT_SIGNS
3245# define WL_SIGN WL_FOLD + 1 /* column for signs */
3246#else
3247# define WL_SIGN WL_FOLD /* column for signs */
3248#endif
3249#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003250#ifdef FEAT_LINEBREAK
3251# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003253# define WL_BRI WL_NR
3254#endif
3255#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3256# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3257#else
3258# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259#endif
3260#define WL_LINE WL_SBR + 1 /* text in the line */
3261 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003262#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 int feedback_col = 0;
3264 int feedback_old_attr = -1;
3265#endif
3266
Bram Moolenaar860cae12010-06-05 23:22:07 +02003267#ifdef FEAT_CONCEAL
3268 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003269 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003270 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003271 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003272 int is_concealing = FALSE;
3273 int boguscols = 0; /* nonexistent columns added to force
3274 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003275 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003276 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003277 int match_conc = 0; /* cchar for match functions */
3278 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003279 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003280# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003281# define FIX_FOR_BOGUSCOLS \
3282 { \
3283 n_extra += vcol_off; \
3284 vcol -= vcol_off; \
3285 vcol_off = 0; \
3286 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003287 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003288 boguscols = 0; \
3289 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003290#else
3291# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294 if (startrow > endrow) /* past the end already! */
3295 return startrow;
3296
3297 row = startrow;
3298 screen_row = row + W_WINROW(wp);
3299
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003300 if (!number_only)
3301 {
3302 /*
3303 * To speed up the loop below, set extra_check when there is linebreak,
3304 * trailing white space and/or syntax processing to be done.
3305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003307 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#endif
3309#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003310 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003311# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003312 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003313# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003314 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003316 /* Prepare for syntax highlighting in this line. When there is an
3317 * error, stop syntax highlighting. */
3318 save_did_emsg = did_emsg;
3319 did_emsg = FALSE;
3320 syntax_start(wp, lnum);
3321 if (did_emsg)
3322 wp->w_s->b_syn_error = TRUE;
3323 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003324 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003325 did_emsg = save_did_emsg;
3326#ifdef SYN_TIME_LIMIT
3327 if (!wp->w_s->b_syn_slow)
3328#endif
3329 {
3330 has_syntax = TRUE;
3331 extra_check = TRUE;
3332 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003335
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003336 /* Check for columns to display for 'colorcolumn'. */
3337 color_cols = wp->w_p_cc_cols;
3338 if (color_cols != NULL)
3339 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003340#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003341
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003342#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003343 if (term_show_buffer(wp->w_buffer))
3344 {
3345 extra_check = TRUE;
3346 get_term_attr = TRUE;
3347 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3348 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003349#endif
3350
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003351#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003352 if (wp->w_p_spell
3353 && *wp->w_s->b_p_spl != NUL
3354 && wp->w_s->b_langp.ga_len > 0
3355 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003356 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 /* Prepare for spell checking. */
3358 has_spell = TRUE;
3359 extra_check = TRUE;
3360
Bram Moolenaar7701f302018-10-02 21:20:32 +02003361 /* Get the start of the next line, so that words that wrap to the
3362 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 * Trick: skip a few chars for C/shell/Vim comments */
3364 nextline[SPWORDLEN] = NUL;
3365 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3366 {
3367 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3368 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3369 }
3370
Bram Moolenaar7701f302018-10-02 21:20:32 +02003371 /* When a word wrapped from the previous line the start of the
3372 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003373 if (lnum == checked_lnum)
3374 cur_checked_col = checked_col;
3375 checked_lnum = 0;
3376
3377 /* When there was a sentence end in the previous line may require a
3378 * word starting with capital in this line. In line 1 always check
3379 * the first word. */
3380 if (lnum != capcol_lnum)
3381 cap_col = -1;
3382 if (lnum == 1)
3383 cap_col = 0;
3384 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#endif
3387
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003388 /*
3389 * handle visual active in this window
3390 */
3391 fromcol = -10;
3392 tocol = MAXCOL;
3393 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003395 /* Visual is after curwin->w_cursor */
3396 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003398 top = &curwin->w_cursor;
3399 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003401 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003403 top = &VIsual;
3404 bot = &curwin->w_cursor;
3405 }
3406 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3407 if (VIsual_mode == Ctrl_V) /* block mode */
3408 {
3409 if (lnum_in_visual_area)
3410 {
3411 fromcol = wp->w_old_cursor_fcol;
3412 tocol = wp->w_old_cursor_lcol;
3413 }
3414 }
3415 else /* non-block mode */
3416 {
3417 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003419 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003421 if (VIsual_mode == 'V') /* linewise */
3422 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 else
3424 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003425 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3426 if (gchar_pos(top) == NUL)
3427 tocol = fromcol + 1;
3428 }
3429 }
3430 if (VIsual_mode != 'V' && lnum == bot->lnum)
3431 {
3432 if (*p_sel == 'e' && bot->col == 0
3433#ifdef FEAT_VIRTUALEDIT
3434 && bot->coladd == 0
3435#endif
3436 )
3437 {
3438 fromcol = -10;
3439 tocol = MAXCOL;
3440 }
3441 else if (bot->col == MAXCOL)
3442 tocol = MAXCOL;
3443 else
3444 {
3445 pos = *bot;
3446 if (*p_sel == 'e')
3447 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3448 else
3449 {
3450 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3451 ++tocol;
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454 }
3455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003457 /* Check if the character under the cursor should not be inverted */
3458 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003459#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003460 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003461#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 )
3463 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003465 /* if inverting in this line set area_highlighting */
3466 if (fromcol >= 0)
3467 {
3468 area_highlighting = TRUE;
3469 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003471 if ((clip_star.available && !clip_star.owned
3472 && clip_isautosel_star())
3473 || (clip_plus.available && !clip_plus.owned
3474 && clip_isautosel_plus()))
3475 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003480 /*
3481 * handle 'incsearch' and ":s///c" highlighting
3482 */
3483 else if (highlight_match
3484 && wp == curwin
3485 && lnum >= curwin->w_cursor.lnum
3486 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003488 if (lnum == curwin->w_cursor.lnum)
3489 getvcol(curwin, &(curwin->w_cursor),
3490 (colnr_T *)&fromcol, NULL, NULL);
3491 else
3492 fromcol = 0;
3493 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3494 {
3495 pos.lnum = lnum;
3496 pos.col = search_match_endcol;
3497 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3498 }
3499 else
3500 tocol = MAXCOL;
3501 /* do at least one character; happens when past end of line */
3502 if (fromcol == tocol)
3503 tocol = fromcol + 1;
3504 area_highlighting = TRUE;
3505 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508
3509#ifdef FEAT_DIFF
3510 filler_lines = diff_check(wp, lnum);
3511 if (filler_lines < 0)
3512 {
3513 if (filler_lines == -1)
3514 {
3515 if (diff_find_change(wp, lnum, &change_start, &change_end))
3516 diff_hlf = HLF_ADD; /* added line */
3517 else if (change_start == 0)
3518 diff_hlf = HLF_TXD; /* changed text */
3519 else
3520 diff_hlf = HLF_CHD; /* changed line */
3521 }
3522 else
3523 diff_hlf = HLF_ADD; /* added line */
3524 filler_lines = 0;
3525 area_highlighting = TRUE;
3526 }
3527 if (lnum == wp->w_topline)
3528 filler_lines = wp->w_topfill;
3529 filler_todo = filler_lines;
3530#endif
3531
3532#ifdef LINE_ATTR
3533# ifdef FEAT_SIGNS
3534 /* If this line has a sign with line highlighting set line_attr. */
3535 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3536 if (v != 0)
3537 line_attr = sign_get_attr((int)v, TRUE);
3538# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003539# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003541 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003542 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543# endif
3544 if (line_attr != 0)
3545 area_highlighting = TRUE;
3546#endif
3547
3548 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3549 ptr = line;
3550
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003551#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003552 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003553 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003554 /* For checking first word with a capital skip white space. */
3555 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003556 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003557
Bram Moolenaar30abd282005-06-22 22:35:10 +00003558 /* To be able to spell-check over line boundaries copy the end of the
3559 * current line into nextline[]. Above the start of the next line was
3560 * copied to nextline[SPWORDLEN]. */
3561 if (nextline[SPWORDLEN] == NUL)
3562 {
3563 /* No next line or it is empty. */
3564 nextlinecol = MAXCOL;
3565 nextline_idx = 0;
3566 }
3567 else
3568 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003569 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003570 if (v < SPWORDLEN)
3571 {
3572 /* Short line, use it completely and append the start of the
3573 * next line. */
3574 nextlinecol = 0;
3575 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003576 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003577 nextline_idx = v + 1;
3578 }
3579 else
3580 {
3581 /* Long line, use only the last SPWORDLEN bytes. */
3582 nextlinecol = v - SPWORDLEN;
3583 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3584 nextline_idx = SPWORDLEN + 1;
3585 }
3586 }
3587 }
3588#endif
3589
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003590 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003592 if (lcs_space || lcs_trail)
3593 extra_check = TRUE;
3594 /* find start of trailing whitespace */
3595 if (lcs_trail)
3596 {
3597 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003598 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003599 --trailcol;
3600 trailcol += (colnr_T) (ptr - line);
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603
3604 /*
3605 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3606 * first character to be displayed.
3607 */
3608 if (wp->w_p_wrap)
3609 v = wp->w_skipcol;
3610 else
3611 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003612 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614#ifdef FEAT_MBYTE
3615 char_u *prev_ptr = ptr;
3616#endif
3617 while (vcol < v && *ptr != NUL)
3618 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003619 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 vcol += c;
3621#ifdef FEAT_MBYTE
3622 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003624 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003627 /* When:
3628 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003629 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003630 * - 'virtualedit' is set, or
3631 * - the visual mode is active,
3632 * the end of the line may be before the start of the displayed part.
3633 */
3634 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003635#ifdef FEAT_SYN_HL
3636 wp->w_p_cuc || draw_color_col ||
3637#endif
3638#ifdef FEAT_VIRTUALEDIT
3639 virtual_active() ||
3640#endif
3641 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646 /* Handle a character that's not completely on the screen: Put ptr at
3647 * that character but skip the first few screen characters. */
3648 if (vcol > v)
3649 {
3650 vcol -= c;
3651#ifdef FEAT_MBYTE
3652 ptr = prev_ptr;
3653#else
3654 --ptr;
3655#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003656 /* If the character fits on the screen, don't need to skip it.
3657 * Except for a TAB. */
3658 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003659#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003660 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003661#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003662 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003663 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665
3666 /*
3667 * Adjust for when the inverted text is before the screen,
3668 * and when the start of the inverted text is before the screen.
3669 */
3670 if (tocol <= vcol)
3671 fromcol = 0;
3672 else if (fromcol >= 0 && fromcol < vcol)
3673 fromcol = vcol;
3674
3675#ifdef FEAT_LINEBREAK
3676 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3677 if (wp->w_p_wrap)
3678 need_showbreak = TRUE;
3679#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003680#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003681 /* When spell checking a word we need to figure out the start of the
3682 * word and if it's badly spelled or not. */
3683 if (has_spell)
3684 {
3685 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003686 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003687 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003688
3689 pos = wp->w_cursor;
3690 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003691 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003692 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003693
3694 /* spell_move_to() may call ml_get() and make "line" invalid */
3695 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3696 ptr = line + linecol;
3697
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003698 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003699 {
3700 /* no bad word found at line start, don't check until end of a
3701 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003702 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003703 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003704 }
3705 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003706 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003707 /* bad word found, use attributes until end of word */
3708 word_end = wp->w_cursor.col + len + 1;
3709
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003710 /* Turn index into actual attributes. */
3711 if (spell_hlf != HLF_COUNT)
3712 spell_attr = highlight_attr[spell_hlf];
3713 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003714 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003715
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003716# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003717 /* Need to restart syntax highlighting for this line. */
3718 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003719 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003720# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003721 }
3722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724
3725 /*
3726 * Correct highlighting for cursor that can't be disabled.
3727 * Avoids having to check this for each character.
3728 */
3729 if (fromcol >= 0)
3730 {
3731 if (noinvcur)
3732 {
3733 if ((colnr_T)fromcol == wp->w_virtcol)
3734 {
3735 /* highlighting starts at cursor, let it start just after the
3736 * cursor */
3737 fromcol_prev = fromcol;
3738 fromcol = -1;
3739 }
3740 else if ((colnr_T)fromcol < wp->w_virtcol)
3741 /* restart highlighting after the cursor */
3742 fromcol_prev = wp->w_virtcol;
3743 }
3744 if (fromcol >= tocol)
3745 fromcol = -1;
3746 }
3747
3748#ifdef FEAT_SEARCH_EXTRA
3749 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003750 * Handle highlighting the last used search pattern and matches.
3751 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003753 cur = wp->w_match_head;
3754 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003755 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003757 if (shl_flag == FALSE)
3758 {
3759 shl = &search_hl;
3760 shl_flag = TRUE;
3761 }
3762 else
3763 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003764 shl->startcol = MAXCOL;
3765 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003767 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003768 v = (long)(ptr - line);
3769 if (cur != NULL)
3770 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003771 next_search_hl(wp, shl, lnum, (colnr_T)v,
3772 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003773
3774 /* Need to get the line again, a multi-line regexp may have made it
3775 * invalid. */
3776 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3777 ptr = line + v;
3778
3779 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003781 if (shl->lnum == lnum)
3782 shl->startcol = shl->rm.startpos[0].col;
3783 else
3784 shl->startcol = 0;
3785 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3786 - shl->rm.startpos[0].lnum)
3787 shl->endcol = shl->rm.endpos[0].col;
3788 else
3789 shl->endcol = MAXCOL;
3790 /* Highlight one character for an empty match. */
3791 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003794 if (has_mbyte && line[shl->endcol] != NUL)
3795 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003798 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003800 if ((long)shl->startcol < v) /* match at leftcol */
3801 {
3802 shl->attr_cur = shl->attr;
3803 search_attr = shl->attr;
3804 }
3805 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003807 if (shl != &search_hl && cur != NULL)
3808 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810#endif
3811
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003812#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003813 /* Cursor line highlighting for 'cursorline' in the current window. Not
3814 * when Visual mode is active, because it's not clear what is selected
3815 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003816 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003817 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003818 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003819 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003820 area_highlighting = TRUE;
3821 }
3822#endif
3823
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003824#ifdef FEAT_TEXT_PROP
3825 {
3826 char_u *prop_start;
3827
3828 text_prop_count = get_text_props(wp->w_buffer, lnum,
3829 &prop_start, FALSE);
3830 if (text_prop_count > 0)
3831 {
3832 // Make a copy of the properties, so that they are properly
3833 // aligned.
3834 text_props = (textprop_T *)alloc(
3835 text_prop_count * sizeof(textprop_T));
3836 if (text_props != NULL)
3837 mch_memmove(text_props, prop_start,
3838 text_prop_count * sizeof(textprop_T));
3839
3840 // Allocate an array for the indexes.
3841 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3842 area_highlighting = TRUE;
3843 extra_check = TRUE;
3844 }
3845 }
3846#endif
3847
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003848 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 col = 0;
3850#ifdef FEAT_RIGHTLEFT
3851 if (wp->w_p_rl)
3852 {
3853 /* Rightleft window: process the text in the normal direction, but put
3854 * it in current_ScreenLine[] from right to left. Start at the
3855 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003856 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 off += col;
3858 }
3859#endif
3860
3861 /*
3862 * Repeat for the whole displayed line.
3863 */
3864 for (;;)
3865 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003866#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003867 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 /* Skip this quickly when working on the text. */
3870 if (draw_state != WL_LINE)
3871 {
3872#ifdef FEAT_CMDWIN
3873 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3874 {
3875 draw_state = WL_CMDLINE;
3876 if (cmdwin_type != 0 && wp == curwin)
3877 {
3878 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003880 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003881 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883 }
3884#endif
3885
3886#ifdef FEAT_FOLDING
3887 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3888 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003889 int fdc = compute_foldcolumn(wp, 0);
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003892 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003894 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003895 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003896 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003897 p_extra_free = alloc(12 + 1);
3898
3899 if (p_extra_free != NULL)
3900 {
3901 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3902 n_extra = fdc;
3903 p_extra_free[n_extra] = NUL;
3904 p_extra = p_extra_free;
3905 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003906 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909 }
3910#endif
3911
3912#ifdef FEAT_SIGNS
3913 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3914 {
3915 draw_state = WL_SIGN;
3916 /* Show the sign column when there are any signs in this
3917 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003918 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003920 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003922 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923# endif
3924
3925 /* Draw two cells with the sign value or blank. */
3926 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003927 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 n_extra = 2;
3929
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003930 if (row == startrow
3931#ifdef FEAT_DIFF
3932 + filler_lines && filler_todo <= 0
3933#endif
3934 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3937 SIGN_TEXT);
3938# ifdef FEAT_SIGN_ICONS
3939 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3940 SIGN_ICON);
3941 if (gui.in_use && icon_sign != 0)
3942 {
3943 /* Use the image in this position. */
3944 c_extra = SIGN_BYTE;
3945# ifdef FEAT_NETBEANS_INTG
3946 if (buf_signcount(wp->w_buffer, lnum) > 1)
3947 c_extra = MULTISIGN_BYTE;
3948# endif
3949 char_attr = icon_sign;
3950 }
3951 else
3952# endif
3953 if (text_sign != 0)
3954 {
3955 p_extra = sign_get_text(text_sign);
3956 if (p_extra != NULL)
3957 {
3958 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003959 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 char_attr = sign_get_attr(text_sign, FALSE);
3962 }
3963 }
3964 }
3965 }
3966#endif
3967
3968 if (draw_state == WL_NR - 1 && n_extra == 0)
3969 {
3970 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003971 /* Display the absolute or relative line number. After the
3972 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3973 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 && (row == startrow
3975#ifdef FEAT_DIFF
3976 + filler_lines
3977#endif
3978 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3979 {
3980 /* Draw the line number (empty space after wrapping). */
3981 if (row == startrow
3982#ifdef FEAT_DIFF
3983 + filler_lines
3984#endif
3985 )
3986 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003987 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003988 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003989
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003990 if (wp->w_p_nu && !wp->w_p_rnu)
3991 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003992 num = (long)lnum;
3993 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003994 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003995 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003996 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003997 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003998 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003999 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004000 num = lnum;
4001 fmt = "%-*ld ";
4002 }
4003 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004004
Bram Moolenaar700e7342013-01-30 12:31:36 +01004005 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004006 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (wp->w_skipcol > 0)
4008 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4009 *p_extra = '-';
4010#ifdef FEAT_RIGHTLEFT
4011 if (wp->w_p_rl) /* reverse line numbers */
4012 rl_mirror(extra);
4013#endif
4014 p_extra = extra;
4015 c_extra = NUL;
4016 }
4017 else
4018 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004019 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004020 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004021#ifdef FEAT_SYN_HL
4022 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004023 * the current line differently.
4024 * TODO: Can we use CursorLine instead of CursorLineNr
4025 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004026 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004027 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004028 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031 }
4032
Bram Moolenaar597a4222014-06-25 14:39:50 +02004033#ifdef FEAT_LINEBREAK
4034 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4035 && n_extra == 0 && *p_sbr != NUL)
4036 /* draw indent after showbreak value */
4037 draw_state = WL_BRI;
4038 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4039 /* After the showbreak, draw the breakindent */
4040 draw_state = WL_BRI - 1;
4041
4042 /* draw 'breakindent': indent wrapped text accordingly */
4043 if (draw_state == WL_BRI - 1 && n_extra == 0)
4044 {
4045 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004046 /* if need_showbreak is set, breakindent also applies */
4047 if (wp->w_p_bri && n_extra == 0
4048 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004049# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004050 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004051# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004052 )
4053 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004054 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004055# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004056 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004057 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004058 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004059# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004060 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4061 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004062 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004063# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004064 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004065# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004066 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004067 c_extra = ' ';
4068 n_extra = get_breakindent_win(wp,
4069 ml_get_buf(wp->w_buffer, lnum, FALSE));
4070 /* Correct end of highlighted area for 'breakindent',
4071 * required when 'linebreak' is also set. */
4072 if (tocol == vcol)
4073 tocol += n_extra;
4074 }
4075 }
4076#endif
4077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4079 if (draw_state == WL_SBR - 1 && n_extra == 0)
4080 {
4081 draw_state = WL_SBR;
4082# ifdef FEAT_DIFF
4083 if (filler_todo > 0)
4084 {
4085 /* Draw "deleted" diff line(s). */
4086 if (char2cells(fill_diff) > 1)
4087 c_extra = '-';
4088 else
4089 c_extra = fill_diff;
4090# ifdef FEAT_RIGHTLEFT
4091 if (wp->w_p_rl)
4092 n_extra = col + 1;
4093 else
4094# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004095 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004096 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098# endif
4099# ifdef FEAT_LINEBREAK
4100 if (*p_sbr != NUL && need_showbreak)
4101 {
4102 /* Draw 'showbreak' at the start of each broken line. */
4103 p_extra = p_sbr;
4104 c_extra = NUL;
4105 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004106 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004108 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 /* Correct end of highlighted area for 'showbreak',
4110 * required when 'linebreak' is also set. */
4111 if (tocol == vcol)
4112 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004113#ifdef FEAT_SYN_HL
4114 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004115 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004116 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004117 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120# endif
4121 }
4122#endif
4123
4124 if (draw_state == WL_LINE - 1 && n_extra == 0)
4125 {
4126 draw_state = WL_LINE;
4127 if (saved_n_extra)
4128 {
4129 /* Continue item from end of wrapped line. */
4130 n_extra = saved_n_extra;
4131 c_extra = saved_c_extra;
4132 p_extra = saved_p_extra;
4133 char_attr = saved_char_attr;
4134 }
4135 else
4136 char_attr = 0;
4137 }
4138 }
4139
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004140 // When still displaying '$' of change command, stop at cursor.
4141 // When only displaying the (relative) line number and that's done,
4142 // stop here.
4143 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004144 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145#ifdef FEAT_DIFF
4146 && filler_todo <= 0
4147#endif
4148 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004149 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004151 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004152 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004153 /* Pretend we have finished updating the window. Except when
4154 * 'cursorcolumn' is set. */
4155#ifdef FEAT_SYN_HL
4156 if (wp->w_p_cuc)
4157 row = wp->w_cline_row + wp->w_cline_height;
4158 else
4159#endif
4160 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 break;
4162 }
4163
4164 if (draw_state == WL_LINE && area_highlighting)
4165 {
4166 /* handle Visual or match highlighting in this line */
4167 if (vcol == fromcol
4168#ifdef FEAT_MBYTE
4169 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4170 && (*mb_ptr2cells)(ptr) > 1)
4171#endif
4172 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004173 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 && vcol < tocol))
4175 area_attr = attr; /* start highlighting */
4176 else if (area_attr != 0
4177 && (vcol == tocol
4178 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181#ifdef FEAT_SEARCH_EXTRA
4182 if (!n_extra)
4183 {
4184 /*
4185 * Check for start/end of search pattern match.
4186 * After end, check for start/end of next match.
4187 * When another match, have to check for start again.
4188 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004189 * Do this for 'search_hl' and the match list (ordered by
4190 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004192 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004193 cur = wp->w_match_head;
4194 shl_flag = FALSE;
4195 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004197 if (shl_flag == FALSE
4198 && ((cur != NULL
4199 && cur->priority > SEARCH_HL_PRIORITY)
4200 || cur == NULL))
4201 {
4202 shl = &search_hl;
4203 shl_flag = TRUE;
4204 }
4205 else
4206 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004207 if (cur != NULL)
4208 cur->pos.cur = 0;
4209 pos_inprogress = TRUE;
4210 while (shl->rm.regprog != NULL
4211 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004213 if (shl->startcol != MAXCOL
4214 && v >= (long)shl->startcol
4215 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004217#ifdef FEAT_MBYTE
4218 int tmp_col = v + MB_PTR2LEN(ptr);
4219
4220 if (shl->endcol < tmp_col)
4221 shl->endcol = tmp_col;
4222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004224#ifdef FEAT_CONCEAL
4225 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4226 == cur->hlg_id)
4227 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004228 has_match_conc =
4229 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004230 match_conc = cur->conceal_char;
4231 }
4232 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004233 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004236 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
4238 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004239 next_search_hl(wp, shl, lnum, (colnr_T)v,
4240 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004241 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004242 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /* Need to get the line again, a multi-line regexp
4245 * may have made it invalid. */
4246 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4247 ptr = line + v;
4248
4249 if (shl->lnum == lnum)
4250 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004251 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004253 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004255 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004257 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
4259 /* highlight empty match, try again after
4260 * it */
4261#ifdef FEAT_MBYTE
4262 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004263 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004264 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 else
4266#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004267 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269
4270 /* Loop to check if the match starts at the
4271 * current position */
4272 continue;
4273 }
4274 }
4275 break;
4276 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004277 if (shl != &search_hl && cur != NULL)
4278 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004280
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004281 /* Use attributes from match with highest priority among
4282 * 'search_hl' and the match list. */
4283 search_attr = search_hl.attr_cur;
4284 cur = wp->w_match_head;
4285 shl_flag = FALSE;
4286 while (cur != NULL || shl_flag == FALSE)
4287 {
4288 if (shl_flag == FALSE
4289 && ((cur != NULL
4290 && cur->priority > SEARCH_HL_PRIORITY)
4291 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004292 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004293 shl = &search_hl;
4294 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004295 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004296 else
4297 shl = &cur->hl;
4298 if (shl->attr_cur != 0)
4299 search_attr = shl->attr_cur;
4300 if (shl != &search_hl && cur != NULL)
4301 cur = cur->next;
4302 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004303 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004304 if (*ptr == NUL && (did_line_attr >= 1
4305 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004306 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308#endif
4309
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004311 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004313 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4314 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004316 if (diff_hlf == HLF_TXD && ptr - line > change_end
4317 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004319 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004320 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004321 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004324
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004325#ifdef FEAT_TEXT_PROP
4326 if (text_props != NULL)
4327 {
4328 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004329 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004330
4331 // Check if any active property ends.
4332 for (pi = 0; pi < text_props_active; ++pi)
4333 {
4334 int tpi = text_prop_idxs[pi];
4335
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004336 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004337 + text_props[tpi].tp_len)
4338 {
4339 if (pi + 1 < text_props_active)
4340 mch_memmove(text_prop_idxs + pi,
4341 text_prop_idxs + pi + 1,
4342 sizeof(int)
4343 * (text_props_active - (pi + 1)));
4344 --text_props_active;
4345 --pi;
4346 }
4347 }
4348
4349 // Add any text property that starts in this column.
4350 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004351 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004352 text_prop_idxs[text_props_active++] = text_prop_next++;
4353
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004354 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004355 if (text_props_active > 0)
4356 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004357 // Sort the properties on priority and/or starting last.
4358 // Then combine the attributes, highest priority last.
4359 current_text_props = text_props;
4360 current_buf = wp->w_buffer;
4361 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4362 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004363
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004364 for (pi = 0; pi < text_props_active; ++pi)
4365 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004366 int tpi = text_prop_idxs[pi];
4367 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004368
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004369 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004370 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004371 int pt_attr = syn_id2attr(pt->pt_hl_id);
4372
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004373 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004374 if (text_prop_attr == 0)
4375 text_prop_attr = pt_attr;
4376 else
4377 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004378 }
4379 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004380 }
4381 }
4382#endif
4383
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004384 /* Decide which of the highlight attributes to use. */
4385 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004386#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004387 if (area_attr != 0)
4388 char_attr = hl_combine_attr(line_attr, area_attr);
4389 else if (search_attr != 0)
4390 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004391 /* Use line_attr when not in the Visual or 'incsearch' area
4392 * (area_attr may be 0 when "noinvcur" is set). */
4393 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004394 || vcol < fromcol || vcol_prev < fromcol_prev
4395 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004396 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004397#else
4398 if (area_attr != 0)
4399 char_attr = area_attr;
4400 else if (search_attr != 0)
4401 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004402#endif
4403 else
4404 {
4405 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004406#ifdef FEAT_TEXT_PROP
4407 if (text_prop_type != NULL)
4408 char_attr = text_prop_attr;
4409 else
4410#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004411#ifdef FEAT_SYN_HL
4412 if (has_syntax)
4413 char_attr = syntax_attr;
4414 else
4415#endif
4416 char_attr = 0;
4417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419
4420 /*
4421 * Get the next character to put on the screen.
4422 */
4423 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004424 * The "p_extra" points to the extra stuff that is inserted to
4425 * represent special characters (non-printable stuff) and other
4426 * things. When all characters are the same, c_extra is used.
4427 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4428 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4430 */
4431 if (n_extra > 0)
4432 {
4433 if (c_extra != NUL)
4434 {
4435 c = c_extra;
4436#ifdef FEAT_MBYTE
4437 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004438 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
4440 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004441 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004442 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 else
4445 mb_utf8 = FALSE;
4446#endif
4447 }
4448 else
4449 {
4450 c = *p_extra;
4451#ifdef FEAT_MBYTE
4452 if (has_mbyte)
4453 {
4454 mb_c = c;
4455 if (enc_utf8)
4456 {
4457 /* If the UTF-8 character is more than one byte:
4458 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004459 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 mb_utf8 = FALSE;
4461 if (mb_l > n_extra)
4462 mb_l = 1;
4463 else if (mb_l > 1)
4464 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004465 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004467 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 }
4470 else
4471 {
4472 /* if this is a DBCS character, put it in "mb_c" */
4473 mb_l = MB_BYTE2LEN(c);
4474 if (mb_l >= n_extra)
4475 mb_l = 1;
4476 else if (mb_l > 1)
4477 mb_c = (c << 8) + p_extra[1];
4478 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004479 if (mb_l == 0) /* at the NUL at end-of-line */
4480 mb_l = 1;
4481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 /* If a double-width char doesn't fit display a '>' in the
4483 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004484 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485# ifdef FEAT_RIGHTLEFT
4486 wp->w_p_rl ? (col <= 0) :
4487# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004488 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 && (*mb_char2cells)(mb_c) == 2)
4490 {
4491 c = '>';
4492 mb_c = c;
4493 mb_l = 1;
4494 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004495 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 /* put the pointer back to output the double-width
4497 * character at the start of the next line. */
4498 ++n_extra;
4499 --p_extra;
4500 }
4501 else
4502 {
4503 n_extra -= mb_l - 1;
4504 p_extra += mb_l - 1;
4505 }
4506 }
4507#endif
4508 ++p_extra;
4509 }
4510 --n_extra;
4511 }
4512 else
4513 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004514#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004515 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004516#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004517
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004518 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004519 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 /*
4521 * Get a character from the line itself.
4522 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004523 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004524#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004525 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#ifdef FEAT_MBYTE
4528 if (has_mbyte)
4529 {
4530 mb_c = c;
4531 if (enc_utf8)
4532 {
4533 /* If the UTF-8 character is more than one byte: Decode it
4534 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004535 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 mb_utf8 = FALSE;
4537 if (mb_l > 1)
4538 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004539 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 /* Overlong encoded ASCII or ASCII with composing char
4541 * is displayed normally, except a NUL. */
4542 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004543 {
4544 c = mb_c;
4545# ifdef FEAT_LINEBREAK
4546 c0 = mb_c;
4547# endif
4548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004550
4551 /* At start of the line we can have a composing char.
4552 * Draw it as a space with a composing char. */
4553 if (utf_iscomposing(mb_c))
4554 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004555 int i;
4556
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004557 for (i = Screen_mco - 1; i > 0; --i)
4558 u8cc[i] = u8cc[i - 1];
4559 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004560 mb_c = ' ';
4561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563
4564 if ((mb_l == 1 && c >= 0x80)
4565 || (mb_l >= 1 && mb_c == 0)
4566 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004567# ifdef UNICODE16
4568 || mb_c >= 0x10000
4569# endif
4570 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
4572 /*
4573 * Illegal UTF-8 byte: display as <xx>.
4574 * Non-BMP character : display as ? or fullwidth ?.
4575 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004576# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
4580 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004581# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 if (wp->w_p_rl) /* reverse */
4583 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004586# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 else if (utf_char2cells(mb_c) != 2)
4588 STRCPY(extra, "?");
4589 else
4590 /* 0xff1f in UTF-8: full-width '?' */
4591 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 p_extra = extra;
4595 c = *p_extra;
4596 mb_c = mb_ptr2char_adv(&p_extra);
4597 mb_utf8 = (c >= 0x80);
4598 n_extra = (int)STRLEN(p_extra);
4599 c_extra = NUL;
4600 if (area_attr == 0 && search_attr == 0)
4601 {
4602 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004603 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 saved_attr2 = char_attr; /* save current attr */
4605 }
4606 }
4607 else if (mb_l == 0) /* at the NUL at end-of-line */
4608 mb_l = 1;
4609#ifdef FEAT_ARABIC
4610 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4611 {
4612 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004613 int pc, pc1, nc;
4614 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 /* The idea of what is the previous and next
4617 * character depends on 'rightleft'. */
4618 if (wp->w_p_rl)
4619 {
4620 pc = prev_c;
4621 pc1 = prev_c1;
4622 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004623 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625 else
4626 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004627 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004629 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631 prev_c = mb_c;
4632
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004633 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635 else
4636 prev_c = mb_c;
4637#endif
4638 }
4639 else /* enc_dbcs */
4640 {
4641 mb_l = MB_BYTE2LEN(c);
4642 if (mb_l == 0) /* at the NUL at end-of-line */
4643 mb_l = 1;
4644 else if (mb_l > 1)
4645 {
4646 /* We assume a second byte below 32 is illegal.
4647 * Hopefully this is OK for all double-byte encodings!
4648 */
4649 if (ptr[1] >= 32)
4650 mb_c = (c << 8) + ptr[1];
4651 else
4652 {
4653 if (ptr[1] == NUL)
4654 {
4655 /* head byte at end of line */
4656 mb_l = 1;
4657 transchar_nonprint(extra, c);
4658 }
4659 else
4660 {
4661 /* illegal tail byte */
4662 mb_l = 2;
4663 STRCPY(extra, "XX");
4664 }
4665 p_extra = extra;
4666 n_extra = (int)STRLEN(extra) - 1;
4667 c_extra = NUL;
4668 c = *p_extra++;
4669 if (area_attr == 0 && search_attr == 0)
4670 {
4671 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004672 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 saved_attr2 = char_attr; /* save current attr */
4674 }
4675 mb_c = c;
4676 }
4677 }
4678 }
4679 /* If a double-width char doesn't fit display a '>' in the
4680 * last column; the character is displayed at the start of the
4681 * next line. */
4682 if ((
4683# ifdef FEAT_RIGHTLEFT
4684 wp->w_p_rl ? (col <= 0) :
4685# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004686 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 && (*mb_char2cells)(mb_c) == 2)
4688 {
4689 c = '>';
4690 mb_c = c;
4691 mb_utf8 = FALSE;
4692 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004693 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 /* Put pointer back so that the character will be
4695 * displayed at the start of the next line. */
4696 --ptr;
4697 }
4698 else if (*ptr != NUL)
4699 ptr += mb_l - 1;
4700
4701 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004702 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004703 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004704 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004707 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 c = ' ';
4709 if (area_attr == 0 && search_attr == 0)
4710 {
4711 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004712 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 saved_attr2 = char_attr; /* save current attr */
4714 }
4715 mb_c = c;
4716 mb_utf8 = FALSE;
4717 mb_l = 1;
4718 }
4719
4720 }
4721#endif
4722 ++ptr;
4723
4724 if (extra_check)
4725 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004726#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004727 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004728#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004729
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004730#ifdef FEAT_TERMINAL
4731 if (get_term_attr)
4732 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004733 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004734
4735 if (!attr_pri)
4736 char_attr = syntax_attr;
4737 else
4738 char_attr = hl_combine_attr(syntax_attr, char_attr);
4739 }
4740#endif
4741
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004742#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004743 // Get syntax attribute, unless still at the start of the line
4744 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004745 v = (long)(ptr - line);
4746 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 /* Get the syntax attribute for the character. If there
4749 * is an error, disable syntax highlighting. */
4750 save_did_emsg = did_emsg;
4751 did_emsg = FALSE;
4752
Bram Moolenaar217ad922005-03-20 22:37:15 +00004753 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004754# ifdef FEAT_SPELL
4755 has_spell ? &can_spell :
4756# endif
4757 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
4759 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004760 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004761 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004762 has_syntax = FALSE;
4763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 else
4765 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004766#ifdef SYN_TIME_LIMIT
4767 if (wp->w_s->b_syn_slow)
4768 has_syntax = FALSE;
4769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
4771 /* Need to get the line again, a multi-line regexp may
4772 * have made it invalid. */
4773 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4774 ptr = line + v;
4775
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004776# ifdef FEAT_TEXT_PROP
4777 // Text properties overrule syntax highlighting.
4778 if (text_prop_attr == 0)
4779#endif
4780 {
4781 if (!attr_pri)
4782 char_attr = syntax_attr;
4783 else
4784 char_attr = hl_combine_attr(syntax_attr, char_attr);
4785 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004786# ifdef FEAT_CONCEAL
4787 /* no concealing past the end of the line, it interferes
4788 * with line highlighting */
4789 if (c == NUL)
4790 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004791 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004792 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004795#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004796
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004797#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004798 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004799 * Only do this when there is no syntax highlighting, the
4800 * @Spell cluster is not used or the current syntax item
4801 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004802 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004803 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004804 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004805 if (c != 0 && (
4806# ifdef FEAT_SYN_HL
4807 !has_syntax ||
4808# endif
4809 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004810 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004811 char_u *prev_ptr, *p;
4812 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004813 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004814# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004815 if (has_mbyte)
4816 {
4817 prev_ptr = ptr - mb_l;
4818 v -= mb_l - 1;
4819 }
4820 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004821# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004822 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004823
4824 /* Use nextline[] if possible, it has the start of the
4825 * next line concatenated. */
4826 if ((prev_ptr - line) - nextlinecol >= 0)
4827 p = nextline + (prev_ptr - line) - nextlinecol;
4828 else
4829 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004830 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004831 len = spell_check(wp, p, &spell_hlf, &cap_col,
4832 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004833 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004834
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004835 /* In Insert mode only highlight a word that
4836 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004837 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004838 && (State & INSERT) != 0
4839 && wp->w_cursor.lnum == lnum
4840 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004841 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004842 && wp->w_cursor.col < (colnr_T)word_end)
4843 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004844 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004845 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004846 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004847
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004848 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004849 && (p - nextline) + len > nextline_idx)
4850 {
4851 /* Remember that the good word continues at the
4852 * start of the next line. */
4853 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004854 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004855 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004856
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004857 /* Turn index into actual attributes. */
4858 if (spell_hlf != HLF_COUNT)
4859 spell_attr = highlight_attr[spell_hlf];
4860
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004861 if (cap_col > 0)
4862 {
4863 if (p != prev_ptr
4864 && (p - nextline) + cap_col >= nextline_idx)
4865 {
4866 /* Remember that the word in the next line
4867 * must start with a capital. */
4868 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004869 cap_col = (int)((p - nextline) + cap_col
4870 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004871 }
4872 else
4873 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004874 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004875 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004876 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004877 }
4878 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004879 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004880 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004881 char_attr = hl_combine_attr(char_attr, spell_attr);
4882 else
4883 char_attr = hl_combine_attr(spell_attr, char_attr);
4884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885#endif
4886#ifdef FEAT_LINEBREAK
4887 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004888 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004890 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004891 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004893# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004894 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004895# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004896 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004898 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004900 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004901
Bram Moolenaar597a4222014-06-25 14:39:50 +02004902 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004903 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004904 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004905 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004906# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004907 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004908 wp->w_buffer->b_p_vts_array) - 1;
4909# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004910 n_extra = (int)wp->w_buffer->b_p_ts
4911 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004912# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004913
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004914# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004915 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004916# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004918# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004919 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004920 {
4921#ifdef FEAT_CONCEAL
4922 if (c == TAB)
4923 /* See "Tab alignment" below. */
4924 FIX_FOR_BOGUSCOLS;
4925#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004926 if (!wp->w_p_list)
4927 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930#endif
4931
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004932 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4933 */
4934 if (wp->w_p_list
4935 && (((c == 160
4936#ifdef FEAT_MBYTE
4937 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4938#endif
4939 ) && lcs_nbsp)
4940 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4941 {
4942 c = (c == ' ') ? lcs_space : lcs_nbsp;
4943 if (area_attr == 0 && search_attr == 0)
4944 {
4945 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004946 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004947 saved_attr2 = char_attr; /* save current attr */
4948 }
4949#ifdef FEAT_MBYTE
4950 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004951 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004952 {
4953 mb_utf8 = TRUE;
4954 u8cc[0] = 0;
4955 c = 0xc0;
4956 }
4957 else
4958 mb_utf8 = FALSE;
4959#endif
4960 }
4961
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4963 {
4964 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004965 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004968 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 saved_attr2 = char_attr; /* save current attr */
4970 }
4971#ifdef FEAT_MBYTE
4972 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004973 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
4975 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004976 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004977 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979 else
4980 mb_utf8 = FALSE;
4981#endif
4982 }
4983 }
4984
4985 /*
4986 * Handling of non-printable characters.
4987 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004988 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 /*
4991 * when getting a character from the file, we may have to
4992 * turn it into something else on the way to putting it
4993 * into "ScreenLines".
4994 */
4995 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4996 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004997 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004998 long vcol_adjusted = vcol; /* removed showbreak length */
4999#ifdef FEAT_LINEBREAK
5000 /* only adjust the tab_len, when at the first column
5001 * after the showbreak value was drawn */
5002 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5003 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005006#ifdef FEAT_VARTABS
5007 tab_len = tabstop_padding(vcol_adjusted,
5008 wp->w_buffer->b_p_ts,
5009 wp->w_buffer->b_p_vts_array) - 1;
5010#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005011 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005012 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5013#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005014
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005015#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005016 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005017#endif
5018 /* tab amount depends on current column */
5019 n_extra = tab_len;
5020#ifdef FEAT_LINEBREAK
5021 else
5022 {
5023 char_u *p;
5024 int len = n_extra;
5025 int i;
5026 int saved_nextra = n_extra;
5027
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005028#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005029 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005030 /* there are characters to conceal */
5031 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005032 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5033 */
5034 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5035 && n_extra > tab_len)
5036 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005037#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005038
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005039 /* if n_extra > 0, it gives the number of chars, to
5040 * use for a tab, else we need to calculate the width
5041 * for a tab */
5042#ifdef FEAT_MBYTE
5043 len = (tab_len * mb_char2len(lcs_tab2));
5044 if (n_extra > 0)
5045 len += n_extra - tab_len;
5046#endif
5047 c = lcs_tab1;
5048 p = alloc((unsigned)(len + 1));
5049 vim_memset(p, ' ', len);
5050 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005051 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005052 p_extra_free = p;
5053 for (i = 0; i < tab_len; i++)
5054 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005055 if (*p == NUL)
5056 {
5057 tab_len = i;
5058 break;
5059 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005060#ifdef FEAT_MBYTE
5061 mb_char2bytes(lcs_tab2, p);
5062 p += mb_char2len(lcs_tab2);
5063 n_extra += mb_char2len(lcs_tab2)
5064 - (saved_nextra > 0 ? 1 : 0);
5065#else
5066 p[i] = lcs_tab2;
5067#endif
5068 }
5069 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005070#ifdef FEAT_CONCEAL
5071 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5072 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005073 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005074 n_extra -= vcol_off;
5075#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005076 }
5077#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005078#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005079 {
5080 int vc_saved = vcol_off;
5081
5082 /* Tab alignment should be identical regardless of
5083 * 'conceallevel' value. So tab compensates of all
5084 * previous concealed characters, and thus resets
5085 * vcol_off and boguscols accumulated so far in the
5086 * line. Note that the tab can be longer than
5087 * 'tabstop' when there are concealed characters. */
5088 FIX_FOR_BOGUSCOLS;
5089
5090 /* Make sure, the highlighting for the tab char will be
5091 * correctly set further below (effectively reverts the
5092 * FIX_FOR_BOGSUCOLS macro */
5093 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005094 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005095 tab_len += vc_saved;
5096 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098#ifdef FEAT_MBYTE
5099 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5100#endif
5101 if (wp->w_p_list)
5102 {
5103 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005104#ifdef FEAT_LINEBREAK
5105 if (wp->w_p_lbr)
5106 c_extra = NUL; /* using p_extra from above */
5107 else
5108#endif
5109 c_extra = lcs_tab2;
5110 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005111 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 saved_attr2 = char_attr; /* save current attr */
5113#ifdef FEAT_MBYTE
5114 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005115 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005118 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005119 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121#endif
5122 }
5123 else
5124 {
5125 c_extra = ' ';
5126 c = ' ';
5127 }
5128 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005129 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005130 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005131 || ((fromcol >= 0 || fromcol_prev >= 0)
5132 && tocol > vcol
5133 && VIsual_mode != Ctrl_V
5134 && (
5135# ifdef FEAT_RIGHTLEFT
5136 wp->w_p_rl ? (col >= 0) :
5137# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005138 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005139 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005140 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005141 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005142 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005144 /* Display a '$' after the line or highlight an extra
5145 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5147 /* For a diff line the highlighting continues after the
5148 * "$". */
5149 if (
5150# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005151 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152# ifdef LINE_ATTR
5153 &&
5154# endif
5155# endif
5156# ifdef LINE_ATTR
5157 line_attr == 0
5158# endif
5159 )
5160#endif
5161 {
5162#ifdef FEAT_VIRTUALEDIT
5163 /* In virtualedit, visual selections may extend
5164 * beyond end of line. */
5165 if (area_highlighting && virtual_active()
5166 && tocol != MAXCOL && vcol < tocol)
5167 n_extra = 0;
5168 else
5169#endif
5170 {
5171 p_extra = at_end_str;
5172 n_extra = 1;
5173 c_extra = NUL;
5174 }
5175 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005176 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005177 c = lcs_eol;
5178 else
5179 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 lcs_eol_one = -1;
5181 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005182 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005184 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 n_attr = 1;
5186 }
5187#ifdef FEAT_MBYTE
5188 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005189 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
5191 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005192 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005193 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 else
5196 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5197#endif
5198 }
5199 else if (c != NUL)
5200 {
5201 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005202 if (n_extra == 0)
5203 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204#ifdef FEAT_RIGHTLEFT
5205 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5206 rl_mirror(p_extra); /* reverse "<12>" */
5207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005209#ifdef FEAT_LINEBREAK
5210 if (wp->w_p_lbr)
5211 {
5212 char_u *p;
5213
5214 c = *p_extra;
5215 p = alloc((unsigned)n_extra + 1);
5216 vim_memset(p, ' ', n_extra);
5217 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5218 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005219 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005220 p_extra_free = p_extra = p;
5221 }
5222 else
5223#endif
5224 {
5225 n_extra = byte2cells(c) - 1;
5226 c = *p_extra++;
5227 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005228 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005231 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 saved_attr2 = char_attr; /* save current attr */
5233 }
5234#ifdef FEAT_MBYTE
5235 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5236#endif
5237 }
5238#ifdef FEAT_VIRTUALEDIT
5239 else if (VIsual_active
5240 && (VIsual_mode == Ctrl_V
5241 || VIsual_mode == 'v')
5242 && virtual_active()
5243 && tocol != MAXCOL
5244 && vcol < tocol
5245 && (
5246# ifdef FEAT_RIGHTLEFT
5247 wp->w_p_rl ? (col >= 0) :
5248# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005249 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 c = ' ';
5252 --ptr; /* put it back at the NUL */
5253 }
5254#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005255#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 else if ((
5257# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005258 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005260# ifdef FEAT_TERMINAL
5261 term_attr != 0 ||
5262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 ) && (
5265# ifdef FEAT_RIGHTLEFT
5266 wp->w_p_rl ? (col >= 0) :
5267# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005268 (col
5269# ifdef FEAT_CONCEAL
5270 - boguscols
5271# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005272 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 /* Highlight until the right side of the window */
5275 c = ' ';
5276 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005277
5278 /* Remember we do the char for line highlighting. */
5279 ++did_line_attr;
5280
5281 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005282 if (line_attr != 0 && char_attr == search_attr
5283 && (did_line_attr > 1
5284 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005285 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286# ifdef FEAT_DIFF
5287 if (diff_hlf == HLF_TXD)
5288 {
5289 diff_hlf = HLF_CHD;
5290 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005291 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005292 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005293 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5294 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005295 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
5298# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005299# ifdef FEAT_TERMINAL
5300 if (term_attr != 0)
5301 {
5302 char_attr = term_attr;
5303 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5304 char_attr = hl_combine_attr(char_attr,
5305 HL_ATTR(HLF_CUL));
5306 }
5307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309#endif
5310 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005311
5312#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005313 if ( wp->w_p_cole > 0
5314 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005315 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005316 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005317 && !(lnum_in_visual_area
5318 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005319 {
5320 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005321 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005322 && (syn_get_sub_char() != NUL || match_conc
5323 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005324 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005325 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005326 /* First time at this concealed item: display one
5327 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005328 if (match_conc)
5329 c = match_conc;
5330 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005331 c = syn_get_sub_char();
5332 else if (lcs_conceal != NUL)
5333 c = lcs_conceal;
5334 else
5335 c = ' ';
5336
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005337 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005338
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005339 if (n_extra > 0)
5340 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005341 vcol += n_extra;
5342 if (wp->w_p_wrap && n_extra > 0)
5343 {
5344# ifdef FEAT_RIGHTLEFT
5345 if (wp->w_p_rl)
5346 {
5347 col -= n_extra;
5348 boguscols -= n_extra;
5349 }
5350 else
5351# endif
5352 {
5353 boguscols += n_extra;
5354 col += n_extra;
5355 }
5356 }
5357 n_extra = 0;
5358 n_attr = 0;
5359 }
5360 else if (n_skip == 0)
5361 {
5362 is_concealing = TRUE;
5363 n_skip = 1;
5364 }
5365# ifdef FEAT_MBYTE
5366 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005367 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005368 {
5369 mb_utf8 = TRUE;
5370 u8cc[0] = 0;
5371 c = 0xc0;
5372 }
5373 else
5374 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5375# endif
5376 }
5377 else
5378 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005379 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005380 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005381 }
5382#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005385#ifdef FEAT_CONCEAL
5386 /* In the cursor line and we may be concealing characters: correct
5387 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005388 if (!did_wcol && draw_state == WL_LINE
5389 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005390 && conceal_cursor_line(wp)
5391 && (int)wp->w_virtcol <= vcol + n_skip)
5392 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005393# ifdef FEAT_RIGHTLEFT
5394 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005395 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005396 else
5397# endif
5398 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005399 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005400 did_wcol = TRUE;
5401 }
5402#endif
5403
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 /* Don't override visual selection highlighting. */
5405 if (n_attr > 0
5406 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005407 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 char_attr = extra_attr;
5409
Bram Moolenaar81695252004-12-29 20:58:21 +00005410#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 /* XIM don't send preedit_start and preedit_end, but they send
5412 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5413 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005414 if (p_imst == IM_ON_THE_SPOT
5415 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005416 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 && (State & INSERT)
5418 && !p_imdisable
5419 && im_is_preediting()
5420 && draw_state == WL_LINE)
5421 {
5422 colnr_T tcol;
5423
5424 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005425 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 else
5427 tcol = preedit_end_col;
5428 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5429 {
5430 if (feedback_old_attr < 0)
5431 {
5432 feedback_col = 0;
5433 feedback_old_attr = char_attr;
5434 }
5435 char_attr = im_get_feedback_attr(feedback_col);
5436 if (char_attr < 0)
5437 char_attr = feedback_old_attr;
5438 feedback_col++;
5439 }
5440 else if (feedback_old_attr >= 0)
5441 {
5442 char_attr = feedback_old_attr;
5443 feedback_old_attr = -1;
5444 feedback_col = 0;
5445 }
5446 }
5447#endif
5448 /*
5449 * Handle the case where we are in column 0 but not on the first
5450 * character of the line and the user wants us to show us a
5451 * special character (via 'listchars' option "precedes:<char>".
5452 */
5453 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005454 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5456#ifdef FEAT_DIFF
5457 && filler_todo <= 0
5458#endif
5459 && draw_state > WL_NR
5460 && c != NUL)
5461 {
5462 c = lcs_prec;
5463 lcs_prec_todo = NUL;
5464#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005465 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5466 {
5467 /* Double-width character being overwritten by the "precedes"
5468 * character, need to fill up half the character. */
5469 c_extra = MB_FILLER_CHAR;
5470 n_extra = 1;
5471 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005472 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005475 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
5477 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005478 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005479 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
5481 else
5482 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5483#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005484 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 {
5486 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005487 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 n_attr3 = 1;
5489 }
5490 }
5491
5492 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005493 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005495 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005496#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005497 || did_line_attr == 1
5498#endif
5499 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005501#ifdef FEAT_SEARCH_EXTRA
5502 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005503
5504 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005505 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005506 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005507#endif
5508
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005509 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 * highlight match at end of line. If it's beyond the last
5511 * char on the screen, just overwrite that one (tricky!) Not
5512 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005513#ifdef FEAT_SEARCH_EXTRA
5514 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005515 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005516 prevcol_hl_flag = TRUE;
5517 else
5518 {
5519 cur = wp->w_match_head;
5520 while (cur != NULL)
5521 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005522 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005523 {
5524 prevcol_hl_flag = TRUE;
5525 break;
5526 }
5527 cur = cur->next;
5528 }
5529 }
5530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005532 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005533 && (VIsual_mode != Ctrl_V
5534 || lnum == VIsual.lnum
5535 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005536 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537#ifdef FEAT_SEARCH_EXTRA
5538 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005539 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005540# ifdef FEAT_SYN_HL
5541 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5542 && !(wp == curwin && VIsual_active))
5543# endif
5544# ifdef FEAT_DIFF
5545 && diff_hlf == (hlf_T)0
5546# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005547# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005548 && did_line_attr <= 1
5549# endif
5550 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551#endif
5552 ))
5553 {
5554 int n = 0;
5555
5556#ifdef FEAT_RIGHTLEFT
5557 if (wp->w_p_rl)
5558 {
5559 if (col < 0)
5560 n = 1;
5561 }
5562 else
5563#endif
5564 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005565 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 n = -1;
5567 }
5568 if (n != 0)
5569 {
5570 /* At the window boundary, highlight the last character
5571 * instead (better than nothing). */
5572 off += n;
5573 col += n;
5574 }
5575 else
5576 {
5577 /* Add a blank character to highlight. */
5578 ScreenLines[off] = ' ';
5579#ifdef FEAT_MBYTE
5580 if (enc_utf8)
5581 ScreenLinesUC[off] = 0;
5582#endif
5583 }
5584#ifdef FEAT_SEARCH_EXTRA
5585 if (area_attr == 0)
5586 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005587 /* Use attributes from match with highest priority among
5588 * 'search_hl' and the match list. */
5589 char_attr = search_hl.attr;
5590 cur = wp->w_match_head;
5591 shl_flag = FALSE;
5592 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005593 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005594 if (shl_flag == FALSE
5595 && ((cur != NULL
5596 && cur->priority > SEARCH_HL_PRIORITY)
5597 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005598 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005599 shl = &search_hl;
5600 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005601 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005602 else
5603 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005604 if ((ptr - line) - 1 == (long)shl->startcol
5605 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005606 char_attr = shl->attr;
5607 if (shl != &search_hl && cur != NULL)
5608 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
5611#endif
5612 ScreenAttrs[off] = char_attr;
5613#ifdef FEAT_RIGHTLEFT
5614 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005617 --off;
5618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 else
5620#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005623 ++off;
5624 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005625 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005626#ifdef FEAT_SYN_HL
5627 eol_hl_off = 1;
5628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Bram Moolenaar91170f82006-05-05 21:15:17 +00005632 /*
5633 * At end of the text line.
5634 */
5635 if (c == NUL)
5636 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005637#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005638 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005639 if (wp->w_p_wrap)
5640 v = wp->w_skipcol;
5641 else
5642 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005643
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005644 /* check if line ends before left margin */
5645 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005646 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005647#ifdef FEAT_CONCEAL
5648 /* Get rid of the boguscols now, we want to draw until the right
5649 * edge for 'cursorcolumn'. */
5650 col -= boguscols;
5651 boguscols = 0;
5652#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005653
5654 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005655 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005656
5657 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005658 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5659 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005660 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005661 && lnum != wp->w_cursor.lnum)
5662 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005663# ifdef FEAT_RIGHTLEFT
5664 && !wp->w_p_rl
5665# endif
5666 )
5667 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005668 int rightmost_vcol = 0;
5669 int i;
5670
5671 if (wp->w_p_cuc)
5672 rightmost_vcol = wp->w_virtcol;
5673 if (draw_color_col)
5674 /* determine rightmost colorcolumn to possibly draw */
5675 for (i = 0; color_cols[i] >= 0; ++i)
5676 if (rightmost_vcol < color_cols[i])
5677 rightmost_vcol = color_cols[i];
5678
Bram Moolenaar02631462017-09-22 15:20:32 +02005679 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005680 {
5681 ScreenLines[off] = ' ';
5682#ifdef FEAT_MBYTE
5683 if (enc_utf8)
5684 ScreenLinesUC[off] = 0;
5685#endif
5686 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005687 if (draw_color_col)
5688 draw_color_col = advance_color_col(VCOL_HLC,
5689 &color_cols);
5690
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005691 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005692 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005693 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005694 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005695 else
5696 ScreenAttrs[off++] = 0;
5697
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005698 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005699 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005700
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005701 ++vcol;
5702 }
5703 }
5704#endif
5705
Bram Moolenaar53f81742017-09-22 14:35:51 +02005706 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005707 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 row++;
5709
5710 /*
5711 * Update w_cline_height and w_cline_folded if the cursor line was
5712 * updated (saves a call to plines() later).
5713 */
5714 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5715 {
5716 curwin->w_cline_row = startrow;
5717 curwin->w_cline_height = row - startrow;
5718#ifdef FEAT_FOLDING
5719 curwin->w_cline_folded = FALSE;
5720#endif
5721 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5722 }
5723
5724 break;
5725 }
5726
5727 /* line continues beyond line end */
5728 if (lcs_ext
5729 && !wp->w_p_wrap
5730#ifdef FEAT_DIFF
5731 && filler_todo <= 0
5732#endif
5733 && (
5734#ifdef FEAT_RIGHTLEFT
5735 wp->w_p_rl ? col == 0 :
5736#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005737 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005739 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5741 {
5742 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005743 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744#ifdef FEAT_MBYTE
5745 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005746 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005749 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005750 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752 else
5753 mb_utf8 = FALSE;
5754#endif
5755 }
5756
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005757#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005758 /* advance to the next 'colorcolumn' */
5759 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005760 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005761
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005762 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005763 * highlight the cursor position itself.
5764 * Also highlight the 'colorcolumn' if it is different than
5765 * 'cursorcolumn' */
5766 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005767 if (draw_state == WL_LINE && !lnum_in_visual_area
5768 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005769 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005770 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005771 && lnum != wp->w_cursor.lnum)
5772 {
5773 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005774 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005775 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005776 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005777 {
5778 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005779 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005780 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005781 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005782#endif
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 /*
5785 * Store character to be displayed.
5786 * Skip characters that are left of the screen for 'nowrap'.
5787 */
5788 vcol_prev = vcol;
5789 if (draw_state < WL_LINE || n_skip <= 0)
5790 {
5791 /*
5792 * Store the character.
5793 */
5794#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5795 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5796 {
5797 /* A double-wide character is: put first halve in left cell. */
5798 --off;
5799 --col;
5800 }
5801#endif
5802 ScreenLines[off] = c;
5803#ifdef FEAT_MBYTE
5804 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005805 {
5806 if ((mb_c & 0xff00) == 0x8e00)
5807 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 else if (enc_utf8)
5811 {
5812 if (mb_utf8)
5813 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005814 int i;
5815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005817 if ((c & 0xff) == 0)
5818 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005819 for (i = 0; i < Screen_mco; ++i)
5820 {
5821 ScreenLinesC[i][off] = u8cc[i];
5822 if (u8cc[i] == 0)
5823 break;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826 else
5827 ScreenLinesUC[off] = 0;
5828 }
5829 if (multi_attr)
5830 {
5831 ScreenAttrs[off] = multi_attr;
5832 multi_attr = 0;
5833 }
5834 else
5835#endif
5836 ScreenAttrs[off] = char_attr;
5837
5838#ifdef FEAT_MBYTE
5839 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5840 {
5841 /* Need to fill two screen columns. */
5842 ++off;
5843 ++col;
5844 if (enc_utf8)
5845 /* UTF-8: Put a 0 in the second screen char. */
5846 ScreenLines[off] = 0;
5847 else
5848 /* DBCS: Put second byte in the second screen char. */
5849 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005850 if (draw_state > WL_NR
5851#ifdef FEAT_DIFF
5852 && filler_todo <= 0
5853#endif
5854 )
5855 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 /* When "tocol" is halfway a character, set it to the end of
5857 * the character, otherwise highlighting won't stop. */
5858 if (tocol == vcol)
5859 ++tocol;
5860#ifdef FEAT_RIGHTLEFT
5861 if (wp->w_p_rl)
5862 {
5863 /* now it's time to backup one cell */
5864 --off;
5865 --col;
5866 }
5867#endif
5868 }
5869#endif
5870#ifdef FEAT_RIGHTLEFT
5871 if (wp->w_p_rl)
5872 {
5873 --off;
5874 --col;
5875 }
5876 else
5877#endif
5878 {
5879 ++off;
5880 ++col;
5881 }
5882 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005883#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005884 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005885 {
5886 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005887 ++vcol_off;
5888 if (n_extra > 0)
5889 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005890 if (wp->w_p_wrap)
5891 {
5892 /*
5893 * Special voodoo required if 'wrap' is on.
5894 *
5895 * Advance the column indicator to force the line
5896 * drawing to wrap early. This will make the line
5897 * take up the same screen space when parts are concealed,
5898 * so that cursor line computations aren't messed up.
5899 *
5900 * To avoid the fictitious advance of 'col' causing
5901 * trailing junk to be written out of the screen line
5902 * we are building, 'boguscols' keeps track of the number
5903 * of bad columns we have advanced.
5904 */
5905 if (n_extra > 0)
5906 {
5907 vcol += n_extra;
5908# ifdef FEAT_RIGHTLEFT
5909 if (wp->w_p_rl)
5910 {
5911 col -= n_extra;
5912 boguscols -= n_extra;
5913 }
5914 else
5915# endif
5916 {
5917 col += n_extra;
5918 boguscols += n_extra;
5919 }
5920 n_extra = 0;
5921 n_attr = 0;
5922 }
5923
5924
5925# ifdef FEAT_MBYTE
5926 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5927 {
5928 /* Need to fill two screen columns. */
5929# ifdef FEAT_RIGHTLEFT
5930 if (wp->w_p_rl)
5931 {
5932 --boguscols;
5933 --col;
5934 }
5935 else
5936# endif
5937 {
5938 ++boguscols;
5939 ++col;
5940 }
5941 }
5942# endif
5943
5944# ifdef FEAT_RIGHTLEFT
5945 if (wp->w_p_rl)
5946 {
5947 --boguscols;
5948 --col;
5949 }
5950 else
5951# endif
5952 {
5953 ++boguscols;
5954 ++col;
5955 }
5956 }
5957 else
5958 {
5959 if (n_extra > 0)
5960 {
5961 vcol += n_extra;
5962 n_extra = 0;
5963 n_attr = 0;
5964 }
5965 }
5966
5967 }
5968#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 else
5970 --n_skip;
5971
Bram Moolenaar64486672010-05-16 15:46:46 +02005972 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5973 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005974 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975#ifdef FEAT_DIFF
5976 && filler_todo <= 0
5977#endif
5978 )
5979 ++vcol;
5980
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005981#ifdef FEAT_SYN_HL
5982 if (vcol_save_attr >= 0)
5983 char_attr = vcol_save_attr;
5984#endif
5985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 /* restore attributes after "predeces" in 'listchars' */
5987 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5988 char_attr = saved_attr3;
5989
5990 /* restore attributes after last 'listchars' or 'number' char */
5991 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5992 char_attr = saved_attr2;
5993
5994 /*
5995 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005996 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 */
5998 if ((
5999#ifdef FEAT_RIGHTLEFT
6000 wp->w_p_rl ? (col < 0) :
6001#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006002 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 && (*ptr != NUL
6004#ifdef FEAT_DIFF
6005 || filler_todo > 0
6006#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006007 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6009 )
6010 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006011#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006012 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02006013 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006014 boguscols = 0;
6015#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006016 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02006017 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 ++row;
6020 ++screen_row;
6021
6022 /* When not wrapping and finished diff lines, or when displayed
6023 * '$' and highlighting until last column, break here. */
6024 if ((!wp->w_p_wrap
6025#ifdef FEAT_DIFF
6026 && filler_todo <= 0
6027#endif
6028 ) || lcs_eol_one == -1)
6029 break;
6030
6031 /* When the window is too narrow draw all "@" lines. */
6032 if (draw_state != WL_LINE
6033#ifdef FEAT_DIFF
6034 && filler_todo <= 0
6035#endif
6036 )
6037 {
6038 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 row = endrow;
6041 }
6042
6043 /* When line got too long for screen break here. */
6044 if (row == endrow)
6045 {
6046 ++row;
6047 break;
6048 }
6049
6050 if (screen_cur_row == screen_row - 1
6051#ifdef FEAT_DIFF
6052 && filler_todo <= 0
6053#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006054 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 {
6056 /* Remember that the line wraps, used for modeless copy. */
6057 LineWraps[screen_row - 1] = TRUE;
6058
6059 /*
6060 * Special trick to make copy/paste of wrapped lines work with
6061 * xterm/screen: write an extra character beyond the end of
6062 * the line. This will work with all terminal types
6063 * (regardless of the xn,am settings).
6064 * Only do this on a fast tty.
6065 * Only do this if the cursor is on the current line
6066 * (something has been written in it).
6067 * Don't do this for the GUI.
6068 * Don't do this for double-width characters.
6069 * Don't do this for a window not at the right screen border.
6070 */
6071 if (p_tf
6072#ifdef FEAT_GUI
6073 && !gui.in_use
6074#endif
6075#ifdef FEAT_MBYTE
6076 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006077 && ((*mb_off2cells)(LineOffset[screen_row],
6078 LineOffset[screen_row] + screen_Columns)
6079 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006081 + (int)Columns - 2,
6082 LineOffset[screen_row] + screen_Columns)
6083 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084#endif
6085 )
6086 {
6087 /* First make sure we are at the end of the screen line,
6088 * then output the same character again to let the
6089 * terminal know about the wrap. If the terminal doesn't
6090 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006091 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 screen_char(LineOffset[screen_row - 1]
6093 + (unsigned)Columns - 1,
6094 screen_row - 1, (int)(Columns - 1));
6095
6096#ifdef FEAT_MBYTE
6097 /* When there is a multi-byte character, just output a
6098 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006099 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6100 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 out_char(' ');
6102 else
6103#endif
6104 out_char(ScreenLines[LineOffset[screen_row - 1]
6105 + (Columns - 1)]);
6106 /* force a redraw of the first char on the next line */
6107 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6108 screen_start(); /* don't know where cursor is now */
6109 }
6110 }
6111
6112 col = 0;
6113 off = (unsigned)(current_ScreenLine - ScreenLines);
6114#ifdef FEAT_RIGHTLEFT
6115 if (wp->w_p_rl)
6116 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006117 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 off += col;
6119 }
6120#endif
6121
6122 /* reset the drawing state for the start of a wrapped line */
6123 draw_state = WL_START;
6124 saved_n_extra = n_extra;
6125 saved_p_extra = p_extra;
6126 saved_c_extra = c_extra;
6127 saved_char_attr = char_attr;
6128 n_extra = 0;
6129 lcs_prec_todo = lcs_prec;
6130#ifdef FEAT_LINEBREAK
6131# ifdef FEAT_DIFF
6132 if (filler_todo <= 0)
6133# endif
6134 need_showbreak = TRUE;
6135#endif
6136#ifdef FEAT_DIFF
6137 --filler_todo;
6138 /* When the filler lines are actually below the last line of the
6139 * file, don't draw the line itself, break here. */
6140 if (filler_todo == 0 && wp->w_botfill)
6141 break;
6142#endif
6143 }
6144
6145 } /* for every character in the line */
6146
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006147#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006148 /* After an empty line check first word for capital. */
6149 if (*skipwhite(line) == NUL)
6150 {
6151 capcol_lnum = lnum + 1;
6152 cap_col = 0;
6153 }
6154#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006155#ifdef FEAT_TEXT_PROP
6156 vim_free(text_props);
6157 vim_free(text_prop_idxs);
6158#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006159
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006160 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 return row;
6162}
6163
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006164#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006165/*
6166 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006167 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006168 */
6169 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006170comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006171{
6172 int i;
6173
6174 for (i = 0; i < Screen_mco; ++i)
6175 {
6176 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6177 return TRUE;
6178 if (ScreenLinesC[i][off_from] == 0)
6179 break;
6180 }
6181 return FALSE;
6182}
6183#endif
6184
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185/*
6186 * Check whether the given character needs redrawing:
6187 * - the (first byte of the) character is different
6188 * - the attributes are different
6189 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006190 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 */
6192 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006193char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
6195 if (cols > 0
6196 && ((ScreenLines[off_from] != ScreenLines[off_to]
6197 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6198
6199#ifdef FEAT_MBYTE
6200 || (enc_dbcs != 0
6201 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6202 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6203 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6204 : (cols > 1 && ScreenLines[off_from + 1]
6205 != ScreenLines[off_to + 1])))
6206 || (enc_utf8
6207 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6208 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006209 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006210 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6211 && ScreenLines[off_from + 1]
6212 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213#endif
6214 ))
6215 return TRUE;
6216 return FALSE;
6217}
6218
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006219#if defined(FEAT_TERMINAL) || defined(PROTO)
6220/*
6221 * Return the index in ScreenLines[] for the current screen line.
6222 */
6223 int
6224screen_get_current_line_off()
6225{
6226 return (int)(current_ScreenLine - ScreenLines);
6227}
6228#endif
6229
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230/*
6231 * Move one "cooked" screen line to the screen, but only the characters that
6232 * have actually changed. Handle insert/delete character.
6233 * "coloff" gives the first column on the screen for this line.
6234 * "endcol" gives the columns where valid characters are.
6235 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6236 * needs to be cleared, negative otherwise.
6237 * "rlflag" is TRUE in a rightleft window:
6238 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6239 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6240 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006241 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006242screen_line(
6243 int row,
6244 int coloff,
6245 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006246 int clear_width,
6247 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
6249 unsigned off_from;
6250 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006251#ifdef FEAT_MBYTE
6252 unsigned max_off_from;
6253 unsigned max_off_to;
6254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 int force = FALSE; /* force update rest of the line */
6258 int redraw_this /* bool: does character need redraw? */
6259#ifdef FEAT_GUI
6260 = TRUE /* For GUI when while-loop empty */
6261#endif
6262 ;
6263 int redraw_next; /* redraw_this for next character */
6264#ifdef FEAT_MBYTE
6265 int clear_next = FALSE;
6266 int char_cells; /* 1: normal char */
6267 /* 2: occupies two display cells */
6268# define CHAR_CELLS char_cells
6269#else
6270# define CHAR_CELLS 1
6271#endif
6272
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006273 /* Check for illegal row and col, just in case. */
6274 if (row >= Rows)
6275 row = Rows - 1;
6276 if (endcol > Columns)
6277 endcol = Columns;
6278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279# ifdef FEAT_CLIPBOARD
6280 clip_may_clear_selection(row, row);
6281# endif
6282
6283 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6284 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006285#ifdef FEAT_MBYTE
6286 max_off_from = off_from + screen_Columns;
6287 max_off_to = LineOffset[row] + screen_Columns;
6288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
6290#ifdef FEAT_RIGHTLEFT
6291 if (rlflag)
6292 {
6293 /* Clear rest first, because it's left of the text. */
6294 if (clear_width > 0)
6295 {
6296 while (col <= endcol && ScreenLines[off_to] == ' '
6297 && ScreenAttrs[off_to] == 0
6298# ifdef FEAT_MBYTE
6299 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6300# endif
6301 )
6302 {
6303 ++off_to;
6304 ++col;
6305 }
6306 if (col <= endcol)
6307 screen_fill(row, row + 1, col + coloff,
6308 endcol + coloff + 1, ' ', ' ', 0);
6309 }
6310 col = endcol + 1;
6311 off_to = LineOffset[row] + col + coloff;
6312 off_from += col;
6313 endcol = (clear_width > 0 ? clear_width : -clear_width);
6314 }
6315#endif /* FEAT_RIGHTLEFT */
6316
6317 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6318
6319 while (col < endcol)
6320 {
6321#ifdef FEAT_MBYTE
6322 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006323 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 else
6325 char_cells = 1;
6326#endif
6327
6328 redraw_this = redraw_next;
6329 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6330 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6331
6332#ifdef FEAT_GUI
6333 /* If the next character was bold, then redraw the current character to
6334 * remove any pixels that might have spilt over into us. This only
6335 * happens in the GUI.
6336 */
6337 if (redraw_next && gui.in_use)
6338 {
6339 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006340 if (hl > HL_ALL)
6341 hl = syn_attr2attr(hl);
6342 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 redraw_this = TRUE;
6344 }
6345#endif
6346
6347 if (redraw_this)
6348 {
6349 /*
6350 * Special handling when 'xs' termcap flag set (hpterm):
6351 * Attributes for characters are stored at the position where the
6352 * cursor is when writing the highlighting code. The
6353 * start-highlighting code must be written with the cursor on the
6354 * first highlighted character. The stop-highlighting code must
6355 * be written with the cursor just after the last highlighted
6356 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006357 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 * to clear the rest of the line, and force redrawing it
6359 * completely.
6360 */
6361 if ( p_wiv
6362 && !force
6363#ifdef FEAT_GUI
6364 && !gui.in_use
6365#endif
6366 && ScreenAttrs[off_to] != 0
6367 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6368 {
6369 /*
6370 * Need to remove highlighting attributes here.
6371 */
6372 windgoto(row, col + coloff);
6373 out_str(T_CE); /* clear rest of this screen line */
6374 screen_start(); /* don't know where cursor is now */
6375 force = TRUE; /* force redraw of rest of the line */
6376 redraw_next = TRUE; /* or else next char would miss out */
6377
6378 /*
6379 * If the previous character was highlighted, need to stop
6380 * highlighting at this character.
6381 */
6382 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6383 {
6384 screen_attr = ScreenAttrs[off_to - 1];
6385 term_windgoto(row, col + coloff);
6386 screen_stop_highlight();
6387 }
6388 else
6389 screen_attr = 0; /* highlighting has stopped */
6390 }
6391#ifdef FEAT_MBYTE
6392 if (enc_dbcs != 0)
6393 {
6394 /* Check if overwriting a double-byte with a single-byte or
6395 * the other way around requires another character to be
6396 * redrawn. For UTF-8 this isn't needed, because comparing
6397 * ScreenLinesUC[] is sufficient. */
6398 if (char_cells == 1
6399 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006400 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
6402 /* Writing a single-cell character over a double-cell
6403 * character: need to redraw the next cell. */
6404 ScreenLines[off_to + 1] = 0;
6405 redraw_next = TRUE;
6406 }
6407 else if (char_cells == 2
6408 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006409 && (*mb_off2cells)(off_to, max_off_to) == 1
6410 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 {
6412 /* Writing the second half of a double-cell character over
6413 * a double-cell character: need to redraw the second
6414 * cell. */
6415 ScreenLines[off_to + 2] = 0;
6416 redraw_next = TRUE;
6417 }
6418
6419 if (enc_dbcs == DBCS_JPNU)
6420 ScreenLines2[off_to] = ScreenLines2[off_from];
6421 }
6422 /* When writing a single-width character over a double-width
6423 * character and at the end of the redrawn text, need to clear out
6424 * the right halve of the old character.
6425 * Also required when writing the right halve of a double-width
6426 * char over the left halve of an existing one. */
6427 if (has_mbyte && col + char_cells == endcol
6428 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006429 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006431 && (*mb_off2cells)(off_to, max_off_to) == 1
6432 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 clear_next = TRUE;
6434#endif
6435
6436 ScreenLines[off_to] = ScreenLines[off_from];
6437#ifdef FEAT_MBYTE
6438 if (enc_utf8)
6439 {
6440 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6441 if (ScreenLinesUC[off_from] != 0)
6442 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006443 int i;
6444
6445 for (i = 0; i < Screen_mco; ++i)
6446 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
6448 }
6449 if (char_cells == 2)
6450 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6451#endif
6452
6453#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006454 /* The bold trick makes a single column of pixels appear in the
6455 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 * character should be redrawn too. This happens for our own GUI
6457 * and for some xterms. */
6458 if (
6459# ifdef FEAT_GUI
6460 gui.in_use
6461# endif
6462# if defined(FEAT_GUI) && defined(UNIX)
6463 ||
6464# endif
6465# ifdef UNIX
6466 term_is_xterm
6467# endif
6468 )
6469 {
6470 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006471 if (hl > HL_ALL)
6472 hl = syn_attr2attr(hl);
6473 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 redraw_next = TRUE;
6475 }
6476#endif
6477 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6478#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006479 /* For simplicity set the attributes of second half of a
6480 * double-wide character equal to the first half. */
6481 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006483
6484 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 else
6487#endif
6488 screen_char(off_to, row, col + coloff);
6489 }
6490 else if ( p_wiv
6491#ifdef FEAT_GUI
6492 && !gui.in_use
6493#endif
6494 && col + coloff > 0)
6495 {
6496 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6497 {
6498 /*
6499 * Don't output stop-highlight when moving the cursor, it will
6500 * stop the highlighting when it should continue.
6501 */
6502 screen_attr = 0;
6503 }
6504 else if (screen_attr != 0)
6505 screen_stop_highlight();
6506 }
6507
6508 off_to += CHAR_CELLS;
6509 off_from += CHAR_CELLS;
6510 col += CHAR_CELLS;
6511 }
6512
6513#ifdef FEAT_MBYTE
6514 if (clear_next)
6515 {
6516 /* Clear the second half of a double-wide character of which the left
6517 * half was overwritten with a single-wide character. */
6518 ScreenLines[off_to] = ' ';
6519 if (enc_utf8)
6520 ScreenLinesUC[off_to] = 0;
6521 screen_char(off_to, row, col + coloff);
6522 }
6523#endif
6524
6525 if (clear_width > 0
6526#ifdef FEAT_RIGHTLEFT
6527 && !rlflag
6528#endif
6529 )
6530 {
6531#ifdef FEAT_GUI
6532 int startCol = col;
6533#endif
6534
6535 /* blank out the rest of the line */
6536 while (col < clear_width && ScreenLines[off_to] == ' '
6537 && ScreenAttrs[off_to] == 0
6538#ifdef FEAT_MBYTE
6539 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6540#endif
6541 )
6542 {
6543 ++off_to;
6544 ++col;
6545 }
6546 if (col < clear_width)
6547 {
6548#ifdef FEAT_GUI
6549 /*
6550 * In the GUI, clearing the rest of the line may leave pixels
6551 * behind if the first character cleared was bold. Some bold
6552 * fonts spill over the left. In this case we redraw the previous
6553 * character too. If we didn't skip any blanks above, then we
6554 * only redraw if the character wasn't already redrawn anyway.
6555 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006556 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
6558 hl = ScreenAttrs[off_to];
6559 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006560 {
6561 int prev_cells = 1;
6562# ifdef FEAT_MBYTE
6563 if (enc_utf8)
6564 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6565 * that its width is 2. */
6566 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6567 else if (enc_dbcs != 0)
6568 {
6569 /* find previous character by counting from first
6570 * column and get its width. */
6571 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006572 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006573
6574 while (off < off_to)
6575 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006576 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006577 off += prev_cells;
6578 }
6579 }
6580
6581 if (enc_dbcs != 0 && prev_cells > 1)
6582 screen_char_2(off_to - prev_cells, row,
6583 col + coloff - prev_cells);
6584 else
6585# endif
6586 screen_char(off_to - prev_cells, row,
6587 col + coloff - prev_cells);
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
6590#endif
6591 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6592 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 off_to += clear_width - col;
6594 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
6596 }
6597
6598 if (clear_width > 0)
6599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 /* For a window that's left of another, draw the separator char. */
6601 if (col + coloff < Columns)
6602 {
6603 int c;
6604
6605 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006606 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006607#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006608 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6609 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 || ScreenAttrs[off_to] != hl)
6612 {
6613 ScreenLines[off_to] = c;
6614 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006615#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (enc_utf8)
6617 {
6618 if (c >= 0x80)
6619 {
6620 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006621 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 }
6623 else
6624 ScreenLinesUC[off_to] = 0;
6625 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 screen_char(off_to, row, col + coloff);
6628 }
6629 }
6630 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 LineWraps[row] = FALSE;
6632 }
6633}
6634
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006635#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006637 * Mirror text "str" for right-left displaying.
6638 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006640 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006641rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643 char_u *p1, *p2;
6644 int t;
6645
6646 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6647 {
6648 t = *p1;
6649 *p1 = *p2;
6650 *p2 = t;
6651 }
6652}
6653#endif
6654
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655/*
6656 * mark all status lines for redraw; used after first :cd
6657 */
6658 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006659status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
6661 win_T *wp;
6662
Bram Moolenaar29323592016-07-24 22:04:11 +02006663 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 if (wp->w_status_height)
6665 {
6666 wp->w_redr_status = TRUE;
6667 redraw_later(VALID);
6668 }
6669}
6670
6671/*
6672 * mark all status lines of the current buffer for redraw
6673 */
6674 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006675status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
6677 win_T *wp;
6678
Bram Moolenaar29323592016-07-24 22:04:11 +02006679 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6681 {
6682 wp->w_redr_status = TRUE;
6683 redraw_later(VALID);
6684 }
6685}
6686
6687/*
6688 * Redraw all status lines that need to be redrawn.
6689 */
6690 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006691redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 win_T *wp;
6694
Bram Moolenaar29323592016-07-24 22:04:11 +02006695 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006697 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006698 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006699 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
Bram Moolenaar4033c552017-09-16 20:54:51 +02006702#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703/*
6704 * Redraw all status lines at the bottom of frame "frp".
6705 */
6706 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006707win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
6709 if (frp->fr_layout == FR_LEAF)
6710 frp->fr_win->w_redr_status = TRUE;
6711 else if (frp->fr_layout == FR_ROW)
6712 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006713 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 win_redraw_last_status(frp);
6715 }
6716 else /* frp->fr_layout == FR_COL */
6717 {
6718 frp = frp->fr_child;
6719 while (frp->fr_next != NULL)
6720 frp = frp->fr_next;
6721 win_redraw_last_status(frp);
6722 }
6723}
6724#endif
6725
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726/*
6727 * Draw the verticap separator right of window "wp" starting with line "row".
6728 */
6729 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006730draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 int hl;
6733 int c;
6734
6735 if (wp->w_vsep_width)
6736 {
6737 /* draw the vertical separator right of this window */
6738 c = fillchar_vsep(&hl);
6739 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6740 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6741 c, ' ', hl);
6742 }
6743}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744
6745#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006746static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
6748/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006749 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 */
6751 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006752status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754 int len = 0;
6755
6756#ifdef FEAT_MENU
6757 int emenu = (xp->xp_context == EXPAND_MENUS
6758 || xp->xp_context == EXPAND_MENUNAMES);
6759
6760 /* Check for menu separators - replace with '|'. */
6761 if (emenu && menu_is_separator(s))
6762 return 1;
6763#endif
6764
6765 while (*s != NUL)
6766 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006767 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006768 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006769 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 }
6771
6772 return len;
6773}
6774
6775/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006776 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006777 * These are backslashes used for escaping. Do show backslashes in help tags.
6778 */
6779 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006780skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006781{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006782 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006783#ifdef FEAT_MENU
6784 || ((xp->xp_context == EXPAND_MENUS
6785 || xp->xp_context == EXPAND_MENUNAMES)
6786 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6787#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006788 )
6789 {
6790#ifndef BACKSLASH_IN_FILENAME
6791 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6792 return 2;
6793#endif
6794 return 1;
6795 }
6796 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006797}
6798
6799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 * Show wildchar matches in the status line.
6801 * Show at least the "match" item.
6802 * We start at item 'first_match' in the list and show all matches that fit.
6803 *
6804 * If inversion is possible we use it. Else '=' characters are used.
6805 */
6806 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006807win_redr_status_matches(
6808 expand_T *xp,
6809 int num_matches,
6810 char_u **matches, /* list of matches */
6811 int match,
6812 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
6814#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6815 int row;
6816 char_u *buf;
6817 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006818 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 int fillchar;
6820 int attr;
6821 int i;
6822 int highlight = TRUE;
6823 char_u *selstart = NULL;
6824 int selstart_col = 0;
6825 char_u *selend = NULL;
6826 static int first_match = 0;
6827 int add_left = FALSE;
6828 char_u *s;
6829#ifdef FEAT_MENU
6830 int emenu;
6831#endif
6832#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6833 int l;
6834#endif
6835
6836 if (matches == NULL) /* interrupted completion? */
6837 return;
6838
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006839#ifdef FEAT_MBYTE
6840 if (has_mbyte)
6841 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6842 else
6843#endif
6844 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 if (buf == NULL)
6846 return;
6847
6848 if (match == -1) /* don't show match but original text */
6849 {
6850 match = 0;
6851 highlight = FALSE;
6852 }
6853 /* count 1 for the ending ">" */
6854 clen = status_match_len(xp, L_MATCH(match)) + 3;
6855 if (match == 0)
6856 first_match = 0;
6857 else if (match < first_match)
6858 {
6859 /* jumping left, as far as we can go */
6860 first_match = match;
6861 add_left = TRUE;
6862 }
6863 else
6864 {
6865 /* check if match fits on the screen */
6866 for (i = first_match; i < match; ++i)
6867 clen += status_match_len(xp, L_MATCH(i)) + 2;
6868 if (first_match > 0)
6869 clen += 2;
6870 /* jumping right, put match at the left */
6871 if ((long)clen > Columns)
6872 {
6873 first_match = match;
6874 /* if showing the last match, we can add some on the left */
6875 clen = 2;
6876 for (i = match; i < num_matches; ++i)
6877 {
6878 clen += status_match_len(xp, L_MATCH(i)) + 2;
6879 if ((long)clen >= Columns)
6880 break;
6881 }
6882 if (i == num_matches)
6883 add_left = TRUE;
6884 }
6885 }
6886 if (add_left)
6887 while (first_match > 0)
6888 {
6889 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6890 if ((long)clen >= Columns)
6891 break;
6892 --first_match;
6893 }
6894
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006895 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
6897 if (first_match == 0)
6898 {
6899 *buf = NUL;
6900 len = 0;
6901 }
6902 else
6903 {
6904 STRCPY(buf, "< ");
6905 len = 2;
6906 }
6907 clen = len;
6908
6909 i = first_match;
6910 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6911 {
6912 if (i == match)
6913 {
6914 selstart = buf + len;
6915 selstart_col = clen;
6916 }
6917
6918 s = L_MATCH(i);
6919 /* Check for menu separators - replace with '|' */
6920#ifdef FEAT_MENU
6921 emenu = (xp->xp_context == EXPAND_MENUS
6922 || xp->xp_context == EXPAND_MENUNAMES);
6923 if (emenu && menu_is_separator(s))
6924 {
6925 STRCPY(buf + len, transchar('|'));
6926 l = (int)STRLEN(buf + len);
6927 len += l;
6928 clen += l;
6929 }
6930 else
6931#endif
6932 for ( ; *s != NUL; ++s)
6933 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006934 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 clen += ptr2cells(s);
6936#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006937 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
6939 STRNCPY(buf + len, s, l);
6940 s += l - 1;
6941 len += l;
6942 }
6943 else
6944#endif
6945 {
6946 STRCPY(buf + len, transchar_byte(*s));
6947 len += (int)STRLEN(buf + len);
6948 }
6949 }
6950 if (i == match)
6951 selend = buf + len;
6952
6953 *(buf + len++) = ' ';
6954 *(buf + len++) = ' ';
6955 clen += 2;
6956 if (++i == num_matches)
6957 break;
6958 }
6959
6960 if (i != num_matches)
6961 {
6962 *(buf + len++) = '>';
6963 ++clen;
6964 }
6965
6966 buf[len] = NUL;
6967
6968 row = cmdline_row - 1;
6969 if (row >= 0)
6970 {
6971 if (wild_menu_showing == 0)
6972 {
6973 if (msg_scrolled > 0)
6974 {
6975 /* Put the wildmenu just above the command line. If there is
6976 * no room, scroll the screen one line up. */
6977 if (cmdline_row == Rows - 1)
6978 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006979 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 ++msg_scrolled;
6981 }
6982 else
6983 {
6984 ++cmdline_row;
6985 ++row;
6986 }
6987 wild_menu_showing = WM_SCROLLED;
6988 }
6989 else
6990 {
6991 /* Create status line if needed by setting 'laststatus' to 2.
6992 * Set 'winminheight' to zero to avoid that the window is
6993 * resized. */
6994 if (lastwin->w_status_height == 0)
6995 {
6996 save_p_ls = p_ls;
6997 save_p_wmh = p_wmh;
6998 p_ls = 2;
6999 p_wmh = 0;
7000 last_status(FALSE);
7001 }
7002 wild_menu_showing = WM_SHOWN;
7003 }
7004 }
7005
7006 screen_puts(buf, row, 0, attr);
7007 if (selstart != NULL && highlight)
7008 {
7009 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007010 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 }
7012
7013 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7014 }
7015
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 vim_free(buf);
7018}
7019#endif
7020
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021/*
7022 * Redraw the status line of window wp.
7023 *
7024 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007025 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7026 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007028 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007029win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030{
7031 int row;
7032 char_u *p;
7033 int len;
7034 int fillchar;
7035 int attr;
7036 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007037 static int busy = FALSE;
7038
7039 /* It's possible to get here recursively when 'statusline' (indirectly)
7040 * invokes ":redrawstatus". Simply ignore the call then. */
7041 if (busy)
7042 return;
7043 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
7045 wp->w_redr_status = FALSE;
7046 if (wp->w_status_height == 0)
7047 {
7048 /* no status line, can only be last window */
7049 redraw_cmdline = TRUE;
7050 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007051 else if (!redrawing()
7052#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007053 // don't update status line when popup menu is visible and may be
7054 // drawn over it, unless it will be redrawn later
7055 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007056#endif
7057 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {
7059 /* Don't redraw right now, do it later. */
7060 wp->w_redr_status = TRUE;
7061 }
7062#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007063 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {
7065 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007066 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068#endif
7069 else
7070 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007071 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007073 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 p = NameBuff;
7075 len = (int)STRLEN(p);
7076
Bram Moolenaard85f2712017-07-28 21:51:57 +02007077 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078#ifdef FEAT_QUICKFIX
7079 || wp->w_p_pvw
7080#endif
7081 || bufIsChanged(wp->w_buffer)
7082 || wp->w_buffer->b_p_ro)
7083 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007084 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007086 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 len += (int)STRLEN(p + len);
7088 }
7089#ifdef FEAT_QUICKFIX
7090 if (wp->w_p_pvw)
7091 {
7092 STRCPY(p + len, _("[Preview]"));
7093 len += (int)STRLEN(p + len);
7094 }
7095#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007096 if (bufIsChanged(wp->w_buffer)
7097#ifdef FEAT_TERMINAL
7098 && !bt_terminal(wp->w_buffer)
7099#endif
7100 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
7102 STRCPY(p + len, "[+]");
7103 len += 3;
7104 }
7105 if (wp->w_buffer->b_p_ro)
7106 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007107 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007108 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 }
7110
Bram Moolenaar02631462017-09-22 15:20:32 +02007111 this_ru_col = ru_col - (Columns - wp->w_width);
7112 if (this_ru_col < (wp->w_width + 1) / 2)
7113 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (this_ru_col <= 1)
7115 {
7116 p = (char_u *)"<"; /* No room for file name! */
7117 len = 1;
7118 }
7119 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120#ifdef FEAT_MBYTE
7121 if (has_mbyte)
7122 {
7123 int clen = 0, i;
7124
7125 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007126 clen = mb_string2cells(p, -1);
7127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 /* Find first character that will fit.
7129 * Going from start to end is much faster for DBCS. */
7130 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007131 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 clen -= (*mb_ptr2cells)(p + i);
7133 len = clen;
7134 if (i > 0)
7135 {
7136 p = p + i - 1;
7137 *p = '<';
7138 ++len;
7139 }
7140
7141 }
7142 else
7143#endif
7144 if (len > this_ru_col - 1)
7145 {
7146 p += len - (this_ru_col - 1);
7147 *p = '<';
7148 len = this_ru_col - 1;
7149 }
7150
7151 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007152 screen_puts(p, row, wp->w_wincol, attr);
7153 screen_fill(row, row + 1, len + wp->w_wincol,
7154 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007156 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7158 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007159 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
7161#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007162 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163#endif
7164 }
7165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 /*
7167 * May need to draw the character below the vertical separator.
7168 */
7169 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7170 {
7171 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007172 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 else
7174 fillchar = fillchar_vsep(&attr);
7175 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7176 attr);
7177 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007178 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179}
7180
Bram Moolenaar238a5642006-02-21 22:12:05 +00007181#ifdef FEAT_STL_OPT
7182/*
7183 * Redraw the status line according to 'statusline' and take care of any
7184 * errors encountered.
7185 */
7186 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007187redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007188{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007189 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007190 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007191
7192 /* When called recursively return. This can happen when the statusline
7193 * contains an expression that triggers a redraw. */
7194 if (entered)
7195 return;
7196 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007197
Bram Moolenaara742e082016-04-05 21:10:38 +02007198 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007199 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007200 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007201 {
7202 /* When there is an error disable the statusline, otherwise the
7203 * display is messed up with errors and a redraw triggers the problem
7204 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007205 set_string_option_direct((char_u *)"statusline", -1,
7206 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007207 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007208 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007209 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007210 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007211}
7212#endif
7213
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214/*
7215 * Return TRUE if the status line of window "wp" is connected to the status
7216 * line of the window right of it. If not, then it's a vertical separator.
7217 * Only call if (wp->w_vsep_width != 0).
7218 */
7219 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007220stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 frame_T *fr;
7223
7224 fr = wp->w_frame;
7225 while (fr->fr_parent != NULL)
7226 {
7227 if (fr->fr_parent->fr_layout == FR_COL)
7228 {
7229 if (fr->fr_next != NULL)
7230 break;
7231 }
7232 else
7233 {
7234 if (fr->fr_next != NULL)
7235 return TRUE;
7236 }
7237 fr = fr->fr_parent;
7238 }
7239 return FALSE;
7240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243/*
7244 * Get the value to show for the language mappings, active 'keymap'.
7245 */
7246 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007247get_keymap_str(
7248 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007249 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007250 char_u *buf, /* buffer for the result */
7251 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252{
7253 char_u *p;
7254
7255 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7256 return FALSE;
7257
7258 {
7259#ifdef FEAT_EVAL
7260 buf_T *old_curbuf = curbuf;
7261 win_T *old_curwin = curwin;
7262 char_u *s;
7263
7264 curbuf = wp->w_buffer;
7265 curwin = wp;
7266 STRCPY(buf, "b:keymap_name"); /* must be writable */
7267 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007268 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 --emsg_skip;
7270 curbuf = old_curbuf;
7271 curwin = old_curwin;
7272 if (p == NULL || *p == NUL)
7273#endif
7274 {
7275#ifdef FEAT_KEYMAP
7276 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7277 p = wp->w_buffer->b_p_keymap;
7278 else
7279#endif
7280 p = (char_u *)"lang";
7281 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007282 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 buf[0] = NUL;
7284#ifdef FEAT_EVAL
7285 vim_free(s);
7286#endif
7287 }
7288 return buf[0] != NUL;
7289}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
7291#if defined(FEAT_STL_OPT) || defined(PROTO)
7292/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007293 * Redraw the status line or ruler of window "wp".
7294 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 */
7296 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007297win_redr_custom(
7298 win_T *wp,
7299 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007301 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 int attr;
7303 int curattr;
7304 int row;
7305 int col = 0;
7306 int maxwidth;
7307 int width;
7308 int n;
7309 int len;
7310 int fillchar;
7311 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007312 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007314 struct stl_hlrec hltab[STL_MAX_ITEM];
7315 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007316 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007317 win_T *ewp;
7318 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
Bram Moolenaar1d633412013-12-11 15:52:01 +01007320 /* There is a tiny chance that this gets called recursively: When
7321 * redrawing a status line triggers redrawing the ruler or tabline.
7322 * Avoid trouble by not allowing recursion. */
7323 if (entered)
7324 return;
7325 entered = TRUE;
7326
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007328 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007330 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007331 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007332 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007333 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007334 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007335 maxwidth = Columns;
7336# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007337 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007338# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007340 else
7341 {
7342 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007343 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007344 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007345
7346 if (draw_ruler)
7347 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007348 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007349 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007350 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007351 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007352 if (*++stl == '-')
7353 stl++;
7354 if (atoi((char *)stl))
7355 while (VIM_ISDIGIT(*stl))
7356 stl++;
7357 if (*stl++ != '(')
7358 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007359 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007360 col = ru_col - (Columns - wp->w_width);
7361 if (col < (wp->w_width + 1) / 2)
7362 col = (wp->w_width + 1) / 2;
7363 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007364 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007365 {
7366 row = Rows - 1;
7367 --maxwidth; /* writing in last column may cause scrolling */
7368 fillchar = ' ';
7369 attr = 0;
7370 }
7371
7372# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007373 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007374# endif
7375 }
7376 else
7377 {
7378 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007379 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007380 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007381 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007382# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007383 use_sandbox = was_set_insecurely((char_u *)"statusline",
7384 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007385# endif
7386 }
7387
Bram Moolenaar53f81742017-09-22 14:35:51 +02007388 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007389 }
7390
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007392 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
Bram Moolenaar61452852011-02-01 18:01:11 +01007394 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7395 * the cursor away and back. */
7396 ewp = wp == NULL ? curwin : wp;
7397 p_crb_save = ewp->w_p_crb;
7398 ewp->w_p_crb = FALSE;
7399
Bram Moolenaar362f3562009-11-03 16:20:34 +00007400 /* Make a copy, because the statusline may include a function call that
7401 * might change the option value and free the memory. */
7402 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007403 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007404 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007405 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007406 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007407 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007409 /* Make all characters printable. */
7410 p = transstr(buf);
7411 if (p != NULL)
7412 {
7413 vim_strncpy(buf, p, sizeof(buf) - 1);
7414 vim_free(p);
7415 }
7416
7417 /* fill up with "fillchar" */
7418 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007419 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
7421#ifdef FEAT_MBYTE
7422 len += (*mb_char2bytes)(fillchar, buf + len);
7423#else
7424 buf[len++] = fillchar;
7425#endif
7426 ++width;
7427 }
7428 buf[len] = NUL;
7429
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007430 /*
7431 * Draw each snippet with the specified highlighting.
7432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 curattr = attr;
7434 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007435 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007437 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 screen_puts_len(p, len, row, col, curattr);
7439 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007440 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007442 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007444 else if (hltab[n].userhl < 0)
7445 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007446#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007447 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7448 && wp->w_status_height != 0)
7449 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007450 else if (wp != NULL && bt_terminal(wp->w_buffer)
7451 && wp->w_status_height != 0)
7452 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007453#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007454 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007455 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007457 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
7459 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007460
7461 if (wp == NULL)
7462 {
7463 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7464 col = 0;
7465 len = 0;
7466 p = buf;
7467 fillchar = 0;
7468 for (n = 0; tabtab[n].start != NULL; n++)
7469 {
7470 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7471 while (col < len)
7472 TabPageIdxs[col++] = fillchar;
7473 p = tabtab[n].start;
7474 fillchar = tabtab[n].userhl;
7475 }
7476 while (col < Columns)
7477 TabPageIdxs[col++] = fillchar;
7478 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007479
7480theend:
7481 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482}
7483
7484#endif /* FEAT_STL_OPT */
7485
7486/*
7487 * Output a single character directly to the screen and update ScreenLines.
7488 */
7489 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007490screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 char_u buf[MB_MAXBYTES + 1];
7493
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007494#ifdef FEAT_MBYTE
7495 if (has_mbyte)
7496 buf[(*mb_char2bytes)(c, buf)] = NUL;
7497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007499 {
7500 buf[0] = c;
7501 buf[1] = NUL;
7502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 screen_puts(buf, row, col, attr);
7504}
7505
7506/*
7507 * Get a single character directly from ScreenLines into "bytes[]".
7508 * Also return its attribute in *attrp;
7509 */
7510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007511screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512{
7513 unsigned off;
7514
7515 /* safety check */
7516 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7517 {
7518 off = LineOffset[row] + col;
7519 *attrp = ScreenAttrs[off];
7520 bytes[0] = ScreenLines[off];
7521 bytes[1] = NUL;
7522
7523#ifdef FEAT_MBYTE
7524 if (enc_utf8 && ScreenLinesUC[off] != 0)
7525 bytes[utfc_char2bytes(off, bytes)] = NUL;
7526 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7527 {
7528 bytes[0] = ScreenLines[off];
7529 bytes[1] = ScreenLines2[off];
7530 bytes[2] = NUL;
7531 }
7532 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7533 {
7534 bytes[1] = ScreenLines[off + 1];
7535 bytes[2] = NUL;
7536 }
7537#endif
7538 }
7539}
7540
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007541#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007542/*
7543 * Return TRUE if composing characters for screen posn "off" differs from
7544 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007545 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007546 */
7547 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007548screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007549{
7550 int i;
7551
7552 for (i = 0; i < Screen_mco; ++i)
7553 {
7554 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7555 return TRUE;
7556 if (u8cc[i] == 0)
7557 break;
7558 }
7559 return FALSE;
7560}
7561#endif
7562
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563/*
7564 * Put string '*text' on the screen at position 'row' and 'col', with
7565 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7566 * Note: only outputs within one row, message is truncated at screen boundary!
7567 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7568 */
7569 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007570screen_puts(
7571 char_u *text,
7572 int row,
7573 int col,
7574 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
7576 screen_puts_len(text, -1, row, col, attr);
7577}
7578
7579/*
7580 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7581 * a NUL.
7582 */
7583 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007584screen_puts_len(
7585 char_u *text,
7586 int textlen,
7587 int row,
7588 int col,
7589 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
7591 unsigned off;
7592 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007593 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 int c;
7595#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007596 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 int mbyte_blen = 1;
7598 int mbyte_cells = 1;
7599 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007600 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 int clear_next_cell = FALSE;
7602# ifdef FEAT_ARABIC
7603 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007604 int pc, nc, nc1;
7605 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606# endif
7607#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007608#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7609 int force_redraw_this;
7610 int force_redraw_next = FALSE;
7611#endif
7612 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613
7614 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7615 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007616 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617
Bram Moolenaarc236c162008-07-13 17:41:49 +00007618#ifdef FEAT_MBYTE
7619 /* When drawing over the right halve of a double-wide char clear out the
7620 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007621 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007622# ifdef FEAT_GUI
7623 && !gui.in_use
7624# endif
7625 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007626 {
7627 ScreenLines[off - 1] = ' ';
7628 ScreenAttrs[off - 1] = 0;
7629 if (enc_utf8)
7630 {
7631 ScreenLinesUC[off - 1] = 0;
7632 ScreenLinesC[0][off - 1] = 0;
7633 }
7634 /* redraw the previous cell, make it empty */
7635 screen_char(off - 1, row, col - 1);
7636 /* force the cell at "col" to be redrawn */
7637 force_redraw_next = TRUE;
7638 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007639#endif
7640
Bram Moolenaar367329b2007-08-30 11:53:22 +00007641#ifdef FEAT_MBYTE
7642 max_off = LineOffset[row] + screen_Columns;
7643#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007644 while (col < screen_Columns
7645 && (len < 0 || (int)(ptr - text) < len)
7646 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {
7648 c = *ptr;
7649#ifdef FEAT_MBYTE
7650 /* check if this is the first byte of a multibyte */
7651 if (has_mbyte)
7652 {
7653 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007654 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007656 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7658 mbyte_cells = 1;
7659 else if (enc_dbcs != 0)
7660 mbyte_cells = mbyte_blen;
7661 else /* enc_utf8 */
7662 {
7663 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007664 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 (int)((text + len) - ptr));
7666 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007667 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007669# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 /* Non-BMP character: display as ? or fullwidth ?. */
7671 if (u8c >= 0x10000)
7672 {
7673 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7674 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007675 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678# ifdef FEAT_ARABIC
7679 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7680 {
7681 /* Do Arabic shaping. */
7682 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7683 {
7684 /* Past end of string to be displayed. */
7685 nc = NUL;
7686 nc1 = NUL;
7687 }
7688 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007689 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007690 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7691 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007692 nc1 = pcc[0];
7693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 pc = prev_c;
7695 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007696 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 }
7698 else
7699 prev_c = u8c;
7700# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007701 if (col + mbyte_cells > screen_Columns)
7702 {
7703 /* Only 1 cell left, but character requires 2 cells:
7704 * display a '>' in the last column to avoid wrapping. */
7705 c = '>';
7706 mbyte_cells = 1;
7707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
7709 }
7710#endif
7711
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007712#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7713 force_redraw_this = force_redraw_next;
7714 force_redraw_next = FALSE;
7715#endif
7716
7717 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718#ifdef FEAT_MBYTE
7719 || (mbyte_cells == 2
7720 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7721 || (enc_dbcs == DBCS_JPNU
7722 && c == 0x8e
7723 && ScreenLines2[off] != ptr[1])
7724 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007725 && (ScreenLinesUC[off] !=
7726 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7727 || (ScreenLinesUC[off] != 0
7728 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729#endif
7730 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007731 || exmode_active;
7732
7733 if (need_redraw
7734#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7735 || force_redraw_this
7736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 )
7738 {
7739#if defined(FEAT_GUI) || defined(UNIX)
7740 /* The bold trick makes a single row of pixels appear in the next
7741 * character. When a bold character is removed, the next
7742 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007743 * and for some xterms. */
7744 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745# ifdef FEAT_GUI
7746 gui.in_use
7747# endif
7748# if defined(FEAT_GUI) && defined(UNIX)
7749 ||
7750# endif
7751# ifdef UNIX
7752 term_is_xterm
7753# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007754 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007756 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007758 if (n > HL_ALL)
7759 n = syn_attr2attr(n);
7760 if (n & HL_BOLD)
7761 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
7763#endif
7764#ifdef FEAT_MBYTE
7765 /* When at the end of the text and overwriting a two-cell
7766 * character with a one-cell character, need to clear the next
7767 * cell. Also when overwriting the left halve of a two-cell char
7768 * with the right halve of a two-cell char. Do this only once
7769 * (mb_off2cells() may return 2 on the right halve). */
7770 if (clear_next_cell)
7771 clear_next_cell = FALSE;
7772 else if (has_mbyte
7773 && (len < 0 ? ptr[mbyte_blen] == NUL
7774 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007775 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007777 && (*mb_off2cells)(off, max_off) == 1
7778 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 clear_next_cell = TRUE;
7780
7781 /* Make sure we never leave a second byte of a double-byte behind,
7782 * it confuses mb_off2cells(). */
7783 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007784 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007786 && (*mb_off2cells)(off, max_off) == 1
7787 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 ScreenLines[off + mbyte_blen] = 0;
7789#endif
7790 ScreenLines[off] = c;
7791 ScreenAttrs[off] = attr;
7792#ifdef FEAT_MBYTE
7793 if (enc_utf8)
7794 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007795 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 ScreenLinesUC[off] = 0;
7797 else
7798 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007799 int i;
7800
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007802 for (i = 0; i < Screen_mco; ++i)
7803 {
7804 ScreenLinesC[i][off] = u8cc[i];
7805 if (u8cc[i] == 0)
7806 break;
7807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809 if (mbyte_cells == 2)
7810 {
7811 ScreenLines[off + 1] = 0;
7812 ScreenAttrs[off + 1] = attr;
7813 }
7814 screen_char(off, row, col);
7815 }
7816 else if (mbyte_cells == 2)
7817 {
7818 ScreenLines[off + 1] = ptr[1];
7819 ScreenAttrs[off + 1] = attr;
7820 screen_char_2(off, row, col);
7821 }
7822 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7823 {
7824 ScreenLines2[off] = ptr[1];
7825 screen_char(off, row, col);
7826 }
7827 else
7828#endif
7829 screen_char(off, row, col);
7830 }
7831#ifdef FEAT_MBYTE
7832 if (has_mbyte)
7833 {
7834 off += mbyte_cells;
7835 col += mbyte_cells;
7836 ptr += mbyte_blen;
7837 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007838 {
7839 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007841 len = -1;
7842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 }
7844 else
7845#endif
7846 {
7847 ++off;
7848 ++col;
7849 ++ptr;
7850 }
7851 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007852
7853#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7854 /* If we detected the next character needs to be redrawn, but the text
7855 * doesn't extend up to there, update the character here. */
7856 if (force_redraw_next && col < screen_Columns)
7857 {
7858# ifdef FEAT_MBYTE
7859 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7860 screen_char_2(off, row, col);
7861 else
7862# endif
7863 screen_char(off, row, col);
7864 }
7865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866}
7867
7868#ifdef FEAT_SEARCH_EXTRA
7869/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007870 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 */
7872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007873start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 if (p_hls && !no_hlsearch)
7876 {
7877 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007878 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007879# ifdef FEAT_RELTIME
7880 /* Set the time limit to 'redrawtime'. */
7881 profile_setlimit(p_rdt, &search_hl.tm);
7882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 }
7884}
7885
7886/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007887 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 */
7889 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007890end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
7892 if (search_hl.rm.regprog != NULL)
7893 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007894 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 search_hl.rm.regprog = NULL;
7896 }
7897}
7898
7899/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007900 * Init for calling prepare_search_hl().
7901 */
7902 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007903init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007904{
7905 matchitem_T *cur;
7906
7907 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7908 * match */
7909 cur = wp->w_match_head;
7910 while (cur != NULL)
7911 {
7912 cur->hl.rm = cur->match;
7913 if (cur->hlg_id == 0)
7914 cur->hl.attr = 0;
7915 else
7916 cur->hl.attr = syn_id2attr(cur->hlg_id);
7917 cur->hl.buf = wp->w_buffer;
7918 cur->hl.lnum = 0;
7919 cur->hl.first_lnum = 0;
7920# ifdef FEAT_RELTIME
7921 /* Set the time limit to 'redrawtime'. */
7922 profile_setlimit(p_rdt, &(cur->hl.tm));
7923# endif
7924 cur = cur->next;
7925 }
7926 search_hl.buf = wp->w_buffer;
7927 search_hl.lnum = 0;
7928 search_hl.first_lnum = 0;
7929 /* time limit is set at the toplevel, for all windows */
7930}
7931
7932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 * Advance to the match in window "wp" line "lnum" or past it.
7934 */
7935 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007936prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007938 matchitem_T *cur; /* points to the match list */
7939 match_T *shl; /* points to search_hl or a match */
7940 int shl_flag; /* flag to indicate whether search_hl
7941 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007942 int pos_inprogress; /* marks that position match search is
7943 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 int n;
7945
7946 /*
7947 * When using a multi-line pattern, start searching at the top
7948 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007949 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007951 cur = wp->w_match_head;
7952 shl_flag = FALSE;
7953 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007955 if (shl_flag == FALSE)
7956 {
7957 shl = &search_hl;
7958 shl_flag = TRUE;
7959 }
7960 else
7961 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (shl->rm.regprog != NULL
7963 && shl->lnum == 0
7964 && re_multiline(shl->rm.regprog))
7965 {
7966 if (shl->first_lnum == 0)
7967 {
7968# ifdef FEAT_FOLDING
7969 for (shl->first_lnum = lnum;
7970 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7971 if (hasFoldingWin(wp, shl->first_lnum - 1,
7972 NULL, NULL, TRUE, NULL))
7973 break;
7974# else
7975 shl->first_lnum = wp->w_topline;
7976# endif
7977 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007978 if (cur != NULL)
7979 cur->pos.cur = 0;
7980 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7983 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007985 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7986 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987 pos_inprogress = cur == NULL || cur->pos.cur == 0
7988 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 if (shl->lnum != 0)
7990 {
7991 shl->first_lnum = shl->lnum
7992 + shl->rm.endpos[0].lnum
7993 - shl->rm.startpos[0].lnum;
7994 n = shl->rm.endpos[0].col;
7995 }
7996 else
7997 {
7998 ++shl->first_lnum;
7999 n = 0;
8000 }
8001 }
8002 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008003 if (shl != &search_hl && cur != NULL)
8004 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 }
8006}
8007
8008/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008009 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 * Uses shl->buf.
8011 * Sets shl->lnum and shl->rm contents.
8012 * Note: Assumes a previous match is always before "lnum", unless
8013 * shl->lnum is zero.
8014 * Careful: Any pointers for buffer lines will become invalid.
8015 */
8016 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008017next_search_hl(
8018 win_T *win,
8019 match_T *shl, /* points to search_hl or a match */
8020 linenr_T lnum,
8021 colnr_T mincol, /* minimal column for a match */
8022 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024 linenr_T l;
8025 colnr_T matchcol;
8026 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008027 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008029 // for :{range}s/pat only highlight inside the range
8030 if (lnum < search_first_line || lnum > search_last_line)
8031 {
8032 shl->lnum = 0;
8033 return;
8034 }
8035
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 if (shl->lnum != 0)
8037 {
8038 /* Check for three situations:
8039 * 1. If the "lnum" is below a previous match, start a new search.
8040 * 2. If the previous match includes "mincol", use it.
8041 * 3. Continue after the previous match.
8042 */
8043 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8044 if (lnum > l)
8045 shl->lnum = 0;
8046 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8047 return;
8048 }
8049
8050 /*
8051 * Repeat searching for a match until one is found that includes "mincol"
8052 * or none is found in this line.
8053 */
8054 called_emsg = FALSE;
8055 for (;;)
8056 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008057#ifdef FEAT_RELTIME
8058 /* Stop searching after passing the time limit. */
8059 if (profile_passed_limit(&(shl->tm)))
8060 {
8061 shl->lnum = 0; /* no match found in time */
8062 break;
8063 }
8064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 /* Three situations:
8066 * 1. No useful previous match: search from start of line.
8067 * 2. Not Vi compatible or empty match: continue at next character.
8068 * Break the loop if this is beyond the end of the line.
8069 * 3. Vi compatible searching: continue at end of previous match.
8070 */
8071 if (shl->lnum == 0)
8072 matchcol = 0;
8073 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8074 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008075 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008077 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008078
8079 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008080 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008081 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008083 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 shl->lnum = 0;
8085 break;
8086 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008087#ifdef FEAT_MBYTE
8088 if (has_mbyte)
8089 matchcol += mb_ptr2len(ml);
8090 else
8091#endif
8092 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
8094 else
8095 matchcol = shl->rm.endpos[0].col;
8096
8097 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008098 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008100 /* Remember whether shl->rm is using a copy of the regprog in
8101 * cur->match. */
8102 int regprog_is_copy = (shl != &search_hl && cur != NULL
8103 && shl == &cur->hl
8104 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008105 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008106
Bram Moolenaarb3414592014-06-17 17:48:32 +02008107 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8108 matchcol,
8109#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008110 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008111#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008112 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008113#endif
8114 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008115 /* Copy the regprog, in case it got freed and recompiled. */
8116 if (regprog_is_copy)
8117 cur->match.regprog = cur->hl.rm.regprog;
8118
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008119 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008120 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008121 /* Error while handling regexp: stop using this regexp. */
8122 if (shl == &search_hl)
8123 {
8124 /* don't free regprog in the match list, it's a copy */
8125 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008126 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008127 }
8128 shl->rm.regprog = NULL;
8129 shl->lnum = 0;
8130 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8131 message */
8132 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008133 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008134 }
8135 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008136 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008137 else
8138 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 if (nmatched == 0)
8140 {
8141 shl->lnum = 0; /* no match found */
8142 break;
8143 }
8144 if (shl->rm.startpos[0].lnum > 0
8145 || shl->rm.startpos[0].col >= mincol
8146 || nmatched > 1
8147 || shl->rm.endpos[0].col > mincol)
8148 {
8149 shl->lnum += shl->rm.startpos[0].lnum;
8150 break; /* useful match found */
8151 }
8152 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008153
8154 // Restore called_emsg for assert_fails().
8155 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157
Bram Moolenaar85077472016-10-16 14:35:48 +02008158/*
8159 * If there is a match fill "shl" and return one.
8160 * Return zero otherwise.
8161 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008162 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008163next_search_hl_pos(
8164 match_T *shl, /* points to a match */
8165 linenr_T lnum,
8166 posmatch_T *posmatch, /* match positions */
8167 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008168{
8169 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008170 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008171
Bram Moolenaarb3414592014-06-17 17:48:32 +02008172 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8173 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008174 llpos_T *pos = &posmatch->pos[i];
8175
8176 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008177 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008178 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008179 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008180 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008181 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008182 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008183 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008184 /* if this match comes before the one at "found" then swap
8185 * them */
8186 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008187 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008188 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008189
Bram Moolenaar85077472016-10-16 14:35:48 +02008190 *pos = posmatch->pos[found];
8191 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008192 }
8193 }
8194 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008195 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008196 }
8197 }
8198 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008199 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008200 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008201 colnr_T start = posmatch->pos[found].col == 0
8202 ? 0 : posmatch->pos[found].col - 1;
8203 colnr_T end = posmatch->pos[found].col == 0
8204 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008205
Bram Moolenaar85077472016-10-16 14:35:48 +02008206 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008207 shl->rm.startpos[0].lnum = 0;
8208 shl->rm.startpos[0].col = start;
8209 shl->rm.endpos[0].lnum = 0;
8210 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008211 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008212 posmatch->cur = found + 1;
8213 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008214 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008215 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008216}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008217#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008218
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008220screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222 attrentry_T *aep = NULL;
8223
8224 screen_attr = attr;
8225 if (full_screen
8226#ifdef WIN3264
8227 && termcap_active
8228#endif
8229 )
8230 {
8231#ifdef FEAT_GUI
8232 if (gui.in_use)
8233 {
8234 char buf[20];
8235
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008236 /* The GUI handles this internally. */
8237 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 OUT_STR(buf);
8239 }
8240 else
8241#endif
8242 {
8243 if (attr > HL_ALL) /* special HL attr. */
8244 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008245 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 aep = syn_cterm_attr2entry(attr);
8247 else
8248 aep = syn_term_attr2entry(attr);
8249 if (aep == NULL) /* did ":syntax clear" */
8250 attr = 0;
8251 else
8252 attr = aep->ae_attr;
8253 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008254 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008256 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008257#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008258 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8259 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8260 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008261#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008262 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008263 /* If the Normal FG color has BOLD attribute and the new HL
8264 * has a FG color defined, clear BOLD. */
8265 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008266 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008268 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008269 out_str(T_UCS);
8270 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008271 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8272 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008274 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008276 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008278 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008279 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280
8281 /*
8282 * Output the color or start string after bold etc., in case the
8283 * bold etc. override the color setting.
8284 */
8285 if (aep != NULL)
8286 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008287#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008288 /* When 'termguicolors' is set but fg or bg is unset,
8289 * fall back to the cterm colors. This helps for SpellBad,
8290 * where the GUI uses a red undercurl. */
8291 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008293 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008294 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008295 }
8296 else
8297#endif
8298 if (t_colors > 1)
8299 {
8300 if (aep->ae_u.cterm.fg_color)
8301 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8302 }
8303#ifdef FEAT_TERMGUICOLORS
8304 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8305 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008306 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008307 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 }
8309 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008310#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008311 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008313 if (aep->ae_u.cterm.bg_color)
8314 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8315 }
8316
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008317 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008318 {
8319 if (aep->ae_u.term.start != NULL)
8320 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
8322 }
8323 }
8324 }
8325}
8326
8327 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008328screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
8330 int do_ME = FALSE; /* output T_ME code */
8331
8332 if (screen_attr != 0
8333#ifdef WIN3264
8334 && termcap_active
8335#endif
8336 )
8337 {
8338#ifdef FEAT_GUI
8339 if (gui.in_use)
8340 {
8341 char buf[20];
8342
8343 /* use internal GUI code */
8344 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8345 OUT_STR(buf);
8346 }
8347 else
8348#endif
8349 {
8350 if (screen_attr > HL_ALL) /* special HL attr. */
8351 {
8352 attrentry_T *aep;
8353
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008354 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {
8356 /*
8357 * Assume that t_me restores the original colors!
8358 */
8359 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008360 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008361#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008362 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8363 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8364 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008365#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008366 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008367#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008368 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8369 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8370 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008371#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008372 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 do_ME = TRUE;
8374 }
8375 else
8376 {
8377 aep = syn_term_attr2entry(screen_attr);
8378 if (aep != NULL && aep->ae_u.term.stop != NULL)
8379 {
8380 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8381 do_ME = TRUE;
8382 else
8383 out_str(aep->ae_u.term.stop);
8384 }
8385 }
8386 if (aep == NULL) /* did ":syntax clear" */
8387 screen_attr = 0;
8388 else
8389 screen_attr = aep->ae_attr;
8390 }
8391
8392 /*
8393 * Often all ending-codes are equal to T_ME. Avoid outputting the
8394 * same sequence several times.
8395 */
8396 if (screen_attr & HL_STANDOUT)
8397 {
8398 if (STRCMP(T_SE, T_ME) == 0)
8399 do_ME = TRUE;
8400 else
8401 out_str(T_SE);
8402 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008403 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008404 {
8405 if (STRCMP(T_UCE, T_ME) == 0)
8406 do_ME = TRUE;
8407 else
8408 out_str(T_UCE);
8409 }
8410 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008411 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {
8413 if (STRCMP(T_UE, T_ME) == 0)
8414 do_ME = TRUE;
8415 else
8416 out_str(T_UE);
8417 }
8418 if (screen_attr & HL_ITALIC)
8419 {
8420 if (STRCMP(T_CZR, T_ME) == 0)
8421 do_ME = TRUE;
8422 else
8423 out_str(T_CZR);
8424 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008425 if (screen_attr & HL_STRIKETHROUGH)
8426 {
8427 if (STRCMP(T_STE, T_ME) == 0)
8428 do_ME = TRUE;
8429 else
8430 out_str(T_STE);
8431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8433 out_str(T_ME);
8434
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008435#ifdef FEAT_TERMGUICOLORS
8436 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008438 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008439 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008440 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008441 term_bg_rgb_color(cterm_normal_bg_gui_color);
8442 }
8443 else
8444#endif
8445 {
8446 if (t_colors > 1)
8447 {
8448 /* set Normal cterm colors */
8449 if (cterm_normal_fg_color != 0)
8450 term_fg_color(cterm_normal_fg_color - 1);
8451 if (cterm_normal_bg_color != 0)
8452 term_bg_color(cterm_normal_bg_color - 1);
8453 if (cterm_normal_fg_bold)
8454 out_str(T_MD);
8455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 }
8457 }
8458 }
8459 screen_attr = 0;
8460}
8461
8462/*
8463 * Reset the colors for a cterm. Used when leaving Vim.
8464 * The machine specific code may override this again.
8465 */
8466 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008467reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008469 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
8471 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008472#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008473 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8474 || cterm_normal_bg_gui_color != INVALCOLOR)
8475 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008476#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
8480 out_str(T_OP);
8481 screen_attr = -1;
8482 }
8483 if (cterm_normal_fg_bold)
8484 {
8485 out_str(T_ME);
8486 screen_attr = -1;
8487 }
8488 }
8489}
8490
8491/*
8492 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8493 * using the attributes from ScreenAttrs["off"].
8494 */
8495 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008496screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
8498 int attr;
8499
8500 /* Check for illegal values, just in case (could happen just after
8501 * resizing). */
8502 if (row >= screen_Rows || col >= screen_Columns)
8503 return;
8504
Bram Moolenaar494838a2015-02-10 19:20:37 +01008505 /* Outputting a character in the last cell on the screen may scroll the
8506 * screen up. Only do it when the "xn" termcap property is set, otherwise
8507 * mark the character invalid (update it when scrolled up). */
8508 if (*T_XN == NUL
8509 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510#ifdef FEAT_RIGHTLEFT
8511 /* account for first command-line character in rightleft mode */
8512 && !cmdmsg_rl
8513#endif
8514 )
8515 {
8516 ScreenAttrs[off] = (sattr_T)-1;
8517 return;
8518 }
8519
8520 /*
8521 * Stop highlighting first, so it's easier to move the cursor.
8522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 if (screen_char_attr != 0)
8524 attr = screen_char_attr;
8525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 attr = ScreenAttrs[off];
8527 if (screen_attr != attr)
8528 screen_stop_highlight();
8529
8530 windgoto(row, col);
8531
8532 if (screen_attr != attr)
8533 screen_start_highlight(attr);
8534
8535#ifdef FEAT_MBYTE
8536 if (enc_utf8 && ScreenLinesUC[off] != 0)
8537 {
8538 char_u buf[MB_MAXBYTES + 1];
8539
Bram Moolenaarcb070082016-04-02 22:14:51 +02008540 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008541 {
8542 if (*p_ambw == 'd'
8543# ifdef FEAT_GUI
8544 && !gui.in_use
8545# endif
8546 )
8547 {
8548 /* Clear the two screen cells. If the character is actually
8549 * single width it won't change the second cell. */
8550 out_str((char_u *)" ");
8551 term_windgoto(row, col);
8552 }
8553 /* not sure where the cursor is after drawing the ambiguous width
8554 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008555 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008556 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008557 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008559
8560 /* Convert the UTF-8 character to bytes and write it. */
8561 buf[utfc_char2bytes(off, buf)] = NUL;
8562 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 }
8564 else
8565#endif
8566 {
8567#ifdef FEAT_MBYTE
8568 out_flush_check();
8569#endif
8570 out_char(ScreenLines[off]);
8571#ifdef FEAT_MBYTE
8572 /* double-byte character in single-width cell */
8573 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8574 out_char(ScreenLines2[off]);
8575#endif
8576 }
8577
8578 screen_cur_col++;
8579}
8580
8581#ifdef FEAT_MBYTE
8582
8583/*
8584 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8585 * on the screen at position 'row' and 'col'.
8586 * The attributes of the first byte is used for all. This is required to
8587 * output the two bytes of a double-byte character with nothing in between.
8588 */
8589 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008590screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
8592 /* Check for illegal values (could be wrong when screen was resized). */
8593 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8594 return;
8595
8596 /* Outputting the last character on the screen may scrollup the screen.
8597 * Don't to it! Mark the character invalid (update it when scrolled up) */
8598 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8599 {
8600 ScreenAttrs[off] = (sattr_T)-1;
8601 return;
8602 }
8603
8604 /* Output the first byte normally (positions the cursor), then write the
8605 * second byte directly. */
8606 screen_char(off, row, col);
8607 out_char(ScreenLines[off + 1]);
8608 ++screen_cur_col;
8609}
8610#endif
8611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612/*
8613 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8614 * This uses the contents of ScreenLines[] and doesn't change it.
8615 */
8616 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008617screen_draw_rectangle(
8618 int row,
8619 int col,
8620 int height,
8621 int width,
8622 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623{
8624 int r, c;
8625 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008626#ifdef FEAT_MBYTE
8627 int max_off;
8628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008630 /* Can't use ScreenLines unless initialized */
8631 if (ScreenLines == NULL)
8632 return;
8633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 if (invert)
8635 screen_char_attr = HL_INVERSE;
8636 for (r = row; r < row + height; ++r)
8637 {
8638 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008639#ifdef FEAT_MBYTE
8640 max_off = off + screen_Columns;
8641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 for (c = col; c < col + width; ++c)
8643 {
8644#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008645 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 {
8647 screen_char_2(off + c, r, c);
8648 ++c;
8649 }
8650 else
8651#endif
8652 {
8653 screen_char(off + c, r, c);
8654#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008655 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 ++c;
8657#endif
8658 }
8659 }
8660 }
8661 screen_char_attr = 0;
8662}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664/*
8665 * Redraw the characters for a vertically split window.
8666 */
8667 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008668redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669{
8670 int col;
8671 int width;
8672
8673# ifdef FEAT_CLIPBOARD
8674 clip_may_clear_selection(row, end - 1);
8675# endif
8676
8677 if (wp == NULL)
8678 {
8679 col = 0;
8680 width = Columns;
8681 }
8682 else
8683 {
8684 col = wp->w_wincol;
8685 width = wp->w_width;
8686 }
8687 screen_draw_rectangle(row, col, end - row, width, FALSE);
8688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008690 static void
8691space_to_screenline(int off, int attr)
8692{
8693 ScreenLines[off] = ' ';
8694 ScreenAttrs[off] = attr;
8695# ifdef FEAT_MBYTE
8696 if (enc_utf8)
8697 ScreenLinesUC[off] = 0;
8698# endif
8699}
8700
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701/*
8702 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8703 * with character 'c1' in first column followed by 'c2' in the other columns.
8704 * Use attributes 'attr'.
8705 */
8706 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008707screen_fill(
8708 int start_row,
8709 int end_row,
8710 int start_col,
8711 int end_col,
8712 int c1,
8713 int c2,
8714 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 int row;
8717 int col;
8718 int off;
8719 int end_off;
8720 int did_delete;
8721 int c;
8722 int norm_term;
8723#if defined(FEAT_GUI) || defined(UNIX)
8724 int force_next = FALSE;
8725#endif
8726
8727 if (end_row > screen_Rows) /* safety check */
8728 end_row = screen_Rows;
8729 if (end_col > screen_Columns) /* safety check */
8730 end_col = screen_Columns;
8731 if (ScreenLines == NULL
8732 || start_row >= end_row
8733 || start_col >= end_col) /* nothing to do */
8734 return;
8735
8736 /* it's a "normal" terminal when not in a GUI or cterm */
8737 norm_term = (
8738#ifdef FEAT_GUI
8739 !gui.in_use &&
8740#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008741 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 for (row = start_row; row < end_row; ++row)
8743 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008744#ifdef FEAT_MBYTE
8745 if (has_mbyte
8746# ifdef FEAT_GUI
8747 && !gui.in_use
8748# endif
8749 )
8750 {
8751 /* When drawing over the right halve of a double-wide char clear
8752 * out the left halve. When drawing over the left halve of a
8753 * double wide-char clear out the right halve. Only needed in a
8754 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008755 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008756 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008757 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008758 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008759 }
8760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 /*
8762 * Try to use delete-line termcap code, when no attributes or in a
8763 * "normal" terminal, where a bold/italic space is just a
8764 * space.
8765 */
8766 did_delete = FALSE;
8767 if (c2 == ' '
8768 && end_col == Columns
8769 && can_clear(T_CE)
8770 && (attr == 0
8771 || (norm_term
8772 && attr <= HL_ALL
8773 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8774 {
8775 /*
8776 * check if we really need to clear something
8777 */
8778 col = start_col;
8779 if (c1 != ' ') /* don't clear first char */
8780 ++col;
8781
8782 off = LineOffset[row] + col;
8783 end_off = LineOffset[row] + end_col;
8784
8785 /* skip blanks (used often, keep it fast!) */
8786#ifdef FEAT_MBYTE
8787 if (enc_utf8)
8788 while (off < end_off && ScreenLines[off] == ' '
8789 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8790 ++off;
8791 else
8792#endif
8793 while (off < end_off && ScreenLines[off] == ' '
8794 && ScreenAttrs[off] == 0)
8795 ++off;
8796 if (off < end_off) /* something to be cleared */
8797 {
8798 col = off - LineOffset[row];
8799 screen_stop_highlight();
8800 term_windgoto(row, col);/* clear rest of this screen line */
8801 out_str(T_CE);
8802 screen_start(); /* don't know where cursor is now */
8803 col = end_col - col;
8804 while (col--) /* clear chars in ScreenLines */
8805 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008806 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 ++off;
8808 }
8809 }
8810 did_delete = TRUE; /* the chars are cleared now */
8811 }
8812
8813 off = LineOffset[row] + start_col;
8814 c = c1;
8815 for (col = start_col; col < end_col; ++col)
8816 {
8817 if (ScreenLines[off] != c
8818#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008819 || (enc_utf8 && (int)ScreenLinesUC[off]
8820 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821#endif
8822 || ScreenAttrs[off] != attr
8823#if defined(FEAT_GUI) || defined(UNIX)
8824 || force_next
8825#endif
8826 )
8827 {
8828#if defined(FEAT_GUI) || defined(UNIX)
8829 /* The bold trick may make a single row of pixels appear in
8830 * the next character. When a bold character is removed, the
8831 * next character should be redrawn too. This happens for our
8832 * own GUI and for some xterms. */
8833 if (
8834# ifdef FEAT_GUI
8835 gui.in_use
8836# endif
8837# if defined(FEAT_GUI) && defined(UNIX)
8838 ||
8839# endif
8840# ifdef UNIX
8841 term_is_xterm
8842# endif
8843 )
8844 {
8845 if (ScreenLines[off] != ' '
8846 && (ScreenAttrs[off] > HL_ALL
8847 || ScreenAttrs[off] & HL_BOLD))
8848 force_next = TRUE;
8849 else
8850 force_next = FALSE;
8851 }
8852#endif
8853 ScreenLines[off] = c;
8854#ifdef FEAT_MBYTE
8855 if (enc_utf8)
8856 {
8857 if (c >= 0x80)
8858 {
8859 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008860 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 }
8862 else
8863 ScreenLinesUC[off] = 0;
8864 }
8865#endif
8866 ScreenAttrs[off] = attr;
8867 if (!did_delete || c != ' ')
8868 screen_char(off, row, col);
8869 }
8870 ++off;
8871 if (col == start_col)
8872 {
8873 if (did_delete)
8874 break;
8875 c = c2;
8876 }
8877 }
8878 if (end_col == Columns)
8879 LineWraps[row] = FALSE;
8880 if (row == Rows - 1) /* overwritten the command line */
8881 {
8882 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008883 if (start_col == 0 && end_col == Columns
8884 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008886 if (start_col == 0)
8887 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 }
8889 }
8890}
8891
8892/*
8893 * Check if there should be a delay. Used before clearing or redrawing the
8894 * screen or the command line.
8895 */
8896 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008897check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898{
8899 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8900 && !did_wait_return
8901 && emsg_silent == 0)
8902 {
8903 out_flush();
8904 ui_delay(1000L, TRUE);
8905 emsg_on_display = FALSE;
8906 if (check_msg_scroll)
8907 msg_scroll = FALSE;
8908 }
8909}
8910
8911/*
8912 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008913 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 * Returns TRUE if there is a valid screen to write to.
8915 * Returns FALSE when starting up and screen not initialized yet.
8916 */
8917 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008918screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008920 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 return (ScreenLines != NULL);
8922}
8923
8924/*
8925 * Resize the shell to Rows and Columns.
8926 * Allocate ScreenLines[] and associated items.
8927 *
8928 * There may be some time between setting Rows and Columns and (re)allocating
8929 * ScreenLines[]. This happens when starting up and when (manually) changing
8930 * the shell size. Always use screen_Rows and screen_Columns to access items
8931 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8932 * final size of the shell is needed.
8933 */
8934 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008935screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 int new_row, old_row;
8938#ifdef FEAT_GUI
8939 int old_Rows;
8940#endif
8941 win_T *wp;
8942 int outofmem = FALSE;
8943 int len;
8944 schar_T *new_ScreenLines;
8945#ifdef FEAT_MBYTE
8946 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008947 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008949 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950#endif
8951 sattr_T *new_ScreenAttrs;
8952 unsigned *new_LineOffset;
8953 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008954 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008955 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008957 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008958 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008960retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 /*
8962 * Allocation of the screen buffers is done only when the size changes and
8963 * when Rows and Columns have been set and we have started doing full
8964 * screen stuff.
8965 */
8966 if ((ScreenLines != NULL
8967 && Rows == screen_Rows
8968 && Columns == screen_Columns
8969#ifdef FEAT_MBYTE
8970 && enc_utf8 == (ScreenLinesUC != NULL)
8971 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008972 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973#endif
8974 )
8975 || Rows == 0
8976 || Columns == 0
8977 || (!full_screen && ScreenLines == NULL))
8978 return;
8979
8980 /*
8981 * It's possible that we produce an out-of-memory message below, which
8982 * will cause this function to be called again. To break the loop, just
8983 * return here.
8984 */
8985 if (entered)
8986 return;
8987 entered = TRUE;
8988
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008989 /*
8990 * Note that the window sizes are updated before reallocating the arrays,
8991 * thus we must not redraw here!
8992 */
8993 ++RedrawingDisabled;
8994
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 win_new_shellsize(); /* fit the windows in the new sized shell */
8996
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 comp_col(); /* recompute columns for shown command and ruler */
8998
8999 /*
9000 * We're changing the size of the screen.
9001 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9002 * - Move lines from the old arrays into the new arrays, clear extra
9003 * lines (unless the screen is going to be cleared).
9004 * - Free the old arrays.
9005 *
9006 * If anything fails, make ScreenLines NULL, so we don't do anything!
9007 * Continuing with the old ScreenLines may result in a crash, because the
9008 * size is wrong.
9009 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009010 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009012 if (aucmd_win != NULL)
9013 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014
9015 new_ScreenLines = (schar_T *)lalloc((long_u)(
9016 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9017#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01009018 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 if (enc_utf8)
9020 {
9021 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
9022 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009023 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01009024 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
9026 }
9027 if (enc_dbcs == DBCS_JPNU)
9028 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9029 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9030#endif
9031 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9032 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9033 new_LineOffset = (unsigned *)lalloc((long_u)(
9034 Rows * sizeof(unsigned)), FALSE);
9035 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009036 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009038 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 {
9040 if (win_alloc_lines(wp) == FAIL)
9041 {
9042 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009043 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 }
9045 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009046 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9047 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009048 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009049give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009051#ifdef FEAT_MBYTE
9052 for (i = 0; i < p_mco; ++i)
9053 if (new_ScreenLinesC[i] == NULL)
9054 break;
9055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 if (new_ScreenLines == NULL
9057#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009058 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9060#endif
9061 || new_ScreenAttrs == NULL
9062 || new_LineOffset == NULL
9063 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009064 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 || outofmem)
9066 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009067 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009068 {
9069 /* guess the size */
9070 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9071
9072 /* Remember we did this to avoid getting outofmem messages over
9073 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009074 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009075 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009076 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009078 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009079 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009080 VIM_CLEAR(new_ScreenLinesC[i]);
9081 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009083 VIM_CLEAR(new_ScreenAttrs);
9084 VIM_CLEAR(new_LineOffset);
9085 VIM_CLEAR(new_LineWraps);
9086 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 }
9088 else
9089 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009090 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009091
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 for (new_row = 0; new_row < Rows; ++new_row)
9093 {
9094 new_LineOffset[new_row] = new_row * Columns;
9095 new_LineWraps[new_row] = FALSE;
9096
9097 /*
9098 * If the screen is not going to be cleared, copy as much as
9099 * possible from the old screen to the new one and clear the rest
9100 * (used when resizing the window at the "--more--" prompt or when
9101 * executing an external command, for the GUI).
9102 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009103 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 {
9105 (void)vim_memset(new_ScreenLines + new_row * Columns,
9106 ' ', (size_t)Columns * sizeof(schar_T));
9107#ifdef FEAT_MBYTE
9108 if (enc_utf8)
9109 {
9110 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9111 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009112 for (i = 0; i < p_mco; ++i)
9113 (void)vim_memset(new_ScreenLinesC[i]
9114 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 0, (size_t)Columns * sizeof(u8char_T));
9116 }
9117 if (enc_dbcs == DBCS_JPNU)
9118 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9119 0, (size_t)Columns * sizeof(schar_T));
9120#endif
9121 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9122 0, (size_t)Columns * sizeof(sattr_T));
9123 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009124 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 {
9126 if (screen_Columns < Columns)
9127 len = screen_Columns;
9128 else
9129 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009130#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009131 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009132 * may be invalid now. Also when p_mco changes. */
9133 if (!(enc_utf8 && ScreenLinesUC == NULL)
9134 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009135#endif
9136 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9137 ScreenLines + LineOffset[old_row],
9138 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009140 if (enc_utf8 && ScreenLinesUC != NULL
9141 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 {
9143 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9144 ScreenLinesUC + LineOffset[old_row],
9145 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009146 for (i = 0; i < p_mco; ++i)
9147 mch_memmove(new_ScreenLinesC[i]
9148 + new_LineOffset[new_row],
9149 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 (size_t)len * sizeof(u8char_T));
9151 }
9152 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9153 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9154 ScreenLines2 + LineOffset[old_row],
9155 (size_t)len * sizeof(schar_T));
9156#endif
9157 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9158 ScreenAttrs + LineOffset[old_row],
9159 (size_t)len * sizeof(sattr_T));
9160 }
9161 }
9162 }
9163 /* Use the last line of the screen for the current line. */
9164 current_ScreenLine = new_ScreenLines + Rows * Columns;
9165 }
9166
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009167 free_screenlines();
9168
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 ScreenLines = new_ScreenLines;
9170#ifdef FEAT_MBYTE
9171 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009172 for (i = 0; i < p_mco; ++i)
9173 ScreenLinesC[i] = new_ScreenLinesC[i];
9174 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 ScreenLines2 = new_ScreenLines2;
9176#endif
9177 ScreenAttrs = new_ScreenAttrs;
9178 LineOffset = new_LineOffset;
9179 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009180 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181
9182 /* It's important that screen_Rows and screen_Columns reflect the actual
9183 * size of ScreenLines[]. Set them before calling anything. */
9184#ifdef FEAT_GUI
9185 old_Rows = screen_Rows;
9186#endif
9187 screen_Rows = Rows;
9188 screen_Columns = Columns;
9189
9190 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009191 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 screenclear2();
9193
9194#ifdef FEAT_GUI
9195 else if (gui.in_use
9196 && !gui.starting
9197 && ScreenLines != NULL
9198 && old_Rows != Rows)
9199 {
9200 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9201 /*
9202 * Adjust the position of the cursor, for when executing an external
9203 * command.
9204 */
9205 if (msg_row >= Rows) /* Rows got smaller */
9206 msg_row = Rows - 1; /* put cursor at last row */
9207 else if (Rows > old_Rows) /* Rows got bigger */
9208 msg_row += Rows - old_Rows; /* put cursor in same place */
9209 if (msg_col >= Columns) /* Columns got smaller */
9210 msg_col = Columns - 1; /* put cursor at last column */
9211 }
9212#endif
9213
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009215 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009216
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009217 /*
9218 * Do not apply autocommands more than 3 times to avoid an endless loop
9219 * in case applying autocommands always changes Rows or Columns.
9220 */
9221 if (starting == 0 && ++retry_count <= 3)
9222 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009223 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009224 /* In rare cases, autocommands may have altered Rows or Columns,
9225 * jump back to check if we need to allocate the screen again. */
9226 goto retry;
9227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228}
9229
9230 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009231free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009232{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009233#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009234 int i;
9235
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009236 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009237 for (i = 0; i < Screen_mco; ++i)
9238 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009239 vim_free(ScreenLines2);
9240#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009241 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009242 vim_free(ScreenAttrs);
9243 vim_free(LineOffset);
9244 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009245 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009246}
9247
9248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009249screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250{
9251 check_for_delay(FALSE);
9252 screenalloc(FALSE); /* allocate screen buffers if size changed */
9253 screenclear2(); /* clear the screen */
9254}
9255
9256 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009257screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 int i;
9260
9261 if (starting == NO_SCREEN || ScreenLines == NULL
9262#ifdef FEAT_GUI
9263 || (gui.in_use && gui.starting)
9264#endif
9265 )
9266 return;
9267
9268#ifdef FEAT_GUI
9269 if (!gui.in_use)
9270#endif
9271 screen_attr = -1; /* force setting the Normal colors */
9272 screen_stop_highlight(); /* don't want highlighting here */
9273
9274#ifdef FEAT_CLIPBOARD
9275 /* disable selection without redrawing it */
9276 clip_scroll_selection(9999);
9277#endif
9278
9279 /* blank out ScreenLines */
9280 for (i = 0; i < Rows; ++i)
9281 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009282 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 LineWraps[i] = FALSE;
9284 }
9285
9286 if (can_clear(T_CL))
9287 {
9288 out_str(T_CL); /* clear the display */
9289 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009290 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 }
9292 else
9293 {
9294 /* can't clear the screen, mark all chars with invalid attributes */
9295 for (i = 0; i < Rows; ++i)
9296 lineinvalid(LineOffset[i], (int)Columns);
9297 clear_cmdline = TRUE;
9298 }
9299
9300 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9301
9302 win_rest_invalid(firstwin);
9303 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009304 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 if (must_redraw == CLEAR) /* no need to clear again */
9306 must_redraw = NOT_VALID;
9307 compute_cmdrow();
9308 msg_row = cmdline_row; /* put cursor on last line for messages */
9309 msg_col = 0;
9310 screen_start(); /* don't know where cursor is now */
9311 msg_scrolled = 0; /* can't scroll back */
9312 msg_didany = FALSE;
9313 msg_didout = FALSE;
9314}
9315
9316/*
9317 * Clear one line in ScreenLines.
9318 */
9319 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009320lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9323#ifdef FEAT_MBYTE
9324 if (enc_utf8)
9325 (void)vim_memset(ScreenLinesUC + off, 0,
9326 (size_t)width * sizeof(u8char_T));
9327#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009328 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329}
9330
9331/*
9332 * Mark one line in ScreenLines invalid by setting the attributes to an
9333 * invalid value.
9334 */
9335 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009336lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9339}
9340
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341/*
9342 * Copy part of a Screenline for vertically split window "wp".
9343 */
9344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009345linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 unsigned off_to = LineOffset[to] + wp->w_wincol;
9348 unsigned off_from = LineOffset[from] + wp->w_wincol;
9349
9350 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9351 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009352#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 if (enc_utf8)
9354 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009355 int i;
9356
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9358 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009359 for (i = 0; i < p_mco; ++i)
9360 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9361 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 }
9363 if (enc_dbcs == DBCS_JPNU)
9364 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9365 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9368 wp->w_width * sizeof(sattr_T));
9369}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370
9371/*
9372 * Return TRUE if clearing with term string "p" would work.
9373 * It can't work when the string is empty or it won't set the right background.
9374 */
9375 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009376can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377{
9378 return (*p != NUL && (t_colors <= 1
9379#ifdef FEAT_GUI
9380 || gui.in_use
9381#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009382#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009383 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009384 || (!p_tgc && cterm_normal_bg_color == 0)
9385#else
9386 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009387#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009388 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389}
9390
9391/*
9392 * Reset cursor position. Use whenever cursor was moved because of outputting
9393 * something directly to the screen (shell commands) or a terminal control
9394 * code.
9395 */
9396 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009397screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398{
9399 screen_cur_row = screen_cur_col = 9999;
9400}
9401
9402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 * Move the cursor to position "row","col" in the screen.
9404 * This tries to find the most efficient way to move, minimizing the number of
9405 * characters sent to the terminal.
9406 */
9407 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009408windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009410 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 int i;
9412 int plan;
9413 int cost;
9414 int wouldbe_col;
9415 int noinvcurs;
9416 char_u *bs;
9417 int goto_cost;
9418 int attr;
9419
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009420#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9422
9423#define PLAN_LE 1
9424#define PLAN_CR 2
9425#define PLAN_NL 3
9426#define PLAN_WRITE 4
9427 /* Can't use ScreenLines unless initialized */
9428 if (ScreenLines == NULL)
9429 return;
9430
9431 if (col != screen_cur_col || row != screen_cur_row)
9432 {
9433 /* Check for valid position. */
9434 if (row < 0) /* window without text lines? */
9435 row = 0;
9436 if (row >= screen_Rows)
9437 row = screen_Rows - 1;
9438 if (col >= screen_Columns)
9439 col = screen_Columns - 1;
9440
9441 /* check if no cursor movement is allowed in highlight mode */
9442 if (screen_attr && *T_MS == NUL)
9443 noinvcurs = HIGHL_COST;
9444 else
9445 noinvcurs = 0;
9446 goto_cost = GOTO_COST + noinvcurs;
9447
9448 /*
9449 * Plan how to do the positioning:
9450 * 1. Use CR to move it to column 0, same row.
9451 * 2. Use T_LE to move it a few columns to the left.
9452 * 3. Use NL to move a few lines down, column 0.
9453 * 4. Move a few columns to the right with T_ND or by writing chars.
9454 *
9455 * Don't do this if the cursor went beyond the last column, the cursor
9456 * position is unknown then (some terminals wrap, some don't )
9457 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009458 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 * characters to move the cursor to the right.
9460 */
9461 if (row >= screen_cur_row && screen_cur_col < Columns)
9462 {
9463 /*
9464 * If the cursor is in the same row, bigger col, we can use CR
9465 * or T_LE.
9466 */
9467 bs = NULL; /* init for GCC */
9468 attr = screen_attr;
9469 if (row == screen_cur_row && col < screen_cur_col)
9470 {
9471 /* "le" is preferred over "bc", because "bc" is obsolete */
9472 if (*T_LE)
9473 bs = T_LE; /* "cursor left" */
9474 else
9475 bs = T_BC; /* "backspace character (old) */
9476 if (*bs)
9477 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9478 else
9479 cost = 999;
9480 if (col + 1 < cost) /* using CR is less characters */
9481 {
9482 plan = PLAN_CR;
9483 wouldbe_col = 0;
9484 cost = 1; /* CR is just one character */
9485 }
9486 else
9487 {
9488 plan = PLAN_LE;
9489 wouldbe_col = col;
9490 }
9491 if (noinvcurs) /* will stop highlighting */
9492 {
9493 cost += noinvcurs;
9494 attr = 0;
9495 }
9496 }
9497
9498 /*
9499 * If the cursor is above where we want to be, we can use CR LF.
9500 */
9501 else if (row > screen_cur_row)
9502 {
9503 plan = PLAN_NL;
9504 wouldbe_col = 0;
9505 cost = (row - screen_cur_row) * 2; /* CR LF */
9506 if (noinvcurs) /* will stop highlighting */
9507 {
9508 cost += noinvcurs;
9509 attr = 0;
9510 }
9511 }
9512
9513 /*
9514 * If the cursor is in the same row, smaller col, just use write.
9515 */
9516 else
9517 {
9518 plan = PLAN_WRITE;
9519 wouldbe_col = screen_cur_col;
9520 cost = 0;
9521 }
9522
9523 /*
9524 * Check if any characters that need to be written have the
9525 * correct attributes. Also avoid UTF-8 characters.
9526 */
9527 i = col - wouldbe_col;
9528 if (i > 0)
9529 cost += i;
9530 if (cost < goto_cost && i > 0)
9531 {
9532 /*
9533 * Check if the attributes are correct without additionally
9534 * stopping highlighting.
9535 */
9536 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9537 while (i && *p++ == attr)
9538 --i;
9539 if (i != 0)
9540 {
9541 /*
9542 * Try if it works when highlighting is stopped here.
9543 */
9544 if (*--p == 0)
9545 {
9546 cost += noinvcurs;
9547 while (i && *p++ == 0)
9548 --i;
9549 }
9550 if (i != 0)
9551 cost = 999; /* different attributes, don't do it */
9552 }
9553#ifdef FEAT_MBYTE
9554 if (enc_utf8)
9555 {
9556 /* Don't use an UTF-8 char for positioning, it's slow. */
9557 for (i = wouldbe_col; i < col; ++i)
9558 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9559 {
9560 cost = 999;
9561 break;
9562 }
9563 }
9564#endif
9565 }
9566
9567 /*
9568 * We can do it without term_windgoto()!
9569 */
9570 if (cost < goto_cost)
9571 {
9572 if (plan == PLAN_LE)
9573 {
9574 if (noinvcurs)
9575 screen_stop_highlight();
9576 while (screen_cur_col > col)
9577 {
9578 out_str(bs);
9579 --screen_cur_col;
9580 }
9581 }
9582 else if (plan == PLAN_CR)
9583 {
9584 if (noinvcurs)
9585 screen_stop_highlight();
9586 out_char('\r');
9587 screen_cur_col = 0;
9588 }
9589 else if (plan == PLAN_NL)
9590 {
9591 if (noinvcurs)
9592 screen_stop_highlight();
9593 while (screen_cur_row < row)
9594 {
9595 out_char('\n');
9596 ++screen_cur_row;
9597 }
9598 screen_cur_col = 0;
9599 }
9600
9601 i = col - screen_cur_col;
9602 if (i > 0)
9603 {
9604 /*
9605 * Use cursor-right if it's one character only. Avoids
9606 * removing a line of pixels from the last bold char, when
9607 * using the bold trick in the GUI.
9608 */
9609 if (T_ND[0] != NUL && T_ND[1] == NUL)
9610 {
9611 while (i-- > 0)
9612 out_char(*T_ND);
9613 }
9614 else
9615 {
9616 int off;
9617
9618 off = LineOffset[row] + screen_cur_col;
9619 while (i-- > 0)
9620 {
9621 if (ScreenAttrs[off] != screen_attr)
9622 screen_stop_highlight();
9623#ifdef FEAT_MBYTE
9624 out_flush_check();
9625#endif
9626 out_char(ScreenLines[off]);
9627#ifdef FEAT_MBYTE
9628 if (enc_dbcs == DBCS_JPNU
9629 && ScreenLines[off] == 0x8e)
9630 out_char(ScreenLines2[off]);
9631#endif
9632 ++off;
9633 }
9634 }
9635 }
9636 }
9637 }
9638 else
9639 cost = 999;
9640
9641 if (cost >= goto_cost)
9642 {
9643 if (noinvcurs)
9644 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009645 if (row == screen_cur_row && (col > screen_cur_col)
9646 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 term_cursor_right(col - screen_cur_col);
9648 else
9649 term_windgoto(row, col);
9650 }
9651 screen_cur_row = row;
9652 screen_cur_col = col;
9653 }
9654}
9655
9656/*
9657 * Set cursor to its position in the current window.
9658 */
9659 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009660setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009662 setcursor_mayforce(FALSE);
9663}
9664
9665/*
9666 * Set cursor to its position in the current window.
9667 * When "force" is TRUE also when not redrawing.
9668 */
9669 void
9670setcursor_mayforce(int force)
9671{
9672 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 {
9674 validate_cursor();
9675 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009676 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009678 /* With 'rightleft' set and the cursor on a double-wide
9679 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009680 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009682 (has_mbyte
9683 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9684 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685# endif
9686 1)) :
9687#endif
9688 curwin->w_wcol));
9689 }
9690}
9691
9692
9693/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009694 * Insert 'line_count' lines at 'row' in window 'wp'.
9695 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9696 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 * scrolling.
9698 * Returns FAIL if the lines are not inserted, OK for success.
9699 */
9700 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009701win_ins_lines(
9702 win_T *wp,
9703 int row,
9704 int line_count,
9705 int invalid,
9706 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708 int did_delete;
9709 int nextrow;
9710 int lastrow;
9711 int retval;
9712
9713 if (invalid)
9714 wp->w_lines_valid = 0;
9715
9716 if (wp->w_height < 5)
9717 return FAIL;
9718
9719 if (line_count > wp->w_height - row)
9720 line_count = wp->w_height - row;
9721
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009722 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 if (retval != MAYBE)
9724 return retval;
9725
9726 /*
9727 * If there is a next window or a status line, we first try to delete the
9728 * lines at the bottom to avoid messing what is after the window.
9729 * If this fails and there are following windows, don't do anything to avoid
9730 * messing up those windows, better just redraw.
9731 */
9732 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (wp->w_next != NULL || wp->w_status_height)
9734 {
9735 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009736 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 did_delete = TRUE;
9738 else if (wp->w_next)
9739 return FAIL;
9740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 /*
9742 * if no lines deleted, blank the lines that will end up below the window
9743 */
9744 if (!did_delete)
9745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009748 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 lastrow = nextrow + line_count;
9750 if (lastrow > Rows)
9751 lastrow = Rows;
9752 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009753 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 ' ', ' ', 0);
9755 }
9756
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009757 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 == FAIL)
9759 {
9760 /* deletion will have messed up other windows */
9761 if (did_delete)
9762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 win_rest_invalid(W_NEXT(wp));
9765 }
9766 return FAIL;
9767 }
9768
9769 return OK;
9770}
9771
9772/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009773 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9775 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9776 * scrolling
9777 * Return OK for success, FAIL if the lines are not deleted.
9778 */
9779 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009780win_del_lines(
9781 win_T *wp,
9782 int row,
9783 int line_count,
9784 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009785 int mayclear,
9786 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
9788 int retval;
9789
9790 if (invalid)
9791 wp->w_lines_valid = 0;
9792
9793 if (line_count > wp->w_height - row)
9794 line_count = wp->w_height - row;
9795
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009796 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 if (retval != MAYBE)
9798 return retval;
9799
9800 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009801 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 return FAIL;
9803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 /*
9805 * If there are windows or status lines below, try to put them at the
9806 * correct place. If we can't do that, they have to be redrawn.
9807 */
9808 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9809 {
9810 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009811 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 {
9813 wp->w_redr_status = TRUE;
9814 win_rest_invalid(wp->w_next);
9815 }
9816 }
9817 /*
9818 * If this is the last window and there is no status line, redraw the
9819 * command line later.
9820 */
9821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 redraw_cmdline = TRUE;
9823 return OK;
9824}
9825
9826/*
9827 * Common code for win_ins_lines() and win_del_lines().
9828 * Returns OK or FAIL when the work has been done.
9829 * Returns MAYBE when not finished yet.
9830 */
9831 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009832win_do_lines(
9833 win_T *wp,
9834 int row,
9835 int line_count,
9836 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009837 int del,
9838 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
9840 int retval;
9841
9842 if (!redrawing() || line_count <= 0)
9843 return FAIL;
9844
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009845 /* When inserting lines would result in loss of command output, just redraw
9846 * the lines. */
9847 if (no_win_do_lines_ins && !del)
9848 return FAIL;
9849
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009851 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009853 if (!no_win_do_lines_ins)
9854 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 return FAIL;
9856 }
9857
9858 /*
9859 * Delete all remaining lines
9860 */
9861 if (row + line_count >= wp->w_height)
9862 {
9863 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009864 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 ' ', ' ', 0);
9866 return OK;
9867 }
9868
9869 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009870 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009872 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009874 if (!no_win_do_lines_ins)
9875 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876
9877 /*
9878 * If the terminal can set a scroll region, use that.
9879 * Always do this in a vertically split window. This will redraw from
9880 * ScreenLines[] when t_CV isn't defined. That's faster than using
9881 * win_line().
9882 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009883 * a character in the lower right corner of the scroll region may cause a
9884 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009886 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 scroll_region_set(wp, row);
9890 if (del)
9891 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009892 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 else
9894 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009895 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 scroll_region_reset();
9898 return retval;
9899 }
9900
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9902 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903
9904 return MAYBE;
9905}
9906
9907/*
9908 * window 'wp' and everything after it is messed up, mark it for redraw
9909 */
9910 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009911win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 {
9915 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 wp->w_redr_status = TRUE;
9917 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 }
9919 redraw_cmdline = TRUE;
9920}
9921
9922/*
9923 * The rest of the routines in this file perform screen manipulations. The
9924 * given operation is performed physically on the screen. The corresponding
9925 * change is also made to the internal screen image. In this way, the editor
9926 * anticipates the effect of editing changes on the appearance of the screen.
9927 * That way, when we call screenupdate a complete redraw isn't usually
9928 * necessary. Another advantage is that we can keep adding code to anticipate
9929 * screen changes, and in the meantime, everything still works.
9930 */
9931
9932/*
9933 * types for inserting or deleting lines
9934 */
9935#define USE_T_CAL 1
9936#define USE_T_CDL 2
9937#define USE_T_AL 3
9938#define USE_T_CE 4
9939#define USE_T_DL 5
9940#define USE_T_SR 6
9941#define USE_NL 7
9942#define USE_T_CD 8
9943#define USE_REDRAW 9
9944
9945/*
9946 * insert lines on the screen and update ScreenLines[]
9947 * 'end' is the line after the scrolled part. Normally it is Rows.
9948 * When scrolling region used 'off' is the offset from the top for the region.
9949 * 'row' and 'end' are relative to the start of the region.
9950 *
9951 * return FAIL for failure, OK for success.
9952 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009953 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009954screen_ins_lines(
9955 int off,
9956 int row,
9957 int line_count,
9958 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009959 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009960 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961{
9962 int i;
9963 int j;
9964 unsigned temp;
9965 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009966 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 int type;
9968 int result_empty;
9969 int can_ce = can_clear(T_CE);
9970
9971 /*
9972 * FAIL if
9973 * - there is no valid screen
9974 * - the screen has to be redrawn completely
9975 * - the line count is less than one
9976 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009977 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009979 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9980#ifdef FEAT_CLIPBOARD
9981 || (clip_star.state != SELECT_CLEARED
9982 && redrawing_for_callback > 0)
9983#endif
9984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 return FAIL;
9986
9987 /*
9988 * There are seven ways to insert lines:
9989 * 0. When in a vertically split window and t_CV isn't set, redraw the
9990 * characters from ScreenLines[].
9991 * 1. Use T_CD (clear to end of display) if it exists and the result of
9992 * the insert is just empty lines
9993 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9994 * present or line_count > 1. It looks better if we do all the inserts
9995 * at once.
9996 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9997 * insert is just empty lines and T_CE is not present or line_count >
9998 * 1.
9999 * 4. Use T_AL (insert line) if it exists.
10000 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10001 * just empty lines.
10002 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10003 * just empty lines.
10004 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10005 * the 'da' flag is not set or we have clear line capability.
10006 * 8. redraw the characters from ScreenLines[].
10007 *
10008 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10009 * the scrollbar for the window. It does have insert line, use that if it
10010 * exists.
10011 */
10012 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10014 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010015 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 type = USE_T_CD;
10017 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10018 type = USE_T_CAL;
10019 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10020 type = USE_T_CDL;
10021 else if (*T_AL != NUL)
10022 type = USE_T_AL;
10023 else if (can_ce && result_empty)
10024 type = USE_T_CE;
10025 else if (*T_DL != NUL && result_empty)
10026 type = USE_T_DL;
10027 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10028 type = USE_T_SR;
10029 else
10030 return FAIL;
10031
10032 /*
10033 * For clearing the lines screen_del_lines() is used. This will also take
10034 * care of t_db if necessary.
10035 */
10036 if (type == USE_T_CD || type == USE_T_CDL ||
10037 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010038 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039
10040 /*
10041 * If text is retained below the screen, first clear or delete as many
10042 * lines at the bottom of the window as are about to be inserted so that
10043 * the deleted lines won't later surface during a screen_del_lines.
10044 */
10045 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010046 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
10048#ifdef FEAT_CLIPBOARD
10049 /* Remove a modeless selection when inserting lines halfway the screen
10050 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010051 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010052 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 else
10054 clip_scroll_selection(-line_count);
10055#endif
10056
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057#ifdef FEAT_GUI
10058 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10059 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010060 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061#endif
10062
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010063 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10064 cursor_col = wp->w_wincol;
10065
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 if (*T_CCS != NUL) /* cursor relative to region */
10067 cursor_row = row;
10068 else
10069 cursor_row = row + off;
10070
10071 /*
10072 * Shift LineOffset[] line_count down to reflect the inserted lines.
10073 * Clear the inserted lines in ScreenLines[].
10074 */
10075 row += off;
10076 end += off;
10077 for (i = 0; i < line_count; ++i)
10078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 if (wp != NULL && wp->w_width != Columns)
10080 {
10081 /* need to copy part of a line */
10082 j = end - 1 - i;
10083 while ((j -= line_count) >= row)
10084 linecopy(j + line_count, j, wp);
10085 j += line_count;
10086 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010087 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10088 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 else
10090 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10091 LineWraps[j] = FALSE;
10092 }
10093 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 {
10095 j = end - 1 - i;
10096 temp = LineOffset[j];
10097 while ((j -= line_count) >= row)
10098 {
10099 LineOffset[j + line_count] = LineOffset[j];
10100 LineWraps[j + line_count] = LineWraps[j];
10101 }
10102 LineOffset[j + line_count] = temp;
10103 LineWraps[j + line_count] = FALSE;
10104 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010105 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 else
10107 lineinvalid(temp, (int)Columns);
10108 }
10109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110
10111 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010112 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010113 if (clear_attr != 0)
10114 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 /* redraw the characters */
10117 if (type == USE_REDRAW)
10118 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010119 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 {
10121 term_append_lines(line_count);
10122 screen_start(); /* don't know where cursor is now */
10123 }
10124 else
10125 {
10126 for (i = 0; i < line_count; i++)
10127 {
10128 if (type == USE_T_AL)
10129 {
10130 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010131 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 out_str(T_AL);
10133 }
10134 else /* type == USE_T_SR */
10135 out_str(T_SR);
10136 screen_start(); /* don't know where cursor is now */
10137 }
10138 }
10139
10140 /*
10141 * With scroll-reverse and 'da' flag set we need to clear the lines that
10142 * have been scrolled down into the region.
10143 */
10144 if (type == USE_T_SR && *T_DA)
10145 {
10146 for (i = 0; i < line_count; ++i)
10147 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010148 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 out_str(T_CE);
10150 screen_start(); /* don't know where cursor is now */
10151 }
10152 }
10153
10154#ifdef FEAT_GUI
10155 gui_can_update_cursor();
10156 if (gui.in_use)
10157 out_flush(); /* always flush after a scroll */
10158#endif
10159 return OK;
10160}
10161
10162/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010163 * Delete lines on the screen and update ScreenLines[].
10164 * "end" is the line after the scrolled part. Normally it is Rows.
10165 * When scrolling region used "off" is the offset from the top for the region.
10166 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 *
10168 * Return OK for success, FAIL if the lines are not deleted.
10169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010171screen_del_lines(
10172 int off,
10173 int row,
10174 int line_count,
10175 int end,
10176 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010177 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010178 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180 int j;
10181 int i;
10182 unsigned temp;
10183 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010184 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 int cursor_end;
10186 int result_empty; /* result is empty until end of region */
10187 int can_delete; /* deleting line codes can be used */
10188 int type;
10189
10190 /*
10191 * FAIL if
10192 * - there is no valid screen
10193 * - the screen has to be redrawn completely
10194 * - the line count is less than one
10195 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010196 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010198 if (!screen_valid(TRUE) || line_count <= 0
10199 || (!force && line_count > p_ttyscroll)
10200#ifdef FEAT_CLIPBOARD
10201 || (clip_star.state != SELECT_CLEARED
10202 && redrawing_for_callback > 0)
10203#endif
10204 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 return FAIL;
10206
10207 /*
10208 * Check if the rest of the current region will become empty.
10209 */
10210 result_empty = row + line_count >= end;
10211
10212 /*
10213 * We can delete lines only when 'db' flag not set or when 'ce' option
10214 * available.
10215 */
10216 can_delete = (*T_DB == NUL || can_clear(T_CE));
10217
10218 /*
10219 * There are six ways to delete lines:
10220 * 0. When in a vertically split window and t_CV isn't set, redraw the
10221 * characters from ScreenLines[].
10222 * 1. Use T_CD if it exists and the result is empty.
10223 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10224 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10225 * none of the other ways work.
10226 * 4. Use T_CE (erase line) if the result is empty.
10227 * 5. Use T_DL (delete line) if it exists.
10228 * 6. redraw the characters from ScreenLines[].
10229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10231 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010232 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 type = USE_T_CD;
10234#if defined(__BEOS__) && defined(BEOS_DR8)
10235 /*
10236 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10237 * its internal termcap... this works okay for tests which test *T_DB !=
10238 * NUL. It has the disadvantage that the user cannot use any :set t_*
10239 * command to get T_DB (back) to empty_option, only :set term=... will do
10240 * the trick...
10241 * Anyway, this hack will hopefully go away with the next OS release.
10242 * (Olaf Seibert)
10243 */
10244 else if (row == 0 && T_DB == empty_option
10245 && (line_count == 1 || *T_CDL == NUL))
10246#else
10247 else if (row == 0 && (
10248#ifndef AMIGA
10249 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10250 * up, so use delete-line command */
10251 line_count == 1 ||
10252#endif
10253 *T_CDL == NUL))
10254#endif
10255 type = USE_NL;
10256 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10257 type = USE_T_CDL;
10258 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010259 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 type = USE_T_CE;
10261 else if (*T_DL != NUL && can_delete)
10262 type = USE_T_DL;
10263 else if (*T_CDL != NUL && can_delete)
10264 type = USE_T_CDL;
10265 else
10266 return FAIL;
10267
10268#ifdef FEAT_CLIPBOARD
10269 /* Remove a modeless selection when deleting lines halfway the screen or
10270 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010271 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010272 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 else
10274 clip_scroll_selection(line_count);
10275#endif
10276
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277#ifdef FEAT_GUI
10278 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10279 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010280 gui_dont_update_cursor(gui.cursor_row >= row + off
10281 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282#endif
10283
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010284 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10285 cursor_col = wp->w_wincol;
10286
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 if (*T_CCS != NUL) /* cursor relative to region */
10288 {
10289 cursor_row = row;
10290 cursor_end = end;
10291 }
10292 else
10293 {
10294 cursor_row = row + off;
10295 cursor_end = end + off;
10296 }
10297
10298 /*
10299 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10300 * Clear the inserted lines in ScreenLines[].
10301 */
10302 row += off;
10303 end += off;
10304 for (i = 0; i < line_count; ++i)
10305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 if (wp != NULL && wp->w_width != Columns)
10307 {
10308 /* need to copy part of a line */
10309 j = row + i;
10310 while ((j += line_count) <= end - 1)
10311 linecopy(j - line_count, j, wp);
10312 j -= line_count;
10313 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010314 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10315 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 else
10317 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10318 LineWraps[j] = FALSE;
10319 }
10320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 {
10322 /* whole width, moving the line pointers is faster */
10323 j = row + i;
10324 temp = LineOffset[j];
10325 while ((j += line_count) <= end - 1)
10326 {
10327 LineOffset[j - line_count] = LineOffset[j];
10328 LineWraps[j - line_count] = LineWraps[j];
10329 }
10330 LineOffset[j - line_count] = temp;
10331 LineWraps[j - line_count] = FALSE;
10332 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010333 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 else
10335 lineinvalid(temp, (int)Columns);
10336 }
10337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010339 if (screen_attr != clear_attr)
10340 screen_stop_highlight();
10341 if (clear_attr != 0)
10342 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 /* redraw the characters */
10345 if (type == USE_REDRAW)
10346 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010347 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010349 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 out_str(T_CD);
10351 screen_start(); /* don't know where cursor is now */
10352 }
10353 else if (type == USE_T_CDL)
10354 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010355 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 term_delete_lines(line_count);
10357 screen_start(); /* don't know where cursor is now */
10358 }
10359 /*
10360 * Deleting lines at top of the screen or scroll region: Just scroll
10361 * the whole screen (scroll region) up by outputting newlines on the
10362 * last line.
10363 */
10364 else if (type == USE_NL)
10365 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010366 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 for (i = line_count; --i >= 0; )
10368 out_char('\n'); /* cursor will remain on same line */
10369 }
10370 else
10371 {
10372 for (i = line_count; --i >= 0; )
10373 {
10374 if (type == USE_T_DL)
10375 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010376 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 out_str(T_DL); /* delete a line */
10378 }
10379 else /* type == USE_T_CE */
10380 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010381 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 out_str(T_CE); /* erase a line */
10383 }
10384 screen_start(); /* don't know where cursor is now */
10385 }
10386 }
10387
10388 /*
10389 * If the 'db' flag is set, we need to clear the lines that have been
10390 * scrolled up at the bottom of the region.
10391 */
10392 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10393 {
10394 for (i = line_count; i > 0; --i)
10395 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010396 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 out_str(T_CE); /* erase a line */
10398 screen_start(); /* don't know where cursor is now */
10399 }
10400 }
10401
10402#ifdef FEAT_GUI
10403 gui_can_update_cursor();
10404 if (gui.in_use)
10405 out_flush(); /* always flush after a scroll */
10406#endif
10407
10408 return OK;
10409}
10410
10411/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010412 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 *
10414 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10415 * If clear_cmdline is FALSE there may be a message there that needs to be
10416 * cleared only if a mode is shown.
10417 * Return the length of the message (0 if no message).
10418 */
10419 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010420showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421{
10422 int need_clear;
10423 int length = 0;
10424 int do_mode;
10425 int attr;
10426 int nwr_save;
10427#ifdef FEAT_INS_EXPAND
10428 int sub_attr;
10429#endif
10430
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010431 do_mode = ((p_smd && msg_silent == 0)
10432 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010433 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010434 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010435 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 {
10437 /*
10438 * Don't show mode right now, when not redrawing or inside a mapping.
10439 * Call char_avail() only when we are going to show something, because
10440 * it takes a bit of time.
10441 */
10442 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10443 {
10444 redraw_cmdline = TRUE; /* show mode later */
10445 return 0;
10446 }
10447
10448 nwr_save = need_wait_return;
10449
10450 /* wait a bit before overwriting an important message */
10451 check_for_delay(FALSE);
10452
10453 /* if the cmdline is more than one line high, erase top lines */
10454 need_clear = clear_cmdline;
10455 if (clear_cmdline && cmdline_row < Rows - 1)
10456 msg_clr_cmdline(); /* will reset clear_cmdline */
10457
10458 /* Position on the last line in the window, column 0 */
10459 msg_pos_mode();
10460 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010461 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 if (do_mode)
10463 {
10464 MSG_PUTS_ATTR("--", attr);
10465#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010466 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010467# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010468 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010469# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010470 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010471# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010472 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010473# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 MSG_PUTS_ATTR(" IM", attr);
10475# else
10476 MSG_PUTS_ATTR(" XIM", attr);
10477# endif
10478#endif
10479#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10480 if (gui.in_use)
10481 {
10482 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010483 {
10484 /* HANGUL */
10485 if (enc_utf8)
10486 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10487 else
10488 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 }
10491#endif
10492#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010493 /* CTRL-X in Insert mode */
10494 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 {
10496 /* These messages can get long, avoid a wrap in a narrow
10497 * window. Prefer showing edit_submode_extra. */
10498 length = (Rows - msg_row) * Columns - 3;
10499 if (edit_submode_extra != NULL)
10500 length -= vim_strsize(edit_submode_extra);
10501 if (length > 0)
10502 {
10503 if (edit_submode_pre != NULL)
10504 length -= vim_strsize(edit_submode_pre);
10505 if (length - vim_strsize(edit_submode) > 0)
10506 {
10507 if (edit_submode_pre != NULL)
10508 msg_puts_attr(edit_submode_pre, attr);
10509 msg_puts_attr(edit_submode, attr);
10510 }
10511 if (edit_submode_extra != NULL)
10512 {
10513 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10514 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010515 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 else
10517 sub_attr = attr;
10518 msg_puts_attr(edit_submode_extra, sub_attr);
10519 }
10520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 }
10522 else
10523#endif
10524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 if (State & VREPLACE_FLAG)
10526 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010527 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10529 else if (State & INSERT)
10530 {
10531#ifdef FEAT_RIGHTLEFT
10532 if (p_ri)
10533 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10534#endif
10535 MSG_PUTS_ATTR(_(" INSERT"), attr);
10536 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010537 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 MSG_PUTS_ATTR(_(" (insert)"), attr);
10539 else if (restart_edit == 'R')
10540 MSG_PUTS_ATTR(_(" (replace)"), attr);
10541 else if (restart_edit == 'V')
10542 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10543#ifdef FEAT_RIGHTLEFT
10544 if (p_hkmap)
10545 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10546# ifdef FEAT_FKMAP
10547 if (p_fkmap)
10548 MSG_PUTS_ATTR(farsi_text_5, attr);
10549# endif
10550#endif
10551#ifdef FEAT_KEYMAP
10552 if (State & LANGMAP)
10553 {
10554# ifdef FEAT_ARABIC
10555 if (curwin->w_p_arab)
10556 MSG_PUTS_ATTR(_(" Arabic"), attr);
10557 else
10558# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010559 if (get_keymap_str(curwin, (char_u *)" (%s)",
10560 NameBuff, MAXPATHL))
10561 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 }
10563#endif
10564 if ((State & INSERT) && p_paste)
10565 MSG_PUTS_ATTR(_(" (paste)"), attr);
10566
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 if (VIsual_active)
10568 {
10569 char *p;
10570
10571 /* Don't concatenate separate words to avoid translation
10572 * problems. */
10573 switch ((VIsual_select ? 4 : 0)
10574 + (VIsual_mode == Ctrl_V) * 2
10575 + (VIsual_mode == 'V'))
10576 {
10577 case 0: p = N_(" VISUAL"); break;
10578 case 1: p = N_(" VISUAL LINE"); break;
10579 case 2: p = N_(" VISUAL BLOCK"); break;
10580 case 4: p = N_(" SELECT"); break;
10581 case 5: p = N_(" SELECT LINE"); break;
10582 default: p = N_(" SELECT BLOCK"); break;
10583 }
10584 MSG_PUTS_ATTR(_(p), attr);
10585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 MSG_PUTS_ATTR(" --", attr);
10587 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010588
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 need_clear = TRUE;
10590 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010591 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592#ifdef FEAT_INS_EXPAND
10593 && edit_submode == NULL /* otherwise it gets too long */
10594#endif
10595 )
10596 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010597 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 need_clear = TRUE;
10599 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010600
10601 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 if (need_clear || clear_cmdline)
10603 msg_clr_eos();
10604 msg_didout = FALSE; /* overwrite this message */
10605 length = msg_col;
10606 msg_col = 0;
10607 need_wait_return = nwr_save; /* never ask for hit-return for this */
10608 }
10609 else if (clear_cmdline && msg_silent == 0)
10610 /* Clear the whole command line. Will reset "clear_cmdline". */
10611 msg_clr_cmdline();
10612
10613#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 /* In Visual mode the size of the selected area must be redrawn. */
10615 if (VIsual_active)
10616 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617
10618 /* If the last window has no status line, the ruler is after the mode
10619 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010620 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010621 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622#endif
10623 redraw_cmdline = FALSE;
10624 clear_cmdline = FALSE;
10625
10626 return length;
10627}
10628
10629/*
10630 * Position for a mode message.
10631 */
10632 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010633msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
10635 msg_col = 0;
10636 msg_row = Rows - 1;
10637}
10638
10639/*
10640 * Delete mode message. Used when ESC is typed which is expected to end
10641 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010642 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 */
10644 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010645unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
10647 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010648 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 */
10650 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10651 redraw_cmdline = TRUE; /* delete mode later */
10652 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010653 clearmode();
10654}
10655
10656/*
10657 * Clear the mode message.
10658 */
10659 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010660clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010661{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010662 int save_msg_row = msg_row;
10663 int save_msg_col = msg_col;
10664
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010665 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010666 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010667 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010668 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010669
10670 msg_col = save_msg_col;
10671 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672}
10673
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010674 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010675recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010676{
10677 MSG_PUTS_ATTR(_("recording"), attr);
10678 if (!shortmess(SHM_RECORDING))
10679 {
10680 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010681 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010682 MSG_PUTS_ATTR(s, attr);
10683 }
10684}
10685
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010686/*
10687 * Draw the tab pages line at the top of the Vim window.
10688 */
10689 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010690draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010691{
10692 int tabcount = 0;
10693 tabpage_T *tp;
10694 int tabwidth;
10695 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010696 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010697 int attr;
10698 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010699 win_T *cwp;
10700 int wincount;
10701 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010702 int c;
10703 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010704 int attr_sel = HL_ATTR(HLF_TPS);
10705 int attr_nosel = HL_ATTR(HLF_TP);
10706 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010707 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010708 int room;
10709 int use_sep_chars = (t_colors < 8
10710#ifdef FEAT_GUI
10711 && !gui.in_use
10712#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010713#ifdef FEAT_TERMGUICOLORS
10714 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010715#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010716 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010717
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010718 if (ScreenLines == NULL)
10719 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010720 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010721
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010722#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010723 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010724 if (gui_use_tabline())
10725 {
10726 gui_update_tabline();
10727 return;
10728 }
10729#endif
10730
10731 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010732 return;
10733
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010734#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010735
10736 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10737 for (scol = 0; scol < Columns; ++scol)
10738 TabPageIdxs[scol] = 0;
10739
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010740 /* Use the 'tabline' option if it's set. */
10741 if (*p_tal != NUL)
10742 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010743 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010744
10745 /* Check for an error. If there is one we would loop in redrawing the
10746 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010747 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010748 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010749 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010750 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010751 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010752 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010753 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010754 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010755#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010756 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010757 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010758 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010759
Bram Moolenaar238a5642006-02-21 22:12:05 +000010760 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10761 if (tabwidth < 6)
10762 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010763
Bram Moolenaar238a5642006-02-21 22:12:05 +000010764 attr = attr_nosel;
10765 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010766 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010767 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10768 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010769 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010770 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010771
Bram Moolenaar238a5642006-02-21 22:12:05 +000010772 if (tp->tp_topframe == topframe)
10773 attr = attr_sel;
10774 if (use_sep_chars && col > 0)
10775 screen_putchar('|', 0, col++, attr);
10776
10777 if (tp->tp_topframe != topframe)
10778 attr = attr_nosel;
10779
10780 screen_putchar(' ', 0, col++, attr);
10781
10782 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010783 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010784 cwp = curwin;
10785 wp = firstwin;
10786 }
10787 else
10788 {
10789 cwp = tp->tp_curwin;
10790 wp = tp->tp_firstwin;
10791 }
10792
10793 modified = FALSE;
10794 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10795 if (bufIsChanged(wp->w_buffer))
10796 modified = TRUE;
10797 if (modified || wincount > 1)
10798 {
10799 if (wincount > 1)
10800 {
10801 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010802 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010803 if (col + len >= Columns - 3)
10804 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010805 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010806#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010807 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010808#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010809 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010810#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010811 );
10812 col += len;
10813 }
10814 if (modified)
10815 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10816 screen_putchar(' ', 0, col++, attr);
10817 }
10818
10819 room = scol - col + tabwidth - 1;
10820 if (room > 0)
10821 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010822 /* Get buffer name in NameBuff[] */
10823 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010824 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010825 len = vim_strsize(NameBuff);
10826 p = NameBuff;
10827#ifdef FEAT_MBYTE
10828 if (has_mbyte)
10829 while (len > room)
10830 {
10831 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010832 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010833 }
10834 else
10835#endif
10836 if (len > room)
10837 {
10838 p += len - room;
10839 len = room;
10840 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010841 if (len > Columns - col - 1)
10842 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010843
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010844 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010845 col += len;
10846 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010847 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010848
10849 /* Store the tab page number in TabPageIdxs[], so that
10850 * jump_to_mouse() knows where each one is. */
10851 ++tabcount;
10852 while (scol < col)
10853 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010854 }
10855
Bram Moolenaar238a5642006-02-21 22:12:05 +000010856 if (use_sep_chars)
10857 c = '_';
10858 else
10859 c = ' ';
10860 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010861
10862 /* Put an "X" for closing the current tab if there are several. */
10863 if (first_tabpage->tp_next != NULL)
10864 {
10865 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10866 TabPageIdxs[Columns - 1] = -999;
10867 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010868 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010869
10870 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10871 * set. */
10872 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010873}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010874
10875/*
10876 * Get buffer name for "buf" into NameBuff[].
10877 * Takes care of special buffer names and translates special characters.
10878 */
10879 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010880get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010881{
10882 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010883 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010884 else
10885 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10886 trans_characters(NameBuff, MAXPATHL);
10887}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010888
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889/*
10890 * Get the character to use in a status line. Get its attributes in "*attr".
10891 */
10892 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010893fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894{
10895 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010896
10897#ifdef FEAT_TERMINAL
10898 if (bt_terminal(wp->w_buffer))
10899 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010900 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010901 {
10902 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010903 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010904 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010905 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010906 {
10907 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010908 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010909 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010910 }
10911 else
10912#endif
10913 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010915 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 fill = fill_stl;
10917 }
10918 else
10919 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010920 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 fill = fill_stlnc;
10922 }
10923 /* Use fill when there is highlighting, and highlighting of current
10924 * window differs, or the fillchars differ, or this is not the
10925 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010926 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010927 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 || (fill_stl != fill_stlnc)))
10929 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010930 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 return '^';
10932 return '=';
10933}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935/*
10936 * Get the character to use in a separator between vertically split windows.
10937 * Get its attributes in "*attr".
10938 */
10939 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010940fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010942 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 if (*attr == 0 && fill_vert == ' ')
10944 return '|';
10945 else
10946 return fill_vert;
10947}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948
10949/*
10950 * Return TRUE if redrawing should currently be done.
10951 */
10952 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010953redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010955#ifdef FEAT_EVAL
10956 if (disable_redraw_for_testing)
10957 return 0;
10958 else
10959#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010960 return ((!RedrawingDisabled
10961#ifdef FEAT_EVAL
10962 || ignore_redraw_flag_for_testing
10963#endif
10964 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965}
10966
10967/*
10968 * Return TRUE if printing messages should currently be done.
10969 */
10970 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010971messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972{
10973 return (!(p_lz && char_avail() && !KeyTyped));
10974}
10975
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010976#ifdef FEAT_MENU
10977/*
10978 * Draw the window toolbar.
10979 */
10980 static void
10981redraw_win_toolbar(win_T *wp)
10982{
10983 vimmenu_T *menu;
10984 int item_idx = 0;
10985 int item_count = 0;
10986 int col = 0;
10987 int next_col;
10988 int off = (int)(current_ScreenLine - ScreenLines);
10989 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10990 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10991
10992 vim_free(wp->w_winbar_items);
10993 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10994 ++item_count;
10995 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10996 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10997
10998 /* TODO: use fewer spaces if there is not enough room */
10999 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011000 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011001 {
11002 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011003 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011004 break;
11005 if (col > 1)
11006 {
11007 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011008 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011009 break;
11010 }
11011
11012 wp->w_winbar_items[item_idx].wb_startcol = col;
11013 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011014 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011015 break;
11016
11017 next_col = text_to_screenline(wp, menu->name, col);
11018 while (col < next_col)
11019 {
11020 ScreenAttrs[off + col] = button_attr;
11021 ++col;
11022 }
11023 wp->w_winbar_items[item_idx].wb_endcol = col;
11024 wp->w_winbar_items[item_idx].wb_menu = menu;
11025 ++item_idx;
11026
Bram Moolenaar02631462017-09-22 15:20:32 +020011027 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011028 break;
11029 space_to_screenline(off + col, button_attr);
11030 ++col;
11031 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011032 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011033 {
11034 space_to_screenline(off + col, fill_attr);
11035 ++col;
11036 }
11037 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11038
Bram Moolenaar02631462017-09-22 15:20:32 +020011039 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11040 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011041}
11042#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011043
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044/*
11045 * Show current status info in ruler and various other places
11046 * If always is FALSE, only show ruler if position has changed.
11047 */
11048 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011049showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050{
11051 if (!always && !redrawing())
11052 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011053#ifdef FEAT_INS_EXPAND
11054 if (pum_visible())
11055 {
11056 /* Don't redraw right now, do it later. */
11057 curwin->w_redr_status = TRUE;
11058 return;
11059 }
11060#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011061#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011062 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011063 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011064 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 else
11067#endif
11068#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011069 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070#endif
11071
11072#ifdef FEAT_TITLE
11073 if (need_maketitle
11074# ifdef FEAT_STL_OPT
11075 || (p_icon && (stl_syntax & STL_IN_ICON))
11076 || (p_title && (stl_syntax & STL_IN_TITLE))
11077# endif
11078 )
11079 maketitle();
11080#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011081 /* Redraw the tab pages line if needed. */
11082 if (redraw_tabline)
11083 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084}
11085
11086#ifdef FEAT_CMDL_INFO
11087 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011088win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011090#define RULER_BUF_LEN 70
11091 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 int row;
11093 int fillchar;
11094 int attr;
11095 int empty_line = FALSE;
11096 colnr_T virtcol;
11097 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011098 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 int this_ru_col;
11101 int off = 0;
11102 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
11104 /* If 'ruler' off or redrawing disabled, don't do anything */
11105 if (!p_ru)
11106 return;
11107
11108 /*
11109 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11110 * after deleting lines, before cursor.lnum is corrected.
11111 */
11112 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11113 return;
11114
11115#ifdef FEAT_INS_EXPAND
11116 /* Don't draw the ruler while doing insert-completion, it might overwrite
11117 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 if (edit_submode != NULL)
11120 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011121 // Don't draw the ruler when the popup menu is visible, it may overlap.
11122 // Except when the popup menu will be redrawn anyway.
11123 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011124 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125#endif
11126
11127#ifdef FEAT_STL_OPT
11128 if (*p_ruf)
11129 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011130 int save_called_emsg = called_emsg;
11131
11132 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011134 if (called_emsg)
11135 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011136 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011137 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 return;
11139 }
11140#endif
11141
11142 /*
11143 * Check if not in Insert mode and the line is empty (will show "0-1").
11144 */
11145 if (!(State & INSERT)
11146 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11147 empty_line = TRUE;
11148
11149 /*
11150 * Only draw the ruler when something changed.
11151 */
11152 validate_virtcol_win(wp);
11153 if ( redraw_cmdline
11154 || always
11155 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11156 || wp->w_cursor.col != wp->w_ru_cursor.col
11157 || wp->w_virtcol != wp->w_ru_virtcol
11158#ifdef FEAT_VIRTUALEDIT
11159 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11160#endif
11161 || wp->w_topline != wp->w_ru_topline
11162 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11163#ifdef FEAT_DIFF
11164 || wp->w_topfill != wp->w_ru_topfill
11165#endif
11166 || empty_line != wp->w_ru_empty)
11167 {
11168 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 if (wp->w_status_height)
11170 {
11171 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011172 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011173 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011174 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 }
11176 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 {
11178 row = Rows - 1;
11179 fillchar = ' ';
11180 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 width = Columns;
11182 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 }
11184
11185 /* In list mode virtcol needs to be recomputed */
11186 virtcol = wp->w_virtcol;
11187 if (wp->w_p_list && lcs_tab1 == NUL)
11188 {
11189 wp->w_p_list = FALSE;
11190 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11191 wp->w_p_list = TRUE;
11192 }
11193
11194 /*
11195 * Some sprintfs return the length, some return a pointer.
11196 * To avoid portability problems we use strlen() here.
11197 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011198 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11200 ? 0L
11201 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011202 len = STRLEN(buffer);
11203 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11205 (int)virtcol + 1);
11206
11207 /*
11208 * Add a "50%" if there is room for it.
11209 * On the last line, don't print in the last column (scrolls the
11210 * screen up on some terminals).
11211 */
11212 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011213 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 this_ru_col = ru_col - (Columns - width);
11218 if (this_ru_col < 0)
11219 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 /* Never use more than half the window/screen width, leave the other
11221 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011222 if (this_ru_col < (width + 1) / 2)
11223 this_ru_col = (width + 1) / 2;
11224 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011226 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011227 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 {
11229#ifdef FEAT_MBYTE
11230 if (has_mbyte)
11231 i += (*mb_char2bytes)(fillchar, buffer + i);
11232 else
11233#endif
11234 buffer[i++] = fillchar;
11235 ++o;
11236 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011237 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 }
11239 /* Truncate at window boundary. */
11240#ifdef FEAT_MBYTE
11241 if (has_mbyte)
11242 {
11243 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011244 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 {
11246 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011247 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 {
11249 buffer[i] = NUL;
11250 break;
11251 }
11252 }
11253 }
11254 else
11255#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011256 if (this_ru_col + (int)STRLEN(buffer) > width)
11257 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258
Bram Moolenaar4033c552017-09-16 20:54:51 +020011259 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 i = redraw_cmdline;
11261 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011262 this_ru_col + off + (int)STRLEN(buffer),
11263 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 fillchar, fillchar, attr);
11265 /* don't redraw the cmdline because of showing the ruler */
11266 redraw_cmdline = i;
11267 wp->w_ru_cursor = wp->w_cursor;
11268 wp->w_ru_virtcol = wp->w_virtcol;
11269 wp->w_ru_empty = empty_line;
11270 wp->w_ru_topline = wp->w_topline;
11271 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11272#ifdef FEAT_DIFF
11273 wp->w_ru_topfill = wp->w_topfill;
11274#endif
11275 }
11276}
11277#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011278
11279#if defined(FEAT_LINEBREAK) || defined(PROTO)
11280/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011281 * Return the width of the 'number' and 'relativenumber' column.
11282 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011283 * Otherwise it depends on 'numberwidth' and the line count.
11284 */
11285 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011286number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011287{
11288 int n;
11289 linenr_T lnum;
11290
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011291 if (wp->w_p_rnu && !wp->w_p_nu)
11292 /* cursor line shows "0" */
11293 lnum = wp->w_height;
11294 else
11295 /* cursor line shows absolute line number */
11296 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011297
Bram Moolenaar6b314672015-03-20 15:42:10 +010011298 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011299 return wp->w_nrwidth_width;
11300 wp->w_nrwidth_line_count = lnum;
11301
11302 n = 0;
11303 do
11304 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011305 lnum /= 10;
11306 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011307 } while (lnum > 0);
11308
11309 /* 'numberwidth' gives the minimal width plus one */
11310 if (n < wp->w_p_nuw - 1)
11311 n = wp->w_p_nuw - 1;
11312
11313 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011314 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011315 return n;
11316}
11317#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011318
11319/*
11320 * Return the current cursor column. This is the actual position on the
11321 * screen. First column is 0.
11322 */
11323 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011324screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011325{
11326 return screen_cur_col;
11327}
11328
11329/*
11330 * Return the current cursor row. This is the actual position on the screen.
11331 * First row is 0.
11332 */
11333 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011334screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011335{
11336 return screen_cur_row;
11337}