blob: 4166928fe084508b62bea364eeb19c80ada6dbb3 [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 start_search_hl();
965 prepare_search_hl(wp, lnum);
966# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200967 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
968 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200969# if defined(FEAT_SEARCH_EXTRA)
970 end_search_hl();
971# endif
972 break;
973 }
974 row += wp->w_lines[j].wl_size;
975 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100976
977 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200978
979#ifdef SYN_TIME_LIMIT
980 syn_set_timeout(NULL);
981#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200982 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200983 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984}
985#endif
986
987#if defined(FEAT_SIGNS) || defined(PROTO)
988 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100989update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 win_T *wp;
992 int doit = FALSE;
993
994# ifdef FEAT_FOLDING
995 win_foldinfo.fi_level = 0;
996# endif
997
998 /* update/delete a specific mark */
999 FOR_ALL_WINDOWS(wp)
1000 {
1001 if (buf != NULL && lnum > 0)
1002 {
1003 if (wp->w_buffer == buf && lnum >= wp->w_topline
1004 && lnum < wp->w_botline)
1005 {
1006 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1007 wp->w_redraw_top = lnum;
1008 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1009 wp->w_redraw_bot = lnum;
1010 redraw_win_later(wp, VALID);
1011 }
1012 }
1013 else
1014 redraw_win_later(wp, VALID);
1015 if (wp->w_redr_type != 0)
1016 doit = TRUE;
1017 }
1018
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001019 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001020 * happening (recursive call), messages on the screen or still starting up.
1021 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001022 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001023 || State == ASKMORE || State == HITRETURN
1024 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001025#ifdef FEAT_GUI
1026 || gui.starting
1027#endif
1028 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 return;
1030
1031 /* update all windows that need updating */
1032 update_prepare();
1033
Bram Moolenaar29323592016-07-24 22:04:11 +02001034 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 if (wp->w_redr_type != 0)
1037 win_update(wp);
1038 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001039 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 update_finish();
1043}
1044#endif
1045
1046
1047#if defined(FEAT_GUI) || defined(PROTO)
1048/*
1049 * Update a single window, its status line and maybe the command line msg.
1050 * Used for the GUI scrollbar.
1051 */
1052 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001053updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001055 /* return if already busy updating */
1056 if (updating_screen)
1057 return;
1058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 update_prepare();
1060
1061#ifdef FEAT_CLIPBOARD
1062 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001063 if (clip_star.available && clip_isautosel_star())
1064 clip_update_selection(&clip_star);
1065 if (clip_plus.available && clip_isautosel_plus())
1066 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001070
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001071 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001072 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001073 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (wp->w_redr_status
1076# ifdef FEAT_CMDL_INFO
1077 || p_ru
1078# endif
1079# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001080 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081# endif
1082 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001083 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
1085 update_finish();
1086}
1087#endif
1088
1089/*
1090 * Update a single window.
1091 *
1092 * This may cause the windows below it also to be redrawn (when clearing the
1093 * screen or scrolling lines).
1094 *
1095 * How the window is redrawn depends on wp->w_redr_type. Each type also
1096 * implies the one below it.
1097 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001098 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1100 * INVERTED redraw the changed part of the Visual area
1101 * INVERTED_ALL redraw the whole Visual area
1102 * VALID 1. scroll up/down to adjust for a changed w_topline
1103 * 2. update lines at the top when scrolled down
1104 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001105 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * b_mod_top and b_mod_bot.
1107 * - if wp->w_redraw_top non-zero, redraw lines between
1108 * wp->w_redraw_top and wp->w_redr_bot.
1109 * - continue redrawing when syntax status is invalid.
1110 * 4. if scrolled up, update lines at the bottom.
1111 * This results in three areas that may need updating:
1112 * top: from first row to top_end (when scrolled down)
1113 * mid: from mid_start to mid_end (update inversion or changed text)
1114 * bot: from bot_start to last row (when scrolled up)
1115 */
1116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001117win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 buf_T *buf = wp->w_buffer;
1120 int type;
1121 int top_end = 0; /* Below last row of the top area that needs
1122 updating. 0 when no top area updating. */
1123 int mid_start = 999;/* first row of the mid area that needs
1124 updating. 999 when no mid area updating. */
1125 int mid_end = 0; /* Below last row of the mid area that needs
1126 updating. 0 when no mid area updating. */
1127 int bot_start = 999;/* first row of the bot area that needs
1128 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int scrolled_down = FALSE; /* TRUE when scrolled down when
1130 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001132 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 int top_to_mod = FALSE; /* redraw above mod_top */
1134#endif
1135
1136 int row; /* current window row to display */
1137 linenr_T lnum; /* current buffer lnum to display */
1138 int idx; /* current index in w_lines[] */
1139 int srow; /* starting row of the current line */
1140
1141 int eof = FALSE; /* if TRUE, we hit the end of the file */
1142 int didline = FALSE; /* if TRUE, we finished the last line */
1143 int i;
1144 long j;
1145 static int recursive = FALSE; /* being called recursively */
1146 int old_botline = wp->w_botline;
1147#ifdef FEAT_FOLDING
1148 long fold_count;
1149#endif
1150#ifdef FEAT_SYN_HL
1151 /* remember what happened to the previous line, to know if
1152 * check_visual_highlight() can be used */
1153#define DID_NONE 1 /* didn't update a line */
1154#define DID_LINE 2 /* updated a normal line */
1155#define DID_FOLD 3 /* updated a folded line */
1156 int did_update = DID_NONE;
1157 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1158#endif
1159 linenr_T mod_top = 0;
1160 linenr_T mod_bot = 0;
1161#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1162 int save_got_int;
1163#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001164#ifdef SYN_TIME_LIMIT
1165 proftime_T syntax_tm;
1166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 type = wp->w_redr_type;
1169
1170 if (type == NOT_VALID)
1171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 wp->w_lines_valid = 0;
1174 }
1175
1176 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001177 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 wp->w_redr_type = 0;
1180 return;
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 /* Window is zero-width: Only need to draw the separator. */
1184 if (wp->w_width == 0)
1185 {
1186 /* draw the vertical separator right of this window */
1187 draw_vsep_win(wp, 0);
1188 wp->w_redr_type = 0;
1189 return;
1190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001192#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001193 // If this window contains a terminal, redraw works completely differently.
1194 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001195 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001196 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001197# ifdef FEAT_MENU
1198 /* Draw the window toolbar, if there is one. */
1199 if (winbar_height(wp) > 0)
1200 redraw_win_toolbar(wp);
1201# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001202 wp->w_redr_type = 0;
1203 return;
1204 }
1205#endif
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001208 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#endif
1210
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001211#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001212 /* Force redraw when width of 'number' or 'relativenumber' column
1213 * changes. */
1214 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001215 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001216 {
1217 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001218 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001219 }
1220 else
1221#endif
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1224 {
1225 /*
1226 * When there are both inserted/deleted lines and specific lines to be
1227 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1228 * everything (only happens when redrawing is off for while).
1229 */
1230 type = NOT_VALID;
1231 }
1232 else
1233 {
1234 /*
1235 * Set mod_top to the first line that needs displaying because of
1236 * changes. Set mod_bot to the first line after the changes.
1237 */
1238 mod_top = wp->w_redraw_top;
1239 if (wp->w_redraw_bot != 0)
1240 mod_bot = wp->w_redraw_bot + 1;
1241 else
1242 mod_bot = 0;
1243 wp->w_redraw_top = 0; /* reset for next time */
1244 wp->w_redraw_bot = 0;
1245 if (buf->b_mod_set)
1246 {
1247 if (mod_top == 0 || mod_top > buf->b_mod_top)
1248 {
1249 mod_top = buf->b_mod_top;
1250#ifdef FEAT_SYN_HL
1251 /* Need to redraw lines above the change that may be included
1252 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001253 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (mod_top < 1)
1257 mod_top = 1;
1258 }
1259#endif
1260 }
1261 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1262 mod_bot = buf->b_mod_bot;
1263
1264#ifdef FEAT_SEARCH_EXTRA
1265 /* When 'hlsearch' is on and using a multi-line search pattern, a
1266 * change in one line may make the Search highlighting in a
1267 * previous line invalid. Simple solution: redraw all visible
1268 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001269 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001271 if (search_hl.rm.regprog != NULL
1272 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001274 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001275 {
1276 cur = wp->w_match_head;
1277 while (cur != NULL)
1278 {
1279 if (cur->match.regprog != NULL
1280 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001281 {
1282 top_to_mod = TRUE;
1283 break;
1284 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001285 cur = cur->next;
1286 }
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
1289 }
1290#ifdef FEAT_FOLDING
1291 if (mod_top != 0 && hasAnyFolding(wp))
1292 {
1293 linenr_T lnumt, lnumb;
1294
1295 /*
1296 * A change in a line can cause lines above it to become folded or
1297 * unfolded. Find the top most buffer line that may be affected.
1298 * If the line was previously folded and displayed, get the first
1299 * line of that fold. If the line is folded now, get the first
1300 * folded line. Use the minimum of these two.
1301 */
1302
1303 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1304 * the line below it. If there is no valid entry, use w_topline.
1305 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1306 * to this line. If there is no valid entry, use MAXLNUM. */
1307 lnumt = wp->w_topline;
1308 lnumb = MAXLNUM;
1309 for (i = 0; i < wp->w_lines_valid; ++i)
1310 if (wp->w_lines[i].wl_valid)
1311 {
1312 if (wp->w_lines[i].wl_lastlnum < mod_top)
1313 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1314 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1315 {
1316 lnumb = wp->w_lines[i].wl_lnum;
1317 /* When there is a fold column it might need updating
1318 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001319 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 ++lnumb;
1321 }
1322 }
1323
1324 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1325 if (mod_top > lnumt)
1326 mod_top = lnumt;
1327
1328 /* Now do the same for the bottom line (one above mod_bot). */
1329 --mod_bot;
1330 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1331 ++mod_bot;
1332 if (mod_bot < lnumb)
1333 mod_bot = lnumb;
1334 }
1335#endif
1336
1337 /* When a change starts above w_topline and the end is below
1338 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001339 * If the end of the change is above w_topline: do like no change was
1340 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (mod_top != 0 && mod_top < wp->w_topline)
1342 {
1343 if (mod_bot > wp->w_topline)
1344 mod_top = wp->w_topline;
1345#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001346 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 top_end = 1;
1348#endif
1349 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350
1351 /* When line numbers are displayed need to redraw all lines below
1352 * inserted/deleted lines. */
1353 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1354 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356
1357 /*
1358 * When only displaying the lines at the top, set top_end. Used when
1359 * window has scrolled down for msg_scrolled.
1360 */
1361 if (type == REDRAW_TOP)
1362 {
1363 j = 0;
1364 for (i = 0; i < wp->w_lines_valid; ++i)
1365 {
1366 j += wp->w_lines[i].wl_size;
1367 if (j >= wp->w_upd_rows)
1368 {
1369 top_end = j;
1370 break;
1371 }
1372 }
1373 if (top_end == 0)
1374 /* not found (cannot happen?): redraw everything */
1375 type = NOT_VALID;
1376 else
1377 /* top area defined, the rest is VALID */
1378 type = VALID;
1379 }
1380
Bram Moolenaar367329b2007-08-30 11:53:22 +00001381 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001382 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1383 * non-zero and thus not FALSE) will indicate that screenclear() was not
1384 * called. */
1385 if (screen_cleared)
1386 screen_cleared = MAYBE;
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 /*
1389 * If there are no changes on the screen that require a complete redraw,
1390 * handle three cases:
1391 * 1: we are off the top of the screen by a few lines: scroll down
1392 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1393 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1394 * w_lines[] that needs updating.
1395 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001396 if ((type == VALID || type == SOME_VALID
1397 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_DIFF
1399 && !wp->w_botfill && !wp->w_old_botfill
1400#endif
1401 )
1402 {
1403 if (mod_top != 0 && wp->w_topline == mod_top)
1404 {
1405 /*
1406 * w_topline is the first changed line, the scrolling will be done
1407 * further down.
1408 */
1409 }
1410 else if (wp->w_lines[0].wl_valid
1411 && (wp->w_topline < wp->w_lines[0].wl_lnum
1412#ifdef FEAT_DIFF
1413 || (wp->w_topline == wp->w_lines[0].wl_lnum
1414 && wp->w_topfill > wp->w_old_topfill)
1415#endif
1416 ))
1417 {
1418 /*
1419 * New topline is above old topline: May scroll down.
1420 */
1421#ifdef FEAT_FOLDING
1422 if (hasAnyFolding(wp))
1423 {
1424 linenr_T ln;
1425
1426 /* count the number of lines we are off, counting a sequence
1427 * of folded lines as one */
1428 j = 0;
1429 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1430 {
1431 ++j;
1432 if (j >= wp->w_height - 2)
1433 break;
1434 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1435 }
1436 }
1437 else
1438#endif
1439 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1440 if (j < wp->w_height - 2) /* not too far off */
1441 {
1442 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1443#ifdef FEAT_DIFF
1444 /* insert extra lines for previously invisible filler lines */
1445 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1446 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1447 - wp->w_old_topfill;
1448#endif
1449 if (i < wp->w_height - 2) /* less than a screen off */
1450 {
1451 /*
1452 * Try to insert the correct number of lines.
1453 * If not the last window, delete the lines at the bottom.
1454 * win_ins_lines may fail when the terminal can't do it.
1455 */
1456 if (i > 0)
1457 check_for_delay(FALSE);
1458 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1459 {
1460 if (wp->w_lines_valid != 0)
1461 {
1462 /* Need to update rows that are new, stop at the
1463 * first one that scrolled down. */
1464 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 /* Move the entries that were scrolled, disable
1468 * the entries for the lines to be redrawn. */
1469 if ((wp->w_lines_valid += j) > wp->w_height)
1470 wp->w_lines_valid = wp->w_height;
1471 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1472 wp->w_lines[idx] = wp->w_lines[idx - j];
1473 while (idx >= 0)
1474 wp->w_lines[idx--].wl_valid = FALSE;
1475 }
1476 }
1477 else
1478 mid_start = 0; /* redraw all lines */
1479 }
1480 else
1481 mid_start = 0; /* redraw all lines */
1482 }
1483 else
1484 mid_start = 0; /* redraw all lines */
1485 }
1486 else
1487 {
1488 /*
1489 * New topline is at or below old topline: May scroll up.
1490 * When topline didn't change, find first entry in w_lines[] that
1491 * needs updating.
1492 */
1493
1494 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1495 j = -1;
1496 row = 0;
1497 for (i = 0; i < wp->w_lines_valid; i++)
1498 {
1499 if (wp->w_lines[i].wl_valid
1500 && wp->w_lines[i].wl_lnum == wp->w_topline)
1501 {
1502 j = i;
1503 break;
1504 }
1505 row += wp->w_lines[i].wl_size;
1506 }
1507 if (j == -1)
1508 {
1509 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1510 * lines */
1511 mid_start = 0;
1512 }
1513 else
1514 {
1515 /*
1516 * Try to delete the correct number of lines.
1517 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1518 */
1519#ifdef FEAT_DIFF
1520 /* If the topline didn't change, delete old filler lines,
1521 * otherwise delete filler lines of the new topline... */
1522 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1523 row += wp->w_old_topfill;
1524 else
1525 row += diff_check_fill(wp, wp->w_topline);
1526 /* ... but don't delete new filler lines. */
1527 row -= wp->w_topfill;
1528#endif
1529 if (row > 0)
1530 {
1531 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001532 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1533 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 bot_start = wp->w_height - row;
1535 else
1536 mid_start = 0; /* redraw all lines */
1537 }
1538 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1539 {
1540 /*
1541 * Skip the lines (below the deleted lines) that are still
1542 * valid and don't need redrawing. Copy their info
1543 * upwards, to compensate for the deleted lines. Set
1544 * bot_start to the first row that needs redrawing.
1545 */
1546 bot_start = 0;
1547 idx = 0;
1548 for (;;)
1549 {
1550 wp->w_lines[idx] = wp->w_lines[j];
1551 /* stop at line that didn't fit, unless it is still
1552 * valid (no lines deleted) */
1553 if (row > 0 && bot_start + row
1554 + (int)wp->w_lines[j].wl_size > wp->w_height)
1555 {
1556 wp->w_lines_valid = idx + 1;
1557 break;
1558 }
1559 bot_start += wp->w_lines[idx++].wl_size;
1560
1561 /* stop at the last valid entry in w_lines[].wl_size */
1562 if (++j >= wp->w_lines_valid)
1563 {
1564 wp->w_lines_valid = idx;
1565 break;
1566 }
1567 }
1568#ifdef FEAT_DIFF
1569 /* Correct the first entry for filler lines at the top
1570 * when it won't get updated below. */
1571 if (wp->w_p_diff && bot_start > 0)
1572 wp->w_lines[0].wl_size =
1573 plines_win_nofill(wp, wp->w_topline, TRUE)
1574 + wp->w_topfill;
1575#endif
1576 }
1577 }
1578 }
1579
1580 /* When starting redraw in the first line, redraw all lines. When
1581 * there is only one window it's probably faster to clear the screen
1582 * first. */
1583 if (mid_start == 0)
1584 {
1585 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001586 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001587 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001588 /* Clear the screen when it was not done by win_del_lines() or
1589 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1590 * then. */
1591 if (screen_cleared != TRUE)
1592 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001593 /* The screen was cleared, redraw the tab pages line. */
1594 if (redraw_tabline)
1595 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001598
1599 /* When win_del_lines() or win_ins_lines() caused the screen to be
1600 * cleared (only happens for the first window) or when screenclear()
1601 * was called directly above, "must_redraw" will have been set to
1602 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1603 if (screen_cleared == TRUE)
1604 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 else
1607 {
1608 /* Not VALID or INVERTED: redraw all lines. */
1609 mid_start = 0;
1610 mid_end = wp->w_height;
1611 }
1612
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001613 if (type == SOME_VALID)
1614 {
1615 /* SOME_VALID: redraw all lines. */
1616 mid_start = 0;
1617 mid_end = wp->w_height;
1618 type = NOT_VALID;
1619 }
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /* check if we are updating or removing the inverted part */
1622 if ((VIsual_active && buf == curwin->w_buffer)
1623 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1624 {
1625 linenr_T from, to;
1626
1627 if (VIsual_active)
1628 {
1629 if (VIsual_active
1630 && (VIsual_mode != wp->w_old_visual_mode
1631 || type == INVERTED_ALL))
1632 {
1633 /*
1634 * If the type of Visual selection changed, redraw the whole
1635 * selection. Also when the ownership of the X selection is
1636 * gained or lost.
1637 */
1638 if (curwin->w_cursor.lnum < VIsual.lnum)
1639 {
1640 from = curwin->w_cursor.lnum;
1641 to = VIsual.lnum;
1642 }
1643 else
1644 {
1645 from = VIsual.lnum;
1646 to = curwin->w_cursor.lnum;
1647 }
1648 /* redraw more when the cursor moved as well */
1649 if (wp->w_old_cursor_lnum < from)
1650 from = wp->w_old_cursor_lnum;
1651 if (wp->w_old_cursor_lnum > to)
1652 to = wp->w_old_cursor_lnum;
1653 if (wp->w_old_visual_lnum < from)
1654 from = wp->w_old_visual_lnum;
1655 if (wp->w_old_visual_lnum > to)
1656 to = wp->w_old_visual_lnum;
1657 }
1658 else
1659 {
1660 /*
1661 * Find the line numbers that need to be updated: The lines
1662 * between the old cursor position and the current cursor
1663 * position. Also check if the Visual position changed.
1664 */
1665 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1666 {
1667 from = curwin->w_cursor.lnum;
1668 to = wp->w_old_cursor_lnum;
1669 }
1670 else
1671 {
1672 from = wp->w_old_cursor_lnum;
1673 to = curwin->w_cursor.lnum;
1674 if (from == 0) /* Visual mode just started */
1675 from = to;
1676 }
1677
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001678 if (VIsual.lnum != wp->w_old_visual_lnum
1679 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 if (wp->w_old_visual_lnum < from
1682 && wp->w_old_visual_lnum != 0)
1683 from = wp->w_old_visual_lnum;
1684 if (wp->w_old_visual_lnum > to)
1685 to = wp->w_old_visual_lnum;
1686 if (VIsual.lnum < from)
1687 from = VIsual.lnum;
1688 if (VIsual.lnum > to)
1689 to = VIsual.lnum;
1690 }
1691 }
1692
1693 /*
1694 * If in block mode and changed column or curwin->w_curswant:
1695 * update all lines.
1696 * First compute the actual start and end column.
1697 */
1698 if (VIsual_mode == Ctrl_V)
1699 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001700 colnr_T fromc, toc;
1701#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1702 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar404406a2014-10-09 13:24:43 +02001704 if (curwin->w_p_lbr)
1705 ve_flags = VE_ALL;
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001708#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1709 ve_flags = save_ve_flags;
1710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 ++toc;
1712 if (curwin->w_curswant == MAXCOL)
1713 toc = MAXCOL;
1714
1715 if (fromc != wp->w_old_cursor_fcol
1716 || toc != wp->w_old_cursor_lcol)
1717 {
1718 if (from > VIsual.lnum)
1719 from = VIsual.lnum;
1720 if (to < VIsual.lnum)
1721 to = VIsual.lnum;
1722 }
1723 wp->w_old_cursor_fcol = fromc;
1724 wp->w_old_cursor_lcol = toc;
1725 }
1726 }
1727 else
1728 {
1729 /* Use the line numbers of the old Visual area. */
1730 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1731 {
1732 from = wp->w_old_cursor_lnum;
1733 to = wp->w_old_visual_lnum;
1734 }
1735 else
1736 {
1737 from = wp->w_old_visual_lnum;
1738 to = wp->w_old_cursor_lnum;
1739 }
1740 }
1741
1742 /*
1743 * There is no need to update lines above the top of the window.
1744 */
1745 if (from < wp->w_topline)
1746 from = wp->w_topline;
1747
1748 /*
1749 * If we know the value of w_botline, use it to restrict the update to
1750 * the lines that are visible in the window.
1751 */
1752 if (wp->w_valid & VALID_BOTLINE)
1753 {
1754 if (from >= wp->w_botline)
1755 from = wp->w_botline - 1;
1756 if (to >= wp->w_botline)
1757 to = wp->w_botline - 1;
1758 }
1759
1760 /*
1761 * Find the minimal part to be updated.
1762 * Watch out for scrolling that made entries in w_lines[] invalid.
1763 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1764 * top_end; need to redraw from top_end to the "to" line.
1765 * A middle mouse click with a Visual selection may change the text
1766 * above the Visual area and reset wl_valid, do count these for
1767 * mid_end (in srow).
1768 */
1769 if (mid_start > 0)
1770 {
1771 lnum = wp->w_topline;
1772 idx = 0;
1773 srow = 0;
1774 if (scrolled_down)
1775 mid_start = top_end;
1776 else
1777 mid_start = 0;
1778 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1779 {
1780 if (wp->w_lines[idx].wl_valid)
1781 mid_start += wp->w_lines[idx].wl_size;
1782 else if (!scrolled_down)
1783 srow += wp->w_lines[idx].wl_size;
1784 ++idx;
1785# ifdef FEAT_FOLDING
1786 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1787 lnum = wp->w_lines[idx].wl_lnum;
1788 else
1789# endif
1790 ++lnum;
1791 }
1792 srow += mid_start;
1793 mid_end = wp->w_height;
1794 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1795 {
1796 if (wp->w_lines[idx].wl_valid
1797 && wp->w_lines[idx].wl_lnum >= to + 1)
1798 {
1799 /* Only update until first row of this line */
1800 mid_end = srow;
1801 break;
1802 }
1803 srow += wp->w_lines[idx].wl_size;
1804 }
1805 }
1806 }
1807
1808 if (VIsual_active && buf == curwin->w_buffer)
1809 {
1810 wp->w_old_visual_mode = VIsual_mode;
1811 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1812 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001813 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 wp->w_old_curswant = curwin->w_curswant;
1815 }
1816 else
1817 {
1818 wp->w_old_visual_mode = 0;
1819 wp->w_old_cursor_lnum = 0;
1820 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001821 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1825 /* reset got_int, otherwise regexp won't work */
1826 save_got_int = got_int;
1827 got_int = 0;
1828#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001829#ifdef SYN_TIME_LIMIT
1830 /* Set the time limit to 'redrawtime'. */
1831 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001832 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835 win_foldinfo.fi_level = 0;
1836#endif
1837
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001838#ifdef FEAT_MENU
1839 /*
1840 * Draw the window toolbar, if there is one.
1841 * TODO: only when needed.
1842 */
1843 if (winbar_height(wp) > 0)
1844 redraw_win_toolbar(wp);
1845#endif
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 /*
1848 * Update all the window rows.
1849 */
1850 idx = 0; /* first entry in w_lines[].wl_size */
1851 row = 0;
1852 srow = 0;
1853 lnum = wp->w_topline; /* first line shown in window */
1854 for (;;)
1855 {
1856 /* stop updating when reached the end of the window (check for _past_
1857 * the end of the window is at the end of the loop) */
1858 if (row == wp->w_height)
1859 {
1860 didline = TRUE;
1861 break;
1862 }
1863
1864 /* stop updating when hit the end of the file */
1865 if (lnum > buf->b_ml.ml_line_count)
1866 {
1867 eof = TRUE;
1868 break;
1869 }
1870
1871 /* Remember the starting row of the line that is going to be dealt
1872 * with. It is used further down when the line doesn't fit. */
1873 srow = row;
1874
1875 /*
1876 * Update a line when it is in an area that needs updating, when it
1877 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001878 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 * win_del_lines(), check if the current line includes it.
1880 * When syntax folding is being used, the saved syntax states will
1881 * already have been updated, we can't see where the syntax state is
1882 * the same again, just update until the end of the window.
1883 */
1884 if (row < top_end
1885 || (row >= mid_start && row < mid_end)
1886#ifdef FEAT_SEARCH_EXTRA
1887 || top_to_mod
1888#endif
1889 || idx >= wp->w_lines_valid
1890 || (row + wp->w_lines[idx].wl_size > bot_start)
1891 || (mod_top != 0
1892 && (lnum == mod_top
1893 || (lnum >= mod_top
1894 && (lnum < mod_bot
1895#ifdef FEAT_SYN_HL
1896 || did_update == DID_FOLD
1897 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001898 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 && (
1900# ifdef FEAT_FOLDING
1901 (foldmethodIsSyntax(wp)
1902 && hasAnyFolding(wp)) ||
1903# endif
1904 syntax_check_changed(lnum)))
1905#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001906#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001907 /* match in fixed position might need redraw
1908 * if lines were inserted or deleted */
1909 || (wp->w_match_head != NULL
1910 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 )))))
1913 {
1914#ifdef FEAT_SEARCH_EXTRA
1915 if (lnum == mod_top)
1916 top_to_mod = FALSE;
1917#endif
1918
1919 /*
1920 * When at start of changed lines: May scroll following lines
1921 * up or down to minimize redrawing.
1922 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001923 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
1925 if (lnum == mod_top
1926 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001927 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 int old_rows = 0;
1930 int new_rows = 0;
1931 int xtra_rows;
1932 linenr_T l;
1933
1934 /* Count the old number of window rows, using w_lines[], which
1935 * should still contain the sizes for the lines as they are
1936 * currently displayed. */
1937 for (i = idx; i < wp->w_lines_valid; ++i)
1938 {
1939 /* Only valid lines have a meaningful wl_lnum. Invalid
1940 * lines are part of the changed area. */
1941 if (wp->w_lines[i].wl_valid
1942 && wp->w_lines[i].wl_lnum == mod_bot)
1943 break;
1944 old_rows += wp->w_lines[i].wl_size;
1945#ifdef FEAT_FOLDING
1946 if (wp->w_lines[i].wl_valid
1947 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1948 {
1949 /* Must have found the last valid entry above mod_bot.
1950 * Add following invalid entries. */
1951 ++i;
1952 while (i < wp->w_lines_valid
1953 && !wp->w_lines[i].wl_valid)
1954 old_rows += wp->w_lines[i++].wl_size;
1955 break;
1956 }
1957#endif
1958 }
1959
1960 if (i >= wp->w_lines_valid)
1961 {
1962 /* We can't find a valid line below the changed lines,
1963 * need to redraw until the end of the window.
1964 * Inserting/deleting lines has no use. */
1965 bot_start = 0;
1966 }
1967 else
1968 {
1969 /* Able to count old number of rows: Count new window
1970 * rows, and may insert/delete lines */
1971 j = idx;
1972 for (l = lnum; l < mod_bot; ++l)
1973 {
1974#ifdef FEAT_FOLDING
1975 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1976 ++new_rows;
1977 else
1978#endif
1979#ifdef FEAT_DIFF
1980 if (l == wp->w_topline)
1981 new_rows += plines_win_nofill(wp, l, TRUE)
1982 + wp->w_topfill;
1983 else
1984#endif
1985 new_rows += plines_win(wp, l, TRUE);
1986 ++j;
1987 if (new_rows > wp->w_height - row - 2)
1988 {
1989 /* it's getting too much, must redraw the rest */
1990 new_rows = 9999;
1991 break;
1992 }
1993 }
1994 xtra_rows = new_rows - old_rows;
1995 if (xtra_rows < 0)
1996 {
1997 /* May scroll text up. If there is not enough
1998 * remaining text or scrolling fails, must redraw the
1999 * rest. If scrolling works, must redraw the text
2000 * below the scrolled text. */
2001 if (row - xtra_rows >= wp->w_height - 2)
2002 mod_bot = MAXLNUM;
2003 else
2004 {
2005 check_for_delay(FALSE);
2006 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002007 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 mod_bot = MAXLNUM;
2009 else
2010 bot_start = wp->w_height + xtra_rows;
2011 }
2012 }
2013 else if (xtra_rows > 0)
2014 {
2015 /* May scroll text down. If there is not enough
2016 * remaining text of scrolling fails, must redraw the
2017 * rest. */
2018 if (row + xtra_rows >= wp->w_height - 2)
2019 mod_bot = MAXLNUM;
2020 else
2021 {
2022 check_for_delay(FALSE);
2023 if (win_ins_lines(wp, row + old_rows,
2024 xtra_rows, FALSE, FALSE) == FAIL)
2025 mod_bot = MAXLNUM;
2026 else if (top_end > row + old_rows)
2027 /* Scrolled the part at the top that requires
2028 * updating down. */
2029 top_end += xtra_rows;
2030 }
2031 }
2032
2033 /* When not updating the rest, may need to move w_lines[]
2034 * entries. */
2035 if (mod_bot != MAXLNUM && i != j)
2036 {
2037 if (j < i)
2038 {
2039 int x = row + new_rows;
2040
2041 /* move entries in w_lines[] upwards */
2042 for (;;)
2043 {
2044 /* stop at last valid entry in w_lines[] */
2045 if (i >= wp->w_lines_valid)
2046 {
2047 wp->w_lines_valid = j;
2048 break;
2049 }
2050 wp->w_lines[j] = wp->w_lines[i];
2051 /* stop at a line that won't fit */
2052 if (x + (int)wp->w_lines[j].wl_size
2053 > wp->w_height)
2054 {
2055 wp->w_lines_valid = j + 1;
2056 break;
2057 }
2058 x += wp->w_lines[j++].wl_size;
2059 ++i;
2060 }
2061 if (bot_start > x)
2062 bot_start = x;
2063 }
2064 else /* j > i */
2065 {
2066 /* move entries in w_lines[] downwards */
2067 j -= i;
2068 wp->w_lines_valid += j;
2069 if (wp->w_lines_valid > wp->w_height)
2070 wp->w_lines_valid = wp->w_height;
2071 for (i = wp->w_lines_valid; i - j >= idx; --i)
2072 wp->w_lines[i] = wp->w_lines[i - j];
2073
2074 /* The w_lines[] entries for inserted lines are
2075 * now invalid, but wl_size may be used above.
2076 * Reset to zero. */
2077 while (i >= idx)
2078 {
2079 wp->w_lines[i].wl_size = 0;
2080 wp->w_lines[i--].wl_valid = FALSE;
2081 }
2082 }
2083 }
2084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086
2087#ifdef FEAT_FOLDING
2088 /*
2089 * When lines are folded, display one line for all of them.
2090 * Otherwise, display normally (can be several display lines when
2091 * 'wrap' is on).
2092 */
2093 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2094 if (fold_count != 0)
2095 {
2096 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2097 ++row;
2098 --fold_count;
2099 wp->w_lines[idx].wl_folded = TRUE;
2100 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2101# ifdef FEAT_SYN_HL
2102 did_update = DID_FOLD;
2103# endif
2104 }
2105 else
2106#endif
2107 if (idx < wp->w_lines_valid
2108 && wp->w_lines[idx].wl_valid
2109 && wp->w_lines[idx].wl_lnum == lnum
2110 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002111 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 && srow + wp->w_lines[idx].wl_size > wp->w_height
2113#ifdef FEAT_DIFF
2114 && diff_check_fill(wp, lnum) == 0
2115#endif
2116 )
2117 {
2118 /* This line is not going to fit. Don't draw anything here,
2119 * will draw "@ " lines below. */
2120 row = wp->w_height + 1;
2121 }
2122 else
2123 {
2124#ifdef FEAT_SEARCH_EXTRA
2125 prepare_search_hl(wp, lnum);
2126#endif
2127#ifdef FEAT_SYN_HL
2128 /* Let the syntax stuff know we skipped a few lines. */
2129 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002130 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 syntax_end_parsing(syntax_last_parsed + 1);
2132#endif
2133
2134 /*
2135 * Display one line.
2136 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002137 row = win_line(wp, lnum, srow, wp->w_height,
2138 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140#ifdef FEAT_FOLDING
2141 wp->w_lines[idx].wl_folded = FALSE;
2142 wp->w_lines[idx].wl_lastlnum = lnum;
2143#endif
2144#ifdef FEAT_SYN_HL
2145 did_update = DID_LINE;
2146 syntax_last_parsed = lnum;
2147#endif
2148 }
2149
2150 wp->w_lines[idx].wl_lnum = lnum;
2151 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002152
2153 /* Past end of the window or end of the screen. Note that after
2154 * resizing wp->w_height may be end up too big. That's a problem
2155 * elsewhere, but prevent a crash here. */
2156 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002159 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2161 ++idx;
2162 break;
2163 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002164 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 wp->w_lines[idx].wl_size = row - srow;
2166 ++idx;
2167#ifdef FEAT_FOLDING
2168 lnum += fold_count + 1;
2169#else
2170 ++lnum;
2171#endif
2172 }
2173 else
2174 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002175 if (wp->w_p_rnu)
2176 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002177#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002178 // 'relativenumber' set: The text doesn't need to be drawn, but
2179 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002180 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2181 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002182 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002183 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002184#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002185 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002186 }
2187
2188 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 row += wp->w_lines[idx++].wl_size;
2190 if (row > wp->w_height) /* past end of screen */
2191 break;
2192#ifdef FEAT_FOLDING
2193 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2194#else
2195 ++lnum;
2196#endif
2197#ifdef FEAT_SYN_HL
2198 did_update = DID_NONE;
2199#endif
2200 }
2201
2202 if (lnum > buf->b_ml.ml_line_count)
2203 {
2204 eof = TRUE;
2205 break;
2206 }
2207 }
2208 /*
2209 * End of loop over all window lines.
2210 */
2211
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002212#ifdef FEAT_VTP
2213 /* Rewrite the character at the end of the screen line. */
2214 if (use_vtp())
2215 {
2216 int i;
2217
2218 for (i = 0; i < Rows; ++i)
2219# ifdef FEAT_MBYTE
2220 if (enc_utf8)
2221 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2222 LineOffset[i] + screen_Columns) > 1)
2223 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2224 else
2225 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2226 else
2227# endif
2228 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2229 }
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 if (idx > wp->w_lines_valid)
2233 wp->w_lines_valid = idx;
2234
2235#ifdef FEAT_SYN_HL
2236 /*
2237 * Let the syntax stuff know we stop parsing here.
2238 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002239 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 syntax_end_parsing(syntax_last_parsed + 1);
2241#endif
2242
2243 /*
2244 * If we didn't hit the end of the file, and we didn't finish the last
2245 * line we were working on, then the line didn't fit.
2246 */
2247 wp->w_empty_rows = 0;
2248#ifdef FEAT_DIFF
2249 wp->w_filler_rows = 0;
2250#endif
2251 if (!eof && !didline)
2252 {
2253 if (lnum == wp->w_topline)
2254 {
2255 /*
2256 * Single line that does not fit!
2257 * Don't overwrite it, it can be edited.
2258 */
2259 wp->w_botline = lnum + 1;
2260 }
2261#ifdef FEAT_DIFF
2262 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2263 {
2264 /* Window ends in filler lines. */
2265 wp->w_botline = lnum;
2266 wp->w_filler_rows = wp->w_height - srow;
2267 }
2268#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002269 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2270 {
2271 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2272
2273 /*
2274 * Last line isn't finished: Display "@@@" in the last screen line.
2275 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002276 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002277 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002278 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002279 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002280 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002281 set_empty_rows(wp, srow);
2282 wp->w_botline = lnum;
2283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2285 {
2286 /*
2287 * Last line isn't finished: Display "@@@" at the end.
2288 */
2289 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2290 W_WINROW(wp) + wp->w_height,
2291 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002292 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 set_empty_rows(wp, srow);
2294 wp->w_botline = lnum;
2295 }
2296 else
2297 {
2298 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2299 wp->w_botline = lnum;
2300 }
2301 }
2302 else
2303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (eof) /* we hit the end of the file */
2306 {
2307 wp->w_botline = buf->b_ml.ml_line_count + 1;
2308#ifdef FEAT_DIFF
2309 j = diff_check_fill(wp, wp->w_botline);
2310 if (j > 0 && !wp->w_botfill)
2311 {
2312 /*
2313 * Display filler lines at the end of the file
2314 */
2315 if (char2cells(fill_diff) > 1)
2316 i = '-';
2317 else
2318 i = fill_diff;
2319 if (row + j > wp->w_height)
2320 j = wp->w_height - row;
2321 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2322 row += j;
2323 }
2324#endif
2325 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002326 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 wp->w_botline = lnum;
2328
2329 /* make sure the rest of the screen is blank */
2330 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002331 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002334#ifdef SYN_TIME_LIMIT
2335 syn_set_timeout(NULL);
2336#endif
2337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 /* Reset the type of redrawing required, the window has been updated. */
2339 wp->w_redr_type = 0;
2340#ifdef FEAT_DIFF
2341 wp->w_old_topfill = wp->w_topfill;
2342 wp->w_old_botfill = wp->w_botfill;
2343#endif
2344
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002345 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
2347 /*
2348 * There is a trick with w_botline. If we invalidate it on each
2349 * change that might modify it, this will cause a lot of expensive
2350 * calls to plines() in update_topline() each time. Therefore the
2351 * value of w_botline is often approximated, and this value is used to
2352 * compute the value of w_topline. If the value of w_botline was
2353 * wrong, check that the value of w_topline is correct (cursor is on
2354 * the visible part of the text). If it's not, we need to redraw
2355 * again. Mostly this just means scrolling up a few lines, so it
2356 * doesn't look too bad. Only do this for the current window (where
2357 * changes are relevant).
2358 */
2359 wp->w_valid |= VALID_BOTLINE;
2360 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2361 {
2362 recursive = TRUE;
2363 curwin->w_valid &= ~VALID_TOPLINE;
2364 update_topline(); /* may invalidate w_botline again */
2365 if (must_redraw != 0)
2366 {
2367 /* Don't update for changes in buffer again. */
2368 i = curbuf->b_mod_set;
2369 curbuf->b_mod_set = FALSE;
2370 win_update(curwin);
2371 must_redraw = 0;
2372 curbuf->b_mod_set = i;
2373 }
2374 recursive = FALSE;
2375 }
2376 }
2377
2378#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2379 /* restore got_int, unless CTRL-C was hit while redrawing */
2380 if (!got_int)
2381 got_int = save_got_int;
2382#endif
2383}
2384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385/*
2386 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2387 * as the filler character.
2388 */
2389 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002390win_draw_end(
2391 win_T *wp,
2392 int c1,
2393 int c2,
2394 int row,
2395 int endrow,
2396 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2399 int n = 0;
2400# define FDC_OFF n
2401#else
2402# define FDC_OFF 0
2403#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002404#ifdef FEAT_FOLDING
2405 int fdc = compute_foldcolumn(wp, 0);
2406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408#ifdef FEAT_RIGHTLEFT
2409 if (wp->w_p_rl)
2410 {
2411 /* No check for cmdline window: should never be right-left. */
2412# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002413 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 if (n > 0)
2416 {
2417 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002418 if (n > wp->w_width)
2419 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2421 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002422 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
2424# endif
2425# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002426 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
2428 int nn = n + 2;
2429
2430 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002431 if (nn > wp->w_width)
2432 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2434 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 n = nn;
2437 }
2438# endif
2439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002440 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002441 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2443 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002444 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446 else
2447#endif
2448 {
2449#ifdef FEAT_CMDWIN
2450 if (cmdwin_type != 0 && wp == curwin)
2451 {
2452 /* draw the cmdline character in the leftmost column */
2453 n = 1;
2454 if (n > wp->w_width)
2455 n = wp->w_width;
2456 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002457 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002458 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460#endif
2461#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002462 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002464 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002467 if (nn > wp->w_width)
2468 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002470 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002471 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 n = nn;
2473 }
2474#endif
2475#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002476 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
2478 int nn = n + 2;
2479
2480 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002481 if (nn > wp->w_width)
2482 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002484 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002485 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 n = nn;
2487 }
2488#endif
2489 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002490 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002491 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493 set_empty_rows(wp, row);
2494}
2495
Bram Moolenaar1a384422010-07-14 19:53:30 +02002496#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002497/*
2498 * Advance **color_cols and return TRUE when there are columns to draw.
2499 */
2500 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002501advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002502{
2503 while (**color_cols >= 0 && vcol > **color_cols)
2504 ++*color_cols;
2505 return (**color_cols >= 0);
2506}
2507#endif
2508
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002509#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2510/*
2511 * Copy "text" to ScreenLines using "attr".
2512 * Returns the next screen column.
2513 */
2514 static int
2515text_to_screenline(win_T *wp, char_u *text, int col)
2516{
2517 int off = (int)(current_ScreenLine - ScreenLines);
2518
2519#ifdef FEAT_MBYTE
2520 if (has_mbyte)
2521 {
2522 int cells;
2523 int u8c, u8cc[MAX_MCO];
2524 int i;
2525 int idx;
2526 int c_len;
2527 char_u *p;
2528# ifdef FEAT_ARABIC
2529 int prev_c = 0; /* previous Arabic character */
2530 int prev_c1 = 0; /* first composing char for prev_c */
2531# endif
2532
2533# ifdef FEAT_RIGHTLEFT
2534 if (wp->w_p_rl)
2535 idx = off;
2536 else
2537# endif
2538 idx = off + col;
2539
2540 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2541 for (p = text; *p != NUL; )
2542 {
2543 cells = (*mb_ptr2cells)(p);
2544 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002545 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002546# ifdef FEAT_RIGHTLEFT
2547 - (wp->w_p_rl ? col : 0)
2548# endif
2549 )
2550 break;
2551 ScreenLines[idx] = *p;
2552 if (enc_utf8)
2553 {
2554 u8c = utfc_ptr2char(p, u8cc);
2555 if (*p < 0x80 && u8cc[0] == 0)
2556 {
2557 ScreenLinesUC[idx] = 0;
2558#ifdef FEAT_ARABIC
2559 prev_c = u8c;
2560#endif
2561 }
2562 else
2563 {
2564#ifdef FEAT_ARABIC
2565 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2566 {
2567 /* Do Arabic shaping. */
2568 int pc, pc1, nc;
2569 int pcc[MAX_MCO];
2570 int firstbyte = *p;
2571
2572 /* The idea of what is the previous and next
2573 * character depends on 'rightleft'. */
2574 if (wp->w_p_rl)
2575 {
2576 pc = prev_c;
2577 pc1 = prev_c1;
2578 nc = utf_ptr2char(p + c_len);
2579 prev_c1 = u8cc[0];
2580 }
2581 else
2582 {
2583 pc = utfc_ptr2char(p + c_len, pcc);
2584 nc = prev_c;
2585 pc1 = pcc[0];
2586 }
2587 prev_c = u8c;
2588
2589 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2590 pc, pc1, nc);
2591 ScreenLines[idx] = firstbyte;
2592 }
2593 else
2594 prev_c = u8c;
2595#endif
2596 /* Non-BMP character: display as ? or fullwidth ?. */
2597#ifdef UNICODE16
2598 if (u8c >= 0x10000)
2599 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2600 else
2601#endif
2602 ScreenLinesUC[idx] = u8c;
2603 for (i = 0; i < Screen_mco; ++i)
2604 {
2605 ScreenLinesC[i][idx] = u8cc[i];
2606 if (u8cc[i] == 0)
2607 break;
2608 }
2609 }
2610 if (cells > 1)
2611 ScreenLines[idx + 1] = 0;
2612 }
2613 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2614 /* double-byte single width character */
2615 ScreenLines2[idx] = p[1];
2616 else if (cells > 1)
2617 /* double-width character */
2618 ScreenLines[idx + 1] = p[1];
2619 col += cells;
2620 idx += cells;
2621 p += c_len;
2622 }
2623 }
2624 else
2625#endif
2626 {
2627 int len = (int)STRLEN(text);
2628
Bram Moolenaar02631462017-09-22 15:20:32 +02002629 if (len > wp->w_width - col)
2630 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002631 if (len > 0)
2632 {
2633#ifdef FEAT_RIGHTLEFT
2634 if (wp->w_p_rl)
2635 STRNCPY(current_ScreenLine, text, len);
2636 else
2637#endif
2638 STRNCPY(current_ScreenLine + col, text, len);
2639 col += len;
2640 }
2641 }
2642 return col;
2643}
2644#endif
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#ifdef FEAT_FOLDING
2647/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002648 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2649 * space is available for window "wp", minus "col".
2650 */
2651 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002652compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002653{
2654 int fdc = wp->w_p_fdc;
2655 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002656 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002657
2658 if (fdc > wwidth - (col + wmw))
2659 fdc = wwidth - (col + wmw);
2660 return fdc;
2661}
2662
2663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 * Display one folded line.
2665 */
2666 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002667fold_line(
2668 win_T *wp,
2669 long fold_count,
2670 foldinfo_T *foldinfo,
2671 linenr_T lnum,
2672 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002674 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 pos_T *top, *bot;
2676 linenr_T lnume = lnum + fold_count - 1;
2677 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002678 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 int col;
2681 int txtcol;
2682 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002683 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
2685 /* Build the fold line:
2686 * 1. Add the cmdwin_type for the command-line window
2687 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002688 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 * 4. Compose the text
2690 * 5. Add the text
2691 * 6. set highlighting for the Visual area an other text
2692 */
2693 col = 0;
2694
2695 /*
2696 * 1. Add the cmdwin_type for the command-line window
2697 * Ignores 'rightleft', this window is never right-left.
2698 */
2699#ifdef FEAT_CMDWIN
2700 if (cmdwin_type != 0 && wp == curwin)
2701 {
2702 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002703 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#ifdef FEAT_MBYTE
2705 if (enc_utf8)
2706 ScreenLinesUC[off] = 0;
2707#endif
2708 ++col;
2709 }
2710#endif
2711
2712 /*
2713 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002714 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002716 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (fdc > 0)
2718 {
2719 fill_foldcolumn(buf, wp, TRUE, lnum);
2720#ifdef FEAT_RIGHTLEFT
2721 if (wp->w_p_rl)
2722 {
2723 int i;
2724
Bram Moolenaar02631462017-09-22 15:20:32 +02002725 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002726 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 /* reverse the fold column */
2728 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002729 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731 else
2732#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002733 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 col += fdc;
2735 }
2736
2737#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002738# define RL_MEMSET(p, v, l) \
2739 do { \
2740 if (wp->w_p_rl) \
2741 for (ri = 0; ri < l; ++ri) \
2742 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2743 else \
2744 for (ri = 0; ri < l; ++ri) \
2745 ScreenAttrs[off + (p) + ri] = v; \
2746 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002748# define RL_MEMSET(p, v, l) \
2749 do { \
2750 for (ri = 0; ri < l; ++ri) \
2751 ScreenAttrs[off + (p) + ri] = v; \
2752 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#endif
2754
Bram Moolenaar64486672010-05-16 15:46:46 +02002755 /* Set all attributes of the 'number' or 'relativenumber' column and the
2756 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002757 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759#ifdef FEAT_SIGNS
2760 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002761 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002763 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (len > 0)
2765 {
2766 if (len > 2)
2767 len = 2;
2768# ifdef FEAT_RIGHTLEFT
2769 if (wp->w_p_rl)
2770 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002771 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002772 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else
2774# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002775 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 col += len;
2777 }
2778 }
2779#endif
2780
2781 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002782 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002784 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002786 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 if (len > 0)
2788 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002789 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002790 long num;
2791 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002792
2793 if (len > w + 1)
2794 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002795
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002796 if (wp->w_p_nu && !wp->w_p_rnu)
2797 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002798 num = (long)lnum;
2799 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002800 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002801 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002802 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002803 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002804 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002805 /* 'number' + 'relativenumber': cursor line shows absolute
2806 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002807 num = lnum;
2808 fmt = "%-*ld ";
2809 }
2810 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002811
Bram Moolenaar700e7342013-01-30 12:31:36 +01002812 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813#ifdef FEAT_RIGHTLEFT
2814 if (wp->w_p_rl)
2815 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002816 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002817 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 else
2819#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002820 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 col += len;
2822 }
2823 }
2824
2825 /*
2826 * 4. Compose the folded-line string with 'foldtext', if set.
2827 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002828 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830 txtcol = col; /* remember where text starts */
2831
2832 /*
2833 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2834 * Right-left text is put in columns 0 - number-col, normal text is put
2835 * in columns number-col - window-width.
2836 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002837 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
2839 /* Fill the rest of the line with the fold filler */
2840#ifdef FEAT_RIGHTLEFT
2841 if (wp->w_p_rl)
2842 col -= txtcol;
2843#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002844 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845#ifdef FEAT_RIGHTLEFT
2846 - (wp->w_p_rl ? txtcol : 0)
2847#endif
2848 )
2849 {
2850#ifdef FEAT_MBYTE
2851 if (enc_utf8)
2852 {
2853 if (fill_fold >= 0x80)
2854 {
2855 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002856 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002857 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002862 ScreenLines[off + col] = fill_fold;
2863 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002864 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002866 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002868 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
2870
2871 if (text != buf)
2872 vim_free(text);
2873
2874 /*
2875 * 6. set highlighting for the Visual area an other text.
2876 * If all folded lines are in the Visual area, highlight the line.
2877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2879 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002880 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
2882 /* Visual is after curwin->w_cursor */
2883 top = &curwin->w_cursor;
2884 bot = &VIsual;
2885 }
2886 else
2887 {
2888 /* Visual is before curwin->w_cursor */
2889 top = &VIsual;
2890 bot = &curwin->w_cursor;
2891 }
2892 if (lnum >= top->lnum
2893 && lnume <= bot->lnum
2894 && (VIsual_mode != 'v'
2895 || ((lnum > top->lnum
2896 || (lnum == top->lnum
2897 && top->col == 0))
2898 && (lnume < bot->lnum
2899 || (lnume == bot->lnum
2900 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002901 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903 if (VIsual_mode == Ctrl_V)
2904 {
2905 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002906 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002908 if (wp->w_old_cursor_lcol != MAXCOL
2909 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002910 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 len = wp->w_old_cursor_lcol;
2912 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002913 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002914 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002915 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917 }
2918 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002921 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002926#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002927 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2928 if (wp->w_p_cc_cols)
2929 {
2930 int i = 0;
2931 int j = wp->w_p_cc_cols[i];
2932 int old_txtcol = txtcol;
2933
2934 while (j > -1)
2935 {
2936 txtcol += j;
2937 if (wp->w_p_wrap)
2938 txtcol -= wp->w_skipcol;
2939 else
2940 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002941 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002942 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002943 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002944 txtcol = old_txtcol;
2945 j = wp->w_p_cc_cols[++i];
2946 }
2947 }
2948
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002949 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002950 if (wp->w_p_cuc)
2951 {
2952 txtcol += wp->w_virtcol;
2953 if (wp->w_p_wrap)
2954 txtcol -= wp->w_skipcol;
2955 else
2956 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002957 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002958 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002959 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002960 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
Bram Moolenaar02631462017-09-22 15:20:32 +02002963 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2964 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 /*
2967 * Update w_cline_height and w_cline_folded if the cursor line was
2968 * updated (saves a call to plines() later).
2969 */
2970 if (wp == curwin
2971 && lnum <= curwin->w_cursor.lnum
2972 && lnume >= curwin->w_cursor.lnum)
2973 {
2974 curwin->w_cline_row = row;
2975 curwin->w_cline_height = 1;
2976 curwin->w_cline_folded = TRUE;
2977 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2978 }
2979}
2980
2981/*
2982 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2983 */
2984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002985copy_text_attr(
2986 int off,
2987 char_u *buf,
2988 int len,
2989 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002991 int i;
2992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 mch_memmove(ScreenLines + off, buf, (size_t)len);
2994# ifdef FEAT_MBYTE
2995 if (enc_utf8)
2996 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2997# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002998 for (i = 0; i < len; ++i)
2999 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000}
3001
3002/*
3003 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003004 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 */
3006 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003007fill_foldcolumn(
3008 char_u *p,
3009 win_T *wp,
3010 int closed, /* TRUE of FALSE */
3011 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 int i = 0;
3014 int level;
3015 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003016 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003017 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
3019 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003020 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 level = win_foldinfo.fi_level;
3023 if (level > 0)
3024 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003025 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003026 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 /* If the column is too narrow, we start at the lowest level that
3029 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003030 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (first_level < 1)
3032 first_level = 1;
3033
Bram Moolenaar1c934292015-01-27 16:39:29 +01003034 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
3036 if (win_foldinfo.fi_lnum == lnum
3037 && first_level + i >= win_foldinfo.fi_low_level)
3038 p[i] = '-';
3039 else if (first_level == 1)
3040 p[i] = '|';
3041 else if (first_level + i <= 9)
3042 p[i] = '0' + first_level + i;
3043 else
3044 p[i] = '>';
3045 if (first_level + i == level)
3046 break;
3047 }
3048 }
3049 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003050 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051}
3052#endif /* FEAT_FOLDING */
3053
3054/*
3055 * Display line "lnum" of window 'wp' on the screen.
3056 * Start at row "startrow", stop when "endrow" is reached.
3057 * wp->w_virtcol needs to be valid.
3058 *
3059 * Return the number of last row the line occupies.
3060 */
3061 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003062win_line(
3063 win_T *wp,
3064 linenr_T lnum,
3065 int startrow,
3066 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003067 int nochange UNUSED, // not updating for changed text
3068 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003070 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3072 int c = 0; /* init for GCC */
3073 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003074#ifdef FEAT_LINEBREAK
3075 long vcol_sbr = -1; /* virtual column after showbreak */
3076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 long vcol_prev = -1; /* "vcol" of previous character */
3078 char_u *line; /* current line */
3079 char_u *ptr; /* current position in "line" */
3080 int row; /* row in the window, excl w_winrow */
3081 int screen_row; /* row on the screen, incl w_winrow */
3082
3083 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3084 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003085 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003086 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 int c_extra = NUL; /* extra chars, all the same */
3088 int extra_attr = 0; /* attributes when n_extra != 0 */
3089 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3090 displaying lcs_eol at end-of-line */
3091 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3092 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3093
3094 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3095 int saved_n_extra = 0;
3096 char_u *saved_p_extra = NULL;
3097 int saved_c_extra = 0;
3098 int saved_char_attr = 0;
3099
3100 int n_attr = 0; /* chars with special attr */
3101 int saved_attr2 = 0; /* char_attr saved for n_attr */
3102 int n_attr3 = 0; /* chars with overruling special attr */
3103 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3104
3105 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3106
3107 int fromcol, tocol; /* start/end of inverting */
3108 int fromcol_prev = -2; /* start of inverting after cursor */
3109 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003111 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 pos_T pos;
3113 long v;
3114
3115 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003116 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3118 in this line */
3119 int attr = 0; /* attributes for area highlighting */
3120 int area_attr = 0; /* attributes desired by highlighting */
3121 int search_attr = 0; /* attributes desired by 'hlsearch' */
3122#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003123 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 int syntax_attr = 0; /* attributes desired by syntax */
3125 int has_syntax = FALSE; /* this buffer has syntax highl. */
3126 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003127 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003128 int draw_color_col = FALSE; /* highlight colorcolumn */
3129 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003130#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003131#ifdef FEAT_TEXT_PROP
3132 int text_prop_count;
3133 int text_prop_next = 0; // next text property to use
3134 textprop_T *text_props = NULL;
3135 int *text_prop_idxs = NULL;
3136 int text_props_active = 0;
3137 proptype_T *text_prop_type = NULL;
3138 int text_prop_attr = 0;
3139#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003140#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003141 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003142# define SPWORDLEN 150
3143 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003144 int nextlinecol = 0; /* column where nextline[] starts */
3145 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003146 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003147 int spell_attr = 0; /* attributes desired by spelling */
3148 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003149 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3150 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003151 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003152 static int cap_col = -1; /* column to check for Cap word */
3153 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003154 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003156 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#ifdef FEAT_MBYTE
3158 int multi_attr = 0; /* attributes desired by multibyte */
3159 int mb_l = 1; /* multi-byte byte length */
3160 int mb_c = 0; /* decoded multi-byte character */
3161 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003162 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
3164#ifdef FEAT_DIFF
3165 int filler_lines; /* nr of filler lines to be drawn */
3166 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003167 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 int change_start = MAXCOL; /* first col of changed area */
3169 int change_end = -1; /* last col of changed area */
3170#endif
3171 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3172#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003173 int need_showbreak = FALSE; /* overlong line, skipping first x
3174 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003176#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003177 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003179 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180#endif
3181#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003182 matchitem_T *cur; /* points to the match list */
3183 match_T *shl; /* points to search_hl or a match */
3184 int shl_flag; /* flag to indicate whether search_hl
3185 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003186 int pos_inprogress; /* marks that position match search is
3187 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003188 int prevcol_hl_flag; /* flag to indicate whether prevcol
3189 equals startcol of search_hl or one
3190 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191#endif
3192#ifdef FEAT_ARABIC
3193 int prev_c = 0; /* previous Arabic character */
3194 int prev_c1 = 0; /* first composing char for prev_c */
3195#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003196#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003197 int did_line_attr = 0;
3198#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003199#ifdef FEAT_TERMINAL
3200 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003201 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 /* draw_state: items that are drawn in sequence: */
3205#define WL_START 0 /* nothing done yet */
3206#ifdef FEAT_CMDWIN
3207# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3208#else
3209# define WL_CMDLINE WL_START
3210#endif
3211#ifdef FEAT_FOLDING
3212# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3213#else
3214# define WL_FOLD WL_CMDLINE
3215#endif
3216#ifdef FEAT_SIGNS
3217# define WL_SIGN WL_FOLD + 1 /* column for signs */
3218#else
3219# define WL_SIGN WL_FOLD /* column for signs */
3220#endif
3221#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003222#ifdef FEAT_LINEBREAK
3223# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003225# define WL_BRI WL_NR
3226#endif
3227#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3228# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3229#else
3230# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231#endif
3232#define WL_LINE WL_SBR + 1 /* text in the line */
3233 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003234#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 int feedback_col = 0;
3236 int feedback_old_attr = -1;
3237#endif
3238
Bram Moolenaar860cae12010-06-05 23:22:07 +02003239#ifdef FEAT_CONCEAL
3240 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003241 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003242 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003243 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003244 int is_concealing = FALSE;
3245 int boguscols = 0; /* nonexistent columns added to force
3246 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003247 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003248 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003249 int match_conc = 0; /* cchar for match functions */
3250 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003251 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003252# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003253# define FIX_FOR_BOGUSCOLS \
3254 { \
3255 n_extra += vcol_off; \
3256 vcol -= vcol_off; \
3257 vcol_off = 0; \
3258 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003259 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003260 boguscols = 0; \
3261 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003262#else
3263# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 if (startrow > endrow) /* past the end already! */
3267 return startrow;
3268
3269 row = startrow;
3270 screen_row = row + W_WINROW(wp);
3271
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003272 if (!number_only)
3273 {
3274 /*
3275 * To speed up the loop below, set extra_check when there is linebreak,
3276 * trailing white space and/or syntax processing to be done.
3277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003279 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#endif
3281#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003282 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003283# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003284 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003285# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003286 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003288 /* Prepare for syntax highlighting in this line. When there is an
3289 * error, stop syntax highlighting. */
3290 save_did_emsg = did_emsg;
3291 did_emsg = FALSE;
3292 syntax_start(wp, lnum);
3293 if (did_emsg)
3294 wp->w_s->b_syn_error = TRUE;
3295 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003296 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003297 did_emsg = save_did_emsg;
3298#ifdef SYN_TIME_LIMIT
3299 if (!wp->w_s->b_syn_slow)
3300#endif
3301 {
3302 has_syntax = TRUE;
3303 extra_check = TRUE;
3304 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003307
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003308 /* Check for columns to display for 'colorcolumn'. */
3309 color_cols = wp->w_p_cc_cols;
3310 if (color_cols != NULL)
3311 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003312#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003313
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003314#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003315 if (term_show_buffer(wp->w_buffer))
3316 {
3317 extra_check = TRUE;
3318 get_term_attr = TRUE;
3319 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3320 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003321#endif
3322
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003323#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003324 if (wp->w_p_spell
3325 && *wp->w_s->b_p_spl != NUL
3326 && wp->w_s->b_langp.ga_len > 0
3327 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003328 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003329 /* Prepare for spell checking. */
3330 has_spell = TRUE;
3331 extra_check = TRUE;
3332
Bram Moolenaar7701f302018-10-02 21:20:32 +02003333 /* Get the start of the next line, so that words that wrap to the
3334 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003335 * Trick: skip a few chars for C/shell/Vim comments */
3336 nextline[SPWORDLEN] = NUL;
3337 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3338 {
3339 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3340 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3341 }
3342
Bram Moolenaar7701f302018-10-02 21:20:32 +02003343 /* When a word wrapped from the previous line the start of the
3344 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003345 if (lnum == checked_lnum)
3346 cur_checked_col = checked_col;
3347 checked_lnum = 0;
3348
3349 /* When there was a sentence end in the previous line may require a
3350 * word starting with capital in this line. In line 1 always check
3351 * the first word. */
3352 if (lnum != capcol_lnum)
3353 cap_col = -1;
3354 if (lnum == 1)
3355 cap_col = 0;
3356 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358#endif
3359
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003360 /*
3361 * handle visual active in this window
3362 */
3363 fromcol = -10;
3364 tocol = MAXCOL;
3365 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 /* Visual is after curwin->w_cursor */
3368 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003370 top = &curwin->w_cursor;
3371 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003373 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003375 top = &VIsual;
3376 bot = &curwin->w_cursor;
3377 }
3378 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3379 if (VIsual_mode == Ctrl_V) /* block mode */
3380 {
3381 if (lnum_in_visual_area)
3382 {
3383 fromcol = wp->w_old_cursor_fcol;
3384 tocol = wp->w_old_cursor_lcol;
3385 }
3386 }
3387 else /* non-block mode */
3388 {
3389 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003391 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003393 if (VIsual_mode == 'V') /* linewise */
3394 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 else
3396 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003397 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3398 if (gchar_pos(top) == NUL)
3399 tocol = fromcol + 1;
3400 }
3401 }
3402 if (VIsual_mode != 'V' && lnum == bot->lnum)
3403 {
3404 if (*p_sel == 'e' && bot->col == 0
3405#ifdef FEAT_VIRTUALEDIT
3406 && bot->coladd == 0
3407#endif
3408 )
3409 {
3410 fromcol = -10;
3411 tocol = MAXCOL;
3412 }
3413 else if (bot->col == MAXCOL)
3414 tocol = MAXCOL;
3415 else
3416 {
3417 pos = *bot;
3418 if (*p_sel == 'e')
3419 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3420 else
3421 {
3422 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3423 ++tocol;
3424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426 }
3427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 /* Check if the character under the cursor should not be inverted */
3430 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003431#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003432 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003433#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003434 )
3435 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003437 /* if inverting in this line set area_highlighting */
3438 if (fromcol >= 0)
3439 {
3440 area_highlighting = TRUE;
3441 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003443 if ((clip_star.available && !clip_star.owned
3444 && clip_isautosel_star())
3445 || (clip_plus.available && !clip_plus.owned
3446 && clip_isautosel_plus()))
3447 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003452 /*
3453 * handle 'incsearch' and ":s///c" highlighting
3454 */
3455 else if (highlight_match
3456 && wp == curwin
3457 && lnum >= curwin->w_cursor.lnum
3458 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003460 if (lnum == curwin->w_cursor.lnum)
3461 getvcol(curwin, &(curwin->w_cursor),
3462 (colnr_T *)&fromcol, NULL, NULL);
3463 else
3464 fromcol = 0;
3465 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3466 {
3467 pos.lnum = lnum;
3468 pos.col = search_match_endcol;
3469 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3470 }
3471 else
3472 tocol = MAXCOL;
3473 /* do at least one character; happens when past end of line */
3474 if (fromcol == tocol)
3475 tocol = fromcol + 1;
3476 area_highlighting = TRUE;
3477 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480
3481#ifdef FEAT_DIFF
3482 filler_lines = diff_check(wp, lnum);
3483 if (filler_lines < 0)
3484 {
3485 if (filler_lines == -1)
3486 {
3487 if (diff_find_change(wp, lnum, &change_start, &change_end))
3488 diff_hlf = HLF_ADD; /* added line */
3489 else if (change_start == 0)
3490 diff_hlf = HLF_TXD; /* changed text */
3491 else
3492 diff_hlf = HLF_CHD; /* changed line */
3493 }
3494 else
3495 diff_hlf = HLF_ADD; /* added line */
3496 filler_lines = 0;
3497 area_highlighting = TRUE;
3498 }
3499 if (lnum == wp->w_topline)
3500 filler_lines = wp->w_topfill;
3501 filler_todo = filler_lines;
3502#endif
3503
3504#ifdef LINE_ATTR
3505# ifdef FEAT_SIGNS
3506 /* If this line has a sign with line highlighting set line_attr. */
3507 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3508 if (v != 0)
3509 line_attr = sign_get_attr((int)v, TRUE);
3510# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003511# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003513 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003514 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515# endif
3516 if (line_attr != 0)
3517 area_highlighting = TRUE;
3518#endif
3519
3520 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3521 ptr = line;
3522
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003523#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003524 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003525 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003526 /* For checking first word with a capital skip white space. */
3527 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003528 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003529
Bram Moolenaar30abd282005-06-22 22:35:10 +00003530 /* To be able to spell-check over line boundaries copy the end of the
3531 * current line into nextline[]. Above the start of the next line was
3532 * copied to nextline[SPWORDLEN]. */
3533 if (nextline[SPWORDLEN] == NUL)
3534 {
3535 /* No next line or it is empty. */
3536 nextlinecol = MAXCOL;
3537 nextline_idx = 0;
3538 }
3539 else
3540 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003541 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003542 if (v < SPWORDLEN)
3543 {
3544 /* Short line, use it completely and append the start of the
3545 * next line. */
3546 nextlinecol = 0;
3547 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003548 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003549 nextline_idx = v + 1;
3550 }
3551 else
3552 {
3553 /* Long line, use only the last SPWORDLEN bytes. */
3554 nextlinecol = v - SPWORDLEN;
3555 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3556 nextline_idx = SPWORDLEN + 1;
3557 }
3558 }
3559 }
3560#endif
3561
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003562 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003564 if (lcs_space || lcs_trail)
3565 extra_check = TRUE;
3566 /* find start of trailing whitespace */
3567 if (lcs_trail)
3568 {
3569 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003570 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003571 --trailcol;
3572 trailcol += (colnr_T) (ptr - line);
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
3576 /*
3577 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3578 * first character to be displayed.
3579 */
3580 if (wp->w_p_wrap)
3581 v = wp->w_skipcol;
3582 else
3583 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003584 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586#ifdef FEAT_MBYTE
3587 char_u *prev_ptr = ptr;
3588#endif
3589 while (vcol < v && *ptr != NUL)
3590 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003591 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 vcol += c;
3593#ifdef FEAT_MBYTE
3594 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003596 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003599 /* When:
3600 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003601 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003602 * - 'virtualedit' is set, or
3603 * - the visual mode is active,
3604 * the end of the line may be before the start of the displayed part.
3605 */
3606 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003607#ifdef FEAT_SYN_HL
3608 wp->w_p_cuc || draw_color_col ||
3609#endif
3610#ifdef FEAT_VIRTUALEDIT
3611 virtual_active() ||
3612#endif
3613 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
3618 /* Handle a character that's not completely on the screen: Put ptr at
3619 * that character but skip the first few screen characters. */
3620 if (vcol > v)
3621 {
3622 vcol -= c;
3623#ifdef FEAT_MBYTE
3624 ptr = prev_ptr;
3625#else
3626 --ptr;
3627#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003628 /* If the character fits on the screen, don't need to skip it.
3629 * Except for a TAB. */
3630 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003631#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003632 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003633#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003634 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003635 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637
3638 /*
3639 * Adjust for when the inverted text is before the screen,
3640 * and when the start of the inverted text is before the screen.
3641 */
3642 if (tocol <= vcol)
3643 fromcol = 0;
3644 else if (fromcol >= 0 && fromcol < vcol)
3645 fromcol = vcol;
3646
3647#ifdef FEAT_LINEBREAK
3648 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3649 if (wp->w_p_wrap)
3650 need_showbreak = TRUE;
3651#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003652#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003653 /* When spell checking a word we need to figure out the start of the
3654 * word and if it's badly spelled or not. */
3655 if (has_spell)
3656 {
3657 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003658 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003659 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003660
3661 pos = wp->w_cursor;
3662 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003663 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003664 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003665
3666 /* spell_move_to() may call ml_get() and make "line" invalid */
3667 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3668 ptr = line + linecol;
3669
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003670 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003671 {
3672 /* no bad word found at line start, don't check until end of a
3673 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003674 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003675 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003676 }
3677 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003678 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003679 /* bad word found, use attributes until end of word */
3680 word_end = wp->w_cursor.col + len + 1;
3681
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003682 /* Turn index into actual attributes. */
3683 if (spell_hlf != HLF_COUNT)
3684 spell_attr = highlight_attr[spell_hlf];
3685 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003686 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003687
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003688# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003689 /* Need to restart syntax highlighting for this line. */
3690 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003691 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003692# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003693 }
3694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696
3697 /*
3698 * Correct highlighting for cursor that can't be disabled.
3699 * Avoids having to check this for each character.
3700 */
3701 if (fromcol >= 0)
3702 {
3703 if (noinvcur)
3704 {
3705 if ((colnr_T)fromcol == wp->w_virtcol)
3706 {
3707 /* highlighting starts at cursor, let it start just after the
3708 * cursor */
3709 fromcol_prev = fromcol;
3710 fromcol = -1;
3711 }
3712 else if ((colnr_T)fromcol < wp->w_virtcol)
3713 /* restart highlighting after the cursor */
3714 fromcol_prev = wp->w_virtcol;
3715 }
3716 if (fromcol >= tocol)
3717 fromcol = -1;
3718 }
3719
3720#ifdef FEAT_SEARCH_EXTRA
3721 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003722 * Handle highlighting the last used search pattern and matches.
3723 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003725 cur = wp->w_match_head;
3726 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003727 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003729 if (shl_flag == FALSE)
3730 {
3731 shl = &search_hl;
3732 shl_flag = TRUE;
3733 }
3734 else
3735 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003736 shl->startcol = MAXCOL;
3737 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003739 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003740 v = (long)(ptr - line);
3741 if (cur != NULL)
3742 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003743 next_search_hl(wp, shl, lnum, (colnr_T)v,
3744 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003745
3746 /* Need to get the line again, a multi-line regexp may have made it
3747 * invalid. */
3748 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3749 ptr = line + v;
3750
3751 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003753 if (shl->lnum == lnum)
3754 shl->startcol = shl->rm.startpos[0].col;
3755 else
3756 shl->startcol = 0;
3757 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3758 - shl->rm.startpos[0].lnum)
3759 shl->endcol = shl->rm.endpos[0].col;
3760 else
3761 shl->endcol = MAXCOL;
3762 /* Highlight one character for an empty match. */
3763 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003766 if (has_mbyte && line[shl->endcol] != NUL)
3767 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3768 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003770 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003772 if ((long)shl->startcol < v) /* match at leftcol */
3773 {
3774 shl->attr_cur = shl->attr;
3775 search_attr = shl->attr;
3776 }
3777 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003779 if (shl != &search_hl && cur != NULL)
3780 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
3782#endif
3783
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003784#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003785 /* Cursor line highlighting for 'cursorline' in the current window. Not
3786 * when Visual mode is active, because it's not clear what is selected
3787 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003788 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003789 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003790 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003791 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003792 area_highlighting = TRUE;
3793 }
3794#endif
3795
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003796#ifdef FEAT_TEXT_PROP
3797 {
3798 char_u *prop_start;
3799
3800 text_prop_count = get_text_props(wp->w_buffer, lnum,
3801 &prop_start, FALSE);
3802 if (text_prop_count > 0)
3803 {
3804 // Make a copy of the properties, so that they are properly
3805 // aligned.
3806 text_props = (textprop_T *)alloc(
3807 text_prop_count * sizeof(textprop_T));
3808 if (text_props != NULL)
3809 mch_memmove(text_props, prop_start,
3810 text_prop_count * sizeof(textprop_T));
3811
3812 // Allocate an array for the indexes.
3813 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3814 area_highlighting = TRUE;
3815 extra_check = TRUE;
3816 }
3817 }
3818#endif
3819
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003820 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 col = 0;
3822#ifdef FEAT_RIGHTLEFT
3823 if (wp->w_p_rl)
3824 {
3825 /* Rightleft window: process the text in the normal direction, but put
3826 * it in current_ScreenLine[] from right to left. Start at the
3827 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003828 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 off += col;
3830 }
3831#endif
3832
3833 /*
3834 * Repeat for the whole displayed line.
3835 */
3836 for (;;)
3837 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003838#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003839 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 /* Skip this quickly when working on the text. */
3842 if (draw_state != WL_LINE)
3843 {
3844#ifdef FEAT_CMDWIN
3845 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3846 {
3847 draw_state = WL_CMDLINE;
3848 if (cmdwin_type != 0 && wp == curwin)
3849 {
3850 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003852 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003853 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 }
3856#endif
3857
3858#ifdef FEAT_FOLDING
3859 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3860 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003861 int fdc = compute_foldcolumn(wp, 0);
3862
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003864 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003866 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003867 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003868 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003869 p_extra_free = alloc(12 + 1);
3870
3871 if (p_extra_free != NULL)
3872 {
3873 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3874 n_extra = fdc;
3875 p_extra_free[n_extra] = NUL;
3876 p_extra = p_extra_free;
3877 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003878 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881 }
3882#endif
3883
3884#ifdef FEAT_SIGNS
3885 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3886 {
3887 draw_state = WL_SIGN;
3888 /* Show the sign column when there are any signs in this
3889 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003890 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003892 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003894 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895# endif
3896
3897 /* Draw two cells with the sign value or blank. */
3898 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003899 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 n_extra = 2;
3901
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003902 if (row == startrow
3903#ifdef FEAT_DIFF
3904 + filler_lines && filler_todo <= 0
3905#endif
3906 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3909 SIGN_TEXT);
3910# ifdef FEAT_SIGN_ICONS
3911 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3912 SIGN_ICON);
3913 if (gui.in_use && icon_sign != 0)
3914 {
3915 /* Use the image in this position. */
3916 c_extra = SIGN_BYTE;
3917# ifdef FEAT_NETBEANS_INTG
3918 if (buf_signcount(wp->w_buffer, lnum) > 1)
3919 c_extra = MULTISIGN_BYTE;
3920# endif
3921 char_attr = icon_sign;
3922 }
3923 else
3924# endif
3925 if (text_sign != 0)
3926 {
3927 p_extra = sign_get_text(text_sign);
3928 if (p_extra != NULL)
3929 {
3930 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003931 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933 char_attr = sign_get_attr(text_sign, FALSE);
3934 }
3935 }
3936 }
3937 }
3938#endif
3939
3940 if (draw_state == WL_NR - 1 && n_extra == 0)
3941 {
3942 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003943 /* Display the absolute or relative line number. After the
3944 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3945 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 && (row == startrow
3947#ifdef FEAT_DIFF
3948 + filler_lines
3949#endif
3950 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3951 {
3952 /* Draw the line number (empty space after wrapping). */
3953 if (row == startrow
3954#ifdef FEAT_DIFF
3955 + filler_lines
3956#endif
3957 )
3958 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003959 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003960 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003961
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003962 if (wp->w_p_nu && !wp->w_p_rnu)
3963 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003964 num = (long)lnum;
3965 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003966 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003967 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003968 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003969 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003970 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003971 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003972 num = lnum;
3973 fmt = "%-*ld ";
3974 }
3975 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003976
Bram Moolenaar700e7342013-01-30 12:31:36 +01003977 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003978 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (wp->w_skipcol > 0)
3980 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3981 *p_extra = '-';
3982#ifdef FEAT_RIGHTLEFT
3983 if (wp->w_p_rl) /* reverse line numbers */
3984 rl_mirror(extra);
3985#endif
3986 p_extra = extra;
3987 c_extra = NUL;
3988 }
3989 else
3990 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003991 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003992 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003993#ifdef FEAT_SYN_HL
3994 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003995 * the current line differently.
3996 * TODO: Can we use CursorLine instead of CursorLineNr
3997 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003998 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003999 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004000 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003 }
4004
Bram Moolenaar597a4222014-06-25 14:39:50 +02004005#ifdef FEAT_LINEBREAK
4006 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4007 && n_extra == 0 && *p_sbr != NUL)
4008 /* draw indent after showbreak value */
4009 draw_state = WL_BRI;
4010 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4011 /* After the showbreak, draw the breakindent */
4012 draw_state = WL_BRI - 1;
4013
4014 /* draw 'breakindent': indent wrapped text accordingly */
4015 if (draw_state == WL_BRI - 1 && n_extra == 0)
4016 {
4017 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004018 /* if need_showbreak is set, breakindent also applies */
4019 if (wp->w_p_bri && n_extra == 0
4020 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004021# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004022 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004023# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004024 )
4025 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004026 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004027# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004028 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004029 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004030 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004031# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004032 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4033 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004034 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004035# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004036 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004037# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004038 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004039 c_extra = ' ';
4040 n_extra = get_breakindent_win(wp,
4041 ml_get_buf(wp->w_buffer, lnum, FALSE));
4042 /* Correct end of highlighted area for 'breakindent',
4043 * required when 'linebreak' is also set. */
4044 if (tocol == vcol)
4045 tocol += n_extra;
4046 }
4047 }
4048#endif
4049
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4051 if (draw_state == WL_SBR - 1 && n_extra == 0)
4052 {
4053 draw_state = WL_SBR;
4054# ifdef FEAT_DIFF
4055 if (filler_todo > 0)
4056 {
4057 /* Draw "deleted" diff line(s). */
4058 if (char2cells(fill_diff) > 1)
4059 c_extra = '-';
4060 else
4061 c_extra = fill_diff;
4062# ifdef FEAT_RIGHTLEFT
4063 if (wp->w_p_rl)
4064 n_extra = col + 1;
4065 else
4066# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004067 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004068 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070# endif
4071# ifdef FEAT_LINEBREAK
4072 if (*p_sbr != NUL && need_showbreak)
4073 {
4074 /* Draw 'showbreak' at the start of each broken line. */
4075 p_extra = p_sbr;
4076 c_extra = NUL;
4077 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004078 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004080 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 /* Correct end of highlighted area for 'showbreak',
4082 * required when 'linebreak' is also set. */
4083 if (tocol == vcol)
4084 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004085#ifdef FEAT_SYN_HL
4086 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004087 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004088 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004089 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
4092# endif
4093 }
4094#endif
4095
4096 if (draw_state == WL_LINE - 1 && n_extra == 0)
4097 {
4098 draw_state = WL_LINE;
4099 if (saved_n_extra)
4100 {
4101 /* Continue item from end of wrapped line. */
4102 n_extra = saved_n_extra;
4103 c_extra = saved_c_extra;
4104 p_extra = saved_p_extra;
4105 char_attr = saved_char_attr;
4106 }
4107 else
4108 char_attr = 0;
4109 }
4110 }
4111
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004112 // When still displaying '$' of change command, stop at cursor.
4113 // When only displaying the (relative) line number and that's done,
4114 // stop here.
4115 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004116 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#ifdef FEAT_DIFF
4118 && filler_todo <= 0
4119#endif
4120 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004121 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004123 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004124 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004125 /* Pretend we have finished updating the window. Except when
4126 * 'cursorcolumn' is set. */
4127#ifdef FEAT_SYN_HL
4128 if (wp->w_p_cuc)
4129 row = wp->w_cline_row + wp->w_cline_height;
4130 else
4131#endif
4132 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134 }
4135
4136 if (draw_state == WL_LINE && area_highlighting)
4137 {
4138 /* handle Visual or match highlighting in this line */
4139 if (vcol == fromcol
4140#ifdef FEAT_MBYTE
4141 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4142 && (*mb_ptr2cells)(ptr) > 1)
4143#endif
4144 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004145 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 && vcol < tocol))
4147 area_attr = attr; /* start highlighting */
4148 else if (area_attr != 0
4149 && (vcol == tocol
4150 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153#ifdef FEAT_SEARCH_EXTRA
4154 if (!n_extra)
4155 {
4156 /*
4157 * Check for start/end of search pattern match.
4158 * After end, check for start/end of next match.
4159 * When another match, have to check for start again.
4160 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004161 * Do this for 'search_hl' and the match list (ordered by
4162 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004164 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004165 cur = wp->w_match_head;
4166 shl_flag = FALSE;
4167 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004169 if (shl_flag == FALSE
4170 && ((cur != NULL
4171 && cur->priority > SEARCH_HL_PRIORITY)
4172 || cur == NULL))
4173 {
4174 shl = &search_hl;
4175 shl_flag = TRUE;
4176 }
4177 else
4178 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004179 if (cur != NULL)
4180 cur->pos.cur = 0;
4181 pos_inprogress = TRUE;
4182 while (shl->rm.regprog != NULL
4183 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004185 if (shl->startcol != MAXCOL
4186 && v >= (long)shl->startcol
4187 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004189#ifdef FEAT_MBYTE
4190 int tmp_col = v + MB_PTR2LEN(ptr);
4191
4192 if (shl->endcol < tmp_col)
4193 shl->endcol = tmp_col;
4194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004196#ifdef FEAT_CONCEAL
4197 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4198 == cur->hlg_id)
4199 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004200 has_match_conc =
4201 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004202 match_conc = cur->conceal_char;
4203 }
4204 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004205 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004208 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004211 next_search_hl(wp, shl, lnum, (colnr_T)v,
4212 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004213 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004214 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 /* Need to get the line again, a multi-line regexp
4217 * may have made it invalid. */
4218 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4219 ptr = line + v;
4220
4221 if (shl->lnum == lnum)
4222 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004223 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004225 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004227 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004229 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
4231 /* highlight empty match, try again after
4232 * it */
4233#ifdef FEAT_MBYTE
4234 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004235 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004236 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 else
4238#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004239 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241
4242 /* Loop to check if the match starts at the
4243 * current position */
4244 continue;
4245 }
4246 }
4247 break;
4248 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004249 if (shl != &search_hl && cur != NULL)
4250 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004252
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004253 /* Use attributes from match with highest priority among
4254 * 'search_hl' and the match list. */
4255 search_attr = search_hl.attr_cur;
4256 cur = wp->w_match_head;
4257 shl_flag = FALSE;
4258 while (cur != NULL || shl_flag == FALSE)
4259 {
4260 if (shl_flag == FALSE
4261 && ((cur != NULL
4262 && cur->priority > SEARCH_HL_PRIORITY)
4263 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004264 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004265 shl = &search_hl;
4266 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004267 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004268 else
4269 shl = &cur->hl;
4270 if (shl->attr_cur != 0)
4271 search_attr = shl->attr_cur;
4272 if (shl != &search_hl && cur != NULL)
4273 cur = cur->next;
4274 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004275 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004276 if (*ptr == NUL && (did_line_attr >= 1
4277 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004278 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280#endif
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004283 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004285 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4286 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004288 if (diff_hlf == HLF_TXD && ptr - line > change_end
4289 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004291 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004292 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004293 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004296
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004297#ifdef FEAT_TEXT_PROP
4298 if (text_props != NULL)
4299 {
4300 int pi;
4301
4302 // Check if any active property ends.
4303 for (pi = 0; pi < text_props_active; ++pi)
4304 {
4305 int tpi = text_prop_idxs[pi];
4306
Bram Moolenaar8caa10a2018-12-30 22:07:40 +01004307 if (vcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004308 + text_props[tpi].tp_len)
4309 {
4310 if (pi + 1 < text_props_active)
4311 mch_memmove(text_prop_idxs + pi,
4312 text_prop_idxs + pi + 1,
4313 sizeof(int)
4314 * (text_props_active - (pi + 1)));
4315 --text_props_active;
4316 --pi;
4317 }
4318 }
4319
4320 // Add any text property that starts in this column.
4321 while (text_prop_next < text_prop_count
Bram Moolenaar8caa10a2018-12-30 22:07:40 +01004322 && vcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004323 text_prop_idxs[text_props_active++] = text_prop_next++;
4324
4325 text_prop_type = NULL;
4326 if (text_props_active > 0)
4327 {
4328 int max_priority = INT_MIN;
4329 int max_col = 0;
4330
4331 // Get the property type with the highest priority
4332 // and/or starting last.
4333 for (pi = 0; pi < text_props_active; ++pi)
4334 {
4335 int tpi = text_prop_idxs[pi];
4336 proptype_T *pt;
4337
4338 pt = text_prop_type_by_id(
4339 curwin->w_buffer, text_props[tpi].tp_type);
4340 if (pt != NULL
4341 && (pt->pt_priority > max_priority
4342 || (pt->pt_priority == max_priority
4343 && text_props[tpi].tp_col >= max_col)))
4344 {
4345 text_prop_type = pt;
4346 max_priority = pt->pt_priority;
4347 max_col = text_props[tpi].tp_col;
4348 }
4349 }
4350 if (text_prop_type != NULL)
4351 text_prop_attr =
4352 syn_id2attr(text_prop_type->pt_hl_id);
4353 }
4354 }
4355#endif
4356
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004357 /* Decide which of the highlight attributes to use. */
4358 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004359#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004360 if (area_attr != 0)
4361 char_attr = hl_combine_attr(line_attr, area_attr);
4362 else if (search_attr != 0)
4363 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004364 /* Use line_attr when not in the Visual or 'incsearch' area
4365 * (area_attr may be 0 when "noinvcur" is set). */
4366 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004367 || vcol < fromcol || vcol_prev < fromcol_prev
4368 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004369 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004370#else
4371 if (area_attr != 0)
4372 char_attr = area_attr;
4373 else if (search_attr != 0)
4374 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004375#endif
4376 else
4377 {
4378 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004379#ifdef FEAT_TEXT_PROP
4380 if (text_prop_type != NULL)
4381 char_attr = text_prop_attr;
4382 else
4383#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004384#ifdef FEAT_SYN_HL
4385 if (has_syntax)
4386 char_attr = syntax_attr;
4387 else
4388#endif
4389 char_attr = 0;
4390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
4392
4393 /*
4394 * Get the next character to put on the screen.
4395 */
4396 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004397 * The "p_extra" points to the extra stuff that is inserted to
4398 * represent special characters (non-printable stuff) and other
4399 * things. When all characters are the same, c_extra is used.
4400 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4401 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4403 */
4404 if (n_extra > 0)
4405 {
4406 if (c_extra != NUL)
4407 {
4408 c = c_extra;
4409#ifdef FEAT_MBYTE
4410 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004411 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
4413 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004414 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004415 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417 else
4418 mb_utf8 = FALSE;
4419#endif
4420 }
4421 else
4422 {
4423 c = *p_extra;
4424#ifdef FEAT_MBYTE
4425 if (has_mbyte)
4426 {
4427 mb_c = c;
4428 if (enc_utf8)
4429 {
4430 /* If the UTF-8 character is more than one byte:
4431 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004432 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 mb_utf8 = FALSE;
4434 if (mb_l > n_extra)
4435 mb_l = 1;
4436 else if (mb_l > 1)
4437 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004438 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004440 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442 }
4443 else
4444 {
4445 /* if this is a DBCS character, put it in "mb_c" */
4446 mb_l = MB_BYTE2LEN(c);
4447 if (mb_l >= n_extra)
4448 mb_l = 1;
4449 else if (mb_l > 1)
4450 mb_c = (c << 8) + p_extra[1];
4451 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004452 if (mb_l == 0) /* at the NUL at end-of-line */
4453 mb_l = 1;
4454
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 /* If a double-width char doesn't fit display a '>' in the
4456 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004457 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458# ifdef FEAT_RIGHTLEFT
4459 wp->w_p_rl ? (col <= 0) :
4460# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004461 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 && (*mb_char2cells)(mb_c) == 2)
4463 {
4464 c = '>';
4465 mb_c = c;
4466 mb_l = 1;
4467 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004468 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 /* put the pointer back to output the double-width
4470 * character at the start of the next line. */
4471 ++n_extra;
4472 --p_extra;
4473 }
4474 else
4475 {
4476 n_extra -= mb_l - 1;
4477 p_extra += mb_l - 1;
4478 }
4479 }
4480#endif
4481 ++p_extra;
4482 }
4483 --n_extra;
4484 }
4485 else
4486 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004487#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004488 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004489#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004490
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004491 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004492 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 /*
4494 * Get a character from the line itself.
4495 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004496 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004497#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004498 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500#ifdef FEAT_MBYTE
4501 if (has_mbyte)
4502 {
4503 mb_c = c;
4504 if (enc_utf8)
4505 {
4506 /* If the UTF-8 character is more than one byte: Decode it
4507 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004508 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 mb_utf8 = FALSE;
4510 if (mb_l > 1)
4511 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004512 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 /* Overlong encoded ASCII or ASCII with composing char
4514 * is displayed normally, except a NUL. */
4515 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004516 {
4517 c = mb_c;
4518# ifdef FEAT_LINEBREAK
4519 c0 = mb_c;
4520# endif
4521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004523
4524 /* At start of the line we can have a composing char.
4525 * Draw it as a space with a composing char. */
4526 if (utf_iscomposing(mb_c))
4527 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004528 int i;
4529
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004530 for (i = Screen_mco - 1; i > 0; --i)
4531 u8cc[i] = u8cc[i - 1];
4532 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004533 mb_c = ' ';
4534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 }
4536
4537 if ((mb_l == 1 && c >= 0x80)
4538 || (mb_l >= 1 && mb_c == 0)
4539 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004540# ifdef UNICODE16
4541 || mb_c >= 0x10000
4542# endif
4543 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
4545 /*
4546 * Illegal UTF-8 byte: display as <xx>.
4547 * Non-BMP character : display as ? or fullwidth ?.
4548 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004549# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004551# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
4553 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004554# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (wp->w_p_rl) /* reverse */
4556 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004559# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 else if (utf_char2cells(mb_c) != 2)
4561 STRCPY(extra, "?");
4562 else
4563 /* 0xff1f in UTF-8: full-width '?' */
4564 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
4567 p_extra = extra;
4568 c = *p_extra;
4569 mb_c = mb_ptr2char_adv(&p_extra);
4570 mb_utf8 = (c >= 0x80);
4571 n_extra = (int)STRLEN(p_extra);
4572 c_extra = NUL;
4573 if (area_attr == 0 && search_attr == 0)
4574 {
4575 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004576 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 saved_attr2 = char_attr; /* save current attr */
4578 }
4579 }
4580 else if (mb_l == 0) /* at the NUL at end-of-line */
4581 mb_l = 1;
4582#ifdef FEAT_ARABIC
4583 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4584 {
4585 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004586 int pc, pc1, nc;
4587 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
4589 /* The idea of what is the previous and next
4590 * character depends on 'rightleft'. */
4591 if (wp->w_p_rl)
4592 {
4593 pc = prev_c;
4594 pc1 = prev_c1;
4595 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004596 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 else
4599 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004600 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004602 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
4604 prev_c = mb_c;
4605
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004606 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 else
4609 prev_c = mb_c;
4610#endif
4611 }
4612 else /* enc_dbcs */
4613 {
4614 mb_l = MB_BYTE2LEN(c);
4615 if (mb_l == 0) /* at the NUL at end-of-line */
4616 mb_l = 1;
4617 else if (mb_l > 1)
4618 {
4619 /* We assume a second byte below 32 is illegal.
4620 * Hopefully this is OK for all double-byte encodings!
4621 */
4622 if (ptr[1] >= 32)
4623 mb_c = (c << 8) + ptr[1];
4624 else
4625 {
4626 if (ptr[1] == NUL)
4627 {
4628 /* head byte at end of line */
4629 mb_l = 1;
4630 transchar_nonprint(extra, c);
4631 }
4632 else
4633 {
4634 /* illegal tail byte */
4635 mb_l = 2;
4636 STRCPY(extra, "XX");
4637 }
4638 p_extra = extra;
4639 n_extra = (int)STRLEN(extra) - 1;
4640 c_extra = NUL;
4641 c = *p_extra++;
4642 if (area_attr == 0 && search_attr == 0)
4643 {
4644 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004645 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 saved_attr2 = char_attr; /* save current attr */
4647 }
4648 mb_c = c;
4649 }
4650 }
4651 }
4652 /* If a double-width char doesn't fit display a '>' in the
4653 * last column; the character is displayed at the start of the
4654 * next line. */
4655 if ((
4656# ifdef FEAT_RIGHTLEFT
4657 wp->w_p_rl ? (col <= 0) :
4658# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004659 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 && (*mb_char2cells)(mb_c) == 2)
4661 {
4662 c = '>';
4663 mb_c = c;
4664 mb_utf8 = FALSE;
4665 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004666 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 /* Put pointer back so that the character will be
4668 * displayed at the start of the next line. */
4669 --ptr;
4670 }
4671 else if (*ptr != NUL)
4672 ptr += mb_l - 1;
4673
4674 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004675 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004676 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004677 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004680 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 c = ' ';
4682 if (area_attr == 0 && search_attr == 0)
4683 {
4684 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004685 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 saved_attr2 = char_attr; /* save current attr */
4687 }
4688 mb_c = c;
4689 mb_utf8 = FALSE;
4690 mb_l = 1;
4691 }
4692
4693 }
4694#endif
4695 ++ptr;
4696
4697 if (extra_check)
4698 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004699#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004700 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004701#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004702
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004703#ifdef FEAT_TERMINAL
4704 if (get_term_attr)
4705 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004706 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004707
4708 if (!attr_pri)
4709 char_attr = syntax_attr;
4710 else
4711 char_attr = hl_combine_attr(syntax_attr, char_attr);
4712 }
4713#endif
4714
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004715#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004716 // Get syntax attribute, unless still at the start of the line
4717 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004718 v = (long)(ptr - line);
4719 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 /* Get the syntax attribute for the character. If there
4722 * is an error, disable syntax highlighting. */
4723 save_did_emsg = did_emsg;
4724 did_emsg = FALSE;
4725
Bram Moolenaar217ad922005-03-20 22:37:15 +00004726 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004727# ifdef FEAT_SPELL
4728 has_spell ? &can_spell :
4729# endif
4730 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
4732 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004733 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004734 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004735 has_syntax = FALSE;
4736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 else
4738 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004739#ifdef SYN_TIME_LIMIT
4740 if (wp->w_s->b_syn_slow)
4741 has_syntax = FALSE;
4742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
4744 /* Need to get the line again, a multi-line regexp may
4745 * have made it invalid. */
4746 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4747 ptr = line + v;
4748
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004749# ifdef FEAT_TEXT_PROP
4750 // Text properties overrule syntax highlighting.
4751 if (text_prop_attr == 0)
4752#endif
4753 {
4754 if (!attr_pri)
4755 char_attr = syntax_attr;
4756 else
4757 char_attr = hl_combine_attr(syntax_attr, char_attr);
4758 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004759# ifdef FEAT_CONCEAL
4760 /* no concealing past the end of the line, it interferes
4761 * with line highlighting */
4762 if (c == NUL)
4763 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004764 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004765 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004766# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004768#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004769
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004770#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004771 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004772 * Only do this when there is no syntax highlighting, the
4773 * @Spell cluster is not used or the current syntax item
4774 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004775 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004776 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004777 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004778# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004779 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004780 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004781# endif
4782 if (c != 0 && (
4783# ifdef FEAT_SYN_HL
4784 !has_syntax ||
4785# endif
4786 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004787 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004788 char_u *prev_ptr, *p;
4789 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004790 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004791# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004792 if (has_mbyte)
4793 {
4794 prev_ptr = ptr - mb_l;
4795 v -= mb_l - 1;
4796 }
4797 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004798# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004799 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004800
4801 /* Use nextline[] if possible, it has the start of the
4802 * next line concatenated. */
4803 if ((prev_ptr - line) - nextlinecol >= 0)
4804 p = nextline + (prev_ptr - line) - nextlinecol;
4805 else
4806 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004807 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004808 len = spell_check(wp, p, &spell_hlf, &cap_col,
4809 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004810 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004811
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004812 /* In Insert mode only highlight a word that
4813 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004814 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004815 && (State & INSERT) != 0
4816 && wp->w_cursor.lnum == lnum
4817 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004818 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004819 && wp->w_cursor.col < (colnr_T)word_end)
4820 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004821 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004822 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004823 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004824
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004825 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004826 && (p - nextline) + len > nextline_idx)
4827 {
4828 /* Remember that the good word continues at the
4829 * start of the next line. */
4830 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004831 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004832 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004833
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004834 /* Turn index into actual attributes. */
4835 if (spell_hlf != HLF_COUNT)
4836 spell_attr = highlight_attr[spell_hlf];
4837
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004838 if (cap_col > 0)
4839 {
4840 if (p != prev_ptr
4841 && (p - nextline) + cap_col >= nextline_idx)
4842 {
4843 /* Remember that the word in the next line
4844 * must start with a capital. */
4845 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004846 cap_col = (int)((p - nextline) + cap_col
4847 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004848 }
4849 else
4850 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004851 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004852 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004853 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004854 }
4855 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004856 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004857 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004858 char_attr = hl_combine_attr(char_attr, spell_attr);
4859 else
4860 char_attr = hl_combine_attr(spell_attr, char_attr);
4861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#endif
4863#ifdef FEAT_LINEBREAK
4864 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004865 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004867 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004868 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004870# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004871 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004872# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004873 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004875 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004877 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004878
Bram Moolenaar597a4222014-06-25 14:39:50 +02004879 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004880 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004881 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004882 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004883# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004884 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004885 wp->w_buffer->b_p_vts_array) - 1;
4886# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004887 n_extra = (int)wp->w_buffer->b_p_ts
4888 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004889# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004890
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004891# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004892 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004893# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004895# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004896 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004897 {
4898#ifdef FEAT_CONCEAL
4899 if (c == TAB)
4900 /* See "Tab alignment" below. */
4901 FIX_FOR_BOGUSCOLS;
4902#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004903 if (!wp->w_p_list)
4904 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907#endif
4908
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004909 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4910 */
4911 if (wp->w_p_list
4912 && (((c == 160
4913#ifdef FEAT_MBYTE
4914 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4915#endif
4916 ) && lcs_nbsp)
4917 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4918 {
4919 c = (c == ' ') ? lcs_space : lcs_nbsp;
4920 if (area_attr == 0 && search_attr == 0)
4921 {
4922 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004923 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004924 saved_attr2 = char_attr; /* save current attr */
4925 }
4926#ifdef FEAT_MBYTE
4927 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004928 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004929 {
4930 mb_utf8 = TRUE;
4931 u8cc[0] = 0;
4932 c = 0xc0;
4933 }
4934 else
4935 mb_utf8 = FALSE;
4936#endif
4937 }
4938
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4940 {
4941 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004942 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
4944 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004945 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 saved_attr2 = char_attr; /* save current attr */
4947 }
4948#ifdef FEAT_MBYTE
4949 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004950 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
4952 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004953 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004954 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 else
4957 mb_utf8 = FALSE;
4958#endif
4959 }
4960 }
4961
4962 /*
4963 * Handling of non-printable characters.
4964 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004965 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 /*
4968 * when getting a character from the file, we may have to
4969 * turn it into something else on the way to putting it
4970 * into "ScreenLines".
4971 */
4972 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4973 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004974 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004975 long vcol_adjusted = vcol; /* removed showbreak length */
4976#ifdef FEAT_LINEBREAK
4977 /* only adjust the tab_len, when at the first column
4978 * after the showbreak value was drawn */
4979 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4980 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004983#ifdef FEAT_VARTABS
4984 tab_len = tabstop_padding(vcol_adjusted,
4985 wp->w_buffer->b_p_ts,
4986 wp->w_buffer->b_p_vts_array) - 1;
4987#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004988 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004989 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4990#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004991
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004992#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004993 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004994#endif
4995 /* tab amount depends on current column */
4996 n_extra = tab_len;
4997#ifdef FEAT_LINEBREAK
4998 else
4999 {
5000 char_u *p;
5001 int len = n_extra;
5002 int i;
5003 int saved_nextra = n_extra;
5004
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005005#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005006 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005007 /* there are characters to conceal */
5008 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005009 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5010 */
5011 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5012 && n_extra > tab_len)
5013 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005014#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005015
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005016 /* if n_extra > 0, it gives the number of chars, to
5017 * use for a tab, else we need to calculate the width
5018 * for a tab */
5019#ifdef FEAT_MBYTE
5020 len = (tab_len * mb_char2len(lcs_tab2));
5021 if (n_extra > 0)
5022 len += n_extra - tab_len;
5023#endif
5024 c = lcs_tab1;
5025 p = alloc((unsigned)(len + 1));
5026 vim_memset(p, ' ', len);
5027 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005028 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005029 p_extra_free = p;
5030 for (i = 0; i < tab_len; i++)
5031 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005032 if (*p == NUL)
5033 {
5034 tab_len = i;
5035 break;
5036 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005037#ifdef FEAT_MBYTE
5038 mb_char2bytes(lcs_tab2, p);
5039 p += mb_char2len(lcs_tab2);
5040 n_extra += mb_char2len(lcs_tab2)
5041 - (saved_nextra > 0 ? 1 : 0);
5042#else
5043 p[i] = lcs_tab2;
5044#endif
5045 }
5046 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005047#ifdef FEAT_CONCEAL
5048 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5049 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005050 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005051 n_extra -= vcol_off;
5052#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005053 }
5054#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005055#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005056 {
5057 int vc_saved = vcol_off;
5058
5059 /* Tab alignment should be identical regardless of
5060 * 'conceallevel' value. So tab compensates of all
5061 * previous concealed characters, and thus resets
5062 * vcol_off and boguscols accumulated so far in the
5063 * line. Note that the tab can be longer than
5064 * 'tabstop' when there are concealed characters. */
5065 FIX_FOR_BOGUSCOLS;
5066
5067 /* Make sure, the highlighting for the tab char will be
5068 * correctly set further below (effectively reverts the
5069 * FIX_FOR_BOGSUCOLS macro */
5070 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005071 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005072 tab_len += vc_saved;
5073 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075#ifdef FEAT_MBYTE
5076 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5077#endif
5078 if (wp->w_p_list)
5079 {
5080 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005081#ifdef FEAT_LINEBREAK
5082 if (wp->w_p_lbr)
5083 c_extra = NUL; /* using p_extra from above */
5084 else
5085#endif
5086 c_extra = lcs_tab2;
5087 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005088 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 saved_attr2 = char_attr; /* save current attr */
5090#ifdef FEAT_MBYTE
5091 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005092 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005095 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005096 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098#endif
5099 }
5100 else
5101 {
5102 c_extra = ' ';
5103 c = ' ';
5104 }
5105 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005106 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005107 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005108 || ((fromcol >= 0 || fromcol_prev >= 0)
5109 && tocol > vcol
5110 && VIsual_mode != Ctrl_V
5111 && (
5112# ifdef FEAT_RIGHTLEFT
5113 wp->w_p_rl ? (col >= 0) :
5114# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005115 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005116 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005117 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005118 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005119 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005121 /* Display a '$' after the line or highlight an extra
5122 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5124 /* For a diff line the highlighting continues after the
5125 * "$". */
5126 if (
5127# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005128 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129# ifdef LINE_ATTR
5130 &&
5131# endif
5132# endif
5133# ifdef LINE_ATTR
5134 line_attr == 0
5135# endif
5136 )
5137#endif
5138 {
5139#ifdef FEAT_VIRTUALEDIT
5140 /* In virtualedit, visual selections may extend
5141 * beyond end of line. */
5142 if (area_highlighting && virtual_active()
5143 && tocol != MAXCOL && vcol < tocol)
5144 n_extra = 0;
5145 else
5146#endif
5147 {
5148 p_extra = at_end_str;
5149 n_extra = 1;
5150 c_extra = NUL;
5151 }
5152 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005153 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005154 c = lcs_eol;
5155 else
5156 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 lcs_eol_one = -1;
5158 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005159 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005161 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 n_attr = 1;
5163 }
5164#ifdef FEAT_MBYTE
5165 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005166 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
5168 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005169 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005170 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 else
5173 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5174#endif
5175 }
5176 else if (c != NUL)
5177 {
5178 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005179 if (n_extra == 0)
5180 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#ifdef FEAT_RIGHTLEFT
5182 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5183 rl_mirror(p_extra); /* reverse "<12>" */
5184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005186#ifdef FEAT_LINEBREAK
5187 if (wp->w_p_lbr)
5188 {
5189 char_u *p;
5190
5191 c = *p_extra;
5192 p = alloc((unsigned)n_extra + 1);
5193 vim_memset(p, ' ', n_extra);
5194 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5195 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005196 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005197 p_extra_free = p_extra = p;
5198 }
5199 else
5200#endif
5201 {
5202 n_extra = byte2cells(c) - 1;
5203 c = *p_extra++;
5204 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005205 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
5207 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005208 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 saved_attr2 = char_attr; /* save current attr */
5210 }
5211#ifdef FEAT_MBYTE
5212 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5213#endif
5214 }
5215#ifdef FEAT_VIRTUALEDIT
5216 else if (VIsual_active
5217 && (VIsual_mode == Ctrl_V
5218 || VIsual_mode == 'v')
5219 && virtual_active()
5220 && tocol != MAXCOL
5221 && vcol < tocol
5222 && (
5223# ifdef FEAT_RIGHTLEFT
5224 wp->w_p_rl ? (col >= 0) :
5225# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005226 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
5228 c = ' ';
5229 --ptr; /* put it back at the NUL */
5230 }
5231#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005232#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 else if ((
5234# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005235 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005237# ifdef FEAT_TERMINAL
5238 term_attr != 0 ||
5239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 ) && (
5242# ifdef FEAT_RIGHTLEFT
5243 wp->w_p_rl ? (col >= 0) :
5244# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005245 (col
5246# ifdef FEAT_CONCEAL
5247 - boguscols
5248# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005249 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 /* Highlight until the right side of the window */
5252 c = ' ';
5253 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005254
5255 /* Remember we do the char for line highlighting. */
5256 ++did_line_attr;
5257
5258 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005259 if (line_attr != 0 && char_attr == search_attr
5260 && (did_line_attr > 1
5261 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005262 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263# ifdef FEAT_DIFF
5264 if (diff_hlf == HLF_TXD)
5265 {
5266 diff_hlf = HLF_CHD;
5267 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005268 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005269 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005270 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5271 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005272 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005276# ifdef FEAT_TERMINAL
5277 if (term_attr != 0)
5278 {
5279 char_attr = term_attr;
5280 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5281 char_attr = hl_combine_attr(char_attr,
5282 HL_ATTR(HLF_CUL));
5283 }
5284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286#endif
5287 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005288
5289#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005290 if ( wp->w_p_cole > 0
5291 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005292 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005293 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005294 && !(lnum_in_visual_area
5295 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005296 {
5297 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005298 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005299 && (syn_get_sub_char() != NUL || match_conc
5300 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005301 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005302 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005303 /* First time at this concealed item: display one
5304 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005305 if (match_conc)
5306 c = match_conc;
5307 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005308 c = syn_get_sub_char();
5309 else if (lcs_conceal != NUL)
5310 c = lcs_conceal;
5311 else
5312 c = ' ';
5313
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005314 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005315
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005316 if (n_extra > 0)
5317 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005318 vcol += n_extra;
5319 if (wp->w_p_wrap && n_extra > 0)
5320 {
5321# ifdef FEAT_RIGHTLEFT
5322 if (wp->w_p_rl)
5323 {
5324 col -= n_extra;
5325 boguscols -= n_extra;
5326 }
5327 else
5328# endif
5329 {
5330 boguscols += n_extra;
5331 col += n_extra;
5332 }
5333 }
5334 n_extra = 0;
5335 n_attr = 0;
5336 }
5337 else if (n_skip == 0)
5338 {
5339 is_concealing = TRUE;
5340 n_skip = 1;
5341 }
5342# ifdef FEAT_MBYTE
5343 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005344 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005345 {
5346 mb_utf8 = TRUE;
5347 u8cc[0] = 0;
5348 c = 0xc0;
5349 }
5350 else
5351 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5352# endif
5353 }
5354 else
5355 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005356 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005357 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005358 }
5359#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005362#ifdef FEAT_CONCEAL
5363 /* In the cursor line and we may be concealing characters: correct
5364 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005365 if (!did_wcol && draw_state == WL_LINE
5366 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005367 && conceal_cursor_line(wp)
5368 && (int)wp->w_virtcol <= vcol + n_skip)
5369 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005370# ifdef FEAT_RIGHTLEFT
5371 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005372 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005373 else
5374# endif
5375 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005376 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005377 did_wcol = TRUE;
5378 }
5379#endif
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 /* Don't override visual selection highlighting. */
5382 if (n_attr > 0
5383 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005384 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 char_attr = extra_attr;
5386
Bram Moolenaar81695252004-12-29 20:58:21 +00005387#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 /* XIM don't send preedit_start and preedit_end, but they send
5389 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5390 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005391 if (p_imst == IM_ON_THE_SPOT
5392 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005393 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 && (State & INSERT)
5395 && !p_imdisable
5396 && im_is_preediting()
5397 && draw_state == WL_LINE)
5398 {
5399 colnr_T tcol;
5400
5401 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005402 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 else
5404 tcol = preedit_end_col;
5405 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5406 {
5407 if (feedback_old_attr < 0)
5408 {
5409 feedback_col = 0;
5410 feedback_old_attr = char_attr;
5411 }
5412 char_attr = im_get_feedback_attr(feedback_col);
5413 if (char_attr < 0)
5414 char_attr = feedback_old_attr;
5415 feedback_col++;
5416 }
5417 else if (feedback_old_attr >= 0)
5418 {
5419 char_attr = feedback_old_attr;
5420 feedback_old_attr = -1;
5421 feedback_col = 0;
5422 }
5423 }
5424#endif
5425 /*
5426 * Handle the case where we are in column 0 but not on the first
5427 * character of the line and the user wants us to show us a
5428 * special character (via 'listchars' option "precedes:<char>".
5429 */
5430 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005431 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5433#ifdef FEAT_DIFF
5434 && filler_todo <= 0
5435#endif
5436 && draw_state > WL_NR
5437 && c != NUL)
5438 {
5439 c = lcs_prec;
5440 lcs_prec_todo = NUL;
5441#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005442 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5443 {
5444 /* Double-width character being overwritten by the "precedes"
5445 * character, need to fill up half the character. */
5446 c_extra = MB_FILLER_CHAR;
5447 n_extra = 1;
5448 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005449 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005452 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
5454 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005455 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005456 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458 else
5459 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5460#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005461 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005464 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 n_attr3 = 1;
5466 }
5467 }
5468
5469 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005470 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005472 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005473#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005474 || did_line_attr == 1
5475#endif
5476 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005478#ifdef FEAT_SEARCH_EXTRA
5479 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005480
5481 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005482 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005483 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005484#endif
5485
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005486 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 * highlight match at end of line. If it's beyond the last
5488 * char on the screen, just overwrite that one (tricky!) Not
5489 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005490#ifdef FEAT_SEARCH_EXTRA
5491 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005492 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005493 prevcol_hl_flag = TRUE;
5494 else
5495 {
5496 cur = wp->w_match_head;
5497 while (cur != NULL)
5498 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005499 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005500 {
5501 prevcol_hl_flag = TRUE;
5502 break;
5503 }
5504 cur = cur->next;
5505 }
5506 }
5507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005509 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005510 && (VIsual_mode != Ctrl_V
5511 || lnum == VIsual.lnum
5512 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005513 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514#ifdef FEAT_SEARCH_EXTRA
5515 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005516 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005517# ifdef FEAT_SYN_HL
5518 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5519 && !(wp == curwin && VIsual_active))
5520# endif
5521# ifdef FEAT_DIFF
5522 && diff_hlf == (hlf_T)0
5523# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005524# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005525 && did_line_attr <= 1
5526# endif
5527 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#endif
5529 ))
5530 {
5531 int n = 0;
5532
5533#ifdef FEAT_RIGHTLEFT
5534 if (wp->w_p_rl)
5535 {
5536 if (col < 0)
5537 n = 1;
5538 }
5539 else
5540#endif
5541 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005542 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 n = -1;
5544 }
5545 if (n != 0)
5546 {
5547 /* At the window boundary, highlight the last character
5548 * instead (better than nothing). */
5549 off += n;
5550 col += n;
5551 }
5552 else
5553 {
5554 /* Add a blank character to highlight. */
5555 ScreenLines[off] = ' ';
5556#ifdef FEAT_MBYTE
5557 if (enc_utf8)
5558 ScreenLinesUC[off] = 0;
5559#endif
5560 }
5561#ifdef FEAT_SEARCH_EXTRA
5562 if (area_attr == 0)
5563 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005564 /* Use attributes from match with highest priority among
5565 * 'search_hl' and the match list. */
5566 char_attr = search_hl.attr;
5567 cur = wp->w_match_head;
5568 shl_flag = FALSE;
5569 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005570 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005571 if (shl_flag == FALSE
5572 && ((cur != NULL
5573 && cur->priority > SEARCH_HL_PRIORITY)
5574 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005575 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005576 shl = &search_hl;
5577 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005578 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005579 else
5580 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005581 if ((ptr - line) - 1 == (long)shl->startcol
5582 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005583 char_attr = shl->attr;
5584 if (shl != &search_hl && cur != NULL)
5585 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588#endif
5589 ScreenAttrs[off] = char_attr;
5590#ifdef FEAT_RIGHTLEFT
5591 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005594 --off;
5595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 else
5597#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005600 ++off;
5601 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005602 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005603#ifdef FEAT_SYN_HL
5604 eol_hl_off = 1;
5605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
Bram Moolenaar91170f82006-05-05 21:15:17 +00005609 /*
5610 * At end of the text line.
5611 */
5612 if (c == NUL)
5613 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005614#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005615 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005616 if (wp->w_p_wrap)
5617 v = wp->w_skipcol;
5618 else
5619 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005620
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005621 /* check if line ends before left margin */
5622 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005623 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005624#ifdef FEAT_CONCEAL
5625 /* Get rid of the boguscols now, we want to draw until the right
5626 * edge for 'cursorcolumn'. */
5627 col -= boguscols;
5628 boguscols = 0;
5629#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005630
5631 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005632 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005633
5634 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005635 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5636 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005637 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005638 && lnum != wp->w_cursor.lnum)
5639 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005640# ifdef FEAT_RIGHTLEFT
5641 && !wp->w_p_rl
5642# endif
5643 )
5644 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005645 int rightmost_vcol = 0;
5646 int i;
5647
5648 if (wp->w_p_cuc)
5649 rightmost_vcol = wp->w_virtcol;
5650 if (draw_color_col)
5651 /* determine rightmost colorcolumn to possibly draw */
5652 for (i = 0; color_cols[i] >= 0; ++i)
5653 if (rightmost_vcol < color_cols[i])
5654 rightmost_vcol = color_cols[i];
5655
Bram Moolenaar02631462017-09-22 15:20:32 +02005656 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005657 {
5658 ScreenLines[off] = ' ';
5659#ifdef FEAT_MBYTE
5660 if (enc_utf8)
5661 ScreenLinesUC[off] = 0;
5662#endif
5663 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005664 if (draw_color_col)
5665 draw_color_col = advance_color_col(VCOL_HLC,
5666 &color_cols);
5667
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005668 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005669 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005670 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005671 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005672 else
5673 ScreenAttrs[off++] = 0;
5674
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005675 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005676 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005677
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005678 ++vcol;
5679 }
5680 }
5681#endif
5682
Bram Moolenaar53f81742017-09-22 14:35:51 +02005683 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005684 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 row++;
5686
5687 /*
5688 * Update w_cline_height and w_cline_folded if the cursor line was
5689 * updated (saves a call to plines() later).
5690 */
5691 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5692 {
5693 curwin->w_cline_row = startrow;
5694 curwin->w_cline_height = row - startrow;
5695#ifdef FEAT_FOLDING
5696 curwin->w_cline_folded = FALSE;
5697#endif
5698 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5699 }
5700
5701 break;
5702 }
5703
5704 /* line continues beyond line end */
5705 if (lcs_ext
5706 && !wp->w_p_wrap
5707#ifdef FEAT_DIFF
5708 && filler_todo <= 0
5709#endif
5710 && (
5711#ifdef FEAT_RIGHTLEFT
5712 wp->w_p_rl ? col == 0 :
5713#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005714 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005716 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5718 {
5719 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005720 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721#ifdef FEAT_MBYTE
5722 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005723 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 {
5725 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005726 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005727 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729 else
5730 mb_utf8 = FALSE;
5731#endif
5732 }
5733
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005734#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005735 /* advance to the next 'colorcolumn' */
5736 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005737 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005738
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005739 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005740 * highlight the cursor position itself.
5741 * Also highlight the 'colorcolumn' if it is different than
5742 * 'cursorcolumn' */
5743 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005744 if (draw_state == WL_LINE && !lnum_in_visual_area
5745 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005746 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005747 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005748 && lnum != wp->w_cursor.lnum)
5749 {
5750 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005751 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005752 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005753 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005754 {
5755 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005756 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005757 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005758 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005759#endif
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 /*
5762 * Store character to be displayed.
5763 * Skip characters that are left of the screen for 'nowrap'.
5764 */
5765 vcol_prev = vcol;
5766 if (draw_state < WL_LINE || n_skip <= 0)
5767 {
5768 /*
5769 * Store the character.
5770 */
5771#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5772 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5773 {
5774 /* A double-wide character is: put first halve in left cell. */
5775 --off;
5776 --col;
5777 }
5778#endif
5779 ScreenLines[off] = c;
5780#ifdef FEAT_MBYTE
5781 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005782 {
5783 if ((mb_c & 0xff00) == 0x8e00)
5784 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 else if (enc_utf8)
5788 {
5789 if (mb_utf8)
5790 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005791 int i;
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005794 if ((c & 0xff) == 0)
5795 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005796 for (i = 0; i < Screen_mco; ++i)
5797 {
5798 ScreenLinesC[i][off] = u8cc[i];
5799 if (u8cc[i] == 0)
5800 break;
5801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
5803 else
5804 ScreenLinesUC[off] = 0;
5805 }
5806 if (multi_attr)
5807 {
5808 ScreenAttrs[off] = multi_attr;
5809 multi_attr = 0;
5810 }
5811 else
5812#endif
5813 ScreenAttrs[off] = char_attr;
5814
5815#ifdef FEAT_MBYTE
5816 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5817 {
5818 /* Need to fill two screen columns. */
5819 ++off;
5820 ++col;
5821 if (enc_utf8)
5822 /* UTF-8: Put a 0 in the second screen char. */
5823 ScreenLines[off] = 0;
5824 else
5825 /* DBCS: Put second byte in the second screen char. */
5826 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005827 if (draw_state > WL_NR
5828#ifdef FEAT_DIFF
5829 && filler_todo <= 0
5830#endif
5831 )
5832 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 /* When "tocol" is halfway a character, set it to the end of
5834 * the character, otherwise highlighting won't stop. */
5835 if (tocol == vcol)
5836 ++tocol;
5837#ifdef FEAT_RIGHTLEFT
5838 if (wp->w_p_rl)
5839 {
5840 /* now it's time to backup one cell */
5841 --off;
5842 --col;
5843 }
5844#endif
5845 }
5846#endif
5847#ifdef FEAT_RIGHTLEFT
5848 if (wp->w_p_rl)
5849 {
5850 --off;
5851 --col;
5852 }
5853 else
5854#endif
5855 {
5856 ++off;
5857 ++col;
5858 }
5859 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005860#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005861 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005862 {
5863 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005864 ++vcol_off;
5865 if (n_extra > 0)
5866 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005867 if (wp->w_p_wrap)
5868 {
5869 /*
5870 * Special voodoo required if 'wrap' is on.
5871 *
5872 * Advance the column indicator to force the line
5873 * drawing to wrap early. This will make the line
5874 * take up the same screen space when parts are concealed,
5875 * so that cursor line computations aren't messed up.
5876 *
5877 * To avoid the fictitious advance of 'col' causing
5878 * trailing junk to be written out of the screen line
5879 * we are building, 'boguscols' keeps track of the number
5880 * of bad columns we have advanced.
5881 */
5882 if (n_extra > 0)
5883 {
5884 vcol += n_extra;
5885# ifdef FEAT_RIGHTLEFT
5886 if (wp->w_p_rl)
5887 {
5888 col -= n_extra;
5889 boguscols -= n_extra;
5890 }
5891 else
5892# endif
5893 {
5894 col += n_extra;
5895 boguscols += n_extra;
5896 }
5897 n_extra = 0;
5898 n_attr = 0;
5899 }
5900
5901
5902# ifdef FEAT_MBYTE
5903 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5904 {
5905 /* Need to fill two screen columns. */
5906# ifdef FEAT_RIGHTLEFT
5907 if (wp->w_p_rl)
5908 {
5909 --boguscols;
5910 --col;
5911 }
5912 else
5913# endif
5914 {
5915 ++boguscols;
5916 ++col;
5917 }
5918 }
5919# endif
5920
5921# ifdef FEAT_RIGHTLEFT
5922 if (wp->w_p_rl)
5923 {
5924 --boguscols;
5925 --col;
5926 }
5927 else
5928# endif
5929 {
5930 ++boguscols;
5931 ++col;
5932 }
5933 }
5934 else
5935 {
5936 if (n_extra > 0)
5937 {
5938 vcol += n_extra;
5939 n_extra = 0;
5940 n_attr = 0;
5941 }
5942 }
5943
5944 }
5945#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 else
5947 --n_skip;
5948
Bram Moolenaar64486672010-05-16 15:46:46 +02005949 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5950 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005951 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952#ifdef FEAT_DIFF
5953 && filler_todo <= 0
5954#endif
5955 )
5956 ++vcol;
5957
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005958#ifdef FEAT_SYN_HL
5959 if (vcol_save_attr >= 0)
5960 char_attr = vcol_save_attr;
5961#endif
5962
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 /* restore attributes after "predeces" in 'listchars' */
5964 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5965 char_attr = saved_attr3;
5966
5967 /* restore attributes after last 'listchars' or 'number' char */
5968 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5969 char_attr = saved_attr2;
5970
5971 /*
5972 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005973 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 */
5975 if ((
5976#ifdef FEAT_RIGHTLEFT
5977 wp->w_p_rl ? (col < 0) :
5978#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005979 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 && (*ptr != NUL
5981#ifdef FEAT_DIFF
5982 || filler_todo > 0
5983#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005984 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5986 )
5987 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005988#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005989 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005990 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005991 boguscols = 0;
5992#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005993 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005994 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 ++row;
5997 ++screen_row;
5998
5999 /* When not wrapping and finished diff lines, or when displayed
6000 * '$' and highlighting until last column, break here. */
6001 if ((!wp->w_p_wrap
6002#ifdef FEAT_DIFF
6003 && filler_todo <= 0
6004#endif
6005 ) || lcs_eol_one == -1)
6006 break;
6007
6008 /* When the window is too narrow draw all "@" lines. */
6009 if (draw_state != WL_LINE
6010#ifdef FEAT_DIFF
6011 && filler_todo <= 0
6012#endif
6013 )
6014 {
6015 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 row = endrow;
6018 }
6019
6020 /* When line got too long for screen break here. */
6021 if (row == endrow)
6022 {
6023 ++row;
6024 break;
6025 }
6026
6027 if (screen_cur_row == screen_row - 1
6028#ifdef FEAT_DIFF
6029 && filler_todo <= 0
6030#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006031 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 {
6033 /* Remember that the line wraps, used for modeless copy. */
6034 LineWraps[screen_row - 1] = TRUE;
6035
6036 /*
6037 * Special trick to make copy/paste of wrapped lines work with
6038 * xterm/screen: write an extra character beyond the end of
6039 * the line. This will work with all terminal types
6040 * (regardless of the xn,am settings).
6041 * Only do this on a fast tty.
6042 * Only do this if the cursor is on the current line
6043 * (something has been written in it).
6044 * Don't do this for the GUI.
6045 * Don't do this for double-width characters.
6046 * Don't do this for a window not at the right screen border.
6047 */
6048 if (p_tf
6049#ifdef FEAT_GUI
6050 && !gui.in_use
6051#endif
6052#ifdef FEAT_MBYTE
6053 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006054 && ((*mb_off2cells)(LineOffset[screen_row],
6055 LineOffset[screen_row] + screen_Columns)
6056 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006058 + (int)Columns - 2,
6059 LineOffset[screen_row] + screen_Columns)
6060 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061#endif
6062 )
6063 {
6064 /* First make sure we are at the end of the screen line,
6065 * then output the same character again to let the
6066 * terminal know about the wrap. If the terminal doesn't
6067 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006068 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 screen_char(LineOffset[screen_row - 1]
6070 + (unsigned)Columns - 1,
6071 screen_row - 1, (int)(Columns - 1));
6072
6073#ifdef FEAT_MBYTE
6074 /* When there is a multi-byte character, just output a
6075 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006076 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6077 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 out_char(' ');
6079 else
6080#endif
6081 out_char(ScreenLines[LineOffset[screen_row - 1]
6082 + (Columns - 1)]);
6083 /* force a redraw of the first char on the next line */
6084 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6085 screen_start(); /* don't know where cursor is now */
6086 }
6087 }
6088
6089 col = 0;
6090 off = (unsigned)(current_ScreenLine - ScreenLines);
6091#ifdef FEAT_RIGHTLEFT
6092 if (wp->w_p_rl)
6093 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006094 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 off += col;
6096 }
6097#endif
6098
6099 /* reset the drawing state for the start of a wrapped line */
6100 draw_state = WL_START;
6101 saved_n_extra = n_extra;
6102 saved_p_extra = p_extra;
6103 saved_c_extra = c_extra;
6104 saved_char_attr = char_attr;
6105 n_extra = 0;
6106 lcs_prec_todo = lcs_prec;
6107#ifdef FEAT_LINEBREAK
6108# ifdef FEAT_DIFF
6109 if (filler_todo <= 0)
6110# endif
6111 need_showbreak = TRUE;
6112#endif
6113#ifdef FEAT_DIFF
6114 --filler_todo;
6115 /* When the filler lines are actually below the last line of the
6116 * file, don't draw the line itself, break here. */
6117 if (filler_todo == 0 && wp->w_botfill)
6118 break;
6119#endif
6120 }
6121
6122 } /* for every character in the line */
6123
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006124#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006125 /* After an empty line check first word for capital. */
6126 if (*skipwhite(line) == NUL)
6127 {
6128 capcol_lnum = lnum + 1;
6129 cap_col = 0;
6130 }
6131#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006132#ifdef FEAT_TEXT_PROP
6133 vim_free(text_props);
6134 vim_free(text_prop_idxs);
6135#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006136
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006137 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 return row;
6139}
6140
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006141#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006142/*
6143 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006144 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006145 */
6146 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006147comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006148{
6149 int i;
6150
6151 for (i = 0; i < Screen_mco; ++i)
6152 {
6153 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6154 return TRUE;
6155 if (ScreenLinesC[i][off_from] == 0)
6156 break;
6157 }
6158 return FALSE;
6159}
6160#endif
6161
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162/*
6163 * Check whether the given character needs redrawing:
6164 * - the (first byte of the) character is different
6165 * - the attributes are different
6166 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006167 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 */
6169 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006170char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 if (cols > 0
6173 && ((ScreenLines[off_from] != ScreenLines[off_to]
6174 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6175
6176#ifdef FEAT_MBYTE
6177 || (enc_dbcs != 0
6178 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6179 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6180 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6181 : (cols > 1 && ScreenLines[off_from + 1]
6182 != ScreenLines[off_to + 1])))
6183 || (enc_utf8
6184 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6185 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006186 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006187 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6188 && ScreenLines[off_from + 1]
6189 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190#endif
6191 ))
6192 return TRUE;
6193 return FALSE;
6194}
6195
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006196#if defined(FEAT_TERMINAL) || defined(PROTO)
6197/*
6198 * Return the index in ScreenLines[] for the current screen line.
6199 */
6200 int
6201screen_get_current_line_off()
6202{
6203 return (int)(current_ScreenLine - ScreenLines);
6204}
6205#endif
6206
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207/*
6208 * Move one "cooked" screen line to the screen, but only the characters that
6209 * have actually changed. Handle insert/delete character.
6210 * "coloff" gives the first column on the screen for this line.
6211 * "endcol" gives the columns where valid characters are.
6212 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6213 * needs to be cleared, negative otherwise.
6214 * "rlflag" is TRUE in a rightleft window:
6215 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6216 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6217 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006218 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006219screen_line(
6220 int row,
6221 int coloff,
6222 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006223 int clear_width,
6224 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
6226 unsigned off_from;
6227 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006228#ifdef FEAT_MBYTE
6229 unsigned max_off_from;
6230 unsigned max_off_to;
6231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 int force = FALSE; /* force update rest of the line */
6235 int redraw_this /* bool: does character need redraw? */
6236#ifdef FEAT_GUI
6237 = TRUE /* For GUI when while-loop empty */
6238#endif
6239 ;
6240 int redraw_next; /* redraw_this for next character */
6241#ifdef FEAT_MBYTE
6242 int clear_next = FALSE;
6243 int char_cells; /* 1: normal char */
6244 /* 2: occupies two display cells */
6245# define CHAR_CELLS char_cells
6246#else
6247# define CHAR_CELLS 1
6248#endif
6249
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006250 /* Check for illegal row and col, just in case. */
6251 if (row >= Rows)
6252 row = Rows - 1;
6253 if (endcol > Columns)
6254 endcol = Columns;
6255
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256# ifdef FEAT_CLIPBOARD
6257 clip_may_clear_selection(row, row);
6258# endif
6259
6260 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6261 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006262#ifdef FEAT_MBYTE
6263 max_off_from = off_from + screen_Columns;
6264 max_off_to = LineOffset[row] + screen_Columns;
6265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267#ifdef FEAT_RIGHTLEFT
6268 if (rlflag)
6269 {
6270 /* Clear rest first, because it's left of the text. */
6271 if (clear_width > 0)
6272 {
6273 while (col <= endcol && ScreenLines[off_to] == ' '
6274 && ScreenAttrs[off_to] == 0
6275# ifdef FEAT_MBYTE
6276 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6277# endif
6278 )
6279 {
6280 ++off_to;
6281 ++col;
6282 }
6283 if (col <= endcol)
6284 screen_fill(row, row + 1, col + coloff,
6285 endcol + coloff + 1, ' ', ' ', 0);
6286 }
6287 col = endcol + 1;
6288 off_to = LineOffset[row] + col + coloff;
6289 off_from += col;
6290 endcol = (clear_width > 0 ? clear_width : -clear_width);
6291 }
6292#endif /* FEAT_RIGHTLEFT */
6293
6294 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6295
6296 while (col < endcol)
6297 {
6298#ifdef FEAT_MBYTE
6299 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006300 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 else
6302 char_cells = 1;
6303#endif
6304
6305 redraw_this = redraw_next;
6306 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6307 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6308
6309#ifdef FEAT_GUI
6310 /* If the next character was bold, then redraw the current character to
6311 * remove any pixels that might have spilt over into us. This only
6312 * happens in the GUI.
6313 */
6314 if (redraw_next && gui.in_use)
6315 {
6316 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006317 if (hl > HL_ALL)
6318 hl = syn_attr2attr(hl);
6319 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 redraw_this = TRUE;
6321 }
6322#endif
6323
6324 if (redraw_this)
6325 {
6326 /*
6327 * Special handling when 'xs' termcap flag set (hpterm):
6328 * Attributes for characters are stored at the position where the
6329 * cursor is when writing the highlighting code. The
6330 * start-highlighting code must be written with the cursor on the
6331 * first highlighted character. The stop-highlighting code must
6332 * be written with the cursor just after the last highlighted
6333 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006334 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 * to clear the rest of the line, and force redrawing it
6336 * completely.
6337 */
6338 if ( p_wiv
6339 && !force
6340#ifdef FEAT_GUI
6341 && !gui.in_use
6342#endif
6343 && ScreenAttrs[off_to] != 0
6344 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6345 {
6346 /*
6347 * Need to remove highlighting attributes here.
6348 */
6349 windgoto(row, col + coloff);
6350 out_str(T_CE); /* clear rest of this screen line */
6351 screen_start(); /* don't know where cursor is now */
6352 force = TRUE; /* force redraw of rest of the line */
6353 redraw_next = TRUE; /* or else next char would miss out */
6354
6355 /*
6356 * If the previous character was highlighted, need to stop
6357 * highlighting at this character.
6358 */
6359 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6360 {
6361 screen_attr = ScreenAttrs[off_to - 1];
6362 term_windgoto(row, col + coloff);
6363 screen_stop_highlight();
6364 }
6365 else
6366 screen_attr = 0; /* highlighting has stopped */
6367 }
6368#ifdef FEAT_MBYTE
6369 if (enc_dbcs != 0)
6370 {
6371 /* Check if overwriting a double-byte with a single-byte or
6372 * the other way around requires another character to be
6373 * redrawn. For UTF-8 this isn't needed, because comparing
6374 * ScreenLinesUC[] is sufficient. */
6375 if (char_cells == 1
6376 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006377 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 {
6379 /* Writing a single-cell character over a double-cell
6380 * character: need to redraw the next cell. */
6381 ScreenLines[off_to + 1] = 0;
6382 redraw_next = TRUE;
6383 }
6384 else if (char_cells == 2
6385 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006386 && (*mb_off2cells)(off_to, max_off_to) == 1
6387 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 {
6389 /* Writing the second half of a double-cell character over
6390 * a double-cell character: need to redraw the second
6391 * cell. */
6392 ScreenLines[off_to + 2] = 0;
6393 redraw_next = TRUE;
6394 }
6395
6396 if (enc_dbcs == DBCS_JPNU)
6397 ScreenLines2[off_to] = ScreenLines2[off_from];
6398 }
6399 /* When writing a single-width character over a double-width
6400 * character and at the end of the redrawn text, need to clear out
6401 * the right halve of the old character.
6402 * Also required when writing the right halve of a double-width
6403 * char over the left halve of an existing one. */
6404 if (has_mbyte && col + char_cells == endcol
6405 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006406 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006408 && (*mb_off2cells)(off_to, max_off_to) == 1
6409 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 clear_next = TRUE;
6411#endif
6412
6413 ScreenLines[off_to] = ScreenLines[off_from];
6414#ifdef FEAT_MBYTE
6415 if (enc_utf8)
6416 {
6417 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6418 if (ScreenLinesUC[off_from] != 0)
6419 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006420 int i;
6421
6422 for (i = 0; i < Screen_mco; ++i)
6423 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425 }
6426 if (char_cells == 2)
6427 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6428#endif
6429
6430#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006431 /* The bold trick makes a single column of pixels appear in the
6432 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 * character should be redrawn too. This happens for our own GUI
6434 * and for some xterms. */
6435 if (
6436# ifdef FEAT_GUI
6437 gui.in_use
6438# endif
6439# if defined(FEAT_GUI) && defined(UNIX)
6440 ||
6441# endif
6442# ifdef UNIX
6443 term_is_xterm
6444# endif
6445 )
6446 {
6447 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006448 if (hl > HL_ALL)
6449 hl = syn_attr2attr(hl);
6450 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 redraw_next = TRUE;
6452 }
6453#endif
6454 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6455#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006456 /* For simplicity set the attributes of second half of a
6457 * double-wide character equal to the first half. */
6458 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006460
6461 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 else
6464#endif
6465 screen_char(off_to, row, col + coloff);
6466 }
6467 else if ( p_wiv
6468#ifdef FEAT_GUI
6469 && !gui.in_use
6470#endif
6471 && col + coloff > 0)
6472 {
6473 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6474 {
6475 /*
6476 * Don't output stop-highlight when moving the cursor, it will
6477 * stop the highlighting when it should continue.
6478 */
6479 screen_attr = 0;
6480 }
6481 else if (screen_attr != 0)
6482 screen_stop_highlight();
6483 }
6484
6485 off_to += CHAR_CELLS;
6486 off_from += CHAR_CELLS;
6487 col += CHAR_CELLS;
6488 }
6489
6490#ifdef FEAT_MBYTE
6491 if (clear_next)
6492 {
6493 /* Clear the second half of a double-wide character of which the left
6494 * half was overwritten with a single-wide character. */
6495 ScreenLines[off_to] = ' ';
6496 if (enc_utf8)
6497 ScreenLinesUC[off_to] = 0;
6498 screen_char(off_to, row, col + coloff);
6499 }
6500#endif
6501
6502 if (clear_width > 0
6503#ifdef FEAT_RIGHTLEFT
6504 && !rlflag
6505#endif
6506 )
6507 {
6508#ifdef FEAT_GUI
6509 int startCol = col;
6510#endif
6511
6512 /* blank out the rest of the line */
6513 while (col < clear_width && ScreenLines[off_to] == ' '
6514 && ScreenAttrs[off_to] == 0
6515#ifdef FEAT_MBYTE
6516 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6517#endif
6518 )
6519 {
6520 ++off_to;
6521 ++col;
6522 }
6523 if (col < clear_width)
6524 {
6525#ifdef FEAT_GUI
6526 /*
6527 * In the GUI, clearing the rest of the line may leave pixels
6528 * behind if the first character cleared was bold. Some bold
6529 * fonts spill over the left. In this case we redraw the previous
6530 * character too. If we didn't skip any blanks above, then we
6531 * only redraw if the character wasn't already redrawn anyway.
6532 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006533 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
6535 hl = ScreenAttrs[off_to];
6536 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006537 {
6538 int prev_cells = 1;
6539# ifdef FEAT_MBYTE
6540 if (enc_utf8)
6541 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6542 * that its width is 2. */
6543 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6544 else if (enc_dbcs != 0)
6545 {
6546 /* find previous character by counting from first
6547 * column and get its width. */
6548 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006549 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006550
6551 while (off < off_to)
6552 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006553 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006554 off += prev_cells;
6555 }
6556 }
6557
6558 if (enc_dbcs != 0 && prev_cells > 1)
6559 screen_char_2(off_to - prev_cells, row,
6560 col + coloff - prev_cells);
6561 else
6562# endif
6563 screen_char(off_to - prev_cells, row,
6564 col + coloff - prev_cells);
6565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 }
6567#endif
6568 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6569 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 off_to += clear_width - col;
6571 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
6573 }
6574
6575 if (clear_width > 0)
6576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 /* For a window that's left of another, draw the separator char. */
6578 if (col + coloff < Columns)
6579 {
6580 int c;
6581
6582 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006583 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006584#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006585 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6586 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 || ScreenAttrs[off_to] != hl)
6589 {
6590 ScreenLines[off_to] = c;
6591 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006592#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 if (enc_utf8)
6594 {
6595 if (c >= 0x80)
6596 {
6597 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006598 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600 else
6601 ScreenLinesUC[off_to] = 0;
6602 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 screen_char(off_to, row, col + coloff);
6605 }
6606 }
6607 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 LineWraps[row] = FALSE;
6609 }
6610}
6611
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006612#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006614 * Mirror text "str" for right-left displaying.
6615 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006617 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006618rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 char_u *p1, *p2;
6621 int t;
6622
6623 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6624 {
6625 t = *p1;
6626 *p1 = *p2;
6627 *p2 = t;
6628 }
6629}
6630#endif
6631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632/*
6633 * mark all status lines for redraw; used after first :cd
6634 */
6635 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006636status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 win_T *wp;
6639
Bram Moolenaar29323592016-07-24 22:04:11 +02006640 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (wp->w_status_height)
6642 {
6643 wp->w_redr_status = TRUE;
6644 redraw_later(VALID);
6645 }
6646}
6647
6648/*
6649 * mark all status lines of the current buffer for redraw
6650 */
6651 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006652status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
6654 win_T *wp;
6655
Bram Moolenaar29323592016-07-24 22:04:11 +02006656 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6658 {
6659 wp->w_redr_status = TRUE;
6660 redraw_later(VALID);
6661 }
6662}
6663
6664/*
6665 * Redraw all status lines that need to be redrawn.
6666 */
6667 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006668redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 win_T *wp;
6671
Bram Moolenaar29323592016-07-24 22:04:11 +02006672 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006674 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006675 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006676 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
Bram Moolenaar4033c552017-09-16 20:54:51 +02006679#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680/*
6681 * Redraw all status lines at the bottom of frame "frp".
6682 */
6683 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006684win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 if (frp->fr_layout == FR_LEAF)
6687 frp->fr_win->w_redr_status = TRUE;
6688 else if (frp->fr_layout == FR_ROW)
6689 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006690 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 win_redraw_last_status(frp);
6692 }
6693 else /* frp->fr_layout == FR_COL */
6694 {
6695 frp = frp->fr_child;
6696 while (frp->fr_next != NULL)
6697 frp = frp->fr_next;
6698 win_redraw_last_status(frp);
6699 }
6700}
6701#endif
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703/*
6704 * Draw the verticap separator right of window "wp" starting with line "row".
6705 */
6706 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006707draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
6709 int hl;
6710 int c;
6711
6712 if (wp->w_vsep_width)
6713 {
6714 /* draw the vertical separator right of this window */
6715 c = fillchar_vsep(&hl);
6716 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6717 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6718 c, ' ', hl);
6719 }
6720}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006723static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
6725/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006726 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 */
6728 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006729status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 int len = 0;
6732
6733#ifdef FEAT_MENU
6734 int emenu = (xp->xp_context == EXPAND_MENUS
6735 || xp->xp_context == EXPAND_MENUNAMES);
6736
6737 /* Check for menu separators - replace with '|'. */
6738 if (emenu && menu_is_separator(s))
6739 return 1;
6740#endif
6741
6742 while (*s != NUL)
6743 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006744 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006745 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006746 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
6748
6749 return len;
6750}
6751
6752/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006753 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006754 * These are backslashes used for escaping. Do show backslashes in help tags.
6755 */
6756 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006757skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006758{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006759 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006760#ifdef FEAT_MENU
6761 || ((xp->xp_context == EXPAND_MENUS
6762 || xp->xp_context == EXPAND_MENUNAMES)
6763 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6764#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006765 )
6766 {
6767#ifndef BACKSLASH_IN_FILENAME
6768 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6769 return 2;
6770#endif
6771 return 1;
6772 }
6773 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006774}
6775
6776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 * Show wildchar matches in the status line.
6778 * Show at least the "match" item.
6779 * We start at item 'first_match' in the list and show all matches that fit.
6780 *
6781 * If inversion is possible we use it. Else '=' characters are used.
6782 */
6783 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006784win_redr_status_matches(
6785 expand_T *xp,
6786 int num_matches,
6787 char_u **matches, /* list of matches */
6788 int match,
6789 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6792 int row;
6793 char_u *buf;
6794 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006795 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 int fillchar;
6797 int attr;
6798 int i;
6799 int highlight = TRUE;
6800 char_u *selstart = NULL;
6801 int selstart_col = 0;
6802 char_u *selend = NULL;
6803 static int first_match = 0;
6804 int add_left = FALSE;
6805 char_u *s;
6806#ifdef FEAT_MENU
6807 int emenu;
6808#endif
6809#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6810 int l;
6811#endif
6812
6813 if (matches == NULL) /* interrupted completion? */
6814 return;
6815
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006816#ifdef FEAT_MBYTE
6817 if (has_mbyte)
6818 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6819 else
6820#endif
6821 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (buf == NULL)
6823 return;
6824
6825 if (match == -1) /* don't show match but original text */
6826 {
6827 match = 0;
6828 highlight = FALSE;
6829 }
6830 /* count 1 for the ending ">" */
6831 clen = status_match_len(xp, L_MATCH(match)) + 3;
6832 if (match == 0)
6833 first_match = 0;
6834 else if (match < first_match)
6835 {
6836 /* jumping left, as far as we can go */
6837 first_match = match;
6838 add_left = TRUE;
6839 }
6840 else
6841 {
6842 /* check if match fits on the screen */
6843 for (i = first_match; i < match; ++i)
6844 clen += status_match_len(xp, L_MATCH(i)) + 2;
6845 if (first_match > 0)
6846 clen += 2;
6847 /* jumping right, put match at the left */
6848 if ((long)clen > Columns)
6849 {
6850 first_match = match;
6851 /* if showing the last match, we can add some on the left */
6852 clen = 2;
6853 for (i = match; i < num_matches; ++i)
6854 {
6855 clen += status_match_len(xp, L_MATCH(i)) + 2;
6856 if ((long)clen >= Columns)
6857 break;
6858 }
6859 if (i == num_matches)
6860 add_left = TRUE;
6861 }
6862 }
6863 if (add_left)
6864 while (first_match > 0)
6865 {
6866 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6867 if ((long)clen >= Columns)
6868 break;
6869 --first_match;
6870 }
6871
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006872 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
6874 if (first_match == 0)
6875 {
6876 *buf = NUL;
6877 len = 0;
6878 }
6879 else
6880 {
6881 STRCPY(buf, "< ");
6882 len = 2;
6883 }
6884 clen = len;
6885
6886 i = first_match;
6887 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6888 {
6889 if (i == match)
6890 {
6891 selstart = buf + len;
6892 selstart_col = clen;
6893 }
6894
6895 s = L_MATCH(i);
6896 /* Check for menu separators - replace with '|' */
6897#ifdef FEAT_MENU
6898 emenu = (xp->xp_context == EXPAND_MENUS
6899 || xp->xp_context == EXPAND_MENUNAMES);
6900 if (emenu && menu_is_separator(s))
6901 {
6902 STRCPY(buf + len, transchar('|'));
6903 l = (int)STRLEN(buf + len);
6904 len += l;
6905 clen += l;
6906 }
6907 else
6908#endif
6909 for ( ; *s != NUL; ++s)
6910 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006911 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 clen += ptr2cells(s);
6913#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006914 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
6916 STRNCPY(buf + len, s, l);
6917 s += l - 1;
6918 len += l;
6919 }
6920 else
6921#endif
6922 {
6923 STRCPY(buf + len, transchar_byte(*s));
6924 len += (int)STRLEN(buf + len);
6925 }
6926 }
6927 if (i == match)
6928 selend = buf + len;
6929
6930 *(buf + len++) = ' ';
6931 *(buf + len++) = ' ';
6932 clen += 2;
6933 if (++i == num_matches)
6934 break;
6935 }
6936
6937 if (i != num_matches)
6938 {
6939 *(buf + len++) = '>';
6940 ++clen;
6941 }
6942
6943 buf[len] = NUL;
6944
6945 row = cmdline_row - 1;
6946 if (row >= 0)
6947 {
6948 if (wild_menu_showing == 0)
6949 {
6950 if (msg_scrolled > 0)
6951 {
6952 /* Put the wildmenu just above the command line. If there is
6953 * no room, scroll the screen one line up. */
6954 if (cmdline_row == Rows - 1)
6955 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006956 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 ++msg_scrolled;
6958 }
6959 else
6960 {
6961 ++cmdline_row;
6962 ++row;
6963 }
6964 wild_menu_showing = WM_SCROLLED;
6965 }
6966 else
6967 {
6968 /* Create status line if needed by setting 'laststatus' to 2.
6969 * Set 'winminheight' to zero to avoid that the window is
6970 * resized. */
6971 if (lastwin->w_status_height == 0)
6972 {
6973 save_p_ls = p_ls;
6974 save_p_wmh = p_wmh;
6975 p_ls = 2;
6976 p_wmh = 0;
6977 last_status(FALSE);
6978 }
6979 wild_menu_showing = WM_SHOWN;
6980 }
6981 }
6982
6983 screen_puts(buf, row, 0, attr);
6984 if (selstart != NULL && highlight)
6985 {
6986 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006987 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 }
6989
6990 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6991 }
6992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 vim_free(buf);
6995}
6996#endif
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998/*
6999 * Redraw the status line of window wp.
7000 *
7001 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007002 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7003 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007005 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007006win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 int row;
7009 char_u *p;
7010 int len;
7011 int fillchar;
7012 int attr;
7013 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007014 static int busy = FALSE;
7015
7016 /* It's possible to get here recursively when 'statusline' (indirectly)
7017 * invokes ":redrawstatus". Simply ignore the call then. */
7018 if (busy)
7019 return;
7020 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
7022 wp->w_redr_status = FALSE;
7023 if (wp->w_status_height == 0)
7024 {
7025 /* no status line, can only be last window */
7026 redraw_cmdline = TRUE;
7027 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007028 else if (!redrawing()
7029#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007030 // don't update status line when popup menu is visible and may be
7031 // drawn over it, unless it will be redrawn later
7032 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007033#endif
7034 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
7036 /* Don't redraw right now, do it later. */
7037 wp->w_redr_status = TRUE;
7038 }
7039#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007040 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {
7042 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007043 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 }
7045#endif
7046 else
7047 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007048 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007050 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 p = NameBuff;
7052 len = (int)STRLEN(p);
7053
Bram Moolenaard85f2712017-07-28 21:51:57 +02007054 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055#ifdef FEAT_QUICKFIX
7056 || wp->w_p_pvw
7057#endif
7058 || bufIsChanged(wp->w_buffer)
7059 || wp->w_buffer->b_p_ro)
7060 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007061 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007063 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 len += (int)STRLEN(p + len);
7065 }
7066#ifdef FEAT_QUICKFIX
7067 if (wp->w_p_pvw)
7068 {
7069 STRCPY(p + len, _("[Preview]"));
7070 len += (int)STRLEN(p + len);
7071 }
7072#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007073 if (bufIsChanged(wp->w_buffer)
7074#ifdef FEAT_TERMINAL
7075 && !bt_terminal(wp->w_buffer)
7076#endif
7077 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {
7079 STRCPY(p + len, "[+]");
7080 len += 3;
7081 }
7082 if (wp->w_buffer->b_p_ro)
7083 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007084 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007085 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
7087
Bram Moolenaar02631462017-09-22 15:20:32 +02007088 this_ru_col = ru_col - (Columns - wp->w_width);
7089 if (this_ru_col < (wp->w_width + 1) / 2)
7090 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 if (this_ru_col <= 1)
7092 {
7093 p = (char_u *)"<"; /* No room for file name! */
7094 len = 1;
7095 }
7096 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097#ifdef FEAT_MBYTE
7098 if (has_mbyte)
7099 {
7100 int clen = 0, i;
7101
7102 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007103 clen = mb_string2cells(p, -1);
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 /* Find first character that will fit.
7106 * Going from start to end is much faster for DBCS. */
7107 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007108 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 clen -= (*mb_ptr2cells)(p + i);
7110 len = clen;
7111 if (i > 0)
7112 {
7113 p = p + i - 1;
7114 *p = '<';
7115 ++len;
7116 }
7117
7118 }
7119 else
7120#endif
7121 if (len > this_ru_col - 1)
7122 {
7123 p += len - (this_ru_col - 1);
7124 *p = '<';
7125 len = this_ru_col - 1;
7126 }
7127
7128 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007129 screen_puts(p, row, wp->w_wincol, attr);
7130 screen_fill(row, row + 1, len + wp->w_wincol,
7131 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007133 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7135 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007136 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007139 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140#endif
7141 }
7142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 /*
7144 * May need to draw the character below the vertical separator.
7145 */
7146 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7147 {
7148 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007149 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 else
7151 fillchar = fillchar_vsep(&attr);
7152 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7153 attr);
7154 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007155 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156}
7157
Bram Moolenaar238a5642006-02-21 22:12:05 +00007158#ifdef FEAT_STL_OPT
7159/*
7160 * Redraw the status line according to 'statusline' and take care of any
7161 * errors encountered.
7162 */
7163 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007164redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007165{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007166 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007167 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007168
7169 /* When called recursively return. This can happen when the statusline
7170 * contains an expression that triggers a redraw. */
7171 if (entered)
7172 return;
7173 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007174
Bram Moolenaara742e082016-04-05 21:10:38 +02007175 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007176 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007177 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007178 {
7179 /* When there is an error disable the statusline, otherwise the
7180 * display is messed up with errors and a redraw triggers the problem
7181 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007182 set_string_option_direct((char_u *)"statusline", -1,
7183 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007184 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007185 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007186 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007187 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007188}
7189#endif
7190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191/*
7192 * Return TRUE if the status line of window "wp" is connected to the status
7193 * line of the window right of it. If not, then it's a vertical separator.
7194 * Only call if (wp->w_vsep_width != 0).
7195 */
7196 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007197stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 frame_T *fr;
7200
7201 fr = wp->w_frame;
7202 while (fr->fr_parent != NULL)
7203 {
7204 if (fr->fr_parent->fr_layout == FR_COL)
7205 {
7206 if (fr->fr_next != NULL)
7207 break;
7208 }
7209 else
7210 {
7211 if (fr->fr_next != NULL)
7212 return TRUE;
7213 }
7214 fr = fr->fr_parent;
7215 }
7216 return FALSE;
7217}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220/*
7221 * Get the value to show for the language mappings, active 'keymap'.
7222 */
7223 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007224get_keymap_str(
7225 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007226 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007227 char_u *buf, /* buffer for the result */
7228 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 char_u *p;
7231
7232 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7233 return FALSE;
7234
7235 {
7236#ifdef FEAT_EVAL
7237 buf_T *old_curbuf = curbuf;
7238 win_T *old_curwin = curwin;
7239 char_u *s;
7240
7241 curbuf = wp->w_buffer;
7242 curwin = wp;
7243 STRCPY(buf, "b:keymap_name"); /* must be writable */
7244 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007245 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 --emsg_skip;
7247 curbuf = old_curbuf;
7248 curwin = old_curwin;
7249 if (p == NULL || *p == NUL)
7250#endif
7251 {
7252#ifdef FEAT_KEYMAP
7253 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7254 p = wp->w_buffer->b_p_keymap;
7255 else
7256#endif
7257 p = (char_u *)"lang";
7258 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007259 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 buf[0] = NUL;
7261#ifdef FEAT_EVAL
7262 vim_free(s);
7263#endif
7264 }
7265 return buf[0] != NUL;
7266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
7268#if defined(FEAT_STL_OPT) || defined(PROTO)
7269/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007270 * Redraw the status line or ruler of window "wp".
7271 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 */
7273 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007274win_redr_custom(
7275 win_T *wp,
7276 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007278 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 int attr;
7280 int curattr;
7281 int row;
7282 int col = 0;
7283 int maxwidth;
7284 int width;
7285 int n;
7286 int len;
7287 int fillchar;
7288 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007289 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007291 struct stl_hlrec hltab[STL_MAX_ITEM];
7292 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007293 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007294 win_T *ewp;
7295 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar1d633412013-12-11 15:52:01 +01007297 /* There is a tiny chance that this gets called recursively: When
7298 * redrawing a status line triggers redrawing the ruler or tabline.
7299 * Avoid trouble by not allowing recursion. */
7300 if (entered)
7301 return;
7302 entered = TRUE;
7303
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007305 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007307 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007308 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007309 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007310 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007311 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007312 maxwidth = Columns;
7313# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007314 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007317 else
7318 {
7319 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007320 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007321 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007322
7323 if (draw_ruler)
7324 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007325 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007326 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007327 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007328 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007329 if (*++stl == '-')
7330 stl++;
7331 if (atoi((char *)stl))
7332 while (VIM_ISDIGIT(*stl))
7333 stl++;
7334 if (*stl++ != '(')
7335 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007336 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007337 col = ru_col - (Columns - wp->w_width);
7338 if (col < (wp->w_width + 1) / 2)
7339 col = (wp->w_width + 1) / 2;
7340 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007341 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007342 {
7343 row = Rows - 1;
7344 --maxwidth; /* writing in last column may cause scrolling */
7345 fillchar = ' ';
7346 attr = 0;
7347 }
7348
7349# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007350 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007351# endif
7352 }
7353 else
7354 {
7355 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007356 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007357 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007358 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007359# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007360 use_sandbox = was_set_insecurely((char_u *)"statusline",
7361 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007362# endif
7363 }
7364
Bram Moolenaar53f81742017-09-22 14:35:51 +02007365 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007366 }
7367
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007369 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
Bram Moolenaar61452852011-02-01 18:01:11 +01007371 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7372 * the cursor away and back. */
7373 ewp = wp == NULL ? curwin : wp;
7374 p_crb_save = ewp->w_p_crb;
7375 ewp->w_p_crb = FALSE;
7376
Bram Moolenaar362f3562009-11-03 16:20:34 +00007377 /* Make a copy, because the statusline may include a function call that
7378 * might change the option value and free the memory. */
7379 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007380 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007381 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007382 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007383 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007384 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007386 /* Make all characters printable. */
7387 p = transstr(buf);
7388 if (p != NULL)
7389 {
7390 vim_strncpy(buf, p, sizeof(buf) - 1);
7391 vim_free(p);
7392 }
7393
7394 /* fill up with "fillchar" */
7395 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007396 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
7398#ifdef FEAT_MBYTE
7399 len += (*mb_char2bytes)(fillchar, buf + len);
7400#else
7401 buf[len++] = fillchar;
7402#endif
7403 ++width;
7404 }
7405 buf[len] = NUL;
7406
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007407 /*
7408 * Draw each snippet with the specified highlighting.
7409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 curattr = attr;
7411 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007412 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007414 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 screen_puts_len(p, len, row, col, curattr);
7416 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007417 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007419 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007421 else if (hltab[n].userhl < 0)
7422 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007423#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007424 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7425 && wp->w_status_height != 0)
7426 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007427 else if (wp != NULL && bt_terminal(wp->w_buffer)
7428 && wp->w_status_height != 0)
7429 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007430#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007431 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007432 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007434 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 }
7436 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007437
7438 if (wp == NULL)
7439 {
7440 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7441 col = 0;
7442 len = 0;
7443 p = buf;
7444 fillchar = 0;
7445 for (n = 0; tabtab[n].start != NULL; n++)
7446 {
7447 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7448 while (col < len)
7449 TabPageIdxs[col++] = fillchar;
7450 p = tabtab[n].start;
7451 fillchar = tabtab[n].userhl;
7452 }
7453 while (col < Columns)
7454 TabPageIdxs[col++] = fillchar;
7455 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007456
7457theend:
7458 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459}
7460
7461#endif /* FEAT_STL_OPT */
7462
7463/*
7464 * Output a single character directly to the screen and update ScreenLines.
7465 */
7466 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007467screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 char_u buf[MB_MAXBYTES + 1];
7470
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007471#ifdef FEAT_MBYTE
7472 if (has_mbyte)
7473 buf[(*mb_char2bytes)(c, buf)] = NUL;
7474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007476 {
7477 buf[0] = c;
7478 buf[1] = NUL;
7479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 screen_puts(buf, row, col, attr);
7481}
7482
7483/*
7484 * Get a single character directly from ScreenLines into "bytes[]".
7485 * Also return its attribute in *attrp;
7486 */
7487 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007488screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 unsigned off;
7491
7492 /* safety check */
7493 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7494 {
7495 off = LineOffset[row] + col;
7496 *attrp = ScreenAttrs[off];
7497 bytes[0] = ScreenLines[off];
7498 bytes[1] = NUL;
7499
7500#ifdef FEAT_MBYTE
7501 if (enc_utf8 && ScreenLinesUC[off] != 0)
7502 bytes[utfc_char2bytes(off, bytes)] = NUL;
7503 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7504 {
7505 bytes[0] = ScreenLines[off];
7506 bytes[1] = ScreenLines2[off];
7507 bytes[2] = NUL;
7508 }
7509 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7510 {
7511 bytes[1] = ScreenLines[off + 1];
7512 bytes[2] = NUL;
7513 }
7514#endif
7515 }
7516}
7517
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007518#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007519/*
7520 * Return TRUE if composing characters for screen posn "off" differs from
7521 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007522 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007523 */
7524 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007525screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007526{
7527 int i;
7528
7529 for (i = 0; i < Screen_mco; ++i)
7530 {
7531 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7532 return TRUE;
7533 if (u8cc[i] == 0)
7534 break;
7535 }
7536 return FALSE;
7537}
7538#endif
7539
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540/*
7541 * Put string '*text' on the screen at position 'row' and 'col', with
7542 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7543 * Note: only outputs within one row, message is truncated at screen boundary!
7544 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7545 */
7546 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007547screen_puts(
7548 char_u *text,
7549 int row,
7550 int col,
7551 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 screen_puts_len(text, -1, row, col, attr);
7554}
7555
7556/*
7557 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7558 * a NUL.
7559 */
7560 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007561screen_puts_len(
7562 char_u *text,
7563 int textlen,
7564 int row,
7565 int col,
7566 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 unsigned off;
7569 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007570 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 int c;
7572#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007573 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 int mbyte_blen = 1;
7575 int mbyte_cells = 1;
7576 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007577 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 int clear_next_cell = FALSE;
7579# ifdef FEAT_ARABIC
7580 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007581 int pc, nc, nc1;
7582 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583# endif
7584#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007585#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7586 int force_redraw_this;
7587 int force_redraw_next = FALSE;
7588#endif
7589 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590
7591 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7592 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007593 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594
Bram Moolenaarc236c162008-07-13 17:41:49 +00007595#ifdef FEAT_MBYTE
7596 /* When drawing over the right halve of a double-wide char clear out the
7597 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007598 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007599# ifdef FEAT_GUI
7600 && !gui.in_use
7601# endif
7602 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007603 {
7604 ScreenLines[off - 1] = ' ';
7605 ScreenAttrs[off - 1] = 0;
7606 if (enc_utf8)
7607 {
7608 ScreenLinesUC[off - 1] = 0;
7609 ScreenLinesC[0][off - 1] = 0;
7610 }
7611 /* redraw the previous cell, make it empty */
7612 screen_char(off - 1, row, col - 1);
7613 /* force the cell at "col" to be redrawn */
7614 force_redraw_next = TRUE;
7615 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007616#endif
7617
Bram Moolenaar367329b2007-08-30 11:53:22 +00007618#ifdef FEAT_MBYTE
7619 max_off = LineOffset[row] + screen_Columns;
7620#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007621 while (col < screen_Columns
7622 && (len < 0 || (int)(ptr - text) < len)
7623 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
7625 c = *ptr;
7626#ifdef FEAT_MBYTE
7627 /* check if this is the first byte of a multibyte */
7628 if (has_mbyte)
7629 {
7630 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007631 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007633 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7635 mbyte_cells = 1;
7636 else if (enc_dbcs != 0)
7637 mbyte_cells = mbyte_blen;
7638 else /* enc_utf8 */
7639 {
7640 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007641 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 (int)((text + len) - ptr));
7643 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007644 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007646# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 /* Non-BMP character: display as ? or fullwidth ?. */
7648 if (u8c >= 0x10000)
7649 {
7650 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7651 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007652 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655# ifdef FEAT_ARABIC
7656 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7657 {
7658 /* Do Arabic shaping. */
7659 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7660 {
7661 /* Past end of string to be displayed. */
7662 nc = NUL;
7663 nc1 = NUL;
7664 }
7665 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007666 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007667 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7668 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007669 nc1 = pcc[0];
7670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 pc = prev_c;
7672 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007673 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
7675 else
7676 prev_c = u8c;
7677# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007678 if (col + mbyte_cells > screen_Columns)
7679 {
7680 /* Only 1 cell left, but character requires 2 cells:
7681 * display a '>' in the last column to avoid wrapping. */
7682 c = '>';
7683 mbyte_cells = 1;
7684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686 }
7687#endif
7688
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007689#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7690 force_redraw_this = force_redraw_next;
7691 force_redraw_next = FALSE;
7692#endif
7693
7694 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695#ifdef FEAT_MBYTE
7696 || (mbyte_cells == 2
7697 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7698 || (enc_dbcs == DBCS_JPNU
7699 && c == 0x8e
7700 && ScreenLines2[off] != ptr[1])
7701 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007702 && (ScreenLinesUC[off] !=
7703 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7704 || (ScreenLinesUC[off] != 0
7705 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706#endif
7707 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007708 || exmode_active;
7709
7710 if (need_redraw
7711#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7712 || force_redraw_this
7713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 )
7715 {
7716#if defined(FEAT_GUI) || defined(UNIX)
7717 /* The bold trick makes a single row of pixels appear in the next
7718 * character. When a bold character is removed, the next
7719 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007720 * and for some xterms. */
7721 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722# ifdef FEAT_GUI
7723 gui.in_use
7724# endif
7725# if defined(FEAT_GUI) && defined(UNIX)
7726 ||
7727# endif
7728# ifdef UNIX
7729 term_is_xterm
7730# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007731 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007733 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007735 if (n > HL_ALL)
7736 n = syn_attr2attr(n);
7737 if (n & HL_BOLD)
7738 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740#endif
7741#ifdef FEAT_MBYTE
7742 /* When at the end of the text and overwriting a two-cell
7743 * character with a one-cell character, need to clear the next
7744 * cell. Also when overwriting the left halve of a two-cell char
7745 * with the right halve of a two-cell char. Do this only once
7746 * (mb_off2cells() may return 2 on the right halve). */
7747 if (clear_next_cell)
7748 clear_next_cell = FALSE;
7749 else if (has_mbyte
7750 && (len < 0 ? ptr[mbyte_blen] == NUL
7751 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007752 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007754 && (*mb_off2cells)(off, max_off) == 1
7755 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 clear_next_cell = TRUE;
7757
7758 /* Make sure we never leave a second byte of a double-byte behind,
7759 * it confuses mb_off2cells(). */
7760 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007761 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007763 && (*mb_off2cells)(off, max_off) == 1
7764 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 ScreenLines[off + mbyte_blen] = 0;
7766#endif
7767 ScreenLines[off] = c;
7768 ScreenAttrs[off] = attr;
7769#ifdef FEAT_MBYTE
7770 if (enc_utf8)
7771 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007772 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 ScreenLinesUC[off] = 0;
7774 else
7775 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007776 int i;
7777
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007779 for (i = 0; i < Screen_mco; ++i)
7780 {
7781 ScreenLinesC[i][off] = u8cc[i];
7782 if (u8cc[i] == 0)
7783 break;
7784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 }
7786 if (mbyte_cells == 2)
7787 {
7788 ScreenLines[off + 1] = 0;
7789 ScreenAttrs[off + 1] = attr;
7790 }
7791 screen_char(off, row, col);
7792 }
7793 else if (mbyte_cells == 2)
7794 {
7795 ScreenLines[off + 1] = ptr[1];
7796 ScreenAttrs[off + 1] = attr;
7797 screen_char_2(off, row, col);
7798 }
7799 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7800 {
7801 ScreenLines2[off] = ptr[1];
7802 screen_char(off, row, col);
7803 }
7804 else
7805#endif
7806 screen_char(off, row, col);
7807 }
7808#ifdef FEAT_MBYTE
7809 if (has_mbyte)
7810 {
7811 off += mbyte_cells;
7812 col += mbyte_cells;
7813 ptr += mbyte_blen;
7814 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007815 {
7816 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007818 len = -1;
7819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 }
7821 else
7822#endif
7823 {
7824 ++off;
7825 ++col;
7826 ++ptr;
7827 }
7828 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007829
7830#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7831 /* If we detected the next character needs to be redrawn, but the text
7832 * doesn't extend up to there, update the character here. */
7833 if (force_redraw_next && col < screen_Columns)
7834 {
7835# ifdef FEAT_MBYTE
7836 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7837 screen_char_2(off, row, col);
7838 else
7839# endif
7840 screen_char(off, row, col);
7841 }
7842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843}
7844
7845#ifdef FEAT_SEARCH_EXTRA
7846/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007847 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 */
7849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007850start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
7852 if (p_hls && !no_hlsearch)
7853 {
7854 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007855 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007856# ifdef FEAT_RELTIME
7857 /* Set the time limit to 'redrawtime'. */
7858 profile_setlimit(p_rdt, &search_hl.tm);
7859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 }
7861}
7862
7863/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007864 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 */
7866 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007867end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 if (search_hl.rm.regprog != NULL)
7870 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007871 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 search_hl.rm.regprog = NULL;
7873 }
7874}
7875
7876/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007877 * Init for calling prepare_search_hl().
7878 */
7879 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007880init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007881{
7882 matchitem_T *cur;
7883
7884 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7885 * match */
7886 cur = wp->w_match_head;
7887 while (cur != NULL)
7888 {
7889 cur->hl.rm = cur->match;
7890 if (cur->hlg_id == 0)
7891 cur->hl.attr = 0;
7892 else
7893 cur->hl.attr = syn_id2attr(cur->hlg_id);
7894 cur->hl.buf = wp->w_buffer;
7895 cur->hl.lnum = 0;
7896 cur->hl.first_lnum = 0;
7897# ifdef FEAT_RELTIME
7898 /* Set the time limit to 'redrawtime'. */
7899 profile_setlimit(p_rdt, &(cur->hl.tm));
7900# endif
7901 cur = cur->next;
7902 }
7903 search_hl.buf = wp->w_buffer;
7904 search_hl.lnum = 0;
7905 search_hl.first_lnum = 0;
7906 /* time limit is set at the toplevel, for all windows */
7907}
7908
7909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 * Advance to the match in window "wp" line "lnum" or past it.
7911 */
7912 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007913prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007915 matchitem_T *cur; /* points to the match list */
7916 match_T *shl; /* points to search_hl or a match */
7917 int shl_flag; /* flag to indicate whether search_hl
7918 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007919 int pos_inprogress; /* marks that position match search is
7920 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 int n;
7922
7923 /*
7924 * When using a multi-line pattern, start searching at the top
7925 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007926 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007928 cur = wp->w_match_head;
7929 shl_flag = FALSE;
7930 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007932 if (shl_flag == FALSE)
7933 {
7934 shl = &search_hl;
7935 shl_flag = TRUE;
7936 }
7937 else
7938 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 if (shl->rm.regprog != NULL
7940 && shl->lnum == 0
7941 && re_multiline(shl->rm.regprog))
7942 {
7943 if (shl->first_lnum == 0)
7944 {
7945# ifdef FEAT_FOLDING
7946 for (shl->first_lnum = lnum;
7947 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7948 if (hasFoldingWin(wp, shl->first_lnum - 1,
7949 NULL, NULL, TRUE, NULL))
7950 break;
7951# else
7952 shl->first_lnum = wp->w_topline;
7953# endif
7954 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955 if (cur != NULL)
7956 cur->pos.cur = 0;
7957 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007959 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7960 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007962 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7963 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007964 pos_inprogress = cur == NULL || cur->pos.cur == 0
7965 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 if (shl->lnum != 0)
7967 {
7968 shl->first_lnum = shl->lnum
7969 + shl->rm.endpos[0].lnum
7970 - shl->rm.startpos[0].lnum;
7971 n = shl->rm.endpos[0].col;
7972 }
7973 else
7974 {
7975 ++shl->first_lnum;
7976 n = 0;
7977 }
7978 }
7979 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007980 if (shl != &search_hl && cur != NULL)
7981 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 }
7983}
7984
7985/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007986 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 * Uses shl->buf.
7988 * Sets shl->lnum and shl->rm contents.
7989 * Note: Assumes a previous match is always before "lnum", unless
7990 * shl->lnum is zero.
7991 * Careful: Any pointers for buffer lines will become invalid.
7992 */
7993 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007994next_search_hl(
7995 win_T *win,
7996 match_T *shl, /* points to search_hl or a match */
7997 linenr_T lnum,
7998 colnr_T mincol, /* minimal column for a match */
7999 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
8001 linenr_T l;
8002 colnr_T matchcol;
8003 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008004 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008006 // for :{range}s/pat only highlight inside the range
8007 if (lnum < search_first_line || lnum > search_last_line)
8008 {
8009 shl->lnum = 0;
8010 return;
8011 }
8012
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 if (shl->lnum != 0)
8014 {
8015 /* Check for three situations:
8016 * 1. If the "lnum" is below a previous match, start a new search.
8017 * 2. If the previous match includes "mincol", use it.
8018 * 3. Continue after the previous match.
8019 */
8020 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8021 if (lnum > l)
8022 shl->lnum = 0;
8023 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8024 return;
8025 }
8026
8027 /*
8028 * Repeat searching for a match until one is found that includes "mincol"
8029 * or none is found in this line.
8030 */
8031 called_emsg = FALSE;
8032 for (;;)
8033 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008034#ifdef FEAT_RELTIME
8035 /* Stop searching after passing the time limit. */
8036 if (profile_passed_limit(&(shl->tm)))
8037 {
8038 shl->lnum = 0; /* no match found in time */
8039 break;
8040 }
8041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 /* Three situations:
8043 * 1. No useful previous match: search from start of line.
8044 * 2. Not Vi compatible or empty match: continue at next character.
8045 * Break the loop if this is beyond the end of the line.
8046 * 3. Vi compatible searching: continue at end of previous match.
8047 */
8048 if (shl->lnum == 0)
8049 matchcol = 0;
8050 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8051 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008052 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008054 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008055
8056 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008057 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008058 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008060 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 shl->lnum = 0;
8062 break;
8063 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008064#ifdef FEAT_MBYTE
8065 if (has_mbyte)
8066 matchcol += mb_ptr2len(ml);
8067 else
8068#endif
8069 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 }
8071 else
8072 matchcol = shl->rm.endpos[0].col;
8073
8074 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008075 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008077 /* Remember whether shl->rm is using a copy of the regprog in
8078 * cur->match. */
8079 int regprog_is_copy = (shl != &search_hl && cur != NULL
8080 && shl == &cur->hl
8081 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008082 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008083
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8085 matchcol,
8086#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008087 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008088#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008089 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008090#endif
8091 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008092 /* Copy the regprog, in case it got freed and recompiled. */
8093 if (regprog_is_copy)
8094 cur->match.regprog = cur->hl.rm.regprog;
8095
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008096 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008097 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008098 /* Error while handling regexp: stop using this regexp. */
8099 if (shl == &search_hl)
8100 {
8101 /* don't free regprog in the match list, it's a copy */
8102 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008103 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008104 }
8105 shl->rm.regprog = NULL;
8106 shl->lnum = 0;
8107 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8108 message */
8109 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008110 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008111 }
8112 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008113 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008114 else
8115 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 if (nmatched == 0)
8117 {
8118 shl->lnum = 0; /* no match found */
8119 break;
8120 }
8121 if (shl->rm.startpos[0].lnum > 0
8122 || shl->rm.startpos[0].col >= mincol
8123 || nmatched > 1
8124 || shl->rm.endpos[0].col > mincol)
8125 {
8126 shl->lnum += shl->rm.startpos[0].lnum;
8127 break; /* useful match found */
8128 }
8129 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008130
8131 // Restore called_emsg for assert_fails().
8132 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134
Bram Moolenaar85077472016-10-16 14:35:48 +02008135/*
8136 * If there is a match fill "shl" and return one.
8137 * Return zero otherwise.
8138 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008139 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008140next_search_hl_pos(
8141 match_T *shl, /* points to a match */
8142 linenr_T lnum,
8143 posmatch_T *posmatch, /* match positions */
8144 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008145{
8146 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008147 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008148
Bram Moolenaarb3414592014-06-17 17:48:32 +02008149 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8150 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008151 llpos_T *pos = &posmatch->pos[i];
8152
8153 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008154 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008155 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008156 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008157 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008158 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008159 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008160 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008161 /* if this match comes before the one at "found" then swap
8162 * them */
8163 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008164 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008165 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008166
Bram Moolenaar85077472016-10-16 14:35:48 +02008167 *pos = posmatch->pos[found];
8168 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008169 }
8170 }
8171 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008172 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008173 }
8174 }
8175 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008176 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008177 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008178 colnr_T start = posmatch->pos[found].col == 0
8179 ? 0 : posmatch->pos[found].col - 1;
8180 colnr_T end = posmatch->pos[found].col == 0
8181 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008182
Bram Moolenaar85077472016-10-16 14:35:48 +02008183 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008184 shl->rm.startpos[0].lnum = 0;
8185 shl->rm.startpos[0].col = start;
8186 shl->rm.endpos[0].lnum = 0;
8187 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008188 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008189 posmatch->cur = found + 1;
8190 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008191 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008192 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008193}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008194#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008197screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198{
8199 attrentry_T *aep = NULL;
8200
8201 screen_attr = attr;
8202 if (full_screen
8203#ifdef WIN3264
8204 && termcap_active
8205#endif
8206 )
8207 {
8208#ifdef FEAT_GUI
8209 if (gui.in_use)
8210 {
8211 char buf[20];
8212
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008213 /* The GUI handles this internally. */
8214 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 OUT_STR(buf);
8216 }
8217 else
8218#endif
8219 {
8220 if (attr > HL_ALL) /* special HL attr. */
8221 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008222 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 aep = syn_cterm_attr2entry(attr);
8224 else
8225 aep = syn_term_attr2entry(attr);
8226 if (aep == NULL) /* did ":syntax clear" */
8227 attr = 0;
8228 else
8229 attr = aep->ae_attr;
8230 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008231 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008233 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008234#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008235 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8236 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8237 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008238#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008239 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008240 /* If the Normal FG color has BOLD attribute and the new HL
8241 * has a FG color defined, clear BOLD. */
8242 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008243 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008245 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008246 out_str(T_UCS);
8247 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008248 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8249 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008251 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008253 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008255 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008256 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257
8258 /*
8259 * Output the color or start string after bold etc., in case the
8260 * bold etc. override the color setting.
8261 */
8262 if (aep != NULL)
8263 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008264#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008265 /* When 'termguicolors' is set but fg or bg is unset,
8266 * fall back to the cterm colors. This helps for SpellBad,
8267 * where the GUI uses a red undercurl. */
8268 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008270 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008271 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008272 }
8273 else
8274#endif
8275 if (t_colors > 1)
8276 {
8277 if (aep->ae_u.cterm.fg_color)
8278 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8279 }
8280#ifdef FEAT_TERMGUICOLORS
8281 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8282 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008283 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008284 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 }
8286 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008287#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008288 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008290 if (aep->ae_u.cterm.bg_color)
8291 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8292 }
8293
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008294 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008295 {
8296 if (aep->ae_u.term.start != NULL)
8297 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 }
8299 }
8300 }
8301 }
8302}
8303
8304 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008305screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
8307 int do_ME = FALSE; /* output T_ME code */
8308
8309 if (screen_attr != 0
8310#ifdef WIN3264
8311 && termcap_active
8312#endif
8313 )
8314 {
8315#ifdef FEAT_GUI
8316 if (gui.in_use)
8317 {
8318 char buf[20];
8319
8320 /* use internal GUI code */
8321 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8322 OUT_STR(buf);
8323 }
8324 else
8325#endif
8326 {
8327 if (screen_attr > HL_ALL) /* special HL attr. */
8328 {
8329 attrentry_T *aep;
8330
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008331 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {
8333 /*
8334 * Assume that t_me restores the original colors!
8335 */
8336 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008337 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008338#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008339 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8340 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8341 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008342#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008343 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008344#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008345 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8346 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8347 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008348#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008349 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 do_ME = TRUE;
8351 }
8352 else
8353 {
8354 aep = syn_term_attr2entry(screen_attr);
8355 if (aep != NULL && aep->ae_u.term.stop != NULL)
8356 {
8357 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8358 do_ME = TRUE;
8359 else
8360 out_str(aep->ae_u.term.stop);
8361 }
8362 }
8363 if (aep == NULL) /* did ":syntax clear" */
8364 screen_attr = 0;
8365 else
8366 screen_attr = aep->ae_attr;
8367 }
8368
8369 /*
8370 * Often all ending-codes are equal to T_ME. Avoid outputting the
8371 * same sequence several times.
8372 */
8373 if (screen_attr & HL_STANDOUT)
8374 {
8375 if (STRCMP(T_SE, T_ME) == 0)
8376 do_ME = TRUE;
8377 else
8378 out_str(T_SE);
8379 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008380 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008381 {
8382 if (STRCMP(T_UCE, T_ME) == 0)
8383 do_ME = TRUE;
8384 else
8385 out_str(T_UCE);
8386 }
8387 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008388 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {
8390 if (STRCMP(T_UE, T_ME) == 0)
8391 do_ME = TRUE;
8392 else
8393 out_str(T_UE);
8394 }
8395 if (screen_attr & HL_ITALIC)
8396 {
8397 if (STRCMP(T_CZR, T_ME) == 0)
8398 do_ME = TRUE;
8399 else
8400 out_str(T_CZR);
8401 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008402 if (screen_attr & HL_STRIKETHROUGH)
8403 {
8404 if (STRCMP(T_STE, T_ME) == 0)
8405 do_ME = TRUE;
8406 else
8407 out_str(T_STE);
8408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8410 out_str(T_ME);
8411
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008412#ifdef FEAT_TERMGUICOLORS
8413 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008415 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008416 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008417 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008418 term_bg_rgb_color(cterm_normal_bg_gui_color);
8419 }
8420 else
8421#endif
8422 {
8423 if (t_colors > 1)
8424 {
8425 /* set Normal cterm colors */
8426 if (cterm_normal_fg_color != 0)
8427 term_fg_color(cterm_normal_fg_color - 1);
8428 if (cterm_normal_bg_color != 0)
8429 term_bg_color(cterm_normal_bg_color - 1);
8430 if (cterm_normal_fg_bold)
8431 out_str(T_MD);
8432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 }
8434 }
8435 }
8436 screen_attr = 0;
8437}
8438
8439/*
8440 * Reset the colors for a cterm. Used when leaving Vim.
8441 * The machine specific code may override this again.
8442 */
8443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008444reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008446 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {
8448 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008449#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008450 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8451 || cterm_normal_bg_gui_color != INVALCOLOR)
8452 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008453#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {
8457 out_str(T_OP);
8458 screen_attr = -1;
8459 }
8460 if (cterm_normal_fg_bold)
8461 {
8462 out_str(T_ME);
8463 screen_attr = -1;
8464 }
8465 }
8466}
8467
8468/*
8469 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8470 * using the attributes from ScreenAttrs["off"].
8471 */
8472 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008473screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474{
8475 int attr;
8476
8477 /* Check for illegal values, just in case (could happen just after
8478 * resizing). */
8479 if (row >= screen_Rows || col >= screen_Columns)
8480 return;
8481
Bram Moolenaar494838a2015-02-10 19:20:37 +01008482 /* Outputting a character in the last cell on the screen may scroll the
8483 * screen up. Only do it when the "xn" termcap property is set, otherwise
8484 * mark the character invalid (update it when scrolled up). */
8485 if (*T_XN == NUL
8486 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487#ifdef FEAT_RIGHTLEFT
8488 /* account for first command-line character in rightleft mode */
8489 && !cmdmsg_rl
8490#endif
8491 )
8492 {
8493 ScreenAttrs[off] = (sattr_T)-1;
8494 return;
8495 }
8496
8497 /*
8498 * Stop highlighting first, so it's easier to move the cursor.
8499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (screen_char_attr != 0)
8501 attr = screen_char_attr;
8502 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 attr = ScreenAttrs[off];
8504 if (screen_attr != attr)
8505 screen_stop_highlight();
8506
8507 windgoto(row, col);
8508
8509 if (screen_attr != attr)
8510 screen_start_highlight(attr);
8511
8512#ifdef FEAT_MBYTE
8513 if (enc_utf8 && ScreenLinesUC[off] != 0)
8514 {
8515 char_u buf[MB_MAXBYTES + 1];
8516
Bram Moolenaarcb070082016-04-02 22:14:51 +02008517 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008518 {
8519 if (*p_ambw == 'd'
8520# ifdef FEAT_GUI
8521 && !gui.in_use
8522# endif
8523 )
8524 {
8525 /* Clear the two screen cells. If the character is actually
8526 * single width it won't change the second cell. */
8527 out_str((char_u *)" ");
8528 term_windgoto(row, col);
8529 }
8530 /* not sure where the cursor is after drawing the ambiguous width
8531 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008532 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008533 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008534 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008536
8537 /* Convert the UTF-8 character to bytes and write it. */
8538 buf[utfc_char2bytes(off, buf)] = NUL;
8539 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 }
8541 else
8542#endif
8543 {
8544#ifdef FEAT_MBYTE
8545 out_flush_check();
8546#endif
8547 out_char(ScreenLines[off]);
8548#ifdef FEAT_MBYTE
8549 /* double-byte character in single-width cell */
8550 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8551 out_char(ScreenLines2[off]);
8552#endif
8553 }
8554
8555 screen_cur_col++;
8556}
8557
8558#ifdef FEAT_MBYTE
8559
8560/*
8561 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8562 * on the screen at position 'row' and 'col'.
8563 * The attributes of the first byte is used for all. This is required to
8564 * output the two bytes of a double-byte character with nothing in between.
8565 */
8566 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008567screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568{
8569 /* Check for illegal values (could be wrong when screen was resized). */
8570 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8571 return;
8572
8573 /* Outputting the last character on the screen may scrollup the screen.
8574 * Don't to it! Mark the character invalid (update it when scrolled up) */
8575 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8576 {
8577 ScreenAttrs[off] = (sattr_T)-1;
8578 return;
8579 }
8580
8581 /* Output the first byte normally (positions the cursor), then write the
8582 * second byte directly. */
8583 screen_char(off, row, col);
8584 out_char(ScreenLines[off + 1]);
8585 ++screen_cur_col;
8586}
8587#endif
8588
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589/*
8590 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8591 * This uses the contents of ScreenLines[] and doesn't change it.
8592 */
8593 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008594screen_draw_rectangle(
8595 int row,
8596 int col,
8597 int height,
8598 int width,
8599 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
8601 int r, c;
8602 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008603#ifdef FEAT_MBYTE
8604 int max_off;
8605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008607 /* Can't use ScreenLines unless initialized */
8608 if (ScreenLines == NULL)
8609 return;
8610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 if (invert)
8612 screen_char_attr = HL_INVERSE;
8613 for (r = row; r < row + height; ++r)
8614 {
8615 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008616#ifdef FEAT_MBYTE
8617 max_off = off + screen_Columns;
8618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 for (c = col; c < col + width; ++c)
8620 {
8621#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008622 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {
8624 screen_char_2(off + c, r, c);
8625 ++c;
8626 }
8627 else
8628#endif
8629 {
8630 screen_char(off + c, r, c);
8631#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008632 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 ++c;
8634#endif
8635 }
8636 }
8637 }
8638 screen_char_attr = 0;
8639}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641/*
8642 * Redraw the characters for a vertically split window.
8643 */
8644 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008645redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
8647 int col;
8648 int width;
8649
8650# ifdef FEAT_CLIPBOARD
8651 clip_may_clear_selection(row, end - 1);
8652# endif
8653
8654 if (wp == NULL)
8655 {
8656 col = 0;
8657 width = Columns;
8658 }
8659 else
8660 {
8661 col = wp->w_wincol;
8662 width = wp->w_width;
8663 }
8664 screen_draw_rectangle(row, col, end - row, width, FALSE);
8665}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008667 static void
8668space_to_screenline(int off, int attr)
8669{
8670 ScreenLines[off] = ' ';
8671 ScreenAttrs[off] = attr;
8672# ifdef FEAT_MBYTE
8673 if (enc_utf8)
8674 ScreenLinesUC[off] = 0;
8675# endif
8676}
8677
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678/*
8679 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8680 * with character 'c1' in first column followed by 'c2' in the other columns.
8681 * Use attributes 'attr'.
8682 */
8683 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008684screen_fill(
8685 int start_row,
8686 int end_row,
8687 int start_col,
8688 int end_col,
8689 int c1,
8690 int c2,
8691 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
8693 int row;
8694 int col;
8695 int off;
8696 int end_off;
8697 int did_delete;
8698 int c;
8699 int norm_term;
8700#if defined(FEAT_GUI) || defined(UNIX)
8701 int force_next = FALSE;
8702#endif
8703
8704 if (end_row > screen_Rows) /* safety check */
8705 end_row = screen_Rows;
8706 if (end_col > screen_Columns) /* safety check */
8707 end_col = screen_Columns;
8708 if (ScreenLines == NULL
8709 || start_row >= end_row
8710 || start_col >= end_col) /* nothing to do */
8711 return;
8712
8713 /* it's a "normal" terminal when not in a GUI or cterm */
8714 norm_term = (
8715#ifdef FEAT_GUI
8716 !gui.in_use &&
8717#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008718 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 for (row = start_row; row < end_row; ++row)
8720 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008721#ifdef FEAT_MBYTE
8722 if (has_mbyte
8723# ifdef FEAT_GUI
8724 && !gui.in_use
8725# endif
8726 )
8727 {
8728 /* When drawing over the right halve of a double-wide char clear
8729 * out the left halve. When drawing over the left halve of a
8730 * double wide-char clear out the right halve. Only needed in a
8731 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008732 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008733 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008734 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008735 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008736 }
8737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 /*
8739 * Try to use delete-line termcap code, when no attributes or in a
8740 * "normal" terminal, where a bold/italic space is just a
8741 * space.
8742 */
8743 did_delete = FALSE;
8744 if (c2 == ' '
8745 && end_col == Columns
8746 && can_clear(T_CE)
8747 && (attr == 0
8748 || (norm_term
8749 && attr <= HL_ALL
8750 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8751 {
8752 /*
8753 * check if we really need to clear something
8754 */
8755 col = start_col;
8756 if (c1 != ' ') /* don't clear first char */
8757 ++col;
8758
8759 off = LineOffset[row] + col;
8760 end_off = LineOffset[row] + end_col;
8761
8762 /* skip blanks (used often, keep it fast!) */
8763#ifdef FEAT_MBYTE
8764 if (enc_utf8)
8765 while (off < end_off && ScreenLines[off] == ' '
8766 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8767 ++off;
8768 else
8769#endif
8770 while (off < end_off && ScreenLines[off] == ' '
8771 && ScreenAttrs[off] == 0)
8772 ++off;
8773 if (off < end_off) /* something to be cleared */
8774 {
8775 col = off - LineOffset[row];
8776 screen_stop_highlight();
8777 term_windgoto(row, col);/* clear rest of this screen line */
8778 out_str(T_CE);
8779 screen_start(); /* don't know where cursor is now */
8780 col = end_col - col;
8781 while (col--) /* clear chars in ScreenLines */
8782 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008783 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 ++off;
8785 }
8786 }
8787 did_delete = TRUE; /* the chars are cleared now */
8788 }
8789
8790 off = LineOffset[row] + start_col;
8791 c = c1;
8792 for (col = start_col; col < end_col; ++col)
8793 {
8794 if (ScreenLines[off] != c
8795#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008796 || (enc_utf8 && (int)ScreenLinesUC[off]
8797 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798#endif
8799 || ScreenAttrs[off] != attr
8800#if defined(FEAT_GUI) || defined(UNIX)
8801 || force_next
8802#endif
8803 )
8804 {
8805#if defined(FEAT_GUI) || defined(UNIX)
8806 /* The bold trick may make a single row of pixels appear in
8807 * the next character. When a bold character is removed, the
8808 * next character should be redrawn too. This happens for our
8809 * own GUI and for some xterms. */
8810 if (
8811# ifdef FEAT_GUI
8812 gui.in_use
8813# endif
8814# if defined(FEAT_GUI) && defined(UNIX)
8815 ||
8816# endif
8817# ifdef UNIX
8818 term_is_xterm
8819# endif
8820 )
8821 {
8822 if (ScreenLines[off] != ' '
8823 && (ScreenAttrs[off] > HL_ALL
8824 || ScreenAttrs[off] & HL_BOLD))
8825 force_next = TRUE;
8826 else
8827 force_next = FALSE;
8828 }
8829#endif
8830 ScreenLines[off] = c;
8831#ifdef FEAT_MBYTE
8832 if (enc_utf8)
8833 {
8834 if (c >= 0x80)
8835 {
8836 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008837 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 }
8839 else
8840 ScreenLinesUC[off] = 0;
8841 }
8842#endif
8843 ScreenAttrs[off] = attr;
8844 if (!did_delete || c != ' ')
8845 screen_char(off, row, col);
8846 }
8847 ++off;
8848 if (col == start_col)
8849 {
8850 if (did_delete)
8851 break;
8852 c = c2;
8853 }
8854 }
8855 if (end_col == Columns)
8856 LineWraps[row] = FALSE;
8857 if (row == Rows - 1) /* overwritten the command line */
8858 {
8859 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008860 if (start_col == 0 && end_col == Columns
8861 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008863 if (start_col == 0)
8864 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 }
8866 }
8867}
8868
8869/*
8870 * Check if there should be a delay. Used before clearing or redrawing the
8871 * screen or the command line.
8872 */
8873 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008874check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875{
8876 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8877 && !did_wait_return
8878 && emsg_silent == 0)
8879 {
8880 out_flush();
8881 ui_delay(1000L, TRUE);
8882 emsg_on_display = FALSE;
8883 if (check_msg_scroll)
8884 msg_scroll = FALSE;
8885 }
8886}
8887
8888/*
8889 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008890 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 * Returns TRUE if there is a valid screen to write to.
8892 * Returns FALSE when starting up and screen not initialized yet.
8893 */
8894 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008895screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008897 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 return (ScreenLines != NULL);
8899}
8900
8901/*
8902 * Resize the shell to Rows and Columns.
8903 * Allocate ScreenLines[] and associated items.
8904 *
8905 * There may be some time between setting Rows and Columns and (re)allocating
8906 * ScreenLines[]. This happens when starting up and when (manually) changing
8907 * the shell size. Always use screen_Rows and screen_Columns to access items
8908 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8909 * final size of the shell is needed.
8910 */
8911 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008912screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913{
8914 int new_row, old_row;
8915#ifdef FEAT_GUI
8916 int old_Rows;
8917#endif
8918 win_T *wp;
8919 int outofmem = FALSE;
8920 int len;
8921 schar_T *new_ScreenLines;
8922#ifdef FEAT_MBYTE
8923 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008924 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008926 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927#endif
8928 sattr_T *new_ScreenAttrs;
8929 unsigned *new_LineOffset;
8930 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008931 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008932 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008934 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008935 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008937retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 /*
8939 * Allocation of the screen buffers is done only when the size changes and
8940 * when Rows and Columns have been set and we have started doing full
8941 * screen stuff.
8942 */
8943 if ((ScreenLines != NULL
8944 && Rows == screen_Rows
8945 && Columns == screen_Columns
8946#ifdef FEAT_MBYTE
8947 && enc_utf8 == (ScreenLinesUC != NULL)
8948 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008949 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950#endif
8951 )
8952 || Rows == 0
8953 || Columns == 0
8954 || (!full_screen && ScreenLines == NULL))
8955 return;
8956
8957 /*
8958 * It's possible that we produce an out-of-memory message below, which
8959 * will cause this function to be called again. To break the loop, just
8960 * return here.
8961 */
8962 if (entered)
8963 return;
8964 entered = TRUE;
8965
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008966 /*
8967 * Note that the window sizes are updated before reallocating the arrays,
8968 * thus we must not redraw here!
8969 */
8970 ++RedrawingDisabled;
8971
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 win_new_shellsize(); /* fit the windows in the new sized shell */
8973
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 comp_col(); /* recompute columns for shown command and ruler */
8975
8976 /*
8977 * We're changing the size of the screen.
8978 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8979 * - Move lines from the old arrays into the new arrays, clear extra
8980 * lines (unless the screen is going to be cleared).
8981 * - Free the old arrays.
8982 *
8983 * If anything fails, make ScreenLines NULL, so we don't do anything!
8984 * Continuing with the old ScreenLines may result in a crash, because the
8985 * size is wrong.
8986 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008987 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008989 if (aucmd_win != NULL)
8990 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991
8992 new_ScreenLines = (schar_T *)lalloc((long_u)(
8993 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8994#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008995 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 if (enc_utf8)
8997 {
8998 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8999 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009000 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01009001 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
9003 }
9004 if (enc_dbcs == DBCS_JPNU)
9005 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9006 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9007#endif
9008 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9009 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9010 new_LineOffset = (unsigned *)lalloc((long_u)(
9011 Rows * sizeof(unsigned)), FALSE);
9012 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009013 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009015 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 {
9017 if (win_alloc_lines(wp) == FAIL)
9018 {
9019 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009020 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 }
9022 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009023 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9024 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009025 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009026give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009028#ifdef FEAT_MBYTE
9029 for (i = 0; i < p_mco; ++i)
9030 if (new_ScreenLinesC[i] == NULL)
9031 break;
9032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 if (new_ScreenLines == NULL
9034#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009035 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9037#endif
9038 || new_ScreenAttrs == NULL
9039 || new_LineOffset == NULL
9040 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009041 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 || outofmem)
9043 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009044 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009045 {
9046 /* guess the size */
9047 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9048
9049 /* Remember we did this to avoid getting outofmem messages over
9050 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009051 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009052 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009053 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009055 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009056 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009057 VIM_CLEAR(new_ScreenLinesC[i]);
9058 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009060 VIM_CLEAR(new_ScreenAttrs);
9061 VIM_CLEAR(new_LineOffset);
9062 VIM_CLEAR(new_LineWraps);
9063 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 }
9065 else
9066 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009067 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009068
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 for (new_row = 0; new_row < Rows; ++new_row)
9070 {
9071 new_LineOffset[new_row] = new_row * Columns;
9072 new_LineWraps[new_row] = FALSE;
9073
9074 /*
9075 * If the screen is not going to be cleared, copy as much as
9076 * possible from the old screen to the new one and clear the rest
9077 * (used when resizing the window at the "--more--" prompt or when
9078 * executing an external command, for the GUI).
9079 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009080 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 {
9082 (void)vim_memset(new_ScreenLines + new_row * Columns,
9083 ' ', (size_t)Columns * sizeof(schar_T));
9084#ifdef FEAT_MBYTE
9085 if (enc_utf8)
9086 {
9087 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9088 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009089 for (i = 0; i < p_mco; ++i)
9090 (void)vim_memset(new_ScreenLinesC[i]
9091 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 0, (size_t)Columns * sizeof(u8char_T));
9093 }
9094 if (enc_dbcs == DBCS_JPNU)
9095 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9096 0, (size_t)Columns * sizeof(schar_T));
9097#endif
9098 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9099 0, (size_t)Columns * sizeof(sattr_T));
9100 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009101 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 {
9103 if (screen_Columns < Columns)
9104 len = screen_Columns;
9105 else
9106 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009107#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009108 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009109 * may be invalid now. Also when p_mco changes. */
9110 if (!(enc_utf8 && ScreenLinesUC == NULL)
9111 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009112#endif
9113 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9114 ScreenLines + LineOffset[old_row],
9115 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009117 if (enc_utf8 && ScreenLinesUC != NULL
9118 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 {
9120 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9121 ScreenLinesUC + LineOffset[old_row],
9122 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009123 for (i = 0; i < p_mco; ++i)
9124 mch_memmove(new_ScreenLinesC[i]
9125 + new_LineOffset[new_row],
9126 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 (size_t)len * sizeof(u8char_T));
9128 }
9129 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9130 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9131 ScreenLines2 + LineOffset[old_row],
9132 (size_t)len * sizeof(schar_T));
9133#endif
9134 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9135 ScreenAttrs + LineOffset[old_row],
9136 (size_t)len * sizeof(sattr_T));
9137 }
9138 }
9139 }
9140 /* Use the last line of the screen for the current line. */
9141 current_ScreenLine = new_ScreenLines + Rows * Columns;
9142 }
9143
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009144 free_screenlines();
9145
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 ScreenLines = new_ScreenLines;
9147#ifdef FEAT_MBYTE
9148 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009149 for (i = 0; i < p_mco; ++i)
9150 ScreenLinesC[i] = new_ScreenLinesC[i];
9151 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 ScreenLines2 = new_ScreenLines2;
9153#endif
9154 ScreenAttrs = new_ScreenAttrs;
9155 LineOffset = new_LineOffset;
9156 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009157 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158
9159 /* It's important that screen_Rows and screen_Columns reflect the actual
9160 * size of ScreenLines[]. Set them before calling anything. */
9161#ifdef FEAT_GUI
9162 old_Rows = screen_Rows;
9163#endif
9164 screen_Rows = Rows;
9165 screen_Columns = Columns;
9166
9167 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009168 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 screenclear2();
9170
9171#ifdef FEAT_GUI
9172 else if (gui.in_use
9173 && !gui.starting
9174 && ScreenLines != NULL
9175 && old_Rows != Rows)
9176 {
9177 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9178 /*
9179 * Adjust the position of the cursor, for when executing an external
9180 * command.
9181 */
9182 if (msg_row >= Rows) /* Rows got smaller */
9183 msg_row = Rows - 1; /* put cursor at last row */
9184 else if (Rows > old_Rows) /* Rows got bigger */
9185 msg_row += Rows - old_Rows; /* put cursor in same place */
9186 if (msg_col >= Columns) /* Columns got smaller */
9187 msg_col = Columns - 1; /* put cursor at last column */
9188 }
9189#endif
9190
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009192 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009193
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009194 /*
9195 * Do not apply autocommands more than 3 times to avoid an endless loop
9196 * in case applying autocommands always changes Rows or Columns.
9197 */
9198 if (starting == 0 && ++retry_count <= 3)
9199 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009200 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009201 /* In rare cases, autocommands may have altered Rows or Columns,
9202 * jump back to check if we need to allocate the screen again. */
9203 goto retry;
9204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205}
9206
9207 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009208free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009209{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009210#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009211 int i;
9212
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009213 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009214 for (i = 0; i < Screen_mco; ++i)
9215 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009216 vim_free(ScreenLines2);
9217#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009218 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009219 vim_free(ScreenAttrs);
9220 vim_free(LineOffset);
9221 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009222 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009223}
9224
9225 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009226screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227{
9228 check_for_delay(FALSE);
9229 screenalloc(FALSE); /* allocate screen buffers if size changed */
9230 screenclear2(); /* clear the screen */
9231}
9232
9233 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009234screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
9236 int i;
9237
9238 if (starting == NO_SCREEN || ScreenLines == NULL
9239#ifdef FEAT_GUI
9240 || (gui.in_use && gui.starting)
9241#endif
9242 )
9243 return;
9244
9245#ifdef FEAT_GUI
9246 if (!gui.in_use)
9247#endif
9248 screen_attr = -1; /* force setting the Normal colors */
9249 screen_stop_highlight(); /* don't want highlighting here */
9250
9251#ifdef FEAT_CLIPBOARD
9252 /* disable selection without redrawing it */
9253 clip_scroll_selection(9999);
9254#endif
9255
9256 /* blank out ScreenLines */
9257 for (i = 0; i < Rows; ++i)
9258 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009259 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 LineWraps[i] = FALSE;
9261 }
9262
9263 if (can_clear(T_CL))
9264 {
9265 out_str(T_CL); /* clear the display */
9266 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009267 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 }
9269 else
9270 {
9271 /* can't clear the screen, mark all chars with invalid attributes */
9272 for (i = 0; i < Rows; ++i)
9273 lineinvalid(LineOffset[i], (int)Columns);
9274 clear_cmdline = TRUE;
9275 }
9276
9277 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9278
9279 win_rest_invalid(firstwin);
9280 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009281 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 if (must_redraw == CLEAR) /* no need to clear again */
9283 must_redraw = NOT_VALID;
9284 compute_cmdrow();
9285 msg_row = cmdline_row; /* put cursor on last line for messages */
9286 msg_col = 0;
9287 screen_start(); /* don't know where cursor is now */
9288 msg_scrolled = 0; /* can't scroll back */
9289 msg_didany = FALSE;
9290 msg_didout = FALSE;
9291}
9292
9293/*
9294 * Clear one line in ScreenLines.
9295 */
9296 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009297lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298{
9299 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9300#ifdef FEAT_MBYTE
9301 if (enc_utf8)
9302 (void)vim_memset(ScreenLinesUC + off, 0,
9303 (size_t)width * sizeof(u8char_T));
9304#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009305 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306}
9307
9308/*
9309 * Mark one line in ScreenLines invalid by setting the attributes to an
9310 * invalid value.
9311 */
9312 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009313lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9316}
9317
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318/*
9319 * Copy part of a Screenline for vertically split window "wp".
9320 */
9321 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009322linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323{
9324 unsigned off_to = LineOffset[to] + wp->w_wincol;
9325 unsigned off_from = LineOffset[from] + wp->w_wincol;
9326
9327 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9328 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009329#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 if (enc_utf8)
9331 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009332 int i;
9333
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9335 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009336 for (i = 0; i < p_mco; ++i)
9337 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9338 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 }
9340 if (enc_dbcs == DBCS_JPNU)
9341 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9342 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9345 wp->w_width * sizeof(sattr_T));
9346}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348/*
9349 * Return TRUE if clearing with term string "p" would work.
9350 * It can't work when the string is empty or it won't set the right background.
9351 */
9352 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009353can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354{
9355 return (*p != NUL && (t_colors <= 1
9356#ifdef FEAT_GUI
9357 || gui.in_use
9358#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009359#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009360 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009361 || (!p_tgc && cterm_normal_bg_color == 0)
9362#else
9363 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009364#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009365 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366}
9367
9368/*
9369 * Reset cursor position. Use whenever cursor was moved because of outputting
9370 * something directly to the screen (shell commands) or a terminal control
9371 * code.
9372 */
9373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009374screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
9376 screen_cur_row = screen_cur_col = 9999;
9377}
9378
9379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 * Move the cursor to position "row","col" in the screen.
9381 * This tries to find the most efficient way to move, minimizing the number of
9382 * characters sent to the terminal.
9383 */
9384 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009385windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009387 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 int i;
9389 int plan;
9390 int cost;
9391 int wouldbe_col;
9392 int noinvcurs;
9393 char_u *bs;
9394 int goto_cost;
9395 int attr;
9396
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009397#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9399
9400#define PLAN_LE 1
9401#define PLAN_CR 2
9402#define PLAN_NL 3
9403#define PLAN_WRITE 4
9404 /* Can't use ScreenLines unless initialized */
9405 if (ScreenLines == NULL)
9406 return;
9407
9408 if (col != screen_cur_col || row != screen_cur_row)
9409 {
9410 /* Check for valid position. */
9411 if (row < 0) /* window without text lines? */
9412 row = 0;
9413 if (row >= screen_Rows)
9414 row = screen_Rows - 1;
9415 if (col >= screen_Columns)
9416 col = screen_Columns - 1;
9417
9418 /* check if no cursor movement is allowed in highlight mode */
9419 if (screen_attr && *T_MS == NUL)
9420 noinvcurs = HIGHL_COST;
9421 else
9422 noinvcurs = 0;
9423 goto_cost = GOTO_COST + noinvcurs;
9424
9425 /*
9426 * Plan how to do the positioning:
9427 * 1. Use CR to move it to column 0, same row.
9428 * 2. Use T_LE to move it a few columns to the left.
9429 * 3. Use NL to move a few lines down, column 0.
9430 * 4. Move a few columns to the right with T_ND or by writing chars.
9431 *
9432 * Don't do this if the cursor went beyond the last column, the cursor
9433 * position is unknown then (some terminals wrap, some don't )
9434 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009435 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 * characters to move the cursor to the right.
9437 */
9438 if (row >= screen_cur_row && screen_cur_col < Columns)
9439 {
9440 /*
9441 * If the cursor is in the same row, bigger col, we can use CR
9442 * or T_LE.
9443 */
9444 bs = NULL; /* init for GCC */
9445 attr = screen_attr;
9446 if (row == screen_cur_row && col < screen_cur_col)
9447 {
9448 /* "le" is preferred over "bc", because "bc" is obsolete */
9449 if (*T_LE)
9450 bs = T_LE; /* "cursor left" */
9451 else
9452 bs = T_BC; /* "backspace character (old) */
9453 if (*bs)
9454 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9455 else
9456 cost = 999;
9457 if (col + 1 < cost) /* using CR is less characters */
9458 {
9459 plan = PLAN_CR;
9460 wouldbe_col = 0;
9461 cost = 1; /* CR is just one character */
9462 }
9463 else
9464 {
9465 plan = PLAN_LE;
9466 wouldbe_col = col;
9467 }
9468 if (noinvcurs) /* will stop highlighting */
9469 {
9470 cost += noinvcurs;
9471 attr = 0;
9472 }
9473 }
9474
9475 /*
9476 * If the cursor is above where we want to be, we can use CR LF.
9477 */
9478 else if (row > screen_cur_row)
9479 {
9480 plan = PLAN_NL;
9481 wouldbe_col = 0;
9482 cost = (row - screen_cur_row) * 2; /* CR LF */
9483 if (noinvcurs) /* will stop highlighting */
9484 {
9485 cost += noinvcurs;
9486 attr = 0;
9487 }
9488 }
9489
9490 /*
9491 * If the cursor is in the same row, smaller col, just use write.
9492 */
9493 else
9494 {
9495 plan = PLAN_WRITE;
9496 wouldbe_col = screen_cur_col;
9497 cost = 0;
9498 }
9499
9500 /*
9501 * Check if any characters that need to be written have the
9502 * correct attributes. Also avoid UTF-8 characters.
9503 */
9504 i = col - wouldbe_col;
9505 if (i > 0)
9506 cost += i;
9507 if (cost < goto_cost && i > 0)
9508 {
9509 /*
9510 * Check if the attributes are correct without additionally
9511 * stopping highlighting.
9512 */
9513 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9514 while (i && *p++ == attr)
9515 --i;
9516 if (i != 0)
9517 {
9518 /*
9519 * Try if it works when highlighting is stopped here.
9520 */
9521 if (*--p == 0)
9522 {
9523 cost += noinvcurs;
9524 while (i && *p++ == 0)
9525 --i;
9526 }
9527 if (i != 0)
9528 cost = 999; /* different attributes, don't do it */
9529 }
9530#ifdef FEAT_MBYTE
9531 if (enc_utf8)
9532 {
9533 /* Don't use an UTF-8 char for positioning, it's slow. */
9534 for (i = wouldbe_col; i < col; ++i)
9535 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9536 {
9537 cost = 999;
9538 break;
9539 }
9540 }
9541#endif
9542 }
9543
9544 /*
9545 * We can do it without term_windgoto()!
9546 */
9547 if (cost < goto_cost)
9548 {
9549 if (plan == PLAN_LE)
9550 {
9551 if (noinvcurs)
9552 screen_stop_highlight();
9553 while (screen_cur_col > col)
9554 {
9555 out_str(bs);
9556 --screen_cur_col;
9557 }
9558 }
9559 else if (plan == PLAN_CR)
9560 {
9561 if (noinvcurs)
9562 screen_stop_highlight();
9563 out_char('\r');
9564 screen_cur_col = 0;
9565 }
9566 else if (plan == PLAN_NL)
9567 {
9568 if (noinvcurs)
9569 screen_stop_highlight();
9570 while (screen_cur_row < row)
9571 {
9572 out_char('\n');
9573 ++screen_cur_row;
9574 }
9575 screen_cur_col = 0;
9576 }
9577
9578 i = col - screen_cur_col;
9579 if (i > 0)
9580 {
9581 /*
9582 * Use cursor-right if it's one character only. Avoids
9583 * removing a line of pixels from the last bold char, when
9584 * using the bold trick in the GUI.
9585 */
9586 if (T_ND[0] != NUL && T_ND[1] == NUL)
9587 {
9588 while (i-- > 0)
9589 out_char(*T_ND);
9590 }
9591 else
9592 {
9593 int off;
9594
9595 off = LineOffset[row] + screen_cur_col;
9596 while (i-- > 0)
9597 {
9598 if (ScreenAttrs[off] != screen_attr)
9599 screen_stop_highlight();
9600#ifdef FEAT_MBYTE
9601 out_flush_check();
9602#endif
9603 out_char(ScreenLines[off]);
9604#ifdef FEAT_MBYTE
9605 if (enc_dbcs == DBCS_JPNU
9606 && ScreenLines[off] == 0x8e)
9607 out_char(ScreenLines2[off]);
9608#endif
9609 ++off;
9610 }
9611 }
9612 }
9613 }
9614 }
9615 else
9616 cost = 999;
9617
9618 if (cost >= goto_cost)
9619 {
9620 if (noinvcurs)
9621 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009622 if (row == screen_cur_row && (col > screen_cur_col)
9623 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 term_cursor_right(col - screen_cur_col);
9625 else
9626 term_windgoto(row, col);
9627 }
9628 screen_cur_row = row;
9629 screen_cur_col = col;
9630 }
9631}
9632
9633/*
9634 * Set cursor to its position in the current window.
9635 */
9636 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009637setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009639 setcursor_mayforce(FALSE);
9640}
9641
9642/*
9643 * Set cursor to its position in the current window.
9644 * When "force" is TRUE also when not redrawing.
9645 */
9646 void
9647setcursor_mayforce(int force)
9648{
9649 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 {
9651 validate_cursor();
9652 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009653 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009655 /* With 'rightleft' set and the cursor on a double-wide
9656 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009657 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009659 (has_mbyte
9660 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9661 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662# endif
9663 1)) :
9664#endif
9665 curwin->w_wcol));
9666 }
9667}
9668
9669
9670/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009671 * Insert 'line_count' lines at 'row' in window 'wp'.
9672 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9673 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 * scrolling.
9675 * Returns FAIL if the lines are not inserted, OK for success.
9676 */
9677 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009678win_ins_lines(
9679 win_T *wp,
9680 int row,
9681 int line_count,
9682 int invalid,
9683 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685 int did_delete;
9686 int nextrow;
9687 int lastrow;
9688 int retval;
9689
9690 if (invalid)
9691 wp->w_lines_valid = 0;
9692
9693 if (wp->w_height < 5)
9694 return FAIL;
9695
9696 if (line_count > wp->w_height - row)
9697 line_count = wp->w_height - row;
9698
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009699 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 if (retval != MAYBE)
9701 return retval;
9702
9703 /*
9704 * If there is a next window or a status line, we first try to delete the
9705 * lines at the bottom to avoid messing what is after the window.
9706 * If this fails and there are following windows, don't do anything to avoid
9707 * messing up those windows, better just redraw.
9708 */
9709 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 if (wp->w_next != NULL || wp->w_status_height)
9711 {
9712 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009713 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 did_delete = TRUE;
9715 else if (wp->w_next)
9716 return FAIL;
9717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 /*
9719 * if no lines deleted, blank the lines that will end up below the window
9720 */
9721 if (!did_delete)
9722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009725 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 lastrow = nextrow + line_count;
9727 if (lastrow > Rows)
9728 lastrow = Rows;
9729 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009730 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 ' ', ' ', 0);
9732 }
9733
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009734 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 == FAIL)
9736 {
9737 /* deletion will have messed up other windows */
9738 if (did_delete)
9739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 win_rest_invalid(W_NEXT(wp));
9742 }
9743 return FAIL;
9744 }
9745
9746 return OK;
9747}
9748
9749/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009750 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9752 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9753 * scrolling
9754 * Return OK for success, FAIL if the lines are not deleted.
9755 */
9756 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009757win_del_lines(
9758 win_T *wp,
9759 int row,
9760 int line_count,
9761 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009762 int mayclear,
9763 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764{
9765 int retval;
9766
9767 if (invalid)
9768 wp->w_lines_valid = 0;
9769
9770 if (line_count > wp->w_height - row)
9771 line_count = wp->w_height - row;
9772
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009773 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 if (retval != MAYBE)
9775 return retval;
9776
9777 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009778 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 return FAIL;
9780
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 /*
9782 * If there are windows or status lines below, try to put them at the
9783 * correct place. If we can't do that, they have to be redrawn.
9784 */
9785 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9786 {
9787 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009788 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 {
9790 wp->w_redr_status = TRUE;
9791 win_rest_invalid(wp->w_next);
9792 }
9793 }
9794 /*
9795 * If this is the last window and there is no status line, redraw the
9796 * command line later.
9797 */
9798 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 redraw_cmdline = TRUE;
9800 return OK;
9801}
9802
9803/*
9804 * Common code for win_ins_lines() and win_del_lines().
9805 * Returns OK or FAIL when the work has been done.
9806 * Returns MAYBE when not finished yet.
9807 */
9808 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009809win_do_lines(
9810 win_T *wp,
9811 int row,
9812 int line_count,
9813 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009814 int del,
9815 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
9817 int retval;
9818
9819 if (!redrawing() || line_count <= 0)
9820 return FAIL;
9821
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009822 /* When inserting lines would result in loss of command output, just redraw
9823 * the lines. */
9824 if (no_win_do_lines_ins && !del)
9825 return FAIL;
9826
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009828 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009830 if (!no_win_do_lines_ins)
9831 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 return FAIL;
9833 }
9834
9835 /*
9836 * Delete all remaining lines
9837 */
9838 if (row + line_count >= wp->w_height)
9839 {
9840 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009841 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 ' ', ' ', 0);
9843 return OK;
9844 }
9845
9846 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009847 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009849 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009851 if (!no_win_do_lines_ins)
9852 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853
9854 /*
9855 * If the terminal can set a scroll region, use that.
9856 * Always do this in a vertically split window. This will redraw from
9857 * ScreenLines[] when t_CV isn't defined. That's faster than using
9858 * win_line().
9859 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009860 * a character in the lower right corner of the scroll region may cause a
9861 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009863 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 scroll_region_set(wp, row);
9867 if (del)
9868 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009869 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 else
9871 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009872 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 scroll_region_reset();
9875 return retval;
9876 }
9877
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9879 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880
9881 return MAYBE;
9882}
9883
9884/*
9885 * window 'wp' and everything after it is messed up, mark it for redraw
9886 */
9887 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009888win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 {
9892 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 wp->w_redr_status = TRUE;
9894 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 }
9896 redraw_cmdline = TRUE;
9897}
9898
9899/*
9900 * The rest of the routines in this file perform screen manipulations. The
9901 * given operation is performed physically on the screen. The corresponding
9902 * change is also made to the internal screen image. In this way, the editor
9903 * anticipates the effect of editing changes on the appearance of the screen.
9904 * That way, when we call screenupdate a complete redraw isn't usually
9905 * necessary. Another advantage is that we can keep adding code to anticipate
9906 * screen changes, and in the meantime, everything still works.
9907 */
9908
9909/*
9910 * types for inserting or deleting lines
9911 */
9912#define USE_T_CAL 1
9913#define USE_T_CDL 2
9914#define USE_T_AL 3
9915#define USE_T_CE 4
9916#define USE_T_DL 5
9917#define USE_T_SR 6
9918#define USE_NL 7
9919#define USE_T_CD 8
9920#define USE_REDRAW 9
9921
9922/*
9923 * insert lines on the screen and update ScreenLines[]
9924 * 'end' is the line after the scrolled part. Normally it is Rows.
9925 * When scrolling region used 'off' is the offset from the top for the region.
9926 * 'row' and 'end' are relative to the start of the region.
9927 *
9928 * return FAIL for failure, OK for success.
9929 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009930 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009931screen_ins_lines(
9932 int off,
9933 int row,
9934 int line_count,
9935 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009936 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009937 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938{
9939 int i;
9940 int j;
9941 unsigned temp;
9942 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009943 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 int type;
9945 int result_empty;
9946 int can_ce = can_clear(T_CE);
9947
9948 /*
9949 * FAIL if
9950 * - there is no valid screen
9951 * - the screen has to be redrawn completely
9952 * - the line count is less than one
9953 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009954 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009956 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9957#ifdef FEAT_CLIPBOARD
9958 || (clip_star.state != SELECT_CLEARED
9959 && redrawing_for_callback > 0)
9960#endif
9961 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 return FAIL;
9963
9964 /*
9965 * There are seven ways to insert lines:
9966 * 0. When in a vertically split window and t_CV isn't set, redraw the
9967 * characters from ScreenLines[].
9968 * 1. Use T_CD (clear to end of display) if it exists and the result of
9969 * the insert is just empty lines
9970 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9971 * present or line_count > 1. It looks better if we do all the inserts
9972 * at once.
9973 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9974 * insert is just empty lines and T_CE is not present or line_count >
9975 * 1.
9976 * 4. Use T_AL (insert line) if it exists.
9977 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9978 * just empty lines.
9979 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9980 * just empty lines.
9981 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9982 * the 'da' flag is not set or we have clear line capability.
9983 * 8. redraw the characters from ScreenLines[].
9984 *
9985 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9986 * the scrollbar for the window. It does have insert line, use that if it
9987 * exists.
9988 */
9989 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9991 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009992 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 type = USE_T_CD;
9994 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9995 type = USE_T_CAL;
9996 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9997 type = USE_T_CDL;
9998 else if (*T_AL != NUL)
9999 type = USE_T_AL;
10000 else if (can_ce && result_empty)
10001 type = USE_T_CE;
10002 else if (*T_DL != NUL && result_empty)
10003 type = USE_T_DL;
10004 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10005 type = USE_T_SR;
10006 else
10007 return FAIL;
10008
10009 /*
10010 * For clearing the lines screen_del_lines() is used. This will also take
10011 * care of t_db if necessary.
10012 */
10013 if (type == USE_T_CD || type == USE_T_CDL ||
10014 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010015 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016
10017 /*
10018 * If text is retained below the screen, first clear or delete as many
10019 * lines at the bottom of the window as are about to be inserted so that
10020 * the deleted lines won't later surface during a screen_del_lines.
10021 */
10022 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010023 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024
10025#ifdef FEAT_CLIPBOARD
10026 /* Remove a modeless selection when inserting lines halfway the screen
10027 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010028 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010029 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 else
10031 clip_scroll_selection(-line_count);
10032#endif
10033
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034#ifdef FEAT_GUI
10035 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10036 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010037 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038#endif
10039
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010040 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10041 cursor_col = wp->w_wincol;
10042
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 if (*T_CCS != NUL) /* cursor relative to region */
10044 cursor_row = row;
10045 else
10046 cursor_row = row + off;
10047
10048 /*
10049 * Shift LineOffset[] line_count down to reflect the inserted lines.
10050 * Clear the inserted lines in ScreenLines[].
10051 */
10052 row += off;
10053 end += off;
10054 for (i = 0; i < line_count; ++i)
10055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 if (wp != NULL && wp->w_width != Columns)
10057 {
10058 /* need to copy part of a line */
10059 j = end - 1 - i;
10060 while ((j -= line_count) >= row)
10061 linecopy(j + line_count, j, wp);
10062 j += line_count;
10063 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010064 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10065 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 else
10067 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10068 LineWraps[j] = FALSE;
10069 }
10070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 {
10072 j = end - 1 - i;
10073 temp = LineOffset[j];
10074 while ((j -= line_count) >= row)
10075 {
10076 LineOffset[j + line_count] = LineOffset[j];
10077 LineWraps[j + line_count] = LineWraps[j];
10078 }
10079 LineOffset[j + line_count] = temp;
10080 LineWraps[j + line_count] = FALSE;
10081 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010082 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 else
10084 lineinvalid(temp, (int)Columns);
10085 }
10086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
10088 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010089 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010090 if (clear_attr != 0)
10091 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 /* redraw the characters */
10094 if (type == USE_REDRAW)
10095 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010096 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 {
10098 term_append_lines(line_count);
10099 screen_start(); /* don't know where cursor is now */
10100 }
10101 else
10102 {
10103 for (i = 0; i < line_count; i++)
10104 {
10105 if (type == USE_T_AL)
10106 {
10107 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010108 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 out_str(T_AL);
10110 }
10111 else /* type == USE_T_SR */
10112 out_str(T_SR);
10113 screen_start(); /* don't know where cursor is now */
10114 }
10115 }
10116
10117 /*
10118 * With scroll-reverse and 'da' flag set we need to clear the lines that
10119 * have been scrolled down into the region.
10120 */
10121 if (type == USE_T_SR && *T_DA)
10122 {
10123 for (i = 0; i < line_count; ++i)
10124 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010125 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 out_str(T_CE);
10127 screen_start(); /* don't know where cursor is now */
10128 }
10129 }
10130
10131#ifdef FEAT_GUI
10132 gui_can_update_cursor();
10133 if (gui.in_use)
10134 out_flush(); /* always flush after a scroll */
10135#endif
10136 return OK;
10137}
10138
10139/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010140 * Delete lines on the screen and update ScreenLines[].
10141 * "end" is the line after the scrolled part. Normally it is Rows.
10142 * When scrolling region used "off" is the offset from the top for the region.
10143 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 *
10145 * Return OK for success, FAIL if the lines are not deleted.
10146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010148screen_del_lines(
10149 int off,
10150 int row,
10151 int line_count,
10152 int end,
10153 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010154 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010155 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156{
10157 int j;
10158 int i;
10159 unsigned temp;
10160 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010161 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 int cursor_end;
10163 int result_empty; /* result is empty until end of region */
10164 int can_delete; /* deleting line codes can be used */
10165 int type;
10166
10167 /*
10168 * FAIL if
10169 * - there is no valid screen
10170 * - the screen has to be redrawn completely
10171 * - the line count is less than one
10172 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010173 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010175 if (!screen_valid(TRUE) || line_count <= 0
10176 || (!force && line_count > p_ttyscroll)
10177#ifdef FEAT_CLIPBOARD
10178 || (clip_star.state != SELECT_CLEARED
10179 && redrawing_for_callback > 0)
10180#endif
10181 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 return FAIL;
10183
10184 /*
10185 * Check if the rest of the current region will become empty.
10186 */
10187 result_empty = row + line_count >= end;
10188
10189 /*
10190 * We can delete lines only when 'db' flag not set or when 'ce' option
10191 * available.
10192 */
10193 can_delete = (*T_DB == NUL || can_clear(T_CE));
10194
10195 /*
10196 * There are six ways to delete lines:
10197 * 0. When in a vertically split window and t_CV isn't set, redraw the
10198 * characters from ScreenLines[].
10199 * 1. Use T_CD if it exists and the result is empty.
10200 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10201 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10202 * none of the other ways work.
10203 * 4. Use T_CE (erase line) if the result is empty.
10204 * 5. Use T_DL (delete line) if it exists.
10205 * 6. redraw the characters from ScreenLines[].
10206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10208 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010209 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 type = USE_T_CD;
10211#if defined(__BEOS__) && defined(BEOS_DR8)
10212 /*
10213 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10214 * its internal termcap... this works okay for tests which test *T_DB !=
10215 * NUL. It has the disadvantage that the user cannot use any :set t_*
10216 * command to get T_DB (back) to empty_option, only :set term=... will do
10217 * the trick...
10218 * Anyway, this hack will hopefully go away with the next OS release.
10219 * (Olaf Seibert)
10220 */
10221 else if (row == 0 && T_DB == empty_option
10222 && (line_count == 1 || *T_CDL == NUL))
10223#else
10224 else if (row == 0 && (
10225#ifndef AMIGA
10226 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10227 * up, so use delete-line command */
10228 line_count == 1 ||
10229#endif
10230 *T_CDL == NUL))
10231#endif
10232 type = USE_NL;
10233 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10234 type = USE_T_CDL;
10235 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010236 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 type = USE_T_CE;
10238 else if (*T_DL != NUL && can_delete)
10239 type = USE_T_DL;
10240 else if (*T_CDL != NUL && can_delete)
10241 type = USE_T_CDL;
10242 else
10243 return FAIL;
10244
10245#ifdef FEAT_CLIPBOARD
10246 /* Remove a modeless selection when deleting lines halfway the screen or
10247 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010248 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010249 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 else
10251 clip_scroll_selection(line_count);
10252#endif
10253
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254#ifdef FEAT_GUI
10255 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10256 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010257 gui_dont_update_cursor(gui.cursor_row >= row + off
10258 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259#endif
10260
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010261 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10262 cursor_col = wp->w_wincol;
10263
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 if (*T_CCS != NUL) /* cursor relative to region */
10265 {
10266 cursor_row = row;
10267 cursor_end = end;
10268 }
10269 else
10270 {
10271 cursor_row = row + off;
10272 cursor_end = end + off;
10273 }
10274
10275 /*
10276 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10277 * Clear the inserted lines in ScreenLines[].
10278 */
10279 row += off;
10280 end += off;
10281 for (i = 0; i < line_count; ++i)
10282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 if (wp != NULL && wp->w_width != Columns)
10284 {
10285 /* need to copy part of a line */
10286 j = row + i;
10287 while ((j += line_count) <= end - 1)
10288 linecopy(j - line_count, j, wp);
10289 j -= line_count;
10290 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010291 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10292 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 else
10294 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10295 LineWraps[j] = FALSE;
10296 }
10297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 {
10299 /* whole width, moving the line pointers is faster */
10300 j = row + i;
10301 temp = LineOffset[j];
10302 while ((j += line_count) <= end - 1)
10303 {
10304 LineOffset[j - line_count] = LineOffset[j];
10305 LineWraps[j - line_count] = LineWraps[j];
10306 }
10307 LineOffset[j - line_count] = temp;
10308 LineWraps[j - line_count] = FALSE;
10309 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010310 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 else
10312 lineinvalid(temp, (int)Columns);
10313 }
10314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010316 if (screen_attr != clear_attr)
10317 screen_stop_highlight();
10318 if (clear_attr != 0)
10319 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 /* redraw the characters */
10322 if (type == USE_REDRAW)
10323 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010324 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010326 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 out_str(T_CD);
10328 screen_start(); /* don't know where cursor is now */
10329 }
10330 else if (type == USE_T_CDL)
10331 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010332 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 term_delete_lines(line_count);
10334 screen_start(); /* don't know where cursor is now */
10335 }
10336 /*
10337 * Deleting lines at top of the screen or scroll region: Just scroll
10338 * the whole screen (scroll region) up by outputting newlines on the
10339 * last line.
10340 */
10341 else if (type == USE_NL)
10342 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010343 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 for (i = line_count; --i >= 0; )
10345 out_char('\n'); /* cursor will remain on same line */
10346 }
10347 else
10348 {
10349 for (i = line_count; --i >= 0; )
10350 {
10351 if (type == USE_T_DL)
10352 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010353 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 out_str(T_DL); /* delete a line */
10355 }
10356 else /* type == USE_T_CE */
10357 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010358 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 out_str(T_CE); /* erase a line */
10360 }
10361 screen_start(); /* don't know where cursor is now */
10362 }
10363 }
10364
10365 /*
10366 * If the 'db' flag is set, we need to clear the lines that have been
10367 * scrolled up at the bottom of the region.
10368 */
10369 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10370 {
10371 for (i = line_count; i > 0; --i)
10372 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010373 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 out_str(T_CE); /* erase a line */
10375 screen_start(); /* don't know where cursor is now */
10376 }
10377 }
10378
10379#ifdef FEAT_GUI
10380 gui_can_update_cursor();
10381 if (gui.in_use)
10382 out_flush(); /* always flush after a scroll */
10383#endif
10384
10385 return OK;
10386}
10387
10388/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010389 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 *
10391 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10392 * If clear_cmdline is FALSE there may be a message there that needs to be
10393 * cleared only if a mode is shown.
10394 * Return the length of the message (0 if no message).
10395 */
10396 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010397showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398{
10399 int need_clear;
10400 int length = 0;
10401 int do_mode;
10402 int attr;
10403 int nwr_save;
10404#ifdef FEAT_INS_EXPAND
10405 int sub_attr;
10406#endif
10407
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010408 do_mode = ((p_smd && msg_silent == 0)
10409 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010410 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010411 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010412 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 {
10414 /*
10415 * Don't show mode right now, when not redrawing or inside a mapping.
10416 * Call char_avail() only when we are going to show something, because
10417 * it takes a bit of time.
10418 */
10419 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10420 {
10421 redraw_cmdline = TRUE; /* show mode later */
10422 return 0;
10423 }
10424
10425 nwr_save = need_wait_return;
10426
10427 /* wait a bit before overwriting an important message */
10428 check_for_delay(FALSE);
10429
10430 /* if the cmdline is more than one line high, erase top lines */
10431 need_clear = clear_cmdline;
10432 if (clear_cmdline && cmdline_row < Rows - 1)
10433 msg_clr_cmdline(); /* will reset clear_cmdline */
10434
10435 /* Position on the last line in the window, column 0 */
10436 msg_pos_mode();
10437 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010438 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 if (do_mode)
10440 {
10441 MSG_PUTS_ATTR("--", attr);
10442#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010443 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010444# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010445 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010446# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010447 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010448# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010449 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010450# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 MSG_PUTS_ATTR(" IM", attr);
10452# else
10453 MSG_PUTS_ATTR(" XIM", attr);
10454# endif
10455#endif
10456#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10457 if (gui.in_use)
10458 {
10459 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010460 {
10461 /* HANGUL */
10462 if (enc_utf8)
10463 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10464 else
10465 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 }
10468#endif
10469#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010470 /* CTRL-X in Insert mode */
10471 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 {
10473 /* These messages can get long, avoid a wrap in a narrow
10474 * window. Prefer showing edit_submode_extra. */
10475 length = (Rows - msg_row) * Columns - 3;
10476 if (edit_submode_extra != NULL)
10477 length -= vim_strsize(edit_submode_extra);
10478 if (length > 0)
10479 {
10480 if (edit_submode_pre != NULL)
10481 length -= vim_strsize(edit_submode_pre);
10482 if (length - vim_strsize(edit_submode) > 0)
10483 {
10484 if (edit_submode_pre != NULL)
10485 msg_puts_attr(edit_submode_pre, attr);
10486 msg_puts_attr(edit_submode, attr);
10487 }
10488 if (edit_submode_extra != NULL)
10489 {
10490 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10491 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010492 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 else
10494 sub_attr = attr;
10495 msg_puts_attr(edit_submode_extra, sub_attr);
10496 }
10497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 }
10499 else
10500#endif
10501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 if (State & VREPLACE_FLAG)
10503 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010504 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10506 else if (State & INSERT)
10507 {
10508#ifdef FEAT_RIGHTLEFT
10509 if (p_ri)
10510 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10511#endif
10512 MSG_PUTS_ATTR(_(" INSERT"), attr);
10513 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010514 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 MSG_PUTS_ATTR(_(" (insert)"), attr);
10516 else if (restart_edit == 'R')
10517 MSG_PUTS_ATTR(_(" (replace)"), attr);
10518 else if (restart_edit == 'V')
10519 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10520#ifdef FEAT_RIGHTLEFT
10521 if (p_hkmap)
10522 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10523# ifdef FEAT_FKMAP
10524 if (p_fkmap)
10525 MSG_PUTS_ATTR(farsi_text_5, attr);
10526# endif
10527#endif
10528#ifdef FEAT_KEYMAP
10529 if (State & LANGMAP)
10530 {
10531# ifdef FEAT_ARABIC
10532 if (curwin->w_p_arab)
10533 MSG_PUTS_ATTR(_(" Arabic"), attr);
10534 else
10535# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010536 if (get_keymap_str(curwin, (char_u *)" (%s)",
10537 NameBuff, MAXPATHL))
10538 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 }
10540#endif
10541 if ((State & INSERT) && p_paste)
10542 MSG_PUTS_ATTR(_(" (paste)"), attr);
10543
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 if (VIsual_active)
10545 {
10546 char *p;
10547
10548 /* Don't concatenate separate words to avoid translation
10549 * problems. */
10550 switch ((VIsual_select ? 4 : 0)
10551 + (VIsual_mode == Ctrl_V) * 2
10552 + (VIsual_mode == 'V'))
10553 {
10554 case 0: p = N_(" VISUAL"); break;
10555 case 1: p = N_(" VISUAL LINE"); break;
10556 case 2: p = N_(" VISUAL BLOCK"); break;
10557 case 4: p = N_(" SELECT"); break;
10558 case 5: p = N_(" SELECT LINE"); break;
10559 default: p = N_(" SELECT BLOCK"); break;
10560 }
10561 MSG_PUTS_ATTR(_(p), attr);
10562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 MSG_PUTS_ATTR(" --", attr);
10564 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010565
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 need_clear = TRUE;
10567 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010568 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569#ifdef FEAT_INS_EXPAND
10570 && edit_submode == NULL /* otherwise it gets too long */
10571#endif
10572 )
10573 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010574 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 need_clear = TRUE;
10576 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010577
10578 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 if (need_clear || clear_cmdline)
10580 msg_clr_eos();
10581 msg_didout = FALSE; /* overwrite this message */
10582 length = msg_col;
10583 msg_col = 0;
10584 need_wait_return = nwr_save; /* never ask for hit-return for this */
10585 }
10586 else if (clear_cmdline && msg_silent == 0)
10587 /* Clear the whole command line. Will reset "clear_cmdline". */
10588 msg_clr_cmdline();
10589
10590#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 /* In Visual mode the size of the selected area must be redrawn. */
10592 if (VIsual_active)
10593 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594
10595 /* If the last window has no status line, the ruler is after the mode
10596 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010597 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010598 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599#endif
10600 redraw_cmdline = FALSE;
10601 clear_cmdline = FALSE;
10602
10603 return length;
10604}
10605
10606/*
10607 * Position for a mode message.
10608 */
10609 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010610msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611{
10612 msg_col = 0;
10613 msg_row = Rows - 1;
10614}
10615
10616/*
10617 * Delete mode message. Used when ESC is typed which is expected to end
10618 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010619 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 */
10621 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010622unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623{
10624 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010625 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 */
10627 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10628 redraw_cmdline = TRUE; /* delete mode later */
10629 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010630 clearmode();
10631}
10632
10633/*
10634 * Clear the mode message.
10635 */
10636 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010637clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010638{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010639 int save_msg_row = msg_row;
10640 int save_msg_col = msg_col;
10641
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010642 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010643 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010644 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010645 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010646
10647 msg_col = save_msg_col;
10648 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649}
10650
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010651 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010652recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010653{
10654 MSG_PUTS_ATTR(_("recording"), attr);
10655 if (!shortmess(SHM_RECORDING))
10656 {
10657 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010658 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010659 MSG_PUTS_ATTR(s, attr);
10660 }
10661}
10662
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010663/*
10664 * Draw the tab pages line at the top of the Vim window.
10665 */
10666 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010667draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010668{
10669 int tabcount = 0;
10670 tabpage_T *tp;
10671 int tabwidth;
10672 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010673 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010674 int attr;
10675 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010676 win_T *cwp;
10677 int wincount;
10678 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010679 int c;
10680 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010681 int attr_sel = HL_ATTR(HLF_TPS);
10682 int attr_nosel = HL_ATTR(HLF_TP);
10683 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010684 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010685 int room;
10686 int use_sep_chars = (t_colors < 8
10687#ifdef FEAT_GUI
10688 && !gui.in_use
10689#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010690#ifdef FEAT_TERMGUICOLORS
10691 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010692#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010693 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010694
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010695 if (ScreenLines == NULL)
10696 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010697 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010698
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010699#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010700 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010701 if (gui_use_tabline())
10702 {
10703 gui_update_tabline();
10704 return;
10705 }
10706#endif
10707
10708 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010709 return;
10710
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010711#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010712
10713 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10714 for (scol = 0; scol < Columns; ++scol)
10715 TabPageIdxs[scol] = 0;
10716
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010717 /* Use the 'tabline' option if it's set. */
10718 if (*p_tal != NUL)
10719 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010720 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010721
10722 /* Check for an error. If there is one we would loop in redrawing the
10723 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010724 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010725 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010726 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010727 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010728 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010729 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010730 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010731 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010732#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010733 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010734 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010735 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010736
Bram Moolenaar238a5642006-02-21 22:12:05 +000010737 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10738 if (tabwidth < 6)
10739 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010740
Bram Moolenaar238a5642006-02-21 22:12:05 +000010741 attr = attr_nosel;
10742 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010743 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010744 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10745 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010746 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010747 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010748
Bram Moolenaar238a5642006-02-21 22:12:05 +000010749 if (tp->tp_topframe == topframe)
10750 attr = attr_sel;
10751 if (use_sep_chars && col > 0)
10752 screen_putchar('|', 0, col++, attr);
10753
10754 if (tp->tp_topframe != topframe)
10755 attr = attr_nosel;
10756
10757 screen_putchar(' ', 0, col++, attr);
10758
10759 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010760 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010761 cwp = curwin;
10762 wp = firstwin;
10763 }
10764 else
10765 {
10766 cwp = tp->tp_curwin;
10767 wp = tp->tp_firstwin;
10768 }
10769
10770 modified = FALSE;
10771 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10772 if (bufIsChanged(wp->w_buffer))
10773 modified = TRUE;
10774 if (modified || wincount > 1)
10775 {
10776 if (wincount > 1)
10777 {
10778 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010779 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010780 if (col + len >= Columns - 3)
10781 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010782 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010783#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010784 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010785#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010786 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010787#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010788 );
10789 col += len;
10790 }
10791 if (modified)
10792 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10793 screen_putchar(' ', 0, col++, attr);
10794 }
10795
10796 room = scol - col + tabwidth - 1;
10797 if (room > 0)
10798 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010799 /* Get buffer name in NameBuff[] */
10800 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010801 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010802 len = vim_strsize(NameBuff);
10803 p = NameBuff;
10804#ifdef FEAT_MBYTE
10805 if (has_mbyte)
10806 while (len > room)
10807 {
10808 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010809 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010810 }
10811 else
10812#endif
10813 if (len > room)
10814 {
10815 p += len - room;
10816 len = room;
10817 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010818 if (len > Columns - col - 1)
10819 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010820
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010821 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010822 col += len;
10823 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010824 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010825
10826 /* Store the tab page number in TabPageIdxs[], so that
10827 * jump_to_mouse() knows where each one is. */
10828 ++tabcount;
10829 while (scol < col)
10830 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010831 }
10832
Bram Moolenaar238a5642006-02-21 22:12:05 +000010833 if (use_sep_chars)
10834 c = '_';
10835 else
10836 c = ' ';
10837 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010838
10839 /* Put an "X" for closing the current tab if there are several. */
10840 if (first_tabpage->tp_next != NULL)
10841 {
10842 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10843 TabPageIdxs[Columns - 1] = -999;
10844 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010845 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010846
10847 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10848 * set. */
10849 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010850}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010851
10852/*
10853 * Get buffer name for "buf" into NameBuff[].
10854 * Takes care of special buffer names and translates special characters.
10855 */
10856 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010857get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010858{
10859 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010860 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010861 else
10862 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10863 trans_characters(NameBuff, MAXPATHL);
10864}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010865
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866/*
10867 * Get the character to use in a status line. Get its attributes in "*attr".
10868 */
10869 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010870fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871{
10872 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010873
10874#ifdef FEAT_TERMINAL
10875 if (bt_terminal(wp->w_buffer))
10876 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010877 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010878 {
10879 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010880 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010881 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010882 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010883 {
10884 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010885 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010886 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010887 }
10888 else
10889#endif
10890 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010892 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 fill = fill_stl;
10894 }
10895 else
10896 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010897 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 fill = fill_stlnc;
10899 }
10900 /* Use fill when there is highlighting, and highlighting of current
10901 * window differs, or the fillchars differ, or this is not the
10902 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010903 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010904 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 || (fill_stl != fill_stlnc)))
10906 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010907 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 return '^';
10909 return '=';
10910}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912/*
10913 * Get the character to use in a separator between vertically split windows.
10914 * Get its attributes in "*attr".
10915 */
10916 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010917fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010919 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 if (*attr == 0 && fill_vert == ' ')
10921 return '|';
10922 else
10923 return fill_vert;
10924}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925
10926/*
10927 * Return TRUE if redrawing should currently be done.
10928 */
10929 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010930redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010932#ifdef FEAT_EVAL
10933 if (disable_redraw_for_testing)
10934 return 0;
10935 else
10936#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010937 return ((!RedrawingDisabled
10938#ifdef FEAT_EVAL
10939 || ignore_redraw_flag_for_testing
10940#endif
10941 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942}
10943
10944/*
10945 * Return TRUE if printing messages should currently be done.
10946 */
10947 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010948messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949{
10950 return (!(p_lz && char_avail() && !KeyTyped));
10951}
10952
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010953#ifdef FEAT_MENU
10954/*
10955 * Draw the window toolbar.
10956 */
10957 static void
10958redraw_win_toolbar(win_T *wp)
10959{
10960 vimmenu_T *menu;
10961 int item_idx = 0;
10962 int item_count = 0;
10963 int col = 0;
10964 int next_col;
10965 int off = (int)(current_ScreenLine - ScreenLines);
10966 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10967 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10968
10969 vim_free(wp->w_winbar_items);
10970 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10971 ++item_count;
10972 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10973 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10974
10975 /* TODO: use fewer spaces if there is not enough room */
10976 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010977 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010978 {
10979 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010980 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010981 break;
10982 if (col > 1)
10983 {
10984 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010985 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010986 break;
10987 }
10988
10989 wp->w_winbar_items[item_idx].wb_startcol = col;
10990 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010991 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010992 break;
10993
10994 next_col = text_to_screenline(wp, menu->name, col);
10995 while (col < next_col)
10996 {
10997 ScreenAttrs[off + col] = button_attr;
10998 ++col;
10999 }
11000 wp->w_winbar_items[item_idx].wb_endcol = col;
11001 wp->w_winbar_items[item_idx].wb_menu = menu;
11002 ++item_idx;
11003
Bram Moolenaar02631462017-09-22 15:20:32 +020011004 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011005 break;
11006 space_to_screenline(off + col, button_attr);
11007 ++col;
11008 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011009 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011010 {
11011 space_to_screenline(off + col, fill_attr);
11012 ++col;
11013 }
11014 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11015
Bram Moolenaar02631462017-09-22 15:20:32 +020011016 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11017 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011018}
11019#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011020
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021/*
11022 * Show current status info in ruler and various other places
11023 * If always is FALSE, only show ruler if position has changed.
11024 */
11025 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011026showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027{
11028 if (!always && !redrawing())
11029 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011030#ifdef FEAT_INS_EXPAND
11031 if (pum_visible())
11032 {
11033 /* Don't redraw right now, do it later. */
11034 curwin->w_redr_status = TRUE;
11035 return;
11036 }
11037#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011038#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011039 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011040 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011041 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 else
11044#endif
11045#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011046 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047#endif
11048
11049#ifdef FEAT_TITLE
11050 if (need_maketitle
11051# ifdef FEAT_STL_OPT
11052 || (p_icon && (stl_syntax & STL_IN_ICON))
11053 || (p_title && (stl_syntax & STL_IN_TITLE))
11054# endif
11055 )
11056 maketitle();
11057#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011058 /* Redraw the tab pages line if needed. */
11059 if (redraw_tabline)
11060 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061}
11062
11063#ifdef FEAT_CMDL_INFO
11064 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011065win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011067#define RULER_BUF_LEN 70
11068 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 int row;
11070 int fillchar;
11071 int attr;
11072 int empty_line = FALSE;
11073 colnr_T virtcol;
11074 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011075 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 int this_ru_col;
11078 int off = 0;
11079 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080
11081 /* If 'ruler' off or redrawing disabled, don't do anything */
11082 if (!p_ru)
11083 return;
11084
11085 /*
11086 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11087 * after deleting lines, before cursor.lnum is corrected.
11088 */
11089 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11090 return;
11091
11092#ifdef FEAT_INS_EXPAND
11093 /* Don't draw the ruler while doing insert-completion, it might overwrite
11094 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 if (edit_submode != NULL)
11097 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011098 // Don't draw the ruler when the popup menu is visible, it may overlap.
11099 // Except when the popup menu will be redrawn anyway.
11100 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011101 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102#endif
11103
11104#ifdef FEAT_STL_OPT
11105 if (*p_ruf)
11106 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011107 int save_called_emsg = called_emsg;
11108
11109 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011111 if (called_emsg)
11112 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011113 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011114 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 return;
11116 }
11117#endif
11118
11119 /*
11120 * Check if not in Insert mode and the line is empty (will show "0-1").
11121 */
11122 if (!(State & INSERT)
11123 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11124 empty_line = TRUE;
11125
11126 /*
11127 * Only draw the ruler when something changed.
11128 */
11129 validate_virtcol_win(wp);
11130 if ( redraw_cmdline
11131 || always
11132 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11133 || wp->w_cursor.col != wp->w_ru_cursor.col
11134 || wp->w_virtcol != wp->w_ru_virtcol
11135#ifdef FEAT_VIRTUALEDIT
11136 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11137#endif
11138 || wp->w_topline != wp->w_ru_topline
11139 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11140#ifdef FEAT_DIFF
11141 || wp->w_topfill != wp->w_ru_topfill
11142#endif
11143 || empty_line != wp->w_ru_empty)
11144 {
11145 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 if (wp->w_status_height)
11147 {
11148 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011149 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011150 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011151 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 }
11153 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 {
11155 row = Rows - 1;
11156 fillchar = ' ';
11157 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 width = Columns;
11159 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 }
11161
11162 /* In list mode virtcol needs to be recomputed */
11163 virtcol = wp->w_virtcol;
11164 if (wp->w_p_list && lcs_tab1 == NUL)
11165 {
11166 wp->w_p_list = FALSE;
11167 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11168 wp->w_p_list = TRUE;
11169 }
11170
11171 /*
11172 * Some sprintfs return the length, some return a pointer.
11173 * To avoid portability problems we use strlen() here.
11174 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011175 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11177 ? 0L
11178 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011179 len = STRLEN(buffer);
11180 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11182 (int)virtcol + 1);
11183
11184 /*
11185 * Add a "50%" if there is room for it.
11186 * On the last line, don't print in the last column (scrolls the
11187 * screen up on some terminals).
11188 */
11189 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011190 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 this_ru_col = ru_col - (Columns - width);
11195 if (this_ru_col < 0)
11196 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 /* Never use more than half the window/screen width, leave the other
11198 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011199 if (this_ru_col < (width + 1) / 2)
11200 this_ru_col = (width + 1) / 2;
11201 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011203 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011204 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 {
11206#ifdef FEAT_MBYTE
11207 if (has_mbyte)
11208 i += (*mb_char2bytes)(fillchar, buffer + i);
11209 else
11210#endif
11211 buffer[i++] = fillchar;
11212 ++o;
11213 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011214 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 }
11216 /* Truncate at window boundary. */
11217#ifdef FEAT_MBYTE
11218 if (has_mbyte)
11219 {
11220 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011221 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 {
11223 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011224 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 {
11226 buffer[i] = NUL;
11227 break;
11228 }
11229 }
11230 }
11231 else
11232#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011233 if (this_ru_col + (int)STRLEN(buffer) > width)
11234 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235
Bram Moolenaar4033c552017-09-16 20:54:51 +020011236 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 i = redraw_cmdline;
11238 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011239 this_ru_col + off + (int)STRLEN(buffer),
11240 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 fillchar, fillchar, attr);
11242 /* don't redraw the cmdline because of showing the ruler */
11243 redraw_cmdline = i;
11244 wp->w_ru_cursor = wp->w_cursor;
11245 wp->w_ru_virtcol = wp->w_virtcol;
11246 wp->w_ru_empty = empty_line;
11247 wp->w_ru_topline = wp->w_topline;
11248 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11249#ifdef FEAT_DIFF
11250 wp->w_ru_topfill = wp->w_topfill;
11251#endif
11252 }
11253}
11254#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011255
11256#if defined(FEAT_LINEBREAK) || defined(PROTO)
11257/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011258 * Return the width of the 'number' and 'relativenumber' column.
11259 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011260 * Otherwise it depends on 'numberwidth' and the line count.
11261 */
11262 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011263number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011264{
11265 int n;
11266 linenr_T lnum;
11267
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011268 if (wp->w_p_rnu && !wp->w_p_nu)
11269 /* cursor line shows "0" */
11270 lnum = wp->w_height;
11271 else
11272 /* cursor line shows absolute line number */
11273 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011274
Bram Moolenaar6b314672015-03-20 15:42:10 +010011275 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011276 return wp->w_nrwidth_width;
11277 wp->w_nrwidth_line_count = lnum;
11278
11279 n = 0;
11280 do
11281 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011282 lnum /= 10;
11283 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011284 } while (lnum > 0);
11285
11286 /* 'numberwidth' gives the minimal width plus one */
11287 if (n < wp->w_p_nuw - 1)
11288 n = wp->w_p_nuw - 1;
11289
11290 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011291 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011292 return n;
11293}
11294#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011295
11296/*
11297 * Return the current cursor column. This is the actual position on the
11298 * screen. First column is 0.
11299 */
11300 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011301screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011302{
11303 return screen_cur_col;
11304}
11305
11306/*
11307 * Return the current cursor row. This is the actual position on the screen.
11308 * First row is 0.
11309 */
11310 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011311screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011312{
11313 return screen_cur_row;
11314}