blob: adc62e10e7198a910fc0119f0afa70c3f736b144 [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
Bram Moolenaar637532b2019-01-03 21:44:40 +01004164 if (draw_state == WL_LINE && (area_highlighting
4165#ifdef FEAT_SPELL
4166 || has_spell
4167#endif
4168 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
4170 /* handle Visual or match highlighting in this line */
4171 if (vcol == fromcol
4172#ifdef FEAT_MBYTE
4173 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4174 && (*mb_ptr2cells)(ptr) > 1)
4175#endif
4176 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004177 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 && vcol < tocol))
4179 area_attr = attr; /* start highlighting */
4180 else if (area_attr != 0
4181 && (vcol == tocol
4182 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
4185#ifdef FEAT_SEARCH_EXTRA
4186 if (!n_extra)
4187 {
4188 /*
4189 * Check for start/end of search pattern match.
4190 * After end, check for start/end of next match.
4191 * When another match, have to check for start again.
4192 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004193 * Do this for 'search_hl' and the match list (ordered by
4194 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004196 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004197 cur = wp->w_match_head;
4198 shl_flag = FALSE;
4199 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004201 if (shl_flag == FALSE
4202 && ((cur != NULL
4203 && cur->priority > SEARCH_HL_PRIORITY)
4204 || cur == NULL))
4205 {
4206 shl = &search_hl;
4207 shl_flag = TRUE;
4208 }
4209 else
4210 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004211 if (cur != NULL)
4212 cur->pos.cur = 0;
4213 pos_inprogress = TRUE;
4214 while (shl->rm.regprog != NULL
4215 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004217 if (shl->startcol != MAXCOL
4218 && v >= (long)shl->startcol
4219 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004221#ifdef FEAT_MBYTE
4222 int tmp_col = v + MB_PTR2LEN(ptr);
4223
4224 if (shl->endcol < tmp_col)
4225 shl->endcol = tmp_col;
4226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004228#ifdef FEAT_CONCEAL
4229 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4230 == cur->hlg_id)
4231 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004232 has_match_conc =
4233 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004234 match_conc = cur->conceal_char;
4235 }
4236 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004237 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004240 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
4242 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004243 next_search_hl(wp, shl, lnum, (colnr_T)v,
4244 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004245 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004246 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 /* Need to get the line again, a multi-line regexp
4249 * may have made it invalid. */
4250 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4251 ptr = line + v;
4252
4253 if (shl->lnum == lnum)
4254 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004255 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004257 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004259 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004261 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
4263 /* highlight empty match, try again after
4264 * it */
4265#ifdef FEAT_MBYTE
4266 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004267 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004268 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 else
4270#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004271 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273
4274 /* Loop to check if the match starts at the
4275 * current position */
4276 continue;
4277 }
4278 }
4279 break;
4280 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004281 if (shl != &search_hl && cur != NULL)
4282 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004284
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004285 /* Use attributes from match with highest priority among
4286 * 'search_hl' and the match list. */
4287 search_attr = search_hl.attr_cur;
4288 cur = wp->w_match_head;
4289 shl_flag = FALSE;
4290 while (cur != NULL || shl_flag == FALSE)
4291 {
4292 if (shl_flag == FALSE
4293 && ((cur != NULL
4294 && cur->priority > SEARCH_HL_PRIORITY)
4295 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004296 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004297 shl = &search_hl;
4298 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004299 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004300 else
4301 shl = &cur->hl;
4302 if (shl->attr_cur != 0)
4303 search_attr = shl->attr_cur;
4304 if (shl != &search_hl && cur != NULL)
4305 cur = cur->next;
4306 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004307 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004308 if (*ptr == NUL && (did_line_attr >= 1
4309 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004310 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312#endif
4313
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004315 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004317 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4318 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004320 if (diff_hlf == HLF_TXD && ptr - line > change_end
4321 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004323 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004324 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004325 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004328
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004329#ifdef FEAT_TEXT_PROP
4330 if (text_props != NULL)
4331 {
4332 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004333 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004334
4335 // Check if any active property ends.
4336 for (pi = 0; pi < text_props_active; ++pi)
4337 {
4338 int tpi = text_prop_idxs[pi];
4339
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004340 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004341 + text_props[tpi].tp_len)
4342 {
4343 if (pi + 1 < text_props_active)
4344 mch_memmove(text_prop_idxs + pi,
4345 text_prop_idxs + pi + 1,
4346 sizeof(int)
4347 * (text_props_active - (pi + 1)));
4348 --text_props_active;
4349 --pi;
4350 }
4351 }
4352
4353 // Add any text property that starts in this column.
4354 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004355 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004356 text_prop_idxs[text_props_active++] = text_prop_next++;
4357
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004358 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004359 if (text_props_active > 0)
4360 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004361 // Sort the properties on priority and/or starting last.
4362 // Then combine the attributes, highest priority last.
4363 current_text_props = text_props;
4364 current_buf = wp->w_buffer;
4365 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4366 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004367
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004368 for (pi = 0; pi < text_props_active; ++pi)
4369 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004370 int tpi = text_prop_idxs[pi];
4371 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004372
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004373 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004374 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004375 int pt_attr = syn_id2attr(pt->pt_hl_id);
4376
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004377 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004378 if (text_prop_attr == 0)
4379 text_prop_attr = pt_attr;
4380 else
4381 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004382 }
4383 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004384 }
4385 }
4386#endif
4387
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004388 /* Decide which of the highlight attributes to use. */
4389 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004390#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004391 if (area_attr != 0)
4392 char_attr = hl_combine_attr(line_attr, area_attr);
4393 else if (search_attr != 0)
4394 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004395 /* Use line_attr when not in the Visual or 'incsearch' area
4396 * (area_attr may be 0 when "noinvcur" is set). */
4397 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004398 || vcol < fromcol || vcol_prev < fromcol_prev
4399 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004400 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004401#else
4402 if (area_attr != 0)
4403 char_attr = area_attr;
4404 else if (search_attr != 0)
4405 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004406#endif
4407 else
4408 {
4409 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004410#ifdef FEAT_TEXT_PROP
4411 if (text_prop_type != NULL)
4412 char_attr = text_prop_attr;
4413 else
4414#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004415#ifdef FEAT_SYN_HL
4416 if (has_syntax)
4417 char_attr = syntax_attr;
4418 else
4419#endif
4420 char_attr = 0;
4421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
4423
4424 /*
4425 * Get the next character to put on the screen.
4426 */
4427 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004428 * The "p_extra" points to the extra stuff that is inserted to
4429 * represent special characters (non-printable stuff) and other
4430 * things. When all characters are the same, c_extra is used.
4431 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4432 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4434 */
4435 if (n_extra > 0)
4436 {
4437 if (c_extra != NUL)
4438 {
4439 c = c_extra;
4440#ifdef FEAT_MBYTE
4441 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004442 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
4444 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004445 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004446 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 else
4449 mb_utf8 = FALSE;
4450#endif
4451 }
4452 else
4453 {
4454 c = *p_extra;
4455#ifdef FEAT_MBYTE
4456 if (has_mbyte)
4457 {
4458 mb_c = c;
4459 if (enc_utf8)
4460 {
4461 /* If the UTF-8 character is more than one byte:
4462 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004463 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 mb_utf8 = FALSE;
4465 if (mb_l > n_extra)
4466 mb_l = 1;
4467 else if (mb_l > 1)
4468 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004469 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004471 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 }
4474 else
4475 {
4476 /* if this is a DBCS character, put it in "mb_c" */
4477 mb_l = MB_BYTE2LEN(c);
4478 if (mb_l >= n_extra)
4479 mb_l = 1;
4480 else if (mb_l > 1)
4481 mb_c = (c << 8) + p_extra[1];
4482 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004483 if (mb_l == 0) /* at the NUL at end-of-line */
4484 mb_l = 1;
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 /* If a double-width char doesn't fit display a '>' in the
4487 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004488 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489# ifdef FEAT_RIGHTLEFT
4490 wp->w_p_rl ? (col <= 0) :
4491# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004492 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 && (*mb_char2cells)(mb_c) == 2)
4494 {
4495 c = '>';
4496 mb_c = c;
4497 mb_l = 1;
4498 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004499 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 /* put the pointer back to output the double-width
4501 * character at the start of the next line. */
4502 ++n_extra;
4503 --p_extra;
4504 }
4505 else
4506 {
4507 n_extra -= mb_l - 1;
4508 p_extra += mb_l - 1;
4509 }
4510 }
4511#endif
4512 ++p_extra;
4513 }
4514 --n_extra;
4515 }
4516 else
4517 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004518#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004519 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004520#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004521
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004522 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004523 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /*
4525 * Get a character from the line itself.
4526 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004527 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004528#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004529 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531#ifdef FEAT_MBYTE
4532 if (has_mbyte)
4533 {
4534 mb_c = c;
4535 if (enc_utf8)
4536 {
4537 /* If the UTF-8 character is more than one byte: Decode it
4538 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004539 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 mb_utf8 = FALSE;
4541 if (mb_l > 1)
4542 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004543 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 /* Overlong encoded ASCII or ASCII with composing char
4545 * is displayed normally, except a NUL. */
4546 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004547 {
4548 c = mb_c;
4549# ifdef FEAT_LINEBREAK
4550 c0 = mb_c;
4551# endif
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004554
4555 /* At start of the line we can have a composing char.
4556 * Draw it as a space with a composing char. */
4557 if (utf_iscomposing(mb_c))
4558 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004559 int i;
4560
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004561 for (i = Screen_mco - 1; i > 0; --i)
4562 u8cc[i] = u8cc[i - 1];
4563 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004564 mb_c = ' ';
4565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567
4568 if ((mb_l == 1 && c >= 0x80)
4569 || (mb_l >= 1 && mb_c == 0)
4570 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004571# ifdef UNICODE16
4572 || mb_c >= 0x10000
4573# endif
4574 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
4576 /*
4577 * Illegal UTF-8 byte: display as <xx>.
4578 * Non-BMP character : display as ? or fullwidth ?.
4579 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004580# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
4584 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004585# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (wp->w_p_rl) /* reverse */
4587 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004590# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 else if (utf_char2cells(mb_c) != 2)
4592 STRCPY(extra, "?");
4593 else
4594 /* 0xff1f in UTF-8: full-width '?' */
4595 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 p_extra = extra;
4599 c = *p_extra;
4600 mb_c = mb_ptr2char_adv(&p_extra);
4601 mb_utf8 = (c >= 0x80);
4602 n_extra = (int)STRLEN(p_extra);
4603 c_extra = NUL;
4604 if (area_attr == 0 && search_attr == 0)
4605 {
4606 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004607 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 saved_attr2 = char_attr; /* save current attr */
4609 }
4610 }
4611 else if (mb_l == 0) /* at the NUL at end-of-line */
4612 mb_l = 1;
4613#ifdef FEAT_ARABIC
4614 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4615 {
4616 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004617 int pc, pc1, nc;
4618 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
4620 /* The idea of what is the previous and next
4621 * character depends on 'rightleft'. */
4622 if (wp->w_p_rl)
4623 {
4624 pc = prev_c;
4625 pc1 = prev_c1;
4626 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004627 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 else
4630 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004631 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004633 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635 prev_c = mb_c;
4636
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004637 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 else
4640 prev_c = mb_c;
4641#endif
4642 }
4643 else /* enc_dbcs */
4644 {
4645 mb_l = MB_BYTE2LEN(c);
4646 if (mb_l == 0) /* at the NUL at end-of-line */
4647 mb_l = 1;
4648 else if (mb_l > 1)
4649 {
4650 /* We assume a second byte below 32 is illegal.
4651 * Hopefully this is OK for all double-byte encodings!
4652 */
4653 if (ptr[1] >= 32)
4654 mb_c = (c << 8) + ptr[1];
4655 else
4656 {
4657 if (ptr[1] == NUL)
4658 {
4659 /* head byte at end of line */
4660 mb_l = 1;
4661 transchar_nonprint(extra, c);
4662 }
4663 else
4664 {
4665 /* illegal tail byte */
4666 mb_l = 2;
4667 STRCPY(extra, "XX");
4668 }
4669 p_extra = extra;
4670 n_extra = (int)STRLEN(extra) - 1;
4671 c_extra = NUL;
4672 c = *p_extra++;
4673 if (area_attr == 0 && search_attr == 0)
4674 {
4675 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004676 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 saved_attr2 = char_attr; /* save current attr */
4678 }
4679 mb_c = c;
4680 }
4681 }
4682 }
4683 /* If a double-width char doesn't fit display a '>' in the
4684 * last column; the character is displayed at the start of the
4685 * next line. */
4686 if ((
4687# ifdef FEAT_RIGHTLEFT
4688 wp->w_p_rl ? (col <= 0) :
4689# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004690 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 && (*mb_char2cells)(mb_c) == 2)
4692 {
4693 c = '>';
4694 mb_c = c;
4695 mb_utf8 = FALSE;
4696 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004697 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 /* Put pointer back so that the character will be
4699 * displayed at the start of the next line. */
4700 --ptr;
4701 }
4702 else if (*ptr != NUL)
4703 ptr += mb_l - 1;
4704
4705 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004706 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004707 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004708 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004711 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 c = ' ';
4713 if (area_attr == 0 && search_attr == 0)
4714 {
4715 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004716 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 saved_attr2 = char_attr; /* save current attr */
4718 }
4719 mb_c = c;
4720 mb_utf8 = FALSE;
4721 mb_l = 1;
4722 }
4723
4724 }
4725#endif
4726 ++ptr;
4727
4728 if (extra_check)
4729 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004730#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004731 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004732#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004733
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004734#ifdef FEAT_TERMINAL
4735 if (get_term_attr)
4736 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004737 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004738
4739 if (!attr_pri)
4740 char_attr = syntax_attr;
4741 else
4742 char_attr = hl_combine_attr(syntax_attr, char_attr);
4743 }
4744#endif
4745
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004746#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004747 // Get syntax attribute, unless still at the start of the line
4748 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004749 v = (long)(ptr - line);
4750 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 /* Get the syntax attribute for the character. If there
4753 * is an error, disable syntax highlighting. */
4754 save_did_emsg = did_emsg;
4755 did_emsg = FALSE;
4756
Bram Moolenaar217ad922005-03-20 22:37:15 +00004757 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004758# ifdef FEAT_SPELL
4759 has_spell ? &can_spell :
4760# endif
4761 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004764 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004765 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004766 has_syntax = FALSE;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 else
4769 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004770#ifdef SYN_TIME_LIMIT
4771 if (wp->w_s->b_syn_slow)
4772 has_syntax = FALSE;
4773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
4775 /* Need to get the line again, a multi-line regexp may
4776 * have made it invalid. */
4777 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4778 ptr = line + v;
4779
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004780# ifdef FEAT_TEXT_PROP
4781 // Text properties overrule syntax highlighting.
4782 if (text_prop_attr == 0)
4783#endif
4784 {
4785 if (!attr_pri)
4786 char_attr = syntax_attr;
4787 else
4788 char_attr = hl_combine_attr(syntax_attr, char_attr);
4789 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004790# ifdef FEAT_CONCEAL
4791 /* no concealing past the end of the line, it interferes
4792 * with line highlighting */
4793 if (c == NUL)
4794 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004795 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004796 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004799#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004800
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004801#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004802 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004803 * Only do this when there is no syntax highlighting, the
4804 * @Spell cluster is not used or the current syntax item
4805 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004806 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004807 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004808 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004809 if (c != 0 && (
4810# ifdef FEAT_SYN_HL
4811 !has_syntax ||
4812# endif
4813 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004814 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004815 char_u *prev_ptr, *p;
4816 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004817 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004818# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004819 if (has_mbyte)
4820 {
4821 prev_ptr = ptr - mb_l;
4822 v -= mb_l - 1;
4823 }
4824 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004825# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004826 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004827
4828 /* Use nextline[] if possible, it has the start of the
4829 * next line concatenated. */
4830 if ((prev_ptr - line) - nextlinecol >= 0)
4831 p = nextline + (prev_ptr - line) - nextlinecol;
4832 else
4833 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004834 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004835 len = spell_check(wp, p, &spell_hlf, &cap_col,
4836 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004837 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004838
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004839 /* In Insert mode only highlight a word that
4840 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004841 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004842 && (State & INSERT) != 0
4843 && wp->w_cursor.lnum == lnum
4844 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004845 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004846 && wp->w_cursor.col < (colnr_T)word_end)
4847 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004848 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004849 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004850 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004851
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004852 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004853 && (p - nextline) + len > nextline_idx)
4854 {
4855 /* Remember that the good word continues at the
4856 * start of the next line. */
4857 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004858 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004859 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004860
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004861 /* Turn index into actual attributes. */
4862 if (spell_hlf != HLF_COUNT)
4863 spell_attr = highlight_attr[spell_hlf];
4864
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004865 if (cap_col > 0)
4866 {
4867 if (p != prev_ptr
4868 && (p - nextline) + cap_col >= nextline_idx)
4869 {
4870 /* Remember that the word in the next line
4871 * must start with a capital. */
4872 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004873 cap_col = (int)((p - nextline) + cap_col
4874 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004875 }
4876 else
4877 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004878 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004879 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004880 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004881 }
4882 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004883 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004884 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004885 char_attr = hl_combine_attr(char_attr, spell_attr);
4886 else
4887 char_attr = hl_combine_attr(spell_attr, char_attr);
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889#endif
4890#ifdef FEAT_LINEBREAK
4891 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004892 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004894 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004895 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004897# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004898 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004899# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004900 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004902 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004904 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004905
Bram Moolenaar597a4222014-06-25 14:39:50 +02004906 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004907 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004908 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004909 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004910# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004911 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004912 wp->w_buffer->b_p_vts_array) - 1;
4913# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004914 n_extra = (int)wp->w_buffer->b_p_ts
4915 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004916# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004917
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004918# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004919 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004920# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004922# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004923 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004924 {
4925#ifdef FEAT_CONCEAL
4926 if (c == TAB)
4927 /* See "Tab alignment" below. */
4928 FIX_FOR_BOGUSCOLS;
4929#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004930 if (!wp->w_p_list)
4931 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934#endif
4935
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004936 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4937 */
4938 if (wp->w_p_list
4939 && (((c == 160
4940#ifdef FEAT_MBYTE
4941 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4942#endif
4943 ) && lcs_nbsp)
4944 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4945 {
4946 c = (c == ' ') ? lcs_space : lcs_nbsp;
4947 if (area_attr == 0 && search_attr == 0)
4948 {
4949 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004950 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004951 saved_attr2 = char_attr; /* save current attr */
4952 }
4953#ifdef FEAT_MBYTE
4954 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004955 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004956 {
4957 mb_utf8 = TRUE;
4958 u8cc[0] = 0;
4959 c = 0xc0;
4960 }
4961 else
4962 mb_utf8 = FALSE;
4963#endif
4964 }
4965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4967 {
4968 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004969 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004972 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 saved_attr2 = char_attr; /* save current attr */
4974 }
4975#ifdef FEAT_MBYTE
4976 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004977 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004980 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004981 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983 else
4984 mb_utf8 = FALSE;
4985#endif
4986 }
4987 }
4988
4989 /*
4990 * Handling of non-printable characters.
4991 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004992 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
4994 /*
4995 * when getting a character from the file, we may have to
4996 * turn it into something else on the way to putting it
4997 * into "ScreenLines".
4998 */
4999 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5000 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005001 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005002 long vcol_adjusted = vcol; /* removed showbreak length */
5003#ifdef FEAT_LINEBREAK
5004 /* only adjust the tab_len, when at the first column
5005 * after the showbreak value was drawn */
5006 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5007 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005010#ifdef FEAT_VARTABS
5011 tab_len = tabstop_padding(vcol_adjusted,
5012 wp->w_buffer->b_p_ts,
5013 wp->w_buffer->b_p_vts_array) - 1;
5014#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005015 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005016 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5017#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005018
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005019#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005020 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005021#endif
5022 /* tab amount depends on current column */
5023 n_extra = tab_len;
5024#ifdef FEAT_LINEBREAK
5025 else
5026 {
5027 char_u *p;
5028 int len = n_extra;
5029 int i;
5030 int saved_nextra = n_extra;
5031
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005032#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005033 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005034 /* there are characters to conceal */
5035 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005036 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5037 */
5038 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5039 && n_extra > tab_len)
5040 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005041#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005042
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005043 /* if n_extra > 0, it gives the number of chars, to
5044 * use for a tab, else we need to calculate the width
5045 * for a tab */
5046#ifdef FEAT_MBYTE
5047 len = (tab_len * mb_char2len(lcs_tab2));
5048 if (n_extra > 0)
5049 len += n_extra - tab_len;
5050#endif
5051 c = lcs_tab1;
5052 p = alloc((unsigned)(len + 1));
5053 vim_memset(p, ' ', len);
5054 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005055 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005056 p_extra_free = p;
5057 for (i = 0; i < tab_len; i++)
5058 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005059 if (*p == NUL)
5060 {
5061 tab_len = i;
5062 break;
5063 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005064#ifdef FEAT_MBYTE
5065 mb_char2bytes(lcs_tab2, p);
5066 p += mb_char2len(lcs_tab2);
5067 n_extra += mb_char2len(lcs_tab2)
5068 - (saved_nextra > 0 ? 1 : 0);
5069#else
5070 p[i] = lcs_tab2;
5071#endif
5072 }
5073 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005074#ifdef FEAT_CONCEAL
5075 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5076 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005077 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005078 n_extra -= vcol_off;
5079#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005080 }
5081#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005082#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005083 {
5084 int vc_saved = vcol_off;
5085
5086 /* Tab alignment should be identical regardless of
5087 * 'conceallevel' value. So tab compensates of all
5088 * previous concealed characters, and thus resets
5089 * vcol_off and boguscols accumulated so far in the
5090 * line. Note that the tab can be longer than
5091 * 'tabstop' when there are concealed characters. */
5092 FIX_FOR_BOGUSCOLS;
5093
5094 /* Make sure, the highlighting for the tab char will be
5095 * correctly set further below (effectively reverts the
5096 * FIX_FOR_BOGSUCOLS macro */
5097 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005098 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005099 tab_len += vc_saved;
5100 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102#ifdef FEAT_MBYTE
5103 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5104#endif
5105 if (wp->w_p_list)
5106 {
5107 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005108#ifdef FEAT_LINEBREAK
5109 if (wp->w_p_lbr)
5110 c_extra = NUL; /* using p_extra from above */
5111 else
5112#endif
5113 c_extra = lcs_tab2;
5114 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005115 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 saved_attr2 = char_attr; /* save current attr */
5117#ifdef FEAT_MBYTE
5118 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005119 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
5121 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005122 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005123 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125#endif
5126 }
5127 else
5128 {
5129 c_extra = ' ';
5130 c = ' ';
5131 }
5132 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005133 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005134 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005135 || ((fromcol >= 0 || fromcol_prev >= 0)
5136 && tocol > vcol
5137 && VIsual_mode != Ctrl_V
5138 && (
5139# ifdef FEAT_RIGHTLEFT
5140 wp->w_p_rl ? (col >= 0) :
5141# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005142 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005143 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005144 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005145 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005146 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005148 /* Display a '$' after the line or highlight an extra
5149 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5151 /* For a diff line the highlighting continues after the
5152 * "$". */
5153 if (
5154# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005155 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156# ifdef LINE_ATTR
5157 &&
5158# endif
5159# endif
5160# ifdef LINE_ATTR
5161 line_attr == 0
5162# endif
5163 )
5164#endif
5165 {
5166#ifdef FEAT_VIRTUALEDIT
5167 /* In virtualedit, visual selections may extend
5168 * beyond end of line. */
5169 if (area_highlighting && virtual_active()
5170 && tocol != MAXCOL && vcol < tocol)
5171 n_extra = 0;
5172 else
5173#endif
5174 {
5175 p_extra = at_end_str;
5176 n_extra = 1;
5177 c_extra = NUL;
5178 }
5179 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005180 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005181 c = lcs_eol;
5182 else
5183 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 lcs_eol_one = -1;
5185 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005186 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005188 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 n_attr = 1;
5190 }
5191#ifdef FEAT_MBYTE
5192 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005193 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005196 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005197 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199 else
5200 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5201#endif
5202 }
5203 else if (c != NUL)
5204 {
5205 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005206 if (n_extra == 0)
5207 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#ifdef FEAT_RIGHTLEFT
5209 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5210 rl_mirror(p_extra); /* reverse "<12>" */
5211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005213#ifdef FEAT_LINEBREAK
5214 if (wp->w_p_lbr)
5215 {
5216 char_u *p;
5217
5218 c = *p_extra;
5219 p = alloc((unsigned)n_extra + 1);
5220 vim_memset(p, ' ', n_extra);
5221 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5222 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005223 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005224 p_extra_free = p_extra = p;
5225 }
5226 else
5227#endif
5228 {
5229 n_extra = byte2cells(c) - 1;
5230 c = *p_extra++;
5231 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005232 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
5234 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005235 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 saved_attr2 = char_attr; /* save current attr */
5237 }
5238#ifdef FEAT_MBYTE
5239 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5240#endif
5241 }
5242#ifdef FEAT_VIRTUALEDIT
5243 else if (VIsual_active
5244 && (VIsual_mode == Ctrl_V
5245 || VIsual_mode == 'v')
5246 && virtual_active()
5247 && tocol != MAXCOL
5248 && vcol < tocol
5249 && (
5250# ifdef FEAT_RIGHTLEFT
5251 wp->w_p_rl ? (col >= 0) :
5252# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005253 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
5255 c = ' ';
5256 --ptr; /* put it back at the NUL */
5257 }
5258#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005259#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 else if ((
5261# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005262 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005264# ifdef FEAT_TERMINAL
5265 term_attr != 0 ||
5266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 ) && (
5269# ifdef FEAT_RIGHTLEFT
5270 wp->w_p_rl ? (col >= 0) :
5271# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005272 (col
5273# ifdef FEAT_CONCEAL
5274 - boguscols
5275# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005276 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 /* Highlight until the right side of the window */
5279 c = ' ';
5280 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005281
5282 /* Remember we do the char for line highlighting. */
5283 ++did_line_attr;
5284
5285 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005286 if (line_attr != 0 && char_attr == search_attr
5287 && (did_line_attr > 1
5288 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005289 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290# ifdef FEAT_DIFF
5291 if (diff_hlf == HLF_TXD)
5292 {
5293 diff_hlf = HLF_CHD;
5294 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005295 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005296 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005297 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5298 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005299 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005303# ifdef FEAT_TERMINAL
5304 if (term_attr != 0)
5305 {
5306 char_attr = term_attr;
5307 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5308 char_attr = hl_combine_attr(char_attr,
5309 HL_ATTR(HLF_CUL));
5310 }
5311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
5313#endif
5314 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005315
5316#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005317 if ( wp->w_p_cole > 0
5318 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005319 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005320 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005321 && !(lnum_in_visual_area
5322 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005323 {
5324 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005325 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005326 && (syn_get_sub_char() != NUL || match_conc
5327 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005328 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005329 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005330 /* First time at this concealed item: display one
5331 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005332 if (match_conc)
5333 c = match_conc;
5334 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005335 c = syn_get_sub_char();
5336 else if (lcs_conceal != NUL)
5337 c = lcs_conceal;
5338 else
5339 c = ' ';
5340
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005341 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005342
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005343 if (n_extra > 0)
5344 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005345 vcol += n_extra;
5346 if (wp->w_p_wrap && n_extra > 0)
5347 {
5348# ifdef FEAT_RIGHTLEFT
5349 if (wp->w_p_rl)
5350 {
5351 col -= n_extra;
5352 boguscols -= n_extra;
5353 }
5354 else
5355# endif
5356 {
5357 boguscols += n_extra;
5358 col += n_extra;
5359 }
5360 }
5361 n_extra = 0;
5362 n_attr = 0;
5363 }
5364 else if (n_skip == 0)
5365 {
5366 is_concealing = TRUE;
5367 n_skip = 1;
5368 }
5369# ifdef FEAT_MBYTE
5370 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005371 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005372 {
5373 mb_utf8 = TRUE;
5374 u8cc[0] = 0;
5375 c = 0xc0;
5376 }
5377 else
5378 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5379# endif
5380 }
5381 else
5382 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005383 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005384 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005385 }
5386#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005389#ifdef FEAT_CONCEAL
5390 /* In the cursor line and we may be concealing characters: correct
5391 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005392 if (!did_wcol && draw_state == WL_LINE
5393 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005394 && conceal_cursor_line(wp)
5395 && (int)wp->w_virtcol <= vcol + n_skip)
5396 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005397# ifdef FEAT_RIGHTLEFT
5398 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005399 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005400 else
5401# endif
5402 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005403 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005404 did_wcol = TRUE;
5405 }
5406#endif
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 /* Don't override visual selection highlighting. */
5409 if (n_attr > 0
5410 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005411 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 char_attr = extra_attr;
5413
Bram Moolenaar81695252004-12-29 20:58:21 +00005414#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 /* XIM don't send preedit_start and preedit_end, but they send
5416 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5417 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005418 if (p_imst == IM_ON_THE_SPOT
5419 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005420 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 && (State & INSERT)
5422 && !p_imdisable
5423 && im_is_preediting()
5424 && draw_state == WL_LINE)
5425 {
5426 colnr_T tcol;
5427
5428 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005429 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 else
5431 tcol = preedit_end_col;
5432 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5433 {
5434 if (feedback_old_attr < 0)
5435 {
5436 feedback_col = 0;
5437 feedback_old_attr = char_attr;
5438 }
5439 char_attr = im_get_feedback_attr(feedback_col);
5440 if (char_attr < 0)
5441 char_attr = feedback_old_attr;
5442 feedback_col++;
5443 }
5444 else if (feedback_old_attr >= 0)
5445 {
5446 char_attr = feedback_old_attr;
5447 feedback_old_attr = -1;
5448 feedback_col = 0;
5449 }
5450 }
5451#endif
5452 /*
5453 * Handle the case where we are in column 0 but not on the first
5454 * character of the line and the user wants us to show us a
5455 * special character (via 'listchars' option "precedes:<char>".
5456 */
5457 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005458 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5460#ifdef FEAT_DIFF
5461 && filler_todo <= 0
5462#endif
5463 && draw_state > WL_NR
5464 && c != NUL)
5465 {
5466 c = lcs_prec;
5467 lcs_prec_todo = NUL;
5468#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005469 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5470 {
5471 /* Double-width character being overwritten by the "precedes"
5472 * character, need to fill up half the character. */
5473 c_extra = MB_FILLER_CHAR;
5474 n_extra = 1;
5475 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005476 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005479 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {
5481 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005482 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005483 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485 else
5486 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5487#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005488 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 {
5490 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005491 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 n_attr3 = 1;
5493 }
5494 }
5495
5496 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005497 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005499 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005500#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005501 || did_line_attr == 1
5502#endif
5503 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005505#ifdef FEAT_SEARCH_EXTRA
5506 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005507
5508 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005509 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005510 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005511#endif
5512
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005513 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 * highlight match at end of line. If it's beyond the last
5515 * char on the screen, just overwrite that one (tricky!) Not
5516 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005517#ifdef FEAT_SEARCH_EXTRA
5518 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005519 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005520 prevcol_hl_flag = TRUE;
5521 else
5522 {
5523 cur = wp->w_match_head;
5524 while (cur != NULL)
5525 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005526 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005527 {
5528 prevcol_hl_flag = TRUE;
5529 break;
5530 }
5531 cur = cur->next;
5532 }
5533 }
5534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005536 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005537 && (VIsual_mode != Ctrl_V
5538 || lnum == VIsual.lnum
5539 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005540 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#ifdef FEAT_SEARCH_EXTRA
5542 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005543 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005544# ifdef FEAT_SYN_HL
5545 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5546 && !(wp == curwin && VIsual_active))
5547# endif
5548# ifdef FEAT_DIFF
5549 && diff_hlf == (hlf_T)0
5550# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005551# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005552 && did_line_attr <= 1
5553# endif
5554 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555#endif
5556 ))
5557 {
5558 int n = 0;
5559
5560#ifdef FEAT_RIGHTLEFT
5561 if (wp->w_p_rl)
5562 {
5563 if (col < 0)
5564 n = 1;
5565 }
5566 else
5567#endif
5568 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005569 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 n = -1;
5571 }
5572 if (n != 0)
5573 {
5574 /* At the window boundary, highlight the last character
5575 * instead (better than nothing). */
5576 off += n;
5577 col += n;
5578 }
5579 else
5580 {
5581 /* Add a blank character to highlight. */
5582 ScreenLines[off] = ' ';
5583#ifdef FEAT_MBYTE
5584 if (enc_utf8)
5585 ScreenLinesUC[off] = 0;
5586#endif
5587 }
5588#ifdef FEAT_SEARCH_EXTRA
5589 if (area_attr == 0)
5590 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005591 /* Use attributes from match with highest priority among
5592 * 'search_hl' and the match list. */
5593 char_attr = search_hl.attr;
5594 cur = wp->w_match_head;
5595 shl_flag = FALSE;
5596 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005597 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005598 if (shl_flag == FALSE
5599 && ((cur != NULL
5600 && cur->priority > SEARCH_HL_PRIORITY)
5601 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005602 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005603 shl = &search_hl;
5604 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005605 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005606 else
5607 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005608 if ((ptr - line) - 1 == (long)shl->startcol
5609 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005610 char_attr = shl->attr;
5611 if (shl != &search_hl && cur != NULL)
5612 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 }
5615#endif
5616 ScreenAttrs[off] = char_attr;
5617#ifdef FEAT_RIGHTLEFT
5618 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005621 --off;
5622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 else
5624#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005627 ++off;
5628 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005629 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005630#ifdef FEAT_SYN_HL
5631 eol_hl_off = 1;
5632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaar91170f82006-05-05 21:15:17 +00005636 /*
5637 * At end of the text line.
5638 */
5639 if (c == NUL)
5640 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005641#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005642 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005643 if (wp->w_p_wrap)
5644 v = wp->w_skipcol;
5645 else
5646 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005647
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005648 /* check if line ends before left margin */
5649 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005650 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005651#ifdef FEAT_CONCEAL
5652 /* Get rid of the boguscols now, we want to draw until the right
5653 * edge for 'cursorcolumn'. */
5654 col -= boguscols;
5655 boguscols = 0;
5656#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005657
5658 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005659 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005660
5661 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005662 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5663 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005664 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005665 && lnum != wp->w_cursor.lnum)
5666 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005667# ifdef FEAT_RIGHTLEFT
5668 && !wp->w_p_rl
5669# endif
5670 )
5671 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005672 int rightmost_vcol = 0;
5673 int i;
5674
5675 if (wp->w_p_cuc)
5676 rightmost_vcol = wp->w_virtcol;
5677 if (draw_color_col)
5678 /* determine rightmost colorcolumn to possibly draw */
5679 for (i = 0; color_cols[i] >= 0; ++i)
5680 if (rightmost_vcol < color_cols[i])
5681 rightmost_vcol = color_cols[i];
5682
Bram Moolenaar02631462017-09-22 15:20:32 +02005683 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005684 {
5685 ScreenLines[off] = ' ';
5686#ifdef FEAT_MBYTE
5687 if (enc_utf8)
5688 ScreenLinesUC[off] = 0;
5689#endif
5690 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005691 if (draw_color_col)
5692 draw_color_col = advance_color_col(VCOL_HLC,
5693 &color_cols);
5694
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005695 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005696 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005697 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005698 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005699 else
5700 ScreenAttrs[off++] = 0;
5701
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005702 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005703 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005704
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005705 ++vcol;
5706 }
5707 }
5708#endif
5709
Bram Moolenaar53f81742017-09-22 14:35:51 +02005710 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005711 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 row++;
5713
5714 /*
5715 * Update w_cline_height and w_cline_folded if the cursor line was
5716 * updated (saves a call to plines() later).
5717 */
5718 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5719 {
5720 curwin->w_cline_row = startrow;
5721 curwin->w_cline_height = row - startrow;
5722#ifdef FEAT_FOLDING
5723 curwin->w_cline_folded = FALSE;
5724#endif
5725 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5726 }
5727
5728 break;
5729 }
5730
5731 /* line continues beyond line end */
5732 if (lcs_ext
5733 && !wp->w_p_wrap
5734#ifdef FEAT_DIFF
5735 && filler_todo <= 0
5736#endif
5737 && (
5738#ifdef FEAT_RIGHTLEFT
5739 wp->w_p_rl ? col == 0 :
5740#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005741 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005743 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5745 {
5746 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005747 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748#ifdef FEAT_MBYTE
5749 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005750 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005753 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005754 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
5756 else
5757 mb_utf8 = FALSE;
5758#endif
5759 }
5760
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005761#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005762 /* advance to the next 'colorcolumn' */
5763 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005764 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005765
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005766 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005767 * highlight the cursor position itself.
5768 * Also highlight the 'colorcolumn' if it is different than
5769 * 'cursorcolumn' */
5770 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005771 if (draw_state == WL_LINE && !lnum_in_visual_area
5772 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005773 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005774 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005775 && lnum != wp->w_cursor.lnum)
5776 {
5777 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005778 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005779 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005780 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005781 {
5782 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005783 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005784 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005785 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005786#endif
5787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 /*
5789 * Store character to be displayed.
5790 * Skip characters that are left of the screen for 'nowrap'.
5791 */
5792 vcol_prev = vcol;
5793 if (draw_state < WL_LINE || n_skip <= 0)
5794 {
5795 /*
5796 * Store the character.
5797 */
5798#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5799 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5800 {
5801 /* A double-wide character is: put first halve in left cell. */
5802 --off;
5803 --col;
5804 }
5805#endif
5806 ScreenLines[off] = c;
5807#ifdef FEAT_MBYTE
5808 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005809 {
5810 if ((mb_c & 0xff00) == 0x8e00)
5811 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 else if (enc_utf8)
5815 {
5816 if (mb_utf8)
5817 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005818 int i;
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005821 if ((c & 0xff) == 0)
5822 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005823 for (i = 0; i < Screen_mco; ++i)
5824 {
5825 ScreenLinesC[i][off] = u8cc[i];
5826 if (u8cc[i] == 0)
5827 break;
5828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830 else
5831 ScreenLinesUC[off] = 0;
5832 }
5833 if (multi_attr)
5834 {
5835 ScreenAttrs[off] = multi_attr;
5836 multi_attr = 0;
5837 }
5838 else
5839#endif
5840 ScreenAttrs[off] = char_attr;
5841
5842#ifdef FEAT_MBYTE
5843 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5844 {
5845 /* Need to fill two screen columns. */
5846 ++off;
5847 ++col;
5848 if (enc_utf8)
5849 /* UTF-8: Put a 0 in the second screen char. */
5850 ScreenLines[off] = 0;
5851 else
5852 /* DBCS: Put second byte in the second screen char. */
5853 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005854 if (draw_state > WL_NR
5855#ifdef FEAT_DIFF
5856 && filler_todo <= 0
5857#endif
5858 )
5859 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 /* When "tocol" is halfway a character, set it to the end of
5861 * the character, otherwise highlighting won't stop. */
5862 if (tocol == vcol)
5863 ++tocol;
5864#ifdef FEAT_RIGHTLEFT
5865 if (wp->w_p_rl)
5866 {
5867 /* now it's time to backup one cell */
5868 --off;
5869 --col;
5870 }
5871#endif
5872 }
5873#endif
5874#ifdef FEAT_RIGHTLEFT
5875 if (wp->w_p_rl)
5876 {
5877 --off;
5878 --col;
5879 }
5880 else
5881#endif
5882 {
5883 ++off;
5884 ++col;
5885 }
5886 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005887#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005888 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005889 {
5890 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005891 ++vcol_off;
5892 if (n_extra > 0)
5893 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005894 if (wp->w_p_wrap)
5895 {
5896 /*
5897 * Special voodoo required if 'wrap' is on.
5898 *
5899 * Advance the column indicator to force the line
5900 * drawing to wrap early. This will make the line
5901 * take up the same screen space when parts are concealed,
5902 * so that cursor line computations aren't messed up.
5903 *
5904 * To avoid the fictitious advance of 'col' causing
5905 * trailing junk to be written out of the screen line
5906 * we are building, 'boguscols' keeps track of the number
5907 * of bad columns we have advanced.
5908 */
5909 if (n_extra > 0)
5910 {
5911 vcol += n_extra;
5912# ifdef FEAT_RIGHTLEFT
5913 if (wp->w_p_rl)
5914 {
5915 col -= n_extra;
5916 boguscols -= n_extra;
5917 }
5918 else
5919# endif
5920 {
5921 col += n_extra;
5922 boguscols += n_extra;
5923 }
5924 n_extra = 0;
5925 n_attr = 0;
5926 }
5927
5928
5929# ifdef FEAT_MBYTE
5930 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5931 {
5932 /* Need to fill two screen columns. */
5933# ifdef FEAT_RIGHTLEFT
5934 if (wp->w_p_rl)
5935 {
5936 --boguscols;
5937 --col;
5938 }
5939 else
5940# endif
5941 {
5942 ++boguscols;
5943 ++col;
5944 }
5945 }
5946# endif
5947
5948# ifdef FEAT_RIGHTLEFT
5949 if (wp->w_p_rl)
5950 {
5951 --boguscols;
5952 --col;
5953 }
5954 else
5955# endif
5956 {
5957 ++boguscols;
5958 ++col;
5959 }
5960 }
5961 else
5962 {
5963 if (n_extra > 0)
5964 {
5965 vcol += n_extra;
5966 n_extra = 0;
5967 n_attr = 0;
5968 }
5969 }
5970
5971 }
5972#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 else
5974 --n_skip;
5975
Bram Moolenaar64486672010-05-16 15:46:46 +02005976 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5977 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005978 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#ifdef FEAT_DIFF
5980 && filler_todo <= 0
5981#endif
5982 )
5983 ++vcol;
5984
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005985#ifdef FEAT_SYN_HL
5986 if (vcol_save_attr >= 0)
5987 char_attr = vcol_save_attr;
5988#endif
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 /* restore attributes after "predeces" in 'listchars' */
5991 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5992 char_attr = saved_attr3;
5993
5994 /* restore attributes after last 'listchars' or 'number' char */
5995 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5996 char_attr = saved_attr2;
5997
5998 /*
5999 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006000 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 */
6002 if ((
6003#ifdef FEAT_RIGHTLEFT
6004 wp->w_p_rl ? (col < 0) :
6005#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006006 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 && (*ptr != NUL
6008#ifdef FEAT_DIFF
6009 || filler_todo > 0
6010#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006011 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6013 )
6014 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006015#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006016 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02006017 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006018 boguscols = 0;
6019#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006020 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02006021 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 ++row;
6024 ++screen_row;
6025
6026 /* When not wrapping and finished diff lines, or when displayed
6027 * '$' and highlighting until last column, break here. */
6028 if ((!wp->w_p_wrap
6029#ifdef FEAT_DIFF
6030 && filler_todo <= 0
6031#endif
6032 ) || lcs_eol_one == -1)
6033 break;
6034
6035 /* When the window is too narrow draw all "@" lines. */
6036 if (draw_state != WL_LINE
6037#ifdef FEAT_DIFF
6038 && filler_todo <= 0
6039#endif
6040 )
6041 {
6042 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 row = endrow;
6045 }
6046
6047 /* When line got too long for screen break here. */
6048 if (row == endrow)
6049 {
6050 ++row;
6051 break;
6052 }
6053
6054 if (screen_cur_row == screen_row - 1
6055#ifdef FEAT_DIFF
6056 && filler_todo <= 0
6057#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006058 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
6060 /* Remember that the line wraps, used for modeless copy. */
6061 LineWraps[screen_row - 1] = TRUE;
6062
6063 /*
6064 * Special trick to make copy/paste of wrapped lines work with
6065 * xterm/screen: write an extra character beyond the end of
6066 * the line. This will work with all terminal types
6067 * (regardless of the xn,am settings).
6068 * Only do this on a fast tty.
6069 * Only do this if the cursor is on the current line
6070 * (something has been written in it).
6071 * Don't do this for the GUI.
6072 * Don't do this for double-width characters.
6073 * Don't do this for a window not at the right screen border.
6074 */
6075 if (p_tf
6076#ifdef FEAT_GUI
6077 && !gui.in_use
6078#endif
6079#ifdef FEAT_MBYTE
6080 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006081 && ((*mb_off2cells)(LineOffset[screen_row],
6082 LineOffset[screen_row] + screen_Columns)
6083 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006085 + (int)Columns - 2,
6086 LineOffset[screen_row] + screen_Columns)
6087 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088#endif
6089 )
6090 {
6091 /* First make sure we are at the end of the screen line,
6092 * then output the same character again to let the
6093 * terminal know about the wrap. If the terminal doesn't
6094 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006095 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 screen_char(LineOffset[screen_row - 1]
6097 + (unsigned)Columns - 1,
6098 screen_row - 1, (int)(Columns - 1));
6099
6100#ifdef FEAT_MBYTE
6101 /* When there is a multi-byte character, just output a
6102 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006103 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6104 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 out_char(' ');
6106 else
6107#endif
6108 out_char(ScreenLines[LineOffset[screen_row - 1]
6109 + (Columns - 1)]);
6110 /* force a redraw of the first char on the next line */
6111 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6112 screen_start(); /* don't know where cursor is now */
6113 }
6114 }
6115
6116 col = 0;
6117 off = (unsigned)(current_ScreenLine - ScreenLines);
6118#ifdef FEAT_RIGHTLEFT
6119 if (wp->w_p_rl)
6120 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006121 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 off += col;
6123 }
6124#endif
6125
6126 /* reset the drawing state for the start of a wrapped line */
6127 draw_state = WL_START;
6128 saved_n_extra = n_extra;
6129 saved_p_extra = p_extra;
6130 saved_c_extra = c_extra;
6131 saved_char_attr = char_attr;
6132 n_extra = 0;
6133 lcs_prec_todo = lcs_prec;
6134#ifdef FEAT_LINEBREAK
6135# ifdef FEAT_DIFF
6136 if (filler_todo <= 0)
6137# endif
6138 need_showbreak = TRUE;
6139#endif
6140#ifdef FEAT_DIFF
6141 --filler_todo;
6142 /* When the filler lines are actually below the last line of the
6143 * file, don't draw the line itself, break here. */
6144 if (filler_todo == 0 && wp->w_botfill)
6145 break;
6146#endif
6147 }
6148
6149 } /* for every character in the line */
6150
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006151#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006152 /* After an empty line check first word for capital. */
6153 if (*skipwhite(line) == NUL)
6154 {
6155 capcol_lnum = lnum + 1;
6156 cap_col = 0;
6157 }
6158#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006159#ifdef FEAT_TEXT_PROP
6160 vim_free(text_props);
6161 vim_free(text_prop_idxs);
6162#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006163
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006164 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 return row;
6166}
6167
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006168#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006169/*
6170 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006171 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006172 */
6173 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006174comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006175{
6176 int i;
6177
6178 for (i = 0; i < Screen_mco; ++i)
6179 {
6180 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6181 return TRUE;
6182 if (ScreenLinesC[i][off_from] == 0)
6183 break;
6184 }
6185 return FALSE;
6186}
6187#endif
6188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189/*
6190 * Check whether the given character needs redrawing:
6191 * - the (first byte of the) character is different
6192 * - the attributes are different
6193 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006194 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 */
6196 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006197char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 if (cols > 0
6200 && ((ScreenLines[off_from] != ScreenLines[off_to]
6201 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6202
6203#ifdef FEAT_MBYTE
6204 || (enc_dbcs != 0
6205 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6206 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6207 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6208 : (cols > 1 && ScreenLines[off_from + 1]
6209 != ScreenLines[off_to + 1])))
6210 || (enc_utf8
6211 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6212 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006213 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006214 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6215 && ScreenLines[off_from + 1]
6216 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217#endif
6218 ))
6219 return TRUE;
6220 return FALSE;
6221}
6222
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006223#if defined(FEAT_TERMINAL) || defined(PROTO)
6224/*
6225 * Return the index in ScreenLines[] for the current screen line.
6226 */
6227 int
6228screen_get_current_line_off()
6229{
6230 return (int)(current_ScreenLine - ScreenLines);
6231}
6232#endif
6233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234/*
6235 * Move one "cooked" screen line to the screen, but only the characters that
6236 * have actually changed. Handle insert/delete character.
6237 * "coloff" gives the first column on the screen for this line.
6238 * "endcol" gives the columns where valid characters are.
6239 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6240 * needs to be cleared, negative otherwise.
6241 * "rlflag" is TRUE in a rightleft window:
6242 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6243 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6244 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006245 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006246screen_line(
6247 int row,
6248 int coloff,
6249 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006250 int clear_width,
6251 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
6253 unsigned off_from;
6254 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006255#ifdef FEAT_MBYTE
6256 unsigned max_off_from;
6257 unsigned max_off_to;
6258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 int force = FALSE; /* force update rest of the line */
6262 int redraw_this /* bool: does character need redraw? */
6263#ifdef FEAT_GUI
6264 = TRUE /* For GUI when while-loop empty */
6265#endif
6266 ;
6267 int redraw_next; /* redraw_this for next character */
6268#ifdef FEAT_MBYTE
6269 int clear_next = FALSE;
6270 int char_cells; /* 1: normal char */
6271 /* 2: occupies two display cells */
6272# define CHAR_CELLS char_cells
6273#else
6274# define CHAR_CELLS 1
6275#endif
6276
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006277 /* Check for illegal row and col, just in case. */
6278 if (row >= Rows)
6279 row = Rows - 1;
6280 if (endcol > Columns)
6281 endcol = Columns;
6282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283# ifdef FEAT_CLIPBOARD
6284 clip_may_clear_selection(row, row);
6285# endif
6286
6287 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6288 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006289#ifdef FEAT_MBYTE
6290 max_off_from = off_from + screen_Columns;
6291 max_off_to = LineOffset[row] + screen_Columns;
6292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
6294#ifdef FEAT_RIGHTLEFT
6295 if (rlflag)
6296 {
6297 /* Clear rest first, because it's left of the text. */
6298 if (clear_width > 0)
6299 {
6300 while (col <= endcol && ScreenLines[off_to] == ' '
6301 && ScreenAttrs[off_to] == 0
6302# ifdef FEAT_MBYTE
6303 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6304# endif
6305 )
6306 {
6307 ++off_to;
6308 ++col;
6309 }
6310 if (col <= endcol)
6311 screen_fill(row, row + 1, col + coloff,
6312 endcol + coloff + 1, ' ', ' ', 0);
6313 }
6314 col = endcol + 1;
6315 off_to = LineOffset[row] + col + coloff;
6316 off_from += col;
6317 endcol = (clear_width > 0 ? clear_width : -clear_width);
6318 }
6319#endif /* FEAT_RIGHTLEFT */
6320
6321 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6322
6323 while (col < endcol)
6324 {
6325#ifdef FEAT_MBYTE
6326 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006327 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 else
6329 char_cells = 1;
6330#endif
6331
6332 redraw_this = redraw_next;
6333 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6334 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6335
6336#ifdef FEAT_GUI
6337 /* If the next character was bold, then redraw the current character to
6338 * remove any pixels that might have spilt over into us. This only
6339 * happens in the GUI.
6340 */
6341 if (redraw_next && gui.in_use)
6342 {
6343 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006344 if (hl > HL_ALL)
6345 hl = syn_attr2attr(hl);
6346 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 redraw_this = TRUE;
6348 }
6349#endif
6350
6351 if (redraw_this)
6352 {
6353 /*
6354 * Special handling when 'xs' termcap flag set (hpterm):
6355 * Attributes for characters are stored at the position where the
6356 * cursor is when writing the highlighting code. The
6357 * start-highlighting code must be written with the cursor on the
6358 * first highlighted character. The stop-highlighting code must
6359 * be written with the cursor just after the last highlighted
6360 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006361 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 * to clear the rest of the line, and force redrawing it
6363 * completely.
6364 */
6365 if ( p_wiv
6366 && !force
6367#ifdef FEAT_GUI
6368 && !gui.in_use
6369#endif
6370 && ScreenAttrs[off_to] != 0
6371 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6372 {
6373 /*
6374 * Need to remove highlighting attributes here.
6375 */
6376 windgoto(row, col + coloff);
6377 out_str(T_CE); /* clear rest of this screen line */
6378 screen_start(); /* don't know where cursor is now */
6379 force = TRUE; /* force redraw of rest of the line */
6380 redraw_next = TRUE; /* or else next char would miss out */
6381
6382 /*
6383 * If the previous character was highlighted, need to stop
6384 * highlighting at this character.
6385 */
6386 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6387 {
6388 screen_attr = ScreenAttrs[off_to - 1];
6389 term_windgoto(row, col + coloff);
6390 screen_stop_highlight();
6391 }
6392 else
6393 screen_attr = 0; /* highlighting has stopped */
6394 }
6395#ifdef FEAT_MBYTE
6396 if (enc_dbcs != 0)
6397 {
6398 /* Check if overwriting a double-byte with a single-byte or
6399 * the other way around requires another character to be
6400 * redrawn. For UTF-8 this isn't needed, because comparing
6401 * ScreenLinesUC[] is sufficient. */
6402 if (char_cells == 1
6403 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006404 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
6406 /* Writing a single-cell character over a double-cell
6407 * character: need to redraw the next cell. */
6408 ScreenLines[off_to + 1] = 0;
6409 redraw_next = TRUE;
6410 }
6411 else if (char_cells == 2
6412 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006413 && (*mb_off2cells)(off_to, max_off_to) == 1
6414 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 {
6416 /* Writing the second half of a double-cell character over
6417 * a double-cell character: need to redraw the second
6418 * cell. */
6419 ScreenLines[off_to + 2] = 0;
6420 redraw_next = TRUE;
6421 }
6422
6423 if (enc_dbcs == DBCS_JPNU)
6424 ScreenLines2[off_to] = ScreenLines2[off_from];
6425 }
6426 /* When writing a single-width character over a double-width
6427 * character and at the end of the redrawn text, need to clear out
6428 * the right halve of the old character.
6429 * Also required when writing the right halve of a double-width
6430 * char over the left halve of an existing one. */
6431 if (has_mbyte && col + char_cells == endcol
6432 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006433 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006435 && (*mb_off2cells)(off_to, max_off_to) == 1
6436 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 clear_next = TRUE;
6438#endif
6439
6440 ScreenLines[off_to] = ScreenLines[off_from];
6441#ifdef FEAT_MBYTE
6442 if (enc_utf8)
6443 {
6444 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6445 if (ScreenLinesUC[off_from] != 0)
6446 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006447 int i;
6448
6449 for (i = 0; i < Screen_mco; ++i)
6450 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 }
6452 }
6453 if (char_cells == 2)
6454 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6455#endif
6456
6457#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006458 /* The bold trick makes a single column of pixels appear in the
6459 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 * character should be redrawn too. This happens for our own GUI
6461 * and for some xterms. */
6462 if (
6463# ifdef FEAT_GUI
6464 gui.in_use
6465# endif
6466# if defined(FEAT_GUI) && defined(UNIX)
6467 ||
6468# endif
6469# ifdef UNIX
6470 term_is_xterm
6471# endif
6472 )
6473 {
6474 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006475 if (hl > HL_ALL)
6476 hl = syn_attr2attr(hl);
6477 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 redraw_next = TRUE;
6479 }
6480#endif
6481 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6482#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006483 /* For simplicity set the attributes of second half of a
6484 * double-wide character equal to the first half. */
6485 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006487
6488 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 else
6491#endif
6492 screen_char(off_to, row, col + coloff);
6493 }
6494 else if ( p_wiv
6495#ifdef FEAT_GUI
6496 && !gui.in_use
6497#endif
6498 && col + coloff > 0)
6499 {
6500 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6501 {
6502 /*
6503 * Don't output stop-highlight when moving the cursor, it will
6504 * stop the highlighting when it should continue.
6505 */
6506 screen_attr = 0;
6507 }
6508 else if (screen_attr != 0)
6509 screen_stop_highlight();
6510 }
6511
6512 off_to += CHAR_CELLS;
6513 off_from += CHAR_CELLS;
6514 col += CHAR_CELLS;
6515 }
6516
6517#ifdef FEAT_MBYTE
6518 if (clear_next)
6519 {
6520 /* Clear the second half of a double-wide character of which the left
6521 * half was overwritten with a single-wide character. */
6522 ScreenLines[off_to] = ' ';
6523 if (enc_utf8)
6524 ScreenLinesUC[off_to] = 0;
6525 screen_char(off_to, row, col + coloff);
6526 }
6527#endif
6528
6529 if (clear_width > 0
6530#ifdef FEAT_RIGHTLEFT
6531 && !rlflag
6532#endif
6533 )
6534 {
6535#ifdef FEAT_GUI
6536 int startCol = col;
6537#endif
6538
6539 /* blank out the rest of the line */
6540 while (col < clear_width && ScreenLines[off_to] == ' '
6541 && ScreenAttrs[off_to] == 0
6542#ifdef FEAT_MBYTE
6543 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6544#endif
6545 )
6546 {
6547 ++off_to;
6548 ++col;
6549 }
6550 if (col < clear_width)
6551 {
6552#ifdef FEAT_GUI
6553 /*
6554 * In the GUI, clearing the rest of the line may leave pixels
6555 * behind if the first character cleared was bold. Some bold
6556 * fonts spill over the left. In this case we redraw the previous
6557 * character too. If we didn't skip any blanks above, then we
6558 * only redraw if the character wasn't already redrawn anyway.
6559 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006560 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
6562 hl = ScreenAttrs[off_to];
6563 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006564 {
6565 int prev_cells = 1;
6566# ifdef FEAT_MBYTE
6567 if (enc_utf8)
6568 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6569 * that its width is 2. */
6570 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6571 else if (enc_dbcs != 0)
6572 {
6573 /* find previous character by counting from first
6574 * column and get its width. */
6575 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006576 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006577
6578 while (off < off_to)
6579 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006580 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006581 off += prev_cells;
6582 }
6583 }
6584
6585 if (enc_dbcs != 0 && prev_cells > 1)
6586 screen_char_2(off_to - prev_cells, row,
6587 col + coloff - prev_cells);
6588 else
6589# endif
6590 screen_char(off_to - prev_cells, row,
6591 col + coloff - prev_cells);
6592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594#endif
6595 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6596 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 off_to += clear_width - col;
6598 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600 }
6601
6602 if (clear_width > 0)
6603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 /* For a window that's left of another, draw the separator char. */
6605 if (col + coloff < Columns)
6606 {
6607 int c;
6608
6609 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006610 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006611#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006612 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6613 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 || ScreenAttrs[off_to] != hl)
6616 {
6617 ScreenLines[off_to] = c;
6618 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006619#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 if (enc_utf8)
6621 {
6622 if (c >= 0x80)
6623 {
6624 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006625 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 }
6627 else
6628 ScreenLinesUC[off_to] = 0;
6629 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 screen_char(off_to, row, col + coloff);
6632 }
6633 }
6634 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 LineWraps[row] = FALSE;
6636 }
6637}
6638
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006639#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006641 * Mirror text "str" for right-left displaying.
6642 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006644 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006645rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 char_u *p1, *p2;
6648 int t;
6649
6650 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6651 {
6652 t = *p1;
6653 *p1 = *p2;
6654 *p2 = t;
6655 }
6656}
6657#endif
6658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659/*
6660 * mark all status lines for redraw; used after first :cd
6661 */
6662 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006663status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 win_T *wp;
6666
Bram Moolenaar29323592016-07-24 22:04:11 +02006667 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (wp->w_status_height)
6669 {
6670 wp->w_redr_status = TRUE;
6671 redraw_later(VALID);
6672 }
6673}
6674
6675/*
6676 * mark all status lines of the current buffer for redraw
6677 */
6678 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006679status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 win_T *wp;
6682
Bram Moolenaar29323592016-07-24 22:04:11 +02006683 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6685 {
6686 wp->w_redr_status = TRUE;
6687 redraw_later(VALID);
6688 }
6689}
6690
6691/*
6692 * Redraw all status lines that need to be redrawn.
6693 */
6694 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006695redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 win_T *wp;
6698
Bram Moolenaar29323592016-07-24 22:04:11 +02006699 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006701 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006702 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006703 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
Bram Moolenaar4033c552017-09-16 20:54:51 +02006706#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707/*
6708 * Redraw all status lines at the bottom of frame "frp".
6709 */
6710 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006711win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 if (frp->fr_layout == FR_LEAF)
6714 frp->fr_win->w_redr_status = TRUE;
6715 else if (frp->fr_layout == FR_ROW)
6716 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006717 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 win_redraw_last_status(frp);
6719 }
6720 else /* frp->fr_layout == FR_COL */
6721 {
6722 frp = frp->fr_child;
6723 while (frp->fr_next != NULL)
6724 frp = frp->fr_next;
6725 win_redraw_last_status(frp);
6726 }
6727}
6728#endif
6729
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730/*
6731 * Draw the verticap separator right of window "wp" starting with line "row".
6732 */
6733 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006734draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 int hl;
6737 int c;
6738
6739 if (wp->w_vsep_width)
6740 {
6741 /* draw the vertical separator right of this window */
6742 c = fillchar_vsep(&hl);
6743 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6744 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6745 c, ' ', hl);
6746 }
6747}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006750static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
6752/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006753 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 */
6755 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006756status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757{
6758 int len = 0;
6759
6760#ifdef FEAT_MENU
6761 int emenu = (xp->xp_context == EXPAND_MENUS
6762 || xp->xp_context == EXPAND_MENUNAMES);
6763
6764 /* Check for menu separators - replace with '|'. */
6765 if (emenu && menu_is_separator(s))
6766 return 1;
6767#endif
6768
6769 while (*s != NUL)
6770 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006771 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006772 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006773 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 }
6775
6776 return len;
6777}
6778
6779/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006780 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006781 * These are backslashes used for escaping. Do show backslashes in help tags.
6782 */
6783 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006784skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006785{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006786 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006787#ifdef FEAT_MENU
6788 || ((xp->xp_context == EXPAND_MENUS
6789 || xp->xp_context == EXPAND_MENUNAMES)
6790 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6791#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006792 )
6793 {
6794#ifndef BACKSLASH_IN_FILENAME
6795 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6796 return 2;
6797#endif
6798 return 1;
6799 }
6800 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006801}
6802
6803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 * Show wildchar matches in the status line.
6805 * Show at least the "match" item.
6806 * We start at item 'first_match' in the list and show all matches that fit.
6807 *
6808 * If inversion is possible we use it. Else '=' characters are used.
6809 */
6810 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006811win_redr_status_matches(
6812 expand_T *xp,
6813 int num_matches,
6814 char_u **matches, /* list of matches */
6815 int match,
6816 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817{
6818#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6819 int row;
6820 char_u *buf;
6821 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006822 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 int fillchar;
6824 int attr;
6825 int i;
6826 int highlight = TRUE;
6827 char_u *selstart = NULL;
6828 int selstart_col = 0;
6829 char_u *selend = NULL;
6830 static int first_match = 0;
6831 int add_left = FALSE;
6832 char_u *s;
6833#ifdef FEAT_MENU
6834 int emenu;
6835#endif
6836#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6837 int l;
6838#endif
6839
6840 if (matches == NULL) /* interrupted completion? */
6841 return;
6842
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006843#ifdef FEAT_MBYTE
6844 if (has_mbyte)
6845 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6846 else
6847#endif
6848 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 if (buf == NULL)
6850 return;
6851
6852 if (match == -1) /* don't show match but original text */
6853 {
6854 match = 0;
6855 highlight = FALSE;
6856 }
6857 /* count 1 for the ending ">" */
6858 clen = status_match_len(xp, L_MATCH(match)) + 3;
6859 if (match == 0)
6860 first_match = 0;
6861 else if (match < first_match)
6862 {
6863 /* jumping left, as far as we can go */
6864 first_match = match;
6865 add_left = TRUE;
6866 }
6867 else
6868 {
6869 /* check if match fits on the screen */
6870 for (i = first_match; i < match; ++i)
6871 clen += status_match_len(xp, L_MATCH(i)) + 2;
6872 if (first_match > 0)
6873 clen += 2;
6874 /* jumping right, put match at the left */
6875 if ((long)clen > Columns)
6876 {
6877 first_match = match;
6878 /* if showing the last match, we can add some on the left */
6879 clen = 2;
6880 for (i = match; i < num_matches; ++i)
6881 {
6882 clen += status_match_len(xp, L_MATCH(i)) + 2;
6883 if ((long)clen >= Columns)
6884 break;
6885 }
6886 if (i == num_matches)
6887 add_left = TRUE;
6888 }
6889 }
6890 if (add_left)
6891 while (first_match > 0)
6892 {
6893 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6894 if ((long)clen >= Columns)
6895 break;
6896 --first_match;
6897 }
6898
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006899 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
6901 if (first_match == 0)
6902 {
6903 *buf = NUL;
6904 len = 0;
6905 }
6906 else
6907 {
6908 STRCPY(buf, "< ");
6909 len = 2;
6910 }
6911 clen = len;
6912
6913 i = first_match;
6914 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6915 {
6916 if (i == match)
6917 {
6918 selstart = buf + len;
6919 selstart_col = clen;
6920 }
6921
6922 s = L_MATCH(i);
6923 /* Check for menu separators - replace with '|' */
6924#ifdef FEAT_MENU
6925 emenu = (xp->xp_context == EXPAND_MENUS
6926 || xp->xp_context == EXPAND_MENUNAMES);
6927 if (emenu && menu_is_separator(s))
6928 {
6929 STRCPY(buf + len, transchar('|'));
6930 l = (int)STRLEN(buf + len);
6931 len += l;
6932 clen += l;
6933 }
6934 else
6935#endif
6936 for ( ; *s != NUL; ++s)
6937 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006938 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 clen += ptr2cells(s);
6940#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006941 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {
6943 STRNCPY(buf + len, s, l);
6944 s += l - 1;
6945 len += l;
6946 }
6947 else
6948#endif
6949 {
6950 STRCPY(buf + len, transchar_byte(*s));
6951 len += (int)STRLEN(buf + len);
6952 }
6953 }
6954 if (i == match)
6955 selend = buf + len;
6956
6957 *(buf + len++) = ' ';
6958 *(buf + len++) = ' ';
6959 clen += 2;
6960 if (++i == num_matches)
6961 break;
6962 }
6963
6964 if (i != num_matches)
6965 {
6966 *(buf + len++) = '>';
6967 ++clen;
6968 }
6969
6970 buf[len] = NUL;
6971
6972 row = cmdline_row - 1;
6973 if (row >= 0)
6974 {
6975 if (wild_menu_showing == 0)
6976 {
6977 if (msg_scrolled > 0)
6978 {
6979 /* Put the wildmenu just above the command line. If there is
6980 * no room, scroll the screen one line up. */
6981 if (cmdline_row == Rows - 1)
6982 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006983 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 ++msg_scrolled;
6985 }
6986 else
6987 {
6988 ++cmdline_row;
6989 ++row;
6990 }
6991 wild_menu_showing = WM_SCROLLED;
6992 }
6993 else
6994 {
6995 /* Create status line if needed by setting 'laststatus' to 2.
6996 * Set 'winminheight' to zero to avoid that the window is
6997 * resized. */
6998 if (lastwin->w_status_height == 0)
6999 {
7000 save_p_ls = p_ls;
7001 save_p_wmh = p_wmh;
7002 p_ls = 2;
7003 p_wmh = 0;
7004 last_status(FALSE);
7005 }
7006 wild_menu_showing = WM_SHOWN;
7007 }
7008 }
7009
7010 screen_puts(buf, row, 0, attr);
7011 if (selstart != NULL && highlight)
7012 {
7013 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007014 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 }
7016
7017 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7018 }
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 vim_free(buf);
7022}
7023#endif
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025/*
7026 * Redraw the status line of window wp.
7027 *
7028 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007029 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7030 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007032 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007033win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 int row;
7036 char_u *p;
7037 int len;
7038 int fillchar;
7039 int attr;
7040 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007041 static int busy = FALSE;
7042
7043 /* It's possible to get here recursively when 'statusline' (indirectly)
7044 * invokes ":redrawstatus". Simply ignore the call then. */
7045 if (busy)
7046 return;
7047 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048
7049 wp->w_redr_status = FALSE;
7050 if (wp->w_status_height == 0)
7051 {
7052 /* no status line, can only be last window */
7053 redraw_cmdline = TRUE;
7054 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007055 else if (!redrawing()
7056#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007057 // don't update status line when popup menu is visible and may be
7058 // drawn over it, unless it will be redrawn later
7059 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007060#endif
7061 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
7063 /* Don't redraw right now, do it later. */
7064 wp->w_redr_status = TRUE;
7065 }
7066#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007067 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {
7069 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007070 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 }
7072#endif
7073 else
7074 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007075 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007077 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 p = NameBuff;
7079 len = (int)STRLEN(p);
7080
Bram Moolenaard85f2712017-07-28 21:51:57 +02007081 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082#ifdef FEAT_QUICKFIX
7083 || wp->w_p_pvw
7084#endif
7085 || bufIsChanged(wp->w_buffer)
7086 || wp->w_buffer->b_p_ro)
7087 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007088 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007090 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 len += (int)STRLEN(p + len);
7092 }
7093#ifdef FEAT_QUICKFIX
7094 if (wp->w_p_pvw)
7095 {
7096 STRCPY(p + len, _("[Preview]"));
7097 len += (int)STRLEN(p + len);
7098 }
7099#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007100 if (bufIsChanged(wp->w_buffer)
7101#ifdef FEAT_TERMINAL
7102 && !bt_terminal(wp->w_buffer)
7103#endif
7104 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {
7106 STRCPY(p + len, "[+]");
7107 len += 3;
7108 }
7109 if (wp->w_buffer->b_p_ro)
7110 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007111 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007112 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 }
7114
Bram Moolenaar02631462017-09-22 15:20:32 +02007115 this_ru_col = ru_col - (Columns - wp->w_width);
7116 if (this_ru_col < (wp->w_width + 1) / 2)
7117 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 if (this_ru_col <= 1)
7119 {
7120 p = (char_u *)"<"; /* No room for file name! */
7121 len = 1;
7122 }
7123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124#ifdef FEAT_MBYTE
7125 if (has_mbyte)
7126 {
7127 int clen = 0, i;
7128
7129 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007130 clen = mb_string2cells(p, -1);
7131
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 /* Find first character that will fit.
7133 * Going from start to end is much faster for DBCS. */
7134 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007135 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 clen -= (*mb_ptr2cells)(p + i);
7137 len = clen;
7138 if (i > 0)
7139 {
7140 p = p + i - 1;
7141 *p = '<';
7142 ++len;
7143 }
7144
7145 }
7146 else
7147#endif
7148 if (len > this_ru_col - 1)
7149 {
7150 p += len - (this_ru_col - 1);
7151 *p = '<';
7152 len = this_ru_col - 1;
7153 }
7154
7155 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007156 screen_puts(p, row, wp->w_wincol, attr);
7157 screen_fill(row, row + 1, len + wp->w_wincol,
7158 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007160 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7162 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007163 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
7165#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007166 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167#endif
7168 }
7169
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 /*
7171 * May need to draw the character below the vertical separator.
7172 */
7173 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7174 {
7175 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007176 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 else
7178 fillchar = fillchar_vsep(&attr);
7179 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7180 attr);
7181 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007182 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183}
7184
Bram Moolenaar238a5642006-02-21 22:12:05 +00007185#ifdef FEAT_STL_OPT
7186/*
7187 * Redraw the status line according to 'statusline' and take care of any
7188 * errors encountered.
7189 */
7190 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007191redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007192{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007193 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007194 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007195
7196 /* When called recursively return. This can happen when the statusline
7197 * contains an expression that triggers a redraw. */
7198 if (entered)
7199 return;
7200 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007201
Bram Moolenaara742e082016-04-05 21:10:38 +02007202 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007203 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007204 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007205 {
7206 /* When there is an error disable the statusline, otherwise the
7207 * display is messed up with errors and a redraw triggers the problem
7208 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007209 set_string_option_direct((char_u *)"statusline", -1,
7210 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007211 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007212 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007213 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007214 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007215}
7216#endif
7217
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218/*
7219 * Return TRUE if the status line of window "wp" is connected to the status
7220 * line of the window right of it. If not, then it's a vertical separator.
7221 * Only call if (wp->w_vsep_width != 0).
7222 */
7223 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007224stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
7226 frame_T *fr;
7227
7228 fr = wp->w_frame;
7229 while (fr->fr_parent != NULL)
7230 {
7231 if (fr->fr_parent->fr_layout == FR_COL)
7232 {
7233 if (fr->fr_next != NULL)
7234 break;
7235 }
7236 else
7237 {
7238 if (fr->fr_next != NULL)
7239 return TRUE;
7240 }
7241 fr = fr->fr_parent;
7242 }
7243 return FALSE;
7244}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247/*
7248 * Get the value to show for the language mappings, active 'keymap'.
7249 */
7250 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007251get_keymap_str(
7252 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007253 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007254 char_u *buf, /* buffer for the result */
7255 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256{
7257 char_u *p;
7258
7259 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7260 return FALSE;
7261
7262 {
7263#ifdef FEAT_EVAL
7264 buf_T *old_curbuf = curbuf;
7265 win_T *old_curwin = curwin;
7266 char_u *s;
7267
7268 curbuf = wp->w_buffer;
7269 curwin = wp;
7270 STRCPY(buf, "b:keymap_name"); /* must be writable */
7271 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007272 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 --emsg_skip;
7274 curbuf = old_curbuf;
7275 curwin = old_curwin;
7276 if (p == NULL || *p == NUL)
7277#endif
7278 {
7279#ifdef FEAT_KEYMAP
7280 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7281 p = wp->w_buffer->b_p_keymap;
7282 else
7283#endif
7284 p = (char_u *)"lang";
7285 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007286 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 buf[0] = NUL;
7288#ifdef FEAT_EVAL
7289 vim_free(s);
7290#endif
7291 }
7292 return buf[0] != NUL;
7293}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
7295#if defined(FEAT_STL_OPT) || defined(PROTO)
7296/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007297 * Redraw the status line or ruler of window "wp".
7298 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 */
7300 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007301win_redr_custom(
7302 win_T *wp,
7303 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007305 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 int attr;
7307 int curattr;
7308 int row;
7309 int col = 0;
7310 int maxwidth;
7311 int width;
7312 int n;
7313 int len;
7314 int fillchar;
7315 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007316 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007318 struct stl_hlrec hltab[STL_MAX_ITEM];
7319 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007320 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007321 win_T *ewp;
7322 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
Bram Moolenaar1d633412013-12-11 15:52:01 +01007324 /* There is a tiny chance that this gets called recursively: When
7325 * redrawing a status line triggers redrawing the ruler or tabline.
7326 * Avoid trouble by not allowing recursion. */
7327 if (entered)
7328 return;
7329 entered = TRUE;
7330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007332 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007334 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007335 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007336 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007337 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007338 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007339 maxwidth = Columns;
7340# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007341 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007342# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007344 else
7345 {
7346 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007347 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007348 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007349
7350 if (draw_ruler)
7351 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007352 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007353 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007354 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007355 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007356 if (*++stl == '-')
7357 stl++;
7358 if (atoi((char *)stl))
7359 while (VIM_ISDIGIT(*stl))
7360 stl++;
7361 if (*stl++ != '(')
7362 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007363 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007364 col = ru_col - (Columns - wp->w_width);
7365 if (col < (wp->w_width + 1) / 2)
7366 col = (wp->w_width + 1) / 2;
7367 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007368 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007369 {
7370 row = Rows - 1;
7371 --maxwidth; /* writing in last column may cause scrolling */
7372 fillchar = ' ';
7373 attr = 0;
7374 }
7375
7376# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007377 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007378# endif
7379 }
7380 else
7381 {
7382 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007383 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007384 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007385 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007386# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007387 use_sandbox = was_set_insecurely((char_u *)"statusline",
7388 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007389# endif
7390 }
7391
Bram Moolenaar53f81742017-09-22 14:35:51 +02007392 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007393 }
7394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007396 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
Bram Moolenaar61452852011-02-01 18:01:11 +01007398 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7399 * the cursor away and back. */
7400 ewp = wp == NULL ? curwin : wp;
7401 p_crb_save = ewp->w_p_crb;
7402 ewp->w_p_crb = FALSE;
7403
Bram Moolenaar362f3562009-11-03 16:20:34 +00007404 /* Make a copy, because the statusline may include a function call that
7405 * might change the option value and free the memory. */
7406 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007407 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007408 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007409 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007410 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007411 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007413 /* Make all characters printable. */
7414 p = transstr(buf);
7415 if (p != NULL)
7416 {
7417 vim_strncpy(buf, p, sizeof(buf) - 1);
7418 vim_free(p);
7419 }
7420
7421 /* fill up with "fillchar" */
7422 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007423 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {
7425#ifdef FEAT_MBYTE
7426 len += (*mb_char2bytes)(fillchar, buf + len);
7427#else
7428 buf[len++] = fillchar;
7429#endif
7430 ++width;
7431 }
7432 buf[len] = NUL;
7433
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007434 /*
7435 * Draw each snippet with the specified highlighting.
7436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 curattr = attr;
7438 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007439 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007441 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 screen_puts_len(p, len, row, col, curattr);
7443 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007444 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007446 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007448 else if (hltab[n].userhl < 0)
7449 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007450#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007451 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7452 && wp->w_status_height != 0)
7453 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007454 else if (wp != NULL && bt_terminal(wp->w_buffer)
7455 && wp->w_status_height != 0)
7456 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007457#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007458 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007459 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007461 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
7463 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007464
7465 if (wp == NULL)
7466 {
7467 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7468 col = 0;
7469 len = 0;
7470 p = buf;
7471 fillchar = 0;
7472 for (n = 0; tabtab[n].start != NULL; n++)
7473 {
7474 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7475 while (col < len)
7476 TabPageIdxs[col++] = fillchar;
7477 p = tabtab[n].start;
7478 fillchar = tabtab[n].userhl;
7479 }
7480 while (col < Columns)
7481 TabPageIdxs[col++] = fillchar;
7482 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007483
7484theend:
7485 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486}
7487
7488#endif /* FEAT_STL_OPT */
7489
7490/*
7491 * Output a single character directly to the screen and update ScreenLines.
7492 */
7493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007494screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 char_u buf[MB_MAXBYTES + 1];
7497
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007498#ifdef FEAT_MBYTE
7499 if (has_mbyte)
7500 buf[(*mb_char2bytes)(c, buf)] = NUL;
7501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007503 {
7504 buf[0] = c;
7505 buf[1] = NUL;
7506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 screen_puts(buf, row, col, attr);
7508}
7509
7510/*
7511 * Get a single character directly from ScreenLines into "bytes[]".
7512 * Also return its attribute in *attrp;
7513 */
7514 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007515screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
7517 unsigned off;
7518
7519 /* safety check */
7520 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7521 {
7522 off = LineOffset[row] + col;
7523 *attrp = ScreenAttrs[off];
7524 bytes[0] = ScreenLines[off];
7525 bytes[1] = NUL;
7526
7527#ifdef FEAT_MBYTE
7528 if (enc_utf8 && ScreenLinesUC[off] != 0)
7529 bytes[utfc_char2bytes(off, bytes)] = NUL;
7530 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7531 {
7532 bytes[0] = ScreenLines[off];
7533 bytes[1] = ScreenLines2[off];
7534 bytes[2] = NUL;
7535 }
7536 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7537 {
7538 bytes[1] = ScreenLines[off + 1];
7539 bytes[2] = NUL;
7540 }
7541#endif
7542 }
7543}
7544
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007545#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007546/*
7547 * Return TRUE if composing characters for screen posn "off" differs from
7548 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007549 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007550 */
7551 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007552screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007553{
7554 int i;
7555
7556 for (i = 0; i < Screen_mco; ++i)
7557 {
7558 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7559 return TRUE;
7560 if (u8cc[i] == 0)
7561 break;
7562 }
7563 return FALSE;
7564}
7565#endif
7566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567/*
7568 * Put string '*text' on the screen at position 'row' and 'col', with
7569 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7570 * Note: only outputs within one row, message is truncated at screen boundary!
7571 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7572 */
7573 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007574screen_puts(
7575 char_u *text,
7576 int row,
7577 int col,
7578 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
7580 screen_puts_len(text, -1, row, col, attr);
7581}
7582
7583/*
7584 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7585 * a NUL.
7586 */
7587 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007588screen_puts_len(
7589 char_u *text,
7590 int textlen,
7591 int row,
7592 int col,
7593 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594{
7595 unsigned off;
7596 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007597 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 int c;
7599#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007600 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 int mbyte_blen = 1;
7602 int mbyte_cells = 1;
7603 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007604 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 int clear_next_cell = FALSE;
7606# ifdef FEAT_ARABIC
7607 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007608 int pc, nc, nc1;
7609 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610# endif
7611#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007612#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7613 int force_redraw_this;
7614 int force_redraw_next = FALSE;
7615#endif
7616 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617
7618 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7619 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007620 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621
Bram Moolenaarc236c162008-07-13 17:41:49 +00007622#ifdef FEAT_MBYTE
7623 /* When drawing over the right halve of a double-wide char clear out the
7624 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007625 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007626# ifdef FEAT_GUI
7627 && !gui.in_use
7628# endif
7629 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007630 {
7631 ScreenLines[off - 1] = ' ';
7632 ScreenAttrs[off - 1] = 0;
7633 if (enc_utf8)
7634 {
7635 ScreenLinesUC[off - 1] = 0;
7636 ScreenLinesC[0][off - 1] = 0;
7637 }
7638 /* redraw the previous cell, make it empty */
7639 screen_char(off - 1, row, col - 1);
7640 /* force the cell at "col" to be redrawn */
7641 force_redraw_next = TRUE;
7642 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007643#endif
7644
Bram Moolenaar367329b2007-08-30 11:53:22 +00007645#ifdef FEAT_MBYTE
7646 max_off = LineOffset[row] + screen_Columns;
7647#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007648 while (col < screen_Columns
7649 && (len < 0 || (int)(ptr - text) < len)
7650 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
7652 c = *ptr;
7653#ifdef FEAT_MBYTE
7654 /* check if this is the first byte of a multibyte */
7655 if (has_mbyte)
7656 {
7657 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007658 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007660 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7662 mbyte_cells = 1;
7663 else if (enc_dbcs != 0)
7664 mbyte_cells = mbyte_blen;
7665 else /* enc_utf8 */
7666 {
7667 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007668 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 (int)((text + len) - ptr));
7670 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007671 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007673# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 /* Non-BMP character: display as ? or fullwidth ?. */
7675 if (u8c >= 0x10000)
7676 {
7677 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7678 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007679 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682# ifdef FEAT_ARABIC
7683 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7684 {
7685 /* Do Arabic shaping. */
7686 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7687 {
7688 /* Past end of string to be displayed. */
7689 nc = NUL;
7690 nc1 = NUL;
7691 }
7692 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007693 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007694 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7695 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007696 nc1 = pcc[0];
7697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 pc = prev_c;
7699 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007700 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
7702 else
7703 prev_c = u8c;
7704# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007705 if (col + mbyte_cells > screen_Columns)
7706 {
7707 /* Only 1 cell left, but character requires 2 cells:
7708 * display a '>' in the last column to avoid wrapping. */
7709 c = '>';
7710 mbyte_cells = 1;
7711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
7713 }
7714#endif
7715
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007716#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7717 force_redraw_this = force_redraw_next;
7718 force_redraw_next = FALSE;
7719#endif
7720
7721 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722#ifdef FEAT_MBYTE
7723 || (mbyte_cells == 2
7724 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7725 || (enc_dbcs == DBCS_JPNU
7726 && c == 0x8e
7727 && ScreenLines2[off] != ptr[1])
7728 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007729 && (ScreenLinesUC[off] !=
7730 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7731 || (ScreenLinesUC[off] != 0
7732 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733#endif
7734 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007735 || exmode_active;
7736
7737 if (need_redraw
7738#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7739 || force_redraw_this
7740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 )
7742 {
7743#if defined(FEAT_GUI) || defined(UNIX)
7744 /* The bold trick makes a single row of pixels appear in the next
7745 * character. When a bold character is removed, the next
7746 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007747 * and for some xterms. */
7748 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749# ifdef FEAT_GUI
7750 gui.in_use
7751# endif
7752# if defined(FEAT_GUI) && defined(UNIX)
7753 ||
7754# endif
7755# ifdef UNIX
7756 term_is_xterm
7757# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007758 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007760 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007762 if (n > HL_ALL)
7763 n = syn_attr2attr(n);
7764 if (n & HL_BOLD)
7765 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
7767#endif
7768#ifdef FEAT_MBYTE
7769 /* When at the end of the text and overwriting a two-cell
7770 * character with a one-cell character, need to clear the next
7771 * cell. Also when overwriting the left halve of a two-cell char
7772 * with the right halve of a two-cell char. Do this only once
7773 * (mb_off2cells() may return 2 on the right halve). */
7774 if (clear_next_cell)
7775 clear_next_cell = FALSE;
7776 else if (has_mbyte
7777 && (len < 0 ? ptr[mbyte_blen] == NUL
7778 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007779 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007781 && (*mb_off2cells)(off, max_off) == 1
7782 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 clear_next_cell = TRUE;
7784
7785 /* Make sure we never leave a second byte of a double-byte behind,
7786 * it confuses mb_off2cells(). */
7787 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007788 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007790 && (*mb_off2cells)(off, max_off) == 1
7791 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 ScreenLines[off + mbyte_blen] = 0;
7793#endif
7794 ScreenLines[off] = c;
7795 ScreenAttrs[off] = attr;
7796#ifdef FEAT_MBYTE
7797 if (enc_utf8)
7798 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007799 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 ScreenLinesUC[off] = 0;
7801 else
7802 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007803 int i;
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007806 for (i = 0; i < Screen_mco; ++i)
7807 {
7808 ScreenLinesC[i][off] = u8cc[i];
7809 if (u8cc[i] == 0)
7810 break;
7811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 }
7813 if (mbyte_cells == 2)
7814 {
7815 ScreenLines[off + 1] = 0;
7816 ScreenAttrs[off + 1] = attr;
7817 }
7818 screen_char(off, row, col);
7819 }
7820 else if (mbyte_cells == 2)
7821 {
7822 ScreenLines[off + 1] = ptr[1];
7823 ScreenAttrs[off + 1] = attr;
7824 screen_char_2(off, row, col);
7825 }
7826 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7827 {
7828 ScreenLines2[off] = ptr[1];
7829 screen_char(off, row, col);
7830 }
7831 else
7832#endif
7833 screen_char(off, row, col);
7834 }
7835#ifdef FEAT_MBYTE
7836 if (has_mbyte)
7837 {
7838 off += mbyte_cells;
7839 col += mbyte_cells;
7840 ptr += mbyte_blen;
7841 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007842 {
7843 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007845 len = -1;
7846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848 else
7849#endif
7850 {
7851 ++off;
7852 ++col;
7853 ++ptr;
7854 }
7855 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007856
7857#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7858 /* If we detected the next character needs to be redrawn, but the text
7859 * doesn't extend up to there, update the character here. */
7860 if (force_redraw_next && col < screen_Columns)
7861 {
7862# ifdef FEAT_MBYTE
7863 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7864 screen_char_2(off, row, col);
7865 else
7866# endif
7867 screen_char(off, row, col);
7868 }
7869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870}
7871
7872#ifdef FEAT_SEARCH_EXTRA
7873/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007874 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 */
7876 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007877start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
7879 if (p_hls && !no_hlsearch)
7880 {
7881 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007882 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007883# ifdef FEAT_RELTIME
7884 /* Set the time limit to 'redrawtime'. */
7885 profile_setlimit(p_rdt, &search_hl.tm);
7886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 }
7888}
7889
7890/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007891 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 */
7893 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007894end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
7896 if (search_hl.rm.regprog != NULL)
7897 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007898 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 search_hl.rm.regprog = NULL;
7900 }
7901}
7902
7903/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007904 * Init for calling prepare_search_hl().
7905 */
7906 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007907init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007908{
7909 matchitem_T *cur;
7910
7911 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7912 * match */
7913 cur = wp->w_match_head;
7914 while (cur != NULL)
7915 {
7916 cur->hl.rm = cur->match;
7917 if (cur->hlg_id == 0)
7918 cur->hl.attr = 0;
7919 else
7920 cur->hl.attr = syn_id2attr(cur->hlg_id);
7921 cur->hl.buf = wp->w_buffer;
7922 cur->hl.lnum = 0;
7923 cur->hl.first_lnum = 0;
7924# ifdef FEAT_RELTIME
7925 /* Set the time limit to 'redrawtime'. */
7926 profile_setlimit(p_rdt, &(cur->hl.tm));
7927# endif
7928 cur = cur->next;
7929 }
7930 search_hl.buf = wp->w_buffer;
7931 search_hl.lnum = 0;
7932 search_hl.first_lnum = 0;
7933 /* time limit is set at the toplevel, for all windows */
7934}
7935
7936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 * Advance to the match in window "wp" line "lnum" or past it.
7938 */
7939 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007940prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007942 matchitem_T *cur; /* points to the match list */
7943 match_T *shl; /* points to search_hl or a match */
7944 int shl_flag; /* flag to indicate whether search_hl
7945 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007946 int pos_inprogress; /* marks that position match search is
7947 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 int n;
7949
7950 /*
7951 * When using a multi-line pattern, start searching at the top
7952 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007953 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007955 cur = wp->w_match_head;
7956 shl_flag = FALSE;
7957 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007959 if (shl_flag == FALSE)
7960 {
7961 shl = &search_hl;
7962 shl_flag = TRUE;
7963 }
7964 else
7965 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 if (shl->rm.regprog != NULL
7967 && shl->lnum == 0
7968 && re_multiline(shl->rm.regprog))
7969 {
7970 if (shl->first_lnum == 0)
7971 {
7972# ifdef FEAT_FOLDING
7973 for (shl->first_lnum = lnum;
7974 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7975 if (hasFoldingWin(wp, shl->first_lnum - 1,
7976 NULL, NULL, TRUE, NULL))
7977 break;
7978# else
7979 shl->first_lnum = wp->w_topline;
7980# endif
7981 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982 if (cur != NULL)
7983 cur->pos.cur = 0;
7984 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007986 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7987 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007989 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7990 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991 pos_inprogress = cur == NULL || cur->pos.cur == 0
7992 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (shl->lnum != 0)
7994 {
7995 shl->first_lnum = shl->lnum
7996 + shl->rm.endpos[0].lnum
7997 - shl->rm.startpos[0].lnum;
7998 n = shl->rm.endpos[0].col;
7999 }
8000 else
8001 {
8002 ++shl->first_lnum;
8003 n = 0;
8004 }
8005 }
8006 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008007 if (shl != &search_hl && cur != NULL)
8008 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 }
8010}
8011
8012/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008013 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 * Uses shl->buf.
8015 * Sets shl->lnum and shl->rm contents.
8016 * Note: Assumes a previous match is always before "lnum", unless
8017 * shl->lnum is zero.
8018 * Careful: Any pointers for buffer lines will become invalid.
8019 */
8020 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008021next_search_hl(
8022 win_T *win,
8023 match_T *shl, /* points to search_hl or a match */
8024 linenr_T lnum,
8025 colnr_T mincol, /* minimal column for a match */
8026 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
8028 linenr_T l;
8029 colnr_T matchcol;
8030 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008031 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008033 // for :{range}s/pat only highlight inside the range
8034 if (lnum < search_first_line || lnum > search_last_line)
8035 {
8036 shl->lnum = 0;
8037 return;
8038 }
8039
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 if (shl->lnum != 0)
8041 {
8042 /* Check for three situations:
8043 * 1. If the "lnum" is below a previous match, start a new search.
8044 * 2. If the previous match includes "mincol", use it.
8045 * 3. Continue after the previous match.
8046 */
8047 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8048 if (lnum > l)
8049 shl->lnum = 0;
8050 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8051 return;
8052 }
8053
8054 /*
8055 * Repeat searching for a match until one is found that includes "mincol"
8056 * or none is found in this line.
8057 */
8058 called_emsg = FALSE;
8059 for (;;)
8060 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008061#ifdef FEAT_RELTIME
8062 /* Stop searching after passing the time limit. */
8063 if (profile_passed_limit(&(shl->tm)))
8064 {
8065 shl->lnum = 0; /* no match found in time */
8066 break;
8067 }
8068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 /* Three situations:
8070 * 1. No useful previous match: search from start of line.
8071 * 2. Not Vi compatible or empty match: continue at next character.
8072 * Break the loop if this is beyond the end of the line.
8073 * 3. Vi compatible searching: continue at end of previous match.
8074 */
8075 if (shl->lnum == 0)
8076 matchcol = 0;
8077 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8078 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008079 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008081 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008082
8083 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008084 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008085 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008087 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 shl->lnum = 0;
8089 break;
8090 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008091#ifdef FEAT_MBYTE
8092 if (has_mbyte)
8093 matchcol += mb_ptr2len(ml);
8094 else
8095#endif
8096 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 }
8098 else
8099 matchcol = shl->rm.endpos[0].col;
8100
8101 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008102 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008104 /* Remember whether shl->rm is using a copy of the regprog in
8105 * cur->match. */
8106 int regprog_is_copy = (shl != &search_hl && cur != NULL
8107 && shl == &cur->hl
8108 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008109 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008110
Bram Moolenaarb3414592014-06-17 17:48:32 +02008111 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8112 matchcol,
8113#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008114 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008115#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008116 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008117#endif
8118 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008119 /* Copy the regprog, in case it got freed and recompiled. */
8120 if (regprog_is_copy)
8121 cur->match.regprog = cur->hl.rm.regprog;
8122
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008123 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008124 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008125 /* Error while handling regexp: stop using this regexp. */
8126 if (shl == &search_hl)
8127 {
8128 /* don't free regprog in the match list, it's a copy */
8129 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008130 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008131 }
8132 shl->rm.regprog = NULL;
8133 shl->lnum = 0;
8134 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8135 message */
8136 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008137 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008138 }
8139 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008140 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008141 else
8142 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 if (nmatched == 0)
8144 {
8145 shl->lnum = 0; /* no match found */
8146 break;
8147 }
8148 if (shl->rm.startpos[0].lnum > 0
8149 || shl->rm.startpos[0].col >= mincol
8150 || nmatched > 1
8151 || shl->rm.endpos[0].col > mincol)
8152 {
8153 shl->lnum += shl->rm.startpos[0].lnum;
8154 break; /* useful match found */
8155 }
8156 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008157
8158 // Restore called_emsg for assert_fails().
8159 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161
Bram Moolenaar85077472016-10-16 14:35:48 +02008162/*
8163 * If there is a match fill "shl" and return one.
8164 * Return zero otherwise.
8165 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008166 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008167next_search_hl_pos(
8168 match_T *shl, /* points to a match */
8169 linenr_T lnum,
8170 posmatch_T *posmatch, /* match positions */
8171 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008172{
8173 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008174 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008175
Bram Moolenaarb3414592014-06-17 17:48:32 +02008176 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8177 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008178 llpos_T *pos = &posmatch->pos[i];
8179
8180 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008181 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008182 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008183 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008184 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008185 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008186 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008187 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008188 /* if this match comes before the one at "found" then swap
8189 * them */
8190 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008191 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008192 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008193
Bram Moolenaar85077472016-10-16 14:35:48 +02008194 *pos = posmatch->pos[found];
8195 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008196 }
8197 }
8198 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008199 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008200 }
8201 }
8202 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008203 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008204 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008205 colnr_T start = posmatch->pos[found].col == 0
8206 ? 0 : posmatch->pos[found].col - 1;
8207 colnr_T end = posmatch->pos[found].col == 0
8208 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008209
Bram Moolenaar85077472016-10-16 14:35:48 +02008210 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008211 shl->rm.startpos[0].lnum = 0;
8212 shl->rm.startpos[0].col = start;
8213 shl->rm.endpos[0].lnum = 0;
8214 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008215 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008216 posmatch->cur = found + 1;
8217 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008218 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008219 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008220}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008221#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008222
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008224screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225{
8226 attrentry_T *aep = NULL;
8227
8228 screen_attr = attr;
8229 if (full_screen
8230#ifdef WIN3264
8231 && termcap_active
8232#endif
8233 )
8234 {
8235#ifdef FEAT_GUI
8236 if (gui.in_use)
8237 {
8238 char buf[20];
8239
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008240 /* The GUI handles this internally. */
8241 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 OUT_STR(buf);
8243 }
8244 else
8245#endif
8246 {
8247 if (attr > HL_ALL) /* special HL attr. */
8248 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008249 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 aep = syn_cterm_attr2entry(attr);
8251 else
8252 aep = syn_term_attr2entry(attr);
8253 if (aep == NULL) /* did ":syntax clear" */
8254 attr = 0;
8255 else
8256 attr = aep->ae_attr;
8257 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008258 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008260 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008261#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008262 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8263 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8264 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008265#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008266 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008267 /* If the Normal FG color has BOLD attribute and the new HL
8268 * has a FG color defined, clear BOLD. */
8269 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008270 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008272 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008273 out_str(T_UCS);
8274 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008275 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8276 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008278 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008280 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008282 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008283 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284
8285 /*
8286 * Output the color or start string after bold etc., in case the
8287 * bold etc. override the color setting.
8288 */
8289 if (aep != NULL)
8290 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008291#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008292 /* When 'termguicolors' is set but fg or bg is unset,
8293 * fall back to the cterm colors. This helps for SpellBad,
8294 * where the GUI uses a red undercurl. */
8295 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008297 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008298 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008299 }
8300 else
8301#endif
8302 if (t_colors > 1)
8303 {
8304 if (aep->ae_u.cterm.fg_color)
8305 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8306 }
8307#ifdef FEAT_TERMGUICOLORS
8308 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8309 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008310 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008311 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 }
8313 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008314#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008315 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008317 if (aep->ae_u.cterm.bg_color)
8318 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8319 }
8320
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008321 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008322 {
8323 if (aep->ae_u.term.start != NULL)
8324 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 }
8326 }
8327 }
8328 }
8329}
8330
8331 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008332screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
8334 int do_ME = FALSE; /* output T_ME code */
8335
8336 if (screen_attr != 0
8337#ifdef WIN3264
8338 && termcap_active
8339#endif
8340 )
8341 {
8342#ifdef FEAT_GUI
8343 if (gui.in_use)
8344 {
8345 char buf[20];
8346
8347 /* use internal GUI code */
8348 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8349 OUT_STR(buf);
8350 }
8351 else
8352#endif
8353 {
8354 if (screen_attr > HL_ALL) /* special HL attr. */
8355 {
8356 attrentry_T *aep;
8357
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008358 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {
8360 /*
8361 * Assume that t_me restores the original colors!
8362 */
8363 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008364 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008365#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008366 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8367 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8368 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008369#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008370 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008371#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008372 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8373 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8374 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008375#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008376 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 do_ME = TRUE;
8378 }
8379 else
8380 {
8381 aep = syn_term_attr2entry(screen_attr);
8382 if (aep != NULL && aep->ae_u.term.stop != NULL)
8383 {
8384 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8385 do_ME = TRUE;
8386 else
8387 out_str(aep->ae_u.term.stop);
8388 }
8389 }
8390 if (aep == NULL) /* did ":syntax clear" */
8391 screen_attr = 0;
8392 else
8393 screen_attr = aep->ae_attr;
8394 }
8395
8396 /*
8397 * Often all ending-codes are equal to T_ME. Avoid outputting the
8398 * same sequence several times.
8399 */
8400 if (screen_attr & HL_STANDOUT)
8401 {
8402 if (STRCMP(T_SE, T_ME) == 0)
8403 do_ME = TRUE;
8404 else
8405 out_str(T_SE);
8406 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008407 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008408 {
8409 if (STRCMP(T_UCE, T_ME) == 0)
8410 do_ME = TRUE;
8411 else
8412 out_str(T_UCE);
8413 }
8414 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008415 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {
8417 if (STRCMP(T_UE, T_ME) == 0)
8418 do_ME = TRUE;
8419 else
8420 out_str(T_UE);
8421 }
8422 if (screen_attr & HL_ITALIC)
8423 {
8424 if (STRCMP(T_CZR, T_ME) == 0)
8425 do_ME = TRUE;
8426 else
8427 out_str(T_CZR);
8428 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008429 if (screen_attr & HL_STRIKETHROUGH)
8430 {
8431 if (STRCMP(T_STE, T_ME) == 0)
8432 do_ME = TRUE;
8433 else
8434 out_str(T_STE);
8435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8437 out_str(T_ME);
8438
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008439#ifdef FEAT_TERMGUICOLORS
8440 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008442 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008443 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008444 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008445 term_bg_rgb_color(cterm_normal_bg_gui_color);
8446 }
8447 else
8448#endif
8449 {
8450 if (t_colors > 1)
8451 {
8452 /* set Normal cterm colors */
8453 if (cterm_normal_fg_color != 0)
8454 term_fg_color(cterm_normal_fg_color - 1);
8455 if (cterm_normal_bg_color != 0)
8456 term_bg_color(cterm_normal_bg_color - 1);
8457 if (cterm_normal_fg_bold)
8458 out_str(T_MD);
8459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 }
8461 }
8462 }
8463 screen_attr = 0;
8464}
8465
8466/*
8467 * Reset the colors for a cterm. Used when leaving Vim.
8468 * The machine specific code may override this again.
8469 */
8470 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008471reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008473 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 {
8475 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008476#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008477 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8478 || cterm_normal_bg_gui_color != INVALCOLOR)
8479 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008480#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {
8484 out_str(T_OP);
8485 screen_attr = -1;
8486 }
8487 if (cterm_normal_fg_bold)
8488 {
8489 out_str(T_ME);
8490 screen_attr = -1;
8491 }
8492 }
8493}
8494
8495/*
8496 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8497 * using the attributes from ScreenAttrs["off"].
8498 */
8499 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008500screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502 int attr;
8503
8504 /* Check for illegal values, just in case (could happen just after
8505 * resizing). */
8506 if (row >= screen_Rows || col >= screen_Columns)
8507 return;
8508
Bram Moolenaar494838a2015-02-10 19:20:37 +01008509 /* Outputting a character in the last cell on the screen may scroll the
8510 * screen up. Only do it when the "xn" termcap property is set, otherwise
8511 * mark the character invalid (update it when scrolled up). */
8512 if (*T_XN == NUL
8513 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514#ifdef FEAT_RIGHTLEFT
8515 /* account for first command-line character in rightleft mode */
8516 && !cmdmsg_rl
8517#endif
8518 )
8519 {
8520 ScreenAttrs[off] = (sattr_T)-1;
8521 return;
8522 }
8523
8524 /*
8525 * Stop highlighting first, so it's easier to move the cursor.
8526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (screen_char_attr != 0)
8528 attr = screen_char_attr;
8529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 attr = ScreenAttrs[off];
8531 if (screen_attr != attr)
8532 screen_stop_highlight();
8533
8534 windgoto(row, col);
8535
8536 if (screen_attr != attr)
8537 screen_start_highlight(attr);
8538
8539#ifdef FEAT_MBYTE
8540 if (enc_utf8 && ScreenLinesUC[off] != 0)
8541 {
8542 char_u buf[MB_MAXBYTES + 1];
8543
Bram Moolenaarcb070082016-04-02 22:14:51 +02008544 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008545 {
8546 if (*p_ambw == 'd'
8547# ifdef FEAT_GUI
8548 && !gui.in_use
8549# endif
8550 )
8551 {
8552 /* Clear the two screen cells. If the character is actually
8553 * single width it won't change the second cell. */
8554 out_str((char_u *)" ");
8555 term_windgoto(row, col);
8556 }
8557 /* not sure where the cursor is after drawing the ambiguous width
8558 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008559 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008560 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008561 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008563
8564 /* Convert the UTF-8 character to bytes and write it. */
8565 buf[utfc_char2bytes(off, buf)] = NUL;
8566 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 }
8568 else
8569#endif
8570 {
8571#ifdef FEAT_MBYTE
8572 out_flush_check();
8573#endif
8574 out_char(ScreenLines[off]);
8575#ifdef FEAT_MBYTE
8576 /* double-byte character in single-width cell */
8577 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8578 out_char(ScreenLines2[off]);
8579#endif
8580 }
8581
8582 screen_cur_col++;
8583}
8584
8585#ifdef FEAT_MBYTE
8586
8587/*
8588 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8589 * on the screen at position 'row' and 'col'.
8590 * The attributes of the first byte is used for all. This is required to
8591 * output the two bytes of a double-byte character with nothing in between.
8592 */
8593 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008594screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595{
8596 /* Check for illegal values (could be wrong when screen was resized). */
8597 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8598 return;
8599
8600 /* Outputting the last character on the screen may scrollup the screen.
8601 * Don't to it! Mark the character invalid (update it when scrolled up) */
8602 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8603 {
8604 ScreenAttrs[off] = (sattr_T)-1;
8605 return;
8606 }
8607
8608 /* Output the first byte normally (positions the cursor), then write the
8609 * second byte directly. */
8610 screen_char(off, row, col);
8611 out_char(ScreenLines[off + 1]);
8612 ++screen_cur_col;
8613}
8614#endif
8615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616/*
8617 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8618 * This uses the contents of ScreenLines[] and doesn't change it.
8619 */
8620 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008621screen_draw_rectangle(
8622 int row,
8623 int col,
8624 int height,
8625 int width,
8626 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 int r, c;
8629 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008630#ifdef FEAT_MBYTE
8631 int max_off;
8632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008634 /* Can't use ScreenLines unless initialized */
8635 if (ScreenLines == NULL)
8636 return;
8637
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 if (invert)
8639 screen_char_attr = HL_INVERSE;
8640 for (r = row; r < row + height; ++r)
8641 {
8642 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008643#ifdef FEAT_MBYTE
8644 max_off = off + screen_Columns;
8645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 for (c = col; c < col + width; ++c)
8647 {
8648#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008649 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {
8651 screen_char_2(off + c, r, c);
8652 ++c;
8653 }
8654 else
8655#endif
8656 {
8657 screen_char(off + c, r, c);
8658#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008659 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 ++c;
8661#endif
8662 }
8663 }
8664 }
8665 screen_char_attr = 0;
8666}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668/*
8669 * Redraw the characters for a vertically split window.
8670 */
8671 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008672redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
8674 int col;
8675 int width;
8676
8677# ifdef FEAT_CLIPBOARD
8678 clip_may_clear_selection(row, end - 1);
8679# endif
8680
8681 if (wp == NULL)
8682 {
8683 col = 0;
8684 width = Columns;
8685 }
8686 else
8687 {
8688 col = wp->w_wincol;
8689 width = wp->w_width;
8690 }
8691 screen_draw_rectangle(row, col, end - row, width, FALSE);
8692}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008694 static void
8695space_to_screenline(int off, int attr)
8696{
8697 ScreenLines[off] = ' ';
8698 ScreenAttrs[off] = attr;
8699# ifdef FEAT_MBYTE
8700 if (enc_utf8)
8701 ScreenLinesUC[off] = 0;
8702# endif
8703}
8704
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705/*
8706 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8707 * with character 'c1' in first column followed by 'c2' in the other columns.
8708 * Use attributes 'attr'.
8709 */
8710 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008711screen_fill(
8712 int start_row,
8713 int end_row,
8714 int start_col,
8715 int end_col,
8716 int c1,
8717 int c2,
8718 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
8720 int row;
8721 int col;
8722 int off;
8723 int end_off;
8724 int did_delete;
8725 int c;
8726 int norm_term;
8727#if defined(FEAT_GUI) || defined(UNIX)
8728 int force_next = FALSE;
8729#endif
8730
8731 if (end_row > screen_Rows) /* safety check */
8732 end_row = screen_Rows;
8733 if (end_col > screen_Columns) /* safety check */
8734 end_col = screen_Columns;
8735 if (ScreenLines == NULL
8736 || start_row >= end_row
8737 || start_col >= end_col) /* nothing to do */
8738 return;
8739
8740 /* it's a "normal" terminal when not in a GUI or cterm */
8741 norm_term = (
8742#ifdef FEAT_GUI
8743 !gui.in_use &&
8744#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008745 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 for (row = start_row; row < end_row; ++row)
8747 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008748#ifdef FEAT_MBYTE
8749 if (has_mbyte
8750# ifdef FEAT_GUI
8751 && !gui.in_use
8752# endif
8753 )
8754 {
8755 /* When drawing over the right halve of a double-wide char clear
8756 * out the left halve. When drawing over the left halve of a
8757 * double wide-char clear out the right halve. Only needed in a
8758 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008759 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008760 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008761 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008762 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008763 }
8764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 /*
8766 * Try to use delete-line termcap code, when no attributes or in a
8767 * "normal" terminal, where a bold/italic space is just a
8768 * space.
8769 */
8770 did_delete = FALSE;
8771 if (c2 == ' '
8772 && end_col == Columns
8773 && can_clear(T_CE)
8774 && (attr == 0
8775 || (norm_term
8776 && attr <= HL_ALL
8777 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8778 {
8779 /*
8780 * check if we really need to clear something
8781 */
8782 col = start_col;
8783 if (c1 != ' ') /* don't clear first char */
8784 ++col;
8785
8786 off = LineOffset[row] + col;
8787 end_off = LineOffset[row] + end_col;
8788
8789 /* skip blanks (used often, keep it fast!) */
8790#ifdef FEAT_MBYTE
8791 if (enc_utf8)
8792 while (off < end_off && ScreenLines[off] == ' '
8793 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8794 ++off;
8795 else
8796#endif
8797 while (off < end_off && ScreenLines[off] == ' '
8798 && ScreenAttrs[off] == 0)
8799 ++off;
8800 if (off < end_off) /* something to be cleared */
8801 {
8802 col = off - LineOffset[row];
8803 screen_stop_highlight();
8804 term_windgoto(row, col);/* clear rest of this screen line */
8805 out_str(T_CE);
8806 screen_start(); /* don't know where cursor is now */
8807 col = end_col - col;
8808 while (col--) /* clear chars in ScreenLines */
8809 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008810 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 ++off;
8812 }
8813 }
8814 did_delete = TRUE; /* the chars are cleared now */
8815 }
8816
8817 off = LineOffset[row] + start_col;
8818 c = c1;
8819 for (col = start_col; col < end_col; ++col)
8820 {
8821 if (ScreenLines[off] != c
8822#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008823 || (enc_utf8 && (int)ScreenLinesUC[off]
8824 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825#endif
8826 || ScreenAttrs[off] != attr
8827#if defined(FEAT_GUI) || defined(UNIX)
8828 || force_next
8829#endif
8830 )
8831 {
8832#if defined(FEAT_GUI) || defined(UNIX)
8833 /* The bold trick may make a single row of pixels appear in
8834 * the next character. When a bold character is removed, the
8835 * next character should be redrawn too. This happens for our
8836 * own GUI and for some xterms. */
8837 if (
8838# ifdef FEAT_GUI
8839 gui.in_use
8840# endif
8841# if defined(FEAT_GUI) && defined(UNIX)
8842 ||
8843# endif
8844# ifdef UNIX
8845 term_is_xterm
8846# endif
8847 )
8848 {
8849 if (ScreenLines[off] != ' '
8850 && (ScreenAttrs[off] > HL_ALL
8851 || ScreenAttrs[off] & HL_BOLD))
8852 force_next = TRUE;
8853 else
8854 force_next = FALSE;
8855 }
8856#endif
8857 ScreenLines[off] = c;
8858#ifdef FEAT_MBYTE
8859 if (enc_utf8)
8860 {
8861 if (c >= 0x80)
8862 {
8863 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008864 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 }
8866 else
8867 ScreenLinesUC[off] = 0;
8868 }
8869#endif
8870 ScreenAttrs[off] = attr;
8871 if (!did_delete || c != ' ')
8872 screen_char(off, row, col);
8873 }
8874 ++off;
8875 if (col == start_col)
8876 {
8877 if (did_delete)
8878 break;
8879 c = c2;
8880 }
8881 }
8882 if (end_col == Columns)
8883 LineWraps[row] = FALSE;
8884 if (row == Rows - 1) /* overwritten the command line */
8885 {
8886 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008887 if (start_col == 0 && end_col == Columns
8888 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008890 if (start_col == 0)
8891 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
8893 }
8894}
8895
8896/*
8897 * Check if there should be a delay. Used before clearing or redrawing the
8898 * screen or the command line.
8899 */
8900 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008901check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8904 && !did_wait_return
8905 && emsg_silent == 0)
8906 {
8907 out_flush();
8908 ui_delay(1000L, TRUE);
8909 emsg_on_display = FALSE;
8910 if (check_msg_scroll)
8911 msg_scroll = FALSE;
8912 }
8913}
8914
8915/*
8916 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008917 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 * Returns TRUE if there is a valid screen to write to.
8919 * Returns FALSE when starting up and screen not initialized yet.
8920 */
8921 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008922screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008924 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 return (ScreenLines != NULL);
8926}
8927
8928/*
8929 * Resize the shell to Rows and Columns.
8930 * Allocate ScreenLines[] and associated items.
8931 *
8932 * There may be some time between setting Rows and Columns and (re)allocating
8933 * ScreenLines[]. This happens when starting up and when (manually) changing
8934 * the shell size. Always use screen_Rows and screen_Columns to access items
8935 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8936 * final size of the shell is needed.
8937 */
8938 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008939screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940{
8941 int new_row, old_row;
8942#ifdef FEAT_GUI
8943 int old_Rows;
8944#endif
8945 win_T *wp;
8946 int outofmem = FALSE;
8947 int len;
8948 schar_T *new_ScreenLines;
8949#ifdef FEAT_MBYTE
8950 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008951 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008953 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954#endif
8955 sattr_T *new_ScreenAttrs;
8956 unsigned *new_LineOffset;
8957 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008958 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008959 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008961 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008962 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008964retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 /*
8966 * Allocation of the screen buffers is done only when the size changes and
8967 * when Rows and Columns have been set and we have started doing full
8968 * screen stuff.
8969 */
8970 if ((ScreenLines != NULL
8971 && Rows == screen_Rows
8972 && Columns == screen_Columns
8973#ifdef FEAT_MBYTE
8974 && enc_utf8 == (ScreenLinesUC != NULL)
8975 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008976 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977#endif
8978 )
8979 || Rows == 0
8980 || Columns == 0
8981 || (!full_screen && ScreenLines == NULL))
8982 return;
8983
8984 /*
8985 * It's possible that we produce an out-of-memory message below, which
8986 * will cause this function to be called again. To break the loop, just
8987 * return here.
8988 */
8989 if (entered)
8990 return;
8991 entered = TRUE;
8992
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008993 /*
8994 * Note that the window sizes are updated before reallocating the arrays,
8995 * thus we must not redraw here!
8996 */
8997 ++RedrawingDisabled;
8998
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 win_new_shellsize(); /* fit the windows in the new sized shell */
9000
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 comp_col(); /* recompute columns for shown command and ruler */
9002
9003 /*
9004 * We're changing the size of the screen.
9005 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9006 * - Move lines from the old arrays into the new arrays, clear extra
9007 * lines (unless the screen is going to be cleared).
9008 * - Free the old arrays.
9009 *
9010 * If anything fails, make ScreenLines NULL, so we don't do anything!
9011 * Continuing with the old ScreenLines may result in a crash, because the
9012 * size is wrong.
9013 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009014 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009016 if (aucmd_win != NULL)
9017 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018
9019 new_ScreenLines = (schar_T *)lalloc((long_u)(
9020 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9021#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01009022 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 if (enc_utf8)
9024 {
9025 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
9026 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009027 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01009028 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
9030 }
9031 if (enc_dbcs == DBCS_JPNU)
9032 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9033 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9034#endif
9035 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9036 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9037 new_LineOffset = (unsigned *)lalloc((long_u)(
9038 Rows * sizeof(unsigned)), FALSE);
9039 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009040 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009042 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 {
9044 if (win_alloc_lines(wp) == FAIL)
9045 {
9046 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009047 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 }
9049 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009050 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9051 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009052 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009053give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009055#ifdef FEAT_MBYTE
9056 for (i = 0; i < p_mco; ++i)
9057 if (new_ScreenLinesC[i] == NULL)
9058 break;
9059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 if (new_ScreenLines == NULL
9061#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009062 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9064#endif
9065 || new_ScreenAttrs == NULL
9066 || new_LineOffset == NULL
9067 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009068 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 || outofmem)
9070 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009071 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009072 {
9073 /* guess the size */
9074 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9075
9076 /* Remember we did this to avoid getting outofmem messages over
9077 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009078 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009079 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009080 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009082 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009083 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009084 VIM_CLEAR(new_ScreenLinesC[i]);
9085 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009087 VIM_CLEAR(new_ScreenAttrs);
9088 VIM_CLEAR(new_LineOffset);
9089 VIM_CLEAR(new_LineWraps);
9090 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 }
9092 else
9093 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009094 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009095
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 for (new_row = 0; new_row < Rows; ++new_row)
9097 {
9098 new_LineOffset[new_row] = new_row * Columns;
9099 new_LineWraps[new_row] = FALSE;
9100
9101 /*
9102 * If the screen is not going to be cleared, copy as much as
9103 * possible from the old screen to the new one and clear the rest
9104 * (used when resizing the window at the "--more--" prompt or when
9105 * executing an external command, for the GUI).
9106 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009107 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 {
9109 (void)vim_memset(new_ScreenLines + new_row * Columns,
9110 ' ', (size_t)Columns * sizeof(schar_T));
9111#ifdef FEAT_MBYTE
9112 if (enc_utf8)
9113 {
9114 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9115 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009116 for (i = 0; i < p_mco; ++i)
9117 (void)vim_memset(new_ScreenLinesC[i]
9118 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 0, (size_t)Columns * sizeof(u8char_T));
9120 }
9121 if (enc_dbcs == DBCS_JPNU)
9122 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9123 0, (size_t)Columns * sizeof(schar_T));
9124#endif
9125 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9126 0, (size_t)Columns * sizeof(sattr_T));
9127 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009128 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 {
9130 if (screen_Columns < Columns)
9131 len = screen_Columns;
9132 else
9133 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009134#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009135 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009136 * may be invalid now. Also when p_mco changes. */
9137 if (!(enc_utf8 && ScreenLinesUC == NULL)
9138 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009139#endif
9140 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9141 ScreenLines + LineOffset[old_row],
9142 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009144 if (enc_utf8 && ScreenLinesUC != NULL
9145 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 {
9147 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9148 ScreenLinesUC + LineOffset[old_row],
9149 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009150 for (i = 0; i < p_mco; ++i)
9151 mch_memmove(new_ScreenLinesC[i]
9152 + new_LineOffset[new_row],
9153 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 (size_t)len * sizeof(u8char_T));
9155 }
9156 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9157 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9158 ScreenLines2 + LineOffset[old_row],
9159 (size_t)len * sizeof(schar_T));
9160#endif
9161 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9162 ScreenAttrs + LineOffset[old_row],
9163 (size_t)len * sizeof(sattr_T));
9164 }
9165 }
9166 }
9167 /* Use the last line of the screen for the current line. */
9168 current_ScreenLine = new_ScreenLines + Rows * Columns;
9169 }
9170
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009171 free_screenlines();
9172
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 ScreenLines = new_ScreenLines;
9174#ifdef FEAT_MBYTE
9175 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009176 for (i = 0; i < p_mco; ++i)
9177 ScreenLinesC[i] = new_ScreenLinesC[i];
9178 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 ScreenLines2 = new_ScreenLines2;
9180#endif
9181 ScreenAttrs = new_ScreenAttrs;
9182 LineOffset = new_LineOffset;
9183 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009184 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
9186 /* It's important that screen_Rows and screen_Columns reflect the actual
9187 * size of ScreenLines[]. Set them before calling anything. */
9188#ifdef FEAT_GUI
9189 old_Rows = screen_Rows;
9190#endif
9191 screen_Rows = Rows;
9192 screen_Columns = Columns;
9193
9194 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009195 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 screenclear2();
9197
9198#ifdef FEAT_GUI
9199 else if (gui.in_use
9200 && !gui.starting
9201 && ScreenLines != NULL
9202 && old_Rows != Rows)
9203 {
9204 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9205 /*
9206 * Adjust the position of the cursor, for when executing an external
9207 * command.
9208 */
9209 if (msg_row >= Rows) /* Rows got smaller */
9210 msg_row = Rows - 1; /* put cursor at last row */
9211 else if (Rows > old_Rows) /* Rows got bigger */
9212 msg_row += Rows - old_Rows; /* put cursor in same place */
9213 if (msg_col >= Columns) /* Columns got smaller */
9214 msg_col = Columns - 1; /* put cursor at last column */
9215 }
9216#endif
9217
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009219 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009220
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009221 /*
9222 * Do not apply autocommands more than 3 times to avoid an endless loop
9223 * in case applying autocommands always changes Rows or Columns.
9224 */
9225 if (starting == 0 && ++retry_count <= 3)
9226 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009227 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009228 /* In rare cases, autocommands may have altered Rows or Columns,
9229 * jump back to check if we need to allocate the screen again. */
9230 goto retry;
9231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232}
9233
9234 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009235free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009236{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009237#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009238 int i;
9239
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009240 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009241 for (i = 0; i < Screen_mco; ++i)
9242 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009243 vim_free(ScreenLines2);
9244#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009245 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009246 vim_free(ScreenAttrs);
9247 vim_free(LineOffset);
9248 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009249 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009250}
9251
9252 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009253screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254{
9255 check_for_delay(FALSE);
9256 screenalloc(FALSE); /* allocate screen buffers if size changed */
9257 screenclear2(); /* clear the screen */
9258}
9259
9260 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009261screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262{
9263 int i;
9264
9265 if (starting == NO_SCREEN || ScreenLines == NULL
9266#ifdef FEAT_GUI
9267 || (gui.in_use && gui.starting)
9268#endif
9269 )
9270 return;
9271
9272#ifdef FEAT_GUI
9273 if (!gui.in_use)
9274#endif
9275 screen_attr = -1; /* force setting the Normal colors */
9276 screen_stop_highlight(); /* don't want highlighting here */
9277
9278#ifdef FEAT_CLIPBOARD
9279 /* disable selection without redrawing it */
9280 clip_scroll_selection(9999);
9281#endif
9282
9283 /* blank out ScreenLines */
9284 for (i = 0; i < Rows; ++i)
9285 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009286 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 LineWraps[i] = FALSE;
9288 }
9289
9290 if (can_clear(T_CL))
9291 {
9292 out_str(T_CL); /* clear the display */
9293 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009294 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 }
9296 else
9297 {
9298 /* can't clear the screen, mark all chars with invalid attributes */
9299 for (i = 0; i < Rows; ++i)
9300 lineinvalid(LineOffset[i], (int)Columns);
9301 clear_cmdline = TRUE;
9302 }
9303
9304 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9305
9306 win_rest_invalid(firstwin);
9307 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009308 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 if (must_redraw == CLEAR) /* no need to clear again */
9310 must_redraw = NOT_VALID;
9311 compute_cmdrow();
9312 msg_row = cmdline_row; /* put cursor on last line for messages */
9313 msg_col = 0;
9314 screen_start(); /* don't know where cursor is now */
9315 msg_scrolled = 0; /* can't scroll back */
9316 msg_didany = FALSE;
9317 msg_didout = FALSE;
9318}
9319
9320/*
9321 * Clear one line in ScreenLines.
9322 */
9323 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009324lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325{
9326 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9327#ifdef FEAT_MBYTE
9328 if (enc_utf8)
9329 (void)vim_memset(ScreenLinesUC + off, 0,
9330 (size_t)width * sizeof(u8char_T));
9331#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009332 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333}
9334
9335/*
9336 * Mark one line in ScreenLines invalid by setting the attributes to an
9337 * invalid value.
9338 */
9339 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009340lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341{
9342 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9343}
9344
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345/*
9346 * Copy part of a Screenline for vertically split window "wp".
9347 */
9348 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009349linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350{
9351 unsigned off_to = LineOffset[to] + wp->w_wincol;
9352 unsigned off_from = LineOffset[from] + wp->w_wincol;
9353
9354 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9355 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009356#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 if (enc_utf8)
9358 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009359 int i;
9360
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9362 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009363 for (i = 0; i < p_mco; ++i)
9364 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9365 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 }
9367 if (enc_dbcs == DBCS_JPNU)
9368 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9369 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9372 wp->w_width * sizeof(sattr_T));
9373}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374
9375/*
9376 * Return TRUE if clearing with term string "p" would work.
9377 * It can't work when the string is empty or it won't set the right background.
9378 */
9379 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009380can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 return (*p != NUL && (t_colors <= 1
9383#ifdef FEAT_GUI
9384 || gui.in_use
9385#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009386#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009387 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009388 || (!p_tgc && cterm_normal_bg_color == 0)
9389#else
9390 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009391#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009392 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393}
9394
9395/*
9396 * Reset cursor position. Use whenever cursor was moved because of outputting
9397 * something directly to the screen (shell commands) or a terminal control
9398 * code.
9399 */
9400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009401screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402{
9403 screen_cur_row = screen_cur_col = 9999;
9404}
9405
9406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 * Move the cursor to position "row","col" in the screen.
9408 * This tries to find the most efficient way to move, minimizing the number of
9409 * characters sent to the terminal.
9410 */
9411 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009412windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009414 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 int i;
9416 int plan;
9417 int cost;
9418 int wouldbe_col;
9419 int noinvcurs;
9420 char_u *bs;
9421 int goto_cost;
9422 int attr;
9423
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009424#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9426
9427#define PLAN_LE 1
9428#define PLAN_CR 2
9429#define PLAN_NL 3
9430#define PLAN_WRITE 4
9431 /* Can't use ScreenLines unless initialized */
9432 if (ScreenLines == NULL)
9433 return;
9434
9435 if (col != screen_cur_col || row != screen_cur_row)
9436 {
9437 /* Check for valid position. */
9438 if (row < 0) /* window without text lines? */
9439 row = 0;
9440 if (row >= screen_Rows)
9441 row = screen_Rows - 1;
9442 if (col >= screen_Columns)
9443 col = screen_Columns - 1;
9444
9445 /* check if no cursor movement is allowed in highlight mode */
9446 if (screen_attr && *T_MS == NUL)
9447 noinvcurs = HIGHL_COST;
9448 else
9449 noinvcurs = 0;
9450 goto_cost = GOTO_COST + noinvcurs;
9451
9452 /*
9453 * Plan how to do the positioning:
9454 * 1. Use CR to move it to column 0, same row.
9455 * 2. Use T_LE to move it a few columns to the left.
9456 * 3. Use NL to move a few lines down, column 0.
9457 * 4. Move a few columns to the right with T_ND or by writing chars.
9458 *
9459 * Don't do this if the cursor went beyond the last column, the cursor
9460 * position is unknown then (some terminals wrap, some don't )
9461 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009462 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 * characters to move the cursor to the right.
9464 */
9465 if (row >= screen_cur_row && screen_cur_col < Columns)
9466 {
9467 /*
9468 * If the cursor is in the same row, bigger col, we can use CR
9469 * or T_LE.
9470 */
9471 bs = NULL; /* init for GCC */
9472 attr = screen_attr;
9473 if (row == screen_cur_row && col < screen_cur_col)
9474 {
9475 /* "le" is preferred over "bc", because "bc" is obsolete */
9476 if (*T_LE)
9477 bs = T_LE; /* "cursor left" */
9478 else
9479 bs = T_BC; /* "backspace character (old) */
9480 if (*bs)
9481 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9482 else
9483 cost = 999;
9484 if (col + 1 < cost) /* using CR is less characters */
9485 {
9486 plan = PLAN_CR;
9487 wouldbe_col = 0;
9488 cost = 1; /* CR is just one character */
9489 }
9490 else
9491 {
9492 plan = PLAN_LE;
9493 wouldbe_col = col;
9494 }
9495 if (noinvcurs) /* will stop highlighting */
9496 {
9497 cost += noinvcurs;
9498 attr = 0;
9499 }
9500 }
9501
9502 /*
9503 * If the cursor is above where we want to be, we can use CR LF.
9504 */
9505 else if (row > screen_cur_row)
9506 {
9507 plan = PLAN_NL;
9508 wouldbe_col = 0;
9509 cost = (row - screen_cur_row) * 2; /* CR LF */
9510 if (noinvcurs) /* will stop highlighting */
9511 {
9512 cost += noinvcurs;
9513 attr = 0;
9514 }
9515 }
9516
9517 /*
9518 * If the cursor is in the same row, smaller col, just use write.
9519 */
9520 else
9521 {
9522 plan = PLAN_WRITE;
9523 wouldbe_col = screen_cur_col;
9524 cost = 0;
9525 }
9526
9527 /*
9528 * Check if any characters that need to be written have the
9529 * correct attributes. Also avoid UTF-8 characters.
9530 */
9531 i = col - wouldbe_col;
9532 if (i > 0)
9533 cost += i;
9534 if (cost < goto_cost && i > 0)
9535 {
9536 /*
9537 * Check if the attributes are correct without additionally
9538 * stopping highlighting.
9539 */
9540 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9541 while (i && *p++ == attr)
9542 --i;
9543 if (i != 0)
9544 {
9545 /*
9546 * Try if it works when highlighting is stopped here.
9547 */
9548 if (*--p == 0)
9549 {
9550 cost += noinvcurs;
9551 while (i && *p++ == 0)
9552 --i;
9553 }
9554 if (i != 0)
9555 cost = 999; /* different attributes, don't do it */
9556 }
9557#ifdef FEAT_MBYTE
9558 if (enc_utf8)
9559 {
9560 /* Don't use an UTF-8 char for positioning, it's slow. */
9561 for (i = wouldbe_col; i < col; ++i)
9562 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9563 {
9564 cost = 999;
9565 break;
9566 }
9567 }
9568#endif
9569 }
9570
9571 /*
9572 * We can do it without term_windgoto()!
9573 */
9574 if (cost < goto_cost)
9575 {
9576 if (plan == PLAN_LE)
9577 {
9578 if (noinvcurs)
9579 screen_stop_highlight();
9580 while (screen_cur_col > col)
9581 {
9582 out_str(bs);
9583 --screen_cur_col;
9584 }
9585 }
9586 else if (plan == PLAN_CR)
9587 {
9588 if (noinvcurs)
9589 screen_stop_highlight();
9590 out_char('\r');
9591 screen_cur_col = 0;
9592 }
9593 else if (plan == PLAN_NL)
9594 {
9595 if (noinvcurs)
9596 screen_stop_highlight();
9597 while (screen_cur_row < row)
9598 {
9599 out_char('\n');
9600 ++screen_cur_row;
9601 }
9602 screen_cur_col = 0;
9603 }
9604
9605 i = col - screen_cur_col;
9606 if (i > 0)
9607 {
9608 /*
9609 * Use cursor-right if it's one character only. Avoids
9610 * removing a line of pixels from the last bold char, when
9611 * using the bold trick in the GUI.
9612 */
9613 if (T_ND[0] != NUL && T_ND[1] == NUL)
9614 {
9615 while (i-- > 0)
9616 out_char(*T_ND);
9617 }
9618 else
9619 {
9620 int off;
9621
9622 off = LineOffset[row] + screen_cur_col;
9623 while (i-- > 0)
9624 {
9625 if (ScreenAttrs[off] != screen_attr)
9626 screen_stop_highlight();
9627#ifdef FEAT_MBYTE
9628 out_flush_check();
9629#endif
9630 out_char(ScreenLines[off]);
9631#ifdef FEAT_MBYTE
9632 if (enc_dbcs == DBCS_JPNU
9633 && ScreenLines[off] == 0x8e)
9634 out_char(ScreenLines2[off]);
9635#endif
9636 ++off;
9637 }
9638 }
9639 }
9640 }
9641 }
9642 else
9643 cost = 999;
9644
9645 if (cost >= goto_cost)
9646 {
9647 if (noinvcurs)
9648 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009649 if (row == screen_cur_row && (col > screen_cur_col)
9650 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 term_cursor_right(col - screen_cur_col);
9652 else
9653 term_windgoto(row, col);
9654 }
9655 screen_cur_row = row;
9656 screen_cur_col = col;
9657 }
9658}
9659
9660/*
9661 * Set cursor to its position in the current window.
9662 */
9663 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009664setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009666 setcursor_mayforce(FALSE);
9667}
9668
9669/*
9670 * Set cursor to its position in the current window.
9671 * When "force" is TRUE also when not redrawing.
9672 */
9673 void
9674setcursor_mayforce(int force)
9675{
9676 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 {
9678 validate_cursor();
9679 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009680 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009682 /* With 'rightleft' set and the cursor on a double-wide
9683 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009684 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009686 (has_mbyte
9687 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9688 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689# endif
9690 1)) :
9691#endif
9692 curwin->w_wcol));
9693 }
9694}
9695
9696
9697/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009698 * Insert 'line_count' lines at 'row' in window 'wp'.
9699 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9700 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 * scrolling.
9702 * Returns FAIL if the lines are not inserted, OK for success.
9703 */
9704 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009705win_ins_lines(
9706 win_T *wp,
9707 int row,
9708 int line_count,
9709 int invalid,
9710 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
9712 int did_delete;
9713 int nextrow;
9714 int lastrow;
9715 int retval;
9716
9717 if (invalid)
9718 wp->w_lines_valid = 0;
9719
9720 if (wp->w_height < 5)
9721 return FAIL;
9722
9723 if (line_count > wp->w_height - row)
9724 line_count = wp->w_height - row;
9725
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009726 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 if (retval != MAYBE)
9728 return retval;
9729
9730 /*
9731 * If there is a next window or a status line, we first try to delete the
9732 * lines at the bottom to avoid messing what is after the window.
9733 * If this fails and there are following windows, don't do anything to avoid
9734 * messing up those windows, better just redraw.
9735 */
9736 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 if (wp->w_next != NULL || wp->w_status_height)
9738 {
9739 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009740 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 did_delete = TRUE;
9742 else if (wp->w_next)
9743 return FAIL;
9744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 /*
9746 * if no lines deleted, blank the lines that will end up below the window
9747 */
9748 if (!did_delete)
9749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009752 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 lastrow = nextrow + line_count;
9754 if (lastrow > Rows)
9755 lastrow = Rows;
9756 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009757 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 ' ', ' ', 0);
9759 }
9760
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009761 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 == FAIL)
9763 {
9764 /* deletion will have messed up other windows */
9765 if (did_delete)
9766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 win_rest_invalid(W_NEXT(wp));
9769 }
9770 return FAIL;
9771 }
9772
9773 return OK;
9774}
9775
9776/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009777 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9779 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9780 * scrolling
9781 * Return OK for success, FAIL if the lines are not deleted.
9782 */
9783 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009784win_del_lines(
9785 win_T *wp,
9786 int row,
9787 int line_count,
9788 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009789 int mayclear,
9790 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
9792 int retval;
9793
9794 if (invalid)
9795 wp->w_lines_valid = 0;
9796
9797 if (line_count > wp->w_height - row)
9798 line_count = wp->w_height - row;
9799
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009800 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 if (retval != MAYBE)
9802 return retval;
9803
9804 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009805 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 return FAIL;
9807
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 /*
9809 * If there are windows or status lines below, try to put them at the
9810 * correct place. If we can't do that, they have to be redrawn.
9811 */
9812 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9813 {
9814 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009815 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 {
9817 wp->w_redr_status = TRUE;
9818 win_rest_invalid(wp->w_next);
9819 }
9820 }
9821 /*
9822 * If this is the last window and there is no status line, redraw the
9823 * command line later.
9824 */
9825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 redraw_cmdline = TRUE;
9827 return OK;
9828}
9829
9830/*
9831 * Common code for win_ins_lines() and win_del_lines().
9832 * Returns OK or FAIL when the work has been done.
9833 * Returns MAYBE when not finished yet.
9834 */
9835 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009836win_do_lines(
9837 win_T *wp,
9838 int row,
9839 int line_count,
9840 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009841 int del,
9842 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843{
9844 int retval;
9845
9846 if (!redrawing() || line_count <= 0)
9847 return FAIL;
9848
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009849 /* When inserting lines would result in loss of command output, just redraw
9850 * the lines. */
9851 if (no_win_do_lines_ins && !del)
9852 return FAIL;
9853
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009855 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009857 if (!no_win_do_lines_ins)
9858 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 return FAIL;
9860 }
9861
9862 /*
9863 * Delete all remaining lines
9864 */
9865 if (row + line_count >= wp->w_height)
9866 {
9867 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009868 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 ' ', ' ', 0);
9870 return OK;
9871 }
9872
9873 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009874 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009876 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009878 if (!no_win_do_lines_ins)
9879 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880
9881 /*
9882 * If the terminal can set a scroll region, use that.
9883 * Always do this in a vertically split window. This will redraw from
9884 * ScreenLines[] when t_CV isn't defined. That's faster than using
9885 * win_line().
9886 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009887 * a character in the lower right corner of the scroll region may cause a
9888 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009890 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 scroll_region_set(wp, row);
9894 if (del)
9895 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009896 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 else
9898 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009899 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 scroll_region_reset();
9902 return retval;
9903 }
9904
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9906 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907
9908 return MAYBE;
9909}
9910
9911/*
9912 * window 'wp' and everything after it is messed up, mark it for redraw
9913 */
9914 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009915win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 {
9919 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 wp->w_redr_status = TRUE;
9921 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 }
9923 redraw_cmdline = TRUE;
9924}
9925
9926/*
9927 * The rest of the routines in this file perform screen manipulations. The
9928 * given operation is performed physically on the screen. The corresponding
9929 * change is also made to the internal screen image. In this way, the editor
9930 * anticipates the effect of editing changes on the appearance of the screen.
9931 * That way, when we call screenupdate a complete redraw isn't usually
9932 * necessary. Another advantage is that we can keep adding code to anticipate
9933 * screen changes, and in the meantime, everything still works.
9934 */
9935
9936/*
9937 * types for inserting or deleting lines
9938 */
9939#define USE_T_CAL 1
9940#define USE_T_CDL 2
9941#define USE_T_AL 3
9942#define USE_T_CE 4
9943#define USE_T_DL 5
9944#define USE_T_SR 6
9945#define USE_NL 7
9946#define USE_T_CD 8
9947#define USE_REDRAW 9
9948
9949/*
9950 * insert lines on the screen and update ScreenLines[]
9951 * 'end' is the line after the scrolled part. Normally it is Rows.
9952 * When scrolling region used 'off' is the offset from the top for the region.
9953 * 'row' and 'end' are relative to the start of the region.
9954 *
9955 * return FAIL for failure, OK for success.
9956 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009957 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009958screen_ins_lines(
9959 int off,
9960 int row,
9961 int line_count,
9962 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009963 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009964 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
9966 int i;
9967 int j;
9968 unsigned temp;
9969 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009970 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 int type;
9972 int result_empty;
9973 int can_ce = can_clear(T_CE);
9974
9975 /*
9976 * FAIL if
9977 * - there is no valid screen
9978 * - the screen has to be redrawn completely
9979 * - the line count is less than one
9980 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009981 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009983 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9984#ifdef FEAT_CLIPBOARD
9985 || (clip_star.state != SELECT_CLEARED
9986 && redrawing_for_callback > 0)
9987#endif
9988 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 return FAIL;
9990
9991 /*
9992 * There are seven ways to insert lines:
9993 * 0. When in a vertically split window and t_CV isn't set, redraw the
9994 * characters from ScreenLines[].
9995 * 1. Use T_CD (clear to end of display) if it exists and the result of
9996 * the insert is just empty lines
9997 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9998 * present or line_count > 1. It looks better if we do all the inserts
9999 * at once.
10000 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10001 * insert is just empty lines and T_CE is not present or line_count >
10002 * 1.
10003 * 4. Use T_AL (insert line) if it exists.
10004 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10005 * just empty lines.
10006 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10007 * just empty lines.
10008 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10009 * the 'da' flag is not set or we have clear line capability.
10010 * 8. redraw the characters from ScreenLines[].
10011 *
10012 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10013 * the scrollbar for the window. It does have insert line, use that if it
10014 * exists.
10015 */
10016 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10018 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010019 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 type = USE_T_CD;
10021 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10022 type = USE_T_CAL;
10023 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10024 type = USE_T_CDL;
10025 else if (*T_AL != NUL)
10026 type = USE_T_AL;
10027 else if (can_ce && result_empty)
10028 type = USE_T_CE;
10029 else if (*T_DL != NUL && result_empty)
10030 type = USE_T_DL;
10031 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10032 type = USE_T_SR;
10033 else
10034 return FAIL;
10035
10036 /*
10037 * For clearing the lines screen_del_lines() is used. This will also take
10038 * care of t_db if necessary.
10039 */
10040 if (type == USE_T_CD || type == USE_T_CDL ||
10041 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010042 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
10044 /*
10045 * If text is retained below the screen, first clear or delete as many
10046 * lines at the bottom of the window as are about to be inserted so that
10047 * the deleted lines won't later surface during a screen_del_lines.
10048 */
10049 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010050 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051
10052#ifdef FEAT_CLIPBOARD
10053 /* Remove a modeless selection when inserting lines halfway the screen
10054 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010055 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010056 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 else
10058 clip_scroll_selection(-line_count);
10059#endif
10060
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061#ifdef FEAT_GUI
10062 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10063 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010064 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065#endif
10066
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010067 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10068 cursor_col = wp->w_wincol;
10069
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 if (*T_CCS != NUL) /* cursor relative to region */
10071 cursor_row = row;
10072 else
10073 cursor_row = row + off;
10074
10075 /*
10076 * Shift LineOffset[] line_count down to reflect the inserted lines.
10077 * Clear the inserted lines in ScreenLines[].
10078 */
10079 row += off;
10080 end += off;
10081 for (i = 0; i < line_count; ++i)
10082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 if (wp != NULL && wp->w_width != Columns)
10084 {
10085 /* need to copy part of a line */
10086 j = end - 1 - i;
10087 while ((j -= line_count) >= row)
10088 linecopy(j + line_count, j, wp);
10089 j += line_count;
10090 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010091 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10092 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 else
10094 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10095 LineWraps[j] = FALSE;
10096 }
10097 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 {
10099 j = end - 1 - i;
10100 temp = LineOffset[j];
10101 while ((j -= line_count) >= row)
10102 {
10103 LineOffset[j + line_count] = LineOffset[j];
10104 LineWraps[j + line_count] = LineWraps[j];
10105 }
10106 LineOffset[j + line_count] = temp;
10107 LineWraps[j + line_count] = FALSE;
10108 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010109 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 else
10111 lineinvalid(temp, (int)Columns);
10112 }
10113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114
10115 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010116 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010117 if (clear_attr != 0)
10118 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 /* redraw the characters */
10121 if (type == USE_REDRAW)
10122 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010123 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 {
10125 term_append_lines(line_count);
10126 screen_start(); /* don't know where cursor is now */
10127 }
10128 else
10129 {
10130 for (i = 0; i < line_count; i++)
10131 {
10132 if (type == USE_T_AL)
10133 {
10134 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010135 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 out_str(T_AL);
10137 }
10138 else /* type == USE_T_SR */
10139 out_str(T_SR);
10140 screen_start(); /* don't know where cursor is now */
10141 }
10142 }
10143
10144 /*
10145 * With scroll-reverse and 'da' flag set we need to clear the lines that
10146 * have been scrolled down into the region.
10147 */
10148 if (type == USE_T_SR && *T_DA)
10149 {
10150 for (i = 0; i < line_count; ++i)
10151 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010152 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 out_str(T_CE);
10154 screen_start(); /* don't know where cursor is now */
10155 }
10156 }
10157
10158#ifdef FEAT_GUI
10159 gui_can_update_cursor();
10160 if (gui.in_use)
10161 out_flush(); /* always flush after a scroll */
10162#endif
10163 return OK;
10164}
10165
10166/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010167 * Delete lines on the screen and update ScreenLines[].
10168 * "end" is the line after the scrolled part. Normally it is Rows.
10169 * When scrolling region used "off" is the offset from the top for the region.
10170 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 *
10172 * Return OK for success, FAIL if the lines are not deleted.
10173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010175screen_del_lines(
10176 int off,
10177 int row,
10178 int line_count,
10179 int end,
10180 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010181 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010182 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183{
10184 int j;
10185 int i;
10186 unsigned temp;
10187 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010188 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 int cursor_end;
10190 int result_empty; /* result is empty until end of region */
10191 int can_delete; /* deleting line codes can be used */
10192 int type;
10193
10194 /*
10195 * FAIL if
10196 * - there is no valid screen
10197 * - the screen has to be redrawn completely
10198 * - the line count is less than one
10199 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010200 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010202 if (!screen_valid(TRUE) || line_count <= 0
10203 || (!force && line_count > p_ttyscroll)
10204#ifdef FEAT_CLIPBOARD
10205 || (clip_star.state != SELECT_CLEARED
10206 && redrawing_for_callback > 0)
10207#endif
10208 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 return FAIL;
10210
10211 /*
10212 * Check if the rest of the current region will become empty.
10213 */
10214 result_empty = row + line_count >= end;
10215
10216 /*
10217 * We can delete lines only when 'db' flag not set or when 'ce' option
10218 * available.
10219 */
10220 can_delete = (*T_DB == NUL || can_clear(T_CE));
10221
10222 /*
10223 * There are six ways to delete lines:
10224 * 0. When in a vertically split window and t_CV isn't set, redraw the
10225 * characters from ScreenLines[].
10226 * 1. Use T_CD if it exists and the result is empty.
10227 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10228 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10229 * none of the other ways work.
10230 * 4. Use T_CE (erase line) if the result is empty.
10231 * 5. Use T_DL (delete line) if it exists.
10232 * 6. redraw the characters from ScreenLines[].
10233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10235 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010236 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 type = USE_T_CD;
10238#if defined(__BEOS__) && defined(BEOS_DR8)
10239 /*
10240 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10241 * its internal termcap... this works okay for tests which test *T_DB !=
10242 * NUL. It has the disadvantage that the user cannot use any :set t_*
10243 * command to get T_DB (back) to empty_option, only :set term=... will do
10244 * the trick...
10245 * Anyway, this hack will hopefully go away with the next OS release.
10246 * (Olaf Seibert)
10247 */
10248 else if (row == 0 && T_DB == empty_option
10249 && (line_count == 1 || *T_CDL == NUL))
10250#else
10251 else if (row == 0 && (
10252#ifndef AMIGA
10253 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10254 * up, so use delete-line command */
10255 line_count == 1 ||
10256#endif
10257 *T_CDL == NUL))
10258#endif
10259 type = USE_NL;
10260 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10261 type = USE_T_CDL;
10262 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010263 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 type = USE_T_CE;
10265 else if (*T_DL != NUL && can_delete)
10266 type = USE_T_DL;
10267 else if (*T_CDL != NUL && can_delete)
10268 type = USE_T_CDL;
10269 else
10270 return FAIL;
10271
10272#ifdef FEAT_CLIPBOARD
10273 /* Remove a modeless selection when deleting lines halfway the screen or
10274 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010275 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010276 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 else
10278 clip_scroll_selection(line_count);
10279#endif
10280
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#ifdef FEAT_GUI
10282 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10283 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010284 gui_dont_update_cursor(gui.cursor_row >= row + off
10285 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286#endif
10287
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010288 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10289 cursor_col = wp->w_wincol;
10290
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 if (*T_CCS != NUL) /* cursor relative to region */
10292 {
10293 cursor_row = row;
10294 cursor_end = end;
10295 }
10296 else
10297 {
10298 cursor_row = row + off;
10299 cursor_end = end + off;
10300 }
10301
10302 /*
10303 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10304 * Clear the inserted lines in ScreenLines[].
10305 */
10306 row += off;
10307 end += off;
10308 for (i = 0; i < line_count; ++i)
10309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 if (wp != NULL && wp->w_width != Columns)
10311 {
10312 /* need to copy part of a line */
10313 j = row + i;
10314 while ((j += line_count) <= end - 1)
10315 linecopy(j - line_count, j, wp);
10316 j -= line_count;
10317 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010318 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10319 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 else
10321 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10322 LineWraps[j] = FALSE;
10323 }
10324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 {
10326 /* whole width, moving the line pointers is faster */
10327 j = row + i;
10328 temp = LineOffset[j];
10329 while ((j += line_count) <= end - 1)
10330 {
10331 LineOffset[j - line_count] = LineOffset[j];
10332 LineWraps[j - line_count] = LineWraps[j];
10333 }
10334 LineOffset[j - line_count] = temp;
10335 LineWraps[j - line_count] = FALSE;
10336 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010337 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 else
10339 lineinvalid(temp, (int)Columns);
10340 }
10341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010343 if (screen_attr != clear_attr)
10344 screen_stop_highlight();
10345 if (clear_attr != 0)
10346 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 /* redraw the characters */
10349 if (type == USE_REDRAW)
10350 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010351 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010353 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 out_str(T_CD);
10355 screen_start(); /* don't know where cursor is now */
10356 }
10357 else if (type == USE_T_CDL)
10358 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010359 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 term_delete_lines(line_count);
10361 screen_start(); /* don't know where cursor is now */
10362 }
10363 /*
10364 * Deleting lines at top of the screen or scroll region: Just scroll
10365 * the whole screen (scroll region) up by outputting newlines on the
10366 * last line.
10367 */
10368 else if (type == USE_NL)
10369 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010370 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 for (i = line_count; --i >= 0; )
10372 out_char('\n'); /* cursor will remain on same line */
10373 }
10374 else
10375 {
10376 for (i = line_count; --i >= 0; )
10377 {
10378 if (type == USE_T_DL)
10379 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010380 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 out_str(T_DL); /* delete a line */
10382 }
10383 else /* type == USE_T_CE */
10384 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010385 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 out_str(T_CE); /* erase a line */
10387 }
10388 screen_start(); /* don't know where cursor is now */
10389 }
10390 }
10391
10392 /*
10393 * If the 'db' flag is set, we need to clear the lines that have been
10394 * scrolled up at the bottom of the region.
10395 */
10396 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10397 {
10398 for (i = line_count; i > 0; --i)
10399 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010400 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 out_str(T_CE); /* erase a line */
10402 screen_start(); /* don't know where cursor is now */
10403 }
10404 }
10405
10406#ifdef FEAT_GUI
10407 gui_can_update_cursor();
10408 if (gui.in_use)
10409 out_flush(); /* always flush after a scroll */
10410#endif
10411
10412 return OK;
10413}
10414
10415/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010416 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 *
10418 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10419 * If clear_cmdline is FALSE there may be a message there that needs to be
10420 * cleared only if a mode is shown.
10421 * Return the length of the message (0 if no message).
10422 */
10423 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010424showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425{
10426 int need_clear;
10427 int length = 0;
10428 int do_mode;
10429 int attr;
10430 int nwr_save;
10431#ifdef FEAT_INS_EXPAND
10432 int sub_attr;
10433#endif
10434
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010435 do_mode = ((p_smd && msg_silent == 0)
10436 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010437 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010438 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010439 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 {
10441 /*
10442 * Don't show mode right now, when not redrawing or inside a mapping.
10443 * Call char_avail() only when we are going to show something, because
10444 * it takes a bit of time.
10445 */
10446 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10447 {
10448 redraw_cmdline = TRUE; /* show mode later */
10449 return 0;
10450 }
10451
10452 nwr_save = need_wait_return;
10453
10454 /* wait a bit before overwriting an important message */
10455 check_for_delay(FALSE);
10456
10457 /* if the cmdline is more than one line high, erase top lines */
10458 need_clear = clear_cmdline;
10459 if (clear_cmdline && cmdline_row < Rows - 1)
10460 msg_clr_cmdline(); /* will reset clear_cmdline */
10461
10462 /* Position on the last line in the window, column 0 */
10463 msg_pos_mode();
10464 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010465 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 if (do_mode)
10467 {
10468 MSG_PUTS_ATTR("--", attr);
10469#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010470 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010471# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010472 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010473# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010474 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010475# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010476 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010477# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 MSG_PUTS_ATTR(" IM", attr);
10479# else
10480 MSG_PUTS_ATTR(" XIM", attr);
10481# endif
10482#endif
10483#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10484 if (gui.in_use)
10485 {
10486 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010487 {
10488 /* HANGUL */
10489 if (enc_utf8)
10490 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10491 else
10492 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 }
10495#endif
10496#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010497 /* CTRL-X in Insert mode */
10498 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 {
10500 /* These messages can get long, avoid a wrap in a narrow
10501 * window. Prefer showing edit_submode_extra. */
10502 length = (Rows - msg_row) * Columns - 3;
10503 if (edit_submode_extra != NULL)
10504 length -= vim_strsize(edit_submode_extra);
10505 if (length > 0)
10506 {
10507 if (edit_submode_pre != NULL)
10508 length -= vim_strsize(edit_submode_pre);
10509 if (length - vim_strsize(edit_submode) > 0)
10510 {
10511 if (edit_submode_pre != NULL)
10512 msg_puts_attr(edit_submode_pre, attr);
10513 msg_puts_attr(edit_submode, attr);
10514 }
10515 if (edit_submode_extra != NULL)
10516 {
10517 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10518 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010519 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 else
10521 sub_attr = attr;
10522 msg_puts_attr(edit_submode_extra, sub_attr);
10523 }
10524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 }
10526 else
10527#endif
10528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 if (State & VREPLACE_FLAG)
10530 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010531 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10533 else if (State & INSERT)
10534 {
10535#ifdef FEAT_RIGHTLEFT
10536 if (p_ri)
10537 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10538#endif
10539 MSG_PUTS_ATTR(_(" INSERT"), attr);
10540 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010541 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 MSG_PUTS_ATTR(_(" (insert)"), attr);
10543 else if (restart_edit == 'R')
10544 MSG_PUTS_ATTR(_(" (replace)"), attr);
10545 else if (restart_edit == 'V')
10546 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10547#ifdef FEAT_RIGHTLEFT
10548 if (p_hkmap)
10549 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10550# ifdef FEAT_FKMAP
10551 if (p_fkmap)
10552 MSG_PUTS_ATTR(farsi_text_5, attr);
10553# endif
10554#endif
10555#ifdef FEAT_KEYMAP
10556 if (State & LANGMAP)
10557 {
10558# ifdef FEAT_ARABIC
10559 if (curwin->w_p_arab)
10560 MSG_PUTS_ATTR(_(" Arabic"), attr);
10561 else
10562# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010563 if (get_keymap_str(curwin, (char_u *)" (%s)",
10564 NameBuff, MAXPATHL))
10565 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 }
10567#endif
10568 if ((State & INSERT) && p_paste)
10569 MSG_PUTS_ATTR(_(" (paste)"), attr);
10570
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 if (VIsual_active)
10572 {
10573 char *p;
10574
10575 /* Don't concatenate separate words to avoid translation
10576 * problems. */
10577 switch ((VIsual_select ? 4 : 0)
10578 + (VIsual_mode == Ctrl_V) * 2
10579 + (VIsual_mode == 'V'))
10580 {
10581 case 0: p = N_(" VISUAL"); break;
10582 case 1: p = N_(" VISUAL LINE"); break;
10583 case 2: p = N_(" VISUAL BLOCK"); break;
10584 case 4: p = N_(" SELECT"); break;
10585 case 5: p = N_(" SELECT LINE"); break;
10586 default: p = N_(" SELECT BLOCK"); break;
10587 }
10588 MSG_PUTS_ATTR(_(p), attr);
10589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 MSG_PUTS_ATTR(" --", attr);
10591 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010592
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 need_clear = TRUE;
10594 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010595 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596#ifdef FEAT_INS_EXPAND
10597 && edit_submode == NULL /* otherwise it gets too long */
10598#endif
10599 )
10600 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010601 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 need_clear = TRUE;
10603 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010604
10605 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 if (need_clear || clear_cmdline)
10607 msg_clr_eos();
10608 msg_didout = FALSE; /* overwrite this message */
10609 length = msg_col;
10610 msg_col = 0;
10611 need_wait_return = nwr_save; /* never ask for hit-return for this */
10612 }
10613 else if (clear_cmdline && msg_silent == 0)
10614 /* Clear the whole command line. Will reset "clear_cmdline". */
10615 msg_clr_cmdline();
10616
10617#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 /* In Visual mode the size of the selected area must be redrawn. */
10619 if (VIsual_active)
10620 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621
10622 /* If the last window has no status line, the ruler is after the mode
10623 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010624 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010625 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626#endif
10627 redraw_cmdline = FALSE;
10628 clear_cmdline = FALSE;
10629
10630 return length;
10631}
10632
10633/*
10634 * Position for a mode message.
10635 */
10636 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010637msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638{
10639 msg_col = 0;
10640 msg_row = Rows - 1;
10641}
10642
10643/*
10644 * Delete mode message. Used when ESC is typed which is expected to end
10645 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010646 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 */
10648 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010649unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650{
10651 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010652 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 */
10654 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10655 redraw_cmdline = TRUE; /* delete mode later */
10656 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010657 clearmode();
10658}
10659
10660/*
10661 * Clear the mode message.
10662 */
10663 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010664clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010665{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010666 int save_msg_row = msg_row;
10667 int save_msg_col = msg_col;
10668
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010669 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010670 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010671 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010672 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010673
10674 msg_col = save_msg_col;
10675 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676}
10677
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010678 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010679recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010680{
10681 MSG_PUTS_ATTR(_("recording"), attr);
10682 if (!shortmess(SHM_RECORDING))
10683 {
10684 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010685 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010686 MSG_PUTS_ATTR(s, attr);
10687 }
10688}
10689
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010690/*
10691 * Draw the tab pages line at the top of the Vim window.
10692 */
10693 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010694draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010695{
10696 int tabcount = 0;
10697 tabpage_T *tp;
10698 int tabwidth;
10699 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010700 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010701 int attr;
10702 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010703 win_T *cwp;
10704 int wincount;
10705 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010706 int c;
10707 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010708 int attr_sel = HL_ATTR(HLF_TPS);
10709 int attr_nosel = HL_ATTR(HLF_TP);
10710 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010711 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010712 int room;
10713 int use_sep_chars = (t_colors < 8
10714#ifdef FEAT_GUI
10715 && !gui.in_use
10716#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010717#ifdef FEAT_TERMGUICOLORS
10718 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010719#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010720 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010721
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010722 if (ScreenLines == NULL)
10723 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010724 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010725
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010726#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010727 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010728 if (gui_use_tabline())
10729 {
10730 gui_update_tabline();
10731 return;
10732 }
10733#endif
10734
10735 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010736 return;
10737
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010738#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010739
10740 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10741 for (scol = 0; scol < Columns; ++scol)
10742 TabPageIdxs[scol] = 0;
10743
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010744 /* Use the 'tabline' option if it's set. */
10745 if (*p_tal != NUL)
10746 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010747 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010748
10749 /* Check for an error. If there is one we would loop in redrawing the
10750 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010751 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010752 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010753 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010754 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010755 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010756 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010757 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010758 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010759#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010760 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010761 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010762 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010763
Bram Moolenaar238a5642006-02-21 22:12:05 +000010764 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10765 if (tabwidth < 6)
10766 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010767
Bram Moolenaar238a5642006-02-21 22:12:05 +000010768 attr = attr_nosel;
10769 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010770 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010771 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10772 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010773 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010774 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010775
Bram Moolenaar238a5642006-02-21 22:12:05 +000010776 if (tp->tp_topframe == topframe)
10777 attr = attr_sel;
10778 if (use_sep_chars && col > 0)
10779 screen_putchar('|', 0, col++, attr);
10780
10781 if (tp->tp_topframe != topframe)
10782 attr = attr_nosel;
10783
10784 screen_putchar(' ', 0, col++, attr);
10785
10786 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010787 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010788 cwp = curwin;
10789 wp = firstwin;
10790 }
10791 else
10792 {
10793 cwp = tp->tp_curwin;
10794 wp = tp->tp_firstwin;
10795 }
10796
10797 modified = FALSE;
10798 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10799 if (bufIsChanged(wp->w_buffer))
10800 modified = TRUE;
10801 if (modified || wincount > 1)
10802 {
10803 if (wincount > 1)
10804 {
10805 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010806 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010807 if (col + len >= Columns - 3)
10808 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010809 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010810#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010811 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010812#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010813 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010814#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010815 );
10816 col += len;
10817 }
10818 if (modified)
10819 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10820 screen_putchar(' ', 0, col++, attr);
10821 }
10822
10823 room = scol - col + tabwidth - 1;
10824 if (room > 0)
10825 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010826 /* Get buffer name in NameBuff[] */
10827 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010828 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010829 len = vim_strsize(NameBuff);
10830 p = NameBuff;
10831#ifdef FEAT_MBYTE
10832 if (has_mbyte)
10833 while (len > room)
10834 {
10835 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010836 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010837 }
10838 else
10839#endif
10840 if (len > room)
10841 {
10842 p += len - room;
10843 len = room;
10844 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010845 if (len > Columns - col - 1)
10846 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010847
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010848 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010849 col += len;
10850 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010851 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010852
10853 /* Store the tab page number in TabPageIdxs[], so that
10854 * jump_to_mouse() knows where each one is. */
10855 ++tabcount;
10856 while (scol < col)
10857 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010858 }
10859
Bram Moolenaar238a5642006-02-21 22:12:05 +000010860 if (use_sep_chars)
10861 c = '_';
10862 else
10863 c = ' ';
10864 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010865
10866 /* Put an "X" for closing the current tab if there are several. */
10867 if (first_tabpage->tp_next != NULL)
10868 {
10869 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10870 TabPageIdxs[Columns - 1] = -999;
10871 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010872 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010873
10874 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10875 * set. */
10876 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010877}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010878
10879/*
10880 * Get buffer name for "buf" into NameBuff[].
10881 * Takes care of special buffer names and translates special characters.
10882 */
10883 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010884get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010885{
10886 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010887 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010888 else
10889 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10890 trans_characters(NameBuff, MAXPATHL);
10891}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010892
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893/*
10894 * Get the character to use in a status line. Get its attributes in "*attr".
10895 */
10896 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010897fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898{
10899 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010900
10901#ifdef FEAT_TERMINAL
10902 if (bt_terminal(wp->w_buffer))
10903 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010904 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010905 {
10906 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010907 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010908 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010909 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010910 {
10911 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010912 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010913 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010914 }
10915 else
10916#endif
10917 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010919 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 fill = fill_stl;
10921 }
10922 else
10923 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010924 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 fill = fill_stlnc;
10926 }
10927 /* Use fill when there is highlighting, and highlighting of current
10928 * window differs, or the fillchars differ, or this is not the
10929 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010930 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010931 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 || (fill_stl != fill_stlnc)))
10933 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010934 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 return '^';
10936 return '=';
10937}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939/*
10940 * Get the character to use in a separator between vertically split windows.
10941 * Get its attributes in "*attr".
10942 */
10943 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010944fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010946 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 if (*attr == 0 && fill_vert == ' ')
10948 return '|';
10949 else
10950 return fill_vert;
10951}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952
10953/*
10954 * Return TRUE if redrawing should currently be done.
10955 */
10956 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010957redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010959#ifdef FEAT_EVAL
10960 if (disable_redraw_for_testing)
10961 return 0;
10962 else
10963#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010964 return ((!RedrawingDisabled
10965#ifdef FEAT_EVAL
10966 || ignore_redraw_flag_for_testing
10967#endif
10968 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969}
10970
10971/*
10972 * Return TRUE if printing messages should currently be done.
10973 */
10974 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010975messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976{
10977 return (!(p_lz && char_avail() && !KeyTyped));
10978}
10979
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010980#ifdef FEAT_MENU
10981/*
10982 * Draw the window toolbar.
10983 */
10984 static void
10985redraw_win_toolbar(win_T *wp)
10986{
10987 vimmenu_T *menu;
10988 int item_idx = 0;
10989 int item_count = 0;
10990 int col = 0;
10991 int next_col;
10992 int off = (int)(current_ScreenLine - ScreenLines);
10993 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10994 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10995
10996 vim_free(wp->w_winbar_items);
10997 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10998 ++item_count;
10999 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
11000 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
11001
11002 /* TODO: use fewer spaces if there is not enough room */
11003 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011004 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011005 {
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 if (col > 1)
11010 {
11011 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011012 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011013 break;
11014 }
11015
11016 wp->w_winbar_items[item_idx].wb_startcol = col;
11017 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011018 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011019 break;
11020
11021 next_col = text_to_screenline(wp, menu->name, col);
11022 while (col < next_col)
11023 {
11024 ScreenAttrs[off + col] = button_attr;
11025 ++col;
11026 }
11027 wp->w_winbar_items[item_idx].wb_endcol = col;
11028 wp->w_winbar_items[item_idx].wb_menu = menu;
11029 ++item_idx;
11030
Bram Moolenaar02631462017-09-22 15:20:32 +020011031 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011032 break;
11033 space_to_screenline(off + col, button_attr);
11034 ++col;
11035 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011036 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011037 {
11038 space_to_screenline(off + col, fill_attr);
11039 ++col;
11040 }
11041 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11042
Bram Moolenaar02631462017-09-22 15:20:32 +020011043 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11044 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011045}
11046#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011047
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048/*
11049 * Show current status info in ruler and various other places
11050 * If always is FALSE, only show ruler if position has changed.
11051 */
11052 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011053showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054{
11055 if (!always && !redrawing())
11056 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011057#ifdef FEAT_INS_EXPAND
11058 if (pum_visible())
11059 {
11060 /* Don't redraw right now, do it later. */
11061 curwin->w_redr_status = TRUE;
11062 return;
11063 }
11064#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011065#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011066 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011067 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011068 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 else
11071#endif
11072#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011073 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074#endif
11075
11076#ifdef FEAT_TITLE
11077 if (need_maketitle
11078# ifdef FEAT_STL_OPT
11079 || (p_icon && (stl_syntax & STL_IN_ICON))
11080 || (p_title && (stl_syntax & STL_IN_TITLE))
11081# endif
11082 )
11083 maketitle();
11084#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011085 /* Redraw the tab pages line if needed. */
11086 if (redraw_tabline)
11087 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088}
11089
11090#ifdef FEAT_CMDL_INFO
11091 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011092win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011094#define RULER_BUF_LEN 70
11095 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 int row;
11097 int fillchar;
11098 int attr;
11099 int empty_line = FALSE;
11100 colnr_T virtcol;
11101 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011102 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 int this_ru_col;
11105 int off = 0;
11106 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107
11108 /* If 'ruler' off or redrawing disabled, don't do anything */
11109 if (!p_ru)
11110 return;
11111
11112 /*
11113 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11114 * after deleting lines, before cursor.lnum is corrected.
11115 */
11116 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11117 return;
11118
11119#ifdef FEAT_INS_EXPAND
11120 /* Don't draw the ruler while doing insert-completion, it might overwrite
11121 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 if (edit_submode != NULL)
11124 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011125 // Don't draw the ruler when the popup menu is visible, it may overlap.
11126 // Except when the popup menu will be redrawn anyway.
11127 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011128 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129#endif
11130
11131#ifdef FEAT_STL_OPT
11132 if (*p_ruf)
11133 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011134 int save_called_emsg = called_emsg;
11135
11136 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011138 if (called_emsg)
11139 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011140 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011141 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 return;
11143 }
11144#endif
11145
11146 /*
11147 * Check if not in Insert mode and the line is empty (will show "0-1").
11148 */
11149 if (!(State & INSERT)
11150 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11151 empty_line = TRUE;
11152
11153 /*
11154 * Only draw the ruler when something changed.
11155 */
11156 validate_virtcol_win(wp);
11157 if ( redraw_cmdline
11158 || always
11159 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11160 || wp->w_cursor.col != wp->w_ru_cursor.col
11161 || wp->w_virtcol != wp->w_ru_virtcol
11162#ifdef FEAT_VIRTUALEDIT
11163 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11164#endif
11165 || wp->w_topline != wp->w_ru_topline
11166 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11167#ifdef FEAT_DIFF
11168 || wp->w_topfill != wp->w_ru_topfill
11169#endif
11170 || empty_line != wp->w_ru_empty)
11171 {
11172 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 if (wp->w_status_height)
11174 {
11175 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011176 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011177 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011178 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 }
11180 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 {
11182 row = Rows - 1;
11183 fillchar = ' ';
11184 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 width = Columns;
11186 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 }
11188
11189 /* In list mode virtcol needs to be recomputed */
11190 virtcol = wp->w_virtcol;
11191 if (wp->w_p_list && lcs_tab1 == NUL)
11192 {
11193 wp->w_p_list = FALSE;
11194 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11195 wp->w_p_list = TRUE;
11196 }
11197
11198 /*
11199 * Some sprintfs return the length, some return a pointer.
11200 * To avoid portability problems we use strlen() here.
11201 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011202 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11204 ? 0L
11205 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011206 len = STRLEN(buffer);
11207 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11209 (int)virtcol + 1);
11210
11211 /*
11212 * Add a "50%" if there is room for it.
11213 * On the last line, don't print in the last column (scrolls the
11214 * screen up on some terminals).
11215 */
11216 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011217 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 this_ru_col = ru_col - (Columns - width);
11222 if (this_ru_col < 0)
11223 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 /* Never use more than half the window/screen width, leave the other
11225 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011226 if (this_ru_col < (width + 1) / 2)
11227 this_ru_col = (width + 1) / 2;
11228 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011230 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011231 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 {
11233#ifdef FEAT_MBYTE
11234 if (has_mbyte)
11235 i += (*mb_char2bytes)(fillchar, buffer + i);
11236 else
11237#endif
11238 buffer[i++] = fillchar;
11239 ++o;
11240 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011241 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 }
11243 /* Truncate at window boundary. */
11244#ifdef FEAT_MBYTE
11245 if (has_mbyte)
11246 {
11247 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011248 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 {
11250 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011251 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 {
11253 buffer[i] = NUL;
11254 break;
11255 }
11256 }
11257 }
11258 else
11259#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011260 if (this_ru_col + (int)STRLEN(buffer) > width)
11261 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262
Bram Moolenaar4033c552017-09-16 20:54:51 +020011263 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 i = redraw_cmdline;
11265 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011266 this_ru_col + off + (int)STRLEN(buffer),
11267 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 fillchar, fillchar, attr);
11269 /* don't redraw the cmdline because of showing the ruler */
11270 redraw_cmdline = i;
11271 wp->w_ru_cursor = wp->w_cursor;
11272 wp->w_ru_virtcol = wp->w_virtcol;
11273 wp->w_ru_empty = empty_line;
11274 wp->w_ru_topline = wp->w_topline;
11275 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11276#ifdef FEAT_DIFF
11277 wp->w_ru_topfill = wp->w_topfill;
11278#endif
11279 }
11280}
11281#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011282
11283#if defined(FEAT_LINEBREAK) || defined(PROTO)
11284/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011285 * Return the width of the 'number' and 'relativenumber' column.
11286 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011287 * Otherwise it depends on 'numberwidth' and the line count.
11288 */
11289 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011290number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011291{
11292 int n;
11293 linenr_T lnum;
11294
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011295 if (wp->w_p_rnu && !wp->w_p_nu)
11296 /* cursor line shows "0" */
11297 lnum = wp->w_height;
11298 else
11299 /* cursor line shows absolute line number */
11300 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011301
Bram Moolenaar6b314672015-03-20 15:42:10 +010011302 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011303 return wp->w_nrwidth_width;
11304 wp->w_nrwidth_line_count = lnum;
11305
11306 n = 0;
11307 do
11308 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011309 lnum /= 10;
11310 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011311 } while (lnum > 0);
11312
11313 /* 'numberwidth' gives the minimal width plus one */
11314 if (n < wp->w_p_nuw - 1)
11315 n = wp->w_p_nuw - 1;
11316
11317 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011318 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011319 return n;
11320}
11321#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011322
11323/*
11324 * Return the current cursor column. This is the actual position on the
11325 * screen. First column is 0.
11326 */
11327 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011328screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011329{
11330 return screen_cur_col;
11331}
11332
11333/*
11334 * Return the current cursor row. This is the actual position on the screen.
11335 * First row is 0.
11336 */
11337 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011338screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011339{
11340 return screen_cur_row;
11341}