blob: 322efceedba347e9aeeb9412320814c1358a204b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200153static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void win_rest_invalid(win_T *wp);
155static void msg_pos_mode(void);
156static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Redraw the current window later, with update_screen(type).
186 * Set must_redraw only if not already set to a higher value.
187 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
188 */
189 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100190redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
192 redraw_win_later(curwin, type);
193}
194
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_win_later(
197 win_T *wp,
198 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200200 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {
202 wp->w_redr_type = type;
203 if (type >= NOT_VALID)
204 wp->w_lines_valid = 0;
205 if (must_redraw < type) /* must_redraw is the maximum of all windows */
206 must_redraw = type;
207 }
208}
209
210/*
211 * Force a complete redraw later. Also resets the highlighting. To be used
212 * after executing a shell command that messes up the screen.
213 */
214 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100215redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
217 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000218#ifdef FEAT_GUI
219 if (gui.in_use)
220 /* Use a code that will reset gui.highlight_mask in
221 * gui_stop_highlight(). */
222 screen_attr = HL_ALL + 1;
223 else
224#endif
225 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200226 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227}
228
229/*
230 * Mark all windows to be redrawn later.
231 */
232 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100233redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100247redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 redraw_buf_later(curbuf, type);
250}
251
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 win_T *wp;
256
257 FOR_ALL_WINDOWS(wp)
258 {
259 if (wp->w_buffer == buf)
260 redraw_win_later(wp, type);
261 }
262}
263
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200264 void
265redraw_buf_and_status_later(buf_T *buf, int type)
266{
267 win_T *wp;
268
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200269#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200270 if (wild_menu_showing != 0)
271 /* Don't redraw while the command line completion is displayed, it
272 * would disappear. */
273 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200274#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200275 FOR_ALL_WINDOWS(wp)
276 {
277 if (wp->w_buffer == buf)
278 {
279 redraw_win_later(wp, type);
280 wp->w_redr_status = TRUE;
281 }
282 }
283}
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200286 * Redraw as soon as possible. When the command line is not scrolled redraw
287 * right away and restore what was on the command line.
288 * Return a code indicating what happened.
289 */
290 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100291redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292{
293 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 int r;
296 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200297 schar_T *screenline; /* copy from ScreenLines[] */
298 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299#ifdef FEAT_MBYTE
300 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200301 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304#endif
305
306 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200307 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 return ret;
309
310 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 if (screenline == NULL || screenattr == NULL)
317 ret = 2;
318#ifdef FEAT_MBYTE
319 if (enc_utf8)
320 {
321 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 if (screenlineUC == NULL)
324 ret = 2;
325 for (i = 0; i < p_mco; ++i)
326 {
327 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineC[i] == NULL)
330 ret = 2;
331 }
332 }
333 if (enc_dbcs == DBCS_JPNU)
334 {
335 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenline2 == NULL)
338 ret = 2;
339 }
340#endif
341
342 if (ret != 2)
343 {
344 /* Save the text displayed in the command line area. */
345 for (r = 0; r < rows; ++r)
346 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200348 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (size_t)cols * sizeof(schar_T));
350 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200352 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353#ifdef FEAT_MBYTE
354 if (enc_utf8)
355 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenlineC[i] + r * cols,
361 ScreenLinesC[i] + LineOffset[cmdline_row + r],
362 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 }
364 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368#endif
369 }
370
371 update_screen(0);
372 ret = 3;
373
374 if (must_redraw == 0)
375 {
376 int off = (int)(current_ScreenLine - ScreenLines);
377
378 /* Restore the text displayed in the command line area. */
379 for (r = 0; r < rows; ++r)
380 {
381 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenline + r * cols,
383 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200385 screenattr + r * cols,
386 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387#ifdef FEAT_MBYTE
388 if (enc_utf8)
389 {
390 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenlineUC + r * cols,
392 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 for (i = 0; i < p_mco; ++i)
394 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenlineC[i] + r * cols,
396 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 }
398 if (enc_dbcs == DBCS_JPNU)
399 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenline2 + r * cols,
401 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200403 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 ret = 4;
406 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408
409 vim_free(screenline);
410 vim_free(screenattr);
411#ifdef FEAT_MBYTE
412 if (enc_utf8)
413 {
414 vim_free(screenlineUC);
415 for (i = 0; i < p_mco; ++i)
416 vim_free(screenlineC[i]);
417 }
418 if (enc_dbcs == DBCS_JPNU)
419 vim_free(screenline2);
420#endif
421
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200422 /* Show the intro message when appropriate. */
423 maybe_intro_message();
424
425 setcursor();
426
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427 return ret;
428}
429
430/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100431 * Invoked after an asynchronous callback is called.
432 * If an echo command was used the cursor needs to be put back where
433 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200434 * If "call_update_screen" is FALSE don't call update_screen() when at the
435 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100436 */
437 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200438redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200440 ++redrawing_for_callback;
441
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100442 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200443 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100444 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200445 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200446 // Don't redraw when in prompt_for_number().
447 if (cmdline_row > 0)
448 {
449 // Redrawing only works when the screen didn't scroll. Don't clear
450 // wildmenu entries.
451 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200452#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 && call_update_screen)
456 update_screen(0);
457
458 // Redraw in the same position, so that the user can continue
459 // editing the command.
460 redrawcmdline_ex(FALSE);
461 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200463 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100464 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200465 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100467 setcursor();
468 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100469 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100471 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // Don't update the cursor when it is blinking and off to avoid
473 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100474 out_flush_cursor(FALSE, FALSE);
475 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100477 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200478
479 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100480}
481
482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 * Changed something in the current window, at buffer line "lnum", that
484 * requires that line and possibly other lines to be redrawn.
485 * Used when entering/leaving Insert mode with the cursor on a folded line.
486 * Used to remove the "$" from a change command.
487 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
488 * may become invalid and the whole window will have to be redrawn.
489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100491redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200492 win_T *wp,
Bram Moolenaar05540972016-01-30 20:31:25 +0100493 linenr_T lnum,
494 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496#ifdef FEAT_FOLDING
497 int i;
498#endif
499
Bram Moolenaar90a99792018-09-12 21:52:18 +0200500 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
501 wp->w_redraw_top = lnum;
502 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
503 wp->w_redraw_bot = lnum;
504 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
506#ifdef FEAT_FOLDING
507 if (invalid)
508 {
509 /* A w_lines[] entry for this lnum has become invalid. */
Bram Moolenaar90a99792018-09-12 21:52:18 +0200510 i = find_wl_entry(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (i >= 0)
Bram Moolenaar90a99792018-09-12 21:52:18 +0200512 wp->w_lines[i].wl_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 }
514#endif
515}
516
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200517 void
518reset_updating_screen(int may_resize_shell UNUSED)
519{
520 updating_screen = FALSE;
521#ifdef FEAT_GUI
522 if (may_resize_shell)
523 gui_may_resize_shell();
524#endif
525#ifdef FEAT_TERMINAL
526 term_check_channel_closed_recently();
527#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200528
529#ifdef HAVE_DROP_FILE
530 // If handle_drop() was called while updating_screen was TRUE need to
531 // handle the drop now.
532 handle_any_postponed_drop();
533#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200534}
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200537 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100540update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 redraw_curbuf_later(type);
543 update_screen(type);
544}
545
546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * Based on the current value of curwin->w_topline, transfer a screenfull
548 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200549 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200551 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 win_T *wp;
556 static int did_intro = FALSE;
557#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
558 int did_one;
559#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200561 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200562 int gui_cursor_col;
563 int gui_cursor_row;
564#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200565 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000567 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200571 if (type == VALID_NO_UPDATE)
572 {
573 no_update = TRUE;
574 type = 0;
575 }
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (must_redraw)
578 {
579 if (type < must_redraw) /* use maximal type */
580 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000581
582 /* must_redraw is reset here, so that when we run into some weird
583 * reason to redraw while busy redrawing (e.g., asynchronous
584 * scrolling), or update_topline() in win_update() will cause a
585 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 must_redraw = 0;
587 }
588
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200589 /* May need to update w_lines[]. */
590 if (curwin->w_lines_valid == 0 && type < NOT_VALID
591#ifdef FEAT_TERMINAL
592 && !term_do_update_window(curwin)
593#endif
594 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 type = NOT_VALID;
596
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000597 /* Postpone the redrawing when it's not needed and when being called
598 * recursively. */
599 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {
601 redraw_later(type); /* remember type for next time */
602 must_redraw = type;
603 if (type > INVERTED_ALL)
604 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
607
608 updating_screen = TRUE;
609#ifdef FEAT_SYN_HL
610 ++display_tick; /* let syntax code know we're in a next round of
611 * display updating */
612#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200613 if (no_update)
614 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 /*
617 * if the screen was scrolled up when displaying a message, scroll it down
618 */
619 if (msg_scrolled)
620 {
621 clear_cmdline = TRUE;
622 if (msg_scrolled > Rows - 5) /* clearing is faster */
623 type = CLEAR;
624 else if (type != CLEAR)
625 {
626 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200627 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
628 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 type = CLEAR;
630 FOR_ALL_WINDOWS(wp)
631 {
632 if (W_WINROW(wp) < msg_scrolled)
633 {
634 if (W_WINROW(wp) + wp->w_height > msg_scrolled
635 && wp->w_redr_type < REDRAW_TOP
636 && wp->w_lines_valid > 0
637 && wp->w_topline == wp->w_lines[0].wl_lnum)
638 {
639 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
640 wp->w_redr_type = REDRAW_TOP;
641 }
642 else
643 {
644 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200645 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
646 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
649 }
650 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200651 if (!no_update)
652 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000653 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 msg_scrolled = 0;
656 need_wait_return = FALSE;
657 }
658
659 /* reset cmdline_row now (may have been changed temporarily) */
660 compute_cmdrow();
661
662 /* Check for changed highlighting */
663 if (need_highlight_changed)
664 highlight_changed();
665
666 if (type == CLEAR) /* first clear screen */
667 {
668 screenclear(); /* will reset clear_cmdline */
669 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200670 /* must_redraw may be set indirectly, avoid another redraw later */
671 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673
674 if (clear_cmdline) /* going to clear cmdline (done below) */
675 check_for_delay(FALSE);
676
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000677#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200678 /* Force redraw when width of 'number' or 'relativenumber' column
679 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000680 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200681 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
682 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000683 curwin->w_redr_type = NOT_VALID;
684#endif
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 /*
687 * Only start redrawing if there is really something to do.
688 */
689 if (type == INVERTED)
690 update_curswant();
691 if (curwin->w_redr_type < type
692 && !((type == VALID
693 && curwin->w_lines[0].wl_valid
694#ifdef FEAT_DIFF
695 && curwin->w_topfill == curwin->w_old_topfill
696 && curwin->w_botfill == curwin->w_old_botfill
697#endif
698 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000700 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
702 && curwin->w_old_visual_mode == VIsual_mode
703 && (curwin->w_valid & VALID_VIRTCOL)
704 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ))
706 curwin->w_redr_type = type;
707
Bram Moolenaar5a305422006-04-28 22:38:25 +0000708 /* Redraw the tab pages line if needed. */
709 if (redraw_tabline || type >= NOT_VALID)
710 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000711
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifdef FEAT_SYN_HL
713 /*
714 * Correct stored syntax highlighting info for changes in each displayed
715 * buffer. Each buffer must only be done once.
716 */
717 FOR_ALL_WINDOWS(wp)
718 {
719 if (wp->w_buffer->b_mod_set)
720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 win_T *wwp;
722
723 /* Check if we already did this buffer. */
724 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
725 if (wwp->w_buffer == wp->w_buffer)
726 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200727 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 syn_stack_apply_changes(wp->w_buffer);
729 }
730 }
731#endif
732
733 /*
734 * Go from top to bottom through the windows, redrawing the ones that need
735 * it.
736 */
737#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
738 did_one = FALSE;
739#endif
740#ifdef FEAT_SEARCH_EXTRA
741 search_hl.rm.regprog = NULL;
742#endif
743 FOR_ALL_WINDOWS(wp)
744 {
745 if (wp->w_redr_type != 0)
746 {
747 cursor_off();
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 if (!did_one)
750 {
751 did_one = TRUE;
752# ifdef FEAT_SEARCH_EXTRA
753 start_search_hl();
754# endif
755# ifdef FEAT_CLIPBOARD
756 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200757 if (clip_star.available && clip_isautosel_star())
758 clip_update_selection(&clip_star);
759 if (clip_plus.available && clip_isautosel_plus())
760 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761# endif
762#ifdef FEAT_GUI
763 /* Remove the cursor before starting to do anything, because
764 * scrolling may make it difficult to redraw the text under
765 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200766 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 {
768 gui_cursor_col = gui.cursor_col;
769 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200771 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
774 }
775#endif
776 win_update(wp);
777 }
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 /* redraw status line after the window to minimize cursor movement */
780 if (wp->w_redr_status)
781 {
782 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200783 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786#if defined(FEAT_SEARCH_EXTRA)
787 end_search_hl();
788#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100789#ifdef FEAT_INS_EXPAND
790 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200791 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 /* Reset b_mod_set flags. Going through all windows is probably faster
795 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200796 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200799 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /* Clear or redraw the command line. Done last, because scrolling may
802 * mess up the command line. */
803 if (clear_cmdline || redraw_cmdline)
804 showmode();
805
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200806 if (no_update)
807 --no_win_do_lines_ins;
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200810 if (!did_intro)
811 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 did_intro = TRUE;
813
814#ifdef FEAT_GUI
815 /* Redraw the cursor and update the scrollbars when all screen updating is
816 * done. */
817 if (gui.in_use)
818 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200819 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100821 mch_disable_flush();
822 out_flush(); /* required before updating the cursor */
823 mch_enable_flush();
824
Bram Moolenaar144445d2016-07-08 21:41:54 +0200825 /* Put the GUI position where the cursor was, gui_update_cursor()
826 * uses that. */
827 gui.col = gui_cursor_col;
828 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200829# ifdef FEAT_MBYTE
830 gui.col = mb_fix_col(gui.col, gui.row);
831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100833 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200834 screen_cur_col = gui.col;
835 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 else
838 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_scrollbars(FALSE);
840 }
841#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200842 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100845#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
846/*
847 * Prepare for updating one or more windows.
848 * Caller must check for "updating_screen" already set to avoid recursiveness.
849 */
850 static void
851update_prepare(void)
852{
853 cursor_off();
854 updating_screen = TRUE;
855#ifdef FEAT_GUI
856 /* Remove the cursor before starting to do anything, because scrolling may
857 * make it difficult to redraw the text under it. */
858 if (gui.in_use)
859 gui_undraw_cursor();
860#endif
861#ifdef FEAT_SEARCH_EXTRA
862 start_search_hl();
863#endif
864}
865
866/*
867 * Finish updating one or more windows.
868 */
869 static void
870update_finish(void)
871{
872 if (redraw_cmdline)
873 showmode();
874
875# ifdef FEAT_SEARCH_EXTRA
876 end_search_hl();
877# endif
878
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200879 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880
881# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100882 /* Redraw the cursor and update the scrollbars when all screen updating is
883 * done. */
884 if (gui.in_use)
885 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100886 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887 gui_update_scrollbars(FALSE);
888 }
889# endif
890}
891#endif
892
Bram Moolenaar860cae12010-06-05 23:22:07 +0200893#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200894/*
895 * Return TRUE if the cursor line in window "wp" may be concealed, according
896 * to the 'concealcursor' option.
897 */
898 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100899conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900{
901 int c;
902
903 if (*wp->w_p_cocu == NUL)
904 return FALSE;
905 if (get_real_state() & VISUAL)
906 c = 'v';
907 else if (State & INSERT)
908 c = 'i';
909 else if (State & NORMAL)
910 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200911 else if (State & CMDLINE)
912 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913 else
914 return FALSE;
915 return vim_strchr(wp->w_p_cocu, c) != NULL;
916}
917
918/*
919 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
920 */
921 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200922conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200923{
924 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
925 {
926 need_cursor_line_redraw = TRUE;
927 /* Need to recompute cursor column, e.g., when starting Visual mode
928 * without concealing. */
929 curs_columns(TRUE);
930 }
931}
932
Bram Moolenaar860cae12010-06-05 23:22:07 +0200933 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935{
936 int row;
937 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200938#ifdef SYN_TIME_LIMIT
939 proftime_T syntax_tm;
940#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941
Bram Moolenaar908be432016-05-24 10:51:30 +0200942 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100943 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200944 return;
945
Bram Moolenaar860cae12010-06-05 23:22:07 +0200946 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200947 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200949#ifdef SYN_TIME_LIMIT
950 /* Set the time limit to 'redrawtime'. */
951 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200952 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200953#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100954 update_prepare();
955
Bram Moolenaar860cae12010-06-05 23:22:07 +0200956 row = 0;
957 for (j = 0; j < wp->w_lines_valid; ++j)
958 {
959 if (lnum == wp->w_lines[j].wl_lnum)
960 {
961 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200962# ifdef FEAT_SEARCH_EXTRA
963 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200964 start_search_hl();
965 prepare_search_hl(wp, lnum);
966# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200967 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
968 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200969# if defined(FEAT_SEARCH_EXTRA)
970 end_search_hl();
971# endif
972 break;
973 }
974 row += wp->w_lines[j].wl_size;
975 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100976
977 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200978
979#ifdef SYN_TIME_LIMIT
980 syn_set_timeout(NULL);
981#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200982 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200983 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984}
985#endif
986
987#if defined(FEAT_SIGNS) || defined(PROTO)
988 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100989update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 win_T *wp;
992 int doit = FALSE;
993
994# ifdef FEAT_FOLDING
995 win_foldinfo.fi_level = 0;
996# endif
997
998 /* update/delete a specific mark */
999 FOR_ALL_WINDOWS(wp)
1000 {
1001 if (buf != NULL && lnum > 0)
1002 {
1003 if (wp->w_buffer == buf && lnum >= wp->w_topline
1004 && lnum < wp->w_botline)
1005 {
1006 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1007 wp->w_redraw_top = lnum;
1008 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1009 wp->w_redraw_bot = lnum;
1010 redraw_win_later(wp, VALID);
1011 }
1012 }
1013 else
1014 redraw_win_later(wp, VALID);
1015 if (wp->w_redr_type != 0)
1016 doit = TRUE;
1017 }
1018
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001019 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001020 * happening (recursive call), messages on the screen or still starting up.
1021 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001022 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001023 || State == ASKMORE || State == HITRETURN
1024 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001025#ifdef FEAT_GUI
1026 || gui.starting
1027#endif
1028 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 return;
1030
1031 /* update all windows that need updating */
1032 update_prepare();
1033
Bram Moolenaar29323592016-07-24 22:04:11 +02001034 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 if (wp->w_redr_type != 0)
1037 win_update(wp);
1038 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001039 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 update_finish();
1043}
1044#endif
1045
1046
1047#if defined(FEAT_GUI) || defined(PROTO)
1048/*
1049 * Update a single window, its status line and maybe the command line msg.
1050 * Used for the GUI scrollbar.
1051 */
1052 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001053updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001055 /* return if already busy updating */
1056 if (updating_screen)
1057 return;
1058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 update_prepare();
1060
1061#ifdef FEAT_CLIPBOARD
1062 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001063 if (clip_star.available && clip_isautosel_star())
1064 clip_update_selection(&clip_star);
1065 if (clip_plus.available && clip_isautosel_plus())
1066 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001070
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001071 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001072 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001073 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (wp->w_redr_status
1076# ifdef FEAT_CMDL_INFO
1077 || p_ru
1078# endif
1079# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001080 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081# endif
1082 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001083 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
1085 update_finish();
1086}
1087#endif
1088
1089/*
1090 * Update a single window.
1091 *
1092 * This may cause the windows below it also to be redrawn (when clearing the
1093 * screen or scrolling lines).
1094 *
1095 * How the window is redrawn depends on wp->w_redr_type. Each type also
1096 * implies the one below it.
1097 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001098 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1100 * INVERTED redraw the changed part of the Visual area
1101 * INVERTED_ALL redraw the whole Visual area
1102 * VALID 1. scroll up/down to adjust for a changed w_topline
1103 * 2. update lines at the top when scrolled down
1104 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001105 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * b_mod_top and b_mod_bot.
1107 * - if wp->w_redraw_top non-zero, redraw lines between
1108 * wp->w_redraw_top and wp->w_redr_bot.
1109 * - continue redrawing when syntax status is invalid.
1110 * 4. if scrolled up, update lines at the bottom.
1111 * This results in three areas that may need updating:
1112 * top: from first row to top_end (when scrolled down)
1113 * mid: from mid_start to mid_end (update inversion or changed text)
1114 * bot: from bot_start to last row (when scrolled up)
1115 */
1116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001117win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 buf_T *buf = wp->w_buffer;
1120 int type;
1121 int top_end = 0; /* Below last row of the top area that needs
1122 updating. 0 when no top area updating. */
1123 int mid_start = 999;/* first row of the mid area that needs
1124 updating. 999 when no mid area updating. */
1125 int mid_end = 0; /* Below last row of the mid area that needs
1126 updating. 0 when no mid area updating. */
1127 int bot_start = 999;/* first row of the bot area that needs
1128 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int scrolled_down = FALSE; /* TRUE when scrolled down when
1130 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001132 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 int top_to_mod = FALSE; /* redraw above mod_top */
1134#endif
1135
1136 int row; /* current window row to display */
1137 linenr_T lnum; /* current buffer lnum to display */
1138 int idx; /* current index in w_lines[] */
1139 int srow; /* starting row of the current line */
1140
1141 int eof = FALSE; /* if TRUE, we hit the end of the file */
1142 int didline = FALSE; /* if TRUE, we finished the last line */
1143 int i;
1144 long j;
1145 static int recursive = FALSE; /* being called recursively */
1146 int old_botline = wp->w_botline;
1147#ifdef FEAT_FOLDING
1148 long fold_count;
1149#endif
1150#ifdef FEAT_SYN_HL
1151 /* remember what happened to the previous line, to know if
1152 * check_visual_highlight() can be used */
1153#define DID_NONE 1 /* didn't update a line */
1154#define DID_LINE 2 /* updated a normal line */
1155#define DID_FOLD 3 /* updated a folded line */
1156 int did_update = DID_NONE;
1157 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1158#endif
1159 linenr_T mod_top = 0;
1160 linenr_T mod_bot = 0;
1161#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1162 int save_got_int;
1163#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001164#ifdef SYN_TIME_LIMIT
1165 proftime_T syntax_tm;
1166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 type = wp->w_redr_type;
1169
1170 if (type == NOT_VALID)
1171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 wp->w_lines_valid = 0;
1174 }
1175
1176 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001177 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 wp->w_redr_type = 0;
1180 return;
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 /* Window is zero-width: Only need to draw the separator. */
1184 if (wp->w_width == 0)
1185 {
1186 /* draw the vertical separator right of this window */
1187 draw_vsep_win(wp, 0);
1188 wp->w_redr_type = 0;
1189 return;
1190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001192#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001193 // If this window contains a terminal, redraw works completely differently.
1194 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001195 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001196 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001197# ifdef FEAT_MENU
1198 /* Draw the window toolbar, if there is one. */
1199 if (winbar_height(wp) > 0)
1200 redraw_win_toolbar(wp);
1201# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001202 wp->w_redr_type = 0;
1203 return;
1204 }
1205#endif
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001208 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#endif
1210
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001211#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001212 /* Force redraw when width of 'number' or 'relativenumber' column
1213 * changes. */
1214 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001215 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001216 {
1217 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001218 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001219 }
1220 else
1221#endif
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1224 {
1225 /*
1226 * When there are both inserted/deleted lines and specific lines to be
1227 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1228 * everything (only happens when redrawing is off for while).
1229 */
1230 type = NOT_VALID;
1231 }
1232 else
1233 {
1234 /*
1235 * Set mod_top to the first line that needs displaying because of
1236 * changes. Set mod_bot to the first line after the changes.
1237 */
1238 mod_top = wp->w_redraw_top;
1239 if (wp->w_redraw_bot != 0)
1240 mod_bot = wp->w_redraw_bot + 1;
1241 else
1242 mod_bot = 0;
1243 wp->w_redraw_top = 0; /* reset for next time */
1244 wp->w_redraw_bot = 0;
1245 if (buf->b_mod_set)
1246 {
1247 if (mod_top == 0 || mod_top > buf->b_mod_top)
1248 {
1249 mod_top = buf->b_mod_top;
1250#ifdef FEAT_SYN_HL
1251 /* Need to redraw lines above the change that may be included
1252 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001253 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (mod_top < 1)
1257 mod_top = 1;
1258 }
1259#endif
1260 }
1261 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1262 mod_bot = buf->b_mod_bot;
1263
1264#ifdef FEAT_SEARCH_EXTRA
1265 /* When 'hlsearch' is on and using a multi-line search pattern, a
1266 * change in one line may make the Search highlighting in a
1267 * previous line invalid. Simple solution: redraw all visible
1268 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001269 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001271 if (search_hl.rm.regprog != NULL
1272 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001274 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001275 {
1276 cur = wp->w_match_head;
1277 while (cur != NULL)
1278 {
1279 if (cur->match.regprog != NULL
1280 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001281 {
1282 top_to_mod = TRUE;
1283 break;
1284 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001285 cur = cur->next;
1286 }
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
1289 }
1290#ifdef FEAT_FOLDING
1291 if (mod_top != 0 && hasAnyFolding(wp))
1292 {
1293 linenr_T lnumt, lnumb;
1294
1295 /*
1296 * A change in a line can cause lines above it to become folded or
1297 * unfolded. Find the top most buffer line that may be affected.
1298 * If the line was previously folded and displayed, get the first
1299 * line of that fold. If the line is folded now, get the first
1300 * folded line. Use the minimum of these two.
1301 */
1302
1303 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1304 * the line below it. If there is no valid entry, use w_topline.
1305 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1306 * to this line. If there is no valid entry, use MAXLNUM. */
1307 lnumt = wp->w_topline;
1308 lnumb = MAXLNUM;
1309 for (i = 0; i < wp->w_lines_valid; ++i)
1310 if (wp->w_lines[i].wl_valid)
1311 {
1312 if (wp->w_lines[i].wl_lastlnum < mod_top)
1313 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1314 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1315 {
1316 lnumb = wp->w_lines[i].wl_lnum;
1317 /* When there is a fold column it might need updating
1318 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001319 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 ++lnumb;
1321 }
1322 }
1323
1324 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1325 if (mod_top > lnumt)
1326 mod_top = lnumt;
1327
1328 /* Now do the same for the bottom line (one above mod_bot). */
1329 --mod_bot;
1330 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1331 ++mod_bot;
1332 if (mod_bot < lnumb)
1333 mod_bot = lnumb;
1334 }
1335#endif
1336
1337 /* When a change starts above w_topline and the end is below
1338 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001339 * If the end of the change is above w_topline: do like no change was
1340 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (mod_top != 0 && mod_top < wp->w_topline)
1342 {
1343 if (mod_bot > wp->w_topline)
1344 mod_top = wp->w_topline;
1345#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001346 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 top_end = 1;
1348#endif
1349 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350
1351 /* When line numbers are displayed need to redraw all lines below
1352 * inserted/deleted lines. */
1353 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1354 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356
1357 /*
1358 * When only displaying the lines at the top, set top_end. Used when
1359 * window has scrolled down for msg_scrolled.
1360 */
1361 if (type == REDRAW_TOP)
1362 {
1363 j = 0;
1364 for (i = 0; i < wp->w_lines_valid; ++i)
1365 {
1366 j += wp->w_lines[i].wl_size;
1367 if (j >= wp->w_upd_rows)
1368 {
1369 top_end = j;
1370 break;
1371 }
1372 }
1373 if (top_end == 0)
1374 /* not found (cannot happen?): redraw everything */
1375 type = NOT_VALID;
1376 else
1377 /* top area defined, the rest is VALID */
1378 type = VALID;
1379 }
1380
Bram Moolenaar367329b2007-08-30 11:53:22 +00001381 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001382 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1383 * non-zero and thus not FALSE) will indicate that screenclear() was not
1384 * called. */
1385 if (screen_cleared)
1386 screen_cleared = MAYBE;
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 /*
1389 * If there are no changes on the screen that require a complete redraw,
1390 * handle three cases:
1391 * 1: we are off the top of the screen by a few lines: scroll down
1392 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1393 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1394 * w_lines[] that needs updating.
1395 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001396 if ((type == VALID || type == SOME_VALID
1397 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_DIFF
1399 && !wp->w_botfill && !wp->w_old_botfill
1400#endif
1401 )
1402 {
1403 if (mod_top != 0 && wp->w_topline == mod_top)
1404 {
1405 /*
1406 * w_topline is the first changed line, the scrolling will be done
1407 * further down.
1408 */
1409 }
1410 else if (wp->w_lines[0].wl_valid
1411 && (wp->w_topline < wp->w_lines[0].wl_lnum
1412#ifdef FEAT_DIFF
1413 || (wp->w_topline == wp->w_lines[0].wl_lnum
1414 && wp->w_topfill > wp->w_old_topfill)
1415#endif
1416 ))
1417 {
1418 /*
1419 * New topline is above old topline: May scroll down.
1420 */
1421#ifdef FEAT_FOLDING
1422 if (hasAnyFolding(wp))
1423 {
1424 linenr_T ln;
1425
1426 /* count the number of lines we are off, counting a sequence
1427 * of folded lines as one */
1428 j = 0;
1429 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1430 {
1431 ++j;
1432 if (j >= wp->w_height - 2)
1433 break;
1434 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1435 }
1436 }
1437 else
1438#endif
1439 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1440 if (j < wp->w_height - 2) /* not too far off */
1441 {
1442 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1443#ifdef FEAT_DIFF
1444 /* insert extra lines for previously invisible filler lines */
1445 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1446 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1447 - wp->w_old_topfill;
1448#endif
1449 if (i < wp->w_height - 2) /* less than a screen off */
1450 {
1451 /*
1452 * Try to insert the correct number of lines.
1453 * If not the last window, delete the lines at the bottom.
1454 * win_ins_lines may fail when the terminal can't do it.
1455 */
1456 if (i > 0)
1457 check_for_delay(FALSE);
1458 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1459 {
1460 if (wp->w_lines_valid != 0)
1461 {
1462 /* Need to update rows that are new, stop at the
1463 * first one that scrolled down. */
1464 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 /* Move the entries that were scrolled, disable
1468 * the entries for the lines to be redrawn. */
1469 if ((wp->w_lines_valid += j) > wp->w_height)
1470 wp->w_lines_valid = wp->w_height;
1471 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1472 wp->w_lines[idx] = wp->w_lines[idx - j];
1473 while (idx >= 0)
1474 wp->w_lines[idx--].wl_valid = FALSE;
1475 }
1476 }
1477 else
1478 mid_start = 0; /* redraw all lines */
1479 }
1480 else
1481 mid_start = 0; /* redraw all lines */
1482 }
1483 else
1484 mid_start = 0; /* redraw all lines */
1485 }
1486 else
1487 {
1488 /*
1489 * New topline is at or below old topline: May scroll up.
1490 * When topline didn't change, find first entry in w_lines[] that
1491 * needs updating.
1492 */
1493
1494 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1495 j = -1;
1496 row = 0;
1497 for (i = 0; i < wp->w_lines_valid; i++)
1498 {
1499 if (wp->w_lines[i].wl_valid
1500 && wp->w_lines[i].wl_lnum == wp->w_topline)
1501 {
1502 j = i;
1503 break;
1504 }
1505 row += wp->w_lines[i].wl_size;
1506 }
1507 if (j == -1)
1508 {
1509 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1510 * lines */
1511 mid_start = 0;
1512 }
1513 else
1514 {
1515 /*
1516 * Try to delete the correct number of lines.
1517 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1518 */
1519#ifdef FEAT_DIFF
1520 /* If the topline didn't change, delete old filler lines,
1521 * otherwise delete filler lines of the new topline... */
1522 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1523 row += wp->w_old_topfill;
1524 else
1525 row += diff_check_fill(wp, wp->w_topline);
1526 /* ... but don't delete new filler lines. */
1527 row -= wp->w_topfill;
1528#endif
1529 if (row > 0)
1530 {
1531 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001532 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1533 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 bot_start = wp->w_height - row;
1535 else
1536 mid_start = 0; /* redraw all lines */
1537 }
1538 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1539 {
1540 /*
1541 * Skip the lines (below the deleted lines) that are still
1542 * valid and don't need redrawing. Copy their info
1543 * upwards, to compensate for the deleted lines. Set
1544 * bot_start to the first row that needs redrawing.
1545 */
1546 bot_start = 0;
1547 idx = 0;
1548 for (;;)
1549 {
1550 wp->w_lines[idx] = wp->w_lines[j];
1551 /* stop at line that didn't fit, unless it is still
1552 * valid (no lines deleted) */
1553 if (row > 0 && bot_start + row
1554 + (int)wp->w_lines[j].wl_size > wp->w_height)
1555 {
1556 wp->w_lines_valid = idx + 1;
1557 break;
1558 }
1559 bot_start += wp->w_lines[idx++].wl_size;
1560
1561 /* stop at the last valid entry in w_lines[].wl_size */
1562 if (++j >= wp->w_lines_valid)
1563 {
1564 wp->w_lines_valid = idx;
1565 break;
1566 }
1567 }
1568#ifdef FEAT_DIFF
1569 /* Correct the first entry for filler lines at the top
1570 * when it won't get updated below. */
1571 if (wp->w_p_diff && bot_start > 0)
1572 wp->w_lines[0].wl_size =
1573 plines_win_nofill(wp, wp->w_topline, TRUE)
1574 + wp->w_topfill;
1575#endif
1576 }
1577 }
1578 }
1579
1580 /* When starting redraw in the first line, redraw all lines. When
1581 * there is only one window it's probably faster to clear the screen
1582 * first. */
1583 if (mid_start == 0)
1584 {
1585 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001586 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001587 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001588 /* Clear the screen when it was not done by win_del_lines() or
1589 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1590 * then. */
1591 if (screen_cleared != TRUE)
1592 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001593 /* The screen was cleared, redraw the tab pages line. */
1594 if (redraw_tabline)
1595 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001598
1599 /* When win_del_lines() or win_ins_lines() caused the screen to be
1600 * cleared (only happens for the first window) or when screenclear()
1601 * was called directly above, "must_redraw" will have been set to
1602 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1603 if (screen_cleared == TRUE)
1604 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 else
1607 {
1608 /* Not VALID or INVERTED: redraw all lines. */
1609 mid_start = 0;
1610 mid_end = wp->w_height;
1611 }
1612
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001613 if (type == SOME_VALID)
1614 {
1615 /* SOME_VALID: redraw all lines. */
1616 mid_start = 0;
1617 mid_end = wp->w_height;
1618 type = NOT_VALID;
1619 }
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /* check if we are updating or removing the inverted part */
1622 if ((VIsual_active && buf == curwin->w_buffer)
1623 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1624 {
1625 linenr_T from, to;
1626
1627 if (VIsual_active)
1628 {
1629 if (VIsual_active
1630 && (VIsual_mode != wp->w_old_visual_mode
1631 || type == INVERTED_ALL))
1632 {
1633 /*
1634 * If the type of Visual selection changed, redraw the whole
1635 * selection. Also when the ownership of the X selection is
1636 * gained or lost.
1637 */
1638 if (curwin->w_cursor.lnum < VIsual.lnum)
1639 {
1640 from = curwin->w_cursor.lnum;
1641 to = VIsual.lnum;
1642 }
1643 else
1644 {
1645 from = VIsual.lnum;
1646 to = curwin->w_cursor.lnum;
1647 }
1648 /* redraw more when the cursor moved as well */
1649 if (wp->w_old_cursor_lnum < from)
1650 from = wp->w_old_cursor_lnum;
1651 if (wp->w_old_cursor_lnum > to)
1652 to = wp->w_old_cursor_lnum;
1653 if (wp->w_old_visual_lnum < from)
1654 from = wp->w_old_visual_lnum;
1655 if (wp->w_old_visual_lnum > to)
1656 to = wp->w_old_visual_lnum;
1657 }
1658 else
1659 {
1660 /*
1661 * Find the line numbers that need to be updated: The lines
1662 * between the old cursor position and the current cursor
1663 * position. Also check if the Visual position changed.
1664 */
1665 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1666 {
1667 from = curwin->w_cursor.lnum;
1668 to = wp->w_old_cursor_lnum;
1669 }
1670 else
1671 {
1672 from = wp->w_old_cursor_lnum;
1673 to = curwin->w_cursor.lnum;
1674 if (from == 0) /* Visual mode just started */
1675 from = to;
1676 }
1677
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001678 if (VIsual.lnum != wp->w_old_visual_lnum
1679 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 if (wp->w_old_visual_lnum < from
1682 && wp->w_old_visual_lnum != 0)
1683 from = wp->w_old_visual_lnum;
1684 if (wp->w_old_visual_lnum > to)
1685 to = wp->w_old_visual_lnum;
1686 if (VIsual.lnum < from)
1687 from = VIsual.lnum;
1688 if (VIsual.lnum > to)
1689 to = VIsual.lnum;
1690 }
1691 }
1692
1693 /*
1694 * If in block mode and changed column or curwin->w_curswant:
1695 * update all lines.
1696 * First compute the actual start and end column.
1697 */
1698 if (VIsual_mode == Ctrl_V)
1699 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001700 colnr_T fromc, toc;
1701#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1702 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar404406a2014-10-09 13:24:43 +02001704 if (curwin->w_p_lbr)
1705 ve_flags = VE_ALL;
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001708#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1709 ve_flags = save_ve_flags;
1710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 ++toc;
1712 if (curwin->w_curswant == MAXCOL)
1713 toc = MAXCOL;
1714
1715 if (fromc != wp->w_old_cursor_fcol
1716 || toc != wp->w_old_cursor_lcol)
1717 {
1718 if (from > VIsual.lnum)
1719 from = VIsual.lnum;
1720 if (to < VIsual.lnum)
1721 to = VIsual.lnum;
1722 }
1723 wp->w_old_cursor_fcol = fromc;
1724 wp->w_old_cursor_lcol = toc;
1725 }
1726 }
1727 else
1728 {
1729 /* Use the line numbers of the old Visual area. */
1730 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1731 {
1732 from = wp->w_old_cursor_lnum;
1733 to = wp->w_old_visual_lnum;
1734 }
1735 else
1736 {
1737 from = wp->w_old_visual_lnum;
1738 to = wp->w_old_cursor_lnum;
1739 }
1740 }
1741
1742 /*
1743 * There is no need to update lines above the top of the window.
1744 */
1745 if (from < wp->w_topline)
1746 from = wp->w_topline;
1747
1748 /*
1749 * If we know the value of w_botline, use it to restrict the update to
1750 * the lines that are visible in the window.
1751 */
1752 if (wp->w_valid & VALID_BOTLINE)
1753 {
1754 if (from >= wp->w_botline)
1755 from = wp->w_botline - 1;
1756 if (to >= wp->w_botline)
1757 to = wp->w_botline - 1;
1758 }
1759
1760 /*
1761 * Find the minimal part to be updated.
1762 * Watch out for scrolling that made entries in w_lines[] invalid.
1763 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1764 * top_end; need to redraw from top_end to the "to" line.
1765 * A middle mouse click with a Visual selection may change the text
1766 * above the Visual area and reset wl_valid, do count these for
1767 * mid_end (in srow).
1768 */
1769 if (mid_start > 0)
1770 {
1771 lnum = wp->w_topline;
1772 idx = 0;
1773 srow = 0;
1774 if (scrolled_down)
1775 mid_start = top_end;
1776 else
1777 mid_start = 0;
1778 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1779 {
1780 if (wp->w_lines[idx].wl_valid)
1781 mid_start += wp->w_lines[idx].wl_size;
1782 else if (!scrolled_down)
1783 srow += wp->w_lines[idx].wl_size;
1784 ++idx;
1785# ifdef FEAT_FOLDING
1786 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1787 lnum = wp->w_lines[idx].wl_lnum;
1788 else
1789# endif
1790 ++lnum;
1791 }
1792 srow += mid_start;
1793 mid_end = wp->w_height;
1794 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1795 {
1796 if (wp->w_lines[idx].wl_valid
1797 && wp->w_lines[idx].wl_lnum >= to + 1)
1798 {
1799 /* Only update until first row of this line */
1800 mid_end = srow;
1801 break;
1802 }
1803 srow += wp->w_lines[idx].wl_size;
1804 }
1805 }
1806 }
1807
1808 if (VIsual_active && buf == curwin->w_buffer)
1809 {
1810 wp->w_old_visual_mode = VIsual_mode;
1811 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1812 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001813 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 wp->w_old_curswant = curwin->w_curswant;
1815 }
1816 else
1817 {
1818 wp->w_old_visual_mode = 0;
1819 wp->w_old_cursor_lnum = 0;
1820 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001821 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1825 /* reset got_int, otherwise regexp won't work */
1826 save_got_int = got_int;
1827 got_int = 0;
1828#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001829#ifdef SYN_TIME_LIMIT
1830 /* Set the time limit to 'redrawtime'. */
1831 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001832 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835 win_foldinfo.fi_level = 0;
1836#endif
1837
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001838#ifdef FEAT_MENU
1839 /*
1840 * Draw the window toolbar, if there is one.
1841 * TODO: only when needed.
1842 */
1843 if (winbar_height(wp) > 0)
1844 redraw_win_toolbar(wp);
1845#endif
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 /*
1848 * Update all the window rows.
1849 */
1850 idx = 0; /* first entry in w_lines[].wl_size */
1851 row = 0;
1852 srow = 0;
1853 lnum = wp->w_topline; /* first line shown in window */
1854 for (;;)
1855 {
1856 /* stop updating when reached the end of the window (check for _past_
1857 * the end of the window is at the end of the loop) */
1858 if (row == wp->w_height)
1859 {
1860 didline = TRUE;
1861 break;
1862 }
1863
1864 /* stop updating when hit the end of the file */
1865 if (lnum > buf->b_ml.ml_line_count)
1866 {
1867 eof = TRUE;
1868 break;
1869 }
1870
1871 /* Remember the starting row of the line that is going to be dealt
1872 * with. It is used further down when the line doesn't fit. */
1873 srow = row;
1874
1875 /*
1876 * Update a line when it is in an area that needs updating, when it
1877 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001878 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 * win_del_lines(), check if the current line includes it.
1880 * When syntax folding is being used, the saved syntax states will
1881 * already have been updated, we can't see where the syntax state is
1882 * the same again, just update until the end of the window.
1883 */
1884 if (row < top_end
1885 || (row >= mid_start && row < mid_end)
1886#ifdef FEAT_SEARCH_EXTRA
1887 || top_to_mod
1888#endif
1889 || idx >= wp->w_lines_valid
1890 || (row + wp->w_lines[idx].wl_size > bot_start)
1891 || (mod_top != 0
1892 && (lnum == mod_top
1893 || (lnum >= mod_top
1894 && (lnum < mod_bot
1895#ifdef FEAT_SYN_HL
1896 || did_update == DID_FOLD
1897 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001898 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 && (
1900# ifdef FEAT_FOLDING
1901 (foldmethodIsSyntax(wp)
1902 && hasAnyFolding(wp)) ||
1903# endif
1904 syntax_check_changed(lnum)))
1905#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001906#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001907 /* match in fixed position might need redraw
1908 * if lines were inserted or deleted */
1909 || (wp->w_match_head != NULL
1910 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 )))))
1913 {
1914#ifdef FEAT_SEARCH_EXTRA
1915 if (lnum == mod_top)
1916 top_to_mod = FALSE;
1917#endif
1918
1919 /*
1920 * When at start of changed lines: May scroll following lines
1921 * up or down to minimize redrawing.
1922 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001923 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
1925 if (lnum == mod_top
1926 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001927 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 int old_rows = 0;
1930 int new_rows = 0;
1931 int xtra_rows;
1932 linenr_T l;
1933
1934 /* Count the old number of window rows, using w_lines[], which
1935 * should still contain the sizes for the lines as they are
1936 * currently displayed. */
1937 for (i = idx; i < wp->w_lines_valid; ++i)
1938 {
1939 /* Only valid lines have a meaningful wl_lnum. Invalid
1940 * lines are part of the changed area. */
1941 if (wp->w_lines[i].wl_valid
1942 && wp->w_lines[i].wl_lnum == mod_bot)
1943 break;
1944 old_rows += wp->w_lines[i].wl_size;
1945#ifdef FEAT_FOLDING
1946 if (wp->w_lines[i].wl_valid
1947 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1948 {
1949 /* Must have found the last valid entry above mod_bot.
1950 * Add following invalid entries. */
1951 ++i;
1952 while (i < wp->w_lines_valid
1953 && !wp->w_lines[i].wl_valid)
1954 old_rows += wp->w_lines[i++].wl_size;
1955 break;
1956 }
1957#endif
1958 }
1959
1960 if (i >= wp->w_lines_valid)
1961 {
1962 /* We can't find a valid line below the changed lines,
1963 * need to redraw until the end of the window.
1964 * Inserting/deleting lines has no use. */
1965 bot_start = 0;
1966 }
1967 else
1968 {
1969 /* Able to count old number of rows: Count new window
1970 * rows, and may insert/delete lines */
1971 j = idx;
1972 for (l = lnum; l < mod_bot; ++l)
1973 {
1974#ifdef FEAT_FOLDING
1975 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1976 ++new_rows;
1977 else
1978#endif
1979#ifdef FEAT_DIFF
1980 if (l == wp->w_topline)
1981 new_rows += plines_win_nofill(wp, l, TRUE)
1982 + wp->w_topfill;
1983 else
1984#endif
1985 new_rows += plines_win(wp, l, TRUE);
1986 ++j;
1987 if (new_rows > wp->w_height - row - 2)
1988 {
1989 /* it's getting too much, must redraw the rest */
1990 new_rows = 9999;
1991 break;
1992 }
1993 }
1994 xtra_rows = new_rows - old_rows;
1995 if (xtra_rows < 0)
1996 {
1997 /* May scroll text up. If there is not enough
1998 * remaining text or scrolling fails, must redraw the
1999 * rest. If scrolling works, must redraw the text
2000 * below the scrolled text. */
2001 if (row - xtra_rows >= wp->w_height - 2)
2002 mod_bot = MAXLNUM;
2003 else
2004 {
2005 check_for_delay(FALSE);
2006 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002007 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 mod_bot = MAXLNUM;
2009 else
2010 bot_start = wp->w_height + xtra_rows;
2011 }
2012 }
2013 else if (xtra_rows > 0)
2014 {
2015 /* May scroll text down. If there is not enough
2016 * remaining text of scrolling fails, must redraw the
2017 * rest. */
2018 if (row + xtra_rows >= wp->w_height - 2)
2019 mod_bot = MAXLNUM;
2020 else
2021 {
2022 check_for_delay(FALSE);
2023 if (win_ins_lines(wp, row + old_rows,
2024 xtra_rows, FALSE, FALSE) == FAIL)
2025 mod_bot = MAXLNUM;
2026 else if (top_end > row + old_rows)
2027 /* Scrolled the part at the top that requires
2028 * updating down. */
2029 top_end += xtra_rows;
2030 }
2031 }
2032
2033 /* When not updating the rest, may need to move w_lines[]
2034 * entries. */
2035 if (mod_bot != MAXLNUM && i != j)
2036 {
2037 if (j < i)
2038 {
2039 int x = row + new_rows;
2040
2041 /* move entries in w_lines[] upwards */
2042 for (;;)
2043 {
2044 /* stop at last valid entry in w_lines[] */
2045 if (i >= wp->w_lines_valid)
2046 {
2047 wp->w_lines_valid = j;
2048 break;
2049 }
2050 wp->w_lines[j] = wp->w_lines[i];
2051 /* stop at a line that won't fit */
2052 if (x + (int)wp->w_lines[j].wl_size
2053 > wp->w_height)
2054 {
2055 wp->w_lines_valid = j + 1;
2056 break;
2057 }
2058 x += wp->w_lines[j++].wl_size;
2059 ++i;
2060 }
2061 if (bot_start > x)
2062 bot_start = x;
2063 }
2064 else /* j > i */
2065 {
2066 /* move entries in w_lines[] downwards */
2067 j -= i;
2068 wp->w_lines_valid += j;
2069 if (wp->w_lines_valid > wp->w_height)
2070 wp->w_lines_valid = wp->w_height;
2071 for (i = wp->w_lines_valid; i - j >= idx; --i)
2072 wp->w_lines[i] = wp->w_lines[i - j];
2073
2074 /* The w_lines[] entries for inserted lines are
2075 * now invalid, but wl_size may be used above.
2076 * Reset to zero. */
2077 while (i >= idx)
2078 {
2079 wp->w_lines[i].wl_size = 0;
2080 wp->w_lines[i--].wl_valid = FALSE;
2081 }
2082 }
2083 }
2084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086
2087#ifdef FEAT_FOLDING
2088 /*
2089 * When lines are folded, display one line for all of them.
2090 * Otherwise, display normally (can be several display lines when
2091 * 'wrap' is on).
2092 */
2093 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2094 if (fold_count != 0)
2095 {
2096 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2097 ++row;
2098 --fold_count;
2099 wp->w_lines[idx].wl_folded = TRUE;
2100 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2101# ifdef FEAT_SYN_HL
2102 did_update = DID_FOLD;
2103# endif
2104 }
2105 else
2106#endif
2107 if (idx < wp->w_lines_valid
2108 && wp->w_lines[idx].wl_valid
2109 && wp->w_lines[idx].wl_lnum == lnum
2110 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002111 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 && srow + wp->w_lines[idx].wl_size > wp->w_height
2113#ifdef FEAT_DIFF
2114 && diff_check_fill(wp, lnum) == 0
2115#endif
2116 )
2117 {
2118 /* This line is not going to fit. Don't draw anything here,
2119 * will draw "@ " lines below. */
2120 row = wp->w_height + 1;
2121 }
2122 else
2123 {
2124#ifdef FEAT_SEARCH_EXTRA
2125 prepare_search_hl(wp, lnum);
2126#endif
2127#ifdef FEAT_SYN_HL
2128 /* Let the syntax stuff know we skipped a few lines. */
2129 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002130 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 syntax_end_parsing(syntax_last_parsed + 1);
2132#endif
2133
2134 /*
2135 * Display one line.
2136 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002137 row = win_line(wp, lnum, srow, wp->w_height,
2138 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140#ifdef FEAT_FOLDING
2141 wp->w_lines[idx].wl_folded = FALSE;
2142 wp->w_lines[idx].wl_lastlnum = lnum;
2143#endif
2144#ifdef FEAT_SYN_HL
2145 did_update = DID_LINE;
2146 syntax_last_parsed = lnum;
2147#endif
2148 }
2149
2150 wp->w_lines[idx].wl_lnum = lnum;
2151 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002152
2153 /* Past end of the window or end of the screen. Note that after
2154 * resizing wp->w_height may be end up too big. That's a problem
2155 * elsewhere, but prevent a crash here. */
2156 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002159 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2161 ++idx;
2162 break;
2163 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002164 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 wp->w_lines[idx].wl_size = row - srow;
2166 ++idx;
2167#ifdef FEAT_FOLDING
2168 lnum += fold_count + 1;
2169#else
2170 ++lnum;
2171#endif
2172 }
2173 else
2174 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002175 if (wp->w_p_rnu)
2176 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002177#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002178 // 'relativenumber' set: The text doesn't need to be drawn, but
2179 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002180 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2181 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002182 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002183 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002184#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002185 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002186 }
2187
2188 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 row += wp->w_lines[idx++].wl_size;
2190 if (row > wp->w_height) /* past end of screen */
2191 break;
2192#ifdef FEAT_FOLDING
2193 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2194#else
2195 ++lnum;
2196#endif
2197#ifdef FEAT_SYN_HL
2198 did_update = DID_NONE;
2199#endif
2200 }
2201
2202 if (lnum > buf->b_ml.ml_line_count)
2203 {
2204 eof = TRUE;
2205 break;
2206 }
2207 }
2208 /*
2209 * End of loop over all window lines.
2210 */
2211
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002212#ifdef FEAT_VTP
2213 /* Rewrite the character at the end of the screen line. */
2214 if (use_vtp())
2215 {
2216 int i;
2217
2218 for (i = 0; i < Rows; ++i)
2219# ifdef FEAT_MBYTE
2220 if (enc_utf8)
2221 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2222 LineOffset[i] + screen_Columns) > 1)
2223 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2224 else
2225 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2226 else
2227# endif
2228 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2229 }
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 if (idx > wp->w_lines_valid)
2233 wp->w_lines_valid = idx;
2234
2235#ifdef FEAT_SYN_HL
2236 /*
2237 * Let the syntax stuff know we stop parsing here.
2238 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002239 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 syntax_end_parsing(syntax_last_parsed + 1);
2241#endif
2242
2243 /*
2244 * If we didn't hit the end of the file, and we didn't finish the last
2245 * line we were working on, then the line didn't fit.
2246 */
2247 wp->w_empty_rows = 0;
2248#ifdef FEAT_DIFF
2249 wp->w_filler_rows = 0;
2250#endif
2251 if (!eof && !didline)
2252 {
2253 if (lnum == wp->w_topline)
2254 {
2255 /*
2256 * Single line that does not fit!
2257 * Don't overwrite it, it can be edited.
2258 */
2259 wp->w_botline = lnum + 1;
2260 }
2261#ifdef FEAT_DIFF
2262 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2263 {
2264 /* Window ends in filler lines. */
2265 wp->w_botline = lnum;
2266 wp->w_filler_rows = wp->w_height - srow;
2267 }
2268#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002269 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2270 {
2271 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2272
2273 /*
2274 * Last line isn't finished: Display "@@@" in the last screen line.
2275 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002276 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002277 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002278 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002279 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002280 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002281 set_empty_rows(wp, srow);
2282 wp->w_botline = lnum;
2283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2285 {
2286 /*
2287 * Last line isn't finished: Display "@@@" at the end.
2288 */
2289 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2290 W_WINROW(wp) + wp->w_height,
2291 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002292 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 set_empty_rows(wp, srow);
2294 wp->w_botline = lnum;
2295 }
2296 else
2297 {
2298 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2299 wp->w_botline = lnum;
2300 }
2301 }
2302 else
2303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (eof) /* we hit the end of the file */
2306 {
2307 wp->w_botline = buf->b_ml.ml_line_count + 1;
2308#ifdef FEAT_DIFF
2309 j = diff_check_fill(wp, wp->w_botline);
2310 if (j > 0 && !wp->w_botfill)
2311 {
2312 /*
2313 * Display filler lines at the end of the file
2314 */
2315 if (char2cells(fill_diff) > 1)
2316 i = '-';
2317 else
2318 i = fill_diff;
2319 if (row + j > wp->w_height)
2320 j = wp->w_height - row;
2321 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2322 row += j;
2323 }
2324#endif
2325 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002326 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 wp->w_botline = lnum;
2328
2329 /* make sure the rest of the screen is blank */
2330 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002331 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002334#ifdef SYN_TIME_LIMIT
2335 syn_set_timeout(NULL);
2336#endif
2337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 /* Reset the type of redrawing required, the window has been updated. */
2339 wp->w_redr_type = 0;
2340#ifdef FEAT_DIFF
2341 wp->w_old_topfill = wp->w_topfill;
2342 wp->w_old_botfill = wp->w_botfill;
2343#endif
2344
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002345 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
2347 /*
2348 * There is a trick with w_botline. If we invalidate it on each
2349 * change that might modify it, this will cause a lot of expensive
2350 * calls to plines() in update_topline() each time. Therefore the
2351 * value of w_botline is often approximated, and this value is used to
2352 * compute the value of w_topline. If the value of w_botline was
2353 * wrong, check that the value of w_topline is correct (cursor is on
2354 * the visible part of the text). If it's not, we need to redraw
2355 * again. Mostly this just means scrolling up a few lines, so it
2356 * doesn't look too bad. Only do this for the current window (where
2357 * changes are relevant).
2358 */
2359 wp->w_valid |= VALID_BOTLINE;
2360 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2361 {
2362 recursive = TRUE;
2363 curwin->w_valid &= ~VALID_TOPLINE;
2364 update_topline(); /* may invalidate w_botline again */
2365 if (must_redraw != 0)
2366 {
2367 /* Don't update for changes in buffer again. */
2368 i = curbuf->b_mod_set;
2369 curbuf->b_mod_set = FALSE;
2370 win_update(curwin);
2371 must_redraw = 0;
2372 curbuf->b_mod_set = i;
2373 }
2374 recursive = FALSE;
2375 }
2376 }
2377
2378#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2379 /* restore got_int, unless CTRL-C was hit while redrawing */
2380 if (!got_int)
2381 got_int = save_got_int;
2382#endif
2383}
2384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385/*
2386 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2387 * as the filler character.
2388 */
2389 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002390win_draw_end(
2391 win_T *wp,
2392 int c1,
2393 int c2,
2394 int row,
2395 int endrow,
2396 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2399 int n = 0;
2400# define FDC_OFF n
2401#else
2402# define FDC_OFF 0
2403#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002404#ifdef FEAT_FOLDING
2405 int fdc = compute_foldcolumn(wp, 0);
2406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408#ifdef FEAT_RIGHTLEFT
2409 if (wp->w_p_rl)
2410 {
2411 /* No check for cmdline window: should never be right-left. */
2412# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002413 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 if (n > 0)
2416 {
2417 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002418 if (n > wp->w_width)
2419 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2421 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002422 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
2424# endif
2425# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002426 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
2428 int nn = n + 2;
2429
2430 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002431 if (nn > wp->w_width)
2432 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2434 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 n = nn;
2437 }
2438# endif
2439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002440 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002441 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2443 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002444 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446 else
2447#endif
2448 {
2449#ifdef FEAT_CMDWIN
2450 if (cmdwin_type != 0 && wp == curwin)
2451 {
2452 /* draw the cmdline character in the leftmost column */
2453 n = 1;
2454 if (n > wp->w_width)
2455 n = wp->w_width;
2456 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002457 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002458 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460#endif
2461#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002462 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002464 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002467 if (nn > wp->w_width)
2468 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002470 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002471 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 n = nn;
2473 }
2474#endif
2475#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002476 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
2478 int nn = n + 2;
2479
2480 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002481 if (nn > wp->w_width)
2482 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002484 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002485 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 n = nn;
2487 }
2488#endif
2489 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002490 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002491 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493 set_empty_rows(wp, row);
2494}
2495
Bram Moolenaar1a384422010-07-14 19:53:30 +02002496#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002497/*
2498 * Advance **color_cols and return TRUE when there are columns to draw.
2499 */
2500 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002501advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002502{
2503 while (**color_cols >= 0 && vcol > **color_cols)
2504 ++*color_cols;
2505 return (**color_cols >= 0);
2506}
2507#endif
2508
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002509#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2510/*
2511 * Copy "text" to ScreenLines using "attr".
2512 * Returns the next screen column.
2513 */
2514 static int
2515text_to_screenline(win_T *wp, char_u *text, int col)
2516{
2517 int off = (int)(current_ScreenLine - ScreenLines);
2518
2519#ifdef FEAT_MBYTE
2520 if (has_mbyte)
2521 {
2522 int cells;
2523 int u8c, u8cc[MAX_MCO];
2524 int i;
2525 int idx;
2526 int c_len;
2527 char_u *p;
2528# ifdef FEAT_ARABIC
2529 int prev_c = 0; /* previous Arabic character */
2530 int prev_c1 = 0; /* first composing char for prev_c */
2531# endif
2532
2533# ifdef FEAT_RIGHTLEFT
2534 if (wp->w_p_rl)
2535 idx = off;
2536 else
2537# endif
2538 idx = off + col;
2539
2540 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2541 for (p = text; *p != NUL; )
2542 {
2543 cells = (*mb_ptr2cells)(p);
2544 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002545 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002546# ifdef FEAT_RIGHTLEFT
2547 - (wp->w_p_rl ? col : 0)
2548# endif
2549 )
2550 break;
2551 ScreenLines[idx] = *p;
2552 if (enc_utf8)
2553 {
2554 u8c = utfc_ptr2char(p, u8cc);
2555 if (*p < 0x80 && u8cc[0] == 0)
2556 {
2557 ScreenLinesUC[idx] = 0;
2558#ifdef FEAT_ARABIC
2559 prev_c = u8c;
2560#endif
2561 }
2562 else
2563 {
2564#ifdef FEAT_ARABIC
2565 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2566 {
2567 /* Do Arabic shaping. */
2568 int pc, pc1, nc;
2569 int pcc[MAX_MCO];
2570 int firstbyte = *p;
2571
2572 /* The idea of what is the previous and next
2573 * character depends on 'rightleft'. */
2574 if (wp->w_p_rl)
2575 {
2576 pc = prev_c;
2577 pc1 = prev_c1;
2578 nc = utf_ptr2char(p + c_len);
2579 prev_c1 = u8cc[0];
2580 }
2581 else
2582 {
2583 pc = utfc_ptr2char(p + c_len, pcc);
2584 nc = prev_c;
2585 pc1 = pcc[0];
2586 }
2587 prev_c = u8c;
2588
2589 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2590 pc, pc1, nc);
2591 ScreenLines[idx] = firstbyte;
2592 }
2593 else
2594 prev_c = u8c;
2595#endif
2596 /* Non-BMP character: display as ? or fullwidth ?. */
2597#ifdef UNICODE16
2598 if (u8c >= 0x10000)
2599 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2600 else
2601#endif
2602 ScreenLinesUC[idx] = u8c;
2603 for (i = 0; i < Screen_mco; ++i)
2604 {
2605 ScreenLinesC[i][idx] = u8cc[i];
2606 if (u8cc[i] == 0)
2607 break;
2608 }
2609 }
2610 if (cells > 1)
2611 ScreenLines[idx + 1] = 0;
2612 }
2613 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2614 /* double-byte single width character */
2615 ScreenLines2[idx] = p[1];
2616 else if (cells > 1)
2617 /* double-width character */
2618 ScreenLines[idx + 1] = p[1];
2619 col += cells;
2620 idx += cells;
2621 p += c_len;
2622 }
2623 }
2624 else
2625#endif
2626 {
2627 int len = (int)STRLEN(text);
2628
Bram Moolenaar02631462017-09-22 15:20:32 +02002629 if (len > wp->w_width - col)
2630 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002631 if (len > 0)
2632 {
2633#ifdef FEAT_RIGHTLEFT
2634 if (wp->w_p_rl)
2635 STRNCPY(current_ScreenLine, text, len);
2636 else
2637#endif
2638 STRNCPY(current_ScreenLine + col, text, len);
2639 col += len;
2640 }
2641 }
2642 return col;
2643}
2644#endif
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#ifdef FEAT_FOLDING
2647/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002648 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2649 * space is available for window "wp", minus "col".
2650 */
2651 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002652compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002653{
2654 int fdc = wp->w_p_fdc;
2655 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002656 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002657
2658 if (fdc > wwidth - (col + wmw))
2659 fdc = wwidth - (col + wmw);
2660 return fdc;
2661}
2662
2663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 * Display one folded line.
2665 */
2666 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002667fold_line(
2668 win_T *wp,
2669 long fold_count,
2670 foldinfo_T *foldinfo,
2671 linenr_T lnum,
2672 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002674 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 pos_T *top, *bot;
2676 linenr_T lnume = lnum + fold_count - 1;
2677 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002678 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 int col;
2681 int txtcol;
2682 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002683 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
2685 /* Build the fold line:
2686 * 1. Add the cmdwin_type for the command-line window
2687 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002688 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 * 4. Compose the text
2690 * 5. Add the text
2691 * 6. set highlighting for the Visual area an other text
2692 */
2693 col = 0;
2694
2695 /*
2696 * 1. Add the cmdwin_type for the command-line window
2697 * Ignores 'rightleft', this window is never right-left.
2698 */
2699#ifdef FEAT_CMDWIN
2700 if (cmdwin_type != 0 && wp == curwin)
2701 {
2702 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002703 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#ifdef FEAT_MBYTE
2705 if (enc_utf8)
2706 ScreenLinesUC[off] = 0;
2707#endif
2708 ++col;
2709 }
2710#endif
2711
2712 /*
2713 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002714 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002716 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (fdc > 0)
2718 {
2719 fill_foldcolumn(buf, wp, TRUE, lnum);
2720#ifdef FEAT_RIGHTLEFT
2721 if (wp->w_p_rl)
2722 {
2723 int i;
2724
Bram Moolenaar02631462017-09-22 15:20:32 +02002725 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002726 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 /* reverse the fold column */
2728 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002729 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731 else
2732#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002733 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 col += fdc;
2735 }
2736
2737#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002738# define RL_MEMSET(p, v, l) \
2739 do { \
2740 if (wp->w_p_rl) \
2741 for (ri = 0; ri < l; ++ri) \
2742 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2743 else \
2744 for (ri = 0; ri < l; ++ri) \
2745 ScreenAttrs[off + (p) + ri] = v; \
2746 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002748# define RL_MEMSET(p, v, l) \
2749 do { \
2750 for (ri = 0; ri < l; ++ri) \
2751 ScreenAttrs[off + (p) + ri] = v; \
2752 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#endif
2754
Bram Moolenaar64486672010-05-16 15:46:46 +02002755 /* Set all attributes of the 'number' or 'relativenumber' column and the
2756 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002757 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759#ifdef FEAT_SIGNS
2760 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002761 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002763 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (len > 0)
2765 {
2766 if (len > 2)
2767 len = 2;
2768# ifdef FEAT_RIGHTLEFT
2769 if (wp->w_p_rl)
2770 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002771 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002772 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else
2774# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002775 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 col += len;
2777 }
2778 }
2779#endif
2780
2781 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002782 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002784 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002786 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 if (len > 0)
2788 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002789 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002790 long num;
2791 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002792
2793 if (len > w + 1)
2794 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002795
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002796 if (wp->w_p_nu && !wp->w_p_rnu)
2797 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002798 num = (long)lnum;
2799 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002800 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002801 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002802 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002803 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002804 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002805 /* 'number' + 'relativenumber': cursor line shows absolute
2806 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002807 num = lnum;
2808 fmt = "%-*ld ";
2809 }
2810 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002811
Bram Moolenaar700e7342013-01-30 12:31:36 +01002812 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813#ifdef FEAT_RIGHTLEFT
2814 if (wp->w_p_rl)
2815 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002816 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002817 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 else
2819#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002820 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 col += len;
2822 }
2823 }
2824
2825 /*
2826 * 4. Compose the folded-line string with 'foldtext', if set.
2827 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002828 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830 txtcol = col; /* remember where text starts */
2831
2832 /*
2833 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2834 * Right-left text is put in columns 0 - number-col, normal text is put
2835 * in columns number-col - window-width.
2836 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002837 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
2839 /* Fill the rest of the line with the fold filler */
2840#ifdef FEAT_RIGHTLEFT
2841 if (wp->w_p_rl)
2842 col -= txtcol;
2843#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002844 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845#ifdef FEAT_RIGHTLEFT
2846 - (wp->w_p_rl ? txtcol : 0)
2847#endif
2848 )
2849 {
2850#ifdef FEAT_MBYTE
2851 if (enc_utf8)
2852 {
2853 if (fill_fold >= 0x80)
2854 {
2855 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002856 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002857 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002862 ScreenLines[off + col] = fill_fold;
2863 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002864 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002866 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002868 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
2870
2871 if (text != buf)
2872 vim_free(text);
2873
2874 /*
2875 * 6. set highlighting for the Visual area an other text.
2876 * If all folded lines are in the Visual area, highlight the line.
2877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2879 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002880 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
2882 /* Visual is after curwin->w_cursor */
2883 top = &curwin->w_cursor;
2884 bot = &VIsual;
2885 }
2886 else
2887 {
2888 /* Visual is before curwin->w_cursor */
2889 top = &VIsual;
2890 bot = &curwin->w_cursor;
2891 }
2892 if (lnum >= top->lnum
2893 && lnume <= bot->lnum
2894 && (VIsual_mode != 'v'
2895 || ((lnum > top->lnum
2896 || (lnum == top->lnum
2897 && top->col == 0))
2898 && (lnume < bot->lnum
2899 || (lnume == bot->lnum
2900 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002901 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903 if (VIsual_mode == Ctrl_V)
2904 {
2905 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002906 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002908 if (wp->w_old_cursor_lcol != MAXCOL
2909 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002910 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 len = wp->w_old_cursor_lcol;
2912 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002913 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002914 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002915 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917 }
2918 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002921 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002926#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002927 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2928 if (wp->w_p_cc_cols)
2929 {
2930 int i = 0;
2931 int j = wp->w_p_cc_cols[i];
2932 int old_txtcol = txtcol;
2933
2934 while (j > -1)
2935 {
2936 txtcol += j;
2937 if (wp->w_p_wrap)
2938 txtcol -= wp->w_skipcol;
2939 else
2940 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002941 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002942 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002943 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002944 txtcol = old_txtcol;
2945 j = wp->w_p_cc_cols[++i];
2946 }
2947 }
2948
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002949 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002950 if (wp->w_p_cuc)
2951 {
2952 txtcol += wp->w_virtcol;
2953 if (wp->w_p_wrap)
2954 txtcol -= wp->w_skipcol;
2955 else
2956 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002957 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002958 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002959 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002960 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
Bram Moolenaar02631462017-09-22 15:20:32 +02002963 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2964 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 /*
2967 * Update w_cline_height and w_cline_folded if the cursor line was
2968 * updated (saves a call to plines() later).
2969 */
2970 if (wp == curwin
2971 && lnum <= curwin->w_cursor.lnum
2972 && lnume >= curwin->w_cursor.lnum)
2973 {
2974 curwin->w_cline_row = row;
2975 curwin->w_cline_height = 1;
2976 curwin->w_cline_folded = TRUE;
2977 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2978 }
2979}
2980
2981/*
2982 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2983 */
2984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002985copy_text_attr(
2986 int off,
2987 char_u *buf,
2988 int len,
2989 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002991 int i;
2992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 mch_memmove(ScreenLines + off, buf, (size_t)len);
2994# ifdef FEAT_MBYTE
2995 if (enc_utf8)
2996 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2997# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002998 for (i = 0; i < len; ++i)
2999 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000}
3001
3002/*
3003 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003004 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 */
3006 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003007fill_foldcolumn(
3008 char_u *p,
3009 win_T *wp,
3010 int closed, /* TRUE of FALSE */
3011 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 int i = 0;
3014 int level;
3015 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003016 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003017 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
3019 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003020 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 level = win_foldinfo.fi_level;
3023 if (level > 0)
3024 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003025 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003026 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 /* If the column is too narrow, we start at the lowest level that
3029 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003030 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (first_level < 1)
3032 first_level = 1;
3033
Bram Moolenaar1c934292015-01-27 16:39:29 +01003034 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
3036 if (win_foldinfo.fi_lnum == lnum
3037 && first_level + i >= win_foldinfo.fi_low_level)
3038 p[i] = '-';
3039 else if (first_level == 1)
3040 p[i] = '|';
3041 else if (first_level + i <= 9)
3042 p[i] = '0' + first_level + i;
3043 else
3044 p[i] = '>';
3045 if (first_level + i == level)
3046 break;
3047 }
3048 }
3049 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003050 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051}
3052#endif /* FEAT_FOLDING */
3053
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003054#ifdef FEAT_TEXT_PROP
3055static textprop_T *current_text_props = NULL;
3056static buf_T *current_buf = NULL;
3057
3058 static int
3059#ifdef __BORLANDC__
3060_RTLENTRYF
3061#endif
3062text_prop_compare(const void *s1, const void *s2)
3063{
3064 int idx1, idx2;
3065 proptype_T *pt1, *pt2;
3066 colnr_T col1, col2;
3067
3068 idx1 = *(int *)s1;
3069 idx2 = *(int *)s2;
3070 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3071 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3072 if (pt1 == pt2)
3073 return 0;
3074 if (pt1 == NULL)
3075 return -1;
3076 if (pt2 == NULL)
3077 return 1;
3078 if (pt1->pt_priority != pt2->pt_priority)
3079 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3080 col1 = current_text_props[idx1].tp_col;
3081 col2 = current_text_props[idx2].tp_col;
3082 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3083}
3084#endif
3085
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086/*
3087 * Display line "lnum" of window 'wp' on the screen.
3088 * Start at row "startrow", stop when "endrow" is reached.
3089 * wp->w_virtcol needs to be valid.
3090 *
3091 * Return the number of last row the line occupies.
3092 */
3093 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003094win_line(
3095 win_T *wp,
3096 linenr_T lnum,
3097 int startrow,
3098 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003099 int nochange UNUSED, // not updating for changed text
3100 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003102 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3104 int c = 0; /* init for GCC */
3105 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003106#ifdef FEAT_LINEBREAK
3107 long vcol_sbr = -1; /* virtual column after showbreak */
3108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 long vcol_prev = -1; /* "vcol" of previous character */
3110 char_u *line; /* current line */
3111 char_u *ptr; /* current position in "line" */
3112 int row; /* row in the window, excl w_winrow */
3113 int screen_row; /* row on the screen, incl w_winrow */
3114
3115 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3116 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003117 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003118 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 int c_extra = NUL; /* extra chars, all the same */
3120 int extra_attr = 0; /* attributes when n_extra != 0 */
3121 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3122 displaying lcs_eol at end-of-line */
3123 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3124 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3125
3126 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3127 int saved_n_extra = 0;
3128 char_u *saved_p_extra = NULL;
3129 int saved_c_extra = 0;
3130 int saved_char_attr = 0;
3131
3132 int n_attr = 0; /* chars with special attr */
3133 int saved_attr2 = 0; /* char_attr saved for n_attr */
3134 int n_attr3 = 0; /* chars with overruling special attr */
3135 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3136
3137 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3138
3139 int fromcol, tocol; /* start/end of inverting */
3140 int fromcol_prev = -2; /* start of inverting after cursor */
3141 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003143 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 pos_T pos;
3145 long v;
3146
3147 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003148 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3150 in this line */
3151 int attr = 0; /* attributes for area highlighting */
3152 int area_attr = 0; /* attributes desired by highlighting */
3153 int search_attr = 0; /* attributes desired by 'hlsearch' */
3154#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003155 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 int syntax_attr = 0; /* attributes desired by syntax */
3157 int has_syntax = FALSE; /* this buffer has syntax highl. */
3158 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003159 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003160 int draw_color_col = FALSE; /* highlight colorcolumn */
3161 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003162#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003163#ifdef FEAT_TEXT_PROP
3164 int text_prop_count;
3165 int text_prop_next = 0; // next text property to use
3166 textprop_T *text_props = NULL;
3167 int *text_prop_idxs = NULL;
3168 int text_props_active = 0;
3169 proptype_T *text_prop_type = NULL;
3170 int text_prop_attr = 0;
3171#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003172#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003173 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003174# define SPWORDLEN 150
3175 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003176 int nextlinecol = 0; /* column where nextline[] starts */
3177 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003178 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003179 int spell_attr = 0; /* attributes desired by spelling */
3180 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003181 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3182 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003183 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003184 static int cap_col = -1; /* column to check for Cap word */
3185 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003186 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003188 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189#ifdef FEAT_MBYTE
3190 int multi_attr = 0; /* attributes desired by multibyte */
3191 int mb_l = 1; /* multi-byte byte length */
3192 int mb_c = 0; /* decoded multi-byte character */
3193 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003194 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
3196#ifdef FEAT_DIFF
3197 int filler_lines; /* nr of filler lines to be drawn */
3198 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003199 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 int change_start = MAXCOL; /* first col of changed area */
3201 int change_end = -1; /* last col of changed area */
3202#endif
3203 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3204#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003205 int need_showbreak = FALSE; /* overlong line, skipping first x
3206 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003208#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003209 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003211 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212#endif
3213#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003214 matchitem_T *cur; /* points to the match list */
3215 match_T *shl; /* points to search_hl or a match */
3216 int shl_flag; /* flag to indicate whether search_hl
3217 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003218 int pos_inprogress; /* marks that position match search is
3219 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003220 int prevcol_hl_flag; /* flag to indicate whether prevcol
3221 equals startcol of search_hl or one
3222 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223#endif
3224#ifdef FEAT_ARABIC
3225 int prev_c = 0; /* previous Arabic character */
3226 int prev_c1 = 0; /* first composing char for prev_c */
3227#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003228#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003229 int did_line_attr = 0;
3230#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003231#ifdef FEAT_TERMINAL
3232 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003233 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236 /* draw_state: items that are drawn in sequence: */
3237#define WL_START 0 /* nothing done yet */
3238#ifdef FEAT_CMDWIN
3239# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3240#else
3241# define WL_CMDLINE WL_START
3242#endif
3243#ifdef FEAT_FOLDING
3244# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3245#else
3246# define WL_FOLD WL_CMDLINE
3247#endif
3248#ifdef FEAT_SIGNS
3249# define WL_SIGN WL_FOLD + 1 /* column for signs */
3250#else
3251# define WL_SIGN WL_FOLD /* column for signs */
3252#endif
3253#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003254#ifdef FEAT_LINEBREAK
3255# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003257# define WL_BRI WL_NR
3258#endif
3259#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3260# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3261#else
3262# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#endif
3264#define WL_LINE WL_SBR + 1 /* text in the line */
3265 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003266#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 int feedback_col = 0;
3268 int feedback_old_attr = -1;
3269#endif
3270
Bram Moolenaar860cae12010-06-05 23:22:07 +02003271#ifdef FEAT_CONCEAL
3272 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003273 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003274 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003275 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003276 int is_concealing = FALSE;
3277 int boguscols = 0; /* nonexistent columns added to force
3278 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003279 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003280 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003281 int match_conc = 0; /* cchar for match functions */
3282 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003283 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003284# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003285# define FIX_FOR_BOGUSCOLS \
3286 { \
3287 n_extra += vcol_off; \
3288 vcol -= vcol_off; \
3289 vcol_off = 0; \
3290 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003291 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003292 boguscols = 0; \
3293 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003294#else
3295# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
3298 if (startrow > endrow) /* past the end already! */
3299 return startrow;
3300
3301 row = startrow;
3302 screen_row = row + W_WINROW(wp);
3303
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003304 if (!number_only)
3305 {
3306 /*
3307 * To speed up the loop below, set extra_check when there is linebreak,
3308 * trailing white space and/or syntax processing to be done.
3309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003311 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#endif
3313#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003314 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003315# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003316 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003317# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003318 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003320 /* Prepare for syntax highlighting in this line. When there is an
3321 * error, stop syntax highlighting. */
3322 save_did_emsg = did_emsg;
3323 did_emsg = FALSE;
3324 syntax_start(wp, lnum);
3325 if (did_emsg)
3326 wp->w_s->b_syn_error = TRUE;
3327 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003328 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003329 did_emsg = save_did_emsg;
3330#ifdef SYN_TIME_LIMIT
3331 if (!wp->w_s->b_syn_slow)
3332#endif
3333 {
3334 has_syntax = TRUE;
3335 extra_check = TRUE;
3336 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003339
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003340 /* Check for columns to display for 'colorcolumn'. */
3341 color_cols = wp->w_p_cc_cols;
3342 if (color_cols != NULL)
3343 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003344#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003345
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003346#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003347 if (term_show_buffer(wp->w_buffer))
3348 {
3349 extra_check = TRUE;
3350 get_term_attr = TRUE;
3351 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3352 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003353#endif
3354
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003355#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003356 if (wp->w_p_spell
3357 && *wp->w_s->b_p_spl != NUL
3358 && wp->w_s->b_langp.ga_len > 0
3359 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003360 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 /* Prepare for spell checking. */
3362 has_spell = TRUE;
3363 extra_check = TRUE;
3364
Bram Moolenaar7701f302018-10-02 21:20:32 +02003365 /* Get the start of the next line, so that words that wrap to the
3366 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 * Trick: skip a few chars for C/shell/Vim comments */
3368 nextline[SPWORDLEN] = NUL;
3369 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3370 {
3371 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3372 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3373 }
3374
Bram Moolenaar7701f302018-10-02 21:20:32 +02003375 /* When a word wrapped from the previous line the start of the
3376 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003377 if (lnum == checked_lnum)
3378 cur_checked_col = checked_col;
3379 checked_lnum = 0;
3380
3381 /* When there was a sentence end in the previous line may require a
3382 * word starting with capital in this line. In line 1 always check
3383 * the first word. */
3384 if (lnum != capcol_lnum)
3385 cap_col = -1;
3386 if (lnum == 1)
3387 cap_col = 0;
3388 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390#endif
3391
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003392 /*
3393 * handle visual active in this window
3394 */
3395 fromcol = -10;
3396 tocol = MAXCOL;
3397 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003399 /* Visual is after curwin->w_cursor */
3400 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003402 top = &curwin->w_cursor;
3403 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003405 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003407 top = &VIsual;
3408 bot = &curwin->w_cursor;
3409 }
3410 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3411 if (VIsual_mode == Ctrl_V) /* block mode */
3412 {
3413 if (lnum_in_visual_area)
3414 {
3415 fromcol = wp->w_old_cursor_fcol;
3416 tocol = wp->w_old_cursor_lcol;
3417 }
3418 }
3419 else /* non-block mode */
3420 {
3421 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003425 if (VIsual_mode == 'V') /* linewise */
3426 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 else
3428 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3430 if (gchar_pos(top) == NUL)
3431 tocol = fromcol + 1;
3432 }
3433 }
3434 if (VIsual_mode != 'V' && lnum == bot->lnum)
3435 {
3436 if (*p_sel == 'e' && bot->col == 0
3437#ifdef FEAT_VIRTUALEDIT
3438 && bot->coladd == 0
3439#endif
3440 )
3441 {
3442 fromcol = -10;
3443 tocol = MAXCOL;
3444 }
3445 else if (bot->col == MAXCOL)
3446 tocol = MAXCOL;
3447 else
3448 {
3449 pos = *bot;
3450 if (*p_sel == 'e')
3451 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3452 else
3453 {
3454 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3455 ++tocol;
3456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
3458 }
3459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003461 /* Check if the character under the cursor should not be inverted */
3462 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003463#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003464 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003465#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003466 )
3467 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003469 /* if inverting in this line set area_highlighting */
3470 if (fromcol >= 0)
3471 {
3472 area_highlighting = TRUE;
3473 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003475 if ((clip_star.available && !clip_star.owned
3476 && clip_isautosel_star())
3477 || (clip_plus.available && !clip_plus.owned
3478 && clip_isautosel_plus()))
3479 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003484 /*
3485 * handle 'incsearch' and ":s///c" highlighting
3486 */
3487 else if (highlight_match
3488 && wp == curwin
3489 && lnum >= curwin->w_cursor.lnum
3490 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003492 if (lnum == curwin->w_cursor.lnum)
3493 getvcol(curwin, &(curwin->w_cursor),
3494 (colnr_T *)&fromcol, NULL, NULL);
3495 else
3496 fromcol = 0;
3497 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3498 {
3499 pos.lnum = lnum;
3500 pos.col = search_match_endcol;
3501 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3502 }
3503 else
3504 tocol = MAXCOL;
3505 /* do at least one character; happens when past end of line */
3506 if (fromcol == tocol)
3507 tocol = fromcol + 1;
3508 area_highlighting = TRUE;
3509 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512
3513#ifdef FEAT_DIFF
3514 filler_lines = diff_check(wp, lnum);
3515 if (filler_lines < 0)
3516 {
3517 if (filler_lines == -1)
3518 {
3519 if (diff_find_change(wp, lnum, &change_start, &change_end))
3520 diff_hlf = HLF_ADD; /* added line */
3521 else if (change_start == 0)
3522 diff_hlf = HLF_TXD; /* changed text */
3523 else
3524 diff_hlf = HLF_CHD; /* changed line */
3525 }
3526 else
3527 diff_hlf = HLF_ADD; /* added line */
3528 filler_lines = 0;
3529 area_highlighting = TRUE;
3530 }
3531 if (lnum == wp->w_topline)
3532 filler_lines = wp->w_topfill;
3533 filler_todo = filler_lines;
3534#endif
3535
3536#ifdef LINE_ATTR
3537# ifdef FEAT_SIGNS
3538 /* If this line has a sign with line highlighting set line_attr. */
3539 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3540 if (v != 0)
3541 line_attr = sign_get_attr((int)v, TRUE);
3542# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003543# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003545 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003546 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547# endif
3548 if (line_attr != 0)
3549 area_highlighting = TRUE;
3550#endif
3551
3552 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3553 ptr = line;
3554
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003555#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003556 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003557 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003558 /* For checking first word with a capital skip white space. */
3559 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003560 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003561
Bram Moolenaar30abd282005-06-22 22:35:10 +00003562 /* To be able to spell-check over line boundaries copy the end of the
3563 * current line into nextline[]. Above the start of the next line was
3564 * copied to nextline[SPWORDLEN]. */
3565 if (nextline[SPWORDLEN] == NUL)
3566 {
3567 /* No next line or it is empty. */
3568 nextlinecol = MAXCOL;
3569 nextline_idx = 0;
3570 }
3571 else
3572 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003573 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003574 if (v < SPWORDLEN)
3575 {
3576 /* Short line, use it completely and append the start of the
3577 * next line. */
3578 nextlinecol = 0;
3579 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003580 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003581 nextline_idx = v + 1;
3582 }
3583 else
3584 {
3585 /* Long line, use only the last SPWORDLEN bytes. */
3586 nextlinecol = v - SPWORDLEN;
3587 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3588 nextline_idx = SPWORDLEN + 1;
3589 }
3590 }
3591 }
3592#endif
3593
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003594 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003596 if (lcs_space || lcs_trail)
3597 extra_check = TRUE;
3598 /* find start of trailing whitespace */
3599 if (lcs_trail)
3600 {
3601 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003602 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003603 --trailcol;
3604 trailcol += (colnr_T) (ptr - line);
3605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607
3608 /*
3609 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3610 * first character to be displayed.
3611 */
3612 if (wp->w_p_wrap)
3613 v = wp->w_skipcol;
3614 else
3615 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003616 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618#ifdef FEAT_MBYTE
3619 char_u *prev_ptr = ptr;
3620#endif
3621 while (vcol < v && *ptr != NUL)
3622 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003623 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 vcol += c;
3625#ifdef FEAT_MBYTE
3626 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003628 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003631 /* When:
3632 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003633 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003634 * - 'virtualedit' is set, or
3635 * - the visual mode is active,
3636 * the end of the line may be before the start of the displayed part.
3637 */
3638 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003639#ifdef FEAT_SYN_HL
3640 wp->w_p_cuc || draw_color_col ||
3641#endif
3642#ifdef FEAT_VIRTUALEDIT
3643 virtual_active() ||
3644#endif
3645 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 /* Handle a character that's not completely on the screen: Put ptr at
3651 * that character but skip the first few screen characters. */
3652 if (vcol > v)
3653 {
3654 vcol -= c;
3655#ifdef FEAT_MBYTE
3656 ptr = prev_ptr;
3657#else
3658 --ptr;
3659#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003660 /* If the character fits on the screen, don't need to skip it.
3661 * Except for a TAB. */
3662 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003663#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003664 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003665#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003666 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003667 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669
3670 /*
3671 * Adjust for when the inverted text is before the screen,
3672 * and when the start of the inverted text is before the screen.
3673 */
3674 if (tocol <= vcol)
3675 fromcol = 0;
3676 else if (fromcol >= 0 && fromcol < vcol)
3677 fromcol = vcol;
3678
3679#ifdef FEAT_LINEBREAK
3680 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3681 if (wp->w_p_wrap)
3682 need_showbreak = TRUE;
3683#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003684#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003685 /* When spell checking a word we need to figure out the start of the
3686 * word and if it's badly spelled or not. */
3687 if (has_spell)
3688 {
3689 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003690 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003691 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003692
3693 pos = wp->w_cursor;
3694 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003695 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003696 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003697
3698 /* spell_move_to() may call ml_get() and make "line" invalid */
3699 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3700 ptr = line + linecol;
3701
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003702 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003703 {
3704 /* no bad word found at line start, don't check until end of a
3705 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003706 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003707 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003708 }
3709 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003710 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003711 /* bad word found, use attributes until end of word */
3712 word_end = wp->w_cursor.col + len + 1;
3713
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003714 /* Turn index into actual attributes. */
3715 if (spell_hlf != HLF_COUNT)
3716 spell_attr = highlight_attr[spell_hlf];
3717 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003718 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003719
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003720# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003721 /* Need to restart syntax highlighting for this line. */
3722 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003723 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003724# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003725 }
3726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
3728
3729 /*
3730 * Correct highlighting for cursor that can't be disabled.
3731 * Avoids having to check this for each character.
3732 */
3733 if (fromcol >= 0)
3734 {
3735 if (noinvcur)
3736 {
3737 if ((colnr_T)fromcol == wp->w_virtcol)
3738 {
3739 /* highlighting starts at cursor, let it start just after the
3740 * cursor */
3741 fromcol_prev = fromcol;
3742 fromcol = -1;
3743 }
3744 else if ((colnr_T)fromcol < wp->w_virtcol)
3745 /* restart highlighting after the cursor */
3746 fromcol_prev = wp->w_virtcol;
3747 }
3748 if (fromcol >= tocol)
3749 fromcol = -1;
3750 }
3751
3752#ifdef FEAT_SEARCH_EXTRA
3753 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003754 * Handle highlighting the last used search pattern and matches.
3755 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003757 cur = wp->w_match_head;
3758 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003759 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003761 if (shl_flag == FALSE)
3762 {
3763 shl = &search_hl;
3764 shl_flag = TRUE;
3765 }
3766 else
3767 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003768 shl->startcol = MAXCOL;
3769 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003771 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003772 v = (long)(ptr - line);
3773 if (cur != NULL)
3774 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003775 next_search_hl(wp, shl, lnum, (colnr_T)v,
3776 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003777
3778 /* Need to get the line again, a multi-line regexp may have made it
3779 * invalid. */
3780 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3781 ptr = line + v;
3782
3783 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003785 if (shl->lnum == lnum)
3786 shl->startcol = shl->rm.startpos[0].col;
3787 else
3788 shl->startcol = 0;
3789 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3790 - shl->rm.startpos[0].lnum)
3791 shl->endcol = shl->rm.endpos[0].col;
3792 else
3793 shl->endcol = MAXCOL;
3794 /* Highlight one character for an empty match. */
3795 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003798 if (has_mbyte && line[shl->endcol] != NUL)
3799 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3800 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003802 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003804 if ((long)shl->startcol < v) /* match at leftcol */
3805 {
3806 shl->attr_cur = shl->attr;
3807 search_attr = shl->attr;
3808 }
3809 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003811 if (shl != &search_hl && cur != NULL)
3812 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814#endif
3815
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003816#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003817 /* Cursor line highlighting for 'cursorline' in the current window. Not
3818 * when Visual mode is active, because it's not clear what is selected
3819 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003820 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003821 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003822 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003823 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003824 area_highlighting = TRUE;
3825 }
3826#endif
3827
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003828#ifdef FEAT_TEXT_PROP
3829 {
3830 char_u *prop_start;
3831
3832 text_prop_count = get_text_props(wp->w_buffer, lnum,
3833 &prop_start, FALSE);
3834 if (text_prop_count > 0)
3835 {
3836 // Make a copy of the properties, so that they are properly
3837 // aligned.
3838 text_props = (textprop_T *)alloc(
3839 text_prop_count * sizeof(textprop_T));
3840 if (text_props != NULL)
3841 mch_memmove(text_props, prop_start,
3842 text_prop_count * sizeof(textprop_T));
3843
3844 // Allocate an array for the indexes.
3845 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3846 area_highlighting = TRUE;
3847 extra_check = TRUE;
3848 }
3849 }
3850#endif
3851
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003852 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 col = 0;
3854#ifdef FEAT_RIGHTLEFT
3855 if (wp->w_p_rl)
3856 {
3857 /* Rightleft window: process the text in the normal direction, but put
3858 * it in current_ScreenLine[] from right to left. Start at the
3859 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003860 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 off += col;
3862 }
3863#endif
3864
3865 /*
3866 * Repeat for the whole displayed line.
3867 */
3868 for (;;)
3869 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003870#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003871 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 /* Skip this quickly when working on the text. */
3874 if (draw_state != WL_LINE)
3875 {
3876#ifdef FEAT_CMDWIN
3877 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3878 {
3879 draw_state = WL_CMDLINE;
3880 if (cmdwin_type != 0 && wp == curwin)
3881 {
3882 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003884 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003885 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
3887 }
3888#endif
3889
3890#ifdef FEAT_FOLDING
3891 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3892 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003893 int fdc = compute_foldcolumn(wp, 0);
3894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003896 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003898 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003899 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003900 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003901 p_extra_free = alloc(12 + 1);
3902
3903 if (p_extra_free != NULL)
3904 {
3905 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3906 n_extra = fdc;
3907 p_extra_free[n_extra] = NUL;
3908 p_extra = p_extra_free;
3909 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003910 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913 }
3914#endif
3915
3916#ifdef FEAT_SIGNS
3917 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3918 {
3919 draw_state = WL_SIGN;
3920 /* Show the sign column when there are any signs in this
3921 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003922 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003924 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003926 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927# endif
3928
3929 /* Draw two cells with the sign value or blank. */
3930 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003931 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 n_extra = 2;
3933
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003934 if (row == startrow
3935#ifdef FEAT_DIFF
3936 + filler_lines && filler_todo <= 0
3937#endif
3938 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
3940 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3941 SIGN_TEXT);
3942# ifdef FEAT_SIGN_ICONS
3943 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3944 SIGN_ICON);
3945 if (gui.in_use && icon_sign != 0)
3946 {
3947 /* Use the image in this position. */
3948 c_extra = SIGN_BYTE;
3949# ifdef FEAT_NETBEANS_INTG
3950 if (buf_signcount(wp->w_buffer, lnum) > 1)
3951 c_extra = MULTISIGN_BYTE;
3952# endif
3953 char_attr = icon_sign;
3954 }
3955 else
3956# endif
3957 if (text_sign != 0)
3958 {
3959 p_extra = sign_get_text(text_sign);
3960 if (p_extra != NULL)
3961 {
3962 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003963 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
3965 char_attr = sign_get_attr(text_sign, FALSE);
3966 }
3967 }
3968 }
3969 }
3970#endif
3971
3972 if (draw_state == WL_NR - 1 && n_extra == 0)
3973 {
3974 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003975 /* Display the absolute or relative line number. After the
3976 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3977 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 && (row == startrow
3979#ifdef FEAT_DIFF
3980 + filler_lines
3981#endif
3982 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3983 {
3984 /* Draw the line number (empty space after wrapping). */
3985 if (row == startrow
3986#ifdef FEAT_DIFF
3987 + filler_lines
3988#endif
3989 )
3990 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003991 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003992 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003993
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003994 if (wp->w_p_nu && !wp->w_p_rnu)
3995 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003996 num = (long)lnum;
3997 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003998 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003999 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004000 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004001 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004002 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004003 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004004 num = lnum;
4005 fmt = "%-*ld ";
4006 }
4007 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004008
Bram Moolenaar700e7342013-01-30 12:31:36 +01004009 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004010 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (wp->w_skipcol > 0)
4012 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4013 *p_extra = '-';
4014#ifdef FEAT_RIGHTLEFT
4015 if (wp->w_p_rl) /* reverse line numbers */
4016 rl_mirror(extra);
4017#endif
4018 p_extra = extra;
4019 c_extra = NUL;
4020 }
4021 else
4022 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004023 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004024 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004025#ifdef FEAT_SYN_HL
4026 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004027 * the current line differently.
4028 * TODO: Can we use CursorLine instead of CursorLineNr
4029 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004030 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004031 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004032 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
4035 }
4036
Bram Moolenaar597a4222014-06-25 14:39:50 +02004037#ifdef FEAT_LINEBREAK
4038 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4039 && n_extra == 0 && *p_sbr != NUL)
4040 /* draw indent after showbreak value */
4041 draw_state = WL_BRI;
4042 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4043 /* After the showbreak, draw the breakindent */
4044 draw_state = WL_BRI - 1;
4045
4046 /* draw 'breakindent': indent wrapped text accordingly */
4047 if (draw_state == WL_BRI - 1 && n_extra == 0)
4048 {
4049 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004050 /* if need_showbreak is set, breakindent also applies */
4051 if (wp->w_p_bri && n_extra == 0
4052 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004053# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004054 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004055# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004056 )
4057 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004058 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004059# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004060 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004061 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004062 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004063# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004064 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4065 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004066 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004067# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004068 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004069# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004070 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004071 c_extra = ' ';
4072 n_extra = get_breakindent_win(wp,
4073 ml_get_buf(wp->w_buffer, lnum, FALSE));
4074 /* Correct end of highlighted area for 'breakindent',
4075 * required when 'linebreak' is also set. */
4076 if (tocol == vcol)
4077 tocol += n_extra;
4078 }
4079 }
4080#endif
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4083 if (draw_state == WL_SBR - 1 && n_extra == 0)
4084 {
4085 draw_state = WL_SBR;
4086# ifdef FEAT_DIFF
4087 if (filler_todo > 0)
4088 {
4089 /* Draw "deleted" diff line(s). */
4090 if (char2cells(fill_diff) > 1)
4091 c_extra = '-';
4092 else
4093 c_extra = fill_diff;
4094# ifdef FEAT_RIGHTLEFT
4095 if (wp->w_p_rl)
4096 n_extra = col + 1;
4097 else
4098# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004099 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004100 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102# endif
4103# ifdef FEAT_LINEBREAK
4104 if (*p_sbr != NUL && need_showbreak)
4105 {
4106 /* Draw 'showbreak' at the start of each broken line. */
4107 p_extra = p_sbr;
4108 c_extra = NUL;
4109 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004110 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004112 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 /* Correct end of highlighted area for 'showbreak',
4114 * required when 'linebreak' is also set. */
4115 if (tocol == vcol)
4116 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004117#ifdef FEAT_SYN_HL
4118 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004119 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004120 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004121 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124# endif
4125 }
4126#endif
4127
4128 if (draw_state == WL_LINE - 1 && n_extra == 0)
4129 {
4130 draw_state = WL_LINE;
4131 if (saved_n_extra)
4132 {
4133 /* Continue item from end of wrapped line. */
4134 n_extra = saved_n_extra;
4135 c_extra = saved_c_extra;
4136 p_extra = saved_p_extra;
4137 char_attr = saved_char_attr;
4138 }
4139 else
4140 char_attr = 0;
4141 }
4142 }
4143
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004144 // When still displaying '$' of change command, stop at cursor.
4145 // When only displaying the (relative) line number and that's done,
4146 // stop here.
4147 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004148 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149#ifdef FEAT_DIFF
4150 && filler_todo <= 0
4151#endif
4152 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004153 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004155 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004156 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004157 /* Pretend we have finished updating the window. Except when
4158 * 'cursorcolumn' is set. */
4159#ifdef FEAT_SYN_HL
4160 if (wp->w_p_cuc)
4161 row = wp->w_cline_row + wp->w_cline_height;
4162 else
4163#endif
4164 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 break;
4166 }
4167
4168 if (draw_state == WL_LINE && area_highlighting)
4169 {
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;
4333
4334 // Check if any active property ends.
4335 for (pi = 0; pi < text_props_active; ++pi)
4336 {
4337 int tpi = text_prop_idxs[pi];
4338
Bram Moolenaar8caa10a2018-12-30 22:07:40 +01004339 if (vcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004340 + text_props[tpi].tp_len)
4341 {
4342 if (pi + 1 < text_props_active)
4343 mch_memmove(text_prop_idxs + pi,
4344 text_prop_idxs + pi + 1,
4345 sizeof(int)
4346 * (text_props_active - (pi + 1)));
4347 --text_props_active;
4348 --pi;
4349 }
4350 }
4351
4352 // Add any text property that starts in this column.
4353 while (text_prop_next < text_prop_count
Bram Moolenaar8caa10a2018-12-30 22:07:40 +01004354 && vcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004355 text_prop_idxs[text_props_active++] = text_prop_next++;
4356
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004357 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004358 if (text_props_active > 0)
4359 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004360 // Sort the properties on priority and/or starting last.
4361 // Then combine the attributes, highest priority last.
4362 current_text_props = text_props;
4363 current_buf = wp->w_buffer;
4364 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4365 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004366
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004367 for (pi = 0; pi < text_props_active; ++pi)
4368 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004369 int tpi = text_prop_idxs[pi];
4370 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004371
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004372 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004373 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004374 int pt_attr = syn_id2attr(pt->pt_hl_id);
4375
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004376 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004377 if (text_prop_attr == 0)
4378 text_prop_attr = pt_attr;
4379 else
4380 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004381 }
4382 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004383 }
4384 }
4385#endif
4386
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004387 /* Decide which of the highlight attributes to use. */
4388 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004389#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004390 if (area_attr != 0)
4391 char_attr = hl_combine_attr(line_attr, area_attr);
4392 else if (search_attr != 0)
4393 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004394 /* Use line_attr when not in the Visual or 'incsearch' area
4395 * (area_attr may be 0 when "noinvcur" is set). */
4396 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004397 || vcol < fromcol || vcol_prev < fromcol_prev
4398 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004399 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004400#else
4401 if (area_attr != 0)
4402 char_attr = area_attr;
4403 else if (search_attr != 0)
4404 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004405#endif
4406 else
4407 {
4408 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004409#ifdef FEAT_TEXT_PROP
4410 if (text_prop_type != NULL)
4411 char_attr = text_prop_attr;
4412 else
4413#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004414#ifdef FEAT_SYN_HL
4415 if (has_syntax)
4416 char_attr = syntax_attr;
4417 else
4418#endif
4419 char_attr = 0;
4420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
4422
4423 /*
4424 * Get the next character to put on the screen.
4425 */
4426 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004427 * The "p_extra" points to the extra stuff that is inserted to
4428 * represent special characters (non-printable stuff) and other
4429 * things. When all characters are the same, c_extra is used.
4430 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4431 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4433 */
4434 if (n_extra > 0)
4435 {
4436 if (c_extra != NUL)
4437 {
4438 c = c_extra;
4439#ifdef FEAT_MBYTE
4440 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004441 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
4443 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004444 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004445 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447 else
4448 mb_utf8 = FALSE;
4449#endif
4450 }
4451 else
4452 {
4453 c = *p_extra;
4454#ifdef FEAT_MBYTE
4455 if (has_mbyte)
4456 {
4457 mb_c = c;
4458 if (enc_utf8)
4459 {
4460 /* If the UTF-8 character is more than one byte:
4461 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004462 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 mb_utf8 = FALSE;
4464 if (mb_l > n_extra)
4465 mb_l = 1;
4466 else if (mb_l > 1)
4467 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004468 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004470 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 }
4473 else
4474 {
4475 /* if this is a DBCS character, put it in "mb_c" */
4476 mb_l = MB_BYTE2LEN(c);
4477 if (mb_l >= n_extra)
4478 mb_l = 1;
4479 else if (mb_l > 1)
4480 mb_c = (c << 8) + p_extra[1];
4481 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004482 if (mb_l == 0) /* at the NUL at end-of-line */
4483 mb_l = 1;
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 /* If a double-width char doesn't fit display a '>' in the
4486 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004487 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488# ifdef FEAT_RIGHTLEFT
4489 wp->w_p_rl ? (col <= 0) :
4490# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004491 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 && (*mb_char2cells)(mb_c) == 2)
4493 {
4494 c = '>';
4495 mb_c = c;
4496 mb_l = 1;
4497 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004498 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 /* put the pointer back to output the double-width
4500 * character at the start of the next line. */
4501 ++n_extra;
4502 --p_extra;
4503 }
4504 else
4505 {
4506 n_extra -= mb_l - 1;
4507 p_extra += mb_l - 1;
4508 }
4509 }
4510#endif
4511 ++p_extra;
4512 }
4513 --n_extra;
4514 }
4515 else
4516 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004517#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004518 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004519#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004520
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004521 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004522 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 /*
4524 * Get a character from the line itself.
4525 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004526 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004527#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004528 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530#ifdef FEAT_MBYTE
4531 if (has_mbyte)
4532 {
4533 mb_c = c;
4534 if (enc_utf8)
4535 {
4536 /* If the UTF-8 character is more than one byte: Decode it
4537 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004538 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 mb_utf8 = FALSE;
4540 if (mb_l > 1)
4541 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004542 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 /* Overlong encoded ASCII or ASCII with composing char
4544 * is displayed normally, except a NUL. */
4545 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004546 {
4547 c = mb_c;
4548# ifdef FEAT_LINEBREAK
4549 c0 = mb_c;
4550# endif
4551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004553
4554 /* At start of the line we can have a composing char.
4555 * Draw it as a space with a composing char. */
4556 if (utf_iscomposing(mb_c))
4557 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004558 int i;
4559
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004560 for (i = Screen_mco - 1; i > 0; --i)
4561 u8cc[i] = u8cc[i - 1];
4562 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004563 mb_c = ' ';
4564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566
4567 if ((mb_l == 1 && c >= 0x80)
4568 || (mb_l >= 1 && mb_c == 0)
4569 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004570# ifdef UNICODE16
4571 || mb_c >= 0x10000
4572# endif
4573 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
4575 /*
4576 * Illegal UTF-8 byte: display as <xx>.
4577 * Non-BMP character : display as ? or fullwidth ?.
4578 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004579# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
4583 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004584# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (wp->w_p_rl) /* reverse */
4586 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004589# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 else if (utf_char2cells(mb_c) != 2)
4591 STRCPY(extra, "?");
4592 else
4593 /* 0xff1f in UTF-8: full-width '?' */
4594 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
4597 p_extra = extra;
4598 c = *p_extra;
4599 mb_c = mb_ptr2char_adv(&p_extra);
4600 mb_utf8 = (c >= 0x80);
4601 n_extra = (int)STRLEN(p_extra);
4602 c_extra = NUL;
4603 if (area_attr == 0 && search_attr == 0)
4604 {
4605 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004606 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 saved_attr2 = char_attr; /* save current attr */
4608 }
4609 }
4610 else if (mb_l == 0) /* at the NUL at end-of-line */
4611 mb_l = 1;
4612#ifdef FEAT_ARABIC
4613 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4614 {
4615 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004616 int pc, pc1, nc;
4617 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 /* The idea of what is the previous and next
4620 * character depends on 'rightleft'. */
4621 if (wp->w_p_rl)
4622 {
4623 pc = prev_c;
4624 pc1 = prev_c1;
4625 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004626 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 else
4629 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004630 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004632 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
4634 prev_c = mb_c;
4635
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004636 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638 else
4639 prev_c = mb_c;
4640#endif
4641 }
4642 else /* enc_dbcs */
4643 {
4644 mb_l = MB_BYTE2LEN(c);
4645 if (mb_l == 0) /* at the NUL at end-of-line */
4646 mb_l = 1;
4647 else if (mb_l > 1)
4648 {
4649 /* We assume a second byte below 32 is illegal.
4650 * Hopefully this is OK for all double-byte encodings!
4651 */
4652 if (ptr[1] >= 32)
4653 mb_c = (c << 8) + ptr[1];
4654 else
4655 {
4656 if (ptr[1] == NUL)
4657 {
4658 /* head byte at end of line */
4659 mb_l = 1;
4660 transchar_nonprint(extra, c);
4661 }
4662 else
4663 {
4664 /* illegal tail byte */
4665 mb_l = 2;
4666 STRCPY(extra, "XX");
4667 }
4668 p_extra = extra;
4669 n_extra = (int)STRLEN(extra) - 1;
4670 c_extra = NUL;
4671 c = *p_extra++;
4672 if (area_attr == 0 && search_attr == 0)
4673 {
4674 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004675 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 saved_attr2 = char_attr; /* save current attr */
4677 }
4678 mb_c = c;
4679 }
4680 }
4681 }
4682 /* If a double-width char doesn't fit display a '>' in the
4683 * last column; the character is displayed at the start of the
4684 * next line. */
4685 if ((
4686# ifdef FEAT_RIGHTLEFT
4687 wp->w_p_rl ? (col <= 0) :
4688# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004689 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 && (*mb_char2cells)(mb_c) == 2)
4691 {
4692 c = '>';
4693 mb_c = c;
4694 mb_utf8 = FALSE;
4695 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004696 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 /* Put pointer back so that the character will be
4698 * displayed at the start of the next line. */
4699 --ptr;
4700 }
4701 else if (*ptr != NUL)
4702 ptr += mb_l - 1;
4703
4704 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004705 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004706 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004707 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004710 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 c = ' ';
4712 if (area_attr == 0 && search_attr == 0)
4713 {
4714 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004715 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 saved_attr2 = char_attr; /* save current attr */
4717 }
4718 mb_c = c;
4719 mb_utf8 = FALSE;
4720 mb_l = 1;
4721 }
4722
4723 }
4724#endif
4725 ++ptr;
4726
4727 if (extra_check)
4728 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004729#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004730 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004731#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004732
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004733#ifdef FEAT_TERMINAL
4734 if (get_term_attr)
4735 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004736 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004737
4738 if (!attr_pri)
4739 char_attr = syntax_attr;
4740 else
4741 char_attr = hl_combine_attr(syntax_attr, char_attr);
4742 }
4743#endif
4744
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004745#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004746 // Get syntax attribute, unless still at the start of the line
4747 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004748 v = (long)(ptr - line);
4749 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
4751 /* Get the syntax attribute for the character. If there
4752 * is an error, disable syntax highlighting. */
4753 save_did_emsg = did_emsg;
4754 did_emsg = FALSE;
4755
Bram Moolenaar217ad922005-03-20 22:37:15 +00004756 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004757# ifdef FEAT_SPELL
4758 has_spell ? &can_spell :
4759# endif
4760 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
4762 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004763 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004764 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004765 has_syntax = FALSE;
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 else
4768 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004769#ifdef SYN_TIME_LIMIT
4770 if (wp->w_s->b_syn_slow)
4771 has_syntax = FALSE;
4772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
4774 /* Need to get the line again, a multi-line regexp may
4775 * have made it invalid. */
4776 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4777 ptr = line + v;
4778
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004779# ifdef FEAT_TEXT_PROP
4780 // Text properties overrule syntax highlighting.
4781 if (text_prop_attr == 0)
4782#endif
4783 {
4784 if (!attr_pri)
4785 char_attr = syntax_attr;
4786 else
4787 char_attr = hl_combine_attr(syntax_attr, char_attr);
4788 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004789# ifdef FEAT_CONCEAL
4790 /* no concealing past the end of the line, it interferes
4791 * with line highlighting */
4792 if (c == NUL)
4793 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004794 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004795 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004798#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004799
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004800#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004801 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004802 * Only do this when there is no syntax highlighting, the
4803 * @Spell cluster is not used or the current syntax item
4804 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004805 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004806 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004807 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004808 if (c != 0 && (
4809# ifdef FEAT_SYN_HL
4810 !has_syntax ||
4811# endif
4812 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004813 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004814 char_u *prev_ptr, *p;
4815 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004816 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004817# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004818 if (has_mbyte)
4819 {
4820 prev_ptr = ptr - mb_l;
4821 v -= mb_l - 1;
4822 }
4823 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004824# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004825 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004826
4827 /* Use nextline[] if possible, it has the start of the
4828 * next line concatenated. */
4829 if ((prev_ptr - line) - nextlinecol >= 0)
4830 p = nextline + (prev_ptr - line) - nextlinecol;
4831 else
4832 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004833 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004834 len = spell_check(wp, p, &spell_hlf, &cap_col,
4835 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004836 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004837
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004838 /* In Insert mode only highlight a word that
4839 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004840 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004841 && (State & INSERT) != 0
4842 && wp->w_cursor.lnum == lnum
4843 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004844 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004845 && wp->w_cursor.col < (colnr_T)word_end)
4846 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004847 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004848 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004849 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004850
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004851 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004852 && (p - nextline) + len > nextline_idx)
4853 {
4854 /* Remember that the good word continues at the
4855 * start of the next line. */
4856 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004857 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004858 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004859
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004860 /* Turn index into actual attributes. */
4861 if (spell_hlf != HLF_COUNT)
4862 spell_attr = highlight_attr[spell_hlf];
4863
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004864 if (cap_col > 0)
4865 {
4866 if (p != prev_ptr
4867 && (p - nextline) + cap_col >= nextline_idx)
4868 {
4869 /* Remember that the word in the next line
4870 * must start with a capital. */
4871 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004872 cap_col = (int)((p - nextline) + cap_col
4873 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004874 }
4875 else
4876 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004877 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004878 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004879 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004880 }
4881 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004882 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004883 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004884 char_attr = hl_combine_attr(char_attr, spell_attr);
4885 else
4886 char_attr = hl_combine_attr(spell_attr, char_attr);
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888#endif
4889#ifdef FEAT_LINEBREAK
4890 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004891 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004893 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004894 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004896# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004897 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004898# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004899 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004901 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004903 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004904
Bram Moolenaar597a4222014-06-25 14:39:50 +02004905 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004906 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004907 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004908 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004909# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004910 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004911 wp->w_buffer->b_p_vts_array) - 1;
4912# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004913 n_extra = (int)wp->w_buffer->b_p_ts
4914 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004915# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004916
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004917# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004918 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004919# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004921# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004922 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004923 {
4924#ifdef FEAT_CONCEAL
4925 if (c == TAB)
4926 /* See "Tab alignment" below. */
4927 FIX_FOR_BOGUSCOLS;
4928#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004929 if (!wp->w_p_list)
4930 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933#endif
4934
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004935 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4936 */
4937 if (wp->w_p_list
4938 && (((c == 160
4939#ifdef FEAT_MBYTE
4940 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4941#endif
4942 ) && lcs_nbsp)
4943 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4944 {
4945 c = (c == ' ') ? lcs_space : lcs_nbsp;
4946 if (area_attr == 0 && search_attr == 0)
4947 {
4948 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004949 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004950 saved_attr2 = char_attr; /* save current attr */
4951 }
4952#ifdef FEAT_MBYTE
4953 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004954 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004955 {
4956 mb_utf8 = TRUE;
4957 u8cc[0] = 0;
4958 c = 0xc0;
4959 }
4960 else
4961 mb_utf8 = FALSE;
4962#endif
4963 }
4964
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4966 {
4967 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004968 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
4970 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004971 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 saved_attr2 = char_attr; /* save current attr */
4973 }
4974#ifdef FEAT_MBYTE
4975 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004976 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
4978 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004979 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004980 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 else
4983 mb_utf8 = FALSE;
4984#endif
4985 }
4986 }
4987
4988 /*
4989 * Handling of non-printable characters.
4990 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004991 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 /*
4994 * when getting a character from the file, we may have to
4995 * turn it into something else on the way to putting it
4996 * into "ScreenLines".
4997 */
4998 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4999 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005000 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005001 long vcol_adjusted = vcol; /* removed showbreak length */
5002#ifdef FEAT_LINEBREAK
5003 /* only adjust the tab_len, when at the first column
5004 * after the showbreak value was drawn */
5005 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5006 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005009#ifdef FEAT_VARTABS
5010 tab_len = tabstop_padding(vcol_adjusted,
5011 wp->w_buffer->b_p_ts,
5012 wp->w_buffer->b_p_vts_array) - 1;
5013#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005014 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005015 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5016#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005017
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005018#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005019 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005020#endif
5021 /* tab amount depends on current column */
5022 n_extra = tab_len;
5023#ifdef FEAT_LINEBREAK
5024 else
5025 {
5026 char_u *p;
5027 int len = n_extra;
5028 int i;
5029 int saved_nextra = n_extra;
5030
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005031#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005032 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005033 /* there are characters to conceal */
5034 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005035 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5036 */
5037 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5038 && n_extra > tab_len)
5039 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005040#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005041
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005042 /* if n_extra > 0, it gives the number of chars, to
5043 * use for a tab, else we need to calculate the width
5044 * for a tab */
5045#ifdef FEAT_MBYTE
5046 len = (tab_len * mb_char2len(lcs_tab2));
5047 if (n_extra > 0)
5048 len += n_extra - tab_len;
5049#endif
5050 c = lcs_tab1;
5051 p = alloc((unsigned)(len + 1));
5052 vim_memset(p, ' ', len);
5053 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005054 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005055 p_extra_free = p;
5056 for (i = 0; i < tab_len; i++)
5057 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005058 if (*p == NUL)
5059 {
5060 tab_len = i;
5061 break;
5062 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005063#ifdef FEAT_MBYTE
5064 mb_char2bytes(lcs_tab2, p);
5065 p += mb_char2len(lcs_tab2);
5066 n_extra += mb_char2len(lcs_tab2)
5067 - (saved_nextra > 0 ? 1 : 0);
5068#else
5069 p[i] = lcs_tab2;
5070#endif
5071 }
5072 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005073#ifdef FEAT_CONCEAL
5074 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5075 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005076 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005077 n_extra -= vcol_off;
5078#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005079 }
5080#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005081#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005082 {
5083 int vc_saved = vcol_off;
5084
5085 /* Tab alignment should be identical regardless of
5086 * 'conceallevel' value. So tab compensates of all
5087 * previous concealed characters, and thus resets
5088 * vcol_off and boguscols accumulated so far in the
5089 * line. Note that the tab can be longer than
5090 * 'tabstop' when there are concealed characters. */
5091 FIX_FOR_BOGUSCOLS;
5092
5093 /* Make sure, the highlighting for the tab char will be
5094 * correctly set further below (effectively reverts the
5095 * FIX_FOR_BOGSUCOLS macro */
5096 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005097 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005098 tab_len += vc_saved;
5099 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101#ifdef FEAT_MBYTE
5102 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5103#endif
5104 if (wp->w_p_list)
5105 {
5106 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005107#ifdef FEAT_LINEBREAK
5108 if (wp->w_p_lbr)
5109 c_extra = NUL; /* using p_extra from above */
5110 else
5111#endif
5112 c_extra = lcs_tab2;
5113 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005114 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 saved_attr2 = char_attr; /* save current attr */
5116#ifdef FEAT_MBYTE
5117 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005118 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
5120 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005121 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005122 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124#endif
5125 }
5126 else
5127 {
5128 c_extra = ' ';
5129 c = ' ';
5130 }
5131 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005132 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005133 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005134 || ((fromcol >= 0 || fromcol_prev >= 0)
5135 && tocol > vcol
5136 && VIsual_mode != Ctrl_V
5137 && (
5138# ifdef FEAT_RIGHTLEFT
5139 wp->w_p_rl ? (col >= 0) :
5140# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005141 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005142 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005143 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005144 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005145 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005147 /* Display a '$' after the line or highlight an extra
5148 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5150 /* For a diff line the highlighting continues after the
5151 * "$". */
5152 if (
5153# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005154 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155# ifdef LINE_ATTR
5156 &&
5157# endif
5158# endif
5159# ifdef LINE_ATTR
5160 line_attr == 0
5161# endif
5162 )
5163#endif
5164 {
5165#ifdef FEAT_VIRTUALEDIT
5166 /* In virtualedit, visual selections may extend
5167 * beyond end of line. */
5168 if (area_highlighting && virtual_active()
5169 && tocol != MAXCOL && vcol < tocol)
5170 n_extra = 0;
5171 else
5172#endif
5173 {
5174 p_extra = at_end_str;
5175 n_extra = 1;
5176 c_extra = NUL;
5177 }
5178 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005179 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005180 c = lcs_eol;
5181 else
5182 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 lcs_eol_one = -1;
5184 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005185 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005187 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 n_attr = 1;
5189 }
5190#ifdef FEAT_MBYTE
5191 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005192 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
5194 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005195 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005196 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198 else
5199 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5200#endif
5201 }
5202 else if (c != NUL)
5203 {
5204 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005205 if (n_extra == 0)
5206 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#ifdef FEAT_RIGHTLEFT
5208 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5209 rl_mirror(p_extra); /* reverse "<12>" */
5210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005212#ifdef FEAT_LINEBREAK
5213 if (wp->w_p_lbr)
5214 {
5215 char_u *p;
5216
5217 c = *p_extra;
5218 p = alloc((unsigned)n_extra + 1);
5219 vim_memset(p, ' ', n_extra);
5220 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5221 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005222 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005223 p_extra_free = p_extra = p;
5224 }
5225 else
5226#endif
5227 {
5228 n_extra = byte2cells(c) - 1;
5229 c = *p_extra++;
5230 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005231 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005234 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 saved_attr2 = char_attr; /* save current attr */
5236 }
5237#ifdef FEAT_MBYTE
5238 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5239#endif
5240 }
5241#ifdef FEAT_VIRTUALEDIT
5242 else if (VIsual_active
5243 && (VIsual_mode == Ctrl_V
5244 || VIsual_mode == 'v')
5245 && virtual_active()
5246 && tocol != MAXCOL
5247 && vcol < tocol
5248 && (
5249# ifdef FEAT_RIGHTLEFT
5250 wp->w_p_rl ? (col >= 0) :
5251# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005252 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
5254 c = ' ';
5255 --ptr; /* put it back at the NUL */
5256 }
5257#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005258#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 else if ((
5260# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005261 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005263# ifdef FEAT_TERMINAL
5264 term_attr != 0 ||
5265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 ) && (
5268# ifdef FEAT_RIGHTLEFT
5269 wp->w_p_rl ? (col >= 0) :
5270# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005271 (col
5272# ifdef FEAT_CONCEAL
5273 - boguscols
5274# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005275 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
5277 /* Highlight until the right side of the window */
5278 c = ' ';
5279 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005280
5281 /* Remember we do the char for line highlighting. */
5282 ++did_line_attr;
5283
5284 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005285 if (line_attr != 0 && char_attr == search_attr
5286 && (did_line_attr > 1
5287 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005288 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289# ifdef FEAT_DIFF
5290 if (diff_hlf == HLF_TXD)
5291 {
5292 diff_hlf = HLF_CHD;
5293 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005294 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005295 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005296 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5297 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005298 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
5301# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005302# ifdef FEAT_TERMINAL
5303 if (term_attr != 0)
5304 {
5305 char_attr = term_attr;
5306 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5307 char_attr = hl_combine_attr(char_attr,
5308 HL_ATTR(HLF_CUL));
5309 }
5310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
5312#endif
5313 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005314
5315#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005316 if ( wp->w_p_cole > 0
5317 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005318 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005319 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005320 && !(lnum_in_visual_area
5321 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005322 {
5323 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005324 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005325 && (syn_get_sub_char() != NUL || match_conc
5326 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005327 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005328 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005329 /* First time at this concealed item: display one
5330 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005331 if (match_conc)
5332 c = match_conc;
5333 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005334 c = syn_get_sub_char();
5335 else if (lcs_conceal != NUL)
5336 c = lcs_conceal;
5337 else
5338 c = ' ';
5339
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005340 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005341
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005342 if (n_extra > 0)
5343 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005344 vcol += n_extra;
5345 if (wp->w_p_wrap && n_extra > 0)
5346 {
5347# ifdef FEAT_RIGHTLEFT
5348 if (wp->w_p_rl)
5349 {
5350 col -= n_extra;
5351 boguscols -= n_extra;
5352 }
5353 else
5354# endif
5355 {
5356 boguscols += n_extra;
5357 col += n_extra;
5358 }
5359 }
5360 n_extra = 0;
5361 n_attr = 0;
5362 }
5363 else if (n_skip == 0)
5364 {
5365 is_concealing = TRUE;
5366 n_skip = 1;
5367 }
5368# ifdef FEAT_MBYTE
5369 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005370 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005371 {
5372 mb_utf8 = TRUE;
5373 u8cc[0] = 0;
5374 c = 0xc0;
5375 }
5376 else
5377 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5378# endif
5379 }
5380 else
5381 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005382 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005383 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005384 }
5385#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005388#ifdef FEAT_CONCEAL
5389 /* In the cursor line and we may be concealing characters: correct
5390 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005391 if (!did_wcol && draw_state == WL_LINE
5392 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005393 && conceal_cursor_line(wp)
5394 && (int)wp->w_virtcol <= vcol + n_skip)
5395 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005396# ifdef FEAT_RIGHTLEFT
5397 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005398 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005399 else
5400# endif
5401 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005402 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005403 did_wcol = TRUE;
5404 }
5405#endif
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 /* Don't override visual selection highlighting. */
5408 if (n_attr > 0
5409 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005410 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 char_attr = extra_attr;
5412
Bram Moolenaar81695252004-12-29 20:58:21 +00005413#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 /* XIM don't send preedit_start and preedit_end, but they send
5415 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5416 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005417 if (p_imst == IM_ON_THE_SPOT
5418 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005419 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 && (State & INSERT)
5421 && !p_imdisable
5422 && im_is_preediting()
5423 && draw_state == WL_LINE)
5424 {
5425 colnr_T tcol;
5426
5427 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005428 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 else
5430 tcol = preedit_end_col;
5431 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5432 {
5433 if (feedback_old_attr < 0)
5434 {
5435 feedback_col = 0;
5436 feedback_old_attr = char_attr;
5437 }
5438 char_attr = im_get_feedback_attr(feedback_col);
5439 if (char_attr < 0)
5440 char_attr = feedback_old_attr;
5441 feedback_col++;
5442 }
5443 else if (feedback_old_attr >= 0)
5444 {
5445 char_attr = feedback_old_attr;
5446 feedback_old_attr = -1;
5447 feedback_col = 0;
5448 }
5449 }
5450#endif
5451 /*
5452 * Handle the case where we are in column 0 but not on the first
5453 * character of the line and the user wants us to show us a
5454 * special character (via 'listchars' option "precedes:<char>".
5455 */
5456 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005457 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5459#ifdef FEAT_DIFF
5460 && filler_todo <= 0
5461#endif
5462 && draw_state > WL_NR
5463 && c != NUL)
5464 {
5465 c = lcs_prec;
5466 lcs_prec_todo = NUL;
5467#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005468 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5469 {
5470 /* Double-width character being overwritten by the "precedes"
5471 * character, need to fill up half the character. */
5472 c_extra = MB_FILLER_CHAR;
5473 n_extra = 1;
5474 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005475 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005478 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {
5480 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005481 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005482 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 else
5485 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5486#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005487 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
5489 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005490 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 n_attr3 = 1;
5492 }
5493 }
5494
5495 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005496 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005498 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005499#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005500 || did_line_attr == 1
5501#endif
5502 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005504#ifdef FEAT_SEARCH_EXTRA
5505 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005506
5507 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005508 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005509 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005510#endif
5511
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005512 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 * highlight match at end of line. If it's beyond the last
5514 * char on the screen, just overwrite that one (tricky!) Not
5515 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005516#ifdef FEAT_SEARCH_EXTRA
5517 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005518 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005519 prevcol_hl_flag = TRUE;
5520 else
5521 {
5522 cur = wp->w_match_head;
5523 while (cur != NULL)
5524 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005525 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005526 {
5527 prevcol_hl_flag = TRUE;
5528 break;
5529 }
5530 cur = cur->next;
5531 }
5532 }
5533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005535 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005536 && (VIsual_mode != Ctrl_V
5537 || lnum == VIsual.lnum
5538 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005539 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540#ifdef FEAT_SEARCH_EXTRA
5541 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005542 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005543# ifdef FEAT_SYN_HL
5544 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5545 && !(wp == curwin && VIsual_active))
5546# endif
5547# ifdef FEAT_DIFF
5548 && diff_hlf == (hlf_T)0
5549# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005550# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005551 && did_line_attr <= 1
5552# endif
5553 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554#endif
5555 ))
5556 {
5557 int n = 0;
5558
5559#ifdef FEAT_RIGHTLEFT
5560 if (wp->w_p_rl)
5561 {
5562 if (col < 0)
5563 n = 1;
5564 }
5565 else
5566#endif
5567 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005568 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 n = -1;
5570 }
5571 if (n != 0)
5572 {
5573 /* At the window boundary, highlight the last character
5574 * instead (better than nothing). */
5575 off += n;
5576 col += n;
5577 }
5578 else
5579 {
5580 /* Add a blank character to highlight. */
5581 ScreenLines[off] = ' ';
5582#ifdef FEAT_MBYTE
5583 if (enc_utf8)
5584 ScreenLinesUC[off] = 0;
5585#endif
5586 }
5587#ifdef FEAT_SEARCH_EXTRA
5588 if (area_attr == 0)
5589 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005590 /* Use attributes from match with highest priority among
5591 * 'search_hl' and the match list. */
5592 char_attr = search_hl.attr;
5593 cur = wp->w_match_head;
5594 shl_flag = FALSE;
5595 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005596 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005597 if (shl_flag == FALSE
5598 && ((cur != NULL
5599 && cur->priority > SEARCH_HL_PRIORITY)
5600 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005601 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005602 shl = &search_hl;
5603 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005604 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005605 else
5606 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005607 if ((ptr - line) - 1 == (long)shl->startcol
5608 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005609 char_attr = shl->attr;
5610 if (shl != &search_hl && cur != NULL)
5611 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614#endif
5615 ScreenAttrs[off] = char_attr;
5616#ifdef FEAT_RIGHTLEFT
5617 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005620 --off;
5621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 else
5623#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005626 ++off;
5627 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005628 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005629#ifdef FEAT_SYN_HL
5630 eol_hl_off = 1;
5631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
Bram Moolenaar91170f82006-05-05 21:15:17 +00005635 /*
5636 * At end of the text line.
5637 */
5638 if (c == NUL)
5639 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005640#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005641 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005642 if (wp->w_p_wrap)
5643 v = wp->w_skipcol;
5644 else
5645 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005646
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005647 /* check if line ends before left margin */
5648 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005649 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005650#ifdef FEAT_CONCEAL
5651 /* Get rid of the boguscols now, we want to draw until the right
5652 * edge for 'cursorcolumn'. */
5653 col -= boguscols;
5654 boguscols = 0;
5655#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005656
5657 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005658 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005659
5660 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005661 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5662 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005663 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005664 && lnum != wp->w_cursor.lnum)
5665 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005666# ifdef FEAT_RIGHTLEFT
5667 && !wp->w_p_rl
5668# endif
5669 )
5670 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005671 int rightmost_vcol = 0;
5672 int i;
5673
5674 if (wp->w_p_cuc)
5675 rightmost_vcol = wp->w_virtcol;
5676 if (draw_color_col)
5677 /* determine rightmost colorcolumn to possibly draw */
5678 for (i = 0; color_cols[i] >= 0; ++i)
5679 if (rightmost_vcol < color_cols[i])
5680 rightmost_vcol = color_cols[i];
5681
Bram Moolenaar02631462017-09-22 15:20:32 +02005682 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005683 {
5684 ScreenLines[off] = ' ';
5685#ifdef FEAT_MBYTE
5686 if (enc_utf8)
5687 ScreenLinesUC[off] = 0;
5688#endif
5689 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005690 if (draw_color_col)
5691 draw_color_col = advance_color_col(VCOL_HLC,
5692 &color_cols);
5693
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005694 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005695 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005696 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005697 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005698 else
5699 ScreenAttrs[off++] = 0;
5700
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005701 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005702 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005703
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005704 ++vcol;
5705 }
5706 }
5707#endif
5708
Bram Moolenaar53f81742017-09-22 14:35:51 +02005709 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005710 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 row++;
5712
5713 /*
5714 * Update w_cline_height and w_cline_folded if the cursor line was
5715 * updated (saves a call to plines() later).
5716 */
5717 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5718 {
5719 curwin->w_cline_row = startrow;
5720 curwin->w_cline_height = row - startrow;
5721#ifdef FEAT_FOLDING
5722 curwin->w_cline_folded = FALSE;
5723#endif
5724 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5725 }
5726
5727 break;
5728 }
5729
5730 /* line continues beyond line end */
5731 if (lcs_ext
5732 && !wp->w_p_wrap
5733#ifdef FEAT_DIFF
5734 && filler_todo <= 0
5735#endif
5736 && (
5737#ifdef FEAT_RIGHTLEFT
5738 wp->w_p_rl ? col == 0 :
5739#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005740 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005742 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5744 {
5745 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005746 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747#ifdef FEAT_MBYTE
5748 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005749 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005752 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005753 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755 else
5756 mb_utf8 = FALSE;
5757#endif
5758 }
5759
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005760#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005761 /* advance to the next 'colorcolumn' */
5762 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005763 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005764
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005765 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005766 * highlight the cursor position itself.
5767 * Also highlight the 'colorcolumn' if it is different than
5768 * 'cursorcolumn' */
5769 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005770 if (draw_state == WL_LINE && !lnum_in_visual_area
5771 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005772 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005773 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005774 && lnum != wp->w_cursor.lnum)
5775 {
5776 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005777 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005778 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005779 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005780 {
5781 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005782 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005783 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005784 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005785#endif
5786
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 /*
5788 * Store character to be displayed.
5789 * Skip characters that are left of the screen for 'nowrap'.
5790 */
5791 vcol_prev = vcol;
5792 if (draw_state < WL_LINE || n_skip <= 0)
5793 {
5794 /*
5795 * Store the character.
5796 */
5797#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5798 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5799 {
5800 /* A double-wide character is: put first halve in left cell. */
5801 --off;
5802 --col;
5803 }
5804#endif
5805 ScreenLines[off] = c;
5806#ifdef FEAT_MBYTE
5807 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005808 {
5809 if ((mb_c & 0xff00) == 0x8e00)
5810 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 else if (enc_utf8)
5814 {
5815 if (mb_utf8)
5816 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005817 int i;
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005820 if ((c & 0xff) == 0)
5821 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005822 for (i = 0; i < Screen_mco; ++i)
5823 {
5824 ScreenLinesC[i][off] = u8cc[i];
5825 if (u8cc[i] == 0)
5826 break;
5827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 }
5829 else
5830 ScreenLinesUC[off] = 0;
5831 }
5832 if (multi_attr)
5833 {
5834 ScreenAttrs[off] = multi_attr;
5835 multi_attr = 0;
5836 }
5837 else
5838#endif
5839 ScreenAttrs[off] = char_attr;
5840
5841#ifdef FEAT_MBYTE
5842 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5843 {
5844 /* Need to fill two screen columns. */
5845 ++off;
5846 ++col;
5847 if (enc_utf8)
5848 /* UTF-8: Put a 0 in the second screen char. */
5849 ScreenLines[off] = 0;
5850 else
5851 /* DBCS: Put second byte in the second screen char. */
5852 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005853 if (draw_state > WL_NR
5854#ifdef FEAT_DIFF
5855 && filler_todo <= 0
5856#endif
5857 )
5858 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 /* When "tocol" is halfway a character, set it to the end of
5860 * the character, otherwise highlighting won't stop. */
5861 if (tocol == vcol)
5862 ++tocol;
5863#ifdef FEAT_RIGHTLEFT
5864 if (wp->w_p_rl)
5865 {
5866 /* now it's time to backup one cell */
5867 --off;
5868 --col;
5869 }
5870#endif
5871 }
5872#endif
5873#ifdef FEAT_RIGHTLEFT
5874 if (wp->w_p_rl)
5875 {
5876 --off;
5877 --col;
5878 }
5879 else
5880#endif
5881 {
5882 ++off;
5883 ++col;
5884 }
5885 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005886#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005887 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005888 {
5889 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005890 ++vcol_off;
5891 if (n_extra > 0)
5892 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005893 if (wp->w_p_wrap)
5894 {
5895 /*
5896 * Special voodoo required if 'wrap' is on.
5897 *
5898 * Advance the column indicator to force the line
5899 * drawing to wrap early. This will make the line
5900 * take up the same screen space when parts are concealed,
5901 * so that cursor line computations aren't messed up.
5902 *
5903 * To avoid the fictitious advance of 'col' causing
5904 * trailing junk to be written out of the screen line
5905 * we are building, 'boguscols' keeps track of the number
5906 * of bad columns we have advanced.
5907 */
5908 if (n_extra > 0)
5909 {
5910 vcol += n_extra;
5911# ifdef FEAT_RIGHTLEFT
5912 if (wp->w_p_rl)
5913 {
5914 col -= n_extra;
5915 boguscols -= n_extra;
5916 }
5917 else
5918# endif
5919 {
5920 col += n_extra;
5921 boguscols += n_extra;
5922 }
5923 n_extra = 0;
5924 n_attr = 0;
5925 }
5926
5927
5928# ifdef FEAT_MBYTE
5929 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5930 {
5931 /* Need to fill two screen columns. */
5932# ifdef FEAT_RIGHTLEFT
5933 if (wp->w_p_rl)
5934 {
5935 --boguscols;
5936 --col;
5937 }
5938 else
5939# endif
5940 {
5941 ++boguscols;
5942 ++col;
5943 }
5944 }
5945# endif
5946
5947# ifdef FEAT_RIGHTLEFT
5948 if (wp->w_p_rl)
5949 {
5950 --boguscols;
5951 --col;
5952 }
5953 else
5954# endif
5955 {
5956 ++boguscols;
5957 ++col;
5958 }
5959 }
5960 else
5961 {
5962 if (n_extra > 0)
5963 {
5964 vcol += n_extra;
5965 n_extra = 0;
5966 n_attr = 0;
5967 }
5968 }
5969
5970 }
5971#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 else
5973 --n_skip;
5974
Bram Moolenaar64486672010-05-16 15:46:46 +02005975 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5976 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005977 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978#ifdef FEAT_DIFF
5979 && filler_todo <= 0
5980#endif
5981 )
5982 ++vcol;
5983
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005984#ifdef FEAT_SYN_HL
5985 if (vcol_save_attr >= 0)
5986 char_attr = vcol_save_attr;
5987#endif
5988
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 /* restore attributes after "predeces" in 'listchars' */
5990 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5991 char_attr = saved_attr3;
5992
5993 /* restore attributes after last 'listchars' or 'number' char */
5994 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5995 char_attr = saved_attr2;
5996
5997 /*
5998 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005999 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 */
6001 if ((
6002#ifdef FEAT_RIGHTLEFT
6003 wp->w_p_rl ? (col < 0) :
6004#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006005 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 && (*ptr != NUL
6007#ifdef FEAT_DIFF
6008 || filler_todo > 0
6009#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006010 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6012 )
6013 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006014#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006015 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02006016 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006017 boguscols = 0;
6018#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006019 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02006020 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 ++row;
6023 ++screen_row;
6024
6025 /* When not wrapping and finished diff lines, or when displayed
6026 * '$' and highlighting until last column, break here. */
6027 if ((!wp->w_p_wrap
6028#ifdef FEAT_DIFF
6029 && filler_todo <= 0
6030#endif
6031 ) || lcs_eol_one == -1)
6032 break;
6033
6034 /* When the window is too narrow draw all "@" lines. */
6035 if (draw_state != WL_LINE
6036#ifdef FEAT_DIFF
6037 && filler_todo <= 0
6038#endif
6039 )
6040 {
6041 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 row = endrow;
6044 }
6045
6046 /* When line got too long for screen break here. */
6047 if (row == endrow)
6048 {
6049 ++row;
6050 break;
6051 }
6052
6053 if (screen_cur_row == screen_row - 1
6054#ifdef FEAT_DIFF
6055 && filler_todo <= 0
6056#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006057 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 {
6059 /* Remember that the line wraps, used for modeless copy. */
6060 LineWraps[screen_row - 1] = TRUE;
6061
6062 /*
6063 * Special trick to make copy/paste of wrapped lines work with
6064 * xterm/screen: write an extra character beyond the end of
6065 * the line. This will work with all terminal types
6066 * (regardless of the xn,am settings).
6067 * Only do this on a fast tty.
6068 * Only do this if the cursor is on the current line
6069 * (something has been written in it).
6070 * Don't do this for the GUI.
6071 * Don't do this for double-width characters.
6072 * Don't do this for a window not at the right screen border.
6073 */
6074 if (p_tf
6075#ifdef FEAT_GUI
6076 && !gui.in_use
6077#endif
6078#ifdef FEAT_MBYTE
6079 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006080 && ((*mb_off2cells)(LineOffset[screen_row],
6081 LineOffset[screen_row] + screen_Columns)
6082 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006084 + (int)Columns - 2,
6085 LineOffset[screen_row] + screen_Columns)
6086 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087#endif
6088 )
6089 {
6090 /* First make sure we are at the end of the screen line,
6091 * then output the same character again to let the
6092 * terminal know about the wrap. If the terminal doesn't
6093 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006094 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 screen_char(LineOffset[screen_row - 1]
6096 + (unsigned)Columns - 1,
6097 screen_row - 1, (int)(Columns - 1));
6098
6099#ifdef FEAT_MBYTE
6100 /* When there is a multi-byte character, just output a
6101 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006102 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6103 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 out_char(' ');
6105 else
6106#endif
6107 out_char(ScreenLines[LineOffset[screen_row - 1]
6108 + (Columns - 1)]);
6109 /* force a redraw of the first char on the next line */
6110 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6111 screen_start(); /* don't know where cursor is now */
6112 }
6113 }
6114
6115 col = 0;
6116 off = (unsigned)(current_ScreenLine - ScreenLines);
6117#ifdef FEAT_RIGHTLEFT
6118 if (wp->w_p_rl)
6119 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006120 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 off += col;
6122 }
6123#endif
6124
6125 /* reset the drawing state for the start of a wrapped line */
6126 draw_state = WL_START;
6127 saved_n_extra = n_extra;
6128 saved_p_extra = p_extra;
6129 saved_c_extra = c_extra;
6130 saved_char_attr = char_attr;
6131 n_extra = 0;
6132 lcs_prec_todo = lcs_prec;
6133#ifdef FEAT_LINEBREAK
6134# ifdef FEAT_DIFF
6135 if (filler_todo <= 0)
6136# endif
6137 need_showbreak = TRUE;
6138#endif
6139#ifdef FEAT_DIFF
6140 --filler_todo;
6141 /* When the filler lines are actually below the last line of the
6142 * file, don't draw the line itself, break here. */
6143 if (filler_todo == 0 && wp->w_botfill)
6144 break;
6145#endif
6146 }
6147
6148 } /* for every character in the line */
6149
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006150#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006151 /* After an empty line check first word for capital. */
6152 if (*skipwhite(line) == NUL)
6153 {
6154 capcol_lnum = lnum + 1;
6155 cap_col = 0;
6156 }
6157#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006158#ifdef FEAT_TEXT_PROP
6159 vim_free(text_props);
6160 vim_free(text_prop_idxs);
6161#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006162
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006163 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 return row;
6165}
6166
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006167#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006168/*
6169 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006170 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006171 */
6172 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006173comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006174{
6175 int i;
6176
6177 for (i = 0; i < Screen_mco; ++i)
6178 {
6179 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6180 return TRUE;
6181 if (ScreenLinesC[i][off_from] == 0)
6182 break;
6183 }
6184 return FALSE;
6185}
6186#endif
6187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188/*
6189 * Check whether the given character needs redrawing:
6190 * - the (first byte of the) character is different
6191 * - the attributes are different
6192 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006193 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 */
6195 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006196char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197{
6198 if (cols > 0
6199 && ((ScreenLines[off_from] != ScreenLines[off_to]
6200 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6201
6202#ifdef FEAT_MBYTE
6203 || (enc_dbcs != 0
6204 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6205 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6206 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6207 : (cols > 1 && ScreenLines[off_from + 1]
6208 != ScreenLines[off_to + 1])))
6209 || (enc_utf8
6210 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6211 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006212 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006213 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6214 && ScreenLines[off_from + 1]
6215 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216#endif
6217 ))
6218 return TRUE;
6219 return FALSE;
6220}
6221
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006222#if defined(FEAT_TERMINAL) || defined(PROTO)
6223/*
6224 * Return the index in ScreenLines[] for the current screen line.
6225 */
6226 int
6227screen_get_current_line_off()
6228{
6229 return (int)(current_ScreenLine - ScreenLines);
6230}
6231#endif
6232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233/*
6234 * Move one "cooked" screen line to the screen, but only the characters that
6235 * have actually changed. Handle insert/delete character.
6236 * "coloff" gives the first column on the screen for this line.
6237 * "endcol" gives the columns where valid characters are.
6238 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6239 * needs to be cleared, negative otherwise.
6240 * "rlflag" is TRUE in a rightleft window:
6241 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6242 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6243 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006244 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006245screen_line(
6246 int row,
6247 int coloff,
6248 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006249 int clear_width,
6250 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 unsigned off_from;
6253 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006254#ifdef FEAT_MBYTE
6255 unsigned max_off_from;
6256 unsigned max_off_to;
6257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 int force = FALSE; /* force update rest of the line */
6261 int redraw_this /* bool: does character need redraw? */
6262#ifdef FEAT_GUI
6263 = TRUE /* For GUI when while-loop empty */
6264#endif
6265 ;
6266 int redraw_next; /* redraw_this for next character */
6267#ifdef FEAT_MBYTE
6268 int clear_next = FALSE;
6269 int char_cells; /* 1: normal char */
6270 /* 2: occupies two display cells */
6271# define CHAR_CELLS char_cells
6272#else
6273# define CHAR_CELLS 1
6274#endif
6275
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006276 /* Check for illegal row and col, just in case. */
6277 if (row >= Rows)
6278 row = Rows - 1;
6279 if (endcol > Columns)
6280 endcol = Columns;
6281
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282# ifdef FEAT_CLIPBOARD
6283 clip_may_clear_selection(row, row);
6284# endif
6285
6286 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6287 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006288#ifdef FEAT_MBYTE
6289 max_off_from = off_from + screen_Columns;
6290 max_off_to = LineOffset[row] + screen_Columns;
6291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292
6293#ifdef FEAT_RIGHTLEFT
6294 if (rlflag)
6295 {
6296 /* Clear rest first, because it's left of the text. */
6297 if (clear_width > 0)
6298 {
6299 while (col <= endcol && ScreenLines[off_to] == ' '
6300 && ScreenAttrs[off_to] == 0
6301# ifdef FEAT_MBYTE
6302 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6303# endif
6304 )
6305 {
6306 ++off_to;
6307 ++col;
6308 }
6309 if (col <= endcol)
6310 screen_fill(row, row + 1, col + coloff,
6311 endcol + coloff + 1, ' ', ' ', 0);
6312 }
6313 col = endcol + 1;
6314 off_to = LineOffset[row] + col + coloff;
6315 off_from += col;
6316 endcol = (clear_width > 0 ? clear_width : -clear_width);
6317 }
6318#endif /* FEAT_RIGHTLEFT */
6319
6320 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6321
6322 while (col < endcol)
6323 {
6324#ifdef FEAT_MBYTE
6325 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006326 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 else
6328 char_cells = 1;
6329#endif
6330
6331 redraw_this = redraw_next;
6332 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6333 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6334
6335#ifdef FEAT_GUI
6336 /* If the next character was bold, then redraw the current character to
6337 * remove any pixels that might have spilt over into us. This only
6338 * happens in the GUI.
6339 */
6340 if (redraw_next && gui.in_use)
6341 {
6342 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006343 if (hl > HL_ALL)
6344 hl = syn_attr2attr(hl);
6345 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 redraw_this = TRUE;
6347 }
6348#endif
6349
6350 if (redraw_this)
6351 {
6352 /*
6353 * Special handling when 'xs' termcap flag set (hpterm):
6354 * Attributes for characters are stored at the position where the
6355 * cursor is when writing the highlighting code. The
6356 * start-highlighting code must be written with the cursor on the
6357 * first highlighted character. The stop-highlighting code must
6358 * be written with the cursor just after the last highlighted
6359 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006360 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 * to clear the rest of the line, and force redrawing it
6362 * completely.
6363 */
6364 if ( p_wiv
6365 && !force
6366#ifdef FEAT_GUI
6367 && !gui.in_use
6368#endif
6369 && ScreenAttrs[off_to] != 0
6370 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6371 {
6372 /*
6373 * Need to remove highlighting attributes here.
6374 */
6375 windgoto(row, col + coloff);
6376 out_str(T_CE); /* clear rest of this screen line */
6377 screen_start(); /* don't know where cursor is now */
6378 force = TRUE; /* force redraw of rest of the line */
6379 redraw_next = TRUE; /* or else next char would miss out */
6380
6381 /*
6382 * If the previous character was highlighted, need to stop
6383 * highlighting at this character.
6384 */
6385 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6386 {
6387 screen_attr = ScreenAttrs[off_to - 1];
6388 term_windgoto(row, col + coloff);
6389 screen_stop_highlight();
6390 }
6391 else
6392 screen_attr = 0; /* highlighting has stopped */
6393 }
6394#ifdef FEAT_MBYTE
6395 if (enc_dbcs != 0)
6396 {
6397 /* Check if overwriting a double-byte with a single-byte or
6398 * the other way around requires another character to be
6399 * redrawn. For UTF-8 this isn't needed, because comparing
6400 * ScreenLinesUC[] is sufficient. */
6401 if (char_cells == 1
6402 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006403 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 {
6405 /* Writing a single-cell character over a double-cell
6406 * character: need to redraw the next cell. */
6407 ScreenLines[off_to + 1] = 0;
6408 redraw_next = TRUE;
6409 }
6410 else if (char_cells == 2
6411 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006412 && (*mb_off2cells)(off_to, max_off_to) == 1
6413 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 {
6415 /* Writing the second half of a double-cell character over
6416 * a double-cell character: need to redraw the second
6417 * cell. */
6418 ScreenLines[off_to + 2] = 0;
6419 redraw_next = TRUE;
6420 }
6421
6422 if (enc_dbcs == DBCS_JPNU)
6423 ScreenLines2[off_to] = ScreenLines2[off_from];
6424 }
6425 /* When writing a single-width character over a double-width
6426 * character and at the end of the redrawn text, need to clear out
6427 * the right halve of the old character.
6428 * Also required when writing the right halve of a double-width
6429 * char over the left halve of an existing one. */
6430 if (has_mbyte && col + char_cells == endcol
6431 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006432 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006434 && (*mb_off2cells)(off_to, max_off_to) == 1
6435 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 clear_next = TRUE;
6437#endif
6438
6439 ScreenLines[off_to] = ScreenLines[off_from];
6440#ifdef FEAT_MBYTE
6441 if (enc_utf8)
6442 {
6443 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6444 if (ScreenLinesUC[off_from] != 0)
6445 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006446 int i;
6447
6448 for (i = 0; i < Screen_mco; ++i)
6449 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 }
6451 }
6452 if (char_cells == 2)
6453 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6454#endif
6455
6456#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006457 /* The bold trick makes a single column of pixels appear in the
6458 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 * character should be redrawn too. This happens for our own GUI
6460 * and for some xterms. */
6461 if (
6462# ifdef FEAT_GUI
6463 gui.in_use
6464# endif
6465# if defined(FEAT_GUI) && defined(UNIX)
6466 ||
6467# endif
6468# ifdef UNIX
6469 term_is_xterm
6470# endif
6471 )
6472 {
6473 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006474 if (hl > HL_ALL)
6475 hl = syn_attr2attr(hl);
6476 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 redraw_next = TRUE;
6478 }
6479#endif
6480 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6481#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006482 /* For simplicity set the attributes of second half of a
6483 * double-wide character equal to the first half. */
6484 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006486
6487 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 else
6490#endif
6491 screen_char(off_to, row, col + coloff);
6492 }
6493 else if ( p_wiv
6494#ifdef FEAT_GUI
6495 && !gui.in_use
6496#endif
6497 && col + coloff > 0)
6498 {
6499 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6500 {
6501 /*
6502 * Don't output stop-highlight when moving the cursor, it will
6503 * stop the highlighting when it should continue.
6504 */
6505 screen_attr = 0;
6506 }
6507 else if (screen_attr != 0)
6508 screen_stop_highlight();
6509 }
6510
6511 off_to += CHAR_CELLS;
6512 off_from += CHAR_CELLS;
6513 col += CHAR_CELLS;
6514 }
6515
6516#ifdef FEAT_MBYTE
6517 if (clear_next)
6518 {
6519 /* Clear the second half of a double-wide character of which the left
6520 * half was overwritten with a single-wide character. */
6521 ScreenLines[off_to] = ' ';
6522 if (enc_utf8)
6523 ScreenLinesUC[off_to] = 0;
6524 screen_char(off_to, row, col + coloff);
6525 }
6526#endif
6527
6528 if (clear_width > 0
6529#ifdef FEAT_RIGHTLEFT
6530 && !rlflag
6531#endif
6532 )
6533 {
6534#ifdef FEAT_GUI
6535 int startCol = col;
6536#endif
6537
6538 /* blank out the rest of the line */
6539 while (col < clear_width && ScreenLines[off_to] == ' '
6540 && ScreenAttrs[off_to] == 0
6541#ifdef FEAT_MBYTE
6542 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6543#endif
6544 )
6545 {
6546 ++off_to;
6547 ++col;
6548 }
6549 if (col < clear_width)
6550 {
6551#ifdef FEAT_GUI
6552 /*
6553 * In the GUI, clearing the rest of the line may leave pixels
6554 * behind if the first character cleared was bold. Some bold
6555 * fonts spill over the left. In this case we redraw the previous
6556 * character too. If we didn't skip any blanks above, then we
6557 * only redraw if the character wasn't already redrawn anyway.
6558 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006559 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
6561 hl = ScreenAttrs[off_to];
6562 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006563 {
6564 int prev_cells = 1;
6565# ifdef FEAT_MBYTE
6566 if (enc_utf8)
6567 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6568 * that its width is 2. */
6569 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6570 else if (enc_dbcs != 0)
6571 {
6572 /* find previous character by counting from first
6573 * column and get its width. */
6574 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006575 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006576
6577 while (off < off_to)
6578 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006579 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006580 off += prev_cells;
6581 }
6582 }
6583
6584 if (enc_dbcs != 0 && prev_cells > 1)
6585 screen_char_2(off_to - prev_cells, row,
6586 col + coloff - prev_cells);
6587 else
6588# endif
6589 screen_char(off_to - prev_cells, row,
6590 col + coloff - prev_cells);
6591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 }
6593#endif
6594 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6595 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 off_to += clear_width - col;
6597 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
6599 }
6600
6601 if (clear_width > 0)
6602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 /* For a window that's left of another, draw the separator char. */
6604 if (col + coloff < Columns)
6605 {
6606 int c;
6607
6608 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006609 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006610#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006611 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6612 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 || ScreenAttrs[off_to] != hl)
6615 {
6616 ScreenLines[off_to] = c;
6617 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006618#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (enc_utf8)
6620 {
6621 if (c >= 0x80)
6622 {
6623 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006624 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 }
6626 else
6627 ScreenLinesUC[off_to] = 0;
6628 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 screen_char(off_to, row, col + coloff);
6631 }
6632 }
6633 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 LineWraps[row] = FALSE;
6635 }
6636}
6637
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006638#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006640 * Mirror text "str" for right-left displaying.
6641 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006643 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006644rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646 char_u *p1, *p2;
6647 int t;
6648
6649 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6650 {
6651 t = *p1;
6652 *p1 = *p2;
6653 *p2 = t;
6654 }
6655}
6656#endif
6657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658/*
6659 * mark all status lines for redraw; used after first :cd
6660 */
6661 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006662status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
6664 win_T *wp;
6665
Bram Moolenaar29323592016-07-24 22:04:11 +02006666 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 if (wp->w_status_height)
6668 {
6669 wp->w_redr_status = TRUE;
6670 redraw_later(VALID);
6671 }
6672}
6673
6674/*
6675 * mark all status lines of the current buffer for redraw
6676 */
6677 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006678status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 win_T *wp;
6681
Bram Moolenaar29323592016-07-24 22:04:11 +02006682 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6684 {
6685 wp->w_redr_status = TRUE;
6686 redraw_later(VALID);
6687 }
6688}
6689
6690/*
6691 * Redraw all status lines that need to be redrawn.
6692 */
6693 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006694redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
6696 win_T *wp;
6697
Bram Moolenaar29323592016-07-24 22:04:11 +02006698 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006700 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006701 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006702 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
Bram Moolenaar4033c552017-09-16 20:54:51 +02006705#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706/*
6707 * Redraw all status lines at the bottom of frame "frp".
6708 */
6709 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006710win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
6712 if (frp->fr_layout == FR_LEAF)
6713 frp->fr_win->w_redr_status = TRUE;
6714 else if (frp->fr_layout == FR_ROW)
6715 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006716 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 win_redraw_last_status(frp);
6718 }
6719 else /* frp->fr_layout == FR_COL */
6720 {
6721 frp = frp->fr_child;
6722 while (frp->fr_next != NULL)
6723 frp = frp->fr_next;
6724 win_redraw_last_status(frp);
6725 }
6726}
6727#endif
6728
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729/*
6730 * Draw the verticap separator right of window "wp" starting with line "row".
6731 */
6732 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006733draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 int hl;
6736 int c;
6737
6738 if (wp->w_vsep_width)
6739 {
6740 /* draw the vertical separator right of this window */
6741 c = fillchar_vsep(&hl);
6742 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6743 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6744 c, ' ', hl);
6745 }
6746}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
6748#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006749static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
6751/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006752 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 */
6754 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006755status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 int len = 0;
6758
6759#ifdef FEAT_MENU
6760 int emenu = (xp->xp_context == EXPAND_MENUS
6761 || xp->xp_context == EXPAND_MENUNAMES);
6762
6763 /* Check for menu separators - replace with '|'. */
6764 if (emenu && menu_is_separator(s))
6765 return 1;
6766#endif
6767
6768 while (*s != NUL)
6769 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006770 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006771 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006772 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 }
6774
6775 return len;
6776}
6777
6778/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006779 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006780 * These are backslashes used for escaping. Do show backslashes in help tags.
6781 */
6782 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006783skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006784{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006785 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006786#ifdef FEAT_MENU
6787 || ((xp->xp_context == EXPAND_MENUS
6788 || xp->xp_context == EXPAND_MENUNAMES)
6789 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6790#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006791 )
6792 {
6793#ifndef BACKSLASH_IN_FILENAME
6794 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6795 return 2;
6796#endif
6797 return 1;
6798 }
6799 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006800}
6801
6802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 * Show wildchar matches in the status line.
6804 * Show at least the "match" item.
6805 * We start at item 'first_match' in the list and show all matches that fit.
6806 *
6807 * If inversion is possible we use it. Else '=' characters are used.
6808 */
6809 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006810win_redr_status_matches(
6811 expand_T *xp,
6812 int num_matches,
6813 char_u **matches, /* list of matches */
6814 int match,
6815 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
6817#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6818 int row;
6819 char_u *buf;
6820 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006821 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 int fillchar;
6823 int attr;
6824 int i;
6825 int highlight = TRUE;
6826 char_u *selstart = NULL;
6827 int selstart_col = 0;
6828 char_u *selend = NULL;
6829 static int first_match = 0;
6830 int add_left = FALSE;
6831 char_u *s;
6832#ifdef FEAT_MENU
6833 int emenu;
6834#endif
6835#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6836 int l;
6837#endif
6838
6839 if (matches == NULL) /* interrupted completion? */
6840 return;
6841
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006842#ifdef FEAT_MBYTE
6843 if (has_mbyte)
6844 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6845 else
6846#endif
6847 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 if (buf == NULL)
6849 return;
6850
6851 if (match == -1) /* don't show match but original text */
6852 {
6853 match = 0;
6854 highlight = FALSE;
6855 }
6856 /* count 1 for the ending ">" */
6857 clen = status_match_len(xp, L_MATCH(match)) + 3;
6858 if (match == 0)
6859 first_match = 0;
6860 else if (match < first_match)
6861 {
6862 /* jumping left, as far as we can go */
6863 first_match = match;
6864 add_left = TRUE;
6865 }
6866 else
6867 {
6868 /* check if match fits on the screen */
6869 for (i = first_match; i < match; ++i)
6870 clen += status_match_len(xp, L_MATCH(i)) + 2;
6871 if (first_match > 0)
6872 clen += 2;
6873 /* jumping right, put match at the left */
6874 if ((long)clen > Columns)
6875 {
6876 first_match = match;
6877 /* if showing the last match, we can add some on the left */
6878 clen = 2;
6879 for (i = match; i < num_matches; ++i)
6880 {
6881 clen += status_match_len(xp, L_MATCH(i)) + 2;
6882 if ((long)clen >= Columns)
6883 break;
6884 }
6885 if (i == num_matches)
6886 add_left = TRUE;
6887 }
6888 }
6889 if (add_left)
6890 while (first_match > 0)
6891 {
6892 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6893 if ((long)clen >= Columns)
6894 break;
6895 --first_match;
6896 }
6897
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006898 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899
6900 if (first_match == 0)
6901 {
6902 *buf = NUL;
6903 len = 0;
6904 }
6905 else
6906 {
6907 STRCPY(buf, "< ");
6908 len = 2;
6909 }
6910 clen = len;
6911
6912 i = first_match;
6913 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6914 {
6915 if (i == match)
6916 {
6917 selstart = buf + len;
6918 selstart_col = clen;
6919 }
6920
6921 s = L_MATCH(i);
6922 /* Check for menu separators - replace with '|' */
6923#ifdef FEAT_MENU
6924 emenu = (xp->xp_context == EXPAND_MENUS
6925 || xp->xp_context == EXPAND_MENUNAMES);
6926 if (emenu && menu_is_separator(s))
6927 {
6928 STRCPY(buf + len, transchar('|'));
6929 l = (int)STRLEN(buf + len);
6930 len += l;
6931 clen += l;
6932 }
6933 else
6934#endif
6935 for ( ; *s != NUL; ++s)
6936 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006937 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 clen += ptr2cells(s);
6939#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006940 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
6942 STRNCPY(buf + len, s, l);
6943 s += l - 1;
6944 len += l;
6945 }
6946 else
6947#endif
6948 {
6949 STRCPY(buf + len, transchar_byte(*s));
6950 len += (int)STRLEN(buf + len);
6951 }
6952 }
6953 if (i == match)
6954 selend = buf + len;
6955
6956 *(buf + len++) = ' ';
6957 *(buf + len++) = ' ';
6958 clen += 2;
6959 if (++i == num_matches)
6960 break;
6961 }
6962
6963 if (i != num_matches)
6964 {
6965 *(buf + len++) = '>';
6966 ++clen;
6967 }
6968
6969 buf[len] = NUL;
6970
6971 row = cmdline_row - 1;
6972 if (row >= 0)
6973 {
6974 if (wild_menu_showing == 0)
6975 {
6976 if (msg_scrolled > 0)
6977 {
6978 /* Put the wildmenu just above the command line. If there is
6979 * no room, scroll the screen one line up. */
6980 if (cmdline_row == Rows - 1)
6981 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006982 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 ++msg_scrolled;
6984 }
6985 else
6986 {
6987 ++cmdline_row;
6988 ++row;
6989 }
6990 wild_menu_showing = WM_SCROLLED;
6991 }
6992 else
6993 {
6994 /* Create status line if needed by setting 'laststatus' to 2.
6995 * Set 'winminheight' to zero to avoid that the window is
6996 * resized. */
6997 if (lastwin->w_status_height == 0)
6998 {
6999 save_p_ls = p_ls;
7000 save_p_wmh = p_wmh;
7001 p_ls = 2;
7002 p_wmh = 0;
7003 last_status(FALSE);
7004 }
7005 wild_menu_showing = WM_SHOWN;
7006 }
7007 }
7008
7009 screen_puts(buf, row, 0, attr);
7010 if (selstart != NULL && highlight)
7011 {
7012 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007013 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015
7016 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7017 }
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 vim_free(buf);
7021}
7022#endif
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024/*
7025 * Redraw the status line of window wp.
7026 *
7027 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007028 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7029 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007031 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007032win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 int row;
7035 char_u *p;
7036 int len;
7037 int fillchar;
7038 int attr;
7039 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007040 static int busy = FALSE;
7041
7042 /* It's possible to get here recursively when 'statusline' (indirectly)
7043 * invokes ":redrawstatus". Simply ignore the call then. */
7044 if (busy)
7045 return;
7046 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
7048 wp->w_redr_status = FALSE;
7049 if (wp->w_status_height == 0)
7050 {
7051 /* no status line, can only be last window */
7052 redraw_cmdline = TRUE;
7053 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007054 else if (!redrawing()
7055#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007056 // don't update status line when popup menu is visible and may be
7057 // drawn over it, unless it will be redrawn later
7058 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007059#endif
7060 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {
7062 /* Don't redraw right now, do it later. */
7063 wp->w_redr_status = TRUE;
7064 }
7065#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007066 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {
7068 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007069 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 }
7071#endif
7072 else
7073 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007074 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007076 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 p = NameBuff;
7078 len = (int)STRLEN(p);
7079
Bram Moolenaard85f2712017-07-28 21:51:57 +02007080 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081#ifdef FEAT_QUICKFIX
7082 || wp->w_p_pvw
7083#endif
7084 || bufIsChanged(wp->w_buffer)
7085 || wp->w_buffer->b_p_ro)
7086 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007087 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007089 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 len += (int)STRLEN(p + len);
7091 }
7092#ifdef FEAT_QUICKFIX
7093 if (wp->w_p_pvw)
7094 {
7095 STRCPY(p + len, _("[Preview]"));
7096 len += (int)STRLEN(p + len);
7097 }
7098#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007099 if (bufIsChanged(wp->w_buffer)
7100#ifdef FEAT_TERMINAL
7101 && !bt_terminal(wp->w_buffer)
7102#endif
7103 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {
7105 STRCPY(p + len, "[+]");
7106 len += 3;
7107 }
7108 if (wp->w_buffer->b_p_ro)
7109 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007110 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007111 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 }
7113
Bram Moolenaar02631462017-09-22 15:20:32 +02007114 this_ru_col = ru_col - (Columns - wp->w_width);
7115 if (this_ru_col < (wp->w_width + 1) / 2)
7116 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 if (this_ru_col <= 1)
7118 {
7119 p = (char_u *)"<"; /* No room for file name! */
7120 len = 1;
7121 }
7122 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123#ifdef FEAT_MBYTE
7124 if (has_mbyte)
7125 {
7126 int clen = 0, i;
7127
7128 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007129 clen = mb_string2cells(p, -1);
7130
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 /* Find first character that will fit.
7132 * Going from start to end is much faster for DBCS. */
7133 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007134 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 clen -= (*mb_ptr2cells)(p + i);
7136 len = clen;
7137 if (i > 0)
7138 {
7139 p = p + i - 1;
7140 *p = '<';
7141 ++len;
7142 }
7143
7144 }
7145 else
7146#endif
7147 if (len > this_ru_col - 1)
7148 {
7149 p += len - (this_ru_col - 1);
7150 *p = '<';
7151 len = this_ru_col - 1;
7152 }
7153
7154 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007155 screen_puts(p, row, wp->w_wincol, attr);
7156 screen_fill(row, row + 1, len + wp->w_wincol,
7157 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007159 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7161 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007162 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
7164#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007165 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166#endif
7167 }
7168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 /*
7170 * May need to draw the character below the vertical separator.
7171 */
7172 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7173 {
7174 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007175 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 else
7177 fillchar = fillchar_vsep(&attr);
7178 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7179 attr);
7180 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007181 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182}
7183
Bram Moolenaar238a5642006-02-21 22:12:05 +00007184#ifdef FEAT_STL_OPT
7185/*
7186 * Redraw the status line according to 'statusline' and take care of any
7187 * errors encountered.
7188 */
7189 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007190redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007191{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007192 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007193 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007194
7195 /* When called recursively return. This can happen when the statusline
7196 * contains an expression that triggers a redraw. */
7197 if (entered)
7198 return;
7199 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007200
Bram Moolenaara742e082016-04-05 21:10:38 +02007201 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007202 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007203 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007204 {
7205 /* When there is an error disable the statusline, otherwise the
7206 * display is messed up with errors and a redraw triggers the problem
7207 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007208 set_string_option_direct((char_u *)"statusline", -1,
7209 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007210 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007211 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007212 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007213 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007214}
7215#endif
7216
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217/*
7218 * Return TRUE if the status line of window "wp" is connected to the status
7219 * line of the window right of it. If not, then it's a vertical separator.
7220 * Only call if (wp->w_vsep_width != 0).
7221 */
7222 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007223stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224{
7225 frame_T *fr;
7226
7227 fr = wp->w_frame;
7228 while (fr->fr_parent != NULL)
7229 {
7230 if (fr->fr_parent->fr_layout == FR_COL)
7231 {
7232 if (fr->fr_next != NULL)
7233 break;
7234 }
7235 else
7236 {
7237 if (fr->fr_next != NULL)
7238 return TRUE;
7239 }
7240 fr = fr->fr_parent;
7241 }
7242 return FALSE;
7243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246/*
7247 * Get the value to show for the language mappings, active 'keymap'.
7248 */
7249 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007250get_keymap_str(
7251 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007252 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007253 char_u *buf, /* buffer for the result */
7254 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
7256 char_u *p;
7257
7258 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7259 return FALSE;
7260
7261 {
7262#ifdef FEAT_EVAL
7263 buf_T *old_curbuf = curbuf;
7264 win_T *old_curwin = curwin;
7265 char_u *s;
7266
7267 curbuf = wp->w_buffer;
7268 curwin = wp;
7269 STRCPY(buf, "b:keymap_name"); /* must be writable */
7270 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007271 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 --emsg_skip;
7273 curbuf = old_curbuf;
7274 curwin = old_curwin;
7275 if (p == NULL || *p == NUL)
7276#endif
7277 {
7278#ifdef FEAT_KEYMAP
7279 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7280 p = wp->w_buffer->b_p_keymap;
7281 else
7282#endif
7283 p = (char_u *)"lang";
7284 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007285 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 buf[0] = NUL;
7287#ifdef FEAT_EVAL
7288 vim_free(s);
7289#endif
7290 }
7291 return buf[0] != NUL;
7292}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294#if defined(FEAT_STL_OPT) || defined(PROTO)
7295/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007296 * Redraw the status line or ruler of window "wp".
7297 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 */
7299 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007300win_redr_custom(
7301 win_T *wp,
7302 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007304 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 int attr;
7306 int curattr;
7307 int row;
7308 int col = 0;
7309 int maxwidth;
7310 int width;
7311 int n;
7312 int len;
7313 int fillchar;
7314 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007315 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007317 struct stl_hlrec hltab[STL_MAX_ITEM];
7318 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007319 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007320 win_T *ewp;
7321 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
Bram Moolenaar1d633412013-12-11 15:52:01 +01007323 /* There is a tiny chance that this gets called recursively: When
7324 * redrawing a status line triggers redrawing the ruler or tabline.
7325 * Avoid trouble by not allowing recursion. */
7326 if (entered)
7327 return;
7328 entered = TRUE;
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007331 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007333 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007334 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007335 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007336 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007337 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007338 maxwidth = Columns;
7339# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007340 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007343 else
7344 {
7345 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007346 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007347 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007348
7349 if (draw_ruler)
7350 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007351 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007352 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007353 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007354 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007355 if (*++stl == '-')
7356 stl++;
7357 if (atoi((char *)stl))
7358 while (VIM_ISDIGIT(*stl))
7359 stl++;
7360 if (*stl++ != '(')
7361 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007362 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007363 col = ru_col - (Columns - wp->w_width);
7364 if (col < (wp->w_width + 1) / 2)
7365 col = (wp->w_width + 1) / 2;
7366 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007367 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007368 {
7369 row = Rows - 1;
7370 --maxwidth; /* writing in last column may cause scrolling */
7371 fillchar = ' ';
7372 attr = 0;
7373 }
7374
7375# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007376 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007377# endif
7378 }
7379 else
7380 {
7381 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007382 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007383 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007384 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007385# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007386 use_sandbox = was_set_insecurely((char_u *)"statusline",
7387 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007388# endif
7389 }
7390
Bram Moolenaar53f81742017-09-22 14:35:51 +02007391 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007392 }
7393
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007395 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
Bram Moolenaar61452852011-02-01 18:01:11 +01007397 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7398 * the cursor away and back. */
7399 ewp = wp == NULL ? curwin : wp;
7400 p_crb_save = ewp->w_p_crb;
7401 ewp->w_p_crb = FALSE;
7402
Bram Moolenaar362f3562009-11-03 16:20:34 +00007403 /* Make a copy, because the statusline may include a function call that
7404 * might change the option value and free the memory. */
7405 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007406 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007407 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007408 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007409 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007410 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007412 /* Make all characters printable. */
7413 p = transstr(buf);
7414 if (p != NULL)
7415 {
7416 vim_strncpy(buf, p, sizeof(buf) - 1);
7417 vim_free(p);
7418 }
7419
7420 /* fill up with "fillchar" */
7421 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007422 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 {
7424#ifdef FEAT_MBYTE
7425 len += (*mb_char2bytes)(fillchar, buf + len);
7426#else
7427 buf[len++] = fillchar;
7428#endif
7429 ++width;
7430 }
7431 buf[len] = NUL;
7432
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007433 /*
7434 * Draw each snippet with the specified highlighting.
7435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 curattr = attr;
7437 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007438 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007440 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 screen_puts_len(p, len, row, col, curattr);
7442 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007443 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007445 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007447 else if (hltab[n].userhl < 0)
7448 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007449#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007450 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7451 && wp->w_status_height != 0)
7452 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007453 else if (wp != NULL && bt_terminal(wp->w_buffer)
7454 && wp->w_status_height != 0)
7455 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007456#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007457 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007458 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007460 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 }
7462 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007463
7464 if (wp == NULL)
7465 {
7466 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7467 col = 0;
7468 len = 0;
7469 p = buf;
7470 fillchar = 0;
7471 for (n = 0; tabtab[n].start != NULL; n++)
7472 {
7473 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7474 while (col < len)
7475 TabPageIdxs[col++] = fillchar;
7476 p = tabtab[n].start;
7477 fillchar = tabtab[n].userhl;
7478 }
7479 while (col < Columns)
7480 TabPageIdxs[col++] = fillchar;
7481 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007482
7483theend:
7484 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485}
7486
7487#endif /* FEAT_STL_OPT */
7488
7489/*
7490 * Output a single character directly to the screen and update ScreenLines.
7491 */
7492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007493screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 char_u buf[MB_MAXBYTES + 1];
7496
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007497#ifdef FEAT_MBYTE
7498 if (has_mbyte)
7499 buf[(*mb_char2bytes)(c, buf)] = NUL;
7500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007502 {
7503 buf[0] = c;
7504 buf[1] = NUL;
7505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 screen_puts(buf, row, col, attr);
7507}
7508
7509/*
7510 * Get a single character directly from ScreenLines into "bytes[]".
7511 * Also return its attribute in *attrp;
7512 */
7513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007514screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515{
7516 unsigned off;
7517
7518 /* safety check */
7519 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7520 {
7521 off = LineOffset[row] + col;
7522 *attrp = ScreenAttrs[off];
7523 bytes[0] = ScreenLines[off];
7524 bytes[1] = NUL;
7525
7526#ifdef FEAT_MBYTE
7527 if (enc_utf8 && ScreenLinesUC[off] != 0)
7528 bytes[utfc_char2bytes(off, bytes)] = NUL;
7529 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7530 {
7531 bytes[0] = ScreenLines[off];
7532 bytes[1] = ScreenLines2[off];
7533 bytes[2] = NUL;
7534 }
7535 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7536 {
7537 bytes[1] = ScreenLines[off + 1];
7538 bytes[2] = NUL;
7539 }
7540#endif
7541 }
7542}
7543
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007544#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007545/*
7546 * Return TRUE if composing characters for screen posn "off" differs from
7547 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007548 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007549 */
7550 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007551screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007552{
7553 int i;
7554
7555 for (i = 0; i < Screen_mco; ++i)
7556 {
7557 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7558 return TRUE;
7559 if (u8cc[i] == 0)
7560 break;
7561 }
7562 return FALSE;
7563}
7564#endif
7565
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566/*
7567 * Put string '*text' on the screen at position 'row' and 'col', with
7568 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7569 * Note: only outputs within one row, message is truncated at screen boundary!
7570 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7571 */
7572 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007573screen_puts(
7574 char_u *text,
7575 int row,
7576 int col,
7577 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
7579 screen_puts_len(text, -1, row, col, attr);
7580}
7581
7582/*
7583 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7584 * a NUL.
7585 */
7586 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007587screen_puts_len(
7588 char_u *text,
7589 int textlen,
7590 int row,
7591 int col,
7592 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
7594 unsigned off;
7595 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007596 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 int c;
7598#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007599 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 int mbyte_blen = 1;
7601 int mbyte_cells = 1;
7602 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007603 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 int clear_next_cell = FALSE;
7605# ifdef FEAT_ARABIC
7606 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007607 int pc, nc, nc1;
7608 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609# endif
7610#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007611#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7612 int force_redraw_this;
7613 int force_redraw_next = FALSE;
7614#endif
7615 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616
7617 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7618 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007619 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
Bram Moolenaarc236c162008-07-13 17:41:49 +00007621#ifdef FEAT_MBYTE
7622 /* When drawing over the right halve of a double-wide char clear out the
7623 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007624 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007625# ifdef FEAT_GUI
7626 && !gui.in_use
7627# endif
7628 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007629 {
7630 ScreenLines[off - 1] = ' ';
7631 ScreenAttrs[off - 1] = 0;
7632 if (enc_utf8)
7633 {
7634 ScreenLinesUC[off - 1] = 0;
7635 ScreenLinesC[0][off - 1] = 0;
7636 }
7637 /* redraw the previous cell, make it empty */
7638 screen_char(off - 1, row, col - 1);
7639 /* force the cell at "col" to be redrawn */
7640 force_redraw_next = TRUE;
7641 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007642#endif
7643
Bram Moolenaar367329b2007-08-30 11:53:22 +00007644#ifdef FEAT_MBYTE
7645 max_off = LineOffset[row] + screen_Columns;
7646#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007647 while (col < screen_Columns
7648 && (len < 0 || (int)(ptr - text) < len)
7649 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 {
7651 c = *ptr;
7652#ifdef FEAT_MBYTE
7653 /* check if this is the first byte of a multibyte */
7654 if (has_mbyte)
7655 {
7656 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007657 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007659 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7661 mbyte_cells = 1;
7662 else if (enc_dbcs != 0)
7663 mbyte_cells = mbyte_blen;
7664 else /* enc_utf8 */
7665 {
7666 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007667 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 (int)((text + len) - ptr));
7669 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007670 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007672# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 /* Non-BMP character: display as ? or fullwidth ?. */
7674 if (u8c >= 0x10000)
7675 {
7676 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7677 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007678 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681# ifdef FEAT_ARABIC
7682 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7683 {
7684 /* Do Arabic shaping. */
7685 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7686 {
7687 /* Past end of string to be displayed. */
7688 nc = NUL;
7689 nc1 = NUL;
7690 }
7691 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007692 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007693 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7694 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007695 nc1 = pcc[0];
7696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 pc = prev_c;
7698 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007699 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
7701 else
7702 prev_c = u8c;
7703# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007704 if (col + mbyte_cells > screen_Columns)
7705 {
7706 /* Only 1 cell left, but character requires 2 cells:
7707 * display a '>' in the last column to avoid wrapping. */
7708 c = '>';
7709 mbyte_cells = 1;
7710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 }
7712 }
7713#endif
7714
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007715#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7716 force_redraw_this = force_redraw_next;
7717 force_redraw_next = FALSE;
7718#endif
7719
7720 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721#ifdef FEAT_MBYTE
7722 || (mbyte_cells == 2
7723 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7724 || (enc_dbcs == DBCS_JPNU
7725 && c == 0x8e
7726 && ScreenLines2[off] != ptr[1])
7727 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007728 && (ScreenLinesUC[off] !=
7729 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7730 || (ScreenLinesUC[off] != 0
7731 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732#endif
7733 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007734 || exmode_active;
7735
7736 if (need_redraw
7737#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7738 || force_redraw_this
7739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 )
7741 {
7742#if defined(FEAT_GUI) || defined(UNIX)
7743 /* The bold trick makes a single row of pixels appear in the next
7744 * character. When a bold character is removed, the next
7745 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007746 * and for some xterms. */
7747 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748# ifdef FEAT_GUI
7749 gui.in_use
7750# endif
7751# if defined(FEAT_GUI) && defined(UNIX)
7752 ||
7753# endif
7754# ifdef UNIX
7755 term_is_xterm
7756# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007757 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007759 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007761 if (n > HL_ALL)
7762 n = syn_attr2attr(n);
7763 if (n & HL_BOLD)
7764 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
7766#endif
7767#ifdef FEAT_MBYTE
7768 /* When at the end of the text and overwriting a two-cell
7769 * character with a one-cell character, need to clear the next
7770 * cell. Also when overwriting the left halve of a two-cell char
7771 * with the right halve of a two-cell char. Do this only once
7772 * (mb_off2cells() may return 2 on the right halve). */
7773 if (clear_next_cell)
7774 clear_next_cell = FALSE;
7775 else if (has_mbyte
7776 && (len < 0 ? ptr[mbyte_blen] == NUL
7777 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007778 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007780 && (*mb_off2cells)(off, max_off) == 1
7781 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 clear_next_cell = TRUE;
7783
7784 /* Make sure we never leave a second byte of a double-byte behind,
7785 * it confuses mb_off2cells(). */
7786 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007787 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007789 && (*mb_off2cells)(off, max_off) == 1
7790 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 ScreenLines[off + mbyte_blen] = 0;
7792#endif
7793 ScreenLines[off] = c;
7794 ScreenAttrs[off] = attr;
7795#ifdef FEAT_MBYTE
7796 if (enc_utf8)
7797 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007798 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 ScreenLinesUC[off] = 0;
7800 else
7801 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007802 int i;
7803
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007805 for (i = 0; i < Screen_mco; ++i)
7806 {
7807 ScreenLinesC[i][off] = u8cc[i];
7808 if (u8cc[i] == 0)
7809 break;
7810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 }
7812 if (mbyte_cells == 2)
7813 {
7814 ScreenLines[off + 1] = 0;
7815 ScreenAttrs[off + 1] = attr;
7816 }
7817 screen_char(off, row, col);
7818 }
7819 else if (mbyte_cells == 2)
7820 {
7821 ScreenLines[off + 1] = ptr[1];
7822 ScreenAttrs[off + 1] = attr;
7823 screen_char_2(off, row, col);
7824 }
7825 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7826 {
7827 ScreenLines2[off] = ptr[1];
7828 screen_char(off, row, col);
7829 }
7830 else
7831#endif
7832 screen_char(off, row, col);
7833 }
7834#ifdef FEAT_MBYTE
7835 if (has_mbyte)
7836 {
7837 off += mbyte_cells;
7838 col += mbyte_cells;
7839 ptr += mbyte_blen;
7840 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007841 {
7842 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007844 len = -1;
7845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 }
7847 else
7848#endif
7849 {
7850 ++off;
7851 ++col;
7852 ++ptr;
7853 }
7854 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007855
7856#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7857 /* If we detected the next character needs to be redrawn, but the text
7858 * doesn't extend up to there, update the character here. */
7859 if (force_redraw_next && col < screen_Columns)
7860 {
7861# ifdef FEAT_MBYTE
7862 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7863 screen_char_2(off, row, col);
7864 else
7865# endif
7866 screen_char(off, row, col);
7867 }
7868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869}
7870
7871#ifdef FEAT_SEARCH_EXTRA
7872/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007873 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 */
7875 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007876start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
7878 if (p_hls && !no_hlsearch)
7879 {
7880 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007881 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007882# ifdef FEAT_RELTIME
7883 /* Set the time limit to 'redrawtime'. */
7884 profile_setlimit(p_rdt, &search_hl.tm);
7885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 }
7887}
7888
7889/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007890 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 */
7892 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007893end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 if (search_hl.rm.regprog != NULL)
7896 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007897 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 search_hl.rm.regprog = NULL;
7899 }
7900}
7901
7902/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007903 * Init for calling prepare_search_hl().
7904 */
7905 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007906init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007907{
7908 matchitem_T *cur;
7909
7910 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7911 * match */
7912 cur = wp->w_match_head;
7913 while (cur != NULL)
7914 {
7915 cur->hl.rm = cur->match;
7916 if (cur->hlg_id == 0)
7917 cur->hl.attr = 0;
7918 else
7919 cur->hl.attr = syn_id2attr(cur->hlg_id);
7920 cur->hl.buf = wp->w_buffer;
7921 cur->hl.lnum = 0;
7922 cur->hl.first_lnum = 0;
7923# ifdef FEAT_RELTIME
7924 /* Set the time limit to 'redrawtime'. */
7925 profile_setlimit(p_rdt, &(cur->hl.tm));
7926# endif
7927 cur = cur->next;
7928 }
7929 search_hl.buf = wp->w_buffer;
7930 search_hl.lnum = 0;
7931 search_hl.first_lnum = 0;
7932 /* time limit is set at the toplevel, for all windows */
7933}
7934
7935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 * Advance to the match in window "wp" line "lnum" or past it.
7937 */
7938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007939prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007941 matchitem_T *cur; /* points to the match list */
7942 match_T *shl; /* points to search_hl or a match */
7943 int shl_flag; /* flag to indicate whether search_hl
7944 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007945 int pos_inprogress; /* marks that position match search is
7946 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 int n;
7948
7949 /*
7950 * When using a multi-line pattern, start searching at the top
7951 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007952 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007954 cur = wp->w_match_head;
7955 shl_flag = FALSE;
7956 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007958 if (shl_flag == FALSE)
7959 {
7960 shl = &search_hl;
7961 shl_flag = TRUE;
7962 }
7963 else
7964 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (shl->rm.regprog != NULL
7966 && shl->lnum == 0
7967 && re_multiline(shl->rm.regprog))
7968 {
7969 if (shl->first_lnum == 0)
7970 {
7971# ifdef FEAT_FOLDING
7972 for (shl->first_lnum = lnum;
7973 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7974 if (hasFoldingWin(wp, shl->first_lnum - 1,
7975 NULL, NULL, TRUE, NULL))
7976 break;
7977# else
7978 shl->first_lnum = wp->w_topline;
7979# endif
7980 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981 if (cur != NULL)
7982 cur->pos.cur = 0;
7983 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7986 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007988 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7989 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 pos_inprogress = cur == NULL || cur->pos.cur == 0
7991 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 if (shl->lnum != 0)
7993 {
7994 shl->first_lnum = shl->lnum
7995 + shl->rm.endpos[0].lnum
7996 - shl->rm.startpos[0].lnum;
7997 n = shl->rm.endpos[0].col;
7998 }
7999 else
8000 {
8001 ++shl->first_lnum;
8002 n = 0;
8003 }
8004 }
8005 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008006 if (shl != &search_hl && cur != NULL)
8007 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 }
8009}
8010
8011/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008012 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 * Uses shl->buf.
8014 * Sets shl->lnum and shl->rm contents.
8015 * Note: Assumes a previous match is always before "lnum", unless
8016 * shl->lnum is zero.
8017 * Careful: Any pointers for buffer lines will become invalid.
8018 */
8019 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008020next_search_hl(
8021 win_T *win,
8022 match_T *shl, /* points to search_hl or a match */
8023 linenr_T lnum,
8024 colnr_T mincol, /* minimal column for a match */
8025 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 linenr_T l;
8028 colnr_T matchcol;
8029 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008030 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008032 // for :{range}s/pat only highlight inside the range
8033 if (lnum < search_first_line || lnum > search_last_line)
8034 {
8035 shl->lnum = 0;
8036 return;
8037 }
8038
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (shl->lnum != 0)
8040 {
8041 /* Check for three situations:
8042 * 1. If the "lnum" is below a previous match, start a new search.
8043 * 2. If the previous match includes "mincol", use it.
8044 * 3. Continue after the previous match.
8045 */
8046 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8047 if (lnum > l)
8048 shl->lnum = 0;
8049 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8050 return;
8051 }
8052
8053 /*
8054 * Repeat searching for a match until one is found that includes "mincol"
8055 * or none is found in this line.
8056 */
8057 called_emsg = FALSE;
8058 for (;;)
8059 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008060#ifdef FEAT_RELTIME
8061 /* Stop searching after passing the time limit. */
8062 if (profile_passed_limit(&(shl->tm)))
8063 {
8064 shl->lnum = 0; /* no match found in time */
8065 break;
8066 }
8067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 /* Three situations:
8069 * 1. No useful previous match: search from start of line.
8070 * 2. Not Vi compatible or empty match: continue at next character.
8071 * Break the loop if this is beyond the end of the line.
8072 * 3. Vi compatible searching: continue at end of previous match.
8073 */
8074 if (shl->lnum == 0)
8075 matchcol = 0;
8076 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8077 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008078 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008080 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008081
8082 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008083 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008084 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008086 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 shl->lnum = 0;
8088 break;
8089 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008090#ifdef FEAT_MBYTE
8091 if (has_mbyte)
8092 matchcol += mb_ptr2len(ml);
8093 else
8094#endif
8095 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 }
8097 else
8098 matchcol = shl->rm.endpos[0].col;
8099
8100 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008101 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008103 /* Remember whether shl->rm is using a copy of the regprog in
8104 * cur->match. */
8105 int regprog_is_copy = (shl != &search_hl && cur != NULL
8106 && shl == &cur->hl
8107 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008108 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008109
Bram Moolenaarb3414592014-06-17 17:48:32 +02008110 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8111 matchcol,
8112#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008113 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008114#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008115 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008116#endif
8117 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008118 /* Copy the regprog, in case it got freed and recompiled. */
8119 if (regprog_is_copy)
8120 cur->match.regprog = cur->hl.rm.regprog;
8121
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008122 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008123 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008124 /* Error while handling regexp: stop using this regexp. */
8125 if (shl == &search_hl)
8126 {
8127 /* don't free regprog in the match list, it's a copy */
8128 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008129 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008130 }
8131 shl->rm.regprog = NULL;
8132 shl->lnum = 0;
8133 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8134 message */
8135 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008136 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008137 }
8138 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008139 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008140 else
8141 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 if (nmatched == 0)
8143 {
8144 shl->lnum = 0; /* no match found */
8145 break;
8146 }
8147 if (shl->rm.startpos[0].lnum > 0
8148 || shl->rm.startpos[0].col >= mincol
8149 || nmatched > 1
8150 || shl->rm.endpos[0].col > mincol)
8151 {
8152 shl->lnum += shl->rm.startpos[0].lnum;
8153 break; /* useful match found */
8154 }
8155 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008156
8157 // Restore called_emsg for assert_fails().
8158 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160
Bram Moolenaar85077472016-10-16 14:35:48 +02008161/*
8162 * If there is a match fill "shl" and return one.
8163 * Return zero otherwise.
8164 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008165 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008166next_search_hl_pos(
8167 match_T *shl, /* points to a match */
8168 linenr_T lnum,
8169 posmatch_T *posmatch, /* match positions */
8170 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008171{
8172 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008173 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008174
Bram Moolenaarb3414592014-06-17 17:48:32 +02008175 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8176 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008177 llpos_T *pos = &posmatch->pos[i];
8178
8179 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008180 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008181 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008182 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008183 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008184 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008185 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008186 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008187 /* if this match comes before the one at "found" then swap
8188 * them */
8189 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008190 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008191 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008192
Bram Moolenaar85077472016-10-16 14:35:48 +02008193 *pos = posmatch->pos[found];
8194 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195 }
8196 }
8197 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008198 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008199 }
8200 }
8201 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008202 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008203 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008204 colnr_T start = posmatch->pos[found].col == 0
8205 ? 0 : posmatch->pos[found].col - 1;
8206 colnr_T end = posmatch->pos[found].col == 0
8207 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008208
Bram Moolenaar85077472016-10-16 14:35:48 +02008209 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008210 shl->rm.startpos[0].lnum = 0;
8211 shl->rm.startpos[0].col = start;
8212 shl->rm.endpos[0].lnum = 0;
8213 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008214 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008215 posmatch->cur = found + 1;
8216 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008217 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008218 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008219}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008220#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008221
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008223screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
8225 attrentry_T *aep = NULL;
8226
8227 screen_attr = attr;
8228 if (full_screen
8229#ifdef WIN3264
8230 && termcap_active
8231#endif
8232 )
8233 {
8234#ifdef FEAT_GUI
8235 if (gui.in_use)
8236 {
8237 char buf[20];
8238
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008239 /* The GUI handles this internally. */
8240 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 OUT_STR(buf);
8242 }
8243 else
8244#endif
8245 {
8246 if (attr > HL_ALL) /* special HL attr. */
8247 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008248 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 aep = syn_cterm_attr2entry(attr);
8250 else
8251 aep = syn_term_attr2entry(attr);
8252 if (aep == NULL) /* did ":syntax clear" */
8253 attr = 0;
8254 else
8255 attr = aep->ae_attr;
8256 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008257 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008259 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008260#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008261 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8262 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8263 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008264#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008265 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008266 /* If the Normal FG color has BOLD attribute and the new HL
8267 * has a FG color defined, clear BOLD. */
8268 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008269 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008271 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008272 out_str(T_UCS);
8273 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008274 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8275 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008277 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008279 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008281 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008282 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283
8284 /*
8285 * Output the color or start string after bold etc., in case the
8286 * bold etc. override the color setting.
8287 */
8288 if (aep != NULL)
8289 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008290#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008291 /* When 'termguicolors' is set but fg or bg is unset,
8292 * fall back to the cterm colors. This helps for SpellBad,
8293 * where the GUI uses a red undercurl. */
8294 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008296 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008297 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008298 }
8299 else
8300#endif
8301 if (t_colors > 1)
8302 {
8303 if (aep->ae_u.cterm.fg_color)
8304 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8305 }
8306#ifdef FEAT_TERMGUICOLORS
8307 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8308 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008309 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008310 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 }
8312 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008313#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008314 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008316 if (aep->ae_u.cterm.bg_color)
8317 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8318 }
8319
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008320 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008321 {
8322 if (aep->ae_u.term.start != NULL)
8323 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325 }
8326 }
8327 }
8328}
8329
8330 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008331screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
8333 int do_ME = FALSE; /* output T_ME code */
8334
8335 if (screen_attr != 0
8336#ifdef WIN3264
8337 && termcap_active
8338#endif
8339 )
8340 {
8341#ifdef FEAT_GUI
8342 if (gui.in_use)
8343 {
8344 char buf[20];
8345
8346 /* use internal GUI code */
8347 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8348 OUT_STR(buf);
8349 }
8350 else
8351#endif
8352 {
8353 if (screen_attr > HL_ALL) /* special HL attr. */
8354 {
8355 attrentry_T *aep;
8356
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008357 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {
8359 /*
8360 * Assume that t_me restores the original colors!
8361 */
8362 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008363 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008364#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008365 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8366 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8367 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008368#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008369 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008370#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008371 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8372 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8373 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008374#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008375 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 do_ME = TRUE;
8377 }
8378 else
8379 {
8380 aep = syn_term_attr2entry(screen_attr);
8381 if (aep != NULL && aep->ae_u.term.stop != NULL)
8382 {
8383 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8384 do_ME = TRUE;
8385 else
8386 out_str(aep->ae_u.term.stop);
8387 }
8388 }
8389 if (aep == NULL) /* did ":syntax clear" */
8390 screen_attr = 0;
8391 else
8392 screen_attr = aep->ae_attr;
8393 }
8394
8395 /*
8396 * Often all ending-codes are equal to T_ME. Avoid outputting the
8397 * same sequence several times.
8398 */
8399 if (screen_attr & HL_STANDOUT)
8400 {
8401 if (STRCMP(T_SE, T_ME) == 0)
8402 do_ME = TRUE;
8403 else
8404 out_str(T_SE);
8405 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008406 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008407 {
8408 if (STRCMP(T_UCE, T_ME) == 0)
8409 do_ME = TRUE;
8410 else
8411 out_str(T_UCE);
8412 }
8413 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008414 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {
8416 if (STRCMP(T_UE, T_ME) == 0)
8417 do_ME = TRUE;
8418 else
8419 out_str(T_UE);
8420 }
8421 if (screen_attr & HL_ITALIC)
8422 {
8423 if (STRCMP(T_CZR, T_ME) == 0)
8424 do_ME = TRUE;
8425 else
8426 out_str(T_CZR);
8427 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008428 if (screen_attr & HL_STRIKETHROUGH)
8429 {
8430 if (STRCMP(T_STE, T_ME) == 0)
8431 do_ME = TRUE;
8432 else
8433 out_str(T_STE);
8434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8436 out_str(T_ME);
8437
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008438#ifdef FEAT_TERMGUICOLORS
8439 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008441 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008442 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008443 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008444 term_bg_rgb_color(cterm_normal_bg_gui_color);
8445 }
8446 else
8447#endif
8448 {
8449 if (t_colors > 1)
8450 {
8451 /* set Normal cterm colors */
8452 if (cterm_normal_fg_color != 0)
8453 term_fg_color(cterm_normal_fg_color - 1);
8454 if (cterm_normal_bg_color != 0)
8455 term_bg_color(cterm_normal_bg_color - 1);
8456 if (cterm_normal_fg_bold)
8457 out_str(T_MD);
8458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 }
8460 }
8461 }
8462 screen_attr = 0;
8463}
8464
8465/*
8466 * Reset the colors for a cterm. Used when leaving Vim.
8467 * The machine specific code may override this again.
8468 */
8469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008470reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008472 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
8474 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008475#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008476 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8477 || cterm_normal_bg_gui_color != INVALCOLOR)
8478 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008479#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {
8483 out_str(T_OP);
8484 screen_attr = -1;
8485 }
8486 if (cterm_normal_fg_bold)
8487 {
8488 out_str(T_ME);
8489 screen_attr = -1;
8490 }
8491 }
8492}
8493
8494/*
8495 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8496 * using the attributes from ScreenAttrs["off"].
8497 */
8498 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008499screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
8501 int attr;
8502
8503 /* Check for illegal values, just in case (could happen just after
8504 * resizing). */
8505 if (row >= screen_Rows || col >= screen_Columns)
8506 return;
8507
Bram Moolenaar494838a2015-02-10 19:20:37 +01008508 /* Outputting a character in the last cell on the screen may scroll the
8509 * screen up. Only do it when the "xn" termcap property is set, otherwise
8510 * mark the character invalid (update it when scrolled up). */
8511 if (*T_XN == NUL
8512 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513#ifdef FEAT_RIGHTLEFT
8514 /* account for first command-line character in rightleft mode */
8515 && !cmdmsg_rl
8516#endif
8517 )
8518 {
8519 ScreenAttrs[off] = (sattr_T)-1;
8520 return;
8521 }
8522
8523 /*
8524 * Stop highlighting first, so it's easier to move the cursor.
8525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (screen_char_attr != 0)
8527 attr = screen_char_attr;
8528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 attr = ScreenAttrs[off];
8530 if (screen_attr != attr)
8531 screen_stop_highlight();
8532
8533 windgoto(row, col);
8534
8535 if (screen_attr != attr)
8536 screen_start_highlight(attr);
8537
8538#ifdef FEAT_MBYTE
8539 if (enc_utf8 && ScreenLinesUC[off] != 0)
8540 {
8541 char_u buf[MB_MAXBYTES + 1];
8542
Bram Moolenaarcb070082016-04-02 22:14:51 +02008543 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008544 {
8545 if (*p_ambw == 'd'
8546# ifdef FEAT_GUI
8547 && !gui.in_use
8548# endif
8549 )
8550 {
8551 /* Clear the two screen cells. If the character is actually
8552 * single width it won't change the second cell. */
8553 out_str((char_u *)" ");
8554 term_windgoto(row, col);
8555 }
8556 /* not sure where the cursor is after drawing the ambiguous width
8557 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008558 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008559 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008560 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008562
8563 /* Convert the UTF-8 character to bytes and write it. */
8564 buf[utfc_char2bytes(off, buf)] = NUL;
8565 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 }
8567 else
8568#endif
8569 {
8570#ifdef FEAT_MBYTE
8571 out_flush_check();
8572#endif
8573 out_char(ScreenLines[off]);
8574#ifdef FEAT_MBYTE
8575 /* double-byte character in single-width cell */
8576 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8577 out_char(ScreenLines2[off]);
8578#endif
8579 }
8580
8581 screen_cur_col++;
8582}
8583
8584#ifdef FEAT_MBYTE
8585
8586/*
8587 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8588 * on the screen at position 'row' and 'col'.
8589 * The attributes of the first byte is used for all. This is required to
8590 * output the two bytes of a double-byte character with nothing in between.
8591 */
8592 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008593screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
8595 /* Check for illegal values (could be wrong when screen was resized). */
8596 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8597 return;
8598
8599 /* Outputting the last character on the screen may scrollup the screen.
8600 * Don't to it! Mark the character invalid (update it when scrolled up) */
8601 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8602 {
8603 ScreenAttrs[off] = (sattr_T)-1;
8604 return;
8605 }
8606
8607 /* Output the first byte normally (positions the cursor), then write the
8608 * second byte directly. */
8609 screen_char(off, row, col);
8610 out_char(ScreenLines[off + 1]);
8611 ++screen_cur_col;
8612}
8613#endif
8614
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615/*
8616 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8617 * This uses the contents of ScreenLines[] and doesn't change it.
8618 */
8619 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008620screen_draw_rectangle(
8621 int row,
8622 int col,
8623 int height,
8624 int width,
8625 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
8627 int r, c;
8628 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008629#ifdef FEAT_MBYTE
8630 int max_off;
8631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008633 /* Can't use ScreenLines unless initialized */
8634 if (ScreenLines == NULL)
8635 return;
8636
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 if (invert)
8638 screen_char_attr = HL_INVERSE;
8639 for (r = row; r < row + height; ++r)
8640 {
8641 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008642#ifdef FEAT_MBYTE
8643 max_off = off + screen_Columns;
8644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 for (c = col; c < col + width; ++c)
8646 {
8647#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008648 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 {
8650 screen_char_2(off + c, r, c);
8651 ++c;
8652 }
8653 else
8654#endif
8655 {
8656 screen_char(off + c, r, c);
8657#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008658 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 ++c;
8660#endif
8661 }
8662 }
8663 }
8664 screen_char_attr = 0;
8665}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667/*
8668 * Redraw the characters for a vertically split window.
8669 */
8670 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008671redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
8673 int col;
8674 int width;
8675
8676# ifdef FEAT_CLIPBOARD
8677 clip_may_clear_selection(row, end - 1);
8678# endif
8679
8680 if (wp == NULL)
8681 {
8682 col = 0;
8683 width = Columns;
8684 }
8685 else
8686 {
8687 col = wp->w_wincol;
8688 width = wp->w_width;
8689 }
8690 screen_draw_rectangle(row, col, end - row, width, FALSE);
8691}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008693 static void
8694space_to_screenline(int off, int attr)
8695{
8696 ScreenLines[off] = ' ';
8697 ScreenAttrs[off] = attr;
8698# ifdef FEAT_MBYTE
8699 if (enc_utf8)
8700 ScreenLinesUC[off] = 0;
8701# endif
8702}
8703
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704/*
8705 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8706 * with character 'c1' in first column followed by 'c2' in the other columns.
8707 * Use attributes 'attr'.
8708 */
8709 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008710screen_fill(
8711 int start_row,
8712 int end_row,
8713 int start_col,
8714 int end_col,
8715 int c1,
8716 int c2,
8717 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718{
8719 int row;
8720 int col;
8721 int off;
8722 int end_off;
8723 int did_delete;
8724 int c;
8725 int norm_term;
8726#if defined(FEAT_GUI) || defined(UNIX)
8727 int force_next = FALSE;
8728#endif
8729
8730 if (end_row > screen_Rows) /* safety check */
8731 end_row = screen_Rows;
8732 if (end_col > screen_Columns) /* safety check */
8733 end_col = screen_Columns;
8734 if (ScreenLines == NULL
8735 || start_row >= end_row
8736 || start_col >= end_col) /* nothing to do */
8737 return;
8738
8739 /* it's a "normal" terminal when not in a GUI or cterm */
8740 norm_term = (
8741#ifdef FEAT_GUI
8742 !gui.in_use &&
8743#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008744 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 for (row = start_row; row < end_row; ++row)
8746 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008747#ifdef FEAT_MBYTE
8748 if (has_mbyte
8749# ifdef FEAT_GUI
8750 && !gui.in_use
8751# endif
8752 )
8753 {
8754 /* When drawing over the right halve of a double-wide char clear
8755 * out the left halve. When drawing over the left halve of a
8756 * double wide-char clear out the right halve. Only needed in a
8757 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008758 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008759 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008760 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008761 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008762 }
8763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 /*
8765 * Try to use delete-line termcap code, when no attributes or in a
8766 * "normal" terminal, where a bold/italic space is just a
8767 * space.
8768 */
8769 did_delete = FALSE;
8770 if (c2 == ' '
8771 && end_col == Columns
8772 && can_clear(T_CE)
8773 && (attr == 0
8774 || (norm_term
8775 && attr <= HL_ALL
8776 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8777 {
8778 /*
8779 * check if we really need to clear something
8780 */
8781 col = start_col;
8782 if (c1 != ' ') /* don't clear first char */
8783 ++col;
8784
8785 off = LineOffset[row] + col;
8786 end_off = LineOffset[row] + end_col;
8787
8788 /* skip blanks (used often, keep it fast!) */
8789#ifdef FEAT_MBYTE
8790 if (enc_utf8)
8791 while (off < end_off && ScreenLines[off] == ' '
8792 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8793 ++off;
8794 else
8795#endif
8796 while (off < end_off && ScreenLines[off] == ' '
8797 && ScreenAttrs[off] == 0)
8798 ++off;
8799 if (off < end_off) /* something to be cleared */
8800 {
8801 col = off - LineOffset[row];
8802 screen_stop_highlight();
8803 term_windgoto(row, col);/* clear rest of this screen line */
8804 out_str(T_CE);
8805 screen_start(); /* don't know where cursor is now */
8806 col = end_col - col;
8807 while (col--) /* clear chars in ScreenLines */
8808 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008809 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 ++off;
8811 }
8812 }
8813 did_delete = TRUE; /* the chars are cleared now */
8814 }
8815
8816 off = LineOffset[row] + start_col;
8817 c = c1;
8818 for (col = start_col; col < end_col; ++col)
8819 {
8820 if (ScreenLines[off] != c
8821#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008822 || (enc_utf8 && (int)ScreenLinesUC[off]
8823 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824#endif
8825 || ScreenAttrs[off] != attr
8826#if defined(FEAT_GUI) || defined(UNIX)
8827 || force_next
8828#endif
8829 )
8830 {
8831#if defined(FEAT_GUI) || defined(UNIX)
8832 /* The bold trick may make a single row of pixels appear in
8833 * the next character. When a bold character is removed, the
8834 * next character should be redrawn too. This happens for our
8835 * own GUI and for some xterms. */
8836 if (
8837# ifdef FEAT_GUI
8838 gui.in_use
8839# endif
8840# if defined(FEAT_GUI) && defined(UNIX)
8841 ||
8842# endif
8843# ifdef UNIX
8844 term_is_xterm
8845# endif
8846 )
8847 {
8848 if (ScreenLines[off] != ' '
8849 && (ScreenAttrs[off] > HL_ALL
8850 || ScreenAttrs[off] & HL_BOLD))
8851 force_next = TRUE;
8852 else
8853 force_next = FALSE;
8854 }
8855#endif
8856 ScreenLines[off] = c;
8857#ifdef FEAT_MBYTE
8858 if (enc_utf8)
8859 {
8860 if (c >= 0x80)
8861 {
8862 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008863 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 }
8865 else
8866 ScreenLinesUC[off] = 0;
8867 }
8868#endif
8869 ScreenAttrs[off] = attr;
8870 if (!did_delete || c != ' ')
8871 screen_char(off, row, col);
8872 }
8873 ++off;
8874 if (col == start_col)
8875 {
8876 if (did_delete)
8877 break;
8878 c = c2;
8879 }
8880 }
8881 if (end_col == Columns)
8882 LineWraps[row] = FALSE;
8883 if (row == Rows - 1) /* overwritten the command line */
8884 {
8885 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008886 if (start_col == 0 && end_col == Columns
8887 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008889 if (start_col == 0)
8890 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 }
8892 }
8893}
8894
8895/*
8896 * Check if there should be a delay. Used before clearing or redrawing the
8897 * screen or the command line.
8898 */
8899 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008900check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
8902 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8903 && !did_wait_return
8904 && emsg_silent == 0)
8905 {
8906 out_flush();
8907 ui_delay(1000L, TRUE);
8908 emsg_on_display = FALSE;
8909 if (check_msg_scroll)
8910 msg_scroll = FALSE;
8911 }
8912}
8913
8914/*
8915 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008916 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 * Returns TRUE if there is a valid screen to write to.
8918 * Returns FALSE when starting up and screen not initialized yet.
8919 */
8920 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008921screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008923 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 return (ScreenLines != NULL);
8925}
8926
8927/*
8928 * Resize the shell to Rows and Columns.
8929 * Allocate ScreenLines[] and associated items.
8930 *
8931 * There may be some time between setting Rows and Columns and (re)allocating
8932 * ScreenLines[]. This happens when starting up and when (manually) changing
8933 * the shell size. Always use screen_Rows and screen_Columns to access items
8934 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8935 * final size of the shell is needed.
8936 */
8937 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008938screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
8940 int new_row, old_row;
8941#ifdef FEAT_GUI
8942 int old_Rows;
8943#endif
8944 win_T *wp;
8945 int outofmem = FALSE;
8946 int len;
8947 schar_T *new_ScreenLines;
8948#ifdef FEAT_MBYTE
8949 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008950 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008952 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953#endif
8954 sattr_T *new_ScreenAttrs;
8955 unsigned *new_LineOffset;
8956 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008957 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008958 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008960 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008961 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008963retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 /*
8965 * Allocation of the screen buffers is done only when the size changes and
8966 * when Rows and Columns have been set and we have started doing full
8967 * screen stuff.
8968 */
8969 if ((ScreenLines != NULL
8970 && Rows == screen_Rows
8971 && Columns == screen_Columns
8972#ifdef FEAT_MBYTE
8973 && enc_utf8 == (ScreenLinesUC != NULL)
8974 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008975 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976#endif
8977 )
8978 || Rows == 0
8979 || Columns == 0
8980 || (!full_screen && ScreenLines == NULL))
8981 return;
8982
8983 /*
8984 * It's possible that we produce an out-of-memory message below, which
8985 * will cause this function to be called again. To break the loop, just
8986 * return here.
8987 */
8988 if (entered)
8989 return;
8990 entered = TRUE;
8991
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008992 /*
8993 * Note that the window sizes are updated before reallocating the arrays,
8994 * thus we must not redraw here!
8995 */
8996 ++RedrawingDisabled;
8997
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 win_new_shellsize(); /* fit the windows in the new sized shell */
8999
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 comp_col(); /* recompute columns for shown command and ruler */
9001
9002 /*
9003 * We're changing the size of the screen.
9004 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9005 * - Move lines from the old arrays into the new arrays, clear extra
9006 * lines (unless the screen is going to be cleared).
9007 * - Free the old arrays.
9008 *
9009 * If anything fails, make ScreenLines NULL, so we don't do anything!
9010 * Continuing with the old ScreenLines may result in a crash, because the
9011 * size is wrong.
9012 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009013 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009015 if (aucmd_win != NULL)
9016 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017
9018 new_ScreenLines = (schar_T *)lalloc((long_u)(
9019 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9020#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01009021 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 if (enc_utf8)
9023 {
9024 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
9025 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009026 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01009027 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
9029 }
9030 if (enc_dbcs == DBCS_JPNU)
9031 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9032 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9033#endif
9034 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9035 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9036 new_LineOffset = (unsigned *)lalloc((long_u)(
9037 Rows * sizeof(unsigned)), FALSE);
9038 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009039 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009041 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 {
9043 if (win_alloc_lines(wp) == FAIL)
9044 {
9045 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009046 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 }
9048 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009049 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9050 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009051 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009052give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009054#ifdef FEAT_MBYTE
9055 for (i = 0; i < p_mco; ++i)
9056 if (new_ScreenLinesC[i] == NULL)
9057 break;
9058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 if (new_ScreenLines == NULL
9060#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009061 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9063#endif
9064 || new_ScreenAttrs == NULL
9065 || new_LineOffset == NULL
9066 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009067 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 || outofmem)
9069 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009070 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009071 {
9072 /* guess the size */
9073 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9074
9075 /* Remember we did this to avoid getting outofmem messages over
9076 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009077 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009078 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009079 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009081 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009082 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009083 VIM_CLEAR(new_ScreenLinesC[i]);
9084 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009086 VIM_CLEAR(new_ScreenAttrs);
9087 VIM_CLEAR(new_LineOffset);
9088 VIM_CLEAR(new_LineWraps);
9089 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 }
9091 else
9092 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009093 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009094
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 for (new_row = 0; new_row < Rows; ++new_row)
9096 {
9097 new_LineOffset[new_row] = new_row * Columns;
9098 new_LineWraps[new_row] = FALSE;
9099
9100 /*
9101 * If the screen is not going to be cleared, copy as much as
9102 * possible from the old screen to the new one and clear the rest
9103 * (used when resizing the window at the "--more--" prompt or when
9104 * executing an external command, for the GUI).
9105 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009106 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 {
9108 (void)vim_memset(new_ScreenLines + new_row * Columns,
9109 ' ', (size_t)Columns * sizeof(schar_T));
9110#ifdef FEAT_MBYTE
9111 if (enc_utf8)
9112 {
9113 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9114 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009115 for (i = 0; i < p_mco; ++i)
9116 (void)vim_memset(new_ScreenLinesC[i]
9117 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 0, (size_t)Columns * sizeof(u8char_T));
9119 }
9120 if (enc_dbcs == DBCS_JPNU)
9121 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9122 0, (size_t)Columns * sizeof(schar_T));
9123#endif
9124 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9125 0, (size_t)Columns * sizeof(sattr_T));
9126 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009127 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 {
9129 if (screen_Columns < Columns)
9130 len = screen_Columns;
9131 else
9132 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009133#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009134 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009135 * may be invalid now. Also when p_mco changes. */
9136 if (!(enc_utf8 && ScreenLinesUC == NULL)
9137 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009138#endif
9139 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9140 ScreenLines + LineOffset[old_row],
9141 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009143 if (enc_utf8 && ScreenLinesUC != NULL
9144 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 {
9146 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9147 ScreenLinesUC + LineOffset[old_row],
9148 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009149 for (i = 0; i < p_mco; ++i)
9150 mch_memmove(new_ScreenLinesC[i]
9151 + new_LineOffset[new_row],
9152 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 (size_t)len * sizeof(u8char_T));
9154 }
9155 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9156 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9157 ScreenLines2 + LineOffset[old_row],
9158 (size_t)len * sizeof(schar_T));
9159#endif
9160 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9161 ScreenAttrs + LineOffset[old_row],
9162 (size_t)len * sizeof(sattr_T));
9163 }
9164 }
9165 }
9166 /* Use the last line of the screen for the current line. */
9167 current_ScreenLine = new_ScreenLines + Rows * Columns;
9168 }
9169
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009170 free_screenlines();
9171
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 ScreenLines = new_ScreenLines;
9173#ifdef FEAT_MBYTE
9174 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009175 for (i = 0; i < p_mco; ++i)
9176 ScreenLinesC[i] = new_ScreenLinesC[i];
9177 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 ScreenLines2 = new_ScreenLines2;
9179#endif
9180 ScreenAttrs = new_ScreenAttrs;
9181 LineOffset = new_LineOffset;
9182 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009183 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184
9185 /* It's important that screen_Rows and screen_Columns reflect the actual
9186 * size of ScreenLines[]. Set them before calling anything. */
9187#ifdef FEAT_GUI
9188 old_Rows = screen_Rows;
9189#endif
9190 screen_Rows = Rows;
9191 screen_Columns = Columns;
9192
9193 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009194 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 screenclear2();
9196
9197#ifdef FEAT_GUI
9198 else if (gui.in_use
9199 && !gui.starting
9200 && ScreenLines != NULL
9201 && old_Rows != Rows)
9202 {
9203 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9204 /*
9205 * Adjust the position of the cursor, for when executing an external
9206 * command.
9207 */
9208 if (msg_row >= Rows) /* Rows got smaller */
9209 msg_row = Rows - 1; /* put cursor at last row */
9210 else if (Rows > old_Rows) /* Rows got bigger */
9211 msg_row += Rows - old_Rows; /* put cursor in same place */
9212 if (msg_col >= Columns) /* Columns got smaller */
9213 msg_col = Columns - 1; /* put cursor at last column */
9214 }
9215#endif
9216
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009218 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009219
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009220 /*
9221 * Do not apply autocommands more than 3 times to avoid an endless loop
9222 * in case applying autocommands always changes Rows or Columns.
9223 */
9224 if (starting == 0 && ++retry_count <= 3)
9225 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009226 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009227 /* In rare cases, autocommands may have altered Rows or Columns,
9228 * jump back to check if we need to allocate the screen again. */
9229 goto retry;
9230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231}
9232
9233 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009234free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009235{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009236#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009237 int i;
9238
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009239 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009240 for (i = 0; i < Screen_mco; ++i)
9241 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009242 vim_free(ScreenLines2);
9243#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009244 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009245 vim_free(ScreenAttrs);
9246 vim_free(LineOffset);
9247 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009248 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009249}
9250
9251 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009252screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253{
9254 check_for_delay(FALSE);
9255 screenalloc(FALSE); /* allocate screen buffers if size changed */
9256 screenclear2(); /* clear the screen */
9257}
9258
9259 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009260screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
9262 int i;
9263
9264 if (starting == NO_SCREEN || ScreenLines == NULL
9265#ifdef FEAT_GUI
9266 || (gui.in_use && gui.starting)
9267#endif
9268 )
9269 return;
9270
9271#ifdef FEAT_GUI
9272 if (!gui.in_use)
9273#endif
9274 screen_attr = -1; /* force setting the Normal colors */
9275 screen_stop_highlight(); /* don't want highlighting here */
9276
9277#ifdef FEAT_CLIPBOARD
9278 /* disable selection without redrawing it */
9279 clip_scroll_selection(9999);
9280#endif
9281
9282 /* blank out ScreenLines */
9283 for (i = 0; i < Rows; ++i)
9284 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009285 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 LineWraps[i] = FALSE;
9287 }
9288
9289 if (can_clear(T_CL))
9290 {
9291 out_str(T_CL); /* clear the display */
9292 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009293 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 }
9295 else
9296 {
9297 /* can't clear the screen, mark all chars with invalid attributes */
9298 for (i = 0; i < Rows; ++i)
9299 lineinvalid(LineOffset[i], (int)Columns);
9300 clear_cmdline = TRUE;
9301 }
9302
9303 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9304
9305 win_rest_invalid(firstwin);
9306 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009307 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 if (must_redraw == CLEAR) /* no need to clear again */
9309 must_redraw = NOT_VALID;
9310 compute_cmdrow();
9311 msg_row = cmdline_row; /* put cursor on last line for messages */
9312 msg_col = 0;
9313 screen_start(); /* don't know where cursor is now */
9314 msg_scrolled = 0; /* can't scroll back */
9315 msg_didany = FALSE;
9316 msg_didout = FALSE;
9317}
9318
9319/*
9320 * Clear one line in ScreenLines.
9321 */
9322 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009323lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9326#ifdef FEAT_MBYTE
9327 if (enc_utf8)
9328 (void)vim_memset(ScreenLinesUC + off, 0,
9329 (size_t)width * sizeof(u8char_T));
9330#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009331 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332}
9333
9334/*
9335 * Mark one line in ScreenLines invalid by setting the attributes to an
9336 * invalid value.
9337 */
9338 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009339lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9342}
9343
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344/*
9345 * Copy part of a Screenline for vertically split window "wp".
9346 */
9347 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009348linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
9350 unsigned off_to = LineOffset[to] + wp->w_wincol;
9351 unsigned off_from = LineOffset[from] + wp->w_wincol;
9352
9353 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9354 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009355#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 if (enc_utf8)
9357 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009358 int i;
9359
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9361 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009362 for (i = 0; i < p_mco; ++i)
9363 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9364 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 }
9366 if (enc_dbcs == DBCS_JPNU)
9367 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9368 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9371 wp->w_width * sizeof(sattr_T));
9372}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373
9374/*
9375 * Return TRUE if clearing with term string "p" would work.
9376 * It can't work when the string is empty or it won't set the right background.
9377 */
9378 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009379can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
9381 return (*p != NUL && (t_colors <= 1
9382#ifdef FEAT_GUI
9383 || gui.in_use
9384#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009385#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009386 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009387 || (!p_tgc && cterm_normal_bg_color == 0)
9388#else
9389 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009390#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009391 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392}
9393
9394/*
9395 * Reset cursor position. Use whenever cursor was moved because of outputting
9396 * something directly to the screen (shell commands) or a terminal control
9397 * code.
9398 */
9399 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009400screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
9402 screen_cur_row = screen_cur_col = 9999;
9403}
9404
9405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 * Move the cursor to position "row","col" in the screen.
9407 * This tries to find the most efficient way to move, minimizing the number of
9408 * characters sent to the terminal.
9409 */
9410 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009411windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009413 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 int i;
9415 int plan;
9416 int cost;
9417 int wouldbe_col;
9418 int noinvcurs;
9419 char_u *bs;
9420 int goto_cost;
9421 int attr;
9422
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009423#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9425
9426#define PLAN_LE 1
9427#define PLAN_CR 2
9428#define PLAN_NL 3
9429#define PLAN_WRITE 4
9430 /* Can't use ScreenLines unless initialized */
9431 if (ScreenLines == NULL)
9432 return;
9433
9434 if (col != screen_cur_col || row != screen_cur_row)
9435 {
9436 /* Check for valid position. */
9437 if (row < 0) /* window without text lines? */
9438 row = 0;
9439 if (row >= screen_Rows)
9440 row = screen_Rows - 1;
9441 if (col >= screen_Columns)
9442 col = screen_Columns - 1;
9443
9444 /* check if no cursor movement is allowed in highlight mode */
9445 if (screen_attr && *T_MS == NUL)
9446 noinvcurs = HIGHL_COST;
9447 else
9448 noinvcurs = 0;
9449 goto_cost = GOTO_COST + noinvcurs;
9450
9451 /*
9452 * Plan how to do the positioning:
9453 * 1. Use CR to move it to column 0, same row.
9454 * 2. Use T_LE to move it a few columns to the left.
9455 * 3. Use NL to move a few lines down, column 0.
9456 * 4. Move a few columns to the right with T_ND or by writing chars.
9457 *
9458 * Don't do this if the cursor went beyond the last column, the cursor
9459 * position is unknown then (some terminals wrap, some don't )
9460 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009461 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 * characters to move the cursor to the right.
9463 */
9464 if (row >= screen_cur_row && screen_cur_col < Columns)
9465 {
9466 /*
9467 * If the cursor is in the same row, bigger col, we can use CR
9468 * or T_LE.
9469 */
9470 bs = NULL; /* init for GCC */
9471 attr = screen_attr;
9472 if (row == screen_cur_row && col < screen_cur_col)
9473 {
9474 /* "le" is preferred over "bc", because "bc" is obsolete */
9475 if (*T_LE)
9476 bs = T_LE; /* "cursor left" */
9477 else
9478 bs = T_BC; /* "backspace character (old) */
9479 if (*bs)
9480 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9481 else
9482 cost = 999;
9483 if (col + 1 < cost) /* using CR is less characters */
9484 {
9485 plan = PLAN_CR;
9486 wouldbe_col = 0;
9487 cost = 1; /* CR is just one character */
9488 }
9489 else
9490 {
9491 plan = PLAN_LE;
9492 wouldbe_col = col;
9493 }
9494 if (noinvcurs) /* will stop highlighting */
9495 {
9496 cost += noinvcurs;
9497 attr = 0;
9498 }
9499 }
9500
9501 /*
9502 * If the cursor is above where we want to be, we can use CR LF.
9503 */
9504 else if (row > screen_cur_row)
9505 {
9506 plan = PLAN_NL;
9507 wouldbe_col = 0;
9508 cost = (row - screen_cur_row) * 2; /* CR LF */
9509 if (noinvcurs) /* will stop highlighting */
9510 {
9511 cost += noinvcurs;
9512 attr = 0;
9513 }
9514 }
9515
9516 /*
9517 * If the cursor is in the same row, smaller col, just use write.
9518 */
9519 else
9520 {
9521 plan = PLAN_WRITE;
9522 wouldbe_col = screen_cur_col;
9523 cost = 0;
9524 }
9525
9526 /*
9527 * Check if any characters that need to be written have the
9528 * correct attributes. Also avoid UTF-8 characters.
9529 */
9530 i = col - wouldbe_col;
9531 if (i > 0)
9532 cost += i;
9533 if (cost < goto_cost && i > 0)
9534 {
9535 /*
9536 * Check if the attributes are correct without additionally
9537 * stopping highlighting.
9538 */
9539 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9540 while (i && *p++ == attr)
9541 --i;
9542 if (i != 0)
9543 {
9544 /*
9545 * Try if it works when highlighting is stopped here.
9546 */
9547 if (*--p == 0)
9548 {
9549 cost += noinvcurs;
9550 while (i && *p++ == 0)
9551 --i;
9552 }
9553 if (i != 0)
9554 cost = 999; /* different attributes, don't do it */
9555 }
9556#ifdef FEAT_MBYTE
9557 if (enc_utf8)
9558 {
9559 /* Don't use an UTF-8 char for positioning, it's slow. */
9560 for (i = wouldbe_col; i < col; ++i)
9561 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9562 {
9563 cost = 999;
9564 break;
9565 }
9566 }
9567#endif
9568 }
9569
9570 /*
9571 * We can do it without term_windgoto()!
9572 */
9573 if (cost < goto_cost)
9574 {
9575 if (plan == PLAN_LE)
9576 {
9577 if (noinvcurs)
9578 screen_stop_highlight();
9579 while (screen_cur_col > col)
9580 {
9581 out_str(bs);
9582 --screen_cur_col;
9583 }
9584 }
9585 else if (plan == PLAN_CR)
9586 {
9587 if (noinvcurs)
9588 screen_stop_highlight();
9589 out_char('\r');
9590 screen_cur_col = 0;
9591 }
9592 else if (plan == PLAN_NL)
9593 {
9594 if (noinvcurs)
9595 screen_stop_highlight();
9596 while (screen_cur_row < row)
9597 {
9598 out_char('\n');
9599 ++screen_cur_row;
9600 }
9601 screen_cur_col = 0;
9602 }
9603
9604 i = col - screen_cur_col;
9605 if (i > 0)
9606 {
9607 /*
9608 * Use cursor-right if it's one character only. Avoids
9609 * removing a line of pixels from the last bold char, when
9610 * using the bold trick in the GUI.
9611 */
9612 if (T_ND[0] != NUL && T_ND[1] == NUL)
9613 {
9614 while (i-- > 0)
9615 out_char(*T_ND);
9616 }
9617 else
9618 {
9619 int off;
9620
9621 off = LineOffset[row] + screen_cur_col;
9622 while (i-- > 0)
9623 {
9624 if (ScreenAttrs[off] != screen_attr)
9625 screen_stop_highlight();
9626#ifdef FEAT_MBYTE
9627 out_flush_check();
9628#endif
9629 out_char(ScreenLines[off]);
9630#ifdef FEAT_MBYTE
9631 if (enc_dbcs == DBCS_JPNU
9632 && ScreenLines[off] == 0x8e)
9633 out_char(ScreenLines2[off]);
9634#endif
9635 ++off;
9636 }
9637 }
9638 }
9639 }
9640 }
9641 else
9642 cost = 999;
9643
9644 if (cost >= goto_cost)
9645 {
9646 if (noinvcurs)
9647 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009648 if (row == screen_cur_row && (col > screen_cur_col)
9649 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 term_cursor_right(col - screen_cur_col);
9651 else
9652 term_windgoto(row, col);
9653 }
9654 screen_cur_row = row;
9655 screen_cur_col = col;
9656 }
9657}
9658
9659/*
9660 * Set cursor to its position in the current window.
9661 */
9662 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009663setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009665 setcursor_mayforce(FALSE);
9666}
9667
9668/*
9669 * Set cursor to its position in the current window.
9670 * When "force" is TRUE also when not redrawing.
9671 */
9672 void
9673setcursor_mayforce(int force)
9674{
9675 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
9677 validate_cursor();
9678 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009679 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009681 /* With 'rightleft' set and the cursor on a double-wide
9682 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009683 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009685 (has_mbyte
9686 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9687 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688# endif
9689 1)) :
9690#endif
9691 curwin->w_wcol));
9692 }
9693}
9694
9695
9696/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009697 * Insert 'line_count' lines at 'row' in window 'wp'.
9698 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9699 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 * scrolling.
9701 * Returns FAIL if the lines are not inserted, OK for success.
9702 */
9703 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009704win_ins_lines(
9705 win_T *wp,
9706 int row,
9707 int line_count,
9708 int invalid,
9709 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 int did_delete;
9712 int nextrow;
9713 int lastrow;
9714 int retval;
9715
9716 if (invalid)
9717 wp->w_lines_valid = 0;
9718
9719 if (wp->w_height < 5)
9720 return FAIL;
9721
9722 if (line_count > wp->w_height - row)
9723 line_count = wp->w_height - row;
9724
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009725 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 if (retval != MAYBE)
9727 return retval;
9728
9729 /*
9730 * If there is a next window or a status line, we first try to delete the
9731 * lines at the bottom to avoid messing what is after the window.
9732 * If this fails and there are following windows, don't do anything to avoid
9733 * messing up those windows, better just redraw.
9734 */
9735 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 if (wp->w_next != NULL || wp->w_status_height)
9737 {
9738 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009739 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 did_delete = TRUE;
9741 else if (wp->w_next)
9742 return FAIL;
9743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 /*
9745 * if no lines deleted, blank the lines that will end up below the window
9746 */
9747 if (!did_delete)
9748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009751 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 lastrow = nextrow + line_count;
9753 if (lastrow > Rows)
9754 lastrow = Rows;
9755 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009756 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 ' ', ' ', 0);
9758 }
9759
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009760 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 == FAIL)
9762 {
9763 /* deletion will have messed up other windows */
9764 if (did_delete)
9765 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 win_rest_invalid(W_NEXT(wp));
9768 }
9769 return FAIL;
9770 }
9771
9772 return OK;
9773}
9774
9775/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009776 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9778 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9779 * scrolling
9780 * Return OK for success, FAIL if the lines are not deleted.
9781 */
9782 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009783win_del_lines(
9784 win_T *wp,
9785 int row,
9786 int line_count,
9787 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009788 int mayclear,
9789 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790{
9791 int retval;
9792
9793 if (invalid)
9794 wp->w_lines_valid = 0;
9795
9796 if (line_count > wp->w_height - row)
9797 line_count = wp->w_height - row;
9798
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009799 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 if (retval != MAYBE)
9801 return retval;
9802
9803 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009804 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 return FAIL;
9806
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 /*
9808 * If there are windows or status lines below, try to put them at the
9809 * correct place. If we can't do that, they have to be redrawn.
9810 */
9811 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9812 {
9813 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009814 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 {
9816 wp->w_redr_status = TRUE;
9817 win_rest_invalid(wp->w_next);
9818 }
9819 }
9820 /*
9821 * If this is the last window and there is no status line, redraw the
9822 * command line later.
9823 */
9824 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 redraw_cmdline = TRUE;
9826 return OK;
9827}
9828
9829/*
9830 * Common code for win_ins_lines() and win_del_lines().
9831 * Returns OK or FAIL when the work has been done.
9832 * Returns MAYBE when not finished yet.
9833 */
9834 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009835win_do_lines(
9836 win_T *wp,
9837 int row,
9838 int line_count,
9839 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009840 int del,
9841 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842{
9843 int retval;
9844
9845 if (!redrawing() || line_count <= 0)
9846 return FAIL;
9847
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009848 /* When inserting lines would result in loss of command output, just redraw
9849 * the lines. */
9850 if (no_win_do_lines_ins && !del)
9851 return FAIL;
9852
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009854 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009856 if (!no_win_do_lines_ins)
9857 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 return FAIL;
9859 }
9860
9861 /*
9862 * Delete all remaining lines
9863 */
9864 if (row + line_count >= wp->w_height)
9865 {
9866 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009867 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 ' ', ' ', 0);
9869 return OK;
9870 }
9871
9872 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009873 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009875 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009877 if (!no_win_do_lines_ins)
9878 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879
9880 /*
9881 * If the terminal can set a scroll region, use that.
9882 * Always do this in a vertically split window. This will redraw from
9883 * ScreenLines[] when t_CV isn't defined. That's faster than using
9884 * win_line().
9885 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009886 * a character in the lower right corner of the scroll region may cause a
9887 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009889 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 scroll_region_set(wp, row);
9893 if (del)
9894 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009895 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 else
9897 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009898 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 scroll_region_reset();
9901 return retval;
9902 }
9903
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9905 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906
9907 return MAYBE;
9908}
9909
9910/*
9911 * window 'wp' and everything after it is messed up, mark it for redraw
9912 */
9913 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009914win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 {
9918 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 wp->w_redr_status = TRUE;
9920 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 }
9922 redraw_cmdline = TRUE;
9923}
9924
9925/*
9926 * The rest of the routines in this file perform screen manipulations. The
9927 * given operation is performed physically on the screen. The corresponding
9928 * change is also made to the internal screen image. In this way, the editor
9929 * anticipates the effect of editing changes on the appearance of the screen.
9930 * That way, when we call screenupdate a complete redraw isn't usually
9931 * necessary. Another advantage is that we can keep adding code to anticipate
9932 * screen changes, and in the meantime, everything still works.
9933 */
9934
9935/*
9936 * types for inserting or deleting lines
9937 */
9938#define USE_T_CAL 1
9939#define USE_T_CDL 2
9940#define USE_T_AL 3
9941#define USE_T_CE 4
9942#define USE_T_DL 5
9943#define USE_T_SR 6
9944#define USE_NL 7
9945#define USE_T_CD 8
9946#define USE_REDRAW 9
9947
9948/*
9949 * insert lines on the screen and update ScreenLines[]
9950 * 'end' is the line after the scrolled part. Normally it is Rows.
9951 * When scrolling region used 'off' is the offset from the top for the region.
9952 * 'row' and 'end' are relative to the start of the region.
9953 *
9954 * return FAIL for failure, OK for success.
9955 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009956 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009957screen_ins_lines(
9958 int off,
9959 int row,
9960 int line_count,
9961 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009962 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009963 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965 int i;
9966 int j;
9967 unsigned temp;
9968 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009969 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 int type;
9971 int result_empty;
9972 int can_ce = can_clear(T_CE);
9973
9974 /*
9975 * FAIL if
9976 * - there is no valid screen
9977 * - the screen has to be redrawn completely
9978 * - the line count is less than one
9979 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009980 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009982 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9983#ifdef FEAT_CLIPBOARD
9984 || (clip_star.state != SELECT_CLEARED
9985 && redrawing_for_callback > 0)
9986#endif
9987 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 return FAIL;
9989
9990 /*
9991 * There are seven ways to insert lines:
9992 * 0. When in a vertically split window and t_CV isn't set, redraw the
9993 * characters from ScreenLines[].
9994 * 1. Use T_CD (clear to end of display) if it exists and the result of
9995 * the insert is just empty lines
9996 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9997 * present or line_count > 1. It looks better if we do all the inserts
9998 * at once.
9999 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10000 * insert is just empty lines and T_CE is not present or line_count >
10001 * 1.
10002 * 4. Use T_AL (insert line) if it exists.
10003 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10004 * just empty lines.
10005 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10006 * just empty lines.
10007 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10008 * the 'da' flag is not set or we have clear line capability.
10009 * 8. redraw the characters from ScreenLines[].
10010 *
10011 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10012 * the scrollbar for the window. It does have insert line, use that if it
10013 * exists.
10014 */
10015 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10017 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010018 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 type = USE_T_CD;
10020 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10021 type = USE_T_CAL;
10022 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10023 type = USE_T_CDL;
10024 else if (*T_AL != NUL)
10025 type = USE_T_AL;
10026 else if (can_ce && result_empty)
10027 type = USE_T_CE;
10028 else if (*T_DL != NUL && result_empty)
10029 type = USE_T_DL;
10030 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10031 type = USE_T_SR;
10032 else
10033 return FAIL;
10034
10035 /*
10036 * For clearing the lines screen_del_lines() is used. This will also take
10037 * care of t_db if necessary.
10038 */
10039 if (type == USE_T_CD || type == USE_T_CDL ||
10040 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010041 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
10043 /*
10044 * If text is retained below the screen, first clear or delete as many
10045 * lines at the bottom of the window as are about to be inserted so that
10046 * the deleted lines won't later surface during a screen_del_lines.
10047 */
10048 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010049 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
10051#ifdef FEAT_CLIPBOARD
10052 /* Remove a modeless selection when inserting lines halfway the screen
10053 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010054 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010055 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 else
10057 clip_scroll_selection(-line_count);
10058#endif
10059
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060#ifdef FEAT_GUI
10061 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10062 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010063 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064#endif
10065
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010066 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10067 cursor_col = wp->w_wincol;
10068
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (*T_CCS != NUL) /* cursor relative to region */
10070 cursor_row = row;
10071 else
10072 cursor_row = row + off;
10073
10074 /*
10075 * Shift LineOffset[] line_count down to reflect the inserted lines.
10076 * Clear the inserted lines in ScreenLines[].
10077 */
10078 row += off;
10079 end += off;
10080 for (i = 0; i < line_count; ++i)
10081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 if (wp != NULL && wp->w_width != Columns)
10083 {
10084 /* need to copy part of a line */
10085 j = end - 1 - i;
10086 while ((j -= line_count) >= row)
10087 linecopy(j + line_count, j, wp);
10088 j += line_count;
10089 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010090 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10091 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 else
10093 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10094 LineWraps[j] = FALSE;
10095 }
10096 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 {
10098 j = end - 1 - i;
10099 temp = LineOffset[j];
10100 while ((j -= line_count) >= row)
10101 {
10102 LineOffset[j + line_count] = LineOffset[j];
10103 LineWraps[j + line_count] = LineWraps[j];
10104 }
10105 LineOffset[j + line_count] = temp;
10106 LineWraps[j + line_count] = FALSE;
10107 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010108 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 else
10110 lineinvalid(temp, (int)Columns);
10111 }
10112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113
10114 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010115 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010116 if (clear_attr != 0)
10117 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 /* redraw the characters */
10120 if (type == USE_REDRAW)
10121 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010122 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 {
10124 term_append_lines(line_count);
10125 screen_start(); /* don't know where cursor is now */
10126 }
10127 else
10128 {
10129 for (i = 0; i < line_count; i++)
10130 {
10131 if (type == USE_T_AL)
10132 {
10133 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010134 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 out_str(T_AL);
10136 }
10137 else /* type == USE_T_SR */
10138 out_str(T_SR);
10139 screen_start(); /* don't know where cursor is now */
10140 }
10141 }
10142
10143 /*
10144 * With scroll-reverse and 'da' flag set we need to clear the lines that
10145 * have been scrolled down into the region.
10146 */
10147 if (type == USE_T_SR && *T_DA)
10148 {
10149 for (i = 0; i < line_count; ++i)
10150 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010151 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 out_str(T_CE);
10153 screen_start(); /* don't know where cursor is now */
10154 }
10155 }
10156
10157#ifdef FEAT_GUI
10158 gui_can_update_cursor();
10159 if (gui.in_use)
10160 out_flush(); /* always flush after a scroll */
10161#endif
10162 return OK;
10163}
10164
10165/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010166 * Delete lines on the screen and update ScreenLines[].
10167 * "end" is the line after the scrolled part. Normally it is Rows.
10168 * When scrolling region used "off" is the offset from the top for the region.
10169 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 *
10171 * Return OK for success, FAIL if the lines are not deleted.
10172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010174screen_del_lines(
10175 int off,
10176 int row,
10177 int line_count,
10178 int end,
10179 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010180 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010181 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182{
10183 int j;
10184 int i;
10185 unsigned temp;
10186 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010187 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 int cursor_end;
10189 int result_empty; /* result is empty until end of region */
10190 int can_delete; /* deleting line codes can be used */
10191 int type;
10192
10193 /*
10194 * FAIL if
10195 * - there is no valid screen
10196 * - the screen has to be redrawn completely
10197 * - the line count is less than one
10198 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010199 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010201 if (!screen_valid(TRUE) || line_count <= 0
10202 || (!force && line_count > p_ttyscroll)
10203#ifdef FEAT_CLIPBOARD
10204 || (clip_star.state != SELECT_CLEARED
10205 && redrawing_for_callback > 0)
10206#endif
10207 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 return FAIL;
10209
10210 /*
10211 * Check if the rest of the current region will become empty.
10212 */
10213 result_empty = row + line_count >= end;
10214
10215 /*
10216 * We can delete lines only when 'db' flag not set or when 'ce' option
10217 * available.
10218 */
10219 can_delete = (*T_DB == NUL || can_clear(T_CE));
10220
10221 /*
10222 * There are six ways to delete lines:
10223 * 0. When in a vertically split window and t_CV isn't set, redraw the
10224 * characters from ScreenLines[].
10225 * 1. Use T_CD if it exists and the result is empty.
10226 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10227 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10228 * none of the other ways work.
10229 * 4. Use T_CE (erase line) if the result is empty.
10230 * 5. Use T_DL (delete line) if it exists.
10231 * 6. redraw the characters from ScreenLines[].
10232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10234 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010235 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 type = USE_T_CD;
10237#if defined(__BEOS__) && defined(BEOS_DR8)
10238 /*
10239 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10240 * its internal termcap... this works okay for tests which test *T_DB !=
10241 * NUL. It has the disadvantage that the user cannot use any :set t_*
10242 * command to get T_DB (back) to empty_option, only :set term=... will do
10243 * the trick...
10244 * Anyway, this hack will hopefully go away with the next OS release.
10245 * (Olaf Seibert)
10246 */
10247 else if (row == 0 && T_DB == empty_option
10248 && (line_count == 1 || *T_CDL == NUL))
10249#else
10250 else if (row == 0 && (
10251#ifndef AMIGA
10252 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10253 * up, so use delete-line command */
10254 line_count == 1 ||
10255#endif
10256 *T_CDL == NUL))
10257#endif
10258 type = USE_NL;
10259 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10260 type = USE_T_CDL;
10261 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010262 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 type = USE_T_CE;
10264 else if (*T_DL != NUL && can_delete)
10265 type = USE_T_DL;
10266 else if (*T_CDL != NUL && can_delete)
10267 type = USE_T_CDL;
10268 else
10269 return FAIL;
10270
10271#ifdef FEAT_CLIPBOARD
10272 /* Remove a modeless selection when deleting lines halfway the screen or
10273 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010274 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010275 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 else
10277 clip_scroll_selection(line_count);
10278#endif
10279
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280#ifdef FEAT_GUI
10281 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10282 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010283 gui_dont_update_cursor(gui.cursor_row >= row + off
10284 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285#endif
10286
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010287 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10288 cursor_col = wp->w_wincol;
10289
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 if (*T_CCS != NUL) /* cursor relative to region */
10291 {
10292 cursor_row = row;
10293 cursor_end = end;
10294 }
10295 else
10296 {
10297 cursor_row = row + off;
10298 cursor_end = end + off;
10299 }
10300
10301 /*
10302 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10303 * Clear the inserted lines in ScreenLines[].
10304 */
10305 row += off;
10306 end += off;
10307 for (i = 0; i < line_count; ++i)
10308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 if (wp != NULL && wp->w_width != Columns)
10310 {
10311 /* need to copy part of a line */
10312 j = row + i;
10313 while ((j += line_count) <= end - 1)
10314 linecopy(j - line_count, j, wp);
10315 j -= line_count;
10316 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010317 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10318 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 else
10320 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10321 LineWraps[j] = FALSE;
10322 }
10323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 {
10325 /* whole width, moving the line pointers is faster */
10326 j = row + i;
10327 temp = LineOffset[j];
10328 while ((j += line_count) <= end - 1)
10329 {
10330 LineOffset[j - line_count] = LineOffset[j];
10331 LineWraps[j - line_count] = LineWraps[j];
10332 }
10333 LineOffset[j - line_count] = temp;
10334 LineWraps[j - line_count] = FALSE;
10335 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010336 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 else
10338 lineinvalid(temp, (int)Columns);
10339 }
10340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010342 if (screen_attr != clear_attr)
10343 screen_stop_highlight();
10344 if (clear_attr != 0)
10345 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 /* redraw the characters */
10348 if (type == USE_REDRAW)
10349 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010350 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010352 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 out_str(T_CD);
10354 screen_start(); /* don't know where cursor is now */
10355 }
10356 else if (type == USE_T_CDL)
10357 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010358 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 term_delete_lines(line_count);
10360 screen_start(); /* don't know where cursor is now */
10361 }
10362 /*
10363 * Deleting lines at top of the screen or scroll region: Just scroll
10364 * the whole screen (scroll region) up by outputting newlines on the
10365 * last line.
10366 */
10367 else if (type == USE_NL)
10368 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010369 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 for (i = line_count; --i >= 0; )
10371 out_char('\n'); /* cursor will remain on same line */
10372 }
10373 else
10374 {
10375 for (i = line_count; --i >= 0; )
10376 {
10377 if (type == USE_T_DL)
10378 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010379 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 out_str(T_DL); /* delete a line */
10381 }
10382 else /* type == USE_T_CE */
10383 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010384 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 out_str(T_CE); /* erase a line */
10386 }
10387 screen_start(); /* don't know where cursor is now */
10388 }
10389 }
10390
10391 /*
10392 * If the 'db' flag is set, we need to clear the lines that have been
10393 * scrolled up at the bottom of the region.
10394 */
10395 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10396 {
10397 for (i = line_count; i > 0; --i)
10398 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010399 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 out_str(T_CE); /* erase a line */
10401 screen_start(); /* don't know where cursor is now */
10402 }
10403 }
10404
10405#ifdef FEAT_GUI
10406 gui_can_update_cursor();
10407 if (gui.in_use)
10408 out_flush(); /* always flush after a scroll */
10409#endif
10410
10411 return OK;
10412}
10413
10414/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010415 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 *
10417 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10418 * If clear_cmdline is FALSE there may be a message there that needs to be
10419 * cleared only if a mode is shown.
10420 * Return the length of the message (0 if no message).
10421 */
10422 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010423showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424{
10425 int need_clear;
10426 int length = 0;
10427 int do_mode;
10428 int attr;
10429 int nwr_save;
10430#ifdef FEAT_INS_EXPAND
10431 int sub_attr;
10432#endif
10433
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010434 do_mode = ((p_smd && msg_silent == 0)
10435 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010436 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010437 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010438 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 {
10440 /*
10441 * Don't show mode right now, when not redrawing or inside a mapping.
10442 * Call char_avail() only when we are going to show something, because
10443 * it takes a bit of time.
10444 */
10445 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10446 {
10447 redraw_cmdline = TRUE; /* show mode later */
10448 return 0;
10449 }
10450
10451 nwr_save = need_wait_return;
10452
10453 /* wait a bit before overwriting an important message */
10454 check_for_delay(FALSE);
10455
10456 /* if the cmdline is more than one line high, erase top lines */
10457 need_clear = clear_cmdline;
10458 if (clear_cmdline && cmdline_row < Rows - 1)
10459 msg_clr_cmdline(); /* will reset clear_cmdline */
10460
10461 /* Position on the last line in the window, column 0 */
10462 msg_pos_mode();
10463 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010464 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 if (do_mode)
10466 {
10467 MSG_PUTS_ATTR("--", attr);
10468#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010469 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010470# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010471 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010472# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010473 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010474# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010475 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010476# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 MSG_PUTS_ATTR(" IM", attr);
10478# else
10479 MSG_PUTS_ATTR(" XIM", attr);
10480# endif
10481#endif
10482#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10483 if (gui.in_use)
10484 {
10485 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010486 {
10487 /* HANGUL */
10488 if (enc_utf8)
10489 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10490 else
10491 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 }
10494#endif
10495#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010496 /* CTRL-X in Insert mode */
10497 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 {
10499 /* These messages can get long, avoid a wrap in a narrow
10500 * window. Prefer showing edit_submode_extra. */
10501 length = (Rows - msg_row) * Columns - 3;
10502 if (edit_submode_extra != NULL)
10503 length -= vim_strsize(edit_submode_extra);
10504 if (length > 0)
10505 {
10506 if (edit_submode_pre != NULL)
10507 length -= vim_strsize(edit_submode_pre);
10508 if (length - vim_strsize(edit_submode) > 0)
10509 {
10510 if (edit_submode_pre != NULL)
10511 msg_puts_attr(edit_submode_pre, attr);
10512 msg_puts_attr(edit_submode, attr);
10513 }
10514 if (edit_submode_extra != NULL)
10515 {
10516 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10517 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010518 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 else
10520 sub_attr = attr;
10521 msg_puts_attr(edit_submode_extra, sub_attr);
10522 }
10523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 }
10525 else
10526#endif
10527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 if (State & VREPLACE_FLAG)
10529 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010530 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10532 else if (State & INSERT)
10533 {
10534#ifdef FEAT_RIGHTLEFT
10535 if (p_ri)
10536 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10537#endif
10538 MSG_PUTS_ATTR(_(" INSERT"), attr);
10539 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010540 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 MSG_PUTS_ATTR(_(" (insert)"), attr);
10542 else if (restart_edit == 'R')
10543 MSG_PUTS_ATTR(_(" (replace)"), attr);
10544 else if (restart_edit == 'V')
10545 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10546#ifdef FEAT_RIGHTLEFT
10547 if (p_hkmap)
10548 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10549# ifdef FEAT_FKMAP
10550 if (p_fkmap)
10551 MSG_PUTS_ATTR(farsi_text_5, attr);
10552# endif
10553#endif
10554#ifdef FEAT_KEYMAP
10555 if (State & LANGMAP)
10556 {
10557# ifdef FEAT_ARABIC
10558 if (curwin->w_p_arab)
10559 MSG_PUTS_ATTR(_(" Arabic"), attr);
10560 else
10561# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010562 if (get_keymap_str(curwin, (char_u *)" (%s)",
10563 NameBuff, MAXPATHL))
10564 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 }
10566#endif
10567 if ((State & INSERT) && p_paste)
10568 MSG_PUTS_ATTR(_(" (paste)"), attr);
10569
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 if (VIsual_active)
10571 {
10572 char *p;
10573
10574 /* Don't concatenate separate words to avoid translation
10575 * problems. */
10576 switch ((VIsual_select ? 4 : 0)
10577 + (VIsual_mode == Ctrl_V) * 2
10578 + (VIsual_mode == 'V'))
10579 {
10580 case 0: p = N_(" VISUAL"); break;
10581 case 1: p = N_(" VISUAL LINE"); break;
10582 case 2: p = N_(" VISUAL BLOCK"); break;
10583 case 4: p = N_(" SELECT"); break;
10584 case 5: p = N_(" SELECT LINE"); break;
10585 default: p = N_(" SELECT BLOCK"); break;
10586 }
10587 MSG_PUTS_ATTR(_(p), attr);
10588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 MSG_PUTS_ATTR(" --", attr);
10590 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010591
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 need_clear = TRUE;
10593 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010594 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#ifdef FEAT_INS_EXPAND
10596 && edit_submode == NULL /* otherwise it gets too long */
10597#endif
10598 )
10599 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010600 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 need_clear = TRUE;
10602 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010603
10604 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 if (need_clear || clear_cmdline)
10606 msg_clr_eos();
10607 msg_didout = FALSE; /* overwrite this message */
10608 length = msg_col;
10609 msg_col = 0;
10610 need_wait_return = nwr_save; /* never ask for hit-return for this */
10611 }
10612 else if (clear_cmdline && msg_silent == 0)
10613 /* Clear the whole command line. Will reset "clear_cmdline". */
10614 msg_clr_cmdline();
10615
10616#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 /* In Visual mode the size of the selected area must be redrawn. */
10618 if (VIsual_active)
10619 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620
10621 /* If the last window has no status line, the ruler is after the mode
10622 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010623 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010624 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625#endif
10626 redraw_cmdline = FALSE;
10627 clear_cmdline = FALSE;
10628
10629 return length;
10630}
10631
10632/*
10633 * Position for a mode message.
10634 */
10635 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010636msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
10638 msg_col = 0;
10639 msg_row = Rows - 1;
10640}
10641
10642/*
10643 * Delete mode message. Used when ESC is typed which is expected to end
10644 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010645 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 */
10647 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010648unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
10650 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010651 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 */
10653 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10654 redraw_cmdline = TRUE; /* delete mode later */
10655 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010656 clearmode();
10657}
10658
10659/*
10660 * Clear the mode message.
10661 */
10662 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010663clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010664{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010665 int save_msg_row = msg_row;
10666 int save_msg_col = msg_col;
10667
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010668 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010669 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010670 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010671 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010672
10673 msg_col = save_msg_col;
10674 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675}
10676
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010677 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010678recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010679{
10680 MSG_PUTS_ATTR(_("recording"), attr);
10681 if (!shortmess(SHM_RECORDING))
10682 {
10683 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010684 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010685 MSG_PUTS_ATTR(s, attr);
10686 }
10687}
10688
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010689/*
10690 * Draw the tab pages line at the top of the Vim window.
10691 */
10692 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010693draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010694{
10695 int tabcount = 0;
10696 tabpage_T *tp;
10697 int tabwidth;
10698 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010699 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010700 int attr;
10701 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010702 win_T *cwp;
10703 int wincount;
10704 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010705 int c;
10706 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010707 int attr_sel = HL_ATTR(HLF_TPS);
10708 int attr_nosel = HL_ATTR(HLF_TP);
10709 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010710 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010711 int room;
10712 int use_sep_chars = (t_colors < 8
10713#ifdef FEAT_GUI
10714 && !gui.in_use
10715#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010716#ifdef FEAT_TERMGUICOLORS
10717 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010718#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010719 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010720
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010721 if (ScreenLines == NULL)
10722 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010723 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010724
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010725#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010726 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010727 if (gui_use_tabline())
10728 {
10729 gui_update_tabline();
10730 return;
10731 }
10732#endif
10733
10734 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010735 return;
10736
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010737#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010738
10739 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10740 for (scol = 0; scol < Columns; ++scol)
10741 TabPageIdxs[scol] = 0;
10742
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010743 /* Use the 'tabline' option if it's set. */
10744 if (*p_tal != NUL)
10745 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010746 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010747
10748 /* Check for an error. If there is one we would loop in redrawing the
10749 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010750 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010751 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010752 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010753 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010754 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010755 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010756 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010757 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010758#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010759 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010760 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010761 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010762
Bram Moolenaar238a5642006-02-21 22:12:05 +000010763 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10764 if (tabwidth < 6)
10765 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010766
Bram Moolenaar238a5642006-02-21 22:12:05 +000010767 attr = attr_nosel;
10768 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010769 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010770 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10771 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010772 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010773 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010774
Bram Moolenaar238a5642006-02-21 22:12:05 +000010775 if (tp->tp_topframe == topframe)
10776 attr = attr_sel;
10777 if (use_sep_chars && col > 0)
10778 screen_putchar('|', 0, col++, attr);
10779
10780 if (tp->tp_topframe != topframe)
10781 attr = attr_nosel;
10782
10783 screen_putchar(' ', 0, col++, attr);
10784
10785 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010786 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010787 cwp = curwin;
10788 wp = firstwin;
10789 }
10790 else
10791 {
10792 cwp = tp->tp_curwin;
10793 wp = tp->tp_firstwin;
10794 }
10795
10796 modified = FALSE;
10797 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10798 if (bufIsChanged(wp->w_buffer))
10799 modified = TRUE;
10800 if (modified || wincount > 1)
10801 {
10802 if (wincount > 1)
10803 {
10804 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010805 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010806 if (col + len >= Columns - 3)
10807 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010808 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010809#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010810 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010811#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010812 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010813#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010814 );
10815 col += len;
10816 }
10817 if (modified)
10818 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10819 screen_putchar(' ', 0, col++, attr);
10820 }
10821
10822 room = scol - col + tabwidth - 1;
10823 if (room > 0)
10824 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010825 /* Get buffer name in NameBuff[] */
10826 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010827 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010828 len = vim_strsize(NameBuff);
10829 p = NameBuff;
10830#ifdef FEAT_MBYTE
10831 if (has_mbyte)
10832 while (len > room)
10833 {
10834 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010835 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010836 }
10837 else
10838#endif
10839 if (len > room)
10840 {
10841 p += len - room;
10842 len = room;
10843 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010844 if (len > Columns - col - 1)
10845 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010846
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010847 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010848 col += len;
10849 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010850 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010851
10852 /* Store the tab page number in TabPageIdxs[], so that
10853 * jump_to_mouse() knows where each one is. */
10854 ++tabcount;
10855 while (scol < col)
10856 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010857 }
10858
Bram Moolenaar238a5642006-02-21 22:12:05 +000010859 if (use_sep_chars)
10860 c = '_';
10861 else
10862 c = ' ';
10863 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010864
10865 /* Put an "X" for closing the current tab if there are several. */
10866 if (first_tabpage->tp_next != NULL)
10867 {
10868 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10869 TabPageIdxs[Columns - 1] = -999;
10870 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010871 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010872
10873 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10874 * set. */
10875 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010876}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010877
10878/*
10879 * Get buffer name for "buf" into NameBuff[].
10880 * Takes care of special buffer names and translates special characters.
10881 */
10882 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010883get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010884{
10885 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010886 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010887 else
10888 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10889 trans_characters(NameBuff, MAXPATHL);
10890}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010891
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892/*
10893 * Get the character to use in a status line. Get its attributes in "*attr".
10894 */
10895 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010896fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897{
10898 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010899
10900#ifdef FEAT_TERMINAL
10901 if (bt_terminal(wp->w_buffer))
10902 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010903 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010904 {
10905 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010906 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010907 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010908 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010909 {
10910 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010911 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010912 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010913 }
10914 else
10915#endif
10916 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010918 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 fill = fill_stl;
10920 }
10921 else
10922 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010923 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 fill = fill_stlnc;
10925 }
10926 /* Use fill when there is highlighting, and highlighting of current
10927 * window differs, or the fillchars differ, or this is not the
10928 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010929 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010930 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 || (fill_stl != fill_stlnc)))
10932 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010933 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 return '^';
10935 return '=';
10936}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938/*
10939 * Get the character to use in a separator between vertically split windows.
10940 * Get its attributes in "*attr".
10941 */
10942 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010943fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010945 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 if (*attr == 0 && fill_vert == ' ')
10947 return '|';
10948 else
10949 return fill_vert;
10950}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951
10952/*
10953 * Return TRUE if redrawing should currently be done.
10954 */
10955 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010956redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010958#ifdef FEAT_EVAL
10959 if (disable_redraw_for_testing)
10960 return 0;
10961 else
10962#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010963 return ((!RedrawingDisabled
10964#ifdef FEAT_EVAL
10965 || ignore_redraw_flag_for_testing
10966#endif
10967 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968}
10969
10970/*
10971 * Return TRUE if printing messages should currently be done.
10972 */
10973 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010974messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975{
10976 return (!(p_lz && char_avail() && !KeyTyped));
10977}
10978
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010979#ifdef FEAT_MENU
10980/*
10981 * Draw the window toolbar.
10982 */
10983 static void
10984redraw_win_toolbar(win_T *wp)
10985{
10986 vimmenu_T *menu;
10987 int item_idx = 0;
10988 int item_count = 0;
10989 int col = 0;
10990 int next_col;
10991 int off = (int)(current_ScreenLine - ScreenLines);
10992 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10993 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10994
10995 vim_free(wp->w_winbar_items);
10996 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10997 ++item_count;
10998 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10999 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
11000
11001 /* TODO: use fewer spaces if there is not enough room */
11002 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011003 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011004 {
11005 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011006 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011007 break;
11008 if (col > 1)
11009 {
11010 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011011 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011012 break;
11013 }
11014
11015 wp->w_winbar_items[item_idx].wb_startcol = col;
11016 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011017 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011018 break;
11019
11020 next_col = text_to_screenline(wp, menu->name, col);
11021 while (col < next_col)
11022 {
11023 ScreenAttrs[off + col] = button_attr;
11024 ++col;
11025 }
11026 wp->w_winbar_items[item_idx].wb_endcol = col;
11027 wp->w_winbar_items[item_idx].wb_menu = menu;
11028 ++item_idx;
11029
Bram Moolenaar02631462017-09-22 15:20:32 +020011030 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011031 break;
11032 space_to_screenline(off + col, button_attr);
11033 ++col;
11034 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011035 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011036 {
11037 space_to_screenline(off + col, fill_attr);
11038 ++col;
11039 }
11040 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11041
Bram Moolenaar02631462017-09-22 15:20:32 +020011042 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11043 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011044}
11045#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011046
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047/*
11048 * Show current status info in ruler and various other places
11049 * If always is FALSE, only show ruler if position has changed.
11050 */
11051 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011052showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053{
11054 if (!always && !redrawing())
11055 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011056#ifdef FEAT_INS_EXPAND
11057 if (pum_visible())
11058 {
11059 /* Don't redraw right now, do it later. */
11060 curwin->w_redr_status = TRUE;
11061 return;
11062 }
11063#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011064#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011065 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011066 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011067 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 else
11070#endif
11071#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011072 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073#endif
11074
11075#ifdef FEAT_TITLE
11076 if (need_maketitle
11077# ifdef FEAT_STL_OPT
11078 || (p_icon && (stl_syntax & STL_IN_ICON))
11079 || (p_title && (stl_syntax & STL_IN_TITLE))
11080# endif
11081 )
11082 maketitle();
11083#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011084 /* Redraw the tab pages line if needed. */
11085 if (redraw_tabline)
11086 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087}
11088
11089#ifdef FEAT_CMDL_INFO
11090 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011091win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011093#define RULER_BUF_LEN 70
11094 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 int row;
11096 int fillchar;
11097 int attr;
11098 int empty_line = FALSE;
11099 colnr_T virtcol;
11100 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011101 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 int this_ru_col;
11104 int off = 0;
11105 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106
11107 /* If 'ruler' off or redrawing disabled, don't do anything */
11108 if (!p_ru)
11109 return;
11110
11111 /*
11112 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11113 * after deleting lines, before cursor.lnum is corrected.
11114 */
11115 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11116 return;
11117
11118#ifdef FEAT_INS_EXPAND
11119 /* Don't draw the ruler while doing insert-completion, it might overwrite
11120 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 if (edit_submode != NULL)
11123 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011124 // Don't draw the ruler when the popup menu is visible, it may overlap.
11125 // Except when the popup menu will be redrawn anyway.
11126 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011127 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128#endif
11129
11130#ifdef FEAT_STL_OPT
11131 if (*p_ruf)
11132 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011133 int save_called_emsg = called_emsg;
11134
11135 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011137 if (called_emsg)
11138 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011139 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011140 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 return;
11142 }
11143#endif
11144
11145 /*
11146 * Check if not in Insert mode and the line is empty (will show "0-1").
11147 */
11148 if (!(State & INSERT)
11149 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11150 empty_line = TRUE;
11151
11152 /*
11153 * Only draw the ruler when something changed.
11154 */
11155 validate_virtcol_win(wp);
11156 if ( redraw_cmdline
11157 || always
11158 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11159 || wp->w_cursor.col != wp->w_ru_cursor.col
11160 || wp->w_virtcol != wp->w_ru_virtcol
11161#ifdef FEAT_VIRTUALEDIT
11162 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11163#endif
11164 || wp->w_topline != wp->w_ru_topline
11165 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11166#ifdef FEAT_DIFF
11167 || wp->w_topfill != wp->w_ru_topfill
11168#endif
11169 || empty_line != wp->w_ru_empty)
11170 {
11171 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 if (wp->w_status_height)
11173 {
11174 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011175 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011176 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011177 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 }
11179 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 {
11181 row = Rows - 1;
11182 fillchar = ' ';
11183 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 width = Columns;
11185 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 }
11187
11188 /* In list mode virtcol needs to be recomputed */
11189 virtcol = wp->w_virtcol;
11190 if (wp->w_p_list && lcs_tab1 == NUL)
11191 {
11192 wp->w_p_list = FALSE;
11193 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11194 wp->w_p_list = TRUE;
11195 }
11196
11197 /*
11198 * Some sprintfs return the length, some return a pointer.
11199 * To avoid portability problems we use strlen() here.
11200 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011201 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11203 ? 0L
11204 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011205 len = STRLEN(buffer);
11206 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11208 (int)virtcol + 1);
11209
11210 /*
11211 * Add a "50%" if there is room for it.
11212 * On the last line, don't print in the last column (scrolls the
11213 * screen up on some terminals).
11214 */
11215 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011216 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 this_ru_col = ru_col - (Columns - width);
11221 if (this_ru_col < 0)
11222 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 /* Never use more than half the window/screen width, leave the other
11224 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011225 if (this_ru_col < (width + 1) / 2)
11226 this_ru_col = (width + 1) / 2;
11227 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011229 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011230 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 {
11232#ifdef FEAT_MBYTE
11233 if (has_mbyte)
11234 i += (*mb_char2bytes)(fillchar, buffer + i);
11235 else
11236#endif
11237 buffer[i++] = fillchar;
11238 ++o;
11239 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011240 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 }
11242 /* Truncate at window boundary. */
11243#ifdef FEAT_MBYTE
11244 if (has_mbyte)
11245 {
11246 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011247 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 {
11249 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011250 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 {
11252 buffer[i] = NUL;
11253 break;
11254 }
11255 }
11256 }
11257 else
11258#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011259 if (this_ru_col + (int)STRLEN(buffer) > width)
11260 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261
Bram Moolenaar4033c552017-09-16 20:54:51 +020011262 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 i = redraw_cmdline;
11264 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011265 this_ru_col + off + (int)STRLEN(buffer),
11266 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 fillchar, fillchar, attr);
11268 /* don't redraw the cmdline because of showing the ruler */
11269 redraw_cmdline = i;
11270 wp->w_ru_cursor = wp->w_cursor;
11271 wp->w_ru_virtcol = wp->w_virtcol;
11272 wp->w_ru_empty = empty_line;
11273 wp->w_ru_topline = wp->w_topline;
11274 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11275#ifdef FEAT_DIFF
11276 wp->w_ru_topfill = wp->w_topfill;
11277#endif
11278 }
11279}
11280#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011281
11282#if defined(FEAT_LINEBREAK) || defined(PROTO)
11283/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011284 * Return the width of the 'number' and 'relativenumber' column.
11285 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011286 * Otherwise it depends on 'numberwidth' and the line count.
11287 */
11288 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011289number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011290{
11291 int n;
11292 linenr_T lnum;
11293
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011294 if (wp->w_p_rnu && !wp->w_p_nu)
11295 /* cursor line shows "0" */
11296 lnum = wp->w_height;
11297 else
11298 /* cursor line shows absolute line number */
11299 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011300
Bram Moolenaar6b314672015-03-20 15:42:10 +010011301 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011302 return wp->w_nrwidth_width;
11303 wp->w_nrwidth_line_count = lnum;
11304
11305 n = 0;
11306 do
11307 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011308 lnum /= 10;
11309 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011310 } while (lnum > 0);
11311
11312 /* 'numberwidth' gives the minimal width plus one */
11313 if (n < wp->w_p_nuw - 1)
11314 n = wp->w_p_nuw - 1;
11315
11316 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011317 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011318 return n;
11319}
11320#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011321
11322/*
11323 * Return the current cursor column. This is the actual position on the
11324 * screen. First column is 0.
11325 */
11326 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011327screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011328{
11329 return screen_cur_col;
11330}
11331
11332/*
11333 * Return the current cursor row. This is the actual position on the screen.
11334 * First row is 0.
11335 */
11336 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011337screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011338{
11339 return screen_cur_row;
11340}