blob: 55874e99fbf29de197e35dde195b782ac874571d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200153static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void win_rest_invalid(win_T *wp);
155static void msg_pos_mode(void);
156static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Redraw the current window later, with update_screen(type).
186 * Set must_redraw only if not already set to a higher value.
187 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
188 */
189 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100190redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
192 redraw_win_later(curwin, type);
193}
194
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_win_later(
197 win_T *wp,
198 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200200 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {
202 wp->w_redr_type = type;
203 if (type >= NOT_VALID)
204 wp->w_lines_valid = 0;
205 if (must_redraw < type) /* must_redraw is the maximum of all windows */
206 must_redraw = type;
207 }
208}
209
210/*
211 * Force a complete redraw later. Also resets the highlighting. To be used
212 * after executing a shell command that messes up the screen.
213 */
214 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100215redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
217 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000218#ifdef FEAT_GUI
219 if (gui.in_use)
220 /* Use a code that will reset gui.highlight_mask in
221 * gui_stop_highlight(). */
222 screen_attr = HL_ALL + 1;
223 else
224#endif
225 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200226 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227}
228
229/*
230 * Mark all windows to be redrawn later.
231 */
232 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100233redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100247redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 redraw_buf_later(curbuf, type);
250}
251
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 win_T *wp;
256
257 FOR_ALL_WINDOWS(wp)
258 {
259 if (wp->w_buffer == buf)
260 redraw_win_later(wp, type);
261 }
262}
263
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200264 void
265redraw_buf_and_status_later(buf_T *buf, int type)
266{
267 win_T *wp;
268
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200269#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200270 if (wild_menu_showing != 0)
271 /* Don't redraw while the command line completion is displayed, it
272 * would disappear. */
273 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200274#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200275 FOR_ALL_WINDOWS(wp)
276 {
277 if (wp->w_buffer == buf)
278 {
279 redraw_win_later(wp, type);
280 wp->w_redr_status = TRUE;
281 }
282 }
283}
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200286 * Redraw as soon as possible. When the command line is not scrolled redraw
287 * right away and restore what was on the command line.
288 * Return a code indicating what happened.
289 */
290 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100291redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292{
293 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 int r;
296 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200297 schar_T *screenline; /* copy from ScreenLines[] */
298 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299#ifdef FEAT_MBYTE
300 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200301 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304#endif
305
306 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200307 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 return ret;
309
310 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 if (screenline == NULL || screenattr == NULL)
317 ret = 2;
318#ifdef FEAT_MBYTE
319 if (enc_utf8)
320 {
321 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 if (screenlineUC == NULL)
324 ret = 2;
325 for (i = 0; i < p_mco; ++i)
326 {
327 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineC[i] == NULL)
330 ret = 2;
331 }
332 }
333 if (enc_dbcs == DBCS_JPNU)
334 {
335 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenline2 == NULL)
338 ret = 2;
339 }
340#endif
341
342 if (ret != 2)
343 {
344 /* Save the text displayed in the command line area. */
345 for (r = 0; r < rows; ++r)
346 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200348 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (size_t)cols * sizeof(schar_T));
350 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200352 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353#ifdef FEAT_MBYTE
354 if (enc_utf8)
355 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenlineC[i] + r * cols,
361 ScreenLinesC[i] + LineOffset[cmdline_row + r],
362 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 }
364 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368#endif
369 }
370
371 update_screen(0);
372 ret = 3;
373
374 if (must_redraw == 0)
375 {
376 int off = (int)(current_ScreenLine - ScreenLines);
377
378 /* Restore the text displayed in the command line area. */
379 for (r = 0; r < rows; ++r)
380 {
381 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenline + r * cols,
383 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200385 screenattr + r * cols,
386 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387#ifdef FEAT_MBYTE
388 if (enc_utf8)
389 {
390 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenlineUC + r * cols,
392 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 for (i = 0; i < p_mco; ++i)
394 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenlineC[i] + r * cols,
396 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 }
398 if (enc_dbcs == DBCS_JPNU)
399 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenline2 + r * cols,
401 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200403 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 ret = 4;
406 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408
409 vim_free(screenline);
410 vim_free(screenattr);
411#ifdef FEAT_MBYTE
412 if (enc_utf8)
413 {
414 vim_free(screenlineUC);
415 for (i = 0; i < p_mco; ++i)
416 vim_free(screenlineC[i]);
417 }
418 if (enc_dbcs == DBCS_JPNU)
419 vim_free(screenline2);
420#endif
421
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200422 /* Show the intro message when appropriate. */
423 maybe_intro_message();
424
425 setcursor();
426
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427 return ret;
428}
429
430/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100431 * Invoked after an asynchronous callback is called.
432 * If an echo command was used the cursor needs to be put back where
433 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200434 * If "call_update_screen" is FALSE don't call update_screen() when at the
435 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100436 */
437 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200438redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200440 ++redrawing_for_callback;
441
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100442 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200443 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100444 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200445 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200446 // Don't redraw when in prompt_for_number().
447 if (cmdline_row > 0)
448 {
449 // Redrawing only works when the screen didn't scroll. Don't clear
450 // wildmenu entries.
451 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200452#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 && call_update_screen)
456 update_screen(0);
457
458 // Redraw in the same position, so that the user can continue
459 // editing the command.
460 redrawcmdline_ex(FALSE);
461 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200463 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100464 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200465 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100467 setcursor();
468 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100469 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100471 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // Don't update the cursor when it is blinking and off to avoid
473 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100474 out_flush_cursor(FALSE, FALSE);
475 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100477 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200478
479 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100480}
481
482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 * Changed something in the current window, at buffer line "lnum", that
484 * requires that line and possibly other lines to be redrawn.
485 * Used when entering/leaving Insert mode with the cursor on a folded line.
486 * Used to remove the "$" from a change command.
487 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
488 * may become invalid and the whole window will have to be redrawn.
489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100491redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200492 win_T *wp,
Bram Moolenaar05540972016-01-30 20:31:25 +0100493 linenr_T lnum,
494 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496#ifdef FEAT_FOLDING
497 int i;
498#endif
499
Bram Moolenaar90a99792018-09-12 21:52:18 +0200500 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
501 wp->w_redraw_top = lnum;
502 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
503 wp->w_redraw_bot = lnum;
504 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
506#ifdef FEAT_FOLDING
507 if (invalid)
508 {
509 /* A w_lines[] entry for this lnum has become invalid. */
Bram Moolenaar90a99792018-09-12 21:52:18 +0200510 i = find_wl_entry(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (i >= 0)
Bram Moolenaar90a99792018-09-12 21:52:18 +0200512 wp->w_lines[i].wl_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 }
514#endif
515}
516
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200517 void
518reset_updating_screen(int may_resize_shell UNUSED)
519{
520 updating_screen = FALSE;
521#ifdef FEAT_GUI
522 if (may_resize_shell)
523 gui_may_resize_shell();
524#endif
525#ifdef FEAT_TERMINAL
526 term_check_channel_closed_recently();
527#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200528
529#ifdef HAVE_DROP_FILE
530 // If handle_drop() was called while updating_screen was TRUE need to
531 // handle the drop now.
532 handle_any_postponed_drop();
533#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200534}
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200537 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100540update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 redraw_curbuf_later(type);
543 update_screen(type);
544}
545
546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * Based on the current value of curwin->w_topline, transfer a screenfull
548 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200549 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200551 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 win_T *wp;
556 static int did_intro = FALSE;
557#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
558 int did_one;
559#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200561 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200562 int gui_cursor_col;
563 int gui_cursor_row;
564#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200565 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000567 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200571 if (type == VALID_NO_UPDATE)
572 {
573 no_update = TRUE;
574 type = 0;
575 }
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (must_redraw)
578 {
579 if (type < must_redraw) /* use maximal type */
580 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000581
582 /* must_redraw is reset here, so that when we run into some weird
583 * reason to redraw while busy redrawing (e.g., asynchronous
584 * scrolling), or update_topline() in win_update() will cause a
585 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 must_redraw = 0;
587 }
588
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200589 /* May need to update w_lines[]. */
590 if (curwin->w_lines_valid == 0 && type < NOT_VALID
591#ifdef FEAT_TERMINAL
592 && !term_do_update_window(curwin)
593#endif
594 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 type = NOT_VALID;
596
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000597 /* Postpone the redrawing when it's not needed and when being called
598 * recursively. */
599 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {
601 redraw_later(type); /* remember type for next time */
602 must_redraw = type;
603 if (type > INVERTED_ALL)
604 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
607
608 updating_screen = TRUE;
609#ifdef FEAT_SYN_HL
610 ++display_tick; /* let syntax code know we're in a next round of
611 * display updating */
612#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200613 if (no_update)
614 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 /*
617 * if the screen was scrolled up when displaying a message, scroll it down
618 */
619 if (msg_scrolled)
620 {
621 clear_cmdline = TRUE;
622 if (msg_scrolled > Rows - 5) /* clearing is faster */
623 type = CLEAR;
624 else if (type != CLEAR)
625 {
626 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200627 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
628 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 type = CLEAR;
630 FOR_ALL_WINDOWS(wp)
631 {
632 if (W_WINROW(wp) < msg_scrolled)
633 {
634 if (W_WINROW(wp) + wp->w_height > msg_scrolled
635 && wp->w_redr_type < REDRAW_TOP
636 && wp->w_lines_valid > 0
637 && wp->w_topline == wp->w_lines[0].wl_lnum)
638 {
639 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
640 wp->w_redr_type = REDRAW_TOP;
641 }
642 else
643 {
644 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200645 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
646 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
649 }
650 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200651 if (!no_update)
652 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000653 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 msg_scrolled = 0;
656 need_wait_return = FALSE;
657 }
658
659 /* reset cmdline_row now (may have been changed temporarily) */
660 compute_cmdrow();
661
662 /* Check for changed highlighting */
663 if (need_highlight_changed)
664 highlight_changed();
665
666 if (type == CLEAR) /* first clear screen */
667 {
668 screenclear(); /* will reset clear_cmdline */
669 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200670 /* must_redraw may be set indirectly, avoid another redraw later */
671 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673
674 if (clear_cmdline) /* going to clear cmdline (done below) */
675 check_for_delay(FALSE);
676
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000677#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200678 /* Force redraw when width of 'number' or 'relativenumber' column
679 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000680 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200681 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
682 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000683 curwin->w_redr_type = NOT_VALID;
684#endif
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 /*
687 * Only start redrawing if there is really something to do.
688 */
689 if (type == INVERTED)
690 update_curswant();
691 if (curwin->w_redr_type < type
692 && !((type == VALID
693 && curwin->w_lines[0].wl_valid
694#ifdef FEAT_DIFF
695 && curwin->w_topfill == curwin->w_old_topfill
696 && curwin->w_botfill == curwin->w_old_botfill
697#endif
698 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000700 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
702 && curwin->w_old_visual_mode == VIsual_mode
703 && (curwin->w_valid & VALID_VIRTCOL)
704 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ))
706 curwin->w_redr_type = type;
707
Bram Moolenaar5a305422006-04-28 22:38:25 +0000708 /* Redraw the tab pages line if needed. */
709 if (redraw_tabline || type >= NOT_VALID)
710 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000711
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifdef FEAT_SYN_HL
713 /*
714 * Correct stored syntax highlighting info for changes in each displayed
715 * buffer. Each buffer must only be done once.
716 */
717 FOR_ALL_WINDOWS(wp)
718 {
719 if (wp->w_buffer->b_mod_set)
720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 win_T *wwp;
722
723 /* Check if we already did this buffer. */
724 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
725 if (wwp->w_buffer == wp->w_buffer)
726 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200727 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 syn_stack_apply_changes(wp->w_buffer);
729 }
730 }
731#endif
732
733 /*
734 * Go from top to bottom through the windows, redrawing the ones that need
735 * it.
736 */
737#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
738 did_one = FALSE;
739#endif
740#ifdef FEAT_SEARCH_EXTRA
741 search_hl.rm.regprog = NULL;
742#endif
743 FOR_ALL_WINDOWS(wp)
744 {
745 if (wp->w_redr_type != 0)
746 {
747 cursor_off();
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 if (!did_one)
750 {
751 did_one = TRUE;
752# ifdef FEAT_SEARCH_EXTRA
753 start_search_hl();
754# endif
755# ifdef FEAT_CLIPBOARD
756 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200757 if (clip_star.available && clip_isautosel_star())
758 clip_update_selection(&clip_star);
759 if (clip_plus.available && clip_isautosel_plus())
760 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761# endif
762#ifdef FEAT_GUI
763 /* Remove the cursor before starting to do anything, because
764 * scrolling may make it difficult to redraw the text under
765 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200766 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 {
768 gui_cursor_col = gui.cursor_col;
769 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200771 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
774 }
775#endif
776 win_update(wp);
777 }
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 /* redraw status line after the window to minimize cursor movement */
780 if (wp->w_redr_status)
781 {
782 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200783 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786#if defined(FEAT_SEARCH_EXTRA)
787 end_search_hl();
788#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100789#ifdef FEAT_INS_EXPAND
790 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200791 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 /* Reset b_mod_set flags. Going through all windows is probably faster
795 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200796 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200799 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /* Clear or redraw the command line. Done last, because scrolling may
802 * mess up the command line. */
803 if (clear_cmdline || redraw_cmdline)
804 showmode();
805
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200806 if (no_update)
807 --no_win_do_lines_ins;
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200810 if (!did_intro)
811 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 did_intro = TRUE;
813
814#ifdef FEAT_GUI
815 /* Redraw the cursor and update the scrollbars when all screen updating is
816 * done. */
817 if (gui.in_use)
818 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200819 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100821 mch_disable_flush();
822 out_flush(); /* required before updating the cursor */
823 mch_enable_flush();
824
Bram Moolenaar144445d2016-07-08 21:41:54 +0200825 /* Put the GUI position where the cursor was, gui_update_cursor()
826 * uses that. */
827 gui.col = gui_cursor_col;
828 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200829# ifdef FEAT_MBYTE
830 gui.col = mb_fix_col(gui.col, gui.row);
831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100833 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200834 screen_cur_col = gui.col;
835 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 else
838 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_scrollbars(FALSE);
840 }
841#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200842 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100845#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
846/*
847 * Prepare for updating one or more windows.
848 * Caller must check for "updating_screen" already set to avoid recursiveness.
849 */
850 static void
851update_prepare(void)
852{
853 cursor_off();
854 updating_screen = TRUE;
855#ifdef FEAT_GUI
856 /* Remove the cursor before starting to do anything, because scrolling may
857 * make it difficult to redraw the text under it. */
858 if (gui.in_use)
859 gui_undraw_cursor();
860#endif
861#ifdef FEAT_SEARCH_EXTRA
862 start_search_hl();
863#endif
864}
865
866/*
867 * Finish updating one or more windows.
868 */
869 static void
870update_finish(void)
871{
872 if (redraw_cmdline)
873 showmode();
874
875# ifdef FEAT_SEARCH_EXTRA
876 end_search_hl();
877# endif
878
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200879 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880
881# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100882 /* Redraw the cursor and update the scrollbars when all screen updating is
883 * done. */
884 if (gui.in_use)
885 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100886 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887 gui_update_scrollbars(FALSE);
888 }
889# endif
890}
891#endif
892
Bram Moolenaar860cae12010-06-05 23:22:07 +0200893#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200894/*
895 * Return TRUE if the cursor line in window "wp" may be concealed, according
896 * to the 'concealcursor' option.
897 */
898 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100899conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900{
901 int c;
902
903 if (*wp->w_p_cocu == NUL)
904 return FALSE;
905 if (get_real_state() & VISUAL)
906 c = 'v';
907 else if (State & INSERT)
908 c = 'i';
909 else if (State & NORMAL)
910 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200911 else if (State & CMDLINE)
912 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913 else
914 return FALSE;
915 return vim_strchr(wp->w_p_cocu, c) != NULL;
916}
917
918/*
919 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
920 */
921 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200922conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200923{
924 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
925 {
926 need_cursor_line_redraw = TRUE;
927 /* Need to recompute cursor column, e.g., when starting Visual mode
928 * without concealing. */
929 curs_columns(TRUE);
930 }
931}
932
Bram Moolenaar860cae12010-06-05 23:22:07 +0200933 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935{
936 int row;
937 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200938#ifdef SYN_TIME_LIMIT
939 proftime_T syntax_tm;
940#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941
Bram Moolenaar908be432016-05-24 10:51:30 +0200942 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100943 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200944 return;
945
Bram Moolenaar860cae12010-06-05 23:22:07 +0200946 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200947 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200949#ifdef SYN_TIME_LIMIT
950 /* Set the time limit to 'redrawtime'. */
951 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200952 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200953#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100954 update_prepare();
955
Bram Moolenaar860cae12010-06-05 23:22:07 +0200956 row = 0;
957 for (j = 0; j < wp->w_lines_valid; ++j)
958 {
959 if (lnum == wp->w_lines[j].wl_lnum)
960 {
961 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200962# ifdef FEAT_SEARCH_EXTRA
963 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200964 prepare_search_hl(wp, lnum);
965# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200966 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
967 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200968 break;
969 }
970 row += wp->w_lines[j].wl_size;
971 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100972
973 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200974
975#ifdef SYN_TIME_LIMIT
976 syn_set_timeout(NULL);
977#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200978 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200979 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981#endif
982
983#if defined(FEAT_SIGNS) || defined(PROTO)
984 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100985update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
987 win_T *wp;
988 int doit = FALSE;
989
990# ifdef FEAT_FOLDING
991 win_foldinfo.fi_level = 0;
992# endif
993
994 /* update/delete a specific mark */
995 FOR_ALL_WINDOWS(wp)
996 {
997 if (buf != NULL && lnum > 0)
998 {
999 if (wp->w_buffer == buf && lnum >= wp->w_topline
1000 && lnum < wp->w_botline)
1001 {
1002 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1003 wp->w_redraw_top = lnum;
1004 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1005 wp->w_redraw_bot = lnum;
1006 redraw_win_later(wp, VALID);
1007 }
1008 }
1009 else
1010 redraw_win_later(wp, VALID);
1011 if (wp->w_redr_type != 0)
1012 doit = TRUE;
1013 }
1014
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001015 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001016 * happening (recursive call), messages on the screen or still starting up.
1017 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001018 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001019 || State == ASKMORE || State == HITRETURN
1020 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001021#ifdef FEAT_GUI
1022 || gui.starting
1023#endif
1024 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 return;
1026
1027 /* update all windows that need updating */
1028 update_prepare();
1029
Bram Moolenaar29323592016-07-24 22:04:11 +02001030 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
1032 if (wp->w_redr_type != 0)
1033 win_update(wp);
1034 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001035 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 update_finish();
1039}
1040#endif
1041
1042
1043#if defined(FEAT_GUI) || defined(PROTO)
1044/*
1045 * Update a single window, its status line and maybe the command line msg.
1046 * Used for the GUI scrollbar.
1047 */
1048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001049updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001051 /* return if already busy updating */
1052 if (updating_screen)
1053 return;
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 update_prepare();
1056
1057#ifdef FEAT_CLIPBOARD
1058 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001059 if (clip_star.available && clip_isautosel_star())
1060 clip_update_selection(&clip_star);
1061 if (clip_plus.available && clip_isautosel_plus())
1062 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001066
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001067 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001068 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001069 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (wp->w_redr_status
1072# ifdef FEAT_CMDL_INFO
1073 || p_ru
1074# endif
1075# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001076 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077# endif
1078 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001079 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
1081 update_finish();
1082}
1083#endif
1084
1085/*
1086 * Update a single window.
1087 *
1088 * This may cause the windows below it also to be redrawn (when clearing the
1089 * screen or scrolling lines).
1090 *
1091 * How the window is redrawn depends on wp->w_redr_type. Each type also
1092 * implies the one below it.
1093 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001094 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1096 * INVERTED redraw the changed part of the Visual area
1097 * INVERTED_ALL redraw the whole Visual area
1098 * VALID 1. scroll up/down to adjust for a changed w_topline
1099 * 2. update lines at the top when scrolled down
1100 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001101 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 * b_mod_top and b_mod_bot.
1103 * - if wp->w_redraw_top non-zero, redraw lines between
1104 * wp->w_redraw_top and wp->w_redr_bot.
1105 * - continue redrawing when syntax status is invalid.
1106 * 4. if scrolled up, update lines at the bottom.
1107 * This results in three areas that may need updating:
1108 * top: from first row to top_end (when scrolled down)
1109 * mid: from mid_start to mid_end (update inversion or changed text)
1110 * bot: from bot_start to last row (when scrolled up)
1111 */
1112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001113win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 buf_T *buf = wp->w_buffer;
1116 int type;
1117 int top_end = 0; /* Below last row of the top area that needs
1118 updating. 0 when no top area updating. */
1119 int mid_start = 999;/* first row of the mid area that needs
1120 updating. 999 when no mid area updating. */
1121 int mid_end = 0; /* Below last row of the mid area that needs
1122 updating. 0 when no mid area updating. */
1123 int bot_start = 999;/* first row of the bot area that needs
1124 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 int scrolled_down = FALSE; /* TRUE when scrolled down when
1126 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001128 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int top_to_mod = FALSE; /* redraw above mod_top */
1130#endif
1131
1132 int row; /* current window row to display */
1133 linenr_T lnum; /* current buffer lnum to display */
1134 int idx; /* current index in w_lines[] */
1135 int srow; /* starting row of the current line */
1136
1137 int eof = FALSE; /* if TRUE, we hit the end of the file */
1138 int didline = FALSE; /* if TRUE, we finished the last line */
1139 int i;
1140 long j;
1141 static int recursive = FALSE; /* being called recursively */
1142 int old_botline = wp->w_botline;
1143#ifdef FEAT_FOLDING
1144 long fold_count;
1145#endif
1146#ifdef FEAT_SYN_HL
1147 /* remember what happened to the previous line, to know if
1148 * check_visual_highlight() can be used */
1149#define DID_NONE 1 /* didn't update a line */
1150#define DID_LINE 2 /* updated a normal line */
1151#define DID_FOLD 3 /* updated a folded line */
1152 int did_update = DID_NONE;
1153 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1154#endif
1155 linenr_T mod_top = 0;
1156 linenr_T mod_bot = 0;
1157#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1158 int save_got_int;
1159#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001160#ifdef SYN_TIME_LIMIT
1161 proftime_T syntax_tm;
1162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 type = wp->w_redr_type;
1165
1166 if (type == NOT_VALID)
1167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 wp->w_lines_valid = 0;
1170 }
1171
1172 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001173 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
1175 wp->w_redr_type = 0;
1176 return;
1177 }
1178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 /* Window is zero-width: Only need to draw the separator. */
1180 if (wp->w_width == 0)
1181 {
1182 /* draw the vertical separator right of this window */
1183 draw_vsep_win(wp, 0);
1184 wp->w_redr_type = 0;
1185 return;
1186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001188#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001189 // If this window contains a terminal, redraw works completely differently.
1190 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001191 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001192 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001193# ifdef FEAT_MENU
1194 /* Draw the window toolbar, if there is one. */
1195 if (winbar_height(wp) > 0)
1196 redraw_win_toolbar(wp);
1197# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001198 wp->w_redr_type = 0;
1199 return;
1200 }
1201#endif
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001204 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#endif
1206
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001207#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001208 /* Force redraw when width of 'number' or 'relativenumber' column
1209 * changes. */
1210 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001211 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001212 {
1213 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001214 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001215 }
1216 else
1217#endif
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1220 {
1221 /*
1222 * When there are both inserted/deleted lines and specific lines to be
1223 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1224 * everything (only happens when redrawing is off for while).
1225 */
1226 type = NOT_VALID;
1227 }
1228 else
1229 {
1230 /*
1231 * Set mod_top to the first line that needs displaying because of
1232 * changes. Set mod_bot to the first line after the changes.
1233 */
1234 mod_top = wp->w_redraw_top;
1235 if (wp->w_redraw_bot != 0)
1236 mod_bot = wp->w_redraw_bot + 1;
1237 else
1238 mod_bot = 0;
1239 wp->w_redraw_top = 0; /* reset for next time */
1240 wp->w_redraw_bot = 0;
1241 if (buf->b_mod_set)
1242 {
1243 if (mod_top == 0 || mod_top > buf->b_mod_top)
1244 {
1245 mod_top = buf->b_mod_top;
1246#ifdef FEAT_SYN_HL
1247 /* Need to redraw lines above the change that may be included
1248 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001249 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001251 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (mod_top < 1)
1253 mod_top = 1;
1254 }
1255#endif
1256 }
1257 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1258 mod_bot = buf->b_mod_bot;
1259
1260#ifdef FEAT_SEARCH_EXTRA
1261 /* When 'hlsearch' is on and using a multi-line search pattern, a
1262 * change in one line may make the Search highlighting in a
1263 * previous line invalid. Simple solution: redraw all visible
1264 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001265 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001267 if (search_hl.rm.regprog != NULL
1268 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001270 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001271 {
1272 cur = wp->w_match_head;
1273 while (cur != NULL)
1274 {
1275 if (cur->match.regprog != NULL
1276 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001277 {
1278 top_to_mod = TRUE;
1279 break;
1280 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001281 cur = cur->next;
1282 }
1283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284#endif
1285 }
1286#ifdef FEAT_FOLDING
1287 if (mod_top != 0 && hasAnyFolding(wp))
1288 {
1289 linenr_T lnumt, lnumb;
1290
1291 /*
1292 * A change in a line can cause lines above it to become folded or
1293 * unfolded. Find the top most buffer line that may be affected.
1294 * If the line was previously folded and displayed, get the first
1295 * line of that fold. If the line is folded now, get the first
1296 * folded line. Use the minimum of these two.
1297 */
1298
1299 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1300 * the line below it. If there is no valid entry, use w_topline.
1301 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1302 * to this line. If there is no valid entry, use MAXLNUM. */
1303 lnumt = wp->w_topline;
1304 lnumb = MAXLNUM;
1305 for (i = 0; i < wp->w_lines_valid; ++i)
1306 if (wp->w_lines[i].wl_valid)
1307 {
1308 if (wp->w_lines[i].wl_lastlnum < mod_top)
1309 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1310 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1311 {
1312 lnumb = wp->w_lines[i].wl_lnum;
1313 /* When there is a fold column it might need updating
1314 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001315 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 ++lnumb;
1317 }
1318 }
1319
1320 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1321 if (mod_top > lnumt)
1322 mod_top = lnumt;
1323
1324 /* Now do the same for the bottom line (one above mod_bot). */
1325 --mod_bot;
1326 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1327 ++mod_bot;
1328 if (mod_bot < lnumb)
1329 mod_bot = lnumb;
1330 }
1331#endif
1332
1333 /* When a change starts above w_topline and the end is below
1334 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001335 * If the end of the change is above w_topline: do like no change was
1336 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (mod_top != 0 && mod_top < wp->w_topline)
1338 {
1339 if (mod_bot > wp->w_topline)
1340 mod_top = wp->w_topline;
1341#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001342 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 top_end = 1;
1344#endif
1345 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001346
1347 /* When line numbers are displayed need to redraw all lines below
1348 * inserted/deleted lines. */
1349 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1350 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352
1353 /*
1354 * When only displaying the lines at the top, set top_end. Used when
1355 * window has scrolled down for msg_scrolled.
1356 */
1357 if (type == REDRAW_TOP)
1358 {
1359 j = 0;
1360 for (i = 0; i < wp->w_lines_valid; ++i)
1361 {
1362 j += wp->w_lines[i].wl_size;
1363 if (j >= wp->w_upd_rows)
1364 {
1365 top_end = j;
1366 break;
1367 }
1368 }
1369 if (top_end == 0)
1370 /* not found (cannot happen?): redraw everything */
1371 type = NOT_VALID;
1372 else
1373 /* top area defined, the rest is VALID */
1374 type = VALID;
1375 }
1376
Bram Moolenaar367329b2007-08-30 11:53:22 +00001377 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001378 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1379 * non-zero and thus not FALSE) will indicate that screenclear() was not
1380 * called. */
1381 if (screen_cleared)
1382 screen_cleared = MAYBE;
1383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 /*
1385 * If there are no changes on the screen that require a complete redraw,
1386 * handle three cases:
1387 * 1: we are off the top of the screen by a few lines: scroll down
1388 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1389 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1390 * w_lines[] that needs updating.
1391 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001392 if ((type == VALID || type == SOME_VALID
1393 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#ifdef FEAT_DIFF
1395 && !wp->w_botfill && !wp->w_old_botfill
1396#endif
1397 )
1398 {
1399 if (mod_top != 0 && wp->w_topline == mod_top)
1400 {
1401 /*
1402 * w_topline is the first changed line, the scrolling will be done
1403 * further down.
1404 */
1405 }
1406 else if (wp->w_lines[0].wl_valid
1407 && (wp->w_topline < wp->w_lines[0].wl_lnum
1408#ifdef FEAT_DIFF
1409 || (wp->w_topline == wp->w_lines[0].wl_lnum
1410 && wp->w_topfill > wp->w_old_topfill)
1411#endif
1412 ))
1413 {
1414 /*
1415 * New topline is above old topline: May scroll down.
1416 */
1417#ifdef FEAT_FOLDING
1418 if (hasAnyFolding(wp))
1419 {
1420 linenr_T ln;
1421
1422 /* count the number of lines we are off, counting a sequence
1423 * of folded lines as one */
1424 j = 0;
1425 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1426 {
1427 ++j;
1428 if (j >= wp->w_height - 2)
1429 break;
1430 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1431 }
1432 }
1433 else
1434#endif
1435 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1436 if (j < wp->w_height - 2) /* not too far off */
1437 {
1438 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1439#ifdef FEAT_DIFF
1440 /* insert extra lines for previously invisible filler lines */
1441 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1442 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1443 - wp->w_old_topfill;
1444#endif
1445 if (i < wp->w_height - 2) /* less than a screen off */
1446 {
1447 /*
1448 * Try to insert the correct number of lines.
1449 * If not the last window, delete the lines at the bottom.
1450 * win_ins_lines may fail when the terminal can't do it.
1451 */
1452 if (i > 0)
1453 check_for_delay(FALSE);
1454 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1455 {
1456 if (wp->w_lines_valid != 0)
1457 {
1458 /* Need to update rows that are new, stop at the
1459 * first one that scrolled down. */
1460 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
1463 /* Move the entries that were scrolled, disable
1464 * the entries for the lines to be redrawn. */
1465 if ((wp->w_lines_valid += j) > wp->w_height)
1466 wp->w_lines_valid = wp->w_height;
1467 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1468 wp->w_lines[idx] = wp->w_lines[idx - j];
1469 while (idx >= 0)
1470 wp->w_lines[idx--].wl_valid = FALSE;
1471 }
1472 }
1473 else
1474 mid_start = 0; /* redraw all lines */
1475 }
1476 else
1477 mid_start = 0; /* redraw all lines */
1478 }
1479 else
1480 mid_start = 0; /* redraw all lines */
1481 }
1482 else
1483 {
1484 /*
1485 * New topline is at or below old topline: May scroll up.
1486 * When topline didn't change, find first entry in w_lines[] that
1487 * needs updating.
1488 */
1489
1490 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1491 j = -1;
1492 row = 0;
1493 for (i = 0; i < wp->w_lines_valid; i++)
1494 {
1495 if (wp->w_lines[i].wl_valid
1496 && wp->w_lines[i].wl_lnum == wp->w_topline)
1497 {
1498 j = i;
1499 break;
1500 }
1501 row += wp->w_lines[i].wl_size;
1502 }
1503 if (j == -1)
1504 {
1505 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1506 * lines */
1507 mid_start = 0;
1508 }
1509 else
1510 {
1511 /*
1512 * Try to delete the correct number of lines.
1513 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1514 */
1515#ifdef FEAT_DIFF
1516 /* If the topline didn't change, delete old filler lines,
1517 * otherwise delete filler lines of the new topline... */
1518 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1519 row += wp->w_old_topfill;
1520 else
1521 row += diff_check_fill(wp, wp->w_topline);
1522 /* ... but don't delete new filler lines. */
1523 row -= wp->w_topfill;
1524#endif
1525 if (row > 0)
1526 {
1527 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001528 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1529 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 bot_start = wp->w_height - row;
1531 else
1532 mid_start = 0; /* redraw all lines */
1533 }
1534 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1535 {
1536 /*
1537 * Skip the lines (below the deleted lines) that are still
1538 * valid and don't need redrawing. Copy their info
1539 * upwards, to compensate for the deleted lines. Set
1540 * bot_start to the first row that needs redrawing.
1541 */
1542 bot_start = 0;
1543 idx = 0;
1544 for (;;)
1545 {
1546 wp->w_lines[idx] = wp->w_lines[j];
1547 /* stop at line that didn't fit, unless it is still
1548 * valid (no lines deleted) */
1549 if (row > 0 && bot_start + row
1550 + (int)wp->w_lines[j].wl_size > wp->w_height)
1551 {
1552 wp->w_lines_valid = idx + 1;
1553 break;
1554 }
1555 bot_start += wp->w_lines[idx++].wl_size;
1556
1557 /* stop at the last valid entry in w_lines[].wl_size */
1558 if (++j >= wp->w_lines_valid)
1559 {
1560 wp->w_lines_valid = idx;
1561 break;
1562 }
1563 }
1564#ifdef FEAT_DIFF
1565 /* Correct the first entry for filler lines at the top
1566 * when it won't get updated below. */
1567 if (wp->w_p_diff && bot_start > 0)
1568 wp->w_lines[0].wl_size =
1569 plines_win_nofill(wp, wp->w_topline, TRUE)
1570 + wp->w_topfill;
1571#endif
1572 }
1573 }
1574 }
1575
1576 /* When starting redraw in the first line, redraw all lines. When
1577 * there is only one window it's probably faster to clear the screen
1578 * first. */
1579 if (mid_start == 0)
1580 {
1581 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001582 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001583 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001584 /* Clear the screen when it was not done by win_del_lines() or
1585 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1586 * then. */
1587 if (screen_cleared != TRUE)
1588 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001589 /* The screen was cleared, redraw the tab pages line. */
1590 if (redraw_tabline)
1591 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001594
1595 /* When win_del_lines() or win_ins_lines() caused the screen to be
1596 * cleared (only happens for the first window) or when screenclear()
1597 * was called directly above, "must_redraw" will have been set to
1598 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1599 if (screen_cleared == TRUE)
1600 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 else
1603 {
1604 /* Not VALID or INVERTED: redraw all lines. */
1605 mid_start = 0;
1606 mid_end = wp->w_height;
1607 }
1608
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001609 if (type == SOME_VALID)
1610 {
1611 /* SOME_VALID: redraw all lines. */
1612 mid_start = 0;
1613 mid_end = wp->w_height;
1614 type = NOT_VALID;
1615 }
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 /* check if we are updating or removing the inverted part */
1618 if ((VIsual_active && buf == curwin->w_buffer)
1619 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1620 {
1621 linenr_T from, to;
1622
1623 if (VIsual_active)
1624 {
1625 if (VIsual_active
1626 && (VIsual_mode != wp->w_old_visual_mode
1627 || type == INVERTED_ALL))
1628 {
1629 /*
1630 * If the type of Visual selection changed, redraw the whole
1631 * selection. Also when the ownership of the X selection is
1632 * gained or lost.
1633 */
1634 if (curwin->w_cursor.lnum < VIsual.lnum)
1635 {
1636 from = curwin->w_cursor.lnum;
1637 to = VIsual.lnum;
1638 }
1639 else
1640 {
1641 from = VIsual.lnum;
1642 to = curwin->w_cursor.lnum;
1643 }
1644 /* redraw more when the cursor moved as well */
1645 if (wp->w_old_cursor_lnum < from)
1646 from = wp->w_old_cursor_lnum;
1647 if (wp->w_old_cursor_lnum > to)
1648 to = wp->w_old_cursor_lnum;
1649 if (wp->w_old_visual_lnum < from)
1650 from = wp->w_old_visual_lnum;
1651 if (wp->w_old_visual_lnum > to)
1652 to = wp->w_old_visual_lnum;
1653 }
1654 else
1655 {
1656 /*
1657 * Find the line numbers that need to be updated: The lines
1658 * between the old cursor position and the current cursor
1659 * position. Also check if the Visual position changed.
1660 */
1661 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1662 {
1663 from = curwin->w_cursor.lnum;
1664 to = wp->w_old_cursor_lnum;
1665 }
1666 else
1667 {
1668 from = wp->w_old_cursor_lnum;
1669 to = curwin->w_cursor.lnum;
1670 if (from == 0) /* Visual mode just started */
1671 from = to;
1672 }
1673
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001674 if (VIsual.lnum != wp->w_old_visual_lnum
1675 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
1677 if (wp->w_old_visual_lnum < from
1678 && wp->w_old_visual_lnum != 0)
1679 from = wp->w_old_visual_lnum;
1680 if (wp->w_old_visual_lnum > to)
1681 to = wp->w_old_visual_lnum;
1682 if (VIsual.lnum < from)
1683 from = VIsual.lnum;
1684 if (VIsual.lnum > to)
1685 to = VIsual.lnum;
1686 }
1687 }
1688
1689 /*
1690 * If in block mode and changed column or curwin->w_curswant:
1691 * update all lines.
1692 * First compute the actual start and end column.
1693 */
1694 if (VIsual_mode == Ctrl_V)
1695 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001696 colnr_T fromc, toc;
1697#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1698 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
Bram Moolenaar404406a2014-10-09 13:24:43 +02001700 if (curwin->w_p_lbr)
1701 ve_flags = VE_ALL;
1702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001704#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1705 ve_flags = save_ve_flags;
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 ++toc;
1708 if (curwin->w_curswant == MAXCOL)
1709 toc = MAXCOL;
1710
1711 if (fromc != wp->w_old_cursor_fcol
1712 || toc != wp->w_old_cursor_lcol)
1713 {
1714 if (from > VIsual.lnum)
1715 from = VIsual.lnum;
1716 if (to < VIsual.lnum)
1717 to = VIsual.lnum;
1718 }
1719 wp->w_old_cursor_fcol = fromc;
1720 wp->w_old_cursor_lcol = toc;
1721 }
1722 }
1723 else
1724 {
1725 /* Use the line numbers of the old Visual area. */
1726 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1727 {
1728 from = wp->w_old_cursor_lnum;
1729 to = wp->w_old_visual_lnum;
1730 }
1731 else
1732 {
1733 from = wp->w_old_visual_lnum;
1734 to = wp->w_old_cursor_lnum;
1735 }
1736 }
1737
1738 /*
1739 * There is no need to update lines above the top of the window.
1740 */
1741 if (from < wp->w_topline)
1742 from = wp->w_topline;
1743
1744 /*
1745 * If we know the value of w_botline, use it to restrict the update to
1746 * the lines that are visible in the window.
1747 */
1748 if (wp->w_valid & VALID_BOTLINE)
1749 {
1750 if (from >= wp->w_botline)
1751 from = wp->w_botline - 1;
1752 if (to >= wp->w_botline)
1753 to = wp->w_botline - 1;
1754 }
1755
1756 /*
1757 * Find the minimal part to be updated.
1758 * Watch out for scrolling that made entries in w_lines[] invalid.
1759 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1760 * top_end; need to redraw from top_end to the "to" line.
1761 * A middle mouse click with a Visual selection may change the text
1762 * above the Visual area and reset wl_valid, do count these for
1763 * mid_end (in srow).
1764 */
1765 if (mid_start > 0)
1766 {
1767 lnum = wp->w_topline;
1768 idx = 0;
1769 srow = 0;
1770 if (scrolled_down)
1771 mid_start = top_end;
1772 else
1773 mid_start = 0;
1774 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1775 {
1776 if (wp->w_lines[idx].wl_valid)
1777 mid_start += wp->w_lines[idx].wl_size;
1778 else if (!scrolled_down)
1779 srow += wp->w_lines[idx].wl_size;
1780 ++idx;
1781# ifdef FEAT_FOLDING
1782 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1783 lnum = wp->w_lines[idx].wl_lnum;
1784 else
1785# endif
1786 ++lnum;
1787 }
1788 srow += mid_start;
1789 mid_end = wp->w_height;
1790 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1791 {
1792 if (wp->w_lines[idx].wl_valid
1793 && wp->w_lines[idx].wl_lnum >= to + 1)
1794 {
1795 /* Only update until first row of this line */
1796 mid_end = srow;
1797 break;
1798 }
1799 srow += wp->w_lines[idx].wl_size;
1800 }
1801 }
1802 }
1803
1804 if (VIsual_active && buf == curwin->w_buffer)
1805 {
1806 wp->w_old_visual_mode = VIsual_mode;
1807 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1808 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001809 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 wp->w_old_curswant = curwin->w_curswant;
1811 }
1812 else
1813 {
1814 wp->w_old_visual_mode = 0;
1815 wp->w_old_cursor_lnum = 0;
1816 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001817 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
1820#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1821 /* reset got_int, otherwise regexp won't work */
1822 save_got_int = got_int;
1823 got_int = 0;
1824#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001825#ifdef SYN_TIME_LIMIT
1826 /* Set the time limit to 'redrawtime'. */
1827 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001828 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830#ifdef FEAT_FOLDING
1831 win_foldinfo.fi_level = 0;
1832#endif
1833
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001834#ifdef FEAT_MENU
1835 /*
1836 * Draw the window toolbar, if there is one.
1837 * TODO: only when needed.
1838 */
1839 if (winbar_height(wp) > 0)
1840 redraw_win_toolbar(wp);
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 /*
1844 * Update all the window rows.
1845 */
1846 idx = 0; /* first entry in w_lines[].wl_size */
1847 row = 0;
1848 srow = 0;
1849 lnum = wp->w_topline; /* first line shown in window */
1850 for (;;)
1851 {
1852 /* stop updating when reached the end of the window (check for _past_
1853 * the end of the window is at the end of the loop) */
1854 if (row == wp->w_height)
1855 {
1856 didline = TRUE;
1857 break;
1858 }
1859
1860 /* stop updating when hit the end of the file */
1861 if (lnum > buf->b_ml.ml_line_count)
1862 {
1863 eof = TRUE;
1864 break;
1865 }
1866
1867 /* Remember the starting row of the line that is going to be dealt
1868 * with. It is used further down when the line doesn't fit. */
1869 srow = row;
1870
1871 /*
1872 * Update a line when it is in an area that needs updating, when it
1873 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001874 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 * win_del_lines(), check if the current line includes it.
1876 * When syntax folding is being used, the saved syntax states will
1877 * already have been updated, we can't see where the syntax state is
1878 * the same again, just update until the end of the window.
1879 */
1880 if (row < top_end
1881 || (row >= mid_start && row < mid_end)
1882#ifdef FEAT_SEARCH_EXTRA
1883 || top_to_mod
1884#endif
1885 || idx >= wp->w_lines_valid
1886 || (row + wp->w_lines[idx].wl_size > bot_start)
1887 || (mod_top != 0
1888 && (lnum == mod_top
1889 || (lnum >= mod_top
1890 && (lnum < mod_bot
1891#ifdef FEAT_SYN_HL
1892 || did_update == DID_FOLD
1893 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001894 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 && (
1896# ifdef FEAT_FOLDING
1897 (foldmethodIsSyntax(wp)
1898 && hasAnyFolding(wp)) ||
1899# endif
1900 syntax_check_changed(lnum)))
1901#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001902#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001903 /* match in fixed position might need redraw
1904 * if lines were inserted or deleted */
1905 || (wp->w_match_head != NULL
1906 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 )))))
1909 {
1910#ifdef FEAT_SEARCH_EXTRA
1911 if (lnum == mod_top)
1912 top_to_mod = FALSE;
1913#endif
1914
1915 /*
1916 * When at start of changed lines: May scroll following lines
1917 * up or down to minimize redrawing.
1918 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001919 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 */
1921 if (lnum == mod_top
1922 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001923 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 int old_rows = 0;
1926 int new_rows = 0;
1927 int xtra_rows;
1928 linenr_T l;
1929
1930 /* Count the old number of window rows, using w_lines[], which
1931 * should still contain the sizes for the lines as they are
1932 * currently displayed. */
1933 for (i = idx; i < wp->w_lines_valid; ++i)
1934 {
1935 /* Only valid lines have a meaningful wl_lnum. Invalid
1936 * lines are part of the changed area. */
1937 if (wp->w_lines[i].wl_valid
1938 && wp->w_lines[i].wl_lnum == mod_bot)
1939 break;
1940 old_rows += wp->w_lines[i].wl_size;
1941#ifdef FEAT_FOLDING
1942 if (wp->w_lines[i].wl_valid
1943 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1944 {
1945 /* Must have found the last valid entry above mod_bot.
1946 * Add following invalid entries. */
1947 ++i;
1948 while (i < wp->w_lines_valid
1949 && !wp->w_lines[i].wl_valid)
1950 old_rows += wp->w_lines[i++].wl_size;
1951 break;
1952 }
1953#endif
1954 }
1955
1956 if (i >= wp->w_lines_valid)
1957 {
1958 /* We can't find a valid line below the changed lines,
1959 * need to redraw until the end of the window.
1960 * Inserting/deleting lines has no use. */
1961 bot_start = 0;
1962 }
1963 else
1964 {
1965 /* Able to count old number of rows: Count new window
1966 * rows, and may insert/delete lines */
1967 j = idx;
1968 for (l = lnum; l < mod_bot; ++l)
1969 {
1970#ifdef FEAT_FOLDING
1971 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1972 ++new_rows;
1973 else
1974#endif
1975#ifdef FEAT_DIFF
1976 if (l == wp->w_topline)
1977 new_rows += plines_win_nofill(wp, l, TRUE)
1978 + wp->w_topfill;
1979 else
1980#endif
1981 new_rows += plines_win(wp, l, TRUE);
1982 ++j;
1983 if (new_rows > wp->w_height - row - 2)
1984 {
1985 /* it's getting too much, must redraw the rest */
1986 new_rows = 9999;
1987 break;
1988 }
1989 }
1990 xtra_rows = new_rows - old_rows;
1991 if (xtra_rows < 0)
1992 {
1993 /* May scroll text up. If there is not enough
1994 * remaining text or scrolling fails, must redraw the
1995 * rest. If scrolling works, must redraw the text
1996 * below the scrolled text. */
1997 if (row - xtra_rows >= wp->w_height - 2)
1998 mod_bot = MAXLNUM;
1999 else
2000 {
2001 check_for_delay(FALSE);
2002 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002003 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 mod_bot = MAXLNUM;
2005 else
2006 bot_start = wp->w_height + xtra_rows;
2007 }
2008 }
2009 else if (xtra_rows > 0)
2010 {
2011 /* May scroll text down. If there is not enough
2012 * remaining text of scrolling fails, must redraw the
2013 * rest. */
2014 if (row + xtra_rows >= wp->w_height - 2)
2015 mod_bot = MAXLNUM;
2016 else
2017 {
2018 check_for_delay(FALSE);
2019 if (win_ins_lines(wp, row + old_rows,
2020 xtra_rows, FALSE, FALSE) == FAIL)
2021 mod_bot = MAXLNUM;
2022 else if (top_end > row + old_rows)
2023 /* Scrolled the part at the top that requires
2024 * updating down. */
2025 top_end += xtra_rows;
2026 }
2027 }
2028
2029 /* When not updating the rest, may need to move w_lines[]
2030 * entries. */
2031 if (mod_bot != MAXLNUM && i != j)
2032 {
2033 if (j < i)
2034 {
2035 int x = row + new_rows;
2036
2037 /* move entries in w_lines[] upwards */
2038 for (;;)
2039 {
2040 /* stop at last valid entry in w_lines[] */
2041 if (i >= wp->w_lines_valid)
2042 {
2043 wp->w_lines_valid = j;
2044 break;
2045 }
2046 wp->w_lines[j] = wp->w_lines[i];
2047 /* stop at a line that won't fit */
2048 if (x + (int)wp->w_lines[j].wl_size
2049 > wp->w_height)
2050 {
2051 wp->w_lines_valid = j + 1;
2052 break;
2053 }
2054 x += wp->w_lines[j++].wl_size;
2055 ++i;
2056 }
2057 if (bot_start > x)
2058 bot_start = x;
2059 }
2060 else /* j > i */
2061 {
2062 /* move entries in w_lines[] downwards */
2063 j -= i;
2064 wp->w_lines_valid += j;
2065 if (wp->w_lines_valid > wp->w_height)
2066 wp->w_lines_valid = wp->w_height;
2067 for (i = wp->w_lines_valid; i - j >= idx; --i)
2068 wp->w_lines[i] = wp->w_lines[i - j];
2069
2070 /* The w_lines[] entries for inserted lines are
2071 * now invalid, but wl_size may be used above.
2072 * Reset to zero. */
2073 while (i >= idx)
2074 {
2075 wp->w_lines[i].wl_size = 0;
2076 wp->w_lines[i--].wl_valid = FALSE;
2077 }
2078 }
2079 }
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082
2083#ifdef FEAT_FOLDING
2084 /*
2085 * When lines are folded, display one line for all of them.
2086 * Otherwise, display normally (can be several display lines when
2087 * 'wrap' is on).
2088 */
2089 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2090 if (fold_count != 0)
2091 {
2092 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2093 ++row;
2094 --fold_count;
2095 wp->w_lines[idx].wl_folded = TRUE;
2096 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2097# ifdef FEAT_SYN_HL
2098 did_update = DID_FOLD;
2099# endif
2100 }
2101 else
2102#endif
2103 if (idx < wp->w_lines_valid
2104 && wp->w_lines[idx].wl_valid
2105 && wp->w_lines[idx].wl_lnum == lnum
2106 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002107 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 && srow + wp->w_lines[idx].wl_size > wp->w_height
2109#ifdef FEAT_DIFF
2110 && diff_check_fill(wp, lnum) == 0
2111#endif
2112 )
2113 {
2114 /* This line is not going to fit. Don't draw anything here,
2115 * will draw "@ " lines below. */
2116 row = wp->w_height + 1;
2117 }
2118 else
2119 {
2120#ifdef FEAT_SEARCH_EXTRA
2121 prepare_search_hl(wp, lnum);
2122#endif
2123#ifdef FEAT_SYN_HL
2124 /* Let the syntax stuff know we skipped a few lines. */
2125 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002126 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 syntax_end_parsing(syntax_last_parsed + 1);
2128#endif
2129
2130 /*
2131 * Display one line.
2132 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002133 row = win_line(wp, lnum, srow, wp->w_height,
2134 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136#ifdef FEAT_FOLDING
2137 wp->w_lines[idx].wl_folded = FALSE;
2138 wp->w_lines[idx].wl_lastlnum = lnum;
2139#endif
2140#ifdef FEAT_SYN_HL
2141 did_update = DID_LINE;
2142 syntax_last_parsed = lnum;
2143#endif
2144 }
2145
2146 wp->w_lines[idx].wl_lnum = lnum;
2147 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002148
2149 /* Past end of the window or end of the screen. Note that after
2150 * resizing wp->w_height may be end up too big. That's a problem
2151 * elsewhere, but prevent a crash here. */
2152 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002155 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2157 ++idx;
2158 break;
2159 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002160 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 wp->w_lines[idx].wl_size = row - srow;
2162 ++idx;
2163#ifdef FEAT_FOLDING
2164 lnum += fold_count + 1;
2165#else
2166 ++lnum;
2167#endif
2168 }
2169 else
2170 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002171 if (wp->w_p_rnu)
2172 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002173#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002174 // 'relativenumber' set: The text doesn't need to be drawn, but
2175 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002176 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2177 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002178 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002179 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002180#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002181 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002182 }
2183
2184 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 row += wp->w_lines[idx++].wl_size;
2186 if (row > wp->w_height) /* past end of screen */
2187 break;
2188#ifdef FEAT_FOLDING
2189 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2190#else
2191 ++lnum;
2192#endif
2193#ifdef FEAT_SYN_HL
2194 did_update = DID_NONE;
2195#endif
2196 }
2197
2198 if (lnum > buf->b_ml.ml_line_count)
2199 {
2200 eof = TRUE;
2201 break;
2202 }
2203 }
2204 /*
2205 * End of loop over all window lines.
2206 */
2207
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002208#ifdef FEAT_VTP
2209 /* Rewrite the character at the end of the screen line. */
2210 if (use_vtp())
2211 {
2212 int i;
2213
2214 for (i = 0; i < Rows; ++i)
2215# ifdef FEAT_MBYTE
2216 if (enc_utf8)
2217 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2218 LineOffset[i] + screen_Columns) > 1)
2219 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2220 else
2221 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2222 else
2223# endif
2224 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2225 }
2226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228 if (idx > wp->w_lines_valid)
2229 wp->w_lines_valid = idx;
2230
2231#ifdef FEAT_SYN_HL
2232 /*
2233 * Let the syntax stuff know we stop parsing here.
2234 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002235 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 syntax_end_parsing(syntax_last_parsed + 1);
2237#endif
2238
2239 /*
2240 * If we didn't hit the end of the file, and we didn't finish the last
2241 * line we were working on, then the line didn't fit.
2242 */
2243 wp->w_empty_rows = 0;
2244#ifdef FEAT_DIFF
2245 wp->w_filler_rows = 0;
2246#endif
2247 if (!eof && !didline)
2248 {
2249 if (lnum == wp->w_topline)
2250 {
2251 /*
2252 * Single line that does not fit!
2253 * Don't overwrite it, it can be edited.
2254 */
2255 wp->w_botline = lnum + 1;
2256 }
2257#ifdef FEAT_DIFF
2258 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2259 {
2260 /* Window ends in filler lines. */
2261 wp->w_botline = lnum;
2262 wp->w_filler_rows = wp->w_height - srow;
2263 }
2264#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002265 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2266 {
2267 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2268
2269 /*
2270 * Last line isn't finished: Display "@@@" in the last screen line.
2271 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002272 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002273 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002274 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002275 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002276 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002277 set_empty_rows(wp, srow);
2278 wp->w_botline = lnum;
2279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2281 {
2282 /*
2283 * Last line isn't finished: Display "@@@" at the end.
2284 */
2285 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2286 W_WINROW(wp) + wp->w_height,
2287 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002288 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 set_empty_rows(wp, srow);
2290 wp->w_botline = lnum;
2291 }
2292 else
2293 {
2294 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2295 wp->w_botline = lnum;
2296 }
2297 }
2298 else
2299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (eof) /* we hit the end of the file */
2302 {
2303 wp->w_botline = buf->b_ml.ml_line_count + 1;
2304#ifdef FEAT_DIFF
2305 j = diff_check_fill(wp, wp->w_botline);
2306 if (j > 0 && !wp->w_botfill)
2307 {
2308 /*
2309 * Display filler lines at the end of the file
2310 */
2311 if (char2cells(fill_diff) > 1)
2312 i = '-';
2313 else
2314 i = fill_diff;
2315 if (row + j > wp->w_height)
2316 j = wp->w_height - row;
2317 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2318 row += j;
2319 }
2320#endif
2321 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002322 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 wp->w_botline = lnum;
2324
2325 /* make sure the rest of the screen is blank */
2326 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002327 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002330#ifdef SYN_TIME_LIMIT
2331 syn_set_timeout(NULL);
2332#endif
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 /* Reset the type of redrawing required, the window has been updated. */
2335 wp->w_redr_type = 0;
2336#ifdef FEAT_DIFF
2337 wp->w_old_topfill = wp->w_topfill;
2338 wp->w_old_botfill = wp->w_botfill;
2339#endif
2340
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002341 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
2343 /*
2344 * There is a trick with w_botline. If we invalidate it on each
2345 * change that might modify it, this will cause a lot of expensive
2346 * calls to plines() in update_topline() each time. Therefore the
2347 * value of w_botline is often approximated, and this value is used to
2348 * compute the value of w_topline. If the value of w_botline was
2349 * wrong, check that the value of w_topline is correct (cursor is on
2350 * the visible part of the text). If it's not, we need to redraw
2351 * again. Mostly this just means scrolling up a few lines, so it
2352 * doesn't look too bad. Only do this for the current window (where
2353 * changes are relevant).
2354 */
2355 wp->w_valid |= VALID_BOTLINE;
2356 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2357 {
2358 recursive = TRUE;
2359 curwin->w_valid &= ~VALID_TOPLINE;
2360 update_topline(); /* may invalidate w_botline again */
2361 if (must_redraw != 0)
2362 {
2363 /* Don't update for changes in buffer again. */
2364 i = curbuf->b_mod_set;
2365 curbuf->b_mod_set = FALSE;
2366 win_update(curwin);
2367 must_redraw = 0;
2368 curbuf->b_mod_set = i;
2369 }
2370 recursive = FALSE;
2371 }
2372 }
2373
2374#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2375 /* restore got_int, unless CTRL-C was hit while redrawing */
2376 if (!got_int)
2377 got_int = save_got_int;
2378#endif
2379}
2380
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381/*
2382 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2383 * as the filler character.
2384 */
2385 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002386win_draw_end(
2387 win_T *wp,
2388 int c1,
2389 int c2,
2390 int row,
2391 int endrow,
2392 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2395 int n = 0;
2396# define FDC_OFF n
2397#else
2398# define FDC_OFF 0
2399#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002400#ifdef FEAT_FOLDING
2401 int fdc = compute_foldcolumn(wp, 0);
2402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404#ifdef FEAT_RIGHTLEFT
2405 if (wp->w_p_rl)
2406 {
2407 /* No check for cmdline window: should never be right-left. */
2408# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002409 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 if (n > 0)
2412 {
2413 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002414 if (n > wp->w_width)
2415 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2417 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002418 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420# endif
2421# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002422 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
2424 int nn = n + 2;
2425
2426 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002427 if (nn > wp->w_width)
2428 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2430 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002431 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 n = nn;
2433 }
2434# endif
2435 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002436 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002437 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2439 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002440 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442 else
2443#endif
2444 {
2445#ifdef FEAT_CMDWIN
2446 if (cmdwin_type != 0 && wp == curwin)
2447 {
2448 /* draw the cmdline character in the leftmost column */
2449 n = 1;
2450 if (n > wp->w_width)
2451 n = wp->w_width;
2452 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002453 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002454 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 }
2456#endif
2457#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002458 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002460 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002463 if (nn > wp->w_width)
2464 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002466 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002467 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 n = nn;
2469 }
2470#endif
2471#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002472 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 int nn = n + 2;
2475
2476 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002477 if (nn > wp->w_width)
2478 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002480 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002481 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 n = nn;
2483 }
2484#endif
2485 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002486 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002487 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489 set_empty_rows(wp, row);
2490}
2491
Bram Moolenaar1a384422010-07-14 19:53:30 +02002492#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002493/*
2494 * Advance **color_cols and return TRUE when there are columns to draw.
2495 */
2496 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002497advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002498{
2499 while (**color_cols >= 0 && vcol > **color_cols)
2500 ++*color_cols;
2501 return (**color_cols >= 0);
2502}
2503#endif
2504
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002505#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2506/*
2507 * Copy "text" to ScreenLines using "attr".
2508 * Returns the next screen column.
2509 */
2510 static int
2511text_to_screenline(win_T *wp, char_u *text, int col)
2512{
2513 int off = (int)(current_ScreenLine - ScreenLines);
2514
2515#ifdef FEAT_MBYTE
2516 if (has_mbyte)
2517 {
2518 int cells;
2519 int u8c, u8cc[MAX_MCO];
2520 int i;
2521 int idx;
2522 int c_len;
2523 char_u *p;
2524# ifdef FEAT_ARABIC
2525 int prev_c = 0; /* previous Arabic character */
2526 int prev_c1 = 0; /* first composing char for prev_c */
2527# endif
2528
2529# ifdef FEAT_RIGHTLEFT
2530 if (wp->w_p_rl)
2531 idx = off;
2532 else
2533# endif
2534 idx = off + col;
2535
2536 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2537 for (p = text; *p != NUL; )
2538 {
2539 cells = (*mb_ptr2cells)(p);
2540 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002541 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002542# ifdef FEAT_RIGHTLEFT
2543 - (wp->w_p_rl ? col : 0)
2544# endif
2545 )
2546 break;
2547 ScreenLines[idx] = *p;
2548 if (enc_utf8)
2549 {
2550 u8c = utfc_ptr2char(p, u8cc);
2551 if (*p < 0x80 && u8cc[0] == 0)
2552 {
2553 ScreenLinesUC[idx] = 0;
2554#ifdef FEAT_ARABIC
2555 prev_c = u8c;
2556#endif
2557 }
2558 else
2559 {
2560#ifdef FEAT_ARABIC
2561 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2562 {
2563 /* Do Arabic shaping. */
2564 int pc, pc1, nc;
2565 int pcc[MAX_MCO];
2566 int firstbyte = *p;
2567
2568 /* The idea of what is the previous and next
2569 * character depends on 'rightleft'. */
2570 if (wp->w_p_rl)
2571 {
2572 pc = prev_c;
2573 pc1 = prev_c1;
2574 nc = utf_ptr2char(p + c_len);
2575 prev_c1 = u8cc[0];
2576 }
2577 else
2578 {
2579 pc = utfc_ptr2char(p + c_len, pcc);
2580 nc = prev_c;
2581 pc1 = pcc[0];
2582 }
2583 prev_c = u8c;
2584
2585 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2586 pc, pc1, nc);
2587 ScreenLines[idx] = firstbyte;
2588 }
2589 else
2590 prev_c = u8c;
2591#endif
2592 /* Non-BMP character: display as ? or fullwidth ?. */
2593#ifdef UNICODE16
2594 if (u8c >= 0x10000)
2595 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2596 else
2597#endif
2598 ScreenLinesUC[idx] = u8c;
2599 for (i = 0; i < Screen_mco; ++i)
2600 {
2601 ScreenLinesC[i][idx] = u8cc[i];
2602 if (u8cc[i] == 0)
2603 break;
2604 }
2605 }
2606 if (cells > 1)
2607 ScreenLines[idx + 1] = 0;
2608 }
2609 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2610 /* double-byte single width character */
2611 ScreenLines2[idx] = p[1];
2612 else if (cells > 1)
2613 /* double-width character */
2614 ScreenLines[idx + 1] = p[1];
2615 col += cells;
2616 idx += cells;
2617 p += c_len;
2618 }
2619 }
2620 else
2621#endif
2622 {
2623 int len = (int)STRLEN(text);
2624
Bram Moolenaar02631462017-09-22 15:20:32 +02002625 if (len > wp->w_width - col)
2626 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002627 if (len > 0)
2628 {
2629#ifdef FEAT_RIGHTLEFT
2630 if (wp->w_p_rl)
2631 STRNCPY(current_ScreenLine, text, len);
2632 else
2633#endif
2634 STRNCPY(current_ScreenLine + col, text, len);
2635 col += len;
2636 }
2637 }
2638 return col;
2639}
2640#endif
2641
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#ifdef FEAT_FOLDING
2643/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002644 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2645 * space is available for window "wp", minus "col".
2646 */
2647 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002648compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002649{
2650 int fdc = wp->w_p_fdc;
2651 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002652 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002653
2654 if (fdc > wwidth - (col + wmw))
2655 fdc = wwidth - (col + wmw);
2656 return fdc;
2657}
2658
2659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 * Display one folded line.
2661 */
2662 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002663fold_line(
2664 win_T *wp,
2665 long fold_count,
2666 foldinfo_T *foldinfo,
2667 linenr_T lnum,
2668 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002670 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 pos_T *top, *bot;
2672 linenr_T lnume = lnum + fold_count - 1;
2673 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002674 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 int col;
2677 int txtcol;
2678 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002679 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 /* Build the fold line:
2682 * 1. Add the cmdwin_type for the command-line window
2683 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002684 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 * 4. Compose the text
2686 * 5. Add the text
2687 * 6. set highlighting for the Visual area an other text
2688 */
2689 col = 0;
2690
2691 /*
2692 * 1. Add the cmdwin_type for the command-line window
2693 * Ignores 'rightleft', this window is never right-left.
2694 */
2695#ifdef FEAT_CMDWIN
2696 if (cmdwin_type != 0 && wp == curwin)
2697 {
2698 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002699 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700#ifdef FEAT_MBYTE
2701 if (enc_utf8)
2702 ScreenLinesUC[off] = 0;
2703#endif
2704 ++col;
2705 }
2706#endif
2707
2708 /*
2709 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002710 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002712 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (fdc > 0)
2714 {
2715 fill_foldcolumn(buf, wp, TRUE, lnum);
2716#ifdef FEAT_RIGHTLEFT
2717 if (wp->w_p_rl)
2718 {
2719 int i;
2720
Bram Moolenaar02631462017-09-22 15:20:32 +02002721 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002722 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 /* reverse the fold column */
2724 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002725 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
2727 else
2728#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002729 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 col += fdc;
2731 }
2732
2733#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002734# define RL_MEMSET(p, v, l) \
2735 do { \
2736 if (wp->w_p_rl) \
2737 for (ri = 0; ri < l; ++ri) \
2738 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2739 else \
2740 for (ri = 0; ri < l; ++ri) \
2741 ScreenAttrs[off + (p) + ri] = v; \
2742 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002744# define RL_MEMSET(p, v, l) \
2745 do { \
2746 for (ri = 0; ri < l; ++ri) \
2747 ScreenAttrs[off + (p) + ri] = v; \
2748 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749#endif
2750
Bram Moolenaar64486672010-05-16 15:46:46 +02002751 /* Set all attributes of the 'number' or 'relativenumber' column and the
2752 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002753 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755#ifdef FEAT_SIGNS
2756 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002757 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002759 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (len > 0)
2761 {
2762 if (len > 2)
2763 len = 2;
2764# ifdef FEAT_RIGHTLEFT
2765 if (wp->w_p_rl)
2766 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002767 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002768 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else
2770# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002771 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 col += len;
2773 }
2774 }
2775#endif
2776
2777 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002778 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002780 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002782 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (len > 0)
2784 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002785 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002786 long num;
2787 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002788
2789 if (len > w + 1)
2790 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002791
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002792 if (wp->w_p_nu && !wp->w_p_rnu)
2793 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002794 num = (long)lnum;
2795 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002796 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002797 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002798 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002799 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002800 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002801 /* 'number' + 'relativenumber': cursor line shows absolute
2802 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002803 num = lnum;
2804 fmt = "%-*ld ";
2805 }
2806 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002807
Bram Moolenaar700e7342013-01-30 12:31:36 +01002808 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809#ifdef FEAT_RIGHTLEFT
2810 if (wp->w_p_rl)
2811 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002812 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002813 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 else
2815#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002816 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 col += len;
2818 }
2819 }
2820
2821 /*
2822 * 4. Compose the folded-line string with 'foldtext', if set.
2823 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002824 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
2826 txtcol = col; /* remember where text starts */
2827
2828 /*
2829 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2830 * Right-left text is put in columns 0 - number-col, normal text is put
2831 * in columns number-col - window-width.
2832 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002833 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 /* Fill the rest of the line with the fold filler */
2836#ifdef FEAT_RIGHTLEFT
2837 if (wp->w_p_rl)
2838 col -= txtcol;
2839#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002840 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841#ifdef FEAT_RIGHTLEFT
2842 - (wp->w_p_rl ? txtcol : 0)
2843#endif
2844 )
2845 {
2846#ifdef FEAT_MBYTE
2847 if (enc_utf8)
2848 {
2849 if (fill_fold >= 0x80)
2850 {
2851 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002852 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002853 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 }
2855 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002858 ScreenLines[off + col] = fill_fold;
2859 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002860 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002862 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002864 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
2866
2867 if (text != buf)
2868 vim_free(text);
2869
2870 /*
2871 * 6. set highlighting for the Visual area an other text.
2872 * If all folded lines are in the Visual area, highlight the line.
2873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2875 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002876 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 {
2878 /* Visual is after curwin->w_cursor */
2879 top = &curwin->w_cursor;
2880 bot = &VIsual;
2881 }
2882 else
2883 {
2884 /* Visual is before curwin->w_cursor */
2885 top = &VIsual;
2886 bot = &curwin->w_cursor;
2887 }
2888 if (lnum >= top->lnum
2889 && lnume <= bot->lnum
2890 && (VIsual_mode != 'v'
2891 || ((lnum > top->lnum
2892 || (lnum == top->lnum
2893 && top->col == 0))
2894 && (lnume < bot->lnum
2895 || (lnume == bot->lnum
2896 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002897 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
2899 if (VIsual_mode == Ctrl_V)
2900 {
2901 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002902 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002904 if (wp->w_old_cursor_lcol != MAXCOL
2905 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002906 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 len = wp->w_old_cursor_lcol;
2908 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002910 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002911 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913 }
2914 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002917 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002922#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002923 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2924 if (wp->w_p_cc_cols)
2925 {
2926 int i = 0;
2927 int j = wp->w_p_cc_cols[i];
2928 int old_txtcol = txtcol;
2929
2930 while (j > -1)
2931 {
2932 txtcol += j;
2933 if (wp->w_p_wrap)
2934 txtcol -= wp->w_skipcol;
2935 else
2936 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002937 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002938 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002939 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002940 txtcol = old_txtcol;
2941 j = wp->w_p_cc_cols[++i];
2942 }
2943 }
2944
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002945 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002946 if (wp->w_p_cuc)
2947 {
2948 txtcol += wp->w_virtcol;
2949 if (wp->w_p_wrap)
2950 txtcol -= wp->w_skipcol;
2951 else
2952 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002953 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002954 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002955 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002956 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
Bram Moolenaar02631462017-09-22 15:20:32 +02002959 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2960 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 /*
2963 * Update w_cline_height and w_cline_folded if the cursor line was
2964 * updated (saves a call to plines() later).
2965 */
2966 if (wp == curwin
2967 && lnum <= curwin->w_cursor.lnum
2968 && lnume >= curwin->w_cursor.lnum)
2969 {
2970 curwin->w_cline_row = row;
2971 curwin->w_cline_height = 1;
2972 curwin->w_cline_folded = TRUE;
2973 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2974 }
2975}
2976
2977/*
2978 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2979 */
2980 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002981copy_text_attr(
2982 int off,
2983 char_u *buf,
2984 int len,
2985 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002987 int i;
2988
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 mch_memmove(ScreenLines + off, buf, (size_t)len);
2990# ifdef FEAT_MBYTE
2991 if (enc_utf8)
2992 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2993# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002994 for (i = 0; i < len; ++i)
2995 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996}
2997
2998/*
2999 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003000 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 */
3002 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003003fill_foldcolumn(
3004 char_u *p,
3005 win_T *wp,
3006 int closed, /* TRUE of FALSE */
3007 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 int i = 0;
3010 int level;
3011 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003012 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003013 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003016 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
3018 level = win_foldinfo.fi_level;
3019 if (level > 0)
3020 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003021 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003022 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 /* If the column is too narrow, we start at the lowest level that
3025 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003026 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (first_level < 1)
3028 first_level = 1;
3029
Bram Moolenaar1c934292015-01-27 16:39:29 +01003030 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 {
3032 if (win_foldinfo.fi_lnum == lnum
3033 && first_level + i >= win_foldinfo.fi_low_level)
3034 p[i] = '-';
3035 else if (first_level == 1)
3036 p[i] = '|';
3037 else if (first_level + i <= 9)
3038 p[i] = '0' + first_level + i;
3039 else
3040 p[i] = '>';
3041 if (first_level + i == level)
3042 break;
3043 }
3044 }
3045 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003046 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047}
3048#endif /* FEAT_FOLDING */
3049
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003050#ifdef FEAT_TEXT_PROP
3051static textprop_T *current_text_props = NULL;
3052static buf_T *current_buf = NULL;
3053
3054 static int
3055#ifdef __BORLANDC__
3056_RTLENTRYF
3057#endif
3058text_prop_compare(const void *s1, const void *s2)
3059{
3060 int idx1, idx2;
3061 proptype_T *pt1, *pt2;
3062 colnr_T col1, col2;
3063
3064 idx1 = *(int *)s1;
3065 idx2 = *(int *)s2;
3066 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3067 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3068 if (pt1 == pt2)
3069 return 0;
3070 if (pt1 == NULL)
3071 return -1;
3072 if (pt2 == NULL)
3073 return 1;
3074 if (pt1->pt_priority != pt2->pt_priority)
3075 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3076 col1 = current_text_props[idx1].tp_col;
3077 col2 = current_text_props[idx2].tp_col;
3078 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3079}
3080#endif
3081
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082/*
3083 * Display line "lnum" of window 'wp' on the screen.
3084 * Start at row "startrow", stop when "endrow" is reached.
3085 * wp->w_virtcol needs to be valid.
3086 *
3087 * Return the number of last row the line occupies.
3088 */
3089 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003090win_line(
3091 win_T *wp,
3092 linenr_T lnum,
3093 int startrow,
3094 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003095 int nochange UNUSED, // not updating for changed text
3096 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003098 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3100 int c = 0; /* init for GCC */
3101 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003102#ifdef FEAT_LINEBREAK
3103 long vcol_sbr = -1; /* virtual column after showbreak */
3104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 long vcol_prev = -1; /* "vcol" of previous character */
3106 char_u *line; /* current line */
3107 char_u *ptr; /* current position in "line" */
3108 int row; /* row in the window, excl w_winrow */
3109 int screen_row; /* row on the screen, incl w_winrow */
3110
3111 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3112 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003113 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003114 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 int c_extra = NUL; /* extra chars, all the same */
3116 int extra_attr = 0; /* attributes when n_extra != 0 */
3117 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3118 displaying lcs_eol at end-of-line */
3119 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3120 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3121
3122 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3123 int saved_n_extra = 0;
3124 char_u *saved_p_extra = NULL;
3125 int saved_c_extra = 0;
3126 int saved_char_attr = 0;
3127
3128 int n_attr = 0; /* chars with special attr */
3129 int saved_attr2 = 0; /* char_attr saved for n_attr */
3130 int n_attr3 = 0; /* chars with overruling special attr */
3131 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3132
3133 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3134
3135 int fromcol, tocol; /* start/end of inverting */
3136 int fromcol_prev = -2; /* start of inverting after cursor */
3137 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003139 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 pos_T pos;
3141 long v;
3142
3143 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003144 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3146 in this line */
3147 int attr = 0; /* attributes for area highlighting */
3148 int area_attr = 0; /* attributes desired by highlighting */
3149 int search_attr = 0; /* attributes desired by 'hlsearch' */
3150#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003151 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 int syntax_attr = 0; /* attributes desired by syntax */
3153 int has_syntax = FALSE; /* this buffer has syntax highl. */
3154 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003155 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003156 int draw_color_col = FALSE; /* highlight colorcolumn */
3157 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003158#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003159#ifdef FEAT_TEXT_PROP
3160 int text_prop_count;
3161 int text_prop_next = 0; // next text property to use
3162 textprop_T *text_props = NULL;
3163 int *text_prop_idxs = NULL;
3164 int text_props_active = 0;
3165 proptype_T *text_prop_type = NULL;
3166 int text_prop_attr = 0;
3167#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003168#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003169 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003170# define SPWORDLEN 150
3171 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003172 int nextlinecol = 0; /* column where nextline[] starts */
3173 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003174 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003175 int spell_attr = 0; /* attributes desired by spelling */
3176 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003177 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3178 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003179 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003180 static int cap_col = -1; /* column to check for Cap word */
3181 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003182 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003184 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185#ifdef FEAT_MBYTE
3186 int multi_attr = 0; /* attributes desired by multibyte */
3187 int mb_l = 1; /* multi-byte byte length */
3188 int mb_c = 0; /* decoded multi-byte character */
3189 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003190 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191#endif
3192#ifdef FEAT_DIFF
3193 int filler_lines; /* nr of filler lines to be drawn */
3194 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003195 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 int change_start = MAXCOL; /* first col of changed area */
3197 int change_end = -1; /* last col of changed area */
3198#endif
3199 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3200#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003201 int need_showbreak = FALSE; /* overlong line, skipping first x
3202 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003204#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003205 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003207 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#endif
3209#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003210 matchitem_T *cur; /* points to the match list */
3211 match_T *shl; /* points to search_hl or a match */
3212 int shl_flag; /* flag to indicate whether search_hl
3213 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003214 int pos_inprogress; /* marks that position match search is
3215 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003216 int prevcol_hl_flag; /* flag to indicate whether prevcol
3217 equals startcol of search_hl or one
3218 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#endif
3220#ifdef FEAT_ARABIC
3221 int prev_c = 0; /* previous Arabic character */
3222 int prev_c1 = 0; /* first composing char for prev_c */
3223#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003224#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003225 int did_line_attr = 0;
3226#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003227#ifdef FEAT_TERMINAL
3228 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003229 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
3232 /* draw_state: items that are drawn in sequence: */
3233#define WL_START 0 /* nothing done yet */
3234#ifdef FEAT_CMDWIN
3235# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3236#else
3237# define WL_CMDLINE WL_START
3238#endif
3239#ifdef FEAT_FOLDING
3240# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3241#else
3242# define WL_FOLD WL_CMDLINE
3243#endif
3244#ifdef FEAT_SIGNS
3245# define WL_SIGN WL_FOLD + 1 /* column for signs */
3246#else
3247# define WL_SIGN WL_FOLD /* column for signs */
3248#endif
3249#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003250#ifdef FEAT_LINEBREAK
3251# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003253# define WL_BRI WL_NR
3254#endif
3255#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3256# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3257#else
3258# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259#endif
3260#define WL_LINE WL_SBR + 1 /* text in the line */
3261 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003262#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 int feedback_col = 0;
3264 int feedback_old_attr = -1;
3265#endif
3266
Bram Moolenaar860cae12010-06-05 23:22:07 +02003267#ifdef FEAT_CONCEAL
3268 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003269 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003270 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003271 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003272 int is_concealing = FALSE;
3273 int boguscols = 0; /* nonexistent columns added to force
3274 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003275 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003276 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003277 int match_conc = 0; /* cchar for match functions */
3278 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003279 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003280# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003281# define FIX_FOR_BOGUSCOLS \
3282 { \
3283 n_extra += vcol_off; \
3284 vcol -= vcol_off; \
3285 vcol_off = 0; \
3286 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003287 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003288 boguscols = 0; \
3289 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003290#else
3291# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294 if (startrow > endrow) /* past the end already! */
3295 return startrow;
3296
3297 row = startrow;
3298 screen_row = row + W_WINROW(wp);
3299
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003300 if (!number_only)
3301 {
3302 /*
3303 * To speed up the loop below, set extra_check when there is linebreak,
3304 * trailing white space and/or syntax processing to be done.
3305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003307 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#endif
3309#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003310 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003311# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003312 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003313# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003314 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003316 /* Prepare for syntax highlighting in this line. When there is an
3317 * error, stop syntax highlighting. */
3318 save_did_emsg = did_emsg;
3319 did_emsg = FALSE;
3320 syntax_start(wp, lnum);
3321 if (did_emsg)
3322 wp->w_s->b_syn_error = TRUE;
3323 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003324 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003325 did_emsg = save_did_emsg;
3326#ifdef SYN_TIME_LIMIT
3327 if (!wp->w_s->b_syn_slow)
3328#endif
3329 {
3330 has_syntax = TRUE;
3331 extra_check = TRUE;
3332 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003335
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003336 /* Check for columns to display for 'colorcolumn'. */
3337 color_cols = wp->w_p_cc_cols;
3338 if (color_cols != NULL)
3339 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003340#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003341
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003342#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003343 if (term_show_buffer(wp->w_buffer))
3344 {
3345 extra_check = TRUE;
3346 get_term_attr = TRUE;
3347 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3348 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003349#endif
3350
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003351#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003352 if (wp->w_p_spell
3353 && *wp->w_s->b_p_spl != NUL
3354 && wp->w_s->b_langp.ga_len > 0
3355 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003356 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 /* Prepare for spell checking. */
3358 has_spell = TRUE;
3359 extra_check = TRUE;
3360
Bram Moolenaar7701f302018-10-02 21:20:32 +02003361 /* Get the start of the next line, so that words that wrap to the
3362 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 * Trick: skip a few chars for C/shell/Vim comments */
3364 nextline[SPWORDLEN] = NUL;
3365 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3366 {
3367 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3368 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3369 }
3370
Bram Moolenaar7701f302018-10-02 21:20:32 +02003371 /* When a word wrapped from the previous line the start of the
3372 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003373 if (lnum == checked_lnum)
3374 cur_checked_col = checked_col;
3375 checked_lnum = 0;
3376
3377 /* When there was a sentence end in the previous line may require a
3378 * word starting with capital in this line. In line 1 always check
3379 * the first word. */
3380 if (lnum != capcol_lnum)
3381 cap_col = -1;
3382 if (lnum == 1)
3383 cap_col = 0;
3384 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#endif
3387
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003388 /*
3389 * handle visual active in this window
3390 */
3391 fromcol = -10;
3392 tocol = MAXCOL;
3393 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003395 /* Visual is after curwin->w_cursor */
3396 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003398 top = &curwin->w_cursor;
3399 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003401 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003403 top = &VIsual;
3404 bot = &curwin->w_cursor;
3405 }
3406 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3407 if (VIsual_mode == Ctrl_V) /* block mode */
3408 {
3409 if (lnum_in_visual_area)
3410 {
3411 fromcol = wp->w_old_cursor_fcol;
3412 tocol = wp->w_old_cursor_lcol;
3413 }
3414 }
3415 else /* non-block mode */
3416 {
3417 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003419 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003421 if (VIsual_mode == 'V') /* linewise */
3422 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 else
3424 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003425 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3426 if (gchar_pos(top) == NUL)
3427 tocol = fromcol + 1;
3428 }
3429 }
3430 if (VIsual_mode != 'V' && lnum == bot->lnum)
3431 {
3432 if (*p_sel == 'e' && bot->col == 0
3433#ifdef FEAT_VIRTUALEDIT
3434 && bot->coladd == 0
3435#endif
3436 )
3437 {
3438 fromcol = -10;
3439 tocol = MAXCOL;
3440 }
3441 else if (bot->col == MAXCOL)
3442 tocol = MAXCOL;
3443 else
3444 {
3445 pos = *bot;
3446 if (*p_sel == 'e')
3447 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3448 else
3449 {
3450 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3451 ++tocol;
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454 }
3455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003457 /* Check if the character under the cursor should not be inverted */
3458 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003459#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003460 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003461#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 )
3463 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003465 /* if inverting in this line set area_highlighting */
3466 if (fromcol >= 0)
3467 {
3468 area_highlighting = TRUE;
3469 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003471 if ((clip_star.available && !clip_star.owned
3472 && clip_isautosel_star())
3473 || (clip_plus.available && !clip_plus.owned
3474 && clip_isautosel_plus()))
3475 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003480 /*
3481 * handle 'incsearch' and ":s///c" highlighting
3482 */
3483 else if (highlight_match
3484 && wp == curwin
3485 && lnum >= curwin->w_cursor.lnum
3486 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003488 if (lnum == curwin->w_cursor.lnum)
3489 getvcol(curwin, &(curwin->w_cursor),
3490 (colnr_T *)&fromcol, NULL, NULL);
3491 else
3492 fromcol = 0;
3493 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3494 {
3495 pos.lnum = lnum;
3496 pos.col = search_match_endcol;
3497 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3498 }
3499 else
3500 tocol = MAXCOL;
3501 /* do at least one character; happens when past end of line */
3502 if (fromcol == tocol)
3503 tocol = fromcol + 1;
3504 area_highlighting = TRUE;
3505 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508
3509#ifdef FEAT_DIFF
3510 filler_lines = diff_check(wp, lnum);
3511 if (filler_lines < 0)
3512 {
3513 if (filler_lines == -1)
3514 {
3515 if (diff_find_change(wp, lnum, &change_start, &change_end))
3516 diff_hlf = HLF_ADD; /* added line */
3517 else if (change_start == 0)
3518 diff_hlf = HLF_TXD; /* changed text */
3519 else
3520 diff_hlf = HLF_CHD; /* changed line */
3521 }
3522 else
3523 diff_hlf = HLF_ADD; /* added line */
3524 filler_lines = 0;
3525 area_highlighting = TRUE;
3526 }
3527 if (lnum == wp->w_topline)
3528 filler_lines = wp->w_topfill;
3529 filler_todo = filler_lines;
3530#endif
3531
3532#ifdef LINE_ATTR
3533# ifdef FEAT_SIGNS
3534 /* If this line has a sign with line highlighting set line_attr. */
3535 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3536 if (v != 0)
3537 line_attr = sign_get_attr((int)v, TRUE);
3538# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003539# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003541 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003542 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543# endif
3544 if (line_attr != 0)
3545 area_highlighting = TRUE;
3546#endif
3547
3548 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3549 ptr = line;
3550
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003551#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003552 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003553 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003554 /* For checking first word with a capital skip white space. */
3555 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003556 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003557
Bram Moolenaar30abd282005-06-22 22:35:10 +00003558 /* To be able to spell-check over line boundaries copy the end of the
3559 * current line into nextline[]. Above the start of the next line was
3560 * copied to nextline[SPWORDLEN]. */
3561 if (nextline[SPWORDLEN] == NUL)
3562 {
3563 /* No next line or it is empty. */
3564 nextlinecol = MAXCOL;
3565 nextline_idx = 0;
3566 }
3567 else
3568 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003569 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003570 if (v < SPWORDLEN)
3571 {
3572 /* Short line, use it completely and append the start of the
3573 * next line. */
3574 nextlinecol = 0;
3575 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003576 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003577 nextline_idx = v + 1;
3578 }
3579 else
3580 {
3581 /* Long line, use only the last SPWORDLEN bytes. */
3582 nextlinecol = v - SPWORDLEN;
3583 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3584 nextline_idx = SPWORDLEN + 1;
3585 }
3586 }
3587 }
3588#endif
3589
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003590 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003592 if (lcs_space || lcs_trail)
3593 extra_check = TRUE;
3594 /* find start of trailing whitespace */
3595 if (lcs_trail)
3596 {
3597 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003598 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003599 --trailcol;
3600 trailcol += (colnr_T) (ptr - line);
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603
3604 /*
3605 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3606 * first character to be displayed.
3607 */
3608 if (wp->w_p_wrap)
3609 v = wp->w_skipcol;
3610 else
3611 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003612 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614#ifdef FEAT_MBYTE
3615 char_u *prev_ptr = ptr;
3616#endif
3617 while (vcol < v && *ptr != NUL)
3618 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003619 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 vcol += c;
3621#ifdef FEAT_MBYTE
3622 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003624 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003627 /* When:
3628 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003629 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003630 * - 'virtualedit' is set, or
3631 * - the visual mode is active,
3632 * the end of the line may be before the start of the displayed part.
3633 */
3634 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003635#ifdef FEAT_SYN_HL
3636 wp->w_p_cuc || draw_color_col ||
3637#endif
3638#ifdef FEAT_VIRTUALEDIT
3639 virtual_active() ||
3640#endif
3641 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646 /* Handle a character that's not completely on the screen: Put ptr at
3647 * that character but skip the first few screen characters. */
3648 if (vcol > v)
3649 {
3650 vcol -= c;
3651#ifdef FEAT_MBYTE
3652 ptr = prev_ptr;
3653#else
3654 --ptr;
3655#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003656 /* If the character fits on the screen, don't need to skip it.
3657 * Except for a TAB. */
3658 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003659#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003660 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003661#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003662 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003663 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665
3666 /*
3667 * Adjust for when the inverted text is before the screen,
3668 * and when the start of the inverted text is before the screen.
3669 */
3670 if (tocol <= vcol)
3671 fromcol = 0;
3672 else if (fromcol >= 0 && fromcol < vcol)
3673 fromcol = vcol;
3674
3675#ifdef FEAT_LINEBREAK
3676 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3677 if (wp->w_p_wrap)
3678 need_showbreak = TRUE;
3679#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003680#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003681 /* When spell checking a word we need to figure out the start of the
3682 * word and if it's badly spelled or not. */
3683 if (has_spell)
3684 {
3685 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003686 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003687 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003688
3689 pos = wp->w_cursor;
3690 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003691 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003692 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003693
3694 /* spell_move_to() may call ml_get() and make "line" invalid */
3695 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3696 ptr = line + linecol;
3697
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003698 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003699 {
3700 /* no bad word found at line start, don't check until end of a
3701 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003702 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003703 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003704 }
3705 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003706 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003707 /* bad word found, use attributes until end of word */
3708 word_end = wp->w_cursor.col + len + 1;
3709
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003710 /* Turn index into actual attributes. */
3711 if (spell_hlf != HLF_COUNT)
3712 spell_attr = highlight_attr[spell_hlf];
3713 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003714 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003715
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003716# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003717 /* Need to restart syntax highlighting for this line. */
3718 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003719 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003720# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003721 }
3722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724
3725 /*
3726 * Correct highlighting for cursor that can't be disabled.
3727 * Avoids having to check this for each character.
3728 */
3729 if (fromcol >= 0)
3730 {
3731 if (noinvcur)
3732 {
3733 if ((colnr_T)fromcol == wp->w_virtcol)
3734 {
3735 /* highlighting starts at cursor, let it start just after the
3736 * cursor */
3737 fromcol_prev = fromcol;
3738 fromcol = -1;
3739 }
3740 else if ((colnr_T)fromcol < wp->w_virtcol)
3741 /* restart highlighting after the cursor */
3742 fromcol_prev = wp->w_virtcol;
3743 }
3744 if (fromcol >= tocol)
3745 fromcol = -1;
3746 }
3747
3748#ifdef FEAT_SEARCH_EXTRA
3749 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003750 * Handle highlighting the last used search pattern and matches.
3751 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003753 cur = wp->w_match_head;
3754 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003755 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003757 if (shl_flag == FALSE)
3758 {
3759 shl = &search_hl;
3760 shl_flag = TRUE;
3761 }
3762 else
3763 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003764 shl->startcol = MAXCOL;
3765 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003767 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003768 v = (long)(ptr - line);
3769 if (cur != NULL)
3770 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003771 next_search_hl(wp, shl, lnum, (colnr_T)v,
3772 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003773
3774 /* Need to get the line again, a multi-line regexp may have made it
3775 * invalid. */
3776 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3777 ptr = line + v;
3778
3779 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003781 if (shl->lnum == lnum)
3782 shl->startcol = shl->rm.startpos[0].col;
3783 else
3784 shl->startcol = 0;
3785 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3786 - shl->rm.startpos[0].lnum)
3787 shl->endcol = shl->rm.endpos[0].col;
3788 else
3789 shl->endcol = MAXCOL;
3790 /* Highlight one character for an empty match. */
3791 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003794 if (has_mbyte && line[shl->endcol] != NUL)
3795 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003798 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003800 if ((long)shl->startcol < v) /* match at leftcol */
3801 {
3802 shl->attr_cur = shl->attr;
3803 search_attr = shl->attr;
3804 }
3805 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003807 if (shl != &search_hl && cur != NULL)
3808 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810#endif
3811
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003812#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003813 /* Cursor line highlighting for 'cursorline' in the current window. Not
3814 * when Visual mode is active, because it's not clear what is selected
3815 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003816 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003817 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003818 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003819 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003820 area_highlighting = TRUE;
3821 }
3822#endif
3823
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003824#ifdef FEAT_TEXT_PROP
3825 {
3826 char_u *prop_start;
3827
3828 text_prop_count = get_text_props(wp->w_buffer, lnum,
3829 &prop_start, FALSE);
3830 if (text_prop_count > 0)
3831 {
3832 // Make a copy of the properties, so that they are properly
3833 // aligned.
3834 text_props = (textprop_T *)alloc(
3835 text_prop_count * sizeof(textprop_T));
3836 if (text_props != NULL)
3837 mch_memmove(text_props, prop_start,
3838 text_prop_count * sizeof(textprop_T));
3839
3840 // Allocate an array for the indexes.
3841 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3842 area_highlighting = TRUE;
3843 extra_check = TRUE;
3844 }
3845 }
3846#endif
3847
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003848 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 col = 0;
3850#ifdef FEAT_RIGHTLEFT
3851 if (wp->w_p_rl)
3852 {
3853 /* Rightleft window: process the text in the normal direction, but put
3854 * it in current_ScreenLine[] from right to left. Start at the
3855 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003856 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 off += col;
3858 }
3859#endif
3860
3861 /*
3862 * Repeat for the whole displayed line.
3863 */
3864 for (;;)
3865 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003866#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003867 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 /* Skip this quickly when working on the text. */
3870 if (draw_state != WL_LINE)
3871 {
3872#ifdef FEAT_CMDWIN
3873 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3874 {
3875 draw_state = WL_CMDLINE;
3876 if (cmdwin_type != 0 && wp == curwin)
3877 {
3878 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003880 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003881 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883 }
3884#endif
3885
3886#ifdef FEAT_FOLDING
3887 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3888 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003889 int fdc = compute_foldcolumn(wp, 0);
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003892 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003894 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003895 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003896 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003897 p_extra_free = alloc(12 + 1);
3898
3899 if (p_extra_free != NULL)
3900 {
3901 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3902 n_extra = fdc;
3903 p_extra_free[n_extra] = NUL;
3904 p_extra = p_extra_free;
3905 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003906 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909 }
3910#endif
3911
3912#ifdef FEAT_SIGNS
3913 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3914 {
3915 draw_state = WL_SIGN;
3916 /* Show the sign column when there are any signs in this
3917 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003918 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003920 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003922 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923# endif
3924
3925 /* Draw two cells with the sign value or blank. */
3926 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003927 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 n_extra = 2;
3929
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003930 if (row == startrow
3931#ifdef FEAT_DIFF
3932 + filler_lines && filler_todo <= 0
3933#endif
3934 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3937 SIGN_TEXT);
3938# ifdef FEAT_SIGN_ICONS
3939 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3940 SIGN_ICON);
3941 if (gui.in_use && icon_sign != 0)
3942 {
3943 /* Use the image in this position. */
3944 c_extra = SIGN_BYTE;
3945# ifdef FEAT_NETBEANS_INTG
3946 if (buf_signcount(wp->w_buffer, lnum) > 1)
3947 c_extra = MULTISIGN_BYTE;
3948# endif
3949 char_attr = icon_sign;
3950 }
3951 else
3952# endif
3953 if (text_sign != 0)
3954 {
3955 p_extra = sign_get_text(text_sign);
3956 if (p_extra != NULL)
3957 {
3958 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003959 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 char_attr = sign_get_attr(text_sign, FALSE);
3962 }
3963 }
3964 }
3965 }
3966#endif
3967
3968 if (draw_state == WL_NR - 1 && n_extra == 0)
3969 {
3970 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003971 /* Display the absolute or relative line number. After the
3972 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3973 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 && (row == startrow
3975#ifdef FEAT_DIFF
3976 + filler_lines
3977#endif
3978 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3979 {
3980 /* Draw the line number (empty space after wrapping). */
3981 if (row == startrow
3982#ifdef FEAT_DIFF
3983 + filler_lines
3984#endif
3985 )
3986 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003987 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003988 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003989
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003990 if (wp->w_p_nu && !wp->w_p_rnu)
3991 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003992 num = (long)lnum;
3993 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003994 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003995 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003996 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003997 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003998 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003999 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004000 num = lnum;
4001 fmt = "%-*ld ";
4002 }
4003 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004004
Bram Moolenaar700e7342013-01-30 12:31:36 +01004005 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004006 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (wp->w_skipcol > 0)
4008 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4009 *p_extra = '-';
4010#ifdef FEAT_RIGHTLEFT
4011 if (wp->w_p_rl) /* reverse line numbers */
4012 rl_mirror(extra);
4013#endif
4014 p_extra = extra;
4015 c_extra = NUL;
4016 }
4017 else
4018 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004019 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004020 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004021#ifdef FEAT_SYN_HL
4022 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004023 * the current line differently.
4024 * TODO: Can we use CursorLine instead of CursorLineNr
4025 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004026 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004027 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004028 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031 }
4032
Bram Moolenaar597a4222014-06-25 14:39:50 +02004033#ifdef FEAT_LINEBREAK
4034 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4035 && n_extra == 0 && *p_sbr != NUL)
4036 /* draw indent after showbreak value */
4037 draw_state = WL_BRI;
4038 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4039 /* After the showbreak, draw the breakindent */
4040 draw_state = WL_BRI - 1;
4041
4042 /* draw 'breakindent': indent wrapped text accordingly */
4043 if (draw_state == WL_BRI - 1 && n_extra == 0)
4044 {
4045 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004046 /* if need_showbreak is set, breakindent also applies */
4047 if (wp->w_p_bri && n_extra == 0
4048 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004049# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004050 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004051# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004052 )
4053 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004054 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004055# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004056 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004057 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004058 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004059# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004060 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4061 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004062 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004063# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004064 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004065# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004066 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004067 c_extra = ' ';
4068 n_extra = get_breakindent_win(wp,
4069 ml_get_buf(wp->w_buffer, lnum, FALSE));
4070 /* Correct end of highlighted area for 'breakindent',
4071 * required when 'linebreak' is also set. */
4072 if (tocol == vcol)
4073 tocol += n_extra;
4074 }
4075 }
4076#endif
4077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4079 if (draw_state == WL_SBR - 1 && n_extra == 0)
4080 {
4081 draw_state = WL_SBR;
4082# ifdef FEAT_DIFF
4083 if (filler_todo > 0)
4084 {
4085 /* Draw "deleted" diff line(s). */
4086 if (char2cells(fill_diff) > 1)
4087 c_extra = '-';
4088 else
4089 c_extra = fill_diff;
4090# ifdef FEAT_RIGHTLEFT
4091 if (wp->w_p_rl)
4092 n_extra = col + 1;
4093 else
4094# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004095 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004096 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098# endif
4099# ifdef FEAT_LINEBREAK
4100 if (*p_sbr != NUL && need_showbreak)
4101 {
4102 /* Draw 'showbreak' at the start of each broken line. */
4103 p_extra = p_sbr;
4104 c_extra = NUL;
4105 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004106 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004108 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 /* Correct end of highlighted area for 'showbreak',
4110 * required when 'linebreak' is also set. */
4111 if (tocol == vcol)
4112 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004113#ifdef FEAT_SYN_HL
4114 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004115 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004116 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004117 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120# endif
4121 }
4122#endif
4123
4124 if (draw_state == WL_LINE - 1 && n_extra == 0)
4125 {
4126 draw_state = WL_LINE;
4127 if (saved_n_extra)
4128 {
4129 /* Continue item from end of wrapped line. */
4130 n_extra = saved_n_extra;
4131 c_extra = saved_c_extra;
4132 p_extra = saved_p_extra;
4133 char_attr = saved_char_attr;
4134 }
4135 else
4136 char_attr = 0;
4137 }
4138 }
4139
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004140 // When still displaying '$' of change command, stop at cursor.
4141 // When only displaying the (relative) line number and that's done,
4142 // stop here.
4143 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004144 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145#ifdef FEAT_DIFF
4146 && filler_todo <= 0
4147#endif
4148 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004149 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004151 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004152 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004153 /* Pretend we have finished updating the window. Except when
4154 * 'cursorcolumn' is set. */
4155#ifdef FEAT_SYN_HL
4156 if (wp->w_p_cuc)
4157 row = wp->w_cline_row + wp->w_cline_height;
4158 else
4159#endif
4160 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 break;
4162 }
4163
4164 if (draw_state == WL_LINE && area_highlighting)
4165 {
4166 /* handle Visual or match highlighting in this line */
4167 if (vcol == fromcol
4168#ifdef FEAT_MBYTE
4169 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4170 && (*mb_ptr2cells)(ptr) > 1)
4171#endif
4172 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004173 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 && vcol < tocol))
4175 area_attr = attr; /* start highlighting */
4176 else if (area_attr != 0
4177 && (vcol == tocol
4178 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181#ifdef FEAT_SEARCH_EXTRA
4182 if (!n_extra)
4183 {
4184 /*
4185 * Check for start/end of search pattern match.
4186 * After end, check for start/end of next match.
4187 * When another match, have to check for start again.
4188 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004189 * Do this for 'search_hl' and the match list (ordered by
4190 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004192 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004193 cur = wp->w_match_head;
4194 shl_flag = FALSE;
4195 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004197 if (shl_flag == FALSE
4198 && ((cur != NULL
4199 && cur->priority > SEARCH_HL_PRIORITY)
4200 || cur == NULL))
4201 {
4202 shl = &search_hl;
4203 shl_flag = TRUE;
4204 }
4205 else
4206 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004207 if (cur != NULL)
4208 cur->pos.cur = 0;
4209 pos_inprogress = TRUE;
4210 while (shl->rm.regprog != NULL
4211 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004213 if (shl->startcol != MAXCOL
4214 && v >= (long)shl->startcol
4215 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004217#ifdef FEAT_MBYTE
4218 int tmp_col = v + MB_PTR2LEN(ptr);
4219
4220 if (shl->endcol < tmp_col)
4221 shl->endcol = tmp_col;
4222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004224#ifdef FEAT_CONCEAL
4225 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4226 == cur->hlg_id)
4227 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004228 has_match_conc =
4229 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004230 match_conc = cur->conceal_char;
4231 }
4232 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004233 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004236 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
4238 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004239 next_search_hl(wp, shl, lnum, (colnr_T)v,
4240 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004241 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004242 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /* Need to get the line again, a multi-line regexp
4245 * may have made it invalid. */
4246 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4247 ptr = line + v;
4248
4249 if (shl->lnum == lnum)
4250 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004251 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004253 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004255 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004257 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
4259 /* highlight empty match, try again after
4260 * it */
4261#ifdef FEAT_MBYTE
4262 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004263 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004264 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 else
4266#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004267 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269
4270 /* Loop to check if the match starts at the
4271 * current position */
4272 continue;
4273 }
4274 }
4275 break;
4276 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004277 if (shl != &search_hl && cur != NULL)
4278 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004280
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004281 /* Use attributes from match with highest priority among
4282 * 'search_hl' and the match list. */
4283 search_attr = search_hl.attr_cur;
4284 cur = wp->w_match_head;
4285 shl_flag = FALSE;
4286 while (cur != NULL || shl_flag == FALSE)
4287 {
4288 if (shl_flag == FALSE
4289 && ((cur != NULL
4290 && cur->priority > SEARCH_HL_PRIORITY)
4291 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004292 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004293 shl = &search_hl;
4294 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004295 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004296 else
4297 shl = &cur->hl;
4298 if (shl->attr_cur != 0)
4299 search_attr = shl->attr_cur;
4300 if (shl != &search_hl && cur != NULL)
4301 cur = cur->next;
4302 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004303 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004304 if (*ptr == NUL && (did_line_attr >= 1
4305 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004306 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308#endif
4309
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004311 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004313 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4314 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004316 if (diff_hlf == HLF_TXD && ptr - line > change_end
4317 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004319 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004320 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004321 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004324
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004325#ifdef FEAT_TEXT_PROP
4326 if (text_props != NULL)
4327 {
4328 int pi;
4329
4330 // Check if any active property ends.
4331 for (pi = 0; pi < text_props_active; ++pi)
4332 {
4333 int tpi = text_prop_idxs[pi];
4334
Bram Moolenaar8caa10a2018-12-30 22:07:40 +01004335 if (vcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004336 + text_props[tpi].tp_len)
4337 {
4338 if (pi + 1 < text_props_active)
4339 mch_memmove(text_prop_idxs + pi,
4340 text_prop_idxs + pi + 1,
4341 sizeof(int)
4342 * (text_props_active - (pi + 1)));
4343 --text_props_active;
4344 --pi;
4345 }
4346 }
4347
4348 // Add any text property that starts in this column.
4349 while (text_prop_next < text_prop_count
Bram Moolenaar8caa10a2018-12-30 22:07:40 +01004350 && vcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004351 text_prop_idxs[text_props_active++] = text_prop_next++;
4352
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004353 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004354 if (text_props_active > 0)
4355 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004356 // Sort the properties on priority and/or starting last.
4357 // Then combine the attributes, highest priority last.
4358 current_text_props = text_props;
4359 current_buf = wp->w_buffer;
4360 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4361 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004362
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004363 for (pi = 0; pi < text_props_active; ++pi)
4364 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004365 int tpi = text_prop_idxs[pi];
4366 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004367
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004368 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004369 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004370 int pt_attr = syn_id2attr(pt->pt_hl_id);
4371
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004372 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004373 if (text_prop_attr == 0)
4374 text_prop_attr = pt_attr;
4375 else
4376 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004377 }
4378 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004379 }
4380 }
4381#endif
4382
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004383 /* Decide which of the highlight attributes to use. */
4384 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004385#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004386 if (area_attr != 0)
4387 char_attr = hl_combine_attr(line_attr, area_attr);
4388 else if (search_attr != 0)
4389 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004390 /* Use line_attr when not in the Visual or 'incsearch' area
4391 * (area_attr may be 0 when "noinvcur" is set). */
4392 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004393 || vcol < fromcol || vcol_prev < fromcol_prev
4394 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004395 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004396#else
4397 if (area_attr != 0)
4398 char_attr = area_attr;
4399 else if (search_attr != 0)
4400 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004401#endif
4402 else
4403 {
4404 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004405#ifdef FEAT_TEXT_PROP
4406 if (text_prop_type != NULL)
4407 char_attr = text_prop_attr;
4408 else
4409#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004410#ifdef FEAT_SYN_HL
4411 if (has_syntax)
4412 char_attr = syntax_attr;
4413 else
4414#endif
4415 char_attr = 0;
4416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418
4419 /*
4420 * Get the next character to put on the screen.
4421 */
4422 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004423 * The "p_extra" points to the extra stuff that is inserted to
4424 * represent special characters (non-printable stuff) and other
4425 * things. When all characters are the same, c_extra is used.
4426 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4427 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4429 */
4430 if (n_extra > 0)
4431 {
4432 if (c_extra != NUL)
4433 {
4434 c = c_extra;
4435#ifdef FEAT_MBYTE
4436 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004437 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
4439 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004440 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004441 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 else
4444 mb_utf8 = FALSE;
4445#endif
4446 }
4447 else
4448 {
4449 c = *p_extra;
4450#ifdef FEAT_MBYTE
4451 if (has_mbyte)
4452 {
4453 mb_c = c;
4454 if (enc_utf8)
4455 {
4456 /* If the UTF-8 character is more than one byte:
4457 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004458 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 mb_utf8 = FALSE;
4460 if (mb_l > n_extra)
4461 mb_l = 1;
4462 else if (mb_l > 1)
4463 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004464 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004466 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 }
4469 else
4470 {
4471 /* if this is a DBCS character, put it in "mb_c" */
4472 mb_l = MB_BYTE2LEN(c);
4473 if (mb_l >= n_extra)
4474 mb_l = 1;
4475 else if (mb_l > 1)
4476 mb_c = (c << 8) + p_extra[1];
4477 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004478 if (mb_l == 0) /* at the NUL at end-of-line */
4479 mb_l = 1;
4480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 /* If a double-width char doesn't fit display a '>' in the
4482 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004483 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484# ifdef FEAT_RIGHTLEFT
4485 wp->w_p_rl ? (col <= 0) :
4486# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004487 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 && (*mb_char2cells)(mb_c) == 2)
4489 {
4490 c = '>';
4491 mb_c = c;
4492 mb_l = 1;
4493 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004494 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 /* put the pointer back to output the double-width
4496 * character at the start of the next line. */
4497 ++n_extra;
4498 --p_extra;
4499 }
4500 else
4501 {
4502 n_extra -= mb_l - 1;
4503 p_extra += mb_l - 1;
4504 }
4505 }
4506#endif
4507 ++p_extra;
4508 }
4509 --n_extra;
4510 }
4511 else
4512 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004513#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004514 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004515#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004516
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004517 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004518 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /*
4520 * Get a character from the line itself.
4521 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004522 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004523#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004524 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526#ifdef FEAT_MBYTE
4527 if (has_mbyte)
4528 {
4529 mb_c = c;
4530 if (enc_utf8)
4531 {
4532 /* If the UTF-8 character is more than one byte: Decode it
4533 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004534 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 mb_utf8 = FALSE;
4536 if (mb_l > 1)
4537 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004538 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 /* Overlong encoded ASCII or ASCII with composing char
4540 * is displayed normally, except a NUL. */
4541 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004542 {
4543 c = mb_c;
4544# ifdef FEAT_LINEBREAK
4545 c0 = mb_c;
4546# endif
4547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004549
4550 /* At start of the line we can have a composing char.
4551 * Draw it as a space with a composing char. */
4552 if (utf_iscomposing(mb_c))
4553 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004554 int i;
4555
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004556 for (i = Screen_mco - 1; i > 0; --i)
4557 u8cc[i] = u8cc[i - 1];
4558 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004559 mb_c = ' ';
4560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
4562
4563 if ((mb_l == 1 && c >= 0x80)
4564 || (mb_l >= 1 && mb_c == 0)
4565 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004566# ifdef UNICODE16
4567 || mb_c >= 0x10000
4568# endif
4569 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
4571 /*
4572 * Illegal UTF-8 byte: display as <xx>.
4573 * Non-BMP character : display as ? or fullwidth ?.
4574 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004575# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
4579 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004580# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (wp->w_p_rl) /* reverse */
4582 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004583# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004585# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 else if (utf_char2cells(mb_c) != 2)
4587 STRCPY(extra, "?");
4588 else
4589 /* 0xff1f in UTF-8: full-width '?' */
4590 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 p_extra = extra;
4594 c = *p_extra;
4595 mb_c = mb_ptr2char_adv(&p_extra);
4596 mb_utf8 = (c >= 0x80);
4597 n_extra = (int)STRLEN(p_extra);
4598 c_extra = NUL;
4599 if (area_attr == 0 && search_attr == 0)
4600 {
4601 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004602 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 saved_attr2 = char_attr; /* save current attr */
4604 }
4605 }
4606 else if (mb_l == 0) /* at the NUL at end-of-line */
4607 mb_l = 1;
4608#ifdef FEAT_ARABIC
4609 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4610 {
4611 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004612 int pc, pc1, nc;
4613 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 /* The idea of what is the previous and next
4616 * character depends on 'rightleft'. */
4617 if (wp->w_p_rl)
4618 {
4619 pc = prev_c;
4620 pc1 = prev_c1;
4621 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004622 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 else
4625 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004626 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004628 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
4630 prev_c = mb_c;
4631
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004632 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
4634 else
4635 prev_c = mb_c;
4636#endif
4637 }
4638 else /* enc_dbcs */
4639 {
4640 mb_l = MB_BYTE2LEN(c);
4641 if (mb_l == 0) /* at the NUL at end-of-line */
4642 mb_l = 1;
4643 else if (mb_l > 1)
4644 {
4645 /* We assume a second byte below 32 is illegal.
4646 * Hopefully this is OK for all double-byte encodings!
4647 */
4648 if (ptr[1] >= 32)
4649 mb_c = (c << 8) + ptr[1];
4650 else
4651 {
4652 if (ptr[1] == NUL)
4653 {
4654 /* head byte at end of line */
4655 mb_l = 1;
4656 transchar_nonprint(extra, c);
4657 }
4658 else
4659 {
4660 /* illegal tail byte */
4661 mb_l = 2;
4662 STRCPY(extra, "XX");
4663 }
4664 p_extra = extra;
4665 n_extra = (int)STRLEN(extra) - 1;
4666 c_extra = NUL;
4667 c = *p_extra++;
4668 if (area_attr == 0 && search_attr == 0)
4669 {
4670 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004671 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 saved_attr2 = char_attr; /* save current attr */
4673 }
4674 mb_c = c;
4675 }
4676 }
4677 }
4678 /* If a double-width char doesn't fit display a '>' in the
4679 * last column; the character is displayed at the start of the
4680 * next line. */
4681 if ((
4682# ifdef FEAT_RIGHTLEFT
4683 wp->w_p_rl ? (col <= 0) :
4684# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004685 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 && (*mb_char2cells)(mb_c) == 2)
4687 {
4688 c = '>';
4689 mb_c = c;
4690 mb_utf8 = FALSE;
4691 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004692 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 /* Put pointer back so that the character will be
4694 * displayed at the start of the next line. */
4695 --ptr;
4696 }
4697 else if (*ptr != NUL)
4698 ptr += mb_l - 1;
4699
4700 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004701 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004702 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004703 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004706 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 c = ' ';
4708 if (area_attr == 0 && search_attr == 0)
4709 {
4710 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004711 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 saved_attr2 = char_attr; /* save current attr */
4713 }
4714 mb_c = c;
4715 mb_utf8 = FALSE;
4716 mb_l = 1;
4717 }
4718
4719 }
4720#endif
4721 ++ptr;
4722
4723 if (extra_check)
4724 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004725#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004726 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004727#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004728
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004729#ifdef FEAT_TERMINAL
4730 if (get_term_attr)
4731 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004732 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004733
4734 if (!attr_pri)
4735 char_attr = syntax_attr;
4736 else
4737 char_attr = hl_combine_attr(syntax_attr, char_attr);
4738 }
4739#endif
4740
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004741#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004742 // Get syntax attribute, unless still at the start of the line
4743 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004744 v = (long)(ptr - line);
4745 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
4747 /* Get the syntax attribute for the character. If there
4748 * is an error, disable syntax highlighting. */
4749 save_did_emsg = did_emsg;
4750 did_emsg = FALSE;
4751
Bram Moolenaar217ad922005-03-20 22:37:15 +00004752 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004753# ifdef FEAT_SPELL
4754 has_spell ? &can_spell :
4755# endif
4756 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
4758 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004759 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004760 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004761 has_syntax = FALSE;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 else
4764 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004765#ifdef SYN_TIME_LIMIT
4766 if (wp->w_s->b_syn_slow)
4767 has_syntax = FALSE;
4768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 /* Need to get the line again, a multi-line regexp may
4771 * have made it invalid. */
4772 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4773 ptr = line + v;
4774
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004775# ifdef FEAT_TEXT_PROP
4776 // Text properties overrule syntax highlighting.
4777 if (text_prop_attr == 0)
4778#endif
4779 {
4780 if (!attr_pri)
4781 char_attr = syntax_attr;
4782 else
4783 char_attr = hl_combine_attr(syntax_attr, char_attr);
4784 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004785# ifdef FEAT_CONCEAL
4786 /* no concealing past the end of the line, it interferes
4787 * with line highlighting */
4788 if (c == NUL)
4789 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004790 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004791 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004794#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004795
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004796#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004797 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004798 * Only do this when there is no syntax highlighting, the
4799 * @Spell cluster is not used or the current syntax item
4800 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004801 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004802 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004803 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004804 if (c != 0 && (
4805# ifdef FEAT_SYN_HL
4806 !has_syntax ||
4807# endif
4808 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004809 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004810 char_u *prev_ptr, *p;
4811 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004812 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004813# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004814 if (has_mbyte)
4815 {
4816 prev_ptr = ptr - mb_l;
4817 v -= mb_l - 1;
4818 }
4819 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004820# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004821 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004822
4823 /* Use nextline[] if possible, it has the start of the
4824 * next line concatenated. */
4825 if ((prev_ptr - line) - nextlinecol >= 0)
4826 p = nextline + (prev_ptr - line) - nextlinecol;
4827 else
4828 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004829 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004830 len = spell_check(wp, p, &spell_hlf, &cap_col,
4831 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004832 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004833
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004834 /* In Insert mode only highlight a word that
4835 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004836 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004837 && (State & INSERT) != 0
4838 && wp->w_cursor.lnum == lnum
4839 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004840 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004841 && wp->w_cursor.col < (colnr_T)word_end)
4842 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004843 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004844 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004845 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004846
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004847 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004848 && (p - nextline) + len > nextline_idx)
4849 {
4850 /* Remember that the good word continues at the
4851 * start of the next line. */
4852 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004853 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004854 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004855
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004856 /* Turn index into actual attributes. */
4857 if (spell_hlf != HLF_COUNT)
4858 spell_attr = highlight_attr[spell_hlf];
4859
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004860 if (cap_col > 0)
4861 {
4862 if (p != prev_ptr
4863 && (p - nextline) + cap_col >= nextline_idx)
4864 {
4865 /* Remember that the word in the next line
4866 * must start with a capital. */
4867 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004868 cap_col = (int)((p - nextline) + cap_col
4869 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004870 }
4871 else
4872 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004873 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004874 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004875 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004876 }
4877 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004878 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004879 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004880 char_attr = hl_combine_attr(char_attr, spell_attr);
4881 else
4882 char_attr = hl_combine_attr(spell_attr, char_attr);
4883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#endif
4885#ifdef FEAT_LINEBREAK
4886 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004887 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004889 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004890 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004892# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004893 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004894# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004895 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004897 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004899 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004900
Bram Moolenaar597a4222014-06-25 14:39:50 +02004901 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004902 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004903 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004904 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004905# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004906 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004907 wp->w_buffer->b_p_vts_array) - 1;
4908# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004909 n_extra = (int)wp->w_buffer->b_p_ts
4910 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004911# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004912
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004913# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004914 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004915# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004917# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004918 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004919 {
4920#ifdef FEAT_CONCEAL
4921 if (c == TAB)
4922 /* See "Tab alignment" below. */
4923 FIX_FOR_BOGUSCOLS;
4924#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004925 if (!wp->w_p_list)
4926 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929#endif
4930
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004931 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4932 */
4933 if (wp->w_p_list
4934 && (((c == 160
4935#ifdef FEAT_MBYTE
4936 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4937#endif
4938 ) && lcs_nbsp)
4939 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4940 {
4941 c = (c == ' ') ? lcs_space : lcs_nbsp;
4942 if (area_attr == 0 && search_attr == 0)
4943 {
4944 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004945 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004946 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 Moolenaar9bc01eb2015-12-17 21:14:58 +01004951 {
4952 mb_utf8 = TRUE;
4953 u8cc[0] = 0;
4954 c = 0xc0;
4955 }
4956 else
4957 mb_utf8 = FALSE;
4958#endif
4959 }
4960
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4962 {
4963 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004964 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004967 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 saved_attr2 = char_attr; /* save current attr */
4969 }
4970#ifdef FEAT_MBYTE
4971 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004972 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
4974 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004975 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004976 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 else
4979 mb_utf8 = FALSE;
4980#endif
4981 }
4982 }
4983
4984 /*
4985 * Handling of non-printable characters.
4986 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004987 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 /*
4990 * when getting a character from the file, we may have to
4991 * turn it into something else on the way to putting it
4992 * into "ScreenLines".
4993 */
4994 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4995 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004996 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004997 long vcol_adjusted = vcol; /* removed showbreak length */
4998#ifdef FEAT_LINEBREAK
4999 /* only adjust the tab_len, when at the first column
5000 * after the showbreak value was drawn */
5001 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5002 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005005#ifdef FEAT_VARTABS
5006 tab_len = tabstop_padding(vcol_adjusted,
5007 wp->w_buffer->b_p_ts,
5008 wp->w_buffer->b_p_vts_array) - 1;
5009#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005010 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005011 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5012#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005013
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005014#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005015 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005016#endif
5017 /* tab amount depends on current column */
5018 n_extra = tab_len;
5019#ifdef FEAT_LINEBREAK
5020 else
5021 {
5022 char_u *p;
5023 int len = n_extra;
5024 int i;
5025 int saved_nextra = n_extra;
5026
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005027#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005028 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005029 /* there are characters to conceal */
5030 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005031 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5032 */
5033 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5034 && n_extra > tab_len)
5035 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005036#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005037
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005038 /* if n_extra > 0, it gives the number of chars, to
5039 * use for a tab, else we need to calculate the width
5040 * for a tab */
5041#ifdef FEAT_MBYTE
5042 len = (tab_len * mb_char2len(lcs_tab2));
5043 if (n_extra > 0)
5044 len += n_extra - tab_len;
5045#endif
5046 c = lcs_tab1;
5047 p = alloc((unsigned)(len + 1));
5048 vim_memset(p, ' ', len);
5049 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005050 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005051 p_extra_free = p;
5052 for (i = 0; i < tab_len; i++)
5053 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005054 if (*p == NUL)
5055 {
5056 tab_len = i;
5057 break;
5058 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005059#ifdef FEAT_MBYTE
5060 mb_char2bytes(lcs_tab2, p);
5061 p += mb_char2len(lcs_tab2);
5062 n_extra += mb_char2len(lcs_tab2)
5063 - (saved_nextra > 0 ? 1 : 0);
5064#else
5065 p[i] = lcs_tab2;
5066#endif
5067 }
5068 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005069#ifdef FEAT_CONCEAL
5070 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5071 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005072 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005073 n_extra -= vcol_off;
5074#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005075 }
5076#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005077#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005078 {
5079 int vc_saved = vcol_off;
5080
5081 /* Tab alignment should be identical regardless of
5082 * 'conceallevel' value. So tab compensates of all
5083 * previous concealed characters, and thus resets
5084 * vcol_off and boguscols accumulated so far in the
5085 * line. Note that the tab can be longer than
5086 * 'tabstop' when there are concealed characters. */
5087 FIX_FOR_BOGUSCOLS;
5088
5089 /* Make sure, the highlighting for the tab char will be
5090 * correctly set further below (effectively reverts the
5091 * FIX_FOR_BOGSUCOLS macro */
5092 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005093 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005094 tab_len += vc_saved;
5095 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097#ifdef FEAT_MBYTE
5098 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5099#endif
5100 if (wp->w_p_list)
5101 {
5102 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005103#ifdef FEAT_LINEBREAK
5104 if (wp->w_p_lbr)
5105 c_extra = NUL; /* using p_extra from above */
5106 else
5107#endif
5108 c_extra = lcs_tab2;
5109 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005110 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 saved_attr2 = char_attr; /* save current attr */
5112#ifdef FEAT_MBYTE
5113 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005114 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
5116 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005117 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005118 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120#endif
5121 }
5122 else
5123 {
5124 c_extra = ' ';
5125 c = ' ';
5126 }
5127 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005128 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005129 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005130 || ((fromcol >= 0 || fromcol_prev >= 0)
5131 && tocol > vcol
5132 && VIsual_mode != Ctrl_V
5133 && (
5134# ifdef FEAT_RIGHTLEFT
5135 wp->w_p_rl ? (col >= 0) :
5136# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005137 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005138 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005139 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005140 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005141 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005143 /* Display a '$' after the line or highlight an extra
5144 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5146 /* For a diff line the highlighting continues after the
5147 * "$". */
5148 if (
5149# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005150 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151# ifdef LINE_ATTR
5152 &&
5153# endif
5154# endif
5155# ifdef LINE_ATTR
5156 line_attr == 0
5157# endif
5158 )
5159#endif
5160 {
5161#ifdef FEAT_VIRTUALEDIT
5162 /* In virtualedit, visual selections may extend
5163 * beyond end of line. */
5164 if (area_highlighting && virtual_active()
5165 && tocol != MAXCOL && vcol < tocol)
5166 n_extra = 0;
5167 else
5168#endif
5169 {
5170 p_extra = at_end_str;
5171 n_extra = 1;
5172 c_extra = NUL;
5173 }
5174 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005175 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005176 c = lcs_eol;
5177 else
5178 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 lcs_eol_one = -1;
5180 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005181 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005183 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 n_attr = 1;
5185 }
5186#ifdef FEAT_MBYTE
5187 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005188 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
5190 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005191 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005192 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 else
5195 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5196#endif
5197 }
5198 else if (c != NUL)
5199 {
5200 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005201 if (n_extra == 0)
5202 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203#ifdef FEAT_RIGHTLEFT
5204 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5205 rl_mirror(p_extra); /* reverse "<12>" */
5206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005208#ifdef FEAT_LINEBREAK
5209 if (wp->w_p_lbr)
5210 {
5211 char_u *p;
5212
5213 c = *p_extra;
5214 p = alloc((unsigned)n_extra + 1);
5215 vim_memset(p, ' ', n_extra);
5216 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5217 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005218 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005219 p_extra_free = p_extra = p;
5220 }
5221 else
5222#endif
5223 {
5224 n_extra = byte2cells(c) - 1;
5225 c = *p_extra++;
5226 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005227 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
5229 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005230 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 saved_attr2 = char_attr; /* save current attr */
5232 }
5233#ifdef FEAT_MBYTE
5234 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5235#endif
5236 }
5237#ifdef FEAT_VIRTUALEDIT
5238 else if (VIsual_active
5239 && (VIsual_mode == Ctrl_V
5240 || VIsual_mode == 'v')
5241 && virtual_active()
5242 && tocol != MAXCOL
5243 && vcol < tocol
5244 && (
5245# ifdef FEAT_RIGHTLEFT
5246 wp->w_p_rl ? (col >= 0) :
5247# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005248 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
5250 c = ' ';
5251 --ptr; /* put it back at the NUL */
5252 }
5253#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005254#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 else if ((
5256# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005257 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005259# ifdef FEAT_TERMINAL
5260 term_attr != 0 ||
5261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 ) && (
5264# ifdef FEAT_RIGHTLEFT
5265 wp->w_p_rl ? (col >= 0) :
5266# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005267 (col
5268# ifdef FEAT_CONCEAL
5269 - boguscols
5270# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005271 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
5273 /* Highlight until the right side of the window */
5274 c = ' ';
5275 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005276
5277 /* Remember we do the char for line highlighting. */
5278 ++did_line_attr;
5279
5280 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005281 if (line_attr != 0 && char_attr == search_attr
5282 && (did_line_attr > 1
5283 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005284 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285# ifdef FEAT_DIFF
5286 if (diff_hlf == HLF_TXD)
5287 {
5288 diff_hlf = HLF_CHD;
5289 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005290 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005291 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005292 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5293 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005294 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005298# ifdef FEAT_TERMINAL
5299 if (term_attr != 0)
5300 {
5301 char_attr = term_attr;
5302 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5303 char_attr = hl_combine_attr(char_attr,
5304 HL_ATTR(HLF_CUL));
5305 }
5306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308#endif
5309 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005310
5311#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005312 if ( wp->w_p_cole > 0
5313 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005314 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005315 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005316 && !(lnum_in_visual_area
5317 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005318 {
5319 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005320 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005321 && (syn_get_sub_char() != NUL || match_conc
5322 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005323 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005324 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005325 /* First time at this concealed item: display one
5326 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005327 if (match_conc)
5328 c = match_conc;
5329 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005330 c = syn_get_sub_char();
5331 else if (lcs_conceal != NUL)
5332 c = lcs_conceal;
5333 else
5334 c = ' ';
5335
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005336 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005337
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005338 if (n_extra > 0)
5339 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005340 vcol += n_extra;
5341 if (wp->w_p_wrap && n_extra > 0)
5342 {
5343# ifdef FEAT_RIGHTLEFT
5344 if (wp->w_p_rl)
5345 {
5346 col -= n_extra;
5347 boguscols -= n_extra;
5348 }
5349 else
5350# endif
5351 {
5352 boguscols += n_extra;
5353 col += n_extra;
5354 }
5355 }
5356 n_extra = 0;
5357 n_attr = 0;
5358 }
5359 else if (n_skip == 0)
5360 {
5361 is_concealing = TRUE;
5362 n_skip = 1;
5363 }
5364# ifdef FEAT_MBYTE
5365 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005366 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005367 {
5368 mb_utf8 = TRUE;
5369 u8cc[0] = 0;
5370 c = 0xc0;
5371 }
5372 else
5373 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5374# endif
5375 }
5376 else
5377 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005378 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005379 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005380 }
5381#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005384#ifdef FEAT_CONCEAL
5385 /* In the cursor line and we may be concealing characters: correct
5386 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005387 if (!did_wcol && draw_state == WL_LINE
5388 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005389 && conceal_cursor_line(wp)
5390 && (int)wp->w_virtcol <= vcol + n_skip)
5391 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005392# ifdef FEAT_RIGHTLEFT
5393 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005394 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005395 else
5396# endif
5397 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005398 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005399 did_wcol = TRUE;
5400 }
5401#endif
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 /* Don't override visual selection highlighting. */
5404 if (n_attr > 0
5405 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005406 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 char_attr = extra_attr;
5408
Bram Moolenaar81695252004-12-29 20:58:21 +00005409#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 /* XIM don't send preedit_start and preedit_end, but they send
5411 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5412 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005413 if (p_imst == IM_ON_THE_SPOT
5414 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005415 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 && (State & INSERT)
5417 && !p_imdisable
5418 && im_is_preediting()
5419 && draw_state == WL_LINE)
5420 {
5421 colnr_T tcol;
5422
5423 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005424 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 else
5426 tcol = preedit_end_col;
5427 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5428 {
5429 if (feedback_old_attr < 0)
5430 {
5431 feedback_col = 0;
5432 feedback_old_attr = char_attr;
5433 }
5434 char_attr = im_get_feedback_attr(feedback_col);
5435 if (char_attr < 0)
5436 char_attr = feedback_old_attr;
5437 feedback_col++;
5438 }
5439 else if (feedback_old_attr >= 0)
5440 {
5441 char_attr = feedback_old_attr;
5442 feedback_old_attr = -1;
5443 feedback_col = 0;
5444 }
5445 }
5446#endif
5447 /*
5448 * Handle the case where we are in column 0 but not on the first
5449 * character of the line and the user wants us to show us a
5450 * special character (via 'listchars' option "precedes:<char>".
5451 */
5452 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005453 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5455#ifdef FEAT_DIFF
5456 && filler_todo <= 0
5457#endif
5458 && draw_state > WL_NR
5459 && c != NUL)
5460 {
5461 c = lcs_prec;
5462 lcs_prec_todo = NUL;
5463#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005464 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5465 {
5466 /* Double-width character being overwritten by the "precedes"
5467 * character, need to fill up half the character. */
5468 c_extra = MB_FILLER_CHAR;
5469 n_extra = 1;
5470 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005471 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005474 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
5476 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005477 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005478 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480 else
5481 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5482#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005483 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
5485 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005486 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 n_attr3 = 1;
5488 }
5489 }
5490
5491 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005492 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005494 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005495#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005496 || did_line_attr == 1
5497#endif
5498 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005500#ifdef FEAT_SEARCH_EXTRA
5501 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005502
5503 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005504 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005505 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005506#endif
5507
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005508 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 * highlight match at end of line. If it's beyond the last
5510 * char on the screen, just overwrite that one (tricky!) Not
5511 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005512#ifdef FEAT_SEARCH_EXTRA
5513 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005514 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005515 prevcol_hl_flag = TRUE;
5516 else
5517 {
5518 cur = wp->w_match_head;
5519 while (cur != NULL)
5520 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005521 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005522 {
5523 prevcol_hl_flag = TRUE;
5524 break;
5525 }
5526 cur = cur->next;
5527 }
5528 }
5529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005531 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005532 && (VIsual_mode != Ctrl_V
5533 || lnum == VIsual.lnum
5534 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005535 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536#ifdef FEAT_SEARCH_EXTRA
5537 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005538 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005539# ifdef FEAT_SYN_HL
5540 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5541 && !(wp == curwin && VIsual_active))
5542# endif
5543# ifdef FEAT_DIFF
5544 && diff_hlf == (hlf_T)0
5545# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005546# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005547 && did_line_attr <= 1
5548# endif
5549 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550#endif
5551 ))
5552 {
5553 int n = 0;
5554
5555#ifdef FEAT_RIGHTLEFT
5556 if (wp->w_p_rl)
5557 {
5558 if (col < 0)
5559 n = 1;
5560 }
5561 else
5562#endif
5563 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005564 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 n = -1;
5566 }
5567 if (n != 0)
5568 {
5569 /* At the window boundary, highlight the last character
5570 * instead (better than nothing). */
5571 off += n;
5572 col += n;
5573 }
5574 else
5575 {
5576 /* Add a blank character to highlight. */
5577 ScreenLines[off] = ' ';
5578#ifdef FEAT_MBYTE
5579 if (enc_utf8)
5580 ScreenLinesUC[off] = 0;
5581#endif
5582 }
5583#ifdef FEAT_SEARCH_EXTRA
5584 if (area_attr == 0)
5585 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005586 /* Use attributes from match with highest priority among
5587 * 'search_hl' and the match list. */
5588 char_attr = search_hl.attr;
5589 cur = wp->w_match_head;
5590 shl_flag = FALSE;
5591 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005592 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005593 if (shl_flag == FALSE
5594 && ((cur != NULL
5595 && cur->priority > SEARCH_HL_PRIORITY)
5596 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005597 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005598 shl = &search_hl;
5599 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005600 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005601 else
5602 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005603 if ((ptr - line) - 1 == (long)shl->startcol
5604 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005605 char_attr = shl->attr;
5606 if (shl != &search_hl && cur != NULL)
5607 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 }
5610#endif
5611 ScreenAttrs[off] = char_attr;
5612#ifdef FEAT_RIGHTLEFT
5613 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005616 --off;
5617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 else
5619#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005620 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005622 ++off;
5623 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005624 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005625#ifdef FEAT_SYN_HL
5626 eol_hl_off = 1;
5627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaar91170f82006-05-05 21:15:17 +00005631 /*
5632 * At end of the text line.
5633 */
5634 if (c == NUL)
5635 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005636#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005637 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005638 if (wp->w_p_wrap)
5639 v = wp->w_skipcol;
5640 else
5641 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005642
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005643 /* check if line ends before left margin */
5644 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005645 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005646#ifdef FEAT_CONCEAL
5647 /* Get rid of the boguscols now, we want to draw until the right
5648 * edge for 'cursorcolumn'. */
5649 col -= boguscols;
5650 boguscols = 0;
5651#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005652
5653 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005654 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005655
5656 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005657 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5658 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005659 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005660 && lnum != wp->w_cursor.lnum)
5661 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005662# ifdef FEAT_RIGHTLEFT
5663 && !wp->w_p_rl
5664# endif
5665 )
5666 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005667 int rightmost_vcol = 0;
5668 int i;
5669
5670 if (wp->w_p_cuc)
5671 rightmost_vcol = wp->w_virtcol;
5672 if (draw_color_col)
5673 /* determine rightmost colorcolumn to possibly draw */
5674 for (i = 0; color_cols[i] >= 0; ++i)
5675 if (rightmost_vcol < color_cols[i])
5676 rightmost_vcol = color_cols[i];
5677
Bram Moolenaar02631462017-09-22 15:20:32 +02005678 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005679 {
5680 ScreenLines[off] = ' ';
5681#ifdef FEAT_MBYTE
5682 if (enc_utf8)
5683 ScreenLinesUC[off] = 0;
5684#endif
5685 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005686 if (draw_color_col)
5687 draw_color_col = advance_color_col(VCOL_HLC,
5688 &color_cols);
5689
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005690 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005691 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005692 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005693 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005694 else
5695 ScreenAttrs[off++] = 0;
5696
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005697 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005698 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005699
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005700 ++vcol;
5701 }
5702 }
5703#endif
5704
Bram Moolenaar53f81742017-09-22 14:35:51 +02005705 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005706 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 row++;
5708
5709 /*
5710 * Update w_cline_height and w_cline_folded if the cursor line was
5711 * updated (saves a call to plines() later).
5712 */
5713 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5714 {
5715 curwin->w_cline_row = startrow;
5716 curwin->w_cline_height = row - startrow;
5717#ifdef FEAT_FOLDING
5718 curwin->w_cline_folded = FALSE;
5719#endif
5720 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5721 }
5722
5723 break;
5724 }
5725
5726 /* line continues beyond line end */
5727 if (lcs_ext
5728 && !wp->w_p_wrap
5729#ifdef FEAT_DIFF
5730 && filler_todo <= 0
5731#endif
5732 && (
5733#ifdef FEAT_RIGHTLEFT
5734 wp->w_p_rl ? col == 0 :
5735#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005736 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005738 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5740 {
5741 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005742 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743#ifdef FEAT_MBYTE
5744 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005745 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
5747 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005748 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005749 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
5751 else
5752 mb_utf8 = FALSE;
5753#endif
5754 }
5755
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005756#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005757 /* advance to the next 'colorcolumn' */
5758 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005759 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005760
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005761 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005762 * highlight the cursor position itself.
5763 * Also highlight the 'colorcolumn' if it is different than
5764 * 'cursorcolumn' */
5765 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005766 if (draw_state == WL_LINE && !lnum_in_visual_area
5767 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005768 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005769 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005770 && lnum != wp->w_cursor.lnum)
5771 {
5772 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005773 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005774 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005775 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005776 {
5777 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005778 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005779 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005780 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005781#endif
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 /*
5784 * Store character to be displayed.
5785 * Skip characters that are left of the screen for 'nowrap'.
5786 */
5787 vcol_prev = vcol;
5788 if (draw_state < WL_LINE || n_skip <= 0)
5789 {
5790 /*
5791 * Store the character.
5792 */
5793#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5794 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5795 {
5796 /* A double-wide character is: put first halve in left cell. */
5797 --off;
5798 --col;
5799 }
5800#endif
5801 ScreenLines[off] = c;
5802#ifdef FEAT_MBYTE
5803 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005804 {
5805 if ((mb_c & 0xff00) == 0x8e00)
5806 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 else if (enc_utf8)
5810 {
5811 if (mb_utf8)
5812 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005813 int i;
5814
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005816 if ((c & 0xff) == 0)
5817 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005818 for (i = 0; i < Screen_mco; ++i)
5819 {
5820 ScreenLinesC[i][off] = u8cc[i];
5821 if (u8cc[i] == 0)
5822 break;
5823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
5825 else
5826 ScreenLinesUC[off] = 0;
5827 }
5828 if (multi_attr)
5829 {
5830 ScreenAttrs[off] = multi_attr;
5831 multi_attr = 0;
5832 }
5833 else
5834#endif
5835 ScreenAttrs[off] = char_attr;
5836
5837#ifdef FEAT_MBYTE
5838 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5839 {
5840 /* Need to fill two screen columns. */
5841 ++off;
5842 ++col;
5843 if (enc_utf8)
5844 /* UTF-8: Put a 0 in the second screen char. */
5845 ScreenLines[off] = 0;
5846 else
5847 /* DBCS: Put second byte in the second screen char. */
5848 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005849 if (draw_state > WL_NR
5850#ifdef FEAT_DIFF
5851 && filler_todo <= 0
5852#endif
5853 )
5854 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 /* When "tocol" is halfway a character, set it to the end of
5856 * the character, otherwise highlighting won't stop. */
5857 if (tocol == vcol)
5858 ++tocol;
5859#ifdef FEAT_RIGHTLEFT
5860 if (wp->w_p_rl)
5861 {
5862 /* now it's time to backup one cell */
5863 --off;
5864 --col;
5865 }
5866#endif
5867 }
5868#endif
5869#ifdef FEAT_RIGHTLEFT
5870 if (wp->w_p_rl)
5871 {
5872 --off;
5873 --col;
5874 }
5875 else
5876#endif
5877 {
5878 ++off;
5879 ++col;
5880 }
5881 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005882#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005883 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005884 {
5885 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005886 ++vcol_off;
5887 if (n_extra > 0)
5888 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005889 if (wp->w_p_wrap)
5890 {
5891 /*
5892 * Special voodoo required if 'wrap' is on.
5893 *
5894 * Advance the column indicator to force the line
5895 * drawing to wrap early. This will make the line
5896 * take up the same screen space when parts are concealed,
5897 * so that cursor line computations aren't messed up.
5898 *
5899 * To avoid the fictitious advance of 'col' causing
5900 * trailing junk to be written out of the screen line
5901 * we are building, 'boguscols' keeps track of the number
5902 * of bad columns we have advanced.
5903 */
5904 if (n_extra > 0)
5905 {
5906 vcol += n_extra;
5907# ifdef FEAT_RIGHTLEFT
5908 if (wp->w_p_rl)
5909 {
5910 col -= n_extra;
5911 boguscols -= n_extra;
5912 }
5913 else
5914# endif
5915 {
5916 col += n_extra;
5917 boguscols += n_extra;
5918 }
5919 n_extra = 0;
5920 n_attr = 0;
5921 }
5922
5923
5924# ifdef FEAT_MBYTE
5925 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5926 {
5927 /* Need to fill two screen columns. */
5928# ifdef FEAT_RIGHTLEFT
5929 if (wp->w_p_rl)
5930 {
5931 --boguscols;
5932 --col;
5933 }
5934 else
5935# endif
5936 {
5937 ++boguscols;
5938 ++col;
5939 }
5940 }
5941# endif
5942
5943# ifdef FEAT_RIGHTLEFT
5944 if (wp->w_p_rl)
5945 {
5946 --boguscols;
5947 --col;
5948 }
5949 else
5950# endif
5951 {
5952 ++boguscols;
5953 ++col;
5954 }
5955 }
5956 else
5957 {
5958 if (n_extra > 0)
5959 {
5960 vcol += n_extra;
5961 n_extra = 0;
5962 n_attr = 0;
5963 }
5964 }
5965
5966 }
5967#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 else
5969 --n_skip;
5970
Bram Moolenaar64486672010-05-16 15:46:46 +02005971 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5972 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005973 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974#ifdef FEAT_DIFF
5975 && filler_todo <= 0
5976#endif
5977 )
5978 ++vcol;
5979
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005980#ifdef FEAT_SYN_HL
5981 if (vcol_save_attr >= 0)
5982 char_attr = vcol_save_attr;
5983#endif
5984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 /* restore attributes after "predeces" in 'listchars' */
5986 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5987 char_attr = saved_attr3;
5988
5989 /* restore attributes after last 'listchars' or 'number' char */
5990 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5991 char_attr = saved_attr2;
5992
5993 /*
5994 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005995 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 */
5997 if ((
5998#ifdef FEAT_RIGHTLEFT
5999 wp->w_p_rl ? (col < 0) :
6000#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006001 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 && (*ptr != NUL
6003#ifdef FEAT_DIFF
6004 || filler_todo > 0
6005#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006006 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6008 )
6009 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006010#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006011 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02006012 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006013 boguscols = 0;
6014#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006015 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02006016 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 ++row;
6019 ++screen_row;
6020
6021 /* When not wrapping and finished diff lines, or when displayed
6022 * '$' and highlighting until last column, break here. */
6023 if ((!wp->w_p_wrap
6024#ifdef FEAT_DIFF
6025 && filler_todo <= 0
6026#endif
6027 ) || lcs_eol_one == -1)
6028 break;
6029
6030 /* When the window is too narrow draw all "@" lines. */
6031 if (draw_state != WL_LINE
6032#ifdef FEAT_DIFF
6033 && filler_todo <= 0
6034#endif
6035 )
6036 {
6037 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 row = endrow;
6040 }
6041
6042 /* When line got too long for screen break here. */
6043 if (row == endrow)
6044 {
6045 ++row;
6046 break;
6047 }
6048
6049 if (screen_cur_row == screen_row - 1
6050#ifdef FEAT_DIFF
6051 && filler_todo <= 0
6052#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006053 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 {
6055 /* Remember that the line wraps, used for modeless copy. */
6056 LineWraps[screen_row - 1] = TRUE;
6057
6058 /*
6059 * Special trick to make copy/paste of wrapped lines work with
6060 * xterm/screen: write an extra character beyond the end of
6061 * the line. This will work with all terminal types
6062 * (regardless of the xn,am settings).
6063 * Only do this on a fast tty.
6064 * Only do this if the cursor is on the current line
6065 * (something has been written in it).
6066 * Don't do this for the GUI.
6067 * Don't do this for double-width characters.
6068 * Don't do this for a window not at the right screen border.
6069 */
6070 if (p_tf
6071#ifdef FEAT_GUI
6072 && !gui.in_use
6073#endif
6074#ifdef FEAT_MBYTE
6075 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006076 && ((*mb_off2cells)(LineOffset[screen_row],
6077 LineOffset[screen_row] + screen_Columns)
6078 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006080 + (int)Columns - 2,
6081 LineOffset[screen_row] + screen_Columns)
6082 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083#endif
6084 )
6085 {
6086 /* First make sure we are at the end of the screen line,
6087 * then output the same character again to let the
6088 * terminal know about the wrap. If the terminal doesn't
6089 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006090 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 screen_char(LineOffset[screen_row - 1]
6092 + (unsigned)Columns - 1,
6093 screen_row - 1, (int)(Columns - 1));
6094
6095#ifdef FEAT_MBYTE
6096 /* When there is a multi-byte character, just output a
6097 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006098 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6099 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 out_char(' ');
6101 else
6102#endif
6103 out_char(ScreenLines[LineOffset[screen_row - 1]
6104 + (Columns - 1)]);
6105 /* force a redraw of the first char on the next line */
6106 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6107 screen_start(); /* don't know where cursor is now */
6108 }
6109 }
6110
6111 col = 0;
6112 off = (unsigned)(current_ScreenLine - ScreenLines);
6113#ifdef FEAT_RIGHTLEFT
6114 if (wp->w_p_rl)
6115 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006116 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 off += col;
6118 }
6119#endif
6120
6121 /* reset the drawing state for the start of a wrapped line */
6122 draw_state = WL_START;
6123 saved_n_extra = n_extra;
6124 saved_p_extra = p_extra;
6125 saved_c_extra = c_extra;
6126 saved_char_attr = char_attr;
6127 n_extra = 0;
6128 lcs_prec_todo = lcs_prec;
6129#ifdef FEAT_LINEBREAK
6130# ifdef FEAT_DIFF
6131 if (filler_todo <= 0)
6132# endif
6133 need_showbreak = TRUE;
6134#endif
6135#ifdef FEAT_DIFF
6136 --filler_todo;
6137 /* When the filler lines are actually below the last line of the
6138 * file, don't draw the line itself, break here. */
6139 if (filler_todo == 0 && wp->w_botfill)
6140 break;
6141#endif
6142 }
6143
6144 } /* for every character in the line */
6145
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006146#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006147 /* After an empty line check first word for capital. */
6148 if (*skipwhite(line) == NUL)
6149 {
6150 capcol_lnum = lnum + 1;
6151 cap_col = 0;
6152 }
6153#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006154#ifdef FEAT_TEXT_PROP
6155 vim_free(text_props);
6156 vim_free(text_prop_idxs);
6157#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006158
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006159 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 return row;
6161}
6162
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006163#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006164/*
6165 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006166 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006167 */
6168 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006169comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006170{
6171 int i;
6172
6173 for (i = 0; i < Screen_mco; ++i)
6174 {
6175 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6176 return TRUE;
6177 if (ScreenLinesC[i][off_from] == 0)
6178 break;
6179 }
6180 return FALSE;
6181}
6182#endif
6183
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184/*
6185 * Check whether the given character needs redrawing:
6186 * - the (first byte of the) character is different
6187 * - the attributes are different
6188 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006189 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 */
6191 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006192char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194 if (cols > 0
6195 && ((ScreenLines[off_from] != ScreenLines[off_to]
6196 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6197
6198#ifdef FEAT_MBYTE
6199 || (enc_dbcs != 0
6200 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6201 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6202 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6203 : (cols > 1 && ScreenLines[off_from + 1]
6204 != ScreenLines[off_to + 1])))
6205 || (enc_utf8
6206 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6207 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006208 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006209 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6210 && ScreenLines[off_from + 1]
6211 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212#endif
6213 ))
6214 return TRUE;
6215 return FALSE;
6216}
6217
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006218#if defined(FEAT_TERMINAL) || defined(PROTO)
6219/*
6220 * Return the index in ScreenLines[] for the current screen line.
6221 */
6222 int
6223screen_get_current_line_off()
6224{
6225 return (int)(current_ScreenLine - ScreenLines);
6226}
6227#endif
6228
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229/*
6230 * Move one "cooked" screen line to the screen, but only the characters that
6231 * have actually changed. Handle insert/delete character.
6232 * "coloff" gives the first column on the screen for this line.
6233 * "endcol" gives the columns where valid characters are.
6234 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6235 * needs to be cleared, negative otherwise.
6236 * "rlflag" is TRUE in a rightleft window:
6237 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6238 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6239 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006240 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006241screen_line(
6242 int row,
6243 int coloff,
6244 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006245 int clear_width,
6246 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 unsigned off_from;
6249 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006250#ifdef FEAT_MBYTE
6251 unsigned max_off_from;
6252 unsigned max_off_to;
6253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 int force = FALSE; /* force update rest of the line */
6257 int redraw_this /* bool: does character need redraw? */
6258#ifdef FEAT_GUI
6259 = TRUE /* For GUI when while-loop empty */
6260#endif
6261 ;
6262 int redraw_next; /* redraw_this for next character */
6263#ifdef FEAT_MBYTE
6264 int clear_next = FALSE;
6265 int char_cells; /* 1: normal char */
6266 /* 2: occupies two display cells */
6267# define CHAR_CELLS char_cells
6268#else
6269# define CHAR_CELLS 1
6270#endif
6271
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006272 /* Check for illegal row and col, just in case. */
6273 if (row >= Rows)
6274 row = Rows - 1;
6275 if (endcol > Columns)
6276 endcol = Columns;
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278# ifdef FEAT_CLIPBOARD
6279 clip_may_clear_selection(row, row);
6280# endif
6281
6282 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6283 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006284#ifdef FEAT_MBYTE
6285 max_off_from = off_from + screen_Columns;
6286 max_off_to = LineOffset[row] + screen_Columns;
6287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288
6289#ifdef FEAT_RIGHTLEFT
6290 if (rlflag)
6291 {
6292 /* Clear rest first, because it's left of the text. */
6293 if (clear_width > 0)
6294 {
6295 while (col <= endcol && ScreenLines[off_to] == ' '
6296 && ScreenAttrs[off_to] == 0
6297# ifdef FEAT_MBYTE
6298 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6299# endif
6300 )
6301 {
6302 ++off_to;
6303 ++col;
6304 }
6305 if (col <= endcol)
6306 screen_fill(row, row + 1, col + coloff,
6307 endcol + coloff + 1, ' ', ' ', 0);
6308 }
6309 col = endcol + 1;
6310 off_to = LineOffset[row] + col + coloff;
6311 off_from += col;
6312 endcol = (clear_width > 0 ? clear_width : -clear_width);
6313 }
6314#endif /* FEAT_RIGHTLEFT */
6315
6316 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6317
6318 while (col < endcol)
6319 {
6320#ifdef FEAT_MBYTE
6321 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006322 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 else
6324 char_cells = 1;
6325#endif
6326
6327 redraw_this = redraw_next;
6328 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6329 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6330
6331#ifdef FEAT_GUI
6332 /* If the next character was bold, then redraw the current character to
6333 * remove any pixels that might have spilt over into us. This only
6334 * happens in the GUI.
6335 */
6336 if (redraw_next && gui.in_use)
6337 {
6338 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006339 if (hl > HL_ALL)
6340 hl = syn_attr2attr(hl);
6341 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 redraw_this = TRUE;
6343 }
6344#endif
6345
6346 if (redraw_this)
6347 {
6348 /*
6349 * Special handling when 'xs' termcap flag set (hpterm):
6350 * Attributes for characters are stored at the position where the
6351 * cursor is when writing the highlighting code. The
6352 * start-highlighting code must be written with the cursor on the
6353 * first highlighted character. The stop-highlighting code must
6354 * be written with the cursor just after the last highlighted
6355 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006356 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 * to clear the rest of the line, and force redrawing it
6358 * completely.
6359 */
6360 if ( p_wiv
6361 && !force
6362#ifdef FEAT_GUI
6363 && !gui.in_use
6364#endif
6365 && ScreenAttrs[off_to] != 0
6366 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6367 {
6368 /*
6369 * Need to remove highlighting attributes here.
6370 */
6371 windgoto(row, col + coloff);
6372 out_str(T_CE); /* clear rest of this screen line */
6373 screen_start(); /* don't know where cursor is now */
6374 force = TRUE; /* force redraw of rest of the line */
6375 redraw_next = TRUE; /* or else next char would miss out */
6376
6377 /*
6378 * If the previous character was highlighted, need to stop
6379 * highlighting at this character.
6380 */
6381 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6382 {
6383 screen_attr = ScreenAttrs[off_to - 1];
6384 term_windgoto(row, col + coloff);
6385 screen_stop_highlight();
6386 }
6387 else
6388 screen_attr = 0; /* highlighting has stopped */
6389 }
6390#ifdef FEAT_MBYTE
6391 if (enc_dbcs != 0)
6392 {
6393 /* Check if overwriting a double-byte with a single-byte or
6394 * the other way around requires another character to be
6395 * redrawn. For UTF-8 this isn't needed, because comparing
6396 * ScreenLinesUC[] is sufficient. */
6397 if (char_cells == 1
6398 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006399 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 {
6401 /* Writing a single-cell character over a double-cell
6402 * character: need to redraw the next cell. */
6403 ScreenLines[off_to + 1] = 0;
6404 redraw_next = TRUE;
6405 }
6406 else if (char_cells == 2
6407 && col + 2 < endcol
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 {
6411 /* Writing the second half of a double-cell character over
6412 * a double-cell character: need to redraw the second
6413 * cell. */
6414 ScreenLines[off_to + 2] = 0;
6415 redraw_next = TRUE;
6416 }
6417
6418 if (enc_dbcs == DBCS_JPNU)
6419 ScreenLines2[off_to] = ScreenLines2[off_from];
6420 }
6421 /* When writing a single-width character over a double-width
6422 * character and at the end of the redrawn text, need to clear out
6423 * the right halve of the old character.
6424 * Also required when writing the right halve of a double-width
6425 * char over the left halve of an existing one. */
6426 if (has_mbyte && col + char_cells == endcol
6427 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006428 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006430 && (*mb_off2cells)(off_to, max_off_to) == 1
6431 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 clear_next = TRUE;
6433#endif
6434
6435 ScreenLines[off_to] = ScreenLines[off_from];
6436#ifdef FEAT_MBYTE
6437 if (enc_utf8)
6438 {
6439 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6440 if (ScreenLinesUC[off_from] != 0)
6441 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006442 int i;
6443
6444 for (i = 0; i < Screen_mco; ++i)
6445 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 }
6447 }
6448 if (char_cells == 2)
6449 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6450#endif
6451
6452#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006453 /* The bold trick makes a single column of pixels appear in the
6454 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 * character should be redrawn too. This happens for our own GUI
6456 * and for some xterms. */
6457 if (
6458# ifdef FEAT_GUI
6459 gui.in_use
6460# endif
6461# if defined(FEAT_GUI) && defined(UNIX)
6462 ||
6463# endif
6464# ifdef UNIX
6465 term_is_xterm
6466# endif
6467 )
6468 {
6469 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006470 if (hl > HL_ALL)
6471 hl = syn_attr2attr(hl);
6472 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 redraw_next = TRUE;
6474 }
6475#endif
6476 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6477#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006478 /* For simplicity set the attributes of second half of a
6479 * double-wide character equal to the first half. */
6480 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006482
6483 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 else
6486#endif
6487 screen_char(off_to, row, col + coloff);
6488 }
6489 else if ( p_wiv
6490#ifdef FEAT_GUI
6491 && !gui.in_use
6492#endif
6493 && col + coloff > 0)
6494 {
6495 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6496 {
6497 /*
6498 * Don't output stop-highlight when moving the cursor, it will
6499 * stop the highlighting when it should continue.
6500 */
6501 screen_attr = 0;
6502 }
6503 else if (screen_attr != 0)
6504 screen_stop_highlight();
6505 }
6506
6507 off_to += CHAR_CELLS;
6508 off_from += CHAR_CELLS;
6509 col += CHAR_CELLS;
6510 }
6511
6512#ifdef FEAT_MBYTE
6513 if (clear_next)
6514 {
6515 /* Clear the second half of a double-wide character of which the left
6516 * half was overwritten with a single-wide character. */
6517 ScreenLines[off_to] = ' ';
6518 if (enc_utf8)
6519 ScreenLinesUC[off_to] = 0;
6520 screen_char(off_to, row, col + coloff);
6521 }
6522#endif
6523
6524 if (clear_width > 0
6525#ifdef FEAT_RIGHTLEFT
6526 && !rlflag
6527#endif
6528 )
6529 {
6530#ifdef FEAT_GUI
6531 int startCol = col;
6532#endif
6533
6534 /* blank out the rest of the line */
6535 while (col < clear_width && ScreenLines[off_to] == ' '
6536 && ScreenAttrs[off_to] == 0
6537#ifdef FEAT_MBYTE
6538 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6539#endif
6540 )
6541 {
6542 ++off_to;
6543 ++col;
6544 }
6545 if (col < clear_width)
6546 {
6547#ifdef FEAT_GUI
6548 /*
6549 * In the GUI, clearing the rest of the line may leave pixels
6550 * behind if the first character cleared was bold. Some bold
6551 * fonts spill over the left. In this case we redraw the previous
6552 * character too. If we didn't skip any blanks above, then we
6553 * only redraw if the character wasn't already redrawn anyway.
6554 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006555 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 {
6557 hl = ScreenAttrs[off_to];
6558 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006559 {
6560 int prev_cells = 1;
6561# ifdef FEAT_MBYTE
6562 if (enc_utf8)
6563 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6564 * that its width is 2. */
6565 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6566 else if (enc_dbcs != 0)
6567 {
6568 /* find previous character by counting from first
6569 * column and get its width. */
6570 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006571 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006572
6573 while (off < off_to)
6574 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006575 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006576 off += prev_cells;
6577 }
6578 }
6579
6580 if (enc_dbcs != 0 && prev_cells > 1)
6581 screen_char_2(off_to - prev_cells, row,
6582 col + coloff - prev_cells);
6583 else
6584# endif
6585 screen_char(off_to - prev_cells, row,
6586 col + coloff - prev_cells);
6587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 }
6589#endif
6590 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6591 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 off_to += clear_width - col;
6593 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 }
6595 }
6596
6597 if (clear_width > 0)
6598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 /* For a window that's left of another, draw the separator char. */
6600 if (col + coloff < Columns)
6601 {
6602 int c;
6603
6604 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006605 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006606#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006607 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6608 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 || ScreenAttrs[off_to] != hl)
6611 {
6612 ScreenLines[off_to] = c;
6613 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006614#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (enc_utf8)
6616 {
6617 if (c >= 0x80)
6618 {
6619 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006620 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 }
6622 else
6623 ScreenLinesUC[off_to] = 0;
6624 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 screen_char(off_to, row, col + coloff);
6627 }
6628 }
6629 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 LineWraps[row] = FALSE;
6631 }
6632}
6633
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006634#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006636 * Mirror text "str" for right-left displaying.
6637 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006639 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006640rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 char_u *p1, *p2;
6643 int t;
6644
6645 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6646 {
6647 t = *p1;
6648 *p1 = *p2;
6649 *p2 = t;
6650 }
6651}
6652#endif
6653
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654/*
6655 * mark all status lines for redraw; used after first :cd
6656 */
6657 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006658status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 win_T *wp;
6661
Bram Moolenaar29323592016-07-24 22:04:11 +02006662 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (wp->w_status_height)
6664 {
6665 wp->w_redr_status = TRUE;
6666 redraw_later(VALID);
6667 }
6668}
6669
6670/*
6671 * mark all status lines of the current buffer for redraw
6672 */
6673 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006674status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 win_T *wp;
6677
Bram Moolenaar29323592016-07-24 22:04:11 +02006678 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6680 {
6681 wp->w_redr_status = TRUE;
6682 redraw_later(VALID);
6683 }
6684}
6685
6686/*
6687 * Redraw all status lines that need to be redrawn.
6688 */
6689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006690redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
6692 win_T *wp;
6693
Bram Moolenaar29323592016-07-24 22:04:11 +02006694 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006696 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006697 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006698 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
Bram Moolenaar4033c552017-09-16 20:54:51 +02006701#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702/*
6703 * Redraw all status lines at the bottom of frame "frp".
6704 */
6705 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006706win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 if (frp->fr_layout == FR_LEAF)
6709 frp->fr_win->w_redr_status = TRUE;
6710 else if (frp->fr_layout == FR_ROW)
6711 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006712 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 win_redraw_last_status(frp);
6714 }
6715 else /* frp->fr_layout == FR_COL */
6716 {
6717 frp = frp->fr_child;
6718 while (frp->fr_next != NULL)
6719 frp = frp->fr_next;
6720 win_redraw_last_status(frp);
6721 }
6722}
6723#endif
6724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725/*
6726 * Draw the verticap separator right of window "wp" starting with line "row".
6727 */
6728 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006729draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 int hl;
6732 int c;
6733
6734 if (wp->w_vsep_width)
6735 {
6736 /* draw the vertical separator right of this window */
6737 c = fillchar_vsep(&hl);
6738 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6739 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6740 c, ' ', hl);
6741 }
6742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
6744#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006745static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746
6747/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006748 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 */
6750 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006751status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 int len = 0;
6754
6755#ifdef FEAT_MENU
6756 int emenu = (xp->xp_context == EXPAND_MENUS
6757 || xp->xp_context == EXPAND_MENUNAMES);
6758
6759 /* Check for menu separators - replace with '|'. */
6760 if (emenu && menu_is_separator(s))
6761 return 1;
6762#endif
6763
6764 while (*s != NUL)
6765 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006766 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006767 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006768 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 }
6770
6771 return len;
6772}
6773
6774/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006775 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006776 * These are backslashes used for escaping. Do show backslashes in help tags.
6777 */
6778 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006779skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006780{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006781 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006782#ifdef FEAT_MENU
6783 || ((xp->xp_context == EXPAND_MENUS
6784 || xp->xp_context == EXPAND_MENUNAMES)
6785 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6786#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006787 )
6788 {
6789#ifndef BACKSLASH_IN_FILENAME
6790 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6791 return 2;
6792#endif
6793 return 1;
6794 }
6795 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006796}
6797
6798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 * Show wildchar matches in the status line.
6800 * Show at least the "match" item.
6801 * We start at item 'first_match' in the list and show all matches that fit.
6802 *
6803 * If inversion is possible we use it. Else '=' characters are used.
6804 */
6805 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006806win_redr_status_matches(
6807 expand_T *xp,
6808 int num_matches,
6809 char_u **matches, /* list of matches */
6810 int match,
6811 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
6813#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6814 int row;
6815 char_u *buf;
6816 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006817 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 int fillchar;
6819 int attr;
6820 int i;
6821 int highlight = TRUE;
6822 char_u *selstart = NULL;
6823 int selstart_col = 0;
6824 char_u *selend = NULL;
6825 static int first_match = 0;
6826 int add_left = FALSE;
6827 char_u *s;
6828#ifdef FEAT_MENU
6829 int emenu;
6830#endif
6831#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6832 int l;
6833#endif
6834
6835 if (matches == NULL) /* interrupted completion? */
6836 return;
6837
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006838#ifdef FEAT_MBYTE
6839 if (has_mbyte)
6840 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6841 else
6842#endif
6843 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 if (buf == NULL)
6845 return;
6846
6847 if (match == -1) /* don't show match but original text */
6848 {
6849 match = 0;
6850 highlight = FALSE;
6851 }
6852 /* count 1 for the ending ">" */
6853 clen = status_match_len(xp, L_MATCH(match)) + 3;
6854 if (match == 0)
6855 first_match = 0;
6856 else if (match < first_match)
6857 {
6858 /* jumping left, as far as we can go */
6859 first_match = match;
6860 add_left = TRUE;
6861 }
6862 else
6863 {
6864 /* check if match fits on the screen */
6865 for (i = first_match; i < match; ++i)
6866 clen += status_match_len(xp, L_MATCH(i)) + 2;
6867 if (first_match > 0)
6868 clen += 2;
6869 /* jumping right, put match at the left */
6870 if ((long)clen > Columns)
6871 {
6872 first_match = match;
6873 /* if showing the last match, we can add some on the left */
6874 clen = 2;
6875 for (i = match; i < num_matches; ++i)
6876 {
6877 clen += status_match_len(xp, L_MATCH(i)) + 2;
6878 if ((long)clen >= Columns)
6879 break;
6880 }
6881 if (i == num_matches)
6882 add_left = TRUE;
6883 }
6884 }
6885 if (add_left)
6886 while (first_match > 0)
6887 {
6888 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6889 if ((long)clen >= Columns)
6890 break;
6891 --first_match;
6892 }
6893
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006894 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
6896 if (first_match == 0)
6897 {
6898 *buf = NUL;
6899 len = 0;
6900 }
6901 else
6902 {
6903 STRCPY(buf, "< ");
6904 len = 2;
6905 }
6906 clen = len;
6907
6908 i = first_match;
6909 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6910 {
6911 if (i == match)
6912 {
6913 selstart = buf + len;
6914 selstart_col = clen;
6915 }
6916
6917 s = L_MATCH(i);
6918 /* Check for menu separators - replace with '|' */
6919#ifdef FEAT_MENU
6920 emenu = (xp->xp_context == EXPAND_MENUS
6921 || xp->xp_context == EXPAND_MENUNAMES);
6922 if (emenu && menu_is_separator(s))
6923 {
6924 STRCPY(buf + len, transchar('|'));
6925 l = (int)STRLEN(buf + len);
6926 len += l;
6927 clen += l;
6928 }
6929 else
6930#endif
6931 for ( ; *s != NUL; ++s)
6932 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006933 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 clen += ptr2cells(s);
6935#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006936 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {
6938 STRNCPY(buf + len, s, l);
6939 s += l - 1;
6940 len += l;
6941 }
6942 else
6943#endif
6944 {
6945 STRCPY(buf + len, transchar_byte(*s));
6946 len += (int)STRLEN(buf + len);
6947 }
6948 }
6949 if (i == match)
6950 selend = buf + len;
6951
6952 *(buf + len++) = ' ';
6953 *(buf + len++) = ' ';
6954 clen += 2;
6955 if (++i == num_matches)
6956 break;
6957 }
6958
6959 if (i != num_matches)
6960 {
6961 *(buf + len++) = '>';
6962 ++clen;
6963 }
6964
6965 buf[len] = NUL;
6966
6967 row = cmdline_row - 1;
6968 if (row >= 0)
6969 {
6970 if (wild_menu_showing == 0)
6971 {
6972 if (msg_scrolled > 0)
6973 {
6974 /* Put the wildmenu just above the command line. If there is
6975 * no room, scroll the screen one line up. */
6976 if (cmdline_row == Rows - 1)
6977 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006978 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 ++msg_scrolled;
6980 }
6981 else
6982 {
6983 ++cmdline_row;
6984 ++row;
6985 }
6986 wild_menu_showing = WM_SCROLLED;
6987 }
6988 else
6989 {
6990 /* Create status line if needed by setting 'laststatus' to 2.
6991 * Set 'winminheight' to zero to avoid that the window is
6992 * resized. */
6993 if (lastwin->w_status_height == 0)
6994 {
6995 save_p_ls = p_ls;
6996 save_p_wmh = p_wmh;
6997 p_ls = 2;
6998 p_wmh = 0;
6999 last_status(FALSE);
7000 }
7001 wild_menu_showing = WM_SHOWN;
7002 }
7003 }
7004
7005 screen_puts(buf, row, 0, attr);
7006 if (selstart != NULL && highlight)
7007 {
7008 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007009 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011
7012 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7013 }
7014
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 vim_free(buf);
7017}
7018#endif
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020/*
7021 * Redraw the status line of window wp.
7022 *
7023 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007024 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7025 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007027 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007028win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 int row;
7031 char_u *p;
7032 int len;
7033 int fillchar;
7034 int attr;
7035 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007036 static int busy = FALSE;
7037
7038 /* It's possible to get here recursively when 'statusline' (indirectly)
7039 * invokes ":redrawstatus". Simply ignore the call then. */
7040 if (busy)
7041 return;
7042 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
7044 wp->w_redr_status = FALSE;
7045 if (wp->w_status_height == 0)
7046 {
7047 /* no status line, can only be last window */
7048 redraw_cmdline = TRUE;
7049 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007050 else if (!redrawing()
7051#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007052 // don't update status line when popup menu is visible and may be
7053 // drawn over it, unless it will be redrawn later
7054 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007055#endif
7056 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
7058 /* Don't redraw right now, do it later. */
7059 wp->w_redr_status = TRUE;
7060 }
7061#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007062 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
7064 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007065 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
7067#endif
7068 else
7069 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007070 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007072 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 p = NameBuff;
7074 len = (int)STRLEN(p);
7075
Bram Moolenaard85f2712017-07-28 21:51:57 +02007076 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077#ifdef FEAT_QUICKFIX
7078 || wp->w_p_pvw
7079#endif
7080 || bufIsChanged(wp->w_buffer)
7081 || wp->w_buffer->b_p_ro)
7082 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007083 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007085 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 len += (int)STRLEN(p + len);
7087 }
7088#ifdef FEAT_QUICKFIX
7089 if (wp->w_p_pvw)
7090 {
7091 STRCPY(p + len, _("[Preview]"));
7092 len += (int)STRLEN(p + len);
7093 }
7094#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007095 if (bufIsChanged(wp->w_buffer)
7096#ifdef FEAT_TERMINAL
7097 && !bt_terminal(wp->w_buffer)
7098#endif
7099 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
7101 STRCPY(p + len, "[+]");
7102 len += 3;
7103 }
7104 if (wp->w_buffer->b_p_ro)
7105 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007106 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007107 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 }
7109
Bram Moolenaar02631462017-09-22 15:20:32 +02007110 this_ru_col = ru_col - (Columns - wp->w_width);
7111 if (this_ru_col < (wp->w_width + 1) / 2)
7112 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 if (this_ru_col <= 1)
7114 {
7115 p = (char_u *)"<"; /* No room for file name! */
7116 len = 1;
7117 }
7118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119#ifdef FEAT_MBYTE
7120 if (has_mbyte)
7121 {
7122 int clen = 0, i;
7123
7124 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007125 clen = mb_string2cells(p, -1);
7126
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 /* Find first character that will fit.
7128 * Going from start to end is much faster for DBCS. */
7129 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007130 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 clen -= (*mb_ptr2cells)(p + i);
7132 len = clen;
7133 if (i > 0)
7134 {
7135 p = p + i - 1;
7136 *p = '<';
7137 ++len;
7138 }
7139
7140 }
7141 else
7142#endif
7143 if (len > this_ru_col - 1)
7144 {
7145 p += len - (this_ru_col - 1);
7146 *p = '<';
7147 len = this_ru_col - 1;
7148 }
7149
7150 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007151 screen_puts(p, row, wp->w_wincol, attr);
7152 screen_fill(row, row + 1, len + wp->w_wincol,
7153 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007155 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7157 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007158 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
7160#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007161 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162#endif
7163 }
7164
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 /*
7166 * May need to draw the character below the vertical separator.
7167 */
7168 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7169 {
7170 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007171 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 else
7173 fillchar = fillchar_vsep(&attr);
7174 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7175 attr);
7176 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007177 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178}
7179
Bram Moolenaar238a5642006-02-21 22:12:05 +00007180#ifdef FEAT_STL_OPT
7181/*
7182 * Redraw the status line according to 'statusline' and take care of any
7183 * errors encountered.
7184 */
7185 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007186redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007187{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007188 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007189 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007190
7191 /* When called recursively return. This can happen when the statusline
7192 * contains an expression that triggers a redraw. */
7193 if (entered)
7194 return;
7195 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007196
Bram Moolenaara742e082016-04-05 21:10:38 +02007197 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007198 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007199 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007200 {
7201 /* When there is an error disable the statusline, otherwise the
7202 * display is messed up with errors and a redraw triggers the problem
7203 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007204 set_string_option_direct((char_u *)"statusline", -1,
7205 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007206 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007207 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007208 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007209 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007210}
7211#endif
7212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213/*
7214 * Return TRUE if the status line of window "wp" is connected to the status
7215 * line of the window right of it. If not, then it's a vertical separator.
7216 * Only call if (wp->w_vsep_width != 0).
7217 */
7218 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007219stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221 frame_T *fr;
7222
7223 fr = wp->w_frame;
7224 while (fr->fr_parent != NULL)
7225 {
7226 if (fr->fr_parent->fr_layout == FR_COL)
7227 {
7228 if (fr->fr_next != NULL)
7229 break;
7230 }
7231 else
7232 {
7233 if (fr->fr_next != NULL)
7234 return TRUE;
7235 }
7236 fr = fr->fr_parent;
7237 }
7238 return FALSE;
7239}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242/*
7243 * Get the value to show for the language mappings, active 'keymap'.
7244 */
7245 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007246get_keymap_str(
7247 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007248 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007249 char_u *buf, /* buffer for the result */
7250 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 char_u *p;
7253
7254 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7255 return FALSE;
7256
7257 {
7258#ifdef FEAT_EVAL
7259 buf_T *old_curbuf = curbuf;
7260 win_T *old_curwin = curwin;
7261 char_u *s;
7262
7263 curbuf = wp->w_buffer;
7264 curwin = wp;
7265 STRCPY(buf, "b:keymap_name"); /* must be writable */
7266 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007267 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 --emsg_skip;
7269 curbuf = old_curbuf;
7270 curwin = old_curwin;
7271 if (p == NULL || *p == NUL)
7272#endif
7273 {
7274#ifdef FEAT_KEYMAP
7275 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7276 p = wp->w_buffer->b_p_keymap;
7277 else
7278#endif
7279 p = (char_u *)"lang";
7280 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007281 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 buf[0] = NUL;
7283#ifdef FEAT_EVAL
7284 vim_free(s);
7285#endif
7286 }
7287 return buf[0] != NUL;
7288}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289
7290#if defined(FEAT_STL_OPT) || defined(PROTO)
7291/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007292 * Redraw the status line or ruler of window "wp".
7293 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 */
7295 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007296win_redr_custom(
7297 win_T *wp,
7298 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007300 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 int attr;
7302 int curattr;
7303 int row;
7304 int col = 0;
7305 int maxwidth;
7306 int width;
7307 int n;
7308 int len;
7309 int fillchar;
7310 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007311 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007313 struct stl_hlrec hltab[STL_MAX_ITEM];
7314 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007315 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007316 win_T *ewp;
7317 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
Bram Moolenaar1d633412013-12-11 15:52:01 +01007319 /* There is a tiny chance that this gets called recursively: When
7320 * redrawing a status line triggers redrawing the ruler or tabline.
7321 * Avoid trouble by not allowing recursion. */
7322 if (entered)
7323 return;
7324 entered = TRUE;
7325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007327 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007329 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007330 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007331 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007332 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007333 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007334 maxwidth = Columns;
7335# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007336 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007339 else
7340 {
7341 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007342 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007343 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007344
7345 if (draw_ruler)
7346 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007347 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007348 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007349 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007350 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007351 if (*++stl == '-')
7352 stl++;
7353 if (atoi((char *)stl))
7354 while (VIM_ISDIGIT(*stl))
7355 stl++;
7356 if (*stl++ != '(')
7357 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007358 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007359 col = ru_col - (Columns - wp->w_width);
7360 if (col < (wp->w_width + 1) / 2)
7361 col = (wp->w_width + 1) / 2;
7362 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007363 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007364 {
7365 row = Rows - 1;
7366 --maxwidth; /* writing in last column may cause scrolling */
7367 fillchar = ' ';
7368 attr = 0;
7369 }
7370
7371# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007372 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007373# endif
7374 }
7375 else
7376 {
7377 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007378 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007379 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007380 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007381# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007382 use_sandbox = was_set_insecurely((char_u *)"statusline",
7383 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007384# endif
7385 }
7386
Bram Moolenaar53f81742017-09-22 14:35:51 +02007387 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007388 }
7389
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007391 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392
Bram Moolenaar61452852011-02-01 18:01:11 +01007393 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7394 * the cursor away and back. */
7395 ewp = wp == NULL ? curwin : wp;
7396 p_crb_save = ewp->w_p_crb;
7397 ewp->w_p_crb = FALSE;
7398
Bram Moolenaar362f3562009-11-03 16:20:34 +00007399 /* Make a copy, because the statusline may include a function call that
7400 * might change the option value and free the memory. */
7401 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007402 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007403 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007404 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007405 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007406 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007408 /* Make all characters printable. */
7409 p = transstr(buf);
7410 if (p != NULL)
7411 {
7412 vim_strncpy(buf, p, sizeof(buf) - 1);
7413 vim_free(p);
7414 }
7415
7416 /* fill up with "fillchar" */
7417 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007418 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 {
7420#ifdef FEAT_MBYTE
7421 len += (*mb_char2bytes)(fillchar, buf + len);
7422#else
7423 buf[len++] = fillchar;
7424#endif
7425 ++width;
7426 }
7427 buf[len] = NUL;
7428
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007429 /*
7430 * Draw each snippet with the specified highlighting.
7431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 curattr = attr;
7433 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007434 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007436 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 screen_puts_len(p, len, row, col, curattr);
7438 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007439 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007441 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007443 else if (hltab[n].userhl < 0)
7444 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007445#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007446 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7447 && wp->w_status_height != 0)
7448 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007449 else if (wp != NULL && bt_terminal(wp->w_buffer)
7450 && wp->w_status_height != 0)
7451 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007452#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007453 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007454 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007456 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 }
7458 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007459
7460 if (wp == NULL)
7461 {
7462 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7463 col = 0;
7464 len = 0;
7465 p = buf;
7466 fillchar = 0;
7467 for (n = 0; tabtab[n].start != NULL; n++)
7468 {
7469 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7470 while (col < len)
7471 TabPageIdxs[col++] = fillchar;
7472 p = tabtab[n].start;
7473 fillchar = tabtab[n].userhl;
7474 }
7475 while (col < Columns)
7476 TabPageIdxs[col++] = fillchar;
7477 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007478
7479theend:
7480 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481}
7482
7483#endif /* FEAT_STL_OPT */
7484
7485/*
7486 * Output a single character directly to the screen and update ScreenLines.
7487 */
7488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007489screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 char_u buf[MB_MAXBYTES + 1];
7492
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007493#ifdef FEAT_MBYTE
7494 if (has_mbyte)
7495 buf[(*mb_char2bytes)(c, buf)] = NUL;
7496 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007498 {
7499 buf[0] = c;
7500 buf[1] = NUL;
7501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 screen_puts(buf, row, col, attr);
7503}
7504
7505/*
7506 * Get a single character directly from ScreenLines into "bytes[]".
7507 * Also return its attribute in *attrp;
7508 */
7509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007510screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
7512 unsigned off;
7513
7514 /* safety check */
7515 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7516 {
7517 off = LineOffset[row] + col;
7518 *attrp = ScreenAttrs[off];
7519 bytes[0] = ScreenLines[off];
7520 bytes[1] = NUL;
7521
7522#ifdef FEAT_MBYTE
7523 if (enc_utf8 && ScreenLinesUC[off] != 0)
7524 bytes[utfc_char2bytes(off, bytes)] = NUL;
7525 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7526 {
7527 bytes[0] = ScreenLines[off];
7528 bytes[1] = ScreenLines2[off];
7529 bytes[2] = NUL;
7530 }
7531 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7532 {
7533 bytes[1] = ScreenLines[off + 1];
7534 bytes[2] = NUL;
7535 }
7536#endif
7537 }
7538}
7539
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007540#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007541/*
7542 * Return TRUE if composing characters for screen posn "off" differs from
7543 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007544 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007545 */
7546 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007547screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007548{
7549 int i;
7550
7551 for (i = 0; i < Screen_mco; ++i)
7552 {
7553 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7554 return TRUE;
7555 if (u8cc[i] == 0)
7556 break;
7557 }
7558 return FALSE;
7559}
7560#endif
7561
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562/*
7563 * Put string '*text' on the screen at position 'row' and 'col', with
7564 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7565 * Note: only outputs within one row, message is truncated at screen boundary!
7566 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7567 */
7568 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007569screen_puts(
7570 char_u *text,
7571 int row,
7572 int col,
7573 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574{
7575 screen_puts_len(text, -1, row, col, attr);
7576}
7577
7578/*
7579 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7580 * a NUL.
7581 */
7582 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007583screen_puts_len(
7584 char_u *text,
7585 int textlen,
7586 int row,
7587 int col,
7588 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
7590 unsigned off;
7591 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007592 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 int c;
7594#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007595 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 int mbyte_blen = 1;
7597 int mbyte_cells = 1;
7598 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007599 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 int clear_next_cell = FALSE;
7601# ifdef FEAT_ARABIC
7602 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007603 int pc, nc, nc1;
7604 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605# endif
7606#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007607#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7608 int force_redraw_this;
7609 int force_redraw_next = FALSE;
7610#endif
7611 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612
7613 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7614 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007615 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616
Bram Moolenaarc236c162008-07-13 17:41:49 +00007617#ifdef FEAT_MBYTE
7618 /* When drawing over the right halve of a double-wide char clear out the
7619 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007620 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007621# ifdef FEAT_GUI
7622 && !gui.in_use
7623# endif
7624 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007625 {
7626 ScreenLines[off - 1] = ' ';
7627 ScreenAttrs[off - 1] = 0;
7628 if (enc_utf8)
7629 {
7630 ScreenLinesUC[off - 1] = 0;
7631 ScreenLinesC[0][off - 1] = 0;
7632 }
7633 /* redraw the previous cell, make it empty */
7634 screen_char(off - 1, row, col - 1);
7635 /* force the cell at "col" to be redrawn */
7636 force_redraw_next = TRUE;
7637 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007638#endif
7639
Bram Moolenaar367329b2007-08-30 11:53:22 +00007640#ifdef FEAT_MBYTE
7641 max_off = LineOffset[row] + screen_Columns;
7642#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007643 while (col < screen_Columns
7644 && (len < 0 || (int)(ptr - text) < len)
7645 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {
7647 c = *ptr;
7648#ifdef FEAT_MBYTE
7649 /* check if this is the first byte of a multibyte */
7650 if (has_mbyte)
7651 {
7652 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007653 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007655 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7657 mbyte_cells = 1;
7658 else if (enc_dbcs != 0)
7659 mbyte_cells = mbyte_blen;
7660 else /* enc_utf8 */
7661 {
7662 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007663 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 (int)((text + len) - ptr));
7665 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007666 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007668# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 /* Non-BMP character: display as ? or fullwidth ?. */
7670 if (u8c >= 0x10000)
7671 {
7672 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7673 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007674 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677# ifdef FEAT_ARABIC
7678 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7679 {
7680 /* Do Arabic shaping. */
7681 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7682 {
7683 /* Past end of string to be displayed. */
7684 nc = NUL;
7685 nc1 = NUL;
7686 }
7687 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007688 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007689 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7690 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007691 nc1 = pcc[0];
7692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 pc = prev_c;
7694 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007695 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 }
7697 else
7698 prev_c = u8c;
7699# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007700 if (col + mbyte_cells > screen_Columns)
7701 {
7702 /* Only 1 cell left, but character requires 2 cells:
7703 * display a '>' in the last column to avoid wrapping. */
7704 c = '>';
7705 mbyte_cells = 1;
7706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
7708 }
7709#endif
7710
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007711#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7712 force_redraw_this = force_redraw_next;
7713 force_redraw_next = FALSE;
7714#endif
7715
7716 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717#ifdef FEAT_MBYTE
7718 || (mbyte_cells == 2
7719 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7720 || (enc_dbcs == DBCS_JPNU
7721 && c == 0x8e
7722 && ScreenLines2[off] != ptr[1])
7723 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007724 && (ScreenLinesUC[off] !=
7725 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7726 || (ScreenLinesUC[off] != 0
7727 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728#endif
7729 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007730 || exmode_active;
7731
7732 if (need_redraw
7733#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7734 || force_redraw_this
7735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 )
7737 {
7738#if defined(FEAT_GUI) || defined(UNIX)
7739 /* The bold trick makes a single row of pixels appear in the next
7740 * character. When a bold character is removed, the next
7741 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007742 * and for some xterms. */
7743 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744# ifdef FEAT_GUI
7745 gui.in_use
7746# endif
7747# if defined(FEAT_GUI) && defined(UNIX)
7748 ||
7749# endif
7750# ifdef UNIX
7751 term_is_xterm
7752# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007753 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007755 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007757 if (n > HL_ALL)
7758 n = syn_attr2attr(n);
7759 if (n & HL_BOLD)
7760 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 }
7762#endif
7763#ifdef FEAT_MBYTE
7764 /* When at the end of the text and overwriting a two-cell
7765 * character with a one-cell character, need to clear the next
7766 * cell. Also when overwriting the left halve of a two-cell char
7767 * with the right halve of a two-cell char. Do this only once
7768 * (mb_off2cells() may return 2 on the right halve). */
7769 if (clear_next_cell)
7770 clear_next_cell = FALSE;
7771 else if (has_mbyte
7772 && (len < 0 ? ptr[mbyte_blen] == NUL
7773 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007774 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007776 && (*mb_off2cells)(off, max_off) == 1
7777 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 clear_next_cell = TRUE;
7779
7780 /* Make sure we never leave a second byte of a double-byte behind,
7781 * it confuses mb_off2cells(). */
7782 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007783 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007785 && (*mb_off2cells)(off, max_off) == 1
7786 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 ScreenLines[off + mbyte_blen] = 0;
7788#endif
7789 ScreenLines[off] = c;
7790 ScreenAttrs[off] = attr;
7791#ifdef FEAT_MBYTE
7792 if (enc_utf8)
7793 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007794 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 ScreenLinesUC[off] = 0;
7796 else
7797 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007798 int i;
7799
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007801 for (i = 0; i < Screen_mco; ++i)
7802 {
7803 ScreenLinesC[i][off] = u8cc[i];
7804 if (u8cc[i] == 0)
7805 break;
7806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 }
7808 if (mbyte_cells == 2)
7809 {
7810 ScreenLines[off + 1] = 0;
7811 ScreenAttrs[off + 1] = attr;
7812 }
7813 screen_char(off, row, col);
7814 }
7815 else if (mbyte_cells == 2)
7816 {
7817 ScreenLines[off + 1] = ptr[1];
7818 ScreenAttrs[off + 1] = attr;
7819 screen_char_2(off, row, col);
7820 }
7821 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7822 {
7823 ScreenLines2[off] = ptr[1];
7824 screen_char(off, row, col);
7825 }
7826 else
7827#endif
7828 screen_char(off, row, col);
7829 }
7830#ifdef FEAT_MBYTE
7831 if (has_mbyte)
7832 {
7833 off += mbyte_cells;
7834 col += mbyte_cells;
7835 ptr += mbyte_blen;
7836 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007837 {
7838 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007840 len = -1;
7841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 }
7843 else
7844#endif
7845 {
7846 ++off;
7847 ++col;
7848 ++ptr;
7849 }
7850 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007851
7852#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7853 /* If we detected the next character needs to be redrawn, but the text
7854 * doesn't extend up to there, update the character here. */
7855 if (force_redraw_next && col < screen_Columns)
7856 {
7857# ifdef FEAT_MBYTE
7858 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7859 screen_char_2(off, row, col);
7860 else
7861# endif
7862 screen_char(off, row, col);
7863 }
7864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865}
7866
7867#ifdef FEAT_SEARCH_EXTRA
7868/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007869 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 */
7871 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007872start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 if (p_hls && !no_hlsearch)
7875 {
7876 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007877 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007878# ifdef FEAT_RELTIME
7879 /* Set the time limit to 'redrawtime'. */
7880 profile_setlimit(p_rdt, &search_hl.tm);
7881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883}
7884
7885/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007886 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 */
7888 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007889end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
7891 if (search_hl.rm.regprog != NULL)
7892 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007893 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 search_hl.rm.regprog = NULL;
7895 }
7896}
7897
7898/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007899 * Init for calling prepare_search_hl().
7900 */
7901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007902init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007903{
7904 matchitem_T *cur;
7905
7906 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7907 * match */
7908 cur = wp->w_match_head;
7909 while (cur != NULL)
7910 {
7911 cur->hl.rm = cur->match;
7912 if (cur->hlg_id == 0)
7913 cur->hl.attr = 0;
7914 else
7915 cur->hl.attr = syn_id2attr(cur->hlg_id);
7916 cur->hl.buf = wp->w_buffer;
7917 cur->hl.lnum = 0;
7918 cur->hl.first_lnum = 0;
7919# ifdef FEAT_RELTIME
7920 /* Set the time limit to 'redrawtime'. */
7921 profile_setlimit(p_rdt, &(cur->hl.tm));
7922# endif
7923 cur = cur->next;
7924 }
7925 search_hl.buf = wp->w_buffer;
7926 search_hl.lnum = 0;
7927 search_hl.first_lnum = 0;
7928 /* time limit is set at the toplevel, for all windows */
7929}
7930
7931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 * Advance to the match in window "wp" line "lnum" or past it.
7933 */
7934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007935prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007937 matchitem_T *cur; /* points to the match list */
7938 match_T *shl; /* points to search_hl or a match */
7939 int shl_flag; /* flag to indicate whether search_hl
7940 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007941 int pos_inprogress; /* marks that position match search is
7942 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 int n;
7944
7945 /*
7946 * When using a multi-line pattern, start searching at the top
7947 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007948 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007950 cur = wp->w_match_head;
7951 shl_flag = FALSE;
7952 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007954 if (shl_flag == FALSE)
7955 {
7956 shl = &search_hl;
7957 shl_flag = TRUE;
7958 }
7959 else
7960 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 if (shl->rm.regprog != NULL
7962 && shl->lnum == 0
7963 && re_multiline(shl->rm.regprog))
7964 {
7965 if (shl->first_lnum == 0)
7966 {
7967# ifdef FEAT_FOLDING
7968 for (shl->first_lnum = lnum;
7969 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7970 if (hasFoldingWin(wp, shl->first_lnum - 1,
7971 NULL, NULL, TRUE, NULL))
7972 break;
7973# else
7974 shl->first_lnum = wp->w_topline;
7975# endif
7976 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007977 if (cur != NULL)
7978 cur->pos.cur = 0;
7979 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7982 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007984 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7985 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007986 pos_inprogress = cur == NULL || cur->pos.cur == 0
7987 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 if (shl->lnum != 0)
7989 {
7990 shl->first_lnum = shl->lnum
7991 + shl->rm.endpos[0].lnum
7992 - shl->rm.startpos[0].lnum;
7993 n = shl->rm.endpos[0].col;
7994 }
7995 else
7996 {
7997 ++shl->first_lnum;
7998 n = 0;
7999 }
8000 }
8001 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008002 if (shl != &search_hl && cur != NULL)
8003 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 }
8005}
8006
8007/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008008 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 * Uses shl->buf.
8010 * Sets shl->lnum and shl->rm contents.
8011 * Note: Assumes a previous match is always before "lnum", unless
8012 * shl->lnum is zero.
8013 * Careful: Any pointers for buffer lines will become invalid.
8014 */
8015 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008016next_search_hl(
8017 win_T *win,
8018 match_T *shl, /* points to search_hl or a match */
8019 linenr_T lnum,
8020 colnr_T mincol, /* minimal column for a match */
8021 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022{
8023 linenr_T l;
8024 colnr_T matchcol;
8025 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008026 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008028 // for :{range}s/pat only highlight inside the range
8029 if (lnum < search_first_line || lnum > search_last_line)
8030 {
8031 shl->lnum = 0;
8032 return;
8033 }
8034
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 if (shl->lnum != 0)
8036 {
8037 /* Check for three situations:
8038 * 1. If the "lnum" is below a previous match, start a new search.
8039 * 2. If the previous match includes "mincol", use it.
8040 * 3. Continue after the previous match.
8041 */
8042 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8043 if (lnum > l)
8044 shl->lnum = 0;
8045 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8046 return;
8047 }
8048
8049 /*
8050 * Repeat searching for a match until one is found that includes "mincol"
8051 * or none is found in this line.
8052 */
8053 called_emsg = FALSE;
8054 for (;;)
8055 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008056#ifdef FEAT_RELTIME
8057 /* Stop searching after passing the time limit. */
8058 if (profile_passed_limit(&(shl->tm)))
8059 {
8060 shl->lnum = 0; /* no match found in time */
8061 break;
8062 }
8063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 /* Three situations:
8065 * 1. No useful previous match: search from start of line.
8066 * 2. Not Vi compatible or empty match: continue at next character.
8067 * Break the loop if this is beyond the end of the line.
8068 * 3. Vi compatible searching: continue at end of previous match.
8069 */
8070 if (shl->lnum == 0)
8071 matchcol = 0;
8072 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8073 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008074 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008076 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008077
8078 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008079 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008080 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008082 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 shl->lnum = 0;
8084 break;
8085 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008086#ifdef FEAT_MBYTE
8087 if (has_mbyte)
8088 matchcol += mb_ptr2len(ml);
8089 else
8090#endif
8091 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 }
8093 else
8094 matchcol = shl->rm.endpos[0].col;
8095
8096 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008097 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008099 /* Remember whether shl->rm is using a copy of the regprog in
8100 * cur->match. */
8101 int regprog_is_copy = (shl != &search_hl && cur != NULL
8102 && shl == &cur->hl
8103 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008104 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008105
Bram Moolenaarb3414592014-06-17 17:48:32 +02008106 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8107 matchcol,
8108#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008109 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008110#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008111 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008112#endif
8113 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008114 /* Copy the regprog, in case it got freed and recompiled. */
8115 if (regprog_is_copy)
8116 cur->match.regprog = cur->hl.rm.regprog;
8117
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008118 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008119 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008120 /* Error while handling regexp: stop using this regexp. */
8121 if (shl == &search_hl)
8122 {
8123 /* don't free regprog in the match list, it's a copy */
8124 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008125 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008126 }
8127 shl->rm.regprog = NULL;
8128 shl->lnum = 0;
8129 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8130 message */
8131 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008132 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008133 }
8134 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008135 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008136 else
8137 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 if (nmatched == 0)
8139 {
8140 shl->lnum = 0; /* no match found */
8141 break;
8142 }
8143 if (shl->rm.startpos[0].lnum > 0
8144 || shl->rm.startpos[0].col >= mincol
8145 || nmatched > 1
8146 || shl->rm.endpos[0].col > mincol)
8147 {
8148 shl->lnum += shl->rm.startpos[0].lnum;
8149 break; /* useful match found */
8150 }
8151 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008152
8153 // Restore called_emsg for assert_fails().
8154 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156
Bram Moolenaar85077472016-10-16 14:35:48 +02008157/*
8158 * If there is a match fill "shl" and return one.
8159 * Return zero otherwise.
8160 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008161 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008162next_search_hl_pos(
8163 match_T *shl, /* points to a match */
8164 linenr_T lnum,
8165 posmatch_T *posmatch, /* match positions */
8166 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008167{
8168 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008169 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008170
Bram Moolenaarb3414592014-06-17 17:48:32 +02008171 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8172 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008173 llpos_T *pos = &posmatch->pos[i];
8174
8175 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008176 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008177 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008178 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008179 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008180 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008181 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008182 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008183 /* if this match comes before the one at "found" then swap
8184 * them */
8185 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008186 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008187 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008188
Bram Moolenaar85077472016-10-16 14:35:48 +02008189 *pos = posmatch->pos[found];
8190 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008191 }
8192 }
8193 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008194 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195 }
8196 }
8197 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008198 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008199 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008200 colnr_T start = posmatch->pos[found].col == 0
8201 ? 0 : posmatch->pos[found].col - 1;
8202 colnr_T end = posmatch->pos[found].col == 0
8203 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008204
Bram Moolenaar85077472016-10-16 14:35:48 +02008205 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008206 shl->rm.startpos[0].lnum = 0;
8207 shl->rm.startpos[0].col = start;
8208 shl->rm.endpos[0].lnum = 0;
8209 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008210 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008211 posmatch->cur = found + 1;
8212 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008213 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008214 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008215}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008216#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008217
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008219screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220{
8221 attrentry_T *aep = NULL;
8222
8223 screen_attr = attr;
8224 if (full_screen
8225#ifdef WIN3264
8226 && termcap_active
8227#endif
8228 )
8229 {
8230#ifdef FEAT_GUI
8231 if (gui.in_use)
8232 {
8233 char buf[20];
8234
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008235 /* The GUI handles this internally. */
8236 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 OUT_STR(buf);
8238 }
8239 else
8240#endif
8241 {
8242 if (attr > HL_ALL) /* special HL attr. */
8243 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008244 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 aep = syn_cterm_attr2entry(attr);
8246 else
8247 aep = syn_term_attr2entry(attr);
8248 if (aep == NULL) /* did ":syntax clear" */
8249 attr = 0;
8250 else
8251 attr = aep->ae_attr;
8252 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008253 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008255 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008256#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008257 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8258 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8259 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008260#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008261 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008262 /* If the Normal FG color has BOLD attribute and the new HL
8263 * has a FG color defined, clear BOLD. */
8264 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008265 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008267 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008268 out_str(T_UCS);
8269 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008270 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8271 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008273 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008275 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008277 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008278 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279
8280 /*
8281 * Output the color or start string after bold etc., in case the
8282 * bold etc. override the color setting.
8283 */
8284 if (aep != NULL)
8285 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008286#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008287 /* When 'termguicolors' is set but fg or bg is unset,
8288 * fall back to the cterm colors. This helps for SpellBad,
8289 * where the GUI uses a red undercurl. */
8290 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008292 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008293 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008294 }
8295 else
8296#endif
8297 if (t_colors > 1)
8298 {
8299 if (aep->ae_u.cterm.fg_color)
8300 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8301 }
8302#ifdef FEAT_TERMGUICOLORS
8303 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8304 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008305 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008306 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008309#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008310 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008312 if (aep->ae_u.cterm.bg_color)
8313 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8314 }
8315
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008316 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008317 {
8318 if (aep->ae_u.term.start != NULL)
8319 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
8321 }
8322 }
8323 }
8324}
8325
8326 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008327screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328{
8329 int do_ME = FALSE; /* output T_ME code */
8330
8331 if (screen_attr != 0
8332#ifdef WIN3264
8333 && termcap_active
8334#endif
8335 )
8336 {
8337#ifdef FEAT_GUI
8338 if (gui.in_use)
8339 {
8340 char buf[20];
8341
8342 /* use internal GUI code */
8343 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8344 OUT_STR(buf);
8345 }
8346 else
8347#endif
8348 {
8349 if (screen_attr > HL_ALL) /* special HL attr. */
8350 {
8351 attrentry_T *aep;
8352
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008353 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {
8355 /*
8356 * Assume that t_me restores the original colors!
8357 */
8358 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008359 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008360#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008361 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8362 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8363 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008364#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008365 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008366#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008367 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8368 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8369 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008370#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008371 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 do_ME = TRUE;
8373 }
8374 else
8375 {
8376 aep = syn_term_attr2entry(screen_attr);
8377 if (aep != NULL && aep->ae_u.term.stop != NULL)
8378 {
8379 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8380 do_ME = TRUE;
8381 else
8382 out_str(aep->ae_u.term.stop);
8383 }
8384 }
8385 if (aep == NULL) /* did ":syntax clear" */
8386 screen_attr = 0;
8387 else
8388 screen_attr = aep->ae_attr;
8389 }
8390
8391 /*
8392 * Often all ending-codes are equal to T_ME. Avoid outputting the
8393 * same sequence several times.
8394 */
8395 if (screen_attr & HL_STANDOUT)
8396 {
8397 if (STRCMP(T_SE, T_ME) == 0)
8398 do_ME = TRUE;
8399 else
8400 out_str(T_SE);
8401 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008402 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008403 {
8404 if (STRCMP(T_UCE, T_ME) == 0)
8405 do_ME = TRUE;
8406 else
8407 out_str(T_UCE);
8408 }
8409 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008410 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 {
8412 if (STRCMP(T_UE, T_ME) == 0)
8413 do_ME = TRUE;
8414 else
8415 out_str(T_UE);
8416 }
8417 if (screen_attr & HL_ITALIC)
8418 {
8419 if (STRCMP(T_CZR, T_ME) == 0)
8420 do_ME = TRUE;
8421 else
8422 out_str(T_CZR);
8423 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008424 if (screen_attr & HL_STRIKETHROUGH)
8425 {
8426 if (STRCMP(T_STE, T_ME) == 0)
8427 do_ME = TRUE;
8428 else
8429 out_str(T_STE);
8430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8432 out_str(T_ME);
8433
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008434#ifdef FEAT_TERMGUICOLORS
8435 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008437 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008438 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008439 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008440 term_bg_rgb_color(cterm_normal_bg_gui_color);
8441 }
8442 else
8443#endif
8444 {
8445 if (t_colors > 1)
8446 {
8447 /* set Normal cterm colors */
8448 if (cterm_normal_fg_color != 0)
8449 term_fg_color(cterm_normal_fg_color - 1);
8450 if (cterm_normal_bg_color != 0)
8451 term_bg_color(cterm_normal_bg_color - 1);
8452 if (cterm_normal_fg_bold)
8453 out_str(T_MD);
8454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 }
8456 }
8457 }
8458 screen_attr = 0;
8459}
8460
8461/*
8462 * Reset the colors for a cterm. Used when leaving Vim.
8463 * The machine specific code may override this again.
8464 */
8465 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008466reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008468 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
8470 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008471#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008472 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8473 || cterm_normal_bg_gui_color != INVALCOLOR)
8474 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008475#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {
8479 out_str(T_OP);
8480 screen_attr = -1;
8481 }
8482 if (cterm_normal_fg_bold)
8483 {
8484 out_str(T_ME);
8485 screen_attr = -1;
8486 }
8487 }
8488}
8489
8490/*
8491 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8492 * using the attributes from ScreenAttrs["off"].
8493 */
8494 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008495screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
8497 int attr;
8498
8499 /* Check for illegal values, just in case (could happen just after
8500 * resizing). */
8501 if (row >= screen_Rows || col >= screen_Columns)
8502 return;
8503
Bram Moolenaar494838a2015-02-10 19:20:37 +01008504 /* Outputting a character in the last cell on the screen may scroll the
8505 * screen up. Only do it when the "xn" termcap property is set, otherwise
8506 * mark the character invalid (update it when scrolled up). */
8507 if (*T_XN == NUL
8508 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509#ifdef FEAT_RIGHTLEFT
8510 /* account for first command-line character in rightleft mode */
8511 && !cmdmsg_rl
8512#endif
8513 )
8514 {
8515 ScreenAttrs[off] = (sattr_T)-1;
8516 return;
8517 }
8518
8519 /*
8520 * Stop highlighting first, so it's easier to move the cursor.
8521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 if (screen_char_attr != 0)
8523 attr = screen_char_attr;
8524 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 attr = ScreenAttrs[off];
8526 if (screen_attr != attr)
8527 screen_stop_highlight();
8528
8529 windgoto(row, col);
8530
8531 if (screen_attr != attr)
8532 screen_start_highlight(attr);
8533
8534#ifdef FEAT_MBYTE
8535 if (enc_utf8 && ScreenLinesUC[off] != 0)
8536 {
8537 char_u buf[MB_MAXBYTES + 1];
8538
Bram Moolenaarcb070082016-04-02 22:14:51 +02008539 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008540 {
8541 if (*p_ambw == 'd'
8542# ifdef FEAT_GUI
8543 && !gui.in_use
8544# endif
8545 )
8546 {
8547 /* Clear the two screen cells. If the character is actually
8548 * single width it won't change the second cell. */
8549 out_str((char_u *)" ");
8550 term_windgoto(row, col);
8551 }
8552 /* not sure where the cursor is after drawing the ambiguous width
8553 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008554 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008555 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008556 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008558
8559 /* Convert the UTF-8 character to bytes and write it. */
8560 buf[utfc_char2bytes(off, buf)] = NUL;
8561 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 }
8563 else
8564#endif
8565 {
8566#ifdef FEAT_MBYTE
8567 out_flush_check();
8568#endif
8569 out_char(ScreenLines[off]);
8570#ifdef FEAT_MBYTE
8571 /* double-byte character in single-width cell */
8572 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8573 out_char(ScreenLines2[off]);
8574#endif
8575 }
8576
8577 screen_cur_col++;
8578}
8579
8580#ifdef FEAT_MBYTE
8581
8582/*
8583 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8584 * on the screen at position 'row' and 'col'.
8585 * The attributes of the first byte is used for all. This is required to
8586 * output the two bytes of a double-byte character with nothing in between.
8587 */
8588 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008589screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591 /* Check for illegal values (could be wrong when screen was resized). */
8592 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8593 return;
8594
8595 /* Outputting the last character on the screen may scrollup the screen.
8596 * Don't to it! Mark the character invalid (update it when scrolled up) */
8597 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8598 {
8599 ScreenAttrs[off] = (sattr_T)-1;
8600 return;
8601 }
8602
8603 /* Output the first byte normally (positions the cursor), then write the
8604 * second byte directly. */
8605 screen_char(off, row, col);
8606 out_char(ScreenLines[off + 1]);
8607 ++screen_cur_col;
8608}
8609#endif
8610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611/*
8612 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8613 * This uses the contents of ScreenLines[] and doesn't change it.
8614 */
8615 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008616screen_draw_rectangle(
8617 int row,
8618 int col,
8619 int height,
8620 int width,
8621 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
8623 int r, c;
8624 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008625#ifdef FEAT_MBYTE
8626 int max_off;
8627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008629 /* Can't use ScreenLines unless initialized */
8630 if (ScreenLines == NULL)
8631 return;
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 if (invert)
8634 screen_char_attr = HL_INVERSE;
8635 for (r = row; r < row + height; ++r)
8636 {
8637 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008638#ifdef FEAT_MBYTE
8639 max_off = off + screen_Columns;
8640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 for (c = col; c < col + width; ++c)
8642 {
8643#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008644 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
8646 screen_char_2(off + c, r, c);
8647 ++c;
8648 }
8649 else
8650#endif
8651 {
8652 screen_char(off + c, r, c);
8653#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008654 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 ++c;
8656#endif
8657 }
8658 }
8659 }
8660 screen_char_attr = 0;
8661}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663/*
8664 * Redraw the characters for a vertically split window.
8665 */
8666 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008667redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669 int col;
8670 int width;
8671
8672# ifdef FEAT_CLIPBOARD
8673 clip_may_clear_selection(row, end - 1);
8674# endif
8675
8676 if (wp == NULL)
8677 {
8678 col = 0;
8679 width = Columns;
8680 }
8681 else
8682 {
8683 col = wp->w_wincol;
8684 width = wp->w_width;
8685 }
8686 screen_draw_rectangle(row, col, end - row, width, FALSE);
8687}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008689 static void
8690space_to_screenline(int off, int attr)
8691{
8692 ScreenLines[off] = ' ';
8693 ScreenAttrs[off] = attr;
8694# ifdef FEAT_MBYTE
8695 if (enc_utf8)
8696 ScreenLinesUC[off] = 0;
8697# endif
8698}
8699
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700/*
8701 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8702 * with character 'c1' in first column followed by 'c2' in the other columns.
8703 * Use attributes 'attr'.
8704 */
8705 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008706screen_fill(
8707 int start_row,
8708 int end_row,
8709 int start_col,
8710 int end_col,
8711 int c1,
8712 int c2,
8713 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
8715 int row;
8716 int col;
8717 int off;
8718 int end_off;
8719 int did_delete;
8720 int c;
8721 int norm_term;
8722#if defined(FEAT_GUI) || defined(UNIX)
8723 int force_next = FALSE;
8724#endif
8725
8726 if (end_row > screen_Rows) /* safety check */
8727 end_row = screen_Rows;
8728 if (end_col > screen_Columns) /* safety check */
8729 end_col = screen_Columns;
8730 if (ScreenLines == NULL
8731 || start_row >= end_row
8732 || start_col >= end_col) /* nothing to do */
8733 return;
8734
8735 /* it's a "normal" terminal when not in a GUI or cterm */
8736 norm_term = (
8737#ifdef FEAT_GUI
8738 !gui.in_use &&
8739#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008740 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 for (row = start_row; row < end_row; ++row)
8742 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008743#ifdef FEAT_MBYTE
8744 if (has_mbyte
8745# ifdef FEAT_GUI
8746 && !gui.in_use
8747# endif
8748 )
8749 {
8750 /* When drawing over the right halve of a double-wide char clear
8751 * out the left halve. When drawing over the left halve of a
8752 * double wide-char clear out the right halve. Only needed in a
8753 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008754 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008755 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008756 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008757 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008758 }
8759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 /*
8761 * Try to use delete-line termcap code, when no attributes or in a
8762 * "normal" terminal, where a bold/italic space is just a
8763 * space.
8764 */
8765 did_delete = FALSE;
8766 if (c2 == ' '
8767 && end_col == Columns
8768 && can_clear(T_CE)
8769 && (attr == 0
8770 || (norm_term
8771 && attr <= HL_ALL
8772 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8773 {
8774 /*
8775 * check if we really need to clear something
8776 */
8777 col = start_col;
8778 if (c1 != ' ') /* don't clear first char */
8779 ++col;
8780
8781 off = LineOffset[row] + col;
8782 end_off = LineOffset[row] + end_col;
8783
8784 /* skip blanks (used often, keep it fast!) */
8785#ifdef FEAT_MBYTE
8786 if (enc_utf8)
8787 while (off < end_off && ScreenLines[off] == ' '
8788 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8789 ++off;
8790 else
8791#endif
8792 while (off < end_off && ScreenLines[off] == ' '
8793 && ScreenAttrs[off] == 0)
8794 ++off;
8795 if (off < end_off) /* something to be cleared */
8796 {
8797 col = off - LineOffset[row];
8798 screen_stop_highlight();
8799 term_windgoto(row, col);/* clear rest of this screen line */
8800 out_str(T_CE);
8801 screen_start(); /* don't know where cursor is now */
8802 col = end_col - col;
8803 while (col--) /* clear chars in ScreenLines */
8804 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008805 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 ++off;
8807 }
8808 }
8809 did_delete = TRUE; /* the chars are cleared now */
8810 }
8811
8812 off = LineOffset[row] + start_col;
8813 c = c1;
8814 for (col = start_col; col < end_col; ++col)
8815 {
8816 if (ScreenLines[off] != c
8817#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008818 || (enc_utf8 && (int)ScreenLinesUC[off]
8819 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820#endif
8821 || ScreenAttrs[off] != attr
8822#if defined(FEAT_GUI) || defined(UNIX)
8823 || force_next
8824#endif
8825 )
8826 {
8827#if defined(FEAT_GUI) || defined(UNIX)
8828 /* The bold trick may make a single row of pixels appear in
8829 * the next character. When a bold character is removed, the
8830 * next character should be redrawn too. This happens for our
8831 * own GUI and for some xterms. */
8832 if (
8833# ifdef FEAT_GUI
8834 gui.in_use
8835# endif
8836# if defined(FEAT_GUI) && defined(UNIX)
8837 ||
8838# endif
8839# ifdef UNIX
8840 term_is_xterm
8841# endif
8842 )
8843 {
8844 if (ScreenLines[off] != ' '
8845 && (ScreenAttrs[off] > HL_ALL
8846 || ScreenAttrs[off] & HL_BOLD))
8847 force_next = TRUE;
8848 else
8849 force_next = FALSE;
8850 }
8851#endif
8852 ScreenLines[off] = c;
8853#ifdef FEAT_MBYTE
8854 if (enc_utf8)
8855 {
8856 if (c >= 0x80)
8857 {
8858 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008859 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 }
8861 else
8862 ScreenLinesUC[off] = 0;
8863 }
8864#endif
8865 ScreenAttrs[off] = attr;
8866 if (!did_delete || c != ' ')
8867 screen_char(off, row, col);
8868 }
8869 ++off;
8870 if (col == start_col)
8871 {
8872 if (did_delete)
8873 break;
8874 c = c2;
8875 }
8876 }
8877 if (end_col == Columns)
8878 LineWraps[row] = FALSE;
8879 if (row == Rows - 1) /* overwritten the command line */
8880 {
8881 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008882 if (start_col == 0 && end_col == Columns
8883 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008885 if (start_col == 0)
8886 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 }
8888 }
8889}
8890
8891/*
8892 * Check if there should be a delay. Used before clearing or redrawing the
8893 * screen or the command line.
8894 */
8895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008896check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
8898 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8899 && !did_wait_return
8900 && emsg_silent == 0)
8901 {
8902 out_flush();
8903 ui_delay(1000L, TRUE);
8904 emsg_on_display = FALSE;
8905 if (check_msg_scroll)
8906 msg_scroll = FALSE;
8907 }
8908}
8909
8910/*
8911 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008912 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 * Returns TRUE if there is a valid screen to write to.
8914 * Returns FALSE when starting up and screen not initialized yet.
8915 */
8916 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008917screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008919 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 return (ScreenLines != NULL);
8921}
8922
8923/*
8924 * Resize the shell to Rows and Columns.
8925 * Allocate ScreenLines[] and associated items.
8926 *
8927 * There may be some time between setting Rows and Columns and (re)allocating
8928 * ScreenLines[]. This happens when starting up and when (manually) changing
8929 * the shell size. Always use screen_Rows and screen_Columns to access items
8930 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8931 * final size of the shell is needed.
8932 */
8933 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008934screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
8936 int new_row, old_row;
8937#ifdef FEAT_GUI
8938 int old_Rows;
8939#endif
8940 win_T *wp;
8941 int outofmem = FALSE;
8942 int len;
8943 schar_T *new_ScreenLines;
8944#ifdef FEAT_MBYTE
8945 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008946 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008948 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949#endif
8950 sattr_T *new_ScreenAttrs;
8951 unsigned *new_LineOffset;
8952 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008953 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008954 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008956 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008957 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008959retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 /*
8961 * Allocation of the screen buffers is done only when the size changes and
8962 * when Rows and Columns have been set and we have started doing full
8963 * screen stuff.
8964 */
8965 if ((ScreenLines != NULL
8966 && Rows == screen_Rows
8967 && Columns == screen_Columns
8968#ifdef FEAT_MBYTE
8969 && enc_utf8 == (ScreenLinesUC != NULL)
8970 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008971 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972#endif
8973 )
8974 || Rows == 0
8975 || Columns == 0
8976 || (!full_screen && ScreenLines == NULL))
8977 return;
8978
8979 /*
8980 * It's possible that we produce an out-of-memory message below, which
8981 * will cause this function to be called again. To break the loop, just
8982 * return here.
8983 */
8984 if (entered)
8985 return;
8986 entered = TRUE;
8987
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008988 /*
8989 * Note that the window sizes are updated before reallocating the arrays,
8990 * thus we must not redraw here!
8991 */
8992 ++RedrawingDisabled;
8993
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 win_new_shellsize(); /* fit the windows in the new sized shell */
8995
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 comp_col(); /* recompute columns for shown command and ruler */
8997
8998 /*
8999 * We're changing the size of the screen.
9000 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9001 * - Move lines from the old arrays into the new arrays, clear extra
9002 * lines (unless the screen is going to be cleared).
9003 * - Free the old arrays.
9004 *
9005 * If anything fails, make ScreenLines NULL, so we don't do anything!
9006 * Continuing with the old ScreenLines may result in a crash, because the
9007 * size is wrong.
9008 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009009 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009011 if (aucmd_win != NULL)
9012 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013
9014 new_ScreenLines = (schar_T *)lalloc((long_u)(
9015 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9016#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01009017 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 if (enc_utf8)
9019 {
9020 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
9021 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009022 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01009023 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
9025 }
9026 if (enc_dbcs == DBCS_JPNU)
9027 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9028 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9029#endif
9030 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9031 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9032 new_LineOffset = (unsigned *)lalloc((long_u)(
9033 Rows * sizeof(unsigned)), FALSE);
9034 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009035 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009037 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 {
9039 if (win_alloc_lines(wp) == FAIL)
9040 {
9041 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009042 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009045 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9046 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009047 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009048give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009050#ifdef FEAT_MBYTE
9051 for (i = 0; i < p_mco; ++i)
9052 if (new_ScreenLinesC[i] == NULL)
9053 break;
9054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 if (new_ScreenLines == NULL
9056#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009057 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9059#endif
9060 || new_ScreenAttrs == NULL
9061 || new_LineOffset == NULL
9062 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009063 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 || outofmem)
9065 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009066 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009067 {
9068 /* guess the size */
9069 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9070
9071 /* Remember we did this to avoid getting outofmem messages over
9072 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009073 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009074 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009075 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009077 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009078 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009079 VIM_CLEAR(new_ScreenLinesC[i]);
9080 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009082 VIM_CLEAR(new_ScreenAttrs);
9083 VIM_CLEAR(new_LineOffset);
9084 VIM_CLEAR(new_LineWraps);
9085 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 }
9087 else
9088 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009089 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009090
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 for (new_row = 0; new_row < Rows; ++new_row)
9092 {
9093 new_LineOffset[new_row] = new_row * Columns;
9094 new_LineWraps[new_row] = FALSE;
9095
9096 /*
9097 * If the screen is not going to be cleared, copy as much as
9098 * possible from the old screen to the new one and clear the rest
9099 * (used when resizing the window at the "--more--" prompt or when
9100 * executing an external command, for the GUI).
9101 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009102 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 {
9104 (void)vim_memset(new_ScreenLines + new_row * Columns,
9105 ' ', (size_t)Columns * sizeof(schar_T));
9106#ifdef FEAT_MBYTE
9107 if (enc_utf8)
9108 {
9109 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9110 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009111 for (i = 0; i < p_mco; ++i)
9112 (void)vim_memset(new_ScreenLinesC[i]
9113 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 0, (size_t)Columns * sizeof(u8char_T));
9115 }
9116 if (enc_dbcs == DBCS_JPNU)
9117 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9118 0, (size_t)Columns * sizeof(schar_T));
9119#endif
9120 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9121 0, (size_t)Columns * sizeof(sattr_T));
9122 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009123 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 {
9125 if (screen_Columns < Columns)
9126 len = screen_Columns;
9127 else
9128 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009129#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009130 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009131 * may be invalid now. Also when p_mco changes. */
9132 if (!(enc_utf8 && ScreenLinesUC == NULL)
9133 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009134#endif
9135 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9136 ScreenLines + LineOffset[old_row],
9137 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009139 if (enc_utf8 && ScreenLinesUC != NULL
9140 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 {
9142 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9143 ScreenLinesUC + LineOffset[old_row],
9144 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009145 for (i = 0; i < p_mco; ++i)
9146 mch_memmove(new_ScreenLinesC[i]
9147 + new_LineOffset[new_row],
9148 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 (size_t)len * sizeof(u8char_T));
9150 }
9151 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9152 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9153 ScreenLines2 + LineOffset[old_row],
9154 (size_t)len * sizeof(schar_T));
9155#endif
9156 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9157 ScreenAttrs + LineOffset[old_row],
9158 (size_t)len * sizeof(sattr_T));
9159 }
9160 }
9161 }
9162 /* Use the last line of the screen for the current line. */
9163 current_ScreenLine = new_ScreenLines + Rows * Columns;
9164 }
9165
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009166 free_screenlines();
9167
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 ScreenLines = new_ScreenLines;
9169#ifdef FEAT_MBYTE
9170 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009171 for (i = 0; i < p_mco; ++i)
9172 ScreenLinesC[i] = new_ScreenLinesC[i];
9173 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 ScreenLines2 = new_ScreenLines2;
9175#endif
9176 ScreenAttrs = new_ScreenAttrs;
9177 LineOffset = new_LineOffset;
9178 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009179 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180
9181 /* It's important that screen_Rows and screen_Columns reflect the actual
9182 * size of ScreenLines[]. Set them before calling anything. */
9183#ifdef FEAT_GUI
9184 old_Rows = screen_Rows;
9185#endif
9186 screen_Rows = Rows;
9187 screen_Columns = Columns;
9188
9189 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009190 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 screenclear2();
9192
9193#ifdef FEAT_GUI
9194 else if (gui.in_use
9195 && !gui.starting
9196 && ScreenLines != NULL
9197 && old_Rows != Rows)
9198 {
9199 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9200 /*
9201 * Adjust the position of the cursor, for when executing an external
9202 * command.
9203 */
9204 if (msg_row >= Rows) /* Rows got smaller */
9205 msg_row = Rows - 1; /* put cursor at last row */
9206 else if (Rows > old_Rows) /* Rows got bigger */
9207 msg_row += Rows - old_Rows; /* put cursor in same place */
9208 if (msg_col >= Columns) /* Columns got smaller */
9209 msg_col = Columns - 1; /* put cursor at last column */
9210 }
9211#endif
9212
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009214 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009215
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009216 /*
9217 * Do not apply autocommands more than 3 times to avoid an endless loop
9218 * in case applying autocommands always changes Rows or Columns.
9219 */
9220 if (starting == 0 && ++retry_count <= 3)
9221 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009222 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009223 /* In rare cases, autocommands may have altered Rows or Columns,
9224 * jump back to check if we need to allocate the screen again. */
9225 goto retry;
9226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227}
9228
9229 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009230free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009231{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009232#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009233 int i;
9234
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009235 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009236 for (i = 0; i < Screen_mco; ++i)
9237 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009238 vim_free(ScreenLines2);
9239#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009240 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009241 vim_free(ScreenAttrs);
9242 vim_free(LineOffset);
9243 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009244 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009245}
9246
9247 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009248screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249{
9250 check_for_delay(FALSE);
9251 screenalloc(FALSE); /* allocate screen buffers if size changed */
9252 screenclear2(); /* clear the screen */
9253}
9254
9255 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009256screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
9258 int i;
9259
9260 if (starting == NO_SCREEN || ScreenLines == NULL
9261#ifdef FEAT_GUI
9262 || (gui.in_use && gui.starting)
9263#endif
9264 )
9265 return;
9266
9267#ifdef FEAT_GUI
9268 if (!gui.in_use)
9269#endif
9270 screen_attr = -1; /* force setting the Normal colors */
9271 screen_stop_highlight(); /* don't want highlighting here */
9272
9273#ifdef FEAT_CLIPBOARD
9274 /* disable selection without redrawing it */
9275 clip_scroll_selection(9999);
9276#endif
9277
9278 /* blank out ScreenLines */
9279 for (i = 0; i < Rows; ++i)
9280 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009281 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 LineWraps[i] = FALSE;
9283 }
9284
9285 if (can_clear(T_CL))
9286 {
9287 out_str(T_CL); /* clear the display */
9288 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009289 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 }
9291 else
9292 {
9293 /* can't clear the screen, mark all chars with invalid attributes */
9294 for (i = 0; i < Rows; ++i)
9295 lineinvalid(LineOffset[i], (int)Columns);
9296 clear_cmdline = TRUE;
9297 }
9298
9299 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9300
9301 win_rest_invalid(firstwin);
9302 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009303 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 if (must_redraw == CLEAR) /* no need to clear again */
9305 must_redraw = NOT_VALID;
9306 compute_cmdrow();
9307 msg_row = cmdline_row; /* put cursor on last line for messages */
9308 msg_col = 0;
9309 screen_start(); /* don't know where cursor is now */
9310 msg_scrolled = 0; /* can't scroll back */
9311 msg_didany = FALSE;
9312 msg_didout = FALSE;
9313}
9314
9315/*
9316 * Clear one line in ScreenLines.
9317 */
9318 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009319lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9322#ifdef FEAT_MBYTE
9323 if (enc_utf8)
9324 (void)vim_memset(ScreenLinesUC + off, 0,
9325 (size_t)width * sizeof(u8char_T));
9326#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009327 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328}
9329
9330/*
9331 * Mark one line in ScreenLines invalid by setting the attributes to an
9332 * invalid value.
9333 */
9334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009335lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336{
9337 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9338}
9339
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340/*
9341 * Copy part of a Screenline for vertically split window "wp".
9342 */
9343 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009344linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345{
9346 unsigned off_to = LineOffset[to] + wp->w_wincol;
9347 unsigned off_from = LineOffset[from] + wp->w_wincol;
9348
9349 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9350 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009351#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 if (enc_utf8)
9353 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009354 int i;
9355
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9357 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009358 for (i = 0; i < p_mco; ++i)
9359 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9360 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 }
9362 if (enc_dbcs == DBCS_JPNU)
9363 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9364 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9367 wp->w_width * sizeof(sattr_T));
9368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
9370/*
9371 * Return TRUE if clearing with term string "p" would work.
9372 * It can't work when the string is empty or it won't set the right background.
9373 */
9374 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009375can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376{
9377 return (*p != NUL && (t_colors <= 1
9378#ifdef FEAT_GUI
9379 || gui.in_use
9380#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009381#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009382 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009383 || (!p_tgc && cterm_normal_bg_color == 0)
9384#else
9385 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009386#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009387 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388}
9389
9390/*
9391 * Reset cursor position. Use whenever cursor was moved because of outputting
9392 * something directly to the screen (shell commands) or a terminal control
9393 * code.
9394 */
9395 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009396screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 screen_cur_row = screen_cur_col = 9999;
9399}
9400
9401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 * Move the cursor to position "row","col" in the screen.
9403 * This tries to find the most efficient way to move, minimizing the number of
9404 * characters sent to the terminal.
9405 */
9406 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009407windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009409 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 int i;
9411 int plan;
9412 int cost;
9413 int wouldbe_col;
9414 int noinvcurs;
9415 char_u *bs;
9416 int goto_cost;
9417 int attr;
9418
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009419#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9421
9422#define PLAN_LE 1
9423#define PLAN_CR 2
9424#define PLAN_NL 3
9425#define PLAN_WRITE 4
9426 /* Can't use ScreenLines unless initialized */
9427 if (ScreenLines == NULL)
9428 return;
9429
9430 if (col != screen_cur_col || row != screen_cur_row)
9431 {
9432 /* Check for valid position. */
9433 if (row < 0) /* window without text lines? */
9434 row = 0;
9435 if (row >= screen_Rows)
9436 row = screen_Rows - 1;
9437 if (col >= screen_Columns)
9438 col = screen_Columns - 1;
9439
9440 /* check if no cursor movement is allowed in highlight mode */
9441 if (screen_attr && *T_MS == NUL)
9442 noinvcurs = HIGHL_COST;
9443 else
9444 noinvcurs = 0;
9445 goto_cost = GOTO_COST + noinvcurs;
9446
9447 /*
9448 * Plan how to do the positioning:
9449 * 1. Use CR to move it to column 0, same row.
9450 * 2. Use T_LE to move it a few columns to the left.
9451 * 3. Use NL to move a few lines down, column 0.
9452 * 4. Move a few columns to the right with T_ND or by writing chars.
9453 *
9454 * Don't do this if the cursor went beyond the last column, the cursor
9455 * position is unknown then (some terminals wrap, some don't )
9456 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009457 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 * characters to move the cursor to the right.
9459 */
9460 if (row >= screen_cur_row && screen_cur_col < Columns)
9461 {
9462 /*
9463 * If the cursor is in the same row, bigger col, we can use CR
9464 * or T_LE.
9465 */
9466 bs = NULL; /* init for GCC */
9467 attr = screen_attr;
9468 if (row == screen_cur_row && col < screen_cur_col)
9469 {
9470 /* "le" is preferred over "bc", because "bc" is obsolete */
9471 if (*T_LE)
9472 bs = T_LE; /* "cursor left" */
9473 else
9474 bs = T_BC; /* "backspace character (old) */
9475 if (*bs)
9476 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9477 else
9478 cost = 999;
9479 if (col + 1 < cost) /* using CR is less characters */
9480 {
9481 plan = PLAN_CR;
9482 wouldbe_col = 0;
9483 cost = 1; /* CR is just one character */
9484 }
9485 else
9486 {
9487 plan = PLAN_LE;
9488 wouldbe_col = col;
9489 }
9490 if (noinvcurs) /* will stop highlighting */
9491 {
9492 cost += noinvcurs;
9493 attr = 0;
9494 }
9495 }
9496
9497 /*
9498 * If the cursor is above where we want to be, we can use CR LF.
9499 */
9500 else if (row > screen_cur_row)
9501 {
9502 plan = PLAN_NL;
9503 wouldbe_col = 0;
9504 cost = (row - screen_cur_row) * 2; /* CR LF */
9505 if (noinvcurs) /* will stop highlighting */
9506 {
9507 cost += noinvcurs;
9508 attr = 0;
9509 }
9510 }
9511
9512 /*
9513 * If the cursor is in the same row, smaller col, just use write.
9514 */
9515 else
9516 {
9517 plan = PLAN_WRITE;
9518 wouldbe_col = screen_cur_col;
9519 cost = 0;
9520 }
9521
9522 /*
9523 * Check if any characters that need to be written have the
9524 * correct attributes. Also avoid UTF-8 characters.
9525 */
9526 i = col - wouldbe_col;
9527 if (i > 0)
9528 cost += i;
9529 if (cost < goto_cost && i > 0)
9530 {
9531 /*
9532 * Check if the attributes are correct without additionally
9533 * stopping highlighting.
9534 */
9535 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9536 while (i && *p++ == attr)
9537 --i;
9538 if (i != 0)
9539 {
9540 /*
9541 * Try if it works when highlighting is stopped here.
9542 */
9543 if (*--p == 0)
9544 {
9545 cost += noinvcurs;
9546 while (i && *p++ == 0)
9547 --i;
9548 }
9549 if (i != 0)
9550 cost = 999; /* different attributes, don't do it */
9551 }
9552#ifdef FEAT_MBYTE
9553 if (enc_utf8)
9554 {
9555 /* Don't use an UTF-8 char for positioning, it's slow. */
9556 for (i = wouldbe_col; i < col; ++i)
9557 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9558 {
9559 cost = 999;
9560 break;
9561 }
9562 }
9563#endif
9564 }
9565
9566 /*
9567 * We can do it without term_windgoto()!
9568 */
9569 if (cost < goto_cost)
9570 {
9571 if (plan == PLAN_LE)
9572 {
9573 if (noinvcurs)
9574 screen_stop_highlight();
9575 while (screen_cur_col > col)
9576 {
9577 out_str(bs);
9578 --screen_cur_col;
9579 }
9580 }
9581 else if (plan == PLAN_CR)
9582 {
9583 if (noinvcurs)
9584 screen_stop_highlight();
9585 out_char('\r');
9586 screen_cur_col = 0;
9587 }
9588 else if (plan == PLAN_NL)
9589 {
9590 if (noinvcurs)
9591 screen_stop_highlight();
9592 while (screen_cur_row < row)
9593 {
9594 out_char('\n');
9595 ++screen_cur_row;
9596 }
9597 screen_cur_col = 0;
9598 }
9599
9600 i = col - screen_cur_col;
9601 if (i > 0)
9602 {
9603 /*
9604 * Use cursor-right if it's one character only. Avoids
9605 * removing a line of pixels from the last bold char, when
9606 * using the bold trick in the GUI.
9607 */
9608 if (T_ND[0] != NUL && T_ND[1] == NUL)
9609 {
9610 while (i-- > 0)
9611 out_char(*T_ND);
9612 }
9613 else
9614 {
9615 int off;
9616
9617 off = LineOffset[row] + screen_cur_col;
9618 while (i-- > 0)
9619 {
9620 if (ScreenAttrs[off] != screen_attr)
9621 screen_stop_highlight();
9622#ifdef FEAT_MBYTE
9623 out_flush_check();
9624#endif
9625 out_char(ScreenLines[off]);
9626#ifdef FEAT_MBYTE
9627 if (enc_dbcs == DBCS_JPNU
9628 && ScreenLines[off] == 0x8e)
9629 out_char(ScreenLines2[off]);
9630#endif
9631 ++off;
9632 }
9633 }
9634 }
9635 }
9636 }
9637 else
9638 cost = 999;
9639
9640 if (cost >= goto_cost)
9641 {
9642 if (noinvcurs)
9643 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009644 if (row == screen_cur_row && (col > screen_cur_col)
9645 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 term_cursor_right(col - screen_cur_col);
9647 else
9648 term_windgoto(row, col);
9649 }
9650 screen_cur_row = row;
9651 screen_cur_col = col;
9652 }
9653}
9654
9655/*
9656 * Set cursor to its position in the current window.
9657 */
9658 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009659setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009661 setcursor_mayforce(FALSE);
9662}
9663
9664/*
9665 * Set cursor to its position in the current window.
9666 * When "force" is TRUE also when not redrawing.
9667 */
9668 void
9669setcursor_mayforce(int force)
9670{
9671 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 {
9673 validate_cursor();
9674 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009675 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009677 /* With 'rightleft' set and the cursor on a double-wide
9678 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009679 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009681 (has_mbyte
9682 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9683 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684# endif
9685 1)) :
9686#endif
9687 curwin->w_wcol));
9688 }
9689}
9690
9691
9692/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009693 * Insert 'line_count' lines at 'row' in window 'wp'.
9694 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9695 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 * scrolling.
9697 * Returns FAIL if the lines are not inserted, OK for success.
9698 */
9699 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009700win_ins_lines(
9701 win_T *wp,
9702 int row,
9703 int line_count,
9704 int invalid,
9705 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 int did_delete;
9708 int nextrow;
9709 int lastrow;
9710 int retval;
9711
9712 if (invalid)
9713 wp->w_lines_valid = 0;
9714
9715 if (wp->w_height < 5)
9716 return FAIL;
9717
9718 if (line_count > wp->w_height - row)
9719 line_count = wp->w_height - row;
9720
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009721 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 if (retval != MAYBE)
9723 return retval;
9724
9725 /*
9726 * If there is a next window or a status line, we first try to delete the
9727 * lines at the bottom to avoid messing what is after the window.
9728 * If this fails and there are following windows, don't do anything to avoid
9729 * messing up those windows, better just redraw.
9730 */
9731 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (wp->w_next != NULL || wp->w_status_height)
9733 {
9734 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009735 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 did_delete = TRUE;
9737 else if (wp->w_next)
9738 return FAIL;
9739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 /*
9741 * if no lines deleted, blank the lines that will end up below the window
9742 */
9743 if (!did_delete)
9744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009747 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 lastrow = nextrow + line_count;
9749 if (lastrow > Rows)
9750 lastrow = Rows;
9751 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009752 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 ' ', ' ', 0);
9754 }
9755
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009756 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 == FAIL)
9758 {
9759 /* deletion will have messed up other windows */
9760 if (did_delete)
9761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 win_rest_invalid(W_NEXT(wp));
9764 }
9765 return FAIL;
9766 }
9767
9768 return OK;
9769}
9770
9771/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009772 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9774 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9775 * scrolling
9776 * Return OK for success, FAIL if the lines are not deleted.
9777 */
9778 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009779win_del_lines(
9780 win_T *wp,
9781 int row,
9782 int line_count,
9783 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009784 int mayclear,
9785 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
9787 int retval;
9788
9789 if (invalid)
9790 wp->w_lines_valid = 0;
9791
9792 if (line_count > wp->w_height - row)
9793 line_count = wp->w_height - row;
9794
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009795 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 if (retval != MAYBE)
9797 return retval;
9798
9799 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009800 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 return FAIL;
9802
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 /*
9804 * If there are windows or status lines below, try to put them at the
9805 * correct place. If we can't do that, they have to be redrawn.
9806 */
9807 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9808 {
9809 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009810 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 {
9812 wp->w_redr_status = TRUE;
9813 win_rest_invalid(wp->w_next);
9814 }
9815 }
9816 /*
9817 * If this is the last window and there is no status line, redraw the
9818 * command line later.
9819 */
9820 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 redraw_cmdline = TRUE;
9822 return OK;
9823}
9824
9825/*
9826 * Common code for win_ins_lines() and win_del_lines().
9827 * Returns OK or FAIL when the work has been done.
9828 * Returns MAYBE when not finished yet.
9829 */
9830 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009831win_do_lines(
9832 win_T *wp,
9833 int row,
9834 int line_count,
9835 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009836 int del,
9837 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
9839 int retval;
9840
9841 if (!redrawing() || line_count <= 0)
9842 return FAIL;
9843
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009844 /* When inserting lines would result in loss of command output, just redraw
9845 * the lines. */
9846 if (no_win_do_lines_ins && !del)
9847 return FAIL;
9848
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009850 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009852 if (!no_win_do_lines_ins)
9853 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 return FAIL;
9855 }
9856
9857 /*
9858 * Delete all remaining lines
9859 */
9860 if (row + line_count >= wp->w_height)
9861 {
9862 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009863 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 ' ', ' ', 0);
9865 return OK;
9866 }
9867
9868 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009869 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009871 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009873 if (!no_win_do_lines_ins)
9874 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875
9876 /*
9877 * If the terminal can set a scroll region, use that.
9878 * Always do this in a vertically split window. This will redraw from
9879 * ScreenLines[] when t_CV isn't defined. That's faster than using
9880 * win_line().
9881 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009882 * a character in the lower right corner of the scroll region may cause a
9883 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009885 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 scroll_region_set(wp, row);
9889 if (del)
9890 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009891 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 else
9893 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009894 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 scroll_region_reset();
9897 return retval;
9898 }
9899
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9901 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
9903 return MAYBE;
9904}
9905
9906/*
9907 * window 'wp' and everything after it is messed up, mark it for redraw
9908 */
9909 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009910win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 {
9914 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 wp->w_redr_status = TRUE;
9916 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 }
9918 redraw_cmdline = TRUE;
9919}
9920
9921/*
9922 * The rest of the routines in this file perform screen manipulations. The
9923 * given operation is performed physically on the screen. The corresponding
9924 * change is also made to the internal screen image. In this way, the editor
9925 * anticipates the effect of editing changes on the appearance of the screen.
9926 * That way, when we call screenupdate a complete redraw isn't usually
9927 * necessary. Another advantage is that we can keep adding code to anticipate
9928 * screen changes, and in the meantime, everything still works.
9929 */
9930
9931/*
9932 * types for inserting or deleting lines
9933 */
9934#define USE_T_CAL 1
9935#define USE_T_CDL 2
9936#define USE_T_AL 3
9937#define USE_T_CE 4
9938#define USE_T_DL 5
9939#define USE_T_SR 6
9940#define USE_NL 7
9941#define USE_T_CD 8
9942#define USE_REDRAW 9
9943
9944/*
9945 * insert lines on the screen and update ScreenLines[]
9946 * 'end' is the line after the scrolled part. Normally it is Rows.
9947 * When scrolling region used 'off' is the offset from the top for the region.
9948 * 'row' and 'end' are relative to the start of the region.
9949 *
9950 * return FAIL for failure, OK for success.
9951 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009952 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009953screen_ins_lines(
9954 int off,
9955 int row,
9956 int line_count,
9957 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009958 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009959 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960{
9961 int i;
9962 int j;
9963 unsigned temp;
9964 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009965 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 int type;
9967 int result_empty;
9968 int can_ce = can_clear(T_CE);
9969
9970 /*
9971 * FAIL if
9972 * - there is no valid screen
9973 * - the screen has to be redrawn completely
9974 * - the line count is less than one
9975 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009976 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009978 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9979#ifdef FEAT_CLIPBOARD
9980 || (clip_star.state != SELECT_CLEARED
9981 && redrawing_for_callback > 0)
9982#endif
9983 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 return FAIL;
9985
9986 /*
9987 * There are seven ways to insert lines:
9988 * 0. When in a vertically split window and t_CV isn't set, redraw the
9989 * characters from ScreenLines[].
9990 * 1. Use T_CD (clear to end of display) if it exists and the result of
9991 * the insert is just empty lines
9992 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9993 * present or line_count > 1. It looks better if we do all the inserts
9994 * at once.
9995 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9996 * insert is just empty lines and T_CE is not present or line_count >
9997 * 1.
9998 * 4. Use T_AL (insert line) if it exists.
9999 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10000 * just empty lines.
10001 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10002 * just empty lines.
10003 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10004 * the 'da' flag is not set or we have clear line capability.
10005 * 8. redraw the characters from ScreenLines[].
10006 *
10007 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10008 * the scrollbar for the window. It does have insert line, use that if it
10009 * exists.
10010 */
10011 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10013 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010014 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 type = USE_T_CD;
10016 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10017 type = USE_T_CAL;
10018 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10019 type = USE_T_CDL;
10020 else if (*T_AL != NUL)
10021 type = USE_T_AL;
10022 else if (can_ce && result_empty)
10023 type = USE_T_CE;
10024 else if (*T_DL != NUL && result_empty)
10025 type = USE_T_DL;
10026 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10027 type = USE_T_SR;
10028 else
10029 return FAIL;
10030
10031 /*
10032 * For clearing the lines screen_del_lines() is used. This will also take
10033 * care of t_db if necessary.
10034 */
10035 if (type == USE_T_CD || type == USE_T_CDL ||
10036 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010037 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038
10039 /*
10040 * If text is retained below the screen, first clear or delete as many
10041 * lines at the bottom of the window as are about to be inserted so that
10042 * the deleted lines won't later surface during a screen_del_lines.
10043 */
10044 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010045 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046
10047#ifdef FEAT_CLIPBOARD
10048 /* Remove a modeless selection when inserting lines halfway the screen
10049 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010050 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010051 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 else
10053 clip_scroll_selection(-line_count);
10054#endif
10055
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056#ifdef FEAT_GUI
10057 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10058 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010059 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060#endif
10061
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010062 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10063 cursor_col = wp->w_wincol;
10064
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 if (*T_CCS != NUL) /* cursor relative to region */
10066 cursor_row = row;
10067 else
10068 cursor_row = row + off;
10069
10070 /*
10071 * Shift LineOffset[] line_count down to reflect the inserted lines.
10072 * Clear the inserted lines in ScreenLines[].
10073 */
10074 row += off;
10075 end += off;
10076 for (i = 0; i < line_count; ++i)
10077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 if (wp != NULL && wp->w_width != Columns)
10079 {
10080 /* need to copy part of a line */
10081 j = end - 1 - i;
10082 while ((j -= line_count) >= row)
10083 linecopy(j + line_count, j, wp);
10084 j += line_count;
10085 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010086 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10087 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 else
10089 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10090 LineWraps[j] = FALSE;
10091 }
10092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 {
10094 j = end - 1 - i;
10095 temp = LineOffset[j];
10096 while ((j -= line_count) >= row)
10097 {
10098 LineOffset[j + line_count] = LineOffset[j];
10099 LineWraps[j + line_count] = LineWraps[j];
10100 }
10101 LineOffset[j + line_count] = temp;
10102 LineWraps[j + line_count] = FALSE;
10103 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010104 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 else
10106 lineinvalid(temp, (int)Columns);
10107 }
10108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109
10110 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010111 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010112 if (clear_attr != 0)
10113 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 /* redraw the characters */
10116 if (type == USE_REDRAW)
10117 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010118 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 {
10120 term_append_lines(line_count);
10121 screen_start(); /* don't know where cursor is now */
10122 }
10123 else
10124 {
10125 for (i = 0; i < line_count; i++)
10126 {
10127 if (type == USE_T_AL)
10128 {
10129 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010130 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 out_str(T_AL);
10132 }
10133 else /* type == USE_T_SR */
10134 out_str(T_SR);
10135 screen_start(); /* don't know where cursor is now */
10136 }
10137 }
10138
10139 /*
10140 * With scroll-reverse and 'da' flag set we need to clear the lines that
10141 * have been scrolled down into the region.
10142 */
10143 if (type == USE_T_SR && *T_DA)
10144 {
10145 for (i = 0; i < line_count; ++i)
10146 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010147 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 out_str(T_CE);
10149 screen_start(); /* don't know where cursor is now */
10150 }
10151 }
10152
10153#ifdef FEAT_GUI
10154 gui_can_update_cursor();
10155 if (gui.in_use)
10156 out_flush(); /* always flush after a scroll */
10157#endif
10158 return OK;
10159}
10160
10161/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010162 * Delete lines on the screen and update ScreenLines[].
10163 * "end" is the line after the scrolled part. Normally it is Rows.
10164 * When scrolling region used "off" is the offset from the top for the region.
10165 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 *
10167 * Return OK for success, FAIL if the lines are not deleted.
10168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010170screen_del_lines(
10171 int off,
10172 int row,
10173 int line_count,
10174 int end,
10175 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010176 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010177 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178{
10179 int j;
10180 int i;
10181 unsigned temp;
10182 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010183 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 int cursor_end;
10185 int result_empty; /* result is empty until end of region */
10186 int can_delete; /* deleting line codes can be used */
10187 int type;
10188
10189 /*
10190 * FAIL if
10191 * - there is no valid screen
10192 * - the screen has to be redrawn completely
10193 * - the line count is less than one
10194 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010195 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010197 if (!screen_valid(TRUE) || line_count <= 0
10198 || (!force && line_count > p_ttyscroll)
10199#ifdef FEAT_CLIPBOARD
10200 || (clip_star.state != SELECT_CLEARED
10201 && redrawing_for_callback > 0)
10202#endif
10203 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 return FAIL;
10205
10206 /*
10207 * Check if the rest of the current region will become empty.
10208 */
10209 result_empty = row + line_count >= end;
10210
10211 /*
10212 * We can delete lines only when 'db' flag not set or when 'ce' option
10213 * available.
10214 */
10215 can_delete = (*T_DB == NUL || can_clear(T_CE));
10216
10217 /*
10218 * There are six ways to delete lines:
10219 * 0. When in a vertically split window and t_CV isn't set, redraw the
10220 * characters from ScreenLines[].
10221 * 1. Use T_CD if it exists and the result is empty.
10222 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10223 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10224 * none of the other ways work.
10225 * 4. Use T_CE (erase line) if the result is empty.
10226 * 5. Use T_DL (delete line) if it exists.
10227 * 6. redraw the characters from ScreenLines[].
10228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10230 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010231 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 type = USE_T_CD;
10233#if defined(__BEOS__) && defined(BEOS_DR8)
10234 /*
10235 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10236 * its internal termcap... this works okay for tests which test *T_DB !=
10237 * NUL. It has the disadvantage that the user cannot use any :set t_*
10238 * command to get T_DB (back) to empty_option, only :set term=... will do
10239 * the trick...
10240 * Anyway, this hack will hopefully go away with the next OS release.
10241 * (Olaf Seibert)
10242 */
10243 else if (row == 0 && T_DB == empty_option
10244 && (line_count == 1 || *T_CDL == NUL))
10245#else
10246 else if (row == 0 && (
10247#ifndef AMIGA
10248 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10249 * up, so use delete-line command */
10250 line_count == 1 ||
10251#endif
10252 *T_CDL == NUL))
10253#endif
10254 type = USE_NL;
10255 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10256 type = USE_T_CDL;
10257 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010258 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 type = USE_T_CE;
10260 else if (*T_DL != NUL && can_delete)
10261 type = USE_T_DL;
10262 else if (*T_CDL != NUL && can_delete)
10263 type = USE_T_CDL;
10264 else
10265 return FAIL;
10266
10267#ifdef FEAT_CLIPBOARD
10268 /* Remove a modeless selection when deleting lines halfway the screen or
10269 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010270 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010271 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 else
10273 clip_scroll_selection(line_count);
10274#endif
10275
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276#ifdef FEAT_GUI
10277 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10278 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010279 gui_dont_update_cursor(gui.cursor_row >= row + off
10280 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#endif
10282
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010283 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10284 cursor_col = wp->w_wincol;
10285
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 if (*T_CCS != NUL) /* cursor relative to region */
10287 {
10288 cursor_row = row;
10289 cursor_end = end;
10290 }
10291 else
10292 {
10293 cursor_row = row + off;
10294 cursor_end = end + off;
10295 }
10296
10297 /*
10298 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10299 * Clear the inserted lines in ScreenLines[].
10300 */
10301 row += off;
10302 end += off;
10303 for (i = 0; i < line_count; ++i)
10304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 if (wp != NULL && wp->w_width != Columns)
10306 {
10307 /* need to copy part of a line */
10308 j = row + i;
10309 while ((j += line_count) <= end - 1)
10310 linecopy(j - line_count, j, wp);
10311 j -= line_count;
10312 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010313 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10314 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 else
10316 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10317 LineWraps[j] = FALSE;
10318 }
10319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 {
10321 /* whole width, moving the line pointers is faster */
10322 j = row + i;
10323 temp = LineOffset[j];
10324 while ((j += line_count) <= end - 1)
10325 {
10326 LineOffset[j - line_count] = LineOffset[j];
10327 LineWraps[j - line_count] = LineWraps[j];
10328 }
10329 LineOffset[j - line_count] = temp;
10330 LineWraps[j - line_count] = FALSE;
10331 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010332 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 else
10334 lineinvalid(temp, (int)Columns);
10335 }
10336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010338 if (screen_attr != clear_attr)
10339 screen_stop_highlight();
10340 if (clear_attr != 0)
10341 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 /* redraw the characters */
10344 if (type == USE_REDRAW)
10345 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010346 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010348 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 out_str(T_CD);
10350 screen_start(); /* don't know where cursor is now */
10351 }
10352 else if (type == USE_T_CDL)
10353 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010354 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 term_delete_lines(line_count);
10356 screen_start(); /* don't know where cursor is now */
10357 }
10358 /*
10359 * Deleting lines at top of the screen or scroll region: Just scroll
10360 * the whole screen (scroll region) up by outputting newlines on the
10361 * last line.
10362 */
10363 else if (type == USE_NL)
10364 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010365 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 for (i = line_count; --i >= 0; )
10367 out_char('\n'); /* cursor will remain on same line */
10368 }
10369 else
10370 {
10371 for (i = line_count; --i >= 0; )
10372 {
10373 if (type == USE_T_DL)
10374 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010375 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 out_str(T_DL); /* delete a line */
10377 }
10378 else /* type == USE_T_CE */
10379 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010380 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 out_str(T_CE); /* erase a line */
10382 }
10383 screen_start(); /* don't know where cursor is now */
10384 }
10385 }
10386
10387 /*
10388 * If the 'db' flag is set, we need to clear the lines that have been
10389 * scrolled up at the bottom of the region.
10390 */
10391 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10392 {
10393 for (i = line_count; i > 0; --i)
10394 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010395 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 out_str(T_CE); /* erase a line */
10397 screen_start(); /* don't know where cursor is now */
10398 }
10399 }
10400
10401#ifdef FEAT_GUI
10402 gui_can_update_cursor();
10403 if (gui.in_use)
10404 out_flush(); /* always flush after a scroll */
10405#endif
10406
10407 return OK;
10408}
10409
10410/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010411 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 *
10413 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10414 * If clear_cmdline is FALSE there may be a message there that needs to be
10415 * cleared only if a mode is shown.
10416 * Return the length of the message (0 if no message).
10417 */
10418 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010419showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420{
10421 int need_clear;
10422 int length = 0;
10423 int do_mode;
10424 int attr;
10425 int nwr_save;
10426#ifdef FEAT_INS_EXPAND
10427 int sub_attr;
10428#endif
10429
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010430 do_mode = ((p_smd && msg_silent == 0)
10431 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010432 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010433 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010434 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 {
10436 /*
10437 * Don't show mode right now, when not redrawing or inside a mapping.
10438 * Call char_avail() only when we are going to show something, because
10439 * it takes a bit of time.
10440 */
10441 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10442 {
10443 redraw_cmdline = TRUE; /* show mode later */
10444 return 0;
10445 }
10446
10447 nwr_save = need_wait_return;
10448
10449 /* wait a bit before overwriting an important message */
10450 check_for_delay(FALSE);
10451
10452 /* if the cmdline is more than one line high, erase top lines */
10453 need_clear = clear_cmdline;
10454 if (clear_cmdline && cmdline_row < Rows - 1)
10455 msg_clr_cmdline(); /* will reset clear_cmdline */
10456
10457 /* Position on the last line in the window, column 0 */
10458 msg_pos_mode();
10459 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010460 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 if (do_mode)
10462 {
10463 MSG_PUTS_ATTR("--", attr);
10464#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010465 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010466# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010467 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010468# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010469 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010470# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010471 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010472# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 MSG_PUTS_ATTR(" IM", attr);
10474# else
10475 MSG_PUTS_ATTR(" XIM", attr);
10476# endif
10477#endif
10478#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10479 if (gui.in_use)
10480 {
10481 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010482 {
10483 /* HANGUL */
10484 if (enc_utf8)
10485 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10486 else
10487 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 }
10490#endif
10491#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010492 /* CTRL-X in Insert mode */
10493 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 {
10495 /* These messages can get long, avoid a wrap in a narrow
10496 * window. Prefer showing edit_submode_extra. */
10497 length = (Rows - msg_row) * Columns - 3;
10498 if (edit_submode_extra != NULL)
10499 length -= vim_strsize(edit_submode_extra);
10500 if (length > 0)
10501 {
10502 if (edit_submode_pre != NULL)
10503 length -= vim_strsize(edit_submode_pre);
10504 if (length - vim_strsize(edit_submode) > 0)
10505 {
10506 if (edit_submode_pre != NULL)
10507 msg_puts_attr(edit_submode_pre, attr);
10508 msg_puts_attr(edit_submode, attr);
10509 }
10510 if (edit_submode_extra != NULL)
10511 {
10512 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10513 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010514 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 else
10516 sub_attr = attr;
10517 msg_puts_attr(edit_submode_extra, sub_attr);
10518 }
10519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 }
10521 else
10522#endif
10523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 if (State & VREPLACE_FLAG)
10525 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010526 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10528 else if (State & INSERT)
10529 {
10530#ifdef FEAT_RIGHTLEFT
10531 if (p_ri)
10532 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10533#endif
10534 MSG_PUTS_ATTR(_(" INSERT"), attr);
10535 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010536 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 MSG_PUTS_ATTR(_(" (insert)"), attr);
10538 else if (restart_edit == 'R')
10539 MSG_PUTS_ATTR(_(" (replace)"), attr);
10540 else if (restart_edit == 'V')
10541 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10542#ifdef FEAT_RIGHTLEFT
10543 if (p_hkmap)
10544 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10545# ifdef FEAT_FKMAP
10546 if (p_fkmap)
10547 MSG_PUTS_ATTR(farsi_text_5, attr);
10548# endif
10549#endif
10550#ifdef FEAT_KEYMAP
10551 if (State & LANGMAP)
10552 {
10553# ifdef FEAT_ARABIC
10554 if (curwin->w_p_arab)
10555 MSG_PUTS_ATTR(_(" Arabic"), attr);
10556 else
10557# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010558 if (get_keymap_str(curwin, (char_u *)" (%s)",
10559 NameBuff, MAXPATHL))
10560 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 }
10562#endif
10563 if ((State & INSERT) && p_paste)
10564 MSG_PUTS_ATTR(_(" (paste)"), attr);
10565
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 if (VIsual_active)
10567 {
10568 char *p;
10569
10570 /* Don't concatenate separate words to avoid translation
10571 * problems. */
10572 switch ((VIsual_select ? 4 : 0)
10573 + (VIsual_mode == Ctrl_V) * 2
10574 + (VIsual_mode == 'V'))
10575 {
10576 case 0: p = N_(" VISUAL"); break;
10577 case 1: p = N_(" VISUAL LINE"); break;
10578 case 2: p = N_(" VISUAL BLOCK"); break;
10579 case 4: p = N_(" SELECT"); break;
10580 case 5: p = N_(" SELECT LINE"); break;
10581 default: p = N_(" SELECT BLOCK"); break;
10582 }
10583 MSG_PUTS_ATTR(_(p), attr);
10584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 MSG_PUTS_ATTR(" --", attr);
10586 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010587
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 need_clear = TRUE;
10589 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010590 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591#ifdef FEAT_INS_EXPAND
10592 && edit_submode == NULL /* otherwise it gets too long */
10593#endif
10594 )
10595 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010596 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 need_clear = TRUE;
10598 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010599
10600 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 if (need_clear || clear_cmdline)
10602 msg_clr_eos();
10603 msg_didout = FALSE; /* overwrite this message */
10604 length = msg_col;
10605 msg_col = 0;
10606 need_wait_return = nwr_save; /* never ask for hit-return for this */
10607 }
10608 else if (clear_cmdline && msg_silent == 0)
10609 /* Clear the whole command line. Will reset "clear_cmdline". */
10610 msg_clr_cmdline();
10611
10612#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 /* In Visual mode the size of the selected area must be redrawn. */
10614 if (VIsual_active)
10615 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616
10617 /* If the last window has no status line, the ruler is after the mode
10618 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010619 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010620 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621#endif
10622 redraw_cmdline = FALSE;
10623 clear_cmdline = FALSE;
10624
10625 return length;
10626}
10627
10628/*
10629 * Position for a mode message.
10630 */
10631 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010632msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
10634 msg_col = 0;
10635 msg_row = Rows - 1;
10636}
10637
10638/*
10639 * Delete mode message. Used when ESC is typed which is expected to end
10640 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010641 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 */
10643 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010644unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645{
10646 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010647 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 */
10649 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10650 redraw_cmdline = TRUE; /* delete mode later */
10651 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010652 clearmode();
10653}
10654
10655/*
10656 * Clear the mode message.
10657 */
10658 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010659clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010660{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010661 int save_msg_row = msg_row;
10662 int save_msg_col = msg_col;
10663
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010664 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010665 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010666 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010667 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010668
10669 msg_col = save_msg_col;
10670 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671}
10672
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010673 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010674recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010675{
10676 MSG_PUTS_ATTR(_("recording"), attr);
10677 if (!shortmess(SHM_RECORDING))
10678 {
10679 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010680 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010681 MSG_PUTS_ATTR(s, attr);
10682 }
10683}
10684
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010685/*
10686 * Draw the tab pages line at the top of the Vim window.
10687 */
10688 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010689draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010690{
10691 int tabcount = 0;
10692 tabpage_T *tp;
10693 int tabwidth;
10694 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010695 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010696 int attr;
10697 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010698 win_T *cwp;
10699 int wincount;
10700 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010701 int c;
10702 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010703 int attr_sel = HL_ATTR(HLF_TPS);
10704 int attr_nosel = HL_ATTR(HLF_TP);
10705 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010706 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010707 int room;
10708 int use_sep_chars = (t_colors < 8
10709#ifdef FEAT_GUI
10710 && !gui.in_use
10711#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010712#ifdef FEAT_TERMGUICOLORS
10713 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010714#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010715 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010716
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010717 if (ScreenLines == NULL)
10718 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010719 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010720
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010721#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010722 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010723 if (gui_use_tabline())
10724 {
10725 gui_update_tabline();
10726 return;
10727 }
10728#endif
10729
10730 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010731 return;
10732
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010733#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010734
10735 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10736 for (scol = 0; scol < Columns; ++scol)
10737 TabPageIdxs[scol] = 0;
10738
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010739 /* Use the 'tabline' option if it's set. */
10740 if (*p_tal != NUL)
10741 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010742 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010743
10744 /* Check for an error. If there is one we would loop in redrawing the
10745 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010746 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010747 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010748 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010749 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010750 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010751 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010752 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010753 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010754#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010755 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010756 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010757 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010758
Bram Moolenaar238a5642006-02-21 22:12:05 +000010759 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10760 if (tabwidth < 6)
10761 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010762
Bram Moolenaar238a5642006-02-21 22:12:05 +000010763 attr = attr_nosel;
10764 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010765 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010766 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10767 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010768 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010769 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010770
Bram Moolenaar238a5642006-02-21 22:12:05 +000010771 if (tp->tp_topframe == topframe)
10772 attr = attr_sel;
10773 if (use_sep_chars && col > 0)
10774 screen_putchar('|', 0, col++, attr);
10775
10776 if (tp->tp_topframe != topframe)
10777 attr = attr_nosel;
10778
10779 screen_putchar(' ', 0, col++, attr);
10780
10781 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010782 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010783 cwp = curwin;
10784 wp = firstwin;
10785 }
10786 else
10787 {
10788 cwp = tp->tp_curwin;
10789 wp = tp->tp_firstwin;
10790 }
10791
10792 modified = FALSE;
10793 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10794 if (bufIsChanged(wp->w_buffer))
10795 modified = TRUE;
10796 if (modified || wincount > 1)
10797 {
10798 if (wincount > 1)
10799 {
10800 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010801 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010802 if (col + len >= Columns - 3)
10803 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010804 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010805#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010806 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010807#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010808 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010809#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010810 );
10811 col += len;
10812 }
10813 if (modified)
10814 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10815 screen_putchar(' ', 0, col++, attr);
10816 }
10817
10818 room = scol - col + tabwidth - 1;
10819 if (room > 0)
10820 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010821 /* Get buffer name in NameBuff[] */
10822 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010823 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010824 len = vim_strsize(NameBuff);
10825 p = NameBuff;
10826#ifdef FEAT_MBYTE
10827 if (has_mbyte)
10828 while (len > room)
10829 {
10830 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010831 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010832 }
10833 else
10834#endif
10835 if (len > room)
10836 {
10837 p += len - room;
10838 len = room;
10839 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010840 if (len > Columns - col - 1)
10841 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010842
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010843 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010844 col += len;
10845 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010846 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010847
10848 /* Store the tab page number in TabPageIdxs[], so that
10849 * jump_to_mouse() knows where each one is. */
10850 ++tabcount;
10851 while (scol < col)
10852 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010853 }
10854
Bram Moolenaar238a5642006-02-21 22:12:05 +000010855 if (use_sep_chars)
10856 c = '_';
10857 else
10858 c = ' ';
10859 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010860
10861 /* Put an "X" for closing the current tab if there are several. */
10862 if (first_tabpage->tp_next != NULL)
10863 {
10864 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10865 TabPageIdxs[Columns - 1] = -999;
10866 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010867 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010868
10869 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10870 * set. */
10871 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010872}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010873
10874/*
10875 * Get buffer name for "buf" into NameBuff[].
10876 * Takes care of special buffer names and translates special characters.
10877 */
10878 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010879get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010880{
10881 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010882 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010883 else
10884 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10885 trans_characters(NameBuff, MAXPATHL);
10886}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010887
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888/*
10889 * Get the character to use in a status line. Get its attributes in "*attr".
10890 */
10891 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010892fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893{
10894 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010895
10896#ifdef FEAT_TERMINAL
10897 if (bt_terminal(wp->w_buffer))
10898 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010899 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010900 {
10901 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010902 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010903 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010904 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010905 {
10906 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010907 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010908 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010909 }
10910 else
10911#endif
10912 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010914 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 fill = fill_stl;
10916 }
10917 else
10918 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010919 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 fill = fill_stlnc;
10921 }
10922 /* Use fill when there is highlighting, and highlighting of current
10923 * window differs, or the fillchars differ, or this is not the
10924 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010925 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010926 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 || (fill_stl != fill_stlnc)))
10928 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010929 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 return '^';
10931 return '=';
10932}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934/*
10935 * Get the character to use in a separator between vertically split windows.
10936 * Get its attributes in "*attr".
10937 */
10938 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010939fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010941 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 if (*attr == 0 && fill_vert == ' ')
10943 return '|';
10944 else
10945 return fill_vert;
10946}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947
10948/*
10949 * Return TRUE if redrawing should currently be done.
10950 */
10951 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010952redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010954#ifdef FEAT_EVAL
10955 if (disable_redraw_for_testing)
10956 return 0;
10957 else
10958#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010959 return ((!RedrawingDisabled
10960#ifdef FEAT_EVAL
10961 || ignore_redraw_flag_for_testing
10962#endif
10963 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964}
10965
10966/*
10967 * Return TRUE if printing messages should currently be done.
10968 */
10969 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010970messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971{
10972 return (!(p_lz && char_avail() && !KeyTyped));
10973}
10974
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010975#ifdef FEAT_MENU
10976/*
10977 * Draw the window toolbar.
10978 */
10979 static void
10980redraw_win_toolbar(win_T *wp)
10981{
10982 vimmenu_T *menu;
10983 int item_idx = 0;
10984 int item_count = 0;
10985 int col = 0;
10986 int next_col;
10987 int off = (int)(current_ScreenLine - ScreenLines);
10988 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10989 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10990
10991 vim_free(wp->w_winbar_items);
10992 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10993 ++item_count;
10994 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10995 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10996
10997 /* TODO: use fewer spaces if there is not enough room */
10998 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010999 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011000 {
11001 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011002 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011003 break;
11004 if (col > 1)
11005 {
11006 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011007 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011008 break;
11009 }
11010
11011 wp->w_winbar_items[item_idx].wb_startcol = col;
11012 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011013 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011014 break;
11015
11016 next_col = text_to_screenline(wp, menu->name, col);
11017 while (col < next_col)
11018 {
11019 ScreenAttrs[off + col] = button_attr;
11020 ++col;
11021 }
11022 wp->w_winbar_items[item_idx].wb_endcol = col;
11023 wp->w_winbar_items[item_idx].wb_menu = menu;
11024 ++item_idx;
11025
Bram Moolenaar02631462017-09-22 15:20:32 +020011026 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011027 break;
11028 space_to_screenline(off + col, button_attr);
11029 ++col;
11030 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011031 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011032 {
11033 space_to_screenline(off + col, fill_attr);
11034 ++col;
11035 }
11036 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11037
Bram Moolenaar02631462017-09-22 15:20:32 +020011038 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11039 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011040}
11041#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011042
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043/*
11044 * Show current status info in ruler and various other places
11045 * If always is FALSE, only show ruler if position has changed.
11046 */
11047 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011048showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049{
11050 if (!always && !redrawing())
11051 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011052#ifdef FEAT_INS_EXPAND
11053 if (pum_visible())
11054 {
11055 /* Don't redraw right now, do it later. */
11056 curwin->w_redr_status = TRUE;
11057 return;
11058 }
11059#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011060#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011061 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011062 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011063 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 else
11066#endif
11067#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011068 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069#endif
11070
11071#ifdef FEAT_TITLE
11072 if (need_maketitle
11073# ifdef FEAT_STL_OPT
11074 || (p_icon && (stl_syntax & STL_IN_ICON))
11075 || (p_title && (stl_syntax & STL_IN_TITLE))
11076# endif
11077 )
11078 maketitle();
11079#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011080 /* Redraw the tab pages line if needed. */
11081 if (redraw_tabline)
11082 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083}
11084
11085#ifdef FEAT_CMDL_INFO
11086 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011087win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011089#define RULER_BUF_LEN 70
11090 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 int row;
11092 int fillchar;
11093 int attr;
11094 int empty_line = FALSE;
11095 colnr_T virtcol;
11096 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011097 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 int this_ru_col;
11100 int off = 0;
11101 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102
11103 /* If 'ruler' off or redrawing disabled, don't do anything */
11104 if (!p_ru)
11105 return;
11106
11107 /*
11108 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11109 * after deleting lines, before cursor.lnum is corrected.
11110 */
11111 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11112 return;
11113
11114#ifdef FEAT_INS_EXPAND
11115 /* Don't draw the ruler while doing insert-completion, it might overwrite
11116 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 if (edit_submode != NULL)
11119 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011120 // Don't draw the ruler when the popup menu is visible, it may overlap.
11121 // Except when the popup menu will be redrawn anyway.
11122 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011123 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124#endif
11125
11126#ifdef FEAT_STL_OPT
11127 if (*p_ruf)
11128 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011129 int save_called_emsg = called_emsg;
11130
11131 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011133 if (called_emsg)
11134 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011135 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011136 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 return;
11138 }
11139#endif
11140
11141 /*
11142 * Check if not in Insert mode and the line is empty (will show "0-1").
11143 */
11144 if (!(State & INSERT)
11145 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11146 empty_line = TRUE;
11147
11148 /*
11149 * Only draw the ruler when something changed.
11150 */
11151 validate_virtcol_win(wp);
11152 if ( redraw_cmdline
11153 || always
11154 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11155 || wp->w_cursor.col != wp->w_ru_cursor.col
11156 || wp->w_virtcol != wp->w_ru_virtcol
11157#ifdef FEAT_VIRTUALEDIT
11158 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11159#endif
11160 || wp->w_topline != wp->w_ru_topline
11161 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11162#ifdef FEAT_DIFF
11163 || wp->w_topfill != wp->w_ru_topfill
11164#endif
11165 || empty_line != wp->w_ru_empty)
11166 {
11167 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 if (wp->w_status_height)
11169 {
11170 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011171 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011172 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011173 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 }
11175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 {
11177 row = Rows - 1;
11178 fillchar = ' ';
11179 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 width = Columns;
11181 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 }
11183
11184 /* In list mode virtcol needs to be recomputed */
11185 virtcol = wp->w_virtcol;
11186 if (wp->w_p_list && lcs_tab1 == NUL)
11187 {
11188 wp->w_p_list = FALSE;
11189 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11190 wp->w_p_list = TRUE;
11191 }
11192
11193 /*
11194 * Some sprintfs return the length, some return a pointer.
11195 * To avoid portability problems we use strlen() here.
11196 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011197 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11199 ? 0L
11200 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011201 len = STRLEN(buffer);
11202 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11204 (int)virtcol + 1);
11205
11206 /*
11207 * Add a "50%" if there is room for it.
11208 * On the last line, don't print in the last column (scrolls the
11209 * screen up on some terminals).
11210 */
11211 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011212 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 this_ru_col = ru_col - (Columns - width);
11217 if (this_ru_col < 0)
11218 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 /* Never use more than half the window/screen width, leave the other
11220 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011221 if (this_ru_col < (width + 1) / 2)
11222 this_ru_col = (width + 1) / 2;
11223 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011225 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011226 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 {
11228#ifdef FEAT_MBYTE
11229 if (has_mbyte)
11230 i += (*mb_char2bytes)(fillchar, buffer + i);
11231 else
11232#endif
11233 buffer[i++] = fillchar;
11234 ++o;
11235 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011236 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238 /* Truncate at window boundary. */
11239#ifdef FEAT_MBYTE
11240 if (has_mbyte)
11241 {
11242 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011243 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 {
11245 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011246 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 {
11248 buffer[i] = NUL;
11249 break;
11250 }
11251 }
11252 }
11253 else
11254#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011255 if (this_ru_col + (int)STRLEN(buffer) > width)
11256 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257
Bram Moolenaar4033c552017-09-16 20:54:51 +020011258 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 i = redraw_cmdline;
11260 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011261 this_ru_col + off + (int)STRLEN(buffer),
11262 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 fillchar, fillchar, attr);
11264 /* don't redraw the cmdline because of showing the ruler */
11265 redraw_cmdline = i;
11266 wp->w_ru_cursor = wp->w_cursor;
11267 wp->w_ru_virtcol = wp->w_virtcol;
11268 wp->w_ru_empty = empty_line;
11269 wp->w_ru_topline = wp->w_topline;
11270 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11271#ifdef FEAT_DIFF
11272 wp->w_ru_topfill = wp->w_topfill;
11273#endif
11274 }
11275}
11276#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011277
11278#if defined(FEAT_LINEBREAK) || defined(PROTO)
11279/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011280 * Return the width of the 'number' and 'relativenumber' column.
11281 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011282 * Otherwise it depends on 'numberwidth' and the line count.
11283 */
11284 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011285number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011286{
11287 int n;
11288 linenr_T lnum;
11289
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011290 if (wp->w_p_rnu && !wp->w_p_nu)
11291 /* cursor line shows "0" */
11292 lnum = wp->w_height;
11293 else
11294 /* cursor line shows absolute line number */
11295 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011296
Bram Moolenaar6b314672015-03-20 15:42:10 +010011297 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011298 return wp->w_nrwidth_width;
11299 wp->w_nrwidth_line_count = lnum;
11300
11301 n = 0;
11302 do
11303 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011304 lnum /= 10;
11305 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011306 } while (lnum > 0);
11307
11308 /* 'numberwidth' gives the minimal width plus one */
11309 if (n < wp->w_p_nuw - 1)
11310 n = wp->w_p_nuw - 1;
11311
11312 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011313 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011314 return n;
11315}
11316#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011317
11318/*
11319 * Return the current cursor column. This is the actual position on the
11320 * screen. First column is 0.
11321 */
11322 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011323screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011324{
11325 return screen_cur_col;
11326}
11327
11328/*
11329 * Return the current cursor row. This is the actual position on the screen.
11330 * First row is 0.
11331 */
11332 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011333screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011334{
11335 return screen_cur_row;
11336}