blob: 4a61807c92a91570d6828d50efd77814de187c97 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200153static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void win_rest_invalid(win_T *wp);
155static void msg_pos_mode(void);
156static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Redraw the current window later, with update_screen(type).
186 * Set must_redraw only if not already set to a higher value.
187 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
188 */
189 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100190redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
192 redraw_win_later(curwin, type);
193}
194
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_win_later(
197 win_T *wp,
198 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200200 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {
202 wp->w_redr_type = type;
203 if (type >= NOT_VALID)
204 wp->w_lines_valid = 0;
205 if (must_redraw < type) /* must_redraw is the maximum of all windows */
206 must_redraw = type;
207 }
208}
209
210/*
211 * Force a complete redraw later. Also resets the highlighting. To be used
212 * after executing a shell command that messes up the screen.
213 */
214 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100215redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
217 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000218#ifdef FEAT_GUI
219 if (gui.in_use)
220 /* Use a code that will reset gui.highlight_mask in
221 * gui_stop_highlight(). */
222 screen_attr = HL_ALL + 1;
223 else
224#endif
225 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200226 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227}
228
229/*
230 * Mark all windows to be redrawn later.
231 */
232 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100233redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100247redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 redraw_buf_later(curbuf, type);
250}
251
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 win_T *wp;
256
257 FOR_ALL_WINDOWS(wp)
258 {
259 if (wp->w_buffer == buf)
260 redraw_win_later(wp, type);
261 }
262}
263
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200264 void
265redraw_buf_and_status_later(buf_T *buf, int type)
266{
267 win_T *wp;
268
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200269#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200270 if (wild_menu_showing != 0)
271 /* Don't redraw while the command line completion is displayed, it
272 * would disappear. */
273 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200274#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200275 FOR_ALL_WINDOWS(wp)
276 {
277 if (wp->w_buffer == buf)
278 {
279 redraw_win_later(wp, type);
280 wp->w_redr_status = TRUE;
281 }
282 }
283}
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200286 * Redraw as soon as possible. When the command line is not scrolled redraw
287 * right away and restore what was on the command line.
288 * Return a code indicating what happened.
289 */
290 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100291redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292{
293 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 int r;
296 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200297 schar_T *screenline; /* copy from ScreenLines[] */
298 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299#ifdef FEAT_MBYTE
300 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200301 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304#endif
305
306 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200307 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 return ret;
309
310 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 if (screenline == NULL || screenattr == NULL)
317 ret = 2;
318#ifdef FEAT_MBYTE
319 if (enc_utf8)
320 {
321 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 if (screenlineUC == NULL)
324 ret = 2;
325 for (i = 0; i < p_mco; ++i)
326 {
327 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineC[i] == NULL)
330 ret = 2;
331 }
332 }
333 if (enc_dbcs == DBCS_JPNU)
334 {
335 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenline2 == NULL)
338 ret = 2;
339 }
340#endif
341
342 if (ret != 2)
343 {
344 /* Save the text displayed in the command line area. */
345 for (r = 0; r < rows; ++r)
346 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200348 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (size_t)cols * sizeof(schar_T));
350 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200352 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353#ifdef FEAT_MBYTE
354 if (enc_utf8)
355 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenlineC[i] + r * cols,
361 ScreenLinesC[i] + LineOffset[cmdline_row + r],
362 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 }
364 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368#endif
369 }
370
371 update_screen(0);
372 ret = 3;
373
374 if (must_redraw == 0)
375 {
376 int off = (int)(current_ScreenLine - ScreenLines);
377
378 /* Restore the text displayed in the command line area. */
379 for (r = 0; r < rows; ++r)
380 {
381 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenline + r * cols,
383 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200385 screenattr + r * cols,
386 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387#ifdef FEAT_MBYTE
388 if (enc_utf8)
389 {
390 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenlineUC + r * cols,
392 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 for (i = 0; i < p_mco; ++i)
394 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenlineC[i] + r * cols,
396 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 }
398 if (enc_dbcs == DBCS_JPNU)
399 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenline2 + r * cols,
401 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200403 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 ret = 4;
406 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408
409 vim_free(screenline);
410 vim_free(screenattr);
411#ifdef FEAT_MBYTE
412 if (enc_utf8)
413 {
414 vim_free(screenlineUC);
415 for (i = 0; i < p_mco; ++i)
416 vim_free(screenlineC[i]);
417 }
418 if (enc_dbcs == DBCS_JPNU)
419 vim_free(screenline2);
420#endif
421
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200422 /* Show the intro message when appropriate. */
423 maybe_intro_message();
424
425 setcursor();
426
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427 return ret;
428}
429
430/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100431 * Invoked after an asynchronous callback is called.
432 * If an echo command was used the cursor needs to be put back where
433 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200434 * If "call_update_screen" is FALSE don't call update_screen() when at the
435 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100436 */
437 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200438redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200440 ++redrawing_for_callback;
441
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100442 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200443 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100444 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200445 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200446 // Don't redraw when in prompt_for_number().
447 if (cmdline_row > 0)
448 {
449 // Redrawing only works when the screen didn't scroll. Don't clear
450 // wildmenu entries.
451 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200452#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 && call_update_screen)
456 update_screen(0);
457
458 // Redraw in the same position, so that the user can continue
459 // editing the command.
460 redrawcmdline_ex(FALSE);
461 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200463 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100464 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200465 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100467 setcursor();
468 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100469 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100471 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // Don't update the cursor when it is blinking and off to avoid
473 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100474 out_flush_cursor(FALSE, FALSE);
475 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100477 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200478
479 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100480}
481
482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 * Changed something in the current window, at buffer line "lnum", that
484 * requires that line and possibly other lines to be redrawn.
485 * Used when entering/leaving Insert mode with the cursor on a folded line.
486 * Used to remove the "$" from a change command.
487 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
488 * may become invalid and the whole window will have to be redrawn.
489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100491redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200492 win_T *wp,
Bram Moolenaar05540972016-01-30 20:31:25 +0100493 linenr_T lnum,
494 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496#ifdef FEAT_FOLDING
497 int i;
498#endif
499
Bram Moolenaar90a99792018-09-12 21:52:18 +0200500 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
501 wp->w_redraw_top = lnum;
502 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
503 wp->w_redraw_bot = lnum;
504 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
506#ifdef FEAT_FOLDING
507 if (invalid)
508 {
509 /* A w_lines[] entry for this lnum has become invalid. */
Bram Moolenaar90a99792018-09-12 21:52:18 +0200510 i = find_wl_entry(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (i >= 0)
Bram Moolenaar90a99792018-09-12 21:52:18 +0200512 wp->w_lines[i].wl_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 }
514#endif
515}
516
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200517 void
518reset_updating_screen(int may_resize_shell UNUSED)
519{
520 updating_screen = FALSE;
521#ifdef FEAT_GUI
522 if (may_resize_shell)
523 gui_may_resize_shell();
524#endif
525#ifdef FEAT_TERMINAL
526 term_check_channel_closed_recently();
527#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200528
529#ifdef HAVE_DROP_FILE
530 // If handle_drop() was called while updating_screen was TRUE need to
531 // handle the drop now.
532 handle_any_postponed_drop();
533#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200534}
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200537 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 */
539 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100540update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 redraw_curbuf_later(type);
543 update_screen(type);
544}
545
546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * Based on the current value of curwin->w_topline, transfer a screenfull
548 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200549 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200551 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 win_T *wp;
556 static int did_intro = FALSE;
557#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
558 int did_one;
559#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200561 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200562 int gui_cursor_col;
563 int gui_cursor_row;
564#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200565 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000567 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200571 if (type == VALID_NO_UPDATE)
572 {
573 no_update = TRUE;
574 type = 0;
575 }
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (must_redraw)
578 {
579 if (type < must_redraw) /* use maximal type */
580 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000581
582 /* must_redraw is reset here, so that when we run into some weird
583 * reason to redraw while busy redrawing (e.g., asynchronous
584 * scrolling), or update_topline() in win_update() will cause a
585 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 must_redraw = 0;
587 }
588
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200589 /* May need to update w_lines[]. */
590 if (curwin->w_lines_valid == 0 && type < NOT_VALID
591#ifdef FEAT_TERMINAL
592 && !term_do_update_window(curwin)
593#endif
594 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 type = NOT_VALID;
596
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000597 /* Postpone the redrawing when it's not needed and when being called
598 * recursively. */
599 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {
601 redraw_later(type); /* remember type for next time */
602 must_redraw = type;
603 if (type > INVERTED_ALL)
604 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
607
608 updating_screen = TRUE;
609#ifdef FEAT_SYN_HL
610 ++display_tick; /* let syntax code know we're in a next round of
611 * display updating */
612#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200613 if (no_update)
614 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 /*
617 * if the screen was scrolled up when displaying a message, scroll it down
618 */
619 if (msg_scrolled)
620 {
621 clear_cmdline = TRUE;
622 if (msg_scrolled > Rows - 5) /* clearing is faster */
623 type = CLEAR;
624 else if (type != CLEAR)
625 {
626 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200627 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
628 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 type = CLEAR;
630 FOR_ALL_WINDOWS(wp)
631 {
632 if (W_WINROW(wp) < msg_scrolled)
633 {
634 if (W_WINROW(wp) + wp->w_height > msg_scrolled
635 && wp->w_redr_type < REDRAW_TOP
636 && wp->w_lines_valid > 0
637 && wp->w_topline == wp->w_lines[0].wl_lnum)
638 {
639 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
640 wp->w_redr_type = REDRAW_TOP;
641 }
642 else
643 {
644 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200645 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
646 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
649 }
650 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200651 if (!no_update)
652 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000653 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 msg_scrolled = 0;
656 need_wait_return = FALSE;
657 }
658
659 /* reset cmdline_row now (may have been changed temporarily) */
660 compute_cmdrow();
661
662 /* Check for changed highlighting */
663 if (need_highlight_changed)
664 highlight_changed();
665
666 if (type == CLEAR) /* first clear screen */
667 {
668 screenclear(); /* will reset clear_cmdline */
669 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200670 /* must_redraw may be set indirectly, avoid another redraw later */
671 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673
674 if (clear_cmdline) /* going to clear cmdline (done below) */
675 check_for_delay(FALSE);
676
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000677#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200678 /* Force redraw when width of 'number' or 'relativenumber' column
679 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000680 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200681 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
682 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000683 curwin->w_redr_type = NOT_VALID;
684#endif
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 /*
687 * Only start redrawing if there is really something to do.
688 */
689 if (type == INVERTED)
690 update_curswant();
691 if (curwin->w_redr_type < type
692 && !((type == VALID
693 && curwin->w_lines[0].wl_valid
694#ifdef FEAT_DIFF
695 && curwin->w_topfill == curwin->w_old_topfill
696 && curwin->w_botfill == curwin->w_old_botfill
697#endif
698 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000700 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
702 && curwin->w_old_visual_mode == VIsual_mode
703 && (curwin->w_valid & VALID_VIRTCOL)
704 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ))
706 curwin->w_redr_type = type;
707
Bram Moolenaar5a305422006-04-28 22:38:25 +0000708 /* Redraw the tab pages line if needed. */
709 if (redraw_tabline || type >= NOT_VALID)
710 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000711
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifdef FEAT_SYN_HL
713 /*
714 * Correct stored syntax highlighting info for changes in each displayed
715 * buffer. Each buffer must only be done once.
716 */
717 FOR_ALL_WINDOWS(wp)
718 {
719 if (wp->w_buffer->b_mod_set)
720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 win_T *wwp;
722
723 /* Check if we already did this buffer. */
724 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
725 if (wwp->w_buffer == wp->w_buffer)
726 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200727 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 syn_stack_apply_changes(wp->w_buffer);
729 }
730 }
731#endif
732
733 /*
734 * Go from top to bottom through the windows, redrawing the ones that need
735 * it.
736 */
737#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
738 did_one = FALSE;
739#endif
740#ifdef FEAT_SEARCH_EXTRA
741 search_hl.rm.regprog = NULL;
742#endif
743 FOR_ALL_WINDOWS(wp)
744 {
745 if (wp->w_redr_type != 0)
746 {
747 cursor_off();
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 if (!did_one)
750 {
751 did_one = TRUE;
752# ifdef FEAT_SEARCH_EXTRA
753 start_search_hl();
754# endif
755# ifdef FEAT_CLIPBOARD
756 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200757 if (clip_star.available && clip_isautosel_star())
758 clip_update_selection(&clip_star);
759 if (clip_plus.available && clip_isautosel_plus())
760 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761# endif
762#ifdef FEAT_GUI
763 /* Remove the cursor before starting to do anything, because
764 * scrolling may make it difficult to redraw the text under
765 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200766 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 {
768 gui_cursor_col = gui.cursor_col;
769 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200771 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
774 }
775#endif
776 win_update(wp);
777 }
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 /* redraw status line after the window to minimize cursor movement */
780 if (wp->w_redr_status)
781 {
782 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200783 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786#if defined(FEAT_SEARCH_EXTRA)
787 end_search_hl();
788#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100789#ifdef FEAT_INS_EXPAND
790 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200791 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 /* Reset b_mod_set flags. Going through all windows is probably faster
795 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200796 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200799 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /* Clear or redraw the command line. Done last, because scrolling may
802 * mess up the command line. */
803 if (clear_cmdline || redraw_cmdline)
804 showmode();
805
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200806 if (no_update)
807 --no_win_do_lines_ins;
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200810 if (!did_intro)
811 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 did_intro = TRUE;
813
814#ifdef FEAT_GUI
815 /* Redraw the cursor and update the scrollbars when all screen updating is
816 * done. */
817 if (gui.in_use)
818 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200819 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100821 mch_disable_flush();
822 out_flush(); /* required before updating the cursor */
823 mch_enable_flush();
824
Bram Moolenaar144445d2016-07-08 21:41:54 +0200825 /* Put the GUI position where the cursor was, gui_update_cursor()
826 * uses that. */
827 gui.col = gui_cursor_col;
828 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200829# ifdef FEAT_MBYTE
830 gui.col = mb_fix_col(gui.col, gui.row);
831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100833 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200834 screen_cur_col = gui.col;
835 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 else
838 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_scrollbars(FALSE);
840 }
841#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200842 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100845#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
846/*
847 * Prepare for updating one or more windows.
848 * Caller must check for "updating_screen" already set to avoid recursiveness.
849 */
850 static void
851update_prepare(void)
852{
853 cursor_off();
854 updating_screen = TRUE;
855#ifdef FEAT_GUI
856 /* Remove the cursor before starting to do anything, because scrolling may
857 * make it difficult to redraw the text under it. */
858 if (gui.in_use)
859 gui_undraw_cursor();
860#endif
861#ifdef FEAT_SEARCH_EXTRA
862 start_search_hl();
863#endif
864}
865
866/*
867 * Finish updating one or more windows.
868 */
869 static void
870update_finish(void)
871{
872 if (redraw_cmdline)
873 showmode();
874
875# ifdef FEAT_SEARCH_EXTRA
876 end_search_hl();
877# endif
878
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200879 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880
881# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100882 /* Redraw the cursor and update the scrollbars when all screen updating is
883 * done. */
884 if (gui.in_use)
885 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100886 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887 gui_update_scrollbars(FALSE);
888 }
889# endif
890}
891#endif
892
Bram Moolenaar860cae12010-06-05 23:22:07 +0200893#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200894/*
895 * Return TRUE if the cursor line in window "wp" may be concealed, according
896 * to the 'concealcursor' option.
897 */
898 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100899conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900{
901 int c;
902
903 if (*wp->w_p_cocu == NUL)
904 return FALSE;
905 if (get_real_state() & VISUAL)
906 c = 'v';
907 else if (State & INSERT)
908 c = 'i';
909 else if (State & NORMAL)
910 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200911 else if (State & CMDLINE)
912 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913 else
914 return FALSE;
915 return vim_strchr(wp->w_p_cocu, c) != NULL;
916}
917
918/*
919 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
920 */
921 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200922conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200923{
924 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
925 {
926 need_cursor_line_redraw = TRUE;
927 /* Need to recompute cursor column, e.g., when starting Visual mode
928 * without concealing. */
929 curs_columns(TRUE);
930 }
931}
932
Bram Moolenaar860cae12010-06-05 23:22:07 +0200933 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935{
936 int row;
937 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200938#ifdef SYN_TIME_LIMIT
939 proftime_T syntax_tm;
940#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941
Bram Moolenaar908be432016-05-24 10:51:30 +0200942 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100943 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200944 return;
945
Bram Moolenaar860cae12010-06-05 23:22:07 +0200946 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200947 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200949#ifdef SYN_TIME_LIMIT
950 /* Set the time limit to 'redrawtime'. */
951 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200952 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200953#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100954 update_prepare();
955
Bram Moolenaar860cae12010-06-05 23:22:07 +0200956 row = 0;
957 for (j = 0; j < wp->w_lines_valid; ++j)
958 {
959 if (lnum == wp->w_lines[j].wl_lnum)
960 {
961 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200962# ifdef FEAT_SEARCH_EXTRA
963 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200964 start_search_hl();
965 prepare_search_hl(wp, lnum);
966# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200967 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
968 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200969# if defined(FEAT_SEARCH_EXTRA)
970 end_search_hl();
971# endif
972 break;
973 }
974 row += wp->w_lines[j].wl_size;
975 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100976
977 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200978
979#ifdef SYN_TIME_LIMIT
980 syn_set_timeout(NULL);
981#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200982 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200983 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984}
985#endif
986
987#if defined(FEAT_SIGNS) || defined(PROTO)
988 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100989update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 win_T *wp;
992 int doit = FALSE;
993
994# ifdef FEAT_FOLDING
995 win_foldinfo.fi_level = 0;
996# endif
997
998 /* update/delete a specific mark */
999 FOR_ALL_WINDOWS(wp)
1000 {
1001 if (buf != NULL && lnum > 0)
1002 {
1003 if (wp->w_buffer == buf && lnum >= wp->w_topline
1004 && lnum < wp->w_botline)
1005 {
1006 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1007 wp->w_redraw_top = lnum;
1008 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1009 wp->w_redraw_bot = lnum;
1010 redraw_win_later(wp, VALID);
1011 }
1012 }
1013 else
1014 redraw_win_later(wp, VALID);
1015 if (wp->w_redr_type != 0)
1016 doit = TRUE;
1017 }
1018
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001019 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001020 * happening (recursive call), messages on the screen or still starting up.
1021 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001022 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001023 || State == ASKMORE || State == HITRETURN
1024 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001025#ifdef FEAT_GUI
1026 || gui.starting
1027#endif
1028 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 return;
1030
1031 /* update all windows that need updating */
1032 update_prepare();
1033
Bram Moolenaar29323592016-07-24 22:04:11 +02001034 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 if (wp->w_redr_type != 0)
1037 win_update(wp);
1038 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001039 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 update_finish();
1043}
1044#endif
1045
1046
1047#if defined(FEAT_GUI) || defined(PROTO)
1048/*
1049 * Update a single window, its status line and maybe the command line msg.
1050 * Used for the GUI scrollbar.
1051 */
1052 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001053updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001055 /* return if already busy updating */
1056 if (updating_screen)
1057 return;
1058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 update_prepare();
1060
1061#ifdef FEAT_CLIPBOARD
1062 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001063 if (clip_star.available && clip_isautosel_star())
1064 clip_update_selection(&clip_star);
1065 if (clip_plus.available && clip_isautosel_plus())
1066 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001070
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001071 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001072 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001073 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (wp->w_redr_status
1076# ifdef FEAT_CMDL_INFO
1077 || p_ru
1078# endif
1079# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001080 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081# endif
1082 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001083 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
1085 update_finish();
1086}
1087#endif
1088
1089/*
1090 * Update a single window.
1091 *
1092 * This may cause the windows below it also to be redrawn (when clearing the
1093 * screen or scrolling lines).
1094 *
1095 * How the window is redrawn depends on wp->w_redr_type. Each type also
1096 * implies the one below it.
1097 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001098 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1100 * INVERTED redraw the changed part of the Visual area
1101 * INVERTED_ALL redraw the whole Visual area
1102 * VALID 1. scroll up/down to adjust for a changed w_topline
1103 * 2. update lines at the top when scrolled down
1104 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001105 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * b_mod_top and b_mod_bot.
1107 * - if wp->w_redraw_top non-zero, redraw lines between
1108 * wp->w_redraw_top and wp->w_redr_bot.
1109 * - continue redrawing when syntax status is invalid.
1110 * 4. if scrolled up, update lines at the bottom.
1111 * This results in three areas that may need updating:
1112 * top: from first row to top_end (when scrolled down)
1113 * mid: from mid_start to mid_end (update inversion or changed text)
1114 * bot: from bot_start to last row (when scrolled up)
1115 */
1116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001117win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 buf_T *buf = wp->w_buffer;
1120 int type;
1121 int top_end = 0; /* Below last row of the top area that needs
1122 updating. 0 when no top area updating. */
1123 int mid_start = 999;/* first row of the mid area that needs
1124 updating. 999 when no mid area updating. */
1125 int mid_end = 0; /* Below last row of the mid area that needs
1126 updating. 0 when no mid area updating. */
1127 int bot_start = 999;/* first row of the bot area that needs
1128 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int scrolled_down = FALSE; /* TRUE when scrolled down when
1130 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001132 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 int top_to_mod = FALSE; /* redraw above mod_top */
1134#endif
1135
1136 int row; /* current window row to display */
1137 linenr_T lnum; /* current buffer lnum to display */
1138 int idx; /* current index in w_lines[] */
1139 int srow; /* starting row of the current line */
1140
1141 int eof = FALSE; /* if TRUE, we hit the end of the file */
1142 int didline = FALSE; /* if TRUE, we finished the last line */
1143 int i;
1144 long j;
1145 static int recursive = FALSE; /* being called recursively */
1146 int old_botline = wp->w_botline;
1147#ifdef FEAT_FOLDING
1148 long fold_count;
1149#endif
1150#ifdef FEAT_SYN_HL
1151 /* remember what happened to the previous line, to know if
1152 * check_visual_highlight() can be used */
1153#define DID_NONE 1 /* didn't update a line */
1154#define DID_LINE 2 /* updated a normal line */
1155#define DID_FOLD 3 /* updated a folded line */
1156 int did_update = DID_NONE;
1157 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1158#endif
1159 linenr_T mod_top = 0;
1160 linenr_T mod_bot = 0;
1161#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1162 int save_got_int;
1163#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001164#ifdef SYN_TIME_LIMIT
1165 proftime_T syntax_tm;
1166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 type = wp->w_redr_type;
1169
1170 if (type == NOT_VALID)
1171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 wp->w_lines_valid = 0;
1174 }
1175
1176 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001177 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 wp->w_redr_type = 0;
1180 return;
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 /* Window is zero-width: Only need to draw the separator. */
1184 if (wp->w_width == 0)
1185 {
1186 /* draw the vertical separator right of this window */
1187 draw_vsep_win(wp, 0);
1188 wp->w_redr_type = 0;
1189 return;
1190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001192#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001193 // If this window contains a terminal, redraw works completely differently.
1194 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001195 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001196 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001197# ifdef FEAT_MENU
1198 /* Draw the window toolbar, if there is one. */
1199 if (winbar_height(wp) > 0)
1200 redraw_win_toolbar(wp);
1201# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001202 wp->w_redr_type = 0;
1203 return;
1204 }
1205#endif
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001208 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#endif
1210
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001211#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001212 /* Force redraw when width of 'number' or 'relativenumber' column
1213 * changes. */
1214 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001215 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001216 {
1217 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001218 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001219 }
1220 else
1221#endif
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1224 {
1225 /*
1226 * When there are both inserted/deleted lines and specific lines to be
1227 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1228 * everything (only happens when redrawing is off for while).
1229 */
1230 type = NOT_VALID;
1231 }
1232 else
1233 {
1234 /*
1235 * Set mod_top to the first line that needs displaying because of
1236 * changes. Set mod_bot to the first line after the changes.
1237 */
1238 mod_top = wp->w_redraw_top;
1239 if (wp->w_redraw_bot != 0)
1240 mod_bot = wp->w_redraw_bot + 1;
1241 else
1242 mod_bot = 0;
1243 wp->w_redraw_top = 0; /* reset for next time */
1244 wp->w_redraw_bot = 0;
1245 if (buf->b_mod_set)
1246 {
1247 if (mod_top == 0 || mod_top > buf->b_mod_top)
1248 {
1249 mod_top = buf->b_mod_top;
1250#ifdef FEAT_SYN_HL
1251 /* Need to redraw lines above the change that may be included
1252 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001253 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (mod_top < 1)
1257 mod_top = 1;
1258 }
1259#endif
1260 }
1261 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1262 mod_bot = buf->b_mod_bot;
1263
1264#ifdef FEAT_SEARCH_EXTRA
1265 /* When 'hlsearch' is on and using a multi-line search pattern, a
1266 * change in one line may make the Search highlighting in a
1267 * previous line invalid. Simple solution: redraw all visible
1268 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001269 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001271 if (search_hl.rm.regprog != NULL
1272 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001274 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001275 {
1276 cur = wp->w_match_head;
1277 while (cur != NULL)
1278 {
1279 if (cur->match.regprog != NULL
1280 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001281 {
1282 top_to_mod = TRUE;
1283 break;
1284 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001285 cur = cur->next;
1286 }
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
1289 }
1290#ifdef FEAT_FOLDING
1291 if (mod_top != 0 && hasAnyFolding(wp))
1292 {
1293 linenr_T lnumt, lnumb;
1294
1295 /*
1296 * A change in a line can cause lines above it to become folded or
1297 * unfolded. Find the top most buffer line that may be affected.
1298 * If the line was previously folded and displayed, get the first
1299 * line of that fold. If the line is folded now, get the first
1300 * folded line. Use the minimum of these two.
1301 */
1302
1303 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1304 * the line below it. If there is no valid entry, use w_topline.
1305 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1306 * to this line. If there is no valid entry, use MAXLNUM. */
1307 lnumt = wp->w_topline;
1308 lnumb = MAXLNUM;
1309 for (i = 0; i < wp->w_lines_valid; ++i)
1310 if (wp->w_lines[i].wl_valid)
1311 {
1312 if (wp->w_lines[i].wl_lastlnum < mod_top)
1313 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1314 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1315 {
1316 lnumb = wp->w_lines[i].wl_lnum;
1317 /* When there is a fold column it might need updating
1318 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001319 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 ++lnumb;
1321 }
1322 }
1323
1324 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1325 if (mod_top > lnumt)
1326 mod_top = lnumt;
1327
1328 /* Now do the same for the bottom line (one above mod_bot). */
1329 --mod_bot;
1330 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1331 ++mod_bot;
1332 if (mod_bot < lnumb)
1333 mod_bot = lnumb;
1334 }
1335#endif
1336
1337 /* When a change starts above w_topline and the end is below
1338 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001339 * If the end of the change is above w_topline: do like no change was
1340 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (mod_top != 0 && mod_top < wp->w_topline)
1342 {
1343 if (mod_bot > wp->w_topline)
1344 mod_top = wp->w_topline;
1345#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001346 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 top_end = 1;
1348#endif
1349 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350
1351 /* When line numbers are displayed need to redraw all lines below
1352 * inserted/deleted lines. */
1353 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1354 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356
1357 /*
1358 * When only displaying the lines at the top, set top_end. Used when
1359 * window has scrolled down for msg_scrolled.
1360 */
1361 if (type == REDRAW_TOP)
1362 {
1363 j = 0;
1364 for (i = 0; i < wp->w_lines_valid; ++i)
1365 {
1366 j += wp->w_lines[i].wl_size;
1367 if (j >= wp->w_upd_rows)
1368 {
1369 top_end = j;
1370 break;
1371 }
1372 }
1373 if (top_end == 0)
1374 /* not found (cannot happen?): redraw everything */
1375 type = NOT_VALID;
1376 else
1377 /* top area defined, the rest is VALID */
1378 type = VALID;
1379 }
1380
Bram Moolenaar367329b2007-08-30 11:53:22 +00001381 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001382 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1383 * non-zero and thus not FALSE) will indicate that screenclear() was not
1384 * called. */
1385 if (screen_cleared)
1386 screen_cleared = MAYBE;
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 /*
1389 * If there are no changes on the screen that require a complete redraw,
1390 * handle three cases:
1391 * 1: we are off the top of the screen by a few lines: scroll down
1392 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1393 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1394 * w_lines[] that needs updating.
1395 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001396 if ((type == VALID || type == SOME_VALID
1397 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_DIFF
1399 && !wp->w_botfill && !wp->w_old_botfill
1400#endif
1401 )
1402 {
1403 if (mod_top != 0 && wp->w_topline == mod_top)
1404 {
1405 /*
1406 * w_topline is the first changed line, the scrolling will be done
1407 * further down.
1408 */
1409 }
1410 else if (wp->w_lines[0].wl_valid
1411 && (wp->w_topline < wp->w_lines[0].wl_lnum
1412#ifdef FEAT_DIFF
1413 || (wp->w_topline == wp->w_lines[0].wl_lnum
1414 && wp->w_topfill > wp->w_old_topfill)
1415#endif
1416 ))
1417 {
1418 /*
1419 * New topline is above old topline: May scroll down.
1420 */
1421#ifdef FEAT_FOLDING
1422 if (hasAnyFolding(wp))
1423 {
1424 linenr_T ln;
1425
1426 /* count the number of lines we are off, counting a sequence
1427 * of folded lines as one */
1428 j = 0;
1429 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1430 {
1431 ++j;
1432 if (j >= wp->w_height - 2)
1433 break;
1434 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1435 }
1436 }
1437 else
1438#endif
1439 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1440 if (j < wp->w_height - 2) /* not too far off */
1441 {
1442 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1443#ifdef FEAT_DIFF
1444 /* insert extra lines for previously invisible filler lines */
1445 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1446 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1447 - wp->w_old_topfill;
1448#endif
1449 if (i < wp->w_height - 2) /* less than a screen off */
1450 {
1451 /*
1452 * Try to insert the correct number of lines.
1453 * If not the last window, delete the lines at the bottom.
1454 * win_ins_lines may fail when the terminal can't do it.
1455 */
1456 if (i > 0)
1457 check_for_delay(FALSE);
1458 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1459 {
1460 if (wp->w_lines_valid != 0)
1461 {
1462 /* Need to update rows that are new, stop at the
1463 * first one that scrolled down. */
1464 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 /* Move the entries that were scrolled, disable
1468 * the entries for the lines to be redrawn. */
1469 if ((wp->w_lines_valid += j) > wp->w_height)
1470 wp->w_lines_valid = wp->w_height;
1471 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1472 wp->w_lines[idx] = wp->w_lines[idx - j];
1473 while (idx >= 0)
1474 wp->w_lines[idx--].wl_valid = FALSE;
1475 }
1476 }
1477 else
1478 mid_start = 0; /* redraw all lines */
1479 }
1480 else
1481 mid_start = 0; /* redraw all lines */
1482 }
1483 else
1484 mid_start = 0; /* redraw all lines */
1485 }
1486 else
1487 {
1488 /*
1489 * New topline is at or below old topline: May scroll up.
1490 * When topline didn't change, find first entry in w_lines[] that
1491 * needs updating.
1492 */
1493
1494 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1495 j = -1;
1496 row = 0;
1497 for (i = 0; i < wp->w_lines_valid; i++)
1498 {
1499 if (wp->w_lines[i].wl_valid
1500 && wp->w_lines[i].wl_lnum == wp->w_topline)
1501 {
1502 j = i;
1503 break;
1504 }
1505 row += wp->w_lines[i].wl_size;
1506 }
1507 if (j == -1)
1508 {
1509 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1510 * lines */
1511 mid_start = 0;
1512 }
1513 else
1514 {
1515 /*
1516 * Try to delete the correct number of lines.
1517 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1518 */
1519#ifdef FEAT_DIFF
1520 /* If the topline didn't change, delete old filler lines,
1521 * otherwise delete filler lines of the new topline... */
1522 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1523 row += wp->w_old_topfill;
1524 else
1525 row += diff_check_fill(wp, wp->w_topline);
1526 /* ... but don't delete new filler lines. */
1527 row -= wp->w_topfill;
1528#endif
1529 if (row > 0)
1530 {
1531 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001532 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1533 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 bot_start = wp->w_height - row;
1535 else
1536 mid_start = 0; /* redraw all lines */
1537 }
1538 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1539 {
1540 /*
1541 * Skip the lines (below the deleted lines) that are still
1542 * valid and don't need redrawing. Copy their info
1543 * upwards, to compensate for the deleted lines. Set
1544 * bot_start to the first row that needs redrawing.
1545 */
1546 bot_start = 0;
1547 idx = 0;
1548 for (;;)
1549 {
1550 wp->w_lines[idx] = wp->w_lines[j];
1551 /* stop at line that didn't fit, unless it is still
1552 * valid (no lines deleted) */
1553 if (row > 0 && bot_start + row
1554 + (int)wp->w_lines[j].wl_size > wp->w_height)
1555 {
1556 wp->w_lines_valid = idx + 1;
1557 break;
1558 }
1559 bot_start += wp->w_lines[idx++].wl_size;
1560
1561 /* stop at the last valid entry in w_lines[].wl_size */
1562 if (++j >= wp->w_lines_valid)
1563 {
1564 wp->w_lines_valid = idx;
1565 break;
1566 }
1567 }
1568#ifdef FEAT_DIFF
1569 /* Correct the first entry for filler lines at the top
1570 * when it won't get updated below. */
1571 if (wp->w_p_diff && bot_start > 0)
1572 wp->w_lines[0].wl_size =
1573 plines_win_nofill(wp, wp->w_topline, TRUE)
1574 + wp->w_topfill;
1575#endif
1576 }
1577 }
1578 }
1579
1580 /* When starting redraw in the first line, redraw all lines. When
1581 * there is only one window it's probably faster to clear the screen
1582 * first. */
1583 if (mid_start == 0)
1584 {
1585 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001586 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001587 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001588 /* Clear the screen when it was not done by win_del_lines() or
1589 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1590 * then. */
1591 if (screen_cleared != TRUE)
1592 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001593 /* The screen was cleared, redraw the tab pages line. */
1594 if (redraw_tabline)
1595 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001598
1599 /* When win_del_lines() or win_ins_lines() caused the screen to be
1600 * cleared (only happens for the first window) or when screenclear()
1601 * was called directly above, "must_redraw" will have been set to
1602 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1603 if (screen_cleared == TRUE)
1604 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 else
1607 {
1608 /* Not VALID or INVERTED: redraw all lines. */
1609 mid_start = 0;
1610 mid_end = wp->w_height;
1611 }
1612
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001613 if (type == SOME_VALID)
1614 {
1615 /* SOME_VALID: redraw all lines. */
1616 mid_start = 0;
1617 mid_end = wp->w_height;
1618 type = NOT_VALID;
1619 }
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /* check if we are updating or removing the inverted part */
1622 if ((VIsual_active && buf == curwin->w_buffer)
1623 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1624 {
1625 linenr_T from, to;
1626
1627 if (VIsual_active)
1628 {
1629 if (VIsual_active
1630 && (VIsual_mode != wp->w_old_visual_mode
1631 || type == INVERTED_ALL))
1632 {
1633 /*
1634 * If the type of Visual selection changed, redraw the whole
1635 * selection. Also when the ownership of the X selection is
1636 * gained or lost.
1637 */
1638 if (curwin->w_cursor.lnum < VIsual.lnum)
1639 {
1640 from = curwin->w_cursor.lnum;
1641 to = VIsual.lnum;
1642 }
1643 else
1644 {
1645 from = VIsual.lnum;
1646 to = curwin->w_cursor.lnum;
1647 }
1648 /* redraw more when the cursor moved as well */
1649 if (wp->w_old_cursor_lnum < from)
1650 from = wp->w_old_cursor_lnum;
1651 if (wp->w_old_cursor_lnum > to)
1652 to = wp->w_old_cursor_lnum;
1653 if (wp->w_old_visual_lnum < from)
1654 from = wp->w_old_visual_lnum;
1655 if (wp->w_old_visual_lnum > to)
1656 to = wp->w_old_visual_lnum;
1657 }
1658 else
1659 {
1660 /*
1661 * Find the line numbers that need to be updated: The lines
1662 * between the old cursor position and the current cursor
1663 * position. Also check if the Visual position changed.
1664 */
1665 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1666 {
1667 from = curwin->w_cursor.lnum;
1668 to = wp->w_old_cursor_lnum;
1669 }
1670 else
1671 {
1672 from = wp->w_old_cursor_lnum;
1673 to = curwin->w_cursor.lnum;
1674 if (from == 0) /* Visual mode just started */
1675 from = to;
1676 }
1677
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001678 if (VIsual.lnum != wp->w_old_visual_lnum
1679 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 if (wp->w_old_visual_lnum < from
1682 && wp->w_old_visual_lnum != 0)
1683 from = wp->w_old_visual_lnum;
1684 if (wp->w_old_visual_lnum > to)
1685 to = wp->w_old_visual_lnum;
1686 if (VIsual.lnum < from)
1687 from = VIsual.lnum;
1688 if (VIsual.lnum > to)
1689 to = VIsual.lnum;
1690 }
1691 }
1692
1693 /*
1694 * If in block mode and changed column or curwin->w_curswant:
1695 * update all lines.
1696 * First compute the actual start and end column.
1697 */
1698 if (VIsual_mode == Ctrl_V)
1699 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001700 colnr_T fromc, toc;
1701#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1702 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar404406a2014-10-09 13:24:43 +02001704 if (curwin->w_p_lbr)
1705 ve_flags = VE_ALL;
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001708#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1709 ve_flags = save_ve_flags;
1710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 ++toc;
1712 if (curwin->w_curswant == MAXCOL)
1713 toc = MAXCOL;
1714
1715 if (fromc != wp->w_old_cursor_fcol
1716 || toc != wp->w_old_cursor_lcol)
1717 {
1718 if (from > VIsual.lnum)
1719 from = VIsual.lnum;
1720 if (to < VIsual.lnum)
1721 to = VIsual.lnum;
1722 }
1723 wp->w_old_cursor_fcol = fromc;
1724 wp->w_old_cursor_lcol = toc;
1725 }
1726 }
1727 else
1728 {
1729 /* Use the line numbers of the old Visual area. */
1730 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1731 {
1732 from = wp->w_old_cursor_lnum;
1733 to = wp->w_old_visual_lnum;
1734 }
1735 else
1736 {
1737 from = wp->w_old_visual_lnum;
1738 to = wp->w_old_cursor_lnum;
1739 }
1740 }
1741
1742 /*
1743 * There is no need to update lines above the top of the window.
1744 */
1745 if (from < wp->w_topline)
1746 from = wp->w_topline;
1747
1748 /*
1749 * If we know the value of w_botline, use it to restrict the update to
1750 * the lines that are visible in the window.
1751 */
1752 if (wp->w_valid & VALID_BOTLINE)
1753 {
1754 if (from >= wp->w_botline)
1755 from = wp->w_botline - 1;
1756 if (to >= wp->w_botline)
1757 to = wp->w_botline - 1;
1758 }
1759
1760 /*
1761 * Find the minimal part to be updated.
1762 * Watch out for scrolling that made entries in w_lines[] invalid.
1763 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1764 * top_end; need to redraw from top_end to the "to" line.
1765 * A middle mouse click with a Visual selection may change the text
1766 * above the Visual area and reset wl_valid, do count these for
1767 * mid_end (in srow).
1768 */
1769 if (mid_start > 0)
1770 {
1771 lnum = wp->w_topline;
1772 idx = 0;
1773 srow = 0;
1774 if (scrolled_down)
1775 mid_start = top_end;
1776 else
1777 mid_start = 0;
1778 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1779 {
1780 if (wp->w_lines[idx].wl_valid)
1781 mid_start += wp->w_lines[idx].wl_size;
1782 else if (!scrolled_down)
1783 srow += wp->w_lines[idx].wl_size;
1784 ++idx;
1785# ifdef FEAT_FOLDING
1786 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1787 lnum = wp->w_lines[idx].wl_lnum;
1788 else
1789# endif
1790 ++lnum;
1791 }
1792 srow += mid_start;
1793 mid_end = wp->w_height;
1794 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1795 {
1796 if (wp->w_lines[idx].wl_valid
1797 && wp->w_lines[idx].wl_lnum >= to + 1)
1798 {
1799 /* Only update until first row of this line */
1800 mid_end = srow;
1801 break;
1802 }
1803 srow += wp->w_lines[idx].wl_size;
1804 }
1805 }
1806 }
1807
1808 if (VIsual_active && buf == curwin->w_buffer)
1809 {
1810 wp->w_old_visual_mode = VIsual_mode;
1811 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1812 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001813 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 wp->w_old_curswant = curwin->w_curswant;
1815 }
1816 else
1817 {
1818 wp->w_old_visual_mode = 0;
1819 wp->w_old_cursor_lnum = 0;
1820 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001821 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1825 /* reset got_int, otherwise regexp won't work */
1826 save_got_int = got_int;
1827 got_int = 0;
1828#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001829#ifdef SYN_TIME_LIMIT
1830 /* Set the time limit to 'redrawtime'. */
1831 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001832 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835 win_foldinfo.fi_level = 0;
1836#endif
1837
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001838#ifdef FEAT_MENU
1839 /*
1840 * Draw the window toolbar, if there is one.
1841 * TODO: only when needed.
1842 */
1843 if (winbar_height(wp) > 0)
1844 redraw_win_toolbar(wp);
1845#endif
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 /*
1848 * Update all the window rows.
1849 */
1850 idx = 0; /* first entry in w_lines[].wl_size */
1851 row = 0;
1852 srow = 0;
1853 lnum = wp->w_topline; /* first line shown in window */
1854 for (;;)
1855 {
1856 /* stop updating when reached the end of the window (check for _past_
1857 * the end of the window is at the end of the loop) */
1858 if (row == wp->w_height)
1859 {
1860 didline = TRUE;
1861 break;
1862 }
1863
1864 /* stop updating when hit the end of the file */
1865 if (lnum > buf->b_ml.ml_line_count)
1866 {
1867 eof = TRUE;
1868 break;
1869 }
1870
1871 /* Remember the starting row of the line that is going to be dealt
1872 * with. It is used further down when the line doesn't fit. */
1873 srow = row;
1874
1875 /*
1876 * Update a line when it is in an area that needs updating, when it
1877 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001878 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 * win_del_lines(), check if the current line includes it.
1880 * When syntax folding is being used, the saved syntax states will
1881 * already have been updated, we can't see where the syntax state is
1882 * the same again, just update until the end of the window.
1883 */
1884 if (row < top_end
1885 || (row >= mid_start && row < mid_end)
1886#ifdef FEAT_SEARCH_EXTRA
1887 || top_to_mod
1888#endif
1889 || idx >= wp->w_lines_valid
1890 || (row + wp->w_lines[idx].wl_size > bot_start)
1891 || (mod_top != 0
1892 && (lnum == mod_top
1893 || (lnum >= mod_top
1894 && (lnum < mod_bot
1895#ifdef FEAT_SYN_HL
1896 || did_update == DID_FOLD
1897 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001898 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 && (
1900# ifdef FEAT_FOLDING
1901 (foldmethodIsSyntax(wp)
1902 && hasAnyFolding(wp)) ||
1903# endif
1904 syntax_check_changed(lnum)))
1905#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001906#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001907 /* match in fixed position might need redraw
1908 * if lines were inserted or deleted */
1909 || (wp->w_match_head != NULL
1910 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 )))))
1913 {
1914#ifdef FEAT_SEARCH_EXTRA
1915 if (lnum == mod_top)
1916 top_to_mod = FALSE;
1917#endif
1918
1919 /*
1920 * When at start of changed lines: May scroll following lines
1921 * up or down to minimize redrawing.
1922 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001923 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
1925 if (lnum == mod_top
1926 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001927 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 int old_rows = 0;
1930 int new_rows = 0;
1931 int xtra_rows;
1932 linenr_T l;
1933
1934 /* Count the old number of window rows, using w_lines[], which
1935 * should still contain the sizes for the lines as they are
1936 * currently displayed. */
1937 for (i = idx; i < wp->w_lines_valid; ++i)
1938 {
1939 /* Only valid lines have a meaningful wl_lnum. Invalid
1940 * lines are part of the changed area. */
1941 if (wp->w_lines[i].wl_valid
1942 && wp->w_lines[i].wl_lnum == mod_bot)
1943 break;
1944 old_rows += wp->w_lines[i].wl_size;
1945#ifdef FEAT_FOLDING
1946 if (wp->w_lines[i].wl_valid
1947 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1948 {
1949 /* Must have found the last valid entry above mod_bot.
1950 * Add following invalid entries. */
1951 ++i;
1952 while (i < wp->w_lines_valid
1953 && !wp->w_lines[i].wl_valid)
1954 old_rows += wp->w_lines[i++].wl_size;
1955 break;
1956 }
1957#endif
1958 }
1959
1960 if (i >= wp->w_lines_valid)
1961 {
1962 /* We can't find a valid line below the changed lines,
1963 * need to redraw until the end of the window.
1964 * Inserting/deleting lines has no use. */
1965 bot_start = 0;
1966 }
1967 else
1968 {
1969 /* Able to count old number of rows: Count new window
1970 * rows, and may insert/delete lines */
1971 j = idx;
1972 for (l = lnum; l < mod_bot; ++l)
1973 {
1974#ifdef FEAT_FOLDING
1975 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1976 ++new_rows;
1977 else
1978#endif
1979#ifdef FEAT_DIFF
1980 if (l == wp->w_topline)
1981 new_rows += plines_win_nofill(wp, l, TRUE)
1982 + wp->w_topfill;
1983 else
1984#endif
1985 new_rows += plines_win(wp, l, TRUE);
1986 ++j;
1987 if (new_rows > wp->w_height - row - 2)
1988 {
1989 /* it's getting too much, must redraw the rest */
1990 new_rows = 9999;
1991 break;
1992 }
1993 }
1994 xtra_rows = new_rows - old_rows;
1995 if (xtra_rows < 0)
1996 {
1997 /* May scroll text up. If there is not enough
1998 * remaining text or scrolling fails, must redraw the
1999 * rest. If scrolling works, must redraw the text
2000 * below the scrolled text. */
2001 if (row - xtra_rows >= wp->w_height - 2)
2002 mod_bot = MAXLNUM;
2003 else
2004 {
2005 check_for_delay(FALSE);
2006 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002007 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 mod_bot = MAXLNUM;
2009 else
2010 bot_start = wp->w_height + xtra_rows;
2011 }
2012 }
2013 else if (xtra_rows > 0)
2014 {
2015 /* May scroll text down. If there is not enough
2016 * remaining text of scrolling fails, must redraw the
2017 * rest. */
2018 if (row + xtra_rows >= wp->w_height - 2)
2019 mod_bot = MAXLNUM;
2020 else
2021 {
2022 check_for_delay(FALSE);
2023 if (win_ins_lines(wp, row + old_rows,
2024 xtra_rows, FALSE, FALSE) == FAIL)
2025 mod_bot = MAXLNUM;
2026 else if (top_end > row + old_rows)
2027 /* Scrolled the part at the top that requires
2028 * updating down. */
2029 top_end += xtra_rows;
2030 }
2031 }
2032
2033 /* When not updating the rest, may need to move w_lines[]
2034 * entries. */
2035 if (mod_bot != MAXLNUM && i != j)
2036 {
2037 if (j < i)
2038 {
2039 int x = row + new_rows;
2040
2041 /* move entries in w_lines[] upwards */
2042 for (;;)
2043 {
2044 /* stop at last valid entry in w_lines[] */
2045 if (i >= wp->w_lines_valid)
2046 {
2047 wp->w_lines_valid = j;
2048 break;
2049 }
2050 wp->w_lines[j] = wp->w_lines[i];
2051 /* stop at a line that won't fit */
2052 if (x + (int)wp->w_lines[j].wl_size
2053 > wp->w_height)
2054 {
2055 wp->w_lines_valid = j + 1;
2056 break;
2057 }
2058 x += wp->w_lines[j++].wl_size;
2059 ++i;
2060 }
2061 if (bot_start > x)
2062 bot_start = x;
2063 }
2064 else /* j > i */
2065 {
2066 /* move entries in w_lines[] downwards */
2067 j -= i;
2068 wp->w_lines_valid += j;
2069 if (wp->w_lines_valid > wp->w_height)
2070 wp->w_lines_valid = wp->w_height;
2071 for (i = wp->w_lines_valid; i - j >= idx; --i)
2072 wp->w_lines[i] = wp->w_lines[i - j];
2073
2074 /* The w_lines[] entries for inserted lines are
2075 * now invalid, but wl_size may be used above.
2076 * Reset to zero. */
2077 while (i >= idx)
2078 {
2079 wp->w_lines[i].wl_size = 0;
2080 wp->w_lines[i--].wl_valid = FALSE;
2081 }
2082 }
2083 }
2084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086
2087#ifdef FEAT_FOLDING
2088 /*
2089 * When lines are folded, display one line for all of them.
2090 * Otherwise, display normally (can be several display lines when
2091 * 'wrap' is on).
2092 */
2093 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2094 if (fold_count != 0)
2095 {
2096 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2097 ++row;
2098 --fold_count;
2099 wp->w_lines[idx].wl_folded = TRUE;
2100 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2101# ifdef FEAT_SYN_HL
2102 did_update = DID_FOLD;
2103# endif
2104 }
2105 else
2106#endif
2107 if (idx < wp->w_lines_valid
2108 && wp->w_lines[idx].wl_valid
2109 && wp->w_lines[idx].wl_lnum == lnum
2110 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002111 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 && srow + wp->w_lines[idx].wl_size > wp->w_height
2113#ifdef FEAT_DIFF
2114 && diff_check_fill(wp, lnum) == 0
2115#endif
2116 )
2117 {
2118 /* This line is not going to fit. Don't draw anything here,
2119 * will draw "@ " lines below. */
2120 row = wp->w_height + 1;
2121 }
2122 else
2123 {
2124#ifdef FEAT_SEARCH_EXTRA
2125 prepare_search_hl(wp, lnum);
2126#endif
2127#ifdef FEAT_SYN_HL
2128 /* Let the syntax stuff know we skipped a few lines. */
2129 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002130 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 syntax_end_parsing(syntax_last_parsed + 1);
2132#endif
2133
2134 /*
2135 * Display one line.
2136 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002137 row = win_line(wp, lnum, srow, wp->w_height,
2138 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140#ifdef FEAT_FOLDING
2141 wp->w_lines[idx].wl_folded = FALSE;
2142 wp->w_lines[idx].wl_lastlnum = lnum;
2143#endif
2144#ifdef FEAT_SYN_HL
2145 did_update = DID_LINE;
2146 syntax_last_parsed = lnum;
2147#endif
2148 }
2149
2150 wp->w_lines[idx].wl_lnum = lnum;
2151 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002152
2153 /* Past end of the window or end of the screen. Note that after
2154 * resizing wp->w_height may be end up too big. That's a problem
2155 * elsewhere, but prevent a crash here. */
2156 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002159 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2161 ++idx;
2162 break;
2163 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002164 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 wp->w_lines[idx].wl_size = row - srow;
2166 ++idx;
2167#ifdef FEAT_FOLDING
2168 lnum += fold_count + 1;
2169#else
2170 ++lnum;
2171#endif
2172 }
2173 else
2174 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002175 if (wp->w_p_rnu)
2176 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002177#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002178 // 'relativenumber' set: The text doesn't need to be drawn, but
2179 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002180 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2181 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002182 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002183 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002184#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002185 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002186 }
2187
2188 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 row += wp->w_lines[idx++].wl_size;
2190 if (row > wp->w_height) /* past end of screen */
2191 break;
2192#ifdef FEAT_FOLDING
2193 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2194#else
2195 ++lnum;
2196#endif
2197#ifdef FEAT_SYN_HL
2198 did_update = DID_NONE;
2199#endif
2200 }
2201
2202 if (lnum > buf->b_ml.ml_line_count)
2203 {
2204 eof = TRUE;
2205 break;
2206 }
2207 }
2208 /*
2209 * End of loop over all window lines.
2210 */
2211
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002212#ifdef FEAT_VTP
2213 /* Rewrite the character at the end of the screen line. */
2214 if (use_vtp())
2215 {
2216 int i;
2217
2218 for (i = 0; i < Rows; ++i)
2219# ifdef FEAT_MBYTE
2220 if (enc_utf8)
2221 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2222 LineOffset[i] + screen_Columns) > 1)
2223 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2224 else
2225 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2226 else
2227# endif
2228 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2229 }
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 if (idx > wp->w_lines_valid)
2233 wp->w_lines_valid = idx;
2234
2235#ifdef FEAT_SYN_HL
2236 /*
2237 * Let the syntax stuff know we stop parsing here.
2238 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002239 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 syntax_end_parsing(syntax_last_parsed + 1);
2241#endif
2242
2243 /*
2244 * If we didn't hit the end of the file, and we didn't finish the last
2245 * line we were working on, then the line didn't fit.
2246 */
2247 wp->w_empty_rows = 0;
2248#ifdef FEAT_DIFF
2249 wp->w_filler_rows = 0;
2250#endif
2251 if (!eof && !didline)
2252 {
2253 if (lnum == wp->w_topline)
2254 {
2255 /*
2256 * Single line that does not fit!
2257 * Don't overwrite it, it can be edited.
2258 */
2259 wp->w_botline = lnum + 1;
2260 }
2261#ifdef FEAT_DIFF
2262 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2263 {
2264 /* Window ends in filler lines. */
2265 wp->w_botline = lnum;
2266 wp->w_filler_rows = wp->w_height - srow;
2267 }
2268#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002269 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2270 {
2271 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2272
2273 /*
2274 * Last line isn't finished: Display "@@@" in the last screen line.
2275 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002276 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002277 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002278 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002279 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002280 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002281 set_empty_rows(wp, srow);
2282 wp->w_botline = lnum;
2283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2285 {
2286 /*
2287 * Last line isn't finished: Display "@@@" at the end.
2288 */
2289 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2290 W_WINROW(wp) + wp->w_height,
2291 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002292 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 set_empty_rows(wp, srow);
2294 wp->w_botline = lnum;
2295 }
2296 else
2297 {
2298 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2299 wp->w_botline = lnum;
2300 }
2301 }
2302 else
2303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (eof) /* we hit the end of the file */
2306 {
2307 wp->w_botline = buf->b_ml.ml_line_count + 1;
2308#ifdef FEAT_DIFF
2309 j = diff_check_fill(wp, wp->w_botline);
2310 if (j > 0 && !wp->w_botfill)
2311 {
2312 /*
2313 * Display filler lines at the end of the file
2314 */
2315 if (char2cells(fill_diff) > 1)
2316 i = '-';
2317 else
2318 i = fill_diff;
2319 if (row + j > wp->w_height)
2320 j = wp->w_height - row;
2321 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2322 row += j;
2323 }
2324#endif
2325 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002326 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 wp->w_botline = lnum;
2328
2329 /* make sure the rest of the screen is blank */
2330 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002331 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002334#ifdef SYN_TIME_LIMIT
2335 syn_set_timeout(NULL);
2336#endif
2337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 /* Reset the type of redrawing required, the window has been updated. */
2339 wp->w_redr_type = 0;
2340#ifdef FEAT_DIFF
2341 wp->w_old_topfill = wp->w_topfill;
2342 wp->w_old_botfill = wp->w_botfill;
2343#endif
2344
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002345 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
2347 /*
2348 * There is a trick with w_botline. If we invalidate it on each
2349 * change that might modify it, this will cause a lot of expensive
2350 * calls to plines() in update_topline() each time. Therefore the
2351 * value of w_botline is often approximated, and this value is used to
2352 * compute the value of w_topline. If the value of w_botline was
2353 * wrong, check that the value of w_topline is correct (cursor is on
2354 * the visible part of the text). If it's not, we need to redraw
2355 * again. Mostly this just means scrolling up a few lines, so it
2356 * doesn't look too bad. Only do this for the current window (where
2357 * changes are relevant).
2358 */
2359 wp->w_valid |= VALID_BOTLINE;
2360 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2361 {
2362 recursive = TRUE;
2363 curwin->w_valid &= ~VALID_TOPLINE;
2364 update_topline(); /* may invalidate w_botline again */
2365 if (must_redraw != 0)
2366 {
2367 /* Don't update for changes in buffer again. */
2368 i = curbuf->b_mod_set;
2369 curbuf->b_mod_set = FALSE;
2370 win_update(curwin);
2371 must_redraw = 0;
2372 curbuf->b_mod_set = i;
2373 }
2374 recursive = FALSE;
2375 }
2376 }
2377
2378#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2379 /* restore got_int, unless CTRL-C was hit while redrawing */
2380 if (!got_int)
2381 got_int = save_got_int;
2382#endif
2383}
2384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385/*
2386 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2387 * as the filler character.
2388 */
2389 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002390win_draw_end(
2391 win_T *wp,
2392 int c1,
2393 int c2,
2394 int row,
2395 int endrow,
2396 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2399 int n = 0;
2400# define FDC_OFF n
2401#else
2402# define FDC_OFF 0
2403#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002404#ifdef FEAT_FOLDING
2405 int fdc = compute_foldcolumn(wp, 0);
2406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408#ifdef FEAT_RIGHTLEFT
2409 if (wp->w_p_rl)
2410 {
2411 /* No check for cmdline window: should never be right-left. */
2412# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002413 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 if (n > 0)
2416 {
2417 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002418 if (n > wp->w_width)
2419 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2421 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002422 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
2424# endif
2425# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002426 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
2428 int nn = n + 2;
2429
2430 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002431 if (nn > wp->w_width)
2432 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2434 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 n = nn;
2437 }
2438# endif
2439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002440 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002441 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2443 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002444 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446 else
2447#endif
2448 {
2449#ifdef FEAT_CMDWIN
2450 if (cmdwin_type != 0 && wp == curwin)
2451 {
2452 /* draw the cmdline character in the leftmost column */
2453 n = 1;
2454 if (n > wp->w_width)
2455 n = wp->w_width;
2456 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002457 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002458 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460#endif
2461#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002462 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002464 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002467 if (nn > wp->w_width)
2468 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002470 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002471 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 n = nn;
2473 }
2474#endif
2475#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002476 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
2478 int nn = n + 2;
2479
2480 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002481 if (nn > wp->w_width)
2482 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002484 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002485 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 n = nn;
2487 }
2488#endif
2489 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002490 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002491 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493 set_empty_rows(wp, row);
2494}
2495
Bram Moolenaar1a384422010-07-14 19:53:30 +02002496#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002497/*
2498 * Advance **color_cols and return TRUE when there are columns to draw.
2499 */
2500 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002501advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002502{
2503 while (**color_cols >= 0 && vcol > **color_cols)
2504 ++*color_cols;
2505 return (**color_cols >= 0);
2506}
2507#endif
2508
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002509#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2510/*
2511 * Copy "text" to ScreenLines using "attr".
2512 * Returns the next screen column.
2513 */
2514 static int
2515text_to_screenline(win_T *wp, char_u *text, int col)
2516{
2517 int off = (int)(current_ScreenLine - ScreenLines);
2518
2519#ifdef FEAT_MBYTE
2520 if (has_mbyte)
2521 {
2522 int cells;
2523 int u8c, u8cc[MAX_MCO];
2524 int i;
2525 int idx;
2526 int c_len;
2527 char_u *p;
2528# ifdef FEAT_ARABIC
2529 int prev_c = 0; /* previous Arabic character */
2530 int prev_c1 = 0; /* first composing char for prev_c */
2531# endif
2532
2533# ifdef FEAT_RIGHTLEFT
2534 if (wp->w_p_rl)
2535 idx = off;
2536 else
2537# endif
2538 idx = off + col;
2539
2540 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2541 for (p = text; *p != NUL; )
2542 {
2543 cells = (*mb_ptr2cells)(p);
2544 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002545 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002546# ifdef FEAT_RIGHTLEFT
2547 - (wp->w_p_rl ? col : 0)
2548# endif
2549 )
2550 break;
2551 ScreenLines[idx] = *p;
2552 if (enc_utf8)
2553 {
2554 u8c = utfc_ptr2char(p, u8cc);
2555 if (*p < 0x80 && u8cc[0] == 0)
2556 {
2557 ScreenLinesUC[idx] = 0;
2558#ifdef FEAT_ARABIC
2559 prev_c = u8c;
2560#endif
2561 }
2562 else
2563 {
2564#ifdef FEAT_ARABIC
2565 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2566 {
2567 /* Do Arabic shaping. */
2568 int pc, pc1, nc;
2569 int pcc[MAX_MCO];
2570 int firstbyte = *p;
2571
2572 /* The idea of what is the previous and next
2573 * character depends on 'rightleft'. */
2574 if (wp->w_p_rl)
2575 {
2576 pc = prev_c;
2577 pc1 = prev_c1;
2578 nc = utf_ptr2char(p + c_len);
2579 prev_c1 = u8cc[0];
2580 }
2581 else
2582 {
2583 pc = utfc_ptr2char(p + c_len, pcc);
2584 nc = prev_c;
2585 pc1 = pcc[0];
2586 }
2587 prev_c = u8c;
2588
2589 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2590 pc, pc1, nc);
2591 ScreenLines[idx] = firstbyte;
2592 }
2593 else
2594 prev_c = u8c;
2595#endif
2596 /* Non-BMP character: display as ? or fullwidth ?. */
2597#ifdef UNICODE16
2598 if (u8c >= 0x10000)
2599 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2600 else
2601#endif
2602 ScreenLinesUC[idx] = u8c;
2603 for (i = 0; i < Screen_mco; ++i)
2604 {
2605 ScreenLinesC[i][idx] = u8cc[i];
2606 if (u8cc[i] == 0)
2607 break;
2608 }
2609 }
2610 if (cells > 1)
2611 ScreenLines[idx + 1] = 0;
2612 }
2613 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2614 /* double-byte single width character */
2615 ScreenLines2[idx] = p[1];
2616 else if (cells > 1)
2617 /* double-width character */
2618 ScreenLines[idx + 1] = p[1];
2619 col += cells;
2620 idx += cells;
2621 p += c_len;
2622 }
2623 }
2624 else
2625#endif
2626 {
2627 int len = (int)STRLEN(text);
2628
Bram Moolenaar02631462017-09-22 15:20:32 +02002629 if (len > wp->w_width - col)
2630 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002631 if (len > 0)
2632 {
2633#ifdef FEAT_RIGHTLEFT
2634 if (wp->w_p_rl)
2635 STRNCPY(current_ScreenLine, text, len);
2636 else
2637#endif
2638 STRNCPY(current_ScreenLine + col, text, len);
2639 col += len;
2640 }
2641 }
2642 return col;
2643}
2644#endif
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#ifdef FEAT_FOLDING
2647/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002648 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2649 * space is available for window "wp", minus "col".
2650 */
2651 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002652compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002653{
2654 int fdc = wp->w_p_fdc;
2655 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002656 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002657
2658 if (fdc > wwidth - (col + wmw))
2659 fdc = wwidth - (col + wmw);
2660 return fdc;
2661}
2662
2663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 * Display one folded line.
2665 */
2666 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002667fold_line(
2668 win_T *wp,
2669 long fold_count,
2670 foldinfo_T *foldinfo,
2671 linenr_T lnum,
2672 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002674 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 pos_T *top, *bot;
2676 linenr_T lnume = lnum + fold_count - 1;
2677 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002678 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 int col;
2681 int txtcol;
2682 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002683 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
2685 /* Build the fold line:
2686 * 1. Add the cmdwin_type for the command-line window
2687 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002688 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 * 4. Compose the text
2690 * 5. Add the text
2691 * 6. set highlighting for the Visual area an other text
2692 */
2693 col = 0;
2694
2695 /*
2696 * 1. Add the cmdwin_type for the command-line window
2697 * Ignores 'rightleft', this window is never right-left.
2698 */
2699#ifdef FEAT_CMDWIN
2700 if (cmdwin_type != 0 && wp == curwin)
2701 {
2702 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002703 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#ifdef FEAT_MBYTE
2705 if (enc_utf8)
2706 ScreenLinesUC[off] = 0;
2707#endif
2708 ++col;
2709 }
2710#endif
2711
2712 /*
2713 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002714 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002716 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (fdc > 0)
2718 {
2719 fill_foldcolumn(buf, wp, TRUE, lnum);
2720#ifdef FEAT_RIGHTLEFT
2721 if (wp->w_p_rl)
2722 {
2723 int i;
2724
Bram Moolenaar02631462017-09-22 15:20:32 +02002725 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002726 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 /* reverse the fold column */
2728 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002729 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731 else
2732#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002733 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 col += fdc;
2735 }
2736
2737#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002738# define RL_MEMSET(p, v, l) \
2739 do { \
2740 if (wp->w_p_rl) \
2741 for (ri = 0; ri < l; ++ri) \
2742 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2743 else \
2744 for (ri = 0; ri < l; ++ri) \
2745 ScreenAttrs[off + (p) + ri] = v; \
2746 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002748# define RL_MEMSET(p, v, l) \
2749 do { \
2750 for (ri = 0; ri < l; ++ri) \
2751 ScreenAttrs[off + (p) + ri] = v; \
2752 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#endif
2754
Bram Moolenaar64486672010-05-16 15:46:46 +02002755 /* Set all attributes of the 'number' or 'relativenumber' column and the
2756 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002757 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759#ifdef FEAT_SIGNS
2760 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002761 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002763 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (len > 0)
2765 {
2766 if (len > 2)
2767 len = 2;
2768# ifdef FEAT_RIGHTLEFT
2769 if (wp->w_p_rl)
2770 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002771 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002772 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else
2774# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002775 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 col += len;
2777 }
2778 }
2779#endif
2780
2781 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002782 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002784 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002786 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 if (len > 0)
2788 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002789 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002790 long num;
2791 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002792
2793 if (len > w + 1)
2794 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002795
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002796 if (wp->w_p_nu && !wp->w_p_rnu)
2797 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002798 num = (long)lnum;
2799 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002800 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002801 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002802 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002803 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002804 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002805 /* 'number' + 'relativenumber': cursor line shows absolute
2806 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002807 num = lnum;
2808 fmt = "%-*ld ";
2809 }
2810 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002811
Bram Moolenaar700e7342013-01-30 12:31:36 +01002812 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813#ifdef FEAT_RIGHTLEFT
2814 if (wp->w_p_rl)
2815 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002816 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002817 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 else
2819#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002820 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 col += len;
2822 }
2823 }
2824
2825 /*
2826 * 4. Compose the folded-line string with 'foldtext', if set.
2827 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002828 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830 txtcol = col; /* remember where text starts */
2831
2832 /*
2833 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2834 * Right-left text is put in columns 0 - number-col, normal text is put
2835 * in columns number-col - window-width.
2836 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002837 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
2839 /* Fill the rest of the line with the fold filler */
2840#ifdef FEAT_RIGHTLEFT
2841 if (wp->w_p_rl)
2842 col -= txtcol;
2843#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002844 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845#ifdef FEAT_RIGHTLEFT
2846 - (wp->w_p_rl ? txtcol : 0)
2847#endif
2848 )
2849 {
2850#ifdef FEAT_MBYTE
2851 if (enc_utf8)
2852 {
2853 if (fill_fold >= 0x80)
2854 {
2855 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002856 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002857 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002862 ScreenLines[off + col] = fill_fold;
2863 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002864 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002866 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002868 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
2870
2871 if (text != buf)
2872 vim_free(text);
2873
2874 /*
2875 * 6. set highlighting for the Visual area an other text.
2876 * If all folded lines are in the Visual area, highlight the line.
2877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2879 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002880 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
2882 /* Visual is after curwin->w_cursor */
2883 top = &curwin->w_cursor;
2884 bot = &VIsual;
2885 }
2886 else
2887 {
2888 /* Visual is before curwin->w_cursor */
2889 top = &VIsual;
2890 bot = &curwin->w_cursor;
2891 }
2892 if (lnum >= top->lnum
2893 && lnume <= bot->lnum
2894 && (VIsual_mode != 'v'
2895 || ((lnum > top->lnum
2896 || (lnum == top->lnum
2897 && top->col == 0))
2898 && (lnume < bot->lnum
2899 || (lnume == bot->lnum
2900 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002901 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903 if (VIsual_mode == Ctrl_V)
2904 {
2905 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002906 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002908 if (wp->w_old_cursor_lcol != MAXCOL
2909 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002910 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 len = wp->w_old_cursor_lcol;
2912 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002913 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002914 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002915 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917 }
2918 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002921 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002926#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002927 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2928 if (wp->w_p_cc_cols)
2929 {
2930 int i = 0;
2931 int j = wp->w_p_cc_cols[i];
2932 int old_txtcol = txtcol;
2933
2934 while (j > -1)
2935 {
2936 txtcol += j;
2937 if (wp->w_p_wrap)
2938 txtcol -= wp->w_skipcol;
2939 else
2940 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002941 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002942 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002943 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002944 txtcol = old_txtcol;
2945 j = wp->w_p_cc_cols[++i];
2946 }
2947 }
2948
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002949 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002950 if (wp->w_p_cuc)
2951 {
2952 txtcol += wp->w_virtcol;
2953 if (wp->w_p_wrap)
2954 txtcol -= wp->w_skipcol;
2955 else
2956 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002957 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002958 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002959 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002960 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
Bram Moolenaar02631462017-09-22 15:20:32 +02002963 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2964 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 /*
2967 * Update w_cline_height and w_cline_folded if the cursor line was
2968 * updated (saves a call to plines() later).
2969 */
2970 if (wp == curwin
2971 && lnum <= curwin->w_cursor.lnum
2972 && lnume >= curwin->w_cursor.lnum)
2973 {
2974 curwin->w_cline_row = row;
2975 curwin->w_cline_height = 1;
2976 curwin->w_cline_folded = TRUE;
2977 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2978 }
2979}
2980
2981/*
2982 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2983 */
2984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002985copy_text_attr(
2986 int off,
2987 char_u *buf,
2988 int len,
2989 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002991 int i;
2992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 mch_memmove(ScreenLines + off, buf, (size_t)len);
2994# ifdef FEAT_MBYTE
2995 if (enc_utf8)
2996 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2997# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002998 for (i = 0; i < len; ++i)
2999 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000}
3001
3002/*
3003 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003004 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 */
3006 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003007fill_foldcolumn(
3008 char_u *p,
3009 win_T *wp,
3010 int closed, /* TRUE of FALSE */
3011 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 int i = 0;
3014 int level;
3015 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003016 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003017 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
3019 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003020 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 level = win_foldinfo.fi_level;
3023 if (level > 0)
3024 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003025 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003026 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 /* If the column is too narrow, we start at the lowest level that
3029 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003030 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (first_level < 1)
3032 first_level = 1;
3033
Bram Moolenaar1c934292015-01-27 16:39:29 +01003034 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
3036 if (win_foldinfo.fi_lnum == lnum
3037 && first_level + i >= win_foldinfo.fi_low_level)
3038 p[i] = '-';
3039 else if (first_level == 1)
3040 p[i] = '|';
3041 else if (first_level + i <= 9)
3042 p[i] = '0' + first_level + i;
3043 else
3044 p[i] = '>';
3045 if (first_level + i == level)
3046 break;
3047 }
3048 }
3049 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003050 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051}
3052#endif /* FEAT_FOLDING */
3053
3054/*
3055 * Display line "lnum" of window 'wp' on the screen.
3056 * Start at row "startrow", stop when "endrow" is reached.
3057 * wp->w_virtcol needs to be valid.
3058 *
3059 * Return the number of last row the line occupies.
3060 */
3061 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003062win_line(
3063 win_T *wp,
3064 linenr_T lnum,
3065 int startrow,
3066 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003067 int nochange UNUSED, // not updating for changed text
3068 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003070 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3072 int c = 0; /* init for GCC */
3073 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003074#ifdef FEAT_LINEBREAK
3075 long vcol_sbr = -1; /* virtual column after showbreak */
3076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 long vcol_prev = -1; /* "vcol" of previous character */
3078 char_u *line; /* current line */
3079 char_u *ptr; /* current position in "line" */
3080 int row; /* row in the window, excl w_winrow */
3081 int screen_row; /* row on the screen, incl w_winrow */
3082
3083 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3084 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003085 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003086 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 int c_extra = NUL; /* extra chars, all the same */
3088 int extra_attr = 0; /* attributes when n_extra != 0 */
3089 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3090 displaying lcs_eol at end-of-line */
3091 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3092 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3093
3094 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3095 int saved_n_extra = 0;
3096 char_u *saved_p_extra = NULL;
3097 int saved_c_extra = 0;
3098 int saved_char_attr = 0;
3099
3100 int n_attr = 0; /* chars with special attr */
3101 int saved_attr2 = 0; /* char_attr saved for n_attr */
3102 int n_attr3 = 0; /* chars with overruling special attr */
3103 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3104
3105 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3106
3107 int fromcol, tocol; /* start/end of inverting */
3108 int fromcol_prev = -2; /* start of inverting after cursor */
3109 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003111 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 pos_T pos;
3113 long v;
3114
3115 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003116 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3118 in this line */
3119 int attr = 0; /* attributes for area highlighting */
3120 int area_attr = 0; /* attributes desired by highlighting */
3121 int search_attr = 0; /* attributes desired by 'hlsearch' */
3122#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003123 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 int syntax_attr = 0; /* attributes desired by syntax */
3125 int has_syntax = FALSE; /* this buffer has syntax highl. */
3126 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003127 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003128 int draw_color_col = FALSE; /* highlight colorcolumn */
3129 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003130#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003131#ifdef FEAT_TEXT_PROP
3132 int text_prop_count;
3133 int text_prop_next = 0; // next text property to use
3134 textprop_T *text_props = NULL;
3135 int *text_prop_idxs = NULL;
3136 int text_props_active = 0;
3137 proptype_T *text_prop_type = NULL;
3138 int text_prop_attr = 0;
3139#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003140#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003141 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003142# define SPWORDLEN 150
3143 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003144 int nextlinecol = 0; /* column where nextline[] starts */
3145 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003146 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003147 int spell_attr = 0; /* attributes desired by spelling */
3148 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003149 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3150 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003151 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003152 static int cap_col = -1; /* column to check for Cap word */
3153 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003154 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003156 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#ifdef FEAT_MBYTE
3158 int multi_attr = 0; /* attributes desired by multibyte */
3159 int mb_l = 1; /* multi-byte byte length */
3160 int mb_c = 0; /* decoded multi-byte character */
3161 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003162 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
3164#ifdef FEAT_DIFF
3165 int filler_lines; /* nr of filler lines to be drawn */
3166 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003167 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 int change_start = MAXCOL; /* first col of changed area */
3169 int change_end = -1; /* last col of changed area */
3170#endif
3171 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3172#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003173 int need_showbreak = FALSE; /* overlong line, skipping first x
3174 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003176#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003177 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003179 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180#endif
3181#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003182 matchitem_T *cur; /* points to the match list */
3183 match_T *shl; /* points to search_hl or a match */
3184 int shl_flag; /* flag to indicate whether search_hl
3185 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003186 int pos_inprogress; /* marks that position match search is
3187 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003188 int prevcol_hl_flag; /* flag to indicate whether prevcol
3189 equals startcol of search_hl or one
3190 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191#endif
3192#ifdef FEAT_ARABIC
3193 int prev_c = 0; /* previous Arabic character */
3194 int prev_c1 = 0; /* first composing char for prev_c */
3195#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003196#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003197 int did_line_attr = 0;
3198#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003199#ifdef FEAT_TERMINAL
3200 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003201 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 /* draw_state: items that are drawn in sequence: */
3205#define WL_START 0 /* nothing done yet */
3206#ifdef FEAT_CMDWIN
3207# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3208#else
3209# define WL_CMDLINE WL_START
3210#endif
3211#ifdef FEAT_FOLDING
3212# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3213#else
3214# define WL_FOLD WL_CMDLINE
3215#endif
3216#ifdef FEAT_SIGNS
3217# define WL_SIGN WL_FOLD + 1 /* column for signs */
3218#else
3219# define WL_SIGN WL_FOLD /* column for signs */
3220#endif
3221#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003222#ifdef FEAT_LINEBREAK
3223# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003225# define WL_BRI WL_NR
3226#endif
3227#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3228# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3229#else
3230# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231#endif
3232#define WL_LINE WL_SBR + 1 /* text in the line */
3233 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003234#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 int feedback_col = 0;
3236 int feedback_old_attr = -1;
3237#endif
3238
Bram Moolenaar860cae12010-06-05 23:22:07 +02003239#ifdef FEAT_CONCEAL
3240 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003241 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003242 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003243 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003244 int is_concealing = FALSE;
3245 int boguscols = 0; /* nonexistent columns added to force
3246 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003247 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003248 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003249 int match_conc = 0; /* cchar for match functions */
3250 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003251 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003252# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003253# define FIX_FOR_BOGUSCOLS \
3254 { \
3255 n_extra += vcol_off; \
3256 vcol -= vcol_off; \
3257 vcol_off = 0; \
3258 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003259 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003260 boguscols = 0; \
3261 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003262#else
3263# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 if (startrow > endrow) /* past the end already! */
3267 return startrow;
3268
3269 row = startrow;
3270 screen_row = row + W_WINROW(wp);
3271
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003272 if (!number_only)
3273 {
3274 /*
3275 * To speed up the loop below, set extra_check when there is linebreak,
3276 * trailing white space and/or syntax processing to be done.
3277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003279 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#endif
3281#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003282 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003283# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003284 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003285# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003286 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003288 /* Prepare for syntax highlighting in this line. When there is an
3289 * error, stop syntax highlighting. */
3290 save_did_emsg = did_emsg;
3291 did_emsg = FALSE;
3292 syntax_start(wp, lnum);
3293 if (did_emsg)
3294 wp->w_s->b_syn_error = TRUE;
3295 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003296 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003297 did_emsg = save_did_emsg;
3298#ifdef SYN_TIME_LIMIT
3299 if (!wp->w_s->b_syn_slow)
3300#endif
3301 {
3302 has_syntax = TRUE;
3303 extra_check = TRUE;
3304 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003307
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003308 /* Check for columns to display for 'colorcolumn'. */
3309 color_cols = wp->w_p_cc_cols;
3310 if (color_cols != NULL)
3311 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003312#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003313
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003314#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003315 if (term_show_buffer(wp->w_buffer))
3316 {
3317 extra_check = TRUE;
3318 get_term_attr = TRUE;
3319 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3320 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003321#endif
3322
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003323#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003324 if (wp->w_p_spell
3325 && *wp->w_s->b_p_spl != NUL
3326 && wp->w_s->b_langp.ga_len > 0
3327 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003328 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003329 /* Prepare for spell checking. */
3330 has_spell = TRUE;
3331 extra_check = TRUE;
3332
Bram Moolenaar7701f302018-10-02 21:20:32 +02003333 /* Get the start of the next line, so that words that wrap to the
3334 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003335 * Trick: skip a few chars for C/shell/Vim comments */
3336 nextline[SPWORDLEN] = NUL;
3337 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3338 {
3339 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3340 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3341 }
3342
Bram Moolenaar7701f302018-10-02 21:20:32 +02003343 /* When a word wrapped from the previous line the start of the
3344 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003345 if (lnum == checked_lnum)
3346 cur_checked_col = checked_col;
3347 checked_lnum = 0;
3348
3349 /* When there was a sentence end in the previous line may require a
3350 * word starting with capital in this line. In line 1 always check
3351 * the first word. */
3352 if (lnum != capcol_lnum)
3353 cap_col = -1;
3354 if (lnum == 1)
3355 cap_col = 0;
3356 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358#endif
3359
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003360 /*
3361 * handle visual active in this window
3362 */
3363 fromcol = -10;
3364 tocol = MAXCOL;
3365 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 /* Visual is after curwin->w_cursor */
3368 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003370 top = &curwin->w_cursor;
3371 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003373 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003375 top = &VIsual;
3376 bot = &curwin->w_cursor;
3377 }
3378 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3379 if (VIsual_mode == Ctrl_V) /* block mode */
3380 {
3381 if (lnum_in_visual_area)
3382 {
3383 fromcol = wp->w_old_cursor_fcol;
3384 tocol = wp->w_old_cursor_lcol;
3385 }
3386 }
3387 else /* non-block mode */
3388 {
3389 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003391 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003393 if (VIsual_mode == 'V') /* linewise */
3394 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 else
3396 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003397 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3398 if (gchar_pos(top) == NUL)
3399 tocol = fromcol + 1;
3400 }
3401 }
3402 if (VIsual_mode != 'V' && lnum == bot->lnum)
3403 {
3404 if (*p_sel == 'e' && bot->col == 0
3405#ifdef FEAT_VIRTUALEDIT
3406 && bot->coladd == 0
3407#endif
3408 )
3409 {
3410 fromcol = -10;
3411 tocol = MAXCOL;
3412 }
3413 else if (bot->col == MAXCOL)
3414 tocol = MAXCOL;
3415 else
3416 {
3417 pos = *bot;
3418 if (*p_sel == 'e')
3419 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3420 else
3421 {
3422 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3423 ++tocol;
3424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426 }
3427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 /* Check if the character under the cursor should not be inverted */
3430 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003431#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003432 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003433#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003434 )
3435 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003437 /* if inverting in this line set area_highlighting */
3438 if (fromcol >= 0)
3439 {
3440 area_highlighting = TRUE;
3441 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003443 if ((clip_star.available && !clip_star.owned
3444 && clip_isautosel_star())
3445 || (clip_plus.available && !clip_plus.owned
3446 && clip_isautosel_plus()))
3447 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003452 /*
3453 * handle 'incsearch' and ":s///c" highlighting
3454 */
3455 else if (highlight_match
3456 && wp == curwin
3457 && lnum >= curwin->w_cursor.lnum
3458 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003460 if (lnum == curwin->w_cursor.lnum)
3461 getvcol(curwin, &(curwin->w_cursor),
3462 (colnr_T *)&fromcol, NULL, NULL);
3463 else
3464 fromcol = 0;
3465 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3466 {
3467 pos.lnum = lnum;
3468 pos.col = search_match_endcol;
3469 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3470 }
3471 else
3472 tocol = MAXCOL;
3473 /* do at least one character; happens when past end of line */
3474 if (fromcol == tocol)
3475 tocol = fromcol + 1;
3476 area_highlighting = TRUE;
3477 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480
3481#ifdef FEAT_DIFF
3482 filler_lines = diff_check(wp, lnum);
3483 if (filler_lines < 0)
3484 {
3485 if (filler_lines == -1)
3486 {
3487 if (diff_find_change(wp, lnum, &change_start, &change_end))
3488 diff_hlf = HLF_ADD; /* added line */
3489 else if (change_start == 0)
3490 diff_hlf = HLF_TXD; /* changed text */
3491 else
3492 diff_hlf = HLF_CHD; /* changed line */
3493 }
3494 else
3495 diff_hlf = HLF_ADD; /* added line */
3496 filler_lines = 0;
3497 area_highlighting = TRUE;
3498 }
3499 if (lnum == wp->w_topline)
3500 filler_lines = wp->w_topfill;
3501 filler_todo = filler_lines;
3502#endif
3503
3504#ifdef LINE_ATTR
3505# ifdef FEAT_SIGNS
3506 /* If this line has a sign with line highlighting set line_attr. */
3507 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3508 if (v != 0)
3509 line_attr = sign_get_attr((int)v, TRUE);
3510# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003511# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003513 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003514 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515# endif
3516 if (line_attr != 0)
3517 area_highlighting = TRUE;
3518#endif
3519
3520 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3521 ptr = line;
3522
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003523#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003524 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003525 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003526 /* For checking first word with a capital skip white space. */
3527 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003528 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003529
Bram Moolenaar30abd282005-06-22 22:35:10 +00003530 /* To be able to spell-check over line boundaries copy the end of the
3531 * current line into nextline[]. Above the start of the next line was
3532 * copied to nextline[SPWORDLEN]. */
3533 if (nextline[SPWORDLEN] == NUL)
3534 {
3535 /* No next line or it is empty. */
3536 nextlinecol = MAXCOL;
3537 nextline_idx = 0;
3538 }
3539 else
3540 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003541 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003542 if (v < SPWORDLEN)
3543 {
3544 /* Short line, use it completely and append the start of the
3545 * next line. */
3546 nextlinecol = 0;
3547 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003548 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003549 nextline_idx = v + 1;
3550 }
3551 else
3552 {
3553 /* Long line, use only the last SPWORDLEN bytes. */
3554 nextlinecol = v - SPWORDLEN;
3555 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3556 nextline_idx = SPWORDLEN + 1;
3557 }
3558 }
3559 }
3560#endif
3561
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003562 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003564 if (lcs_space || lcs_trail)
3565 extra_check = TRUE;
3566 /* find start of trailing whitespace */
3567 if (lcs_trail)
3568 {
3569 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003570 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003571 --trailcol;
3572 trailcol += (colnr_T) (ptr - line);
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
3576 /*
3577 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3578 * first character to be displayed.
3579 */
3580 if (wp->w_p_wrap)
3581 v = wp->w_skipcol;
3582 else
3583 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003584 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586#ifdef FEAT_MBYTE
3587 char_u *prev_ptr = ptr;
3588#endif
3589 while (vcol < v && *ptr != NUL)
3590 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003591 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 vcol += c;
3593#ifdef FEAT_MBYTE
3594 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003596 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003599 /* When:
3600 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003601 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003602 * - 'virtualedit' is set, or
3603 * - the visual mode is active,
3604 * the end of the line may be before the start of the displayed part.
3605 */
3606 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003607#ifdef FEAT_SYN_HL
3608 wp->w_p_cuc || draw_color_col ||
3609#endif
3610#ifdef FEAT_VIRTUALEDIT
3611 virtual_active() ||
3612#endif
3613 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
3618 /* Handle a character that's not completely on the screen: Put ptr at
3619 * that character but skip the first few screen characters. */
3620 if (vcol > v)
3621 {
3622 vcol -= c;
3623#ifdef FEAT_MBYTE
3624 ptr = prev_ptr;
3625#else
3626 --ptr;
3627#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003628 /* If the character fits on the screen, don't need to skip it.
3629 * Except for a TAB. */
3630 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003631#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003632 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003633#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003634 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003635 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637
3638 /*
3639 * Adjust for when the inverted text is before the screen,
3640 * and when the start of the inverted text is before the screen.
3641 */
3642 if (tocol <= vcol)
3643 fromcol = 0;
3644 else if (fromcol >= 0 && fromcol < vcol)
3645 fromcol = vcol;
3646
3647#ifdef FEAT_LINEBREAK
3648 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3649 if (wp->w_p_wrap)
3650 need_showbreak = TRUE;
3651#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003652#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003653 /* When spell checking a word we need to figure out the start of the
3654 * word and if it's badly spelled or not. */
3655 if (has_spell)
3656 {
3657 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003658 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003659 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003660
3661 pos = wp->w_cursor;
3662 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003663 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003664 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003665
3666 /* spell_move_to() may call ml_get() and make "line" invalid */
3667 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3668 ptr = line + linecol;
3669
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003670 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003671 {
3672 /* no bad word found at line start, don't check until end of a
3673 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003674 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003675 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003676 }
3677 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003678 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003679 /* bad word found, use attributes until end of word */
3680 word_end = wp->w_cursor.col + len + 1;
3681
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003682 /* Turn index into actual attributes. */
3683 if (spell_hlf != HLF_COUNT)
3684 spell_attr = highlight_attr[spell_hlf];
3685 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003686 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003687
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003688# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003689 /* Need to restart syntax highlighting for this line. */
3690 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003691 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003692# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003693 }
3694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696
3697 /*
3698 * Correct highlighting for cursor that can't be disabled.
3699 * Avoids having to check this for each character.
3700 */
3701 if (fromcol >= 0)
3702 {
3703 if (noinvcur)
3704 {
3705 if ((colnr_T)fromcol == wp->w_virtcol)
3706 {
3707 /* highlighting starts at cursor, let it start just after the
3708 * cursor */
3709 fromcol_prev = fromcol;
3710 fromcol = -1;
3711 }
3712 else if ((colnr_T)fromcol < wp->w_virtcol)
3713 /* restart highlighting after the cursor */
3714 fromcol_prev = wp->w_virtcol;
3715 }
3716 if (fromcol >= tocol)
3717 fromcol = -1;
3718 }
3719
3720#ifdef FEAT_SEARCH_EXTRA
3721 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003722 * Handle highlighting the last used search pattern and matches.
3723 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003725 cur = wp->w_match_head;
3726 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003727 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003729 if (shl_flag == FALSE)
3730 {
3731 shl = &search_hl;
3732 shl_flag = TRUE;
3733 }
3734 else
3735 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003736 shl->startcol = MAXCOL;
3737 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003739 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003740 v = (long)(ptr - line);
3741 if (cur != NULL)
3742 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003743 next_search_hl(wp, shl, lnum, (colnr_T)v,
3744 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003745
3746 /* Need to get the line again, a multi-line regexp may have made it
3747 * invalid. */
3748 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3749 ptr = line + v;
3750
3751 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003753 if (shl->lnum == lnum)
3754 shl->startcol = shl->rm.startpos[0].col;
3755 else
3756 shl->startcol = 0;
3757 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3758 - shl->rm.startpos[0].lnum)
3759 shl->endcol = shl->rm.endpos[0].col;
3760 else
3761 shl->endcol = MAXCOL;
3762 /* Highlight one character for an empty match. */
3763 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003766 if (has_mbyte && line[shl->endcol] != NUL)
3767 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3768 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003770 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003772 if ((long)shl->startcol < v) /* match at leftcol */
3773 {
3774 shl->attr_cur = shl->attr;
3775 search_attr = shl->attr;
3776 }
3777 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003779 if (shl != &search_hl && cur != NULL)
3780 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
3782#endif
3783
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003784#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003785 /* Cursor line highlighting for 'cursorline' in the current window. Not
3786 * when Visual mode is active, because it's not clear what is selected
3787 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003788 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003789 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003790 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003791 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003792 area_highlighting = TRUE;
3793 }
3794#endif
3795
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003796#ifdef FEAT_TEXT_PROP
3797 {
3798 char_u *prop_start;
3799
3800 text_prop_count = get_text_props(wp->w_buffer, lnum,
3801 &prop_start, FALSE);
3802 if (text_prop_count > 0)
3803 {
3804 // Make a copy of the properties, so that they are properly
3805 // aligned.
3806 text_props = (textprop_T *)alloc(
3807 text_prop_count * sizeof(textprop_T));
3808 if (text_props != NULL)
3809 mch_memmove(text_props, prop_start,
3810 text_prop_count * sizeof(textprop_T));
3811
3812 // Allocate an array for the indexes.
3813 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3814 area_highlighting = TRUE;
3815 extra_check = TRUE;
3816 }
3817 }
3818#endif
3819
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003820 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 col = 0;
3822#ifdef FEAT_RIGHTLEFT
3823 if (wp->w_p_rl)
3824 {
3825 /* Rightleft window: process the text in the normal direction, but put
3826 * it in current_ScreenLine[] from right to left. Start at the
3827 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003828 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 off += col;
3830 }
3831#endif
3832
3833 /*
3834 * Repeat for the whole displayed line.
3835 */
3836 for (;;)
3837 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003838#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003839 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 /* Skip this quickly when working on the text. */
3842 if (draw_state != WL_LINE)
3843 {
3844#ifdef FEAT_CMDWIN
3845 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3846 {
3847 draw_state = WL_CMDLINE;
3848 if (cmdwin_type != 0 && wp == curwin)
3849 {
3850 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003852 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003853 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 }
3856#endif
3857
3858#ifdef FEAT_FOLDING
3859 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3860 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003861 int fdc = compute_foldcolumn(wp, 0);
3862
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003864 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003866 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003867 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003868 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003869 p_extra_free = alloc(12 + 1);
3870
3871 if (p_extra_free != NULL)
3872 {
3873 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3874 n_extra = fdc;
3875 p_extra_free[n_extra] = NUL;
3876 p_extra = p_extra_free;
3877 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003878 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881 }
3882#endif
3883
3884#ifdef FEAT_SIGNS
3885 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3886 {
3887 draw_state = WL_SIGN;
3888 /* Show the sign column when there are any signs in this
3889 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003890 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003892 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003894 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895# endif
3896
3897 /* Draw two cells with the sign value or blank. */
3898 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003899 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 n_extra = 2;
3901
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003902 if (row == startrow
3903#ifdef FEAT_DIFF
3904 + filler_lines && filler_todo <= 0
3905#endif
3906 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3909 SIGN_TEXT);
3910# ifdef FEAT_SIGN_ICONS
3911 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3912 SIGN_ICON);
3913 if (gui.in_use && icon_sign != 0)
3914 {
3915 /* Use the image in this position. */
3916 c_extra = SIGN_BYTE;
3917# ifdef FEAT_NETBEANS_INTG
3918 if (buf_signcount(wp->w_buffer, lnum) > 1)
3919 c_extra = MULTISIGN_BYTE;
3920# endif
3921 char_attr = icon_sign;
3922 }
3923 else
3924# endif
3925 if (text_sign != 0)
3926 {
3927 p_extra = sign_get_text(text_sign);
3928 if (p_extra != NULL)
3929 {
3930 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003931 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933 char_attr = sign_get_attr(text_sign, FALSE);
3934 }
3935 }
3936 }
3937 }
3938#endif
3939
3940 if (draw_state == WL_NR - 1 && n_extra == 0)
3941 {
3942 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003943 /* Display the absolute or relative line number. After the
3944 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3945 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 && (row == startrow
3947#ifdef FEAT_DIFF
3948 + filler_lines
3949#endif
3950 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3951 {
3952 /* Draw the line number (empty space after wrapping). */
3953 if (row == startrow
3954#ifdef FEAT_DIFF
3955 + filler_lines
3956#endif
3957 )
3958 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003959 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003960 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003961
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003962 if (wp->w_p_nu && !wp->w_p_rnu)
3963 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003964 num = (long)lnum;
3965 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003966 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003967 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003968 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003969 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003970 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003971 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003972 num = lnum;
3973 fmt = "%-*ld ";
3974 }
3975 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003976
Bram Moolenaar700e7342013-01-30 12:31:36 +01003977 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003978 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (wp->w_skipcol > 0)
3980 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3981 *p_extra = '-';
3982#ifdef FEAT_RIGHTLEFT
3983 if (wp->w_p_rl) /* reverse line numbers */
3984 rl_mirror(extra);
3985#endif
3986 p_extra = extra;
3987 c_extra = NUL;
3988 }
3989 else
3990 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003991 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003992 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003993#ifdef FEAT_SYN_HL
3994 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003995 * the current line differently.
3996 * TODO: Can we use CursorLine instead of CursorLineNr
3997 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003998 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003999 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004000 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003 }
4004
Bram Moolenaar597a4222014-06-25 14:39:50 +02004005#ifdef FEAT_LINEBREAK
4006 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4007 && n_extra == 0 && *p_sbr != NUL)
4008 /* draw indent after showbreak value */
4009 draw_state = WL_BRI;
4010 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4011 /* After the showbreak, draw the breakindent */
4012 draw_state = WL_BRI - 1;
4013
4014 /* draw 'breakindent': indent wrapped text accordingly */
4015 if (draw_state == WL_BRI - 1 && n_extra == 0)
4016 {
4017 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004018 /* if need_showbreak is set, breakindent also applies */
4019 if (wp->w_p_bri && n_extra == 0
4020 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004021# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004022 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004023# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004024 )
4025 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004026 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004027# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004028 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004029 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004030 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004031# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004032 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4033 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004034 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004035# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004036 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004037# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004038 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004039 c_extra = ' ';
4040 n_extra = get_breakindent_win(wp,
4041 ml_get_buf(wp->w_buffer, lnum, FALSE));
4042 /* Correct end of highlighted area for 'breakindent',
4043 * required when 'linebreak' is also set. */
4044 if (tocol == vcol)
4045 tocol += n_extra;
4046 }
4047 }
4048#endif
4049
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4051 if (draw_state == WL_SBR - 1 && n_extra == 0)
4052 {
4053 draw_state = WL_SBR;
4054# ifdef FEAT_DIFF
4055 if (filler_todo > 0)
4056 {
4057 /* Draw "deleted" diff line(s). */
4058 if (char2cells(fill_diff) > 1)
4059 c_extra = '-';
4060 else
4061 c_extra = fill_diff;
4062# ifdef FEAT_RIGHTLEFT
4063 if (wp->w_p_rl)
4064 n_extra = col + 1;
4065 else
4066# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004067 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004068 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070# endif
4071# ifdef FEAT_LINEBREAK
4072 if (*p_sbr != NUL && need_showbreak)
4073 {
4074 /* Draw 'showbreak' at the start of each broken line. */
4075 p_extra = p_sbr;
4076 c_extra = NUL;
4077 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004078 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004080 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 /* Correct end of highlighted area for 'showbreak',
4082 * required when 'linebreak' is also set. */
4083 if (tocol == vcol)
4084 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004085#ifdef FEAT_SYN_HL
4086 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004087 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004088 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004089 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
4092# endif
4093 }
4094#endif
4095
4096 if (draw_state == WL_LINE - 1 && n_extra == 0)
4097 {
4098 draw_state = WL_LINE;
4099 if (saved_n_extra)
4100 {
4101 /* Continue item from end of wrapped line. */
4102 n_extra = saved_n_extra;
4103 c_extra = saved_c_extra;
4104 p_extra = saved_p_extra;
4105 char_attr = saved_char_attr;
4106 }
4107 else
4108 char_attr = 0;
4109 }
4110 }
4111
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004112 // When still displaying '$' of change command, stop at cursor.
4113 // When only displaying the (relative) line number and that's done,
4114 // stop here.
4115 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004116 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#ifdef FEAT_DIFF
4118 && filler_todo <= 0
4119#endif
4120 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004121 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004123 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004124 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004125 /* Pretend we have finished updating the window. Except when
4126 * 'cursorcolumn' is set. */
4127#ifdef FEAT_SYN_HL
4128 if (wp->w_p_cuc)
4129 row = wp->w_cline_row + wp->w_cline_height;
4130 else
4131#endif
4132 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134 }
4135
4136 if (draw_state == WL_LINE && area_highlighting)
4137 {
4138 /* handle Visual or match highlighting in this line */
4139 if (vcol == fromcol
4140#ifdef FEAT_MBYTE
4141 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4142 && (*mb_ptr2cells)(ptr) > 1)
4143#endif
4144 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004145 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 && vcol < tocol))
4147 area_attr = attr; /* start highlighting */
4148 else if (area_attr != 0
4149 && (vcol == tocol
4150 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153#ifdef FEAT_SEARCH_EXTRA
4154 if (!n_extra)
4155 {
4156 /*
4157 * Check for start/end of search pattern match.
4158 * After end, check for start/end of next match.
4159 * When another match, have to check for start again.
4160 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004161 * Do this for 'search_hl' and the match list (ordered by
4162 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004164 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004165 cur = wp->w_match_head;
4166 shl_flag = FALSE;
4167 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004169 if (shl_flag == FALSE
4170 && ((cur != NULL
4171 && cur->priority > SEARCH_HL_PRIORITY)
4172 || cur == NULL))
4173 {
4174 shl = &search_hl;
4175 shl_flag = TRUE;
4176 }
4177 else
4178 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004179 if (cur != NULL)
4180 cur->pos.cur = 0;
4181 pos_inprogress = TRUE;
4182 while (shl->rm.regprog != NULL
4183 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004185 if (shl->startcol != MAXCOL
4186 && v >= (long)shl->startcol
4187 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004189#ifdef FEAT_MBYTE
4190 int tmp_col = v + MB_PTR2LEN(ptr);
4191
4192 if (shl->endcol < tmp_col)
4193 shl->endcol = tmp_col;
4194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004196#ifdef FEAT_CONCEAL
4197 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4198 == cur->hlg_id)
4199 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004200 has_match_conc =
4201 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004202 match_conc = cur->conceal_char;
4203 }
4204 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004205 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004208 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004211 next_search_hl(wp, shl, lnum, (colnr_T)v,
4212 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004213 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004214 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 /* Need to get the line again, a multi-line regexp
4217 * may have made it invalid. */
4218 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4219 ptr = line + v;
4220
4221 if (shl->lnum == lnum)
4222 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004223 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004225 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004227 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004229 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
4231 /* highlight empty match, try again after
4232 * it */
4233#ifdef FEAT_MBYTE
4234 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004235 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004236 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 else
4238#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004239 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241
4242 /* Loop to check if the match starts at the
4243 * current position */
4244 continue;
4245 }
4246 }
4247 break;
4248 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004249 if (shl != &search_hl && cur != NULL)
4250 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004252
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004253 /* Use attributes from match with highest priority among
4254 * 'search_hl' and the match list. */
4255 search_attr = search_hl.attr_cur;
4256 cur = wp->w_match_head;
4257 shl_flag = FALSE;
4258 while (cur != NULL || shl_flag == FALSE)
4259 {
4260 if (shl_flag == FALSE
4261 && ((cur != NULL
4262 && cur->priority > SEARCH_HL_PRIORITY)
4263 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004264 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004265 shl = &search_hl;
4266 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004267 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004268 else
4269 shl = &cur->hl;
4270 if (shl->attr_cur != 0)
4271 search_attr = shl->attr_cur;
4272 if (shl != &search_hl && cur != NULL)
4273 cur = cur->next;
4274 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004275 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004276 if (*ptr == NUL && (did_line_attr >= 1
4277 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004278 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280#endif
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004283 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004285 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4286 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004288 if (diff_hlf == HLF_TXD && ptr - line > change_end
4289 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004291 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004292 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004293 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004296
4297 /* Decide which of the highlight attributes to use. */
4298 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004299#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004300 if (area_attr != 0)
4301 char_attr = hl_combine_attr(line_attr, area_attr);
4302 else if (search_attr != 0)
4303 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004304 /* Use line_attr when not in the Visual or 'incsearch' area
4305 * (area_attr may be 0 when "noinvcur" is set). */
4306 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004307 || vcol < fromcol || vcol_prev < fromcol_prev
4308 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004309 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004310#else
4311 if (area_attr != 0)
4312 char_attr = area_attr;
4313 else if (search_attr != 0)
4314 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004315#endif
4316 else
4317 {
4318 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004319#ifdef FEAT_TEXT_PROP
4320 if (text_prop_type != NULL)
4321 char_attr = text_prop_attr;
4322 else
4323#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004324#ifdef FEAT_SYN_HL
4325 if (has_syntax)
4326 char_attr = syntax_attr;
4327 else
4328#endif
4329 char_attr = 0;
4330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332
4333 /*
4334 * Get the next character to put on the screen.
4335 */
4336 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004337 * The "p_extra" points to the extra stuff that is inserted to
4338 * represent special characters (non-printable stuff) and other
4339 * things. When all characters are the same, c_extra is used.
4340 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4341 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4343 */
4344 if (n_extra > 0)
4345 {
4346 if (c_extra != NUL)
4347 {
4348 c = c_extra;
4349#ifdef FEAT_MBYTE
4350 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004351 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004354 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004355 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 else
4358 mb_utf8 = FALSE;
4359#endif
4360 }
4361 else
4362 {
4363 c = *p_extra;
4364#ifdef FEAT_MBYTE
4365 if (has_mbyte)
4366 {
4367 mb_c = c;
4368 if (enc_utf8)
4369 {
4370 /* If the UTF-8 character is more than one byte:
4371 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004372 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 mb_utf8 = FALSE;
4374 if (mb_l > n_extra)
4375 mb_l = 1;
4376 else if (mb_l > 1)
4377 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004378 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004380 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382 }
4383 else
4384 {
4385 /* if this is a DBCS character, put it in "mb_c" */
4386 mb_l = MB_BYTE2LEN(c);
4387 if (mb_l >= n_extra)
4388 mb_l = 1;
4389 else if (mb_l > 1)
4390 mb_c = (c << 8) + p_extra[1];
4391 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004392 if (mb_l == 0) /* at the NUL at end-of-line */
4393 mb_l = 1;
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 /* If a double-width char doesn't fit display a '>' in the
4396 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004397 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398# ifdef FEAT_RIGHTLEFT
4399 wp->w_p_rl ? (col <= 0) :
4400# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004401 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 && (*mb_char2cells)(mb_c) == 2)
4403 {
4404 c = '>';
4405 mb_c = c;
4406 mb_l = 1;
4407 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004408 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 /* put the pointer back to output the double-width
4410 * character at the start of the next line. */
4411 ++n_extra;
4412 --p_extra;
4413 }
4414 else
4415 {
4416 n_extra -= mb_l - 1;
4417 p_extra += mb_l - 1;
4418 }
4419 }
4420#endif
4421 ++p_extra;
4422 }
4423 --n_extra;
4424 }
4425 else
4426 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004427#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004428 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004429#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004430
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004431 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004432 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 /*
4434 * Get a character from the line itself.
4435 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004436 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004437#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004438 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#ifdef FEAT_MBYTE
4441 if (has_mbyte)
4442 {
4443 mb_c = c;
4444 if (enc_utf8)
4445 {
4446 /* If the UTF-8 character is more than one byte: Decode it
4447 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004448 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 mb_utf8 = FALSE;
4450 if (mb_l > 1)
4451 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004452 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 /* Overlong encoded ASCII or ASCII with composing char
4454 * is displayed normally, except a NUL. */
4455 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004456 {
4457 c = mb_c;
4458# ifdef FEAT_LINEBREAK
4459 c0 = mb_c;
4460# endif
4461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004463
4464 /* At start of the line we can have a composing char.
4465 * Draw it as a space with a composing char. */
4466 if (utf_iscomposing(mb_c))
4467 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004468 int i;
4469
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004470 for (i = Screen_mco - 1; i > 0; --i)
4471 u8cc[i] = u8cc[i - 1];
4472 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004473 mb_c = ' ';
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476
4477 if ((mb_l == 1 && c >= 0x80)
4478 || (mb_l >= 1 && mb_c == 0)
4479 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004480# ifdef UNICODE16
4481 || mb_c >= 0x10000
4482# endif
4483 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
4485 /*
4486 * Illegal UTF-8 byte: display as <xx>.
4487 * Non-BMP character : display as ? or fullwidth ?.
4488 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004489# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
4493 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004494# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (wp->w_p_rl) /* reverse */
4496 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004499# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 else if (utf_char2cells(mb_c) != 2)
4501 STRCPY(extra, "?");
4502 else
4503 /* 0xff1f in UTF-8: full-width '?' */
4504 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 p_extra = extra;
4508 c = *p_extra;
4509 mb_c = mb_ptr2char_adv(&p_extra);
4510 mb_utf8 = (c >= 0x80);
4511 n_extra = (int)STRLEN(p_extra);
4512 c_extra = NUL;
4513 if (area_attr == 0 && search_attr == 0)
4514 {
4515 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004516 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 saved_attr2 = char_attr; /* save current attr */
4518 }
4519 }
4520 else if (mb_l == 0) /* at the NUL at end-of-line */
4521 mb_l = 1;
4522#ifdef FEAT_ARABIC
4523 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4524 {
4525 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004526 int pc, pc1, nc;
4527 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 /* The idea of what is the previous and next
4530 * character depends on 'rightleft'. */
4531 if (wp->w_p_rl)
4532 {
4533 pc = prev_c;
4534 pc1 = prev_c1;
4535 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004536 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538 else
4539 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004540 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004542 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544 prev_c = mb_c;
4545
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004546 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548 else
4549 prev_c = mb_c;
4550#endif
4551 }
4552 else /* enc_dbcs */
4553 {
4554 mb_l = MB_BYTE2LEN(c);
4555 if (mb_l == 0) /* at the NUL at end-of-line */
4556 mb_l = 1;
4557 else if (mb_l > 1)
4558 {
4559 /* We assume a second byte below 32 is illegal.
4560 * Hopefully this is OK for all double-byte encodings!
4561 */
4562 if (ptr[1] >= 32)
4563 mb_c = (c << 8) + ptr[1];
4564 else
4565 {
4566 if (ptr[1] == NUL)
4567 {
4568 /* head byte at end of line */
4569 mb_l = 1;
4570 transchar_nonprint(extra, c);
4571 }
4572 else
4573 {
4574 /* illegal tail byte */
4575 mb_l = 2;
4576 STRCPY(extra, "XX");
4577 }
4578 p_extra = extra;
4579 n_extra = (int)STRLEN(extra) - 1;
4580 c_extra = NUL;
4581 c = *p_extra++;
4582 if (area_attr == 0 && search_attr == 0)
4583 {
4584 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004585 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 saved_attr2 = char_attr; /* save current attr */
4587 }
4588 mb_c = c;
4589 }
4590 }
4591 }
4592 /* If a double-width char doesn't fit display a '>' in the
4593 * last column; the character is displayed at the start of the
4594 * next line. */
4595 if ((
4596# ifdef FEAT_RIGHTLEFT
4597 wp->w_p_rl ? (col <= 0) :
4598# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004599 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 && (*mb_char2cells)(mb_c) == 2)
4601 {
4602 c = '>';
4603 mb_c = c;
4604 mb_utf8 = FALSE;
4605 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004606 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /* Put pointer back so that the character will be
4608 * displayed at the start of the next line. */
4609 --ptr;
4610 }
4611 else if (*ptr != NUL)
4612 ptr += mb_l - 1;
4613
4614 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004615 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004616 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004617 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004620 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 c = ' ';
4622 if (area_attr == 0 && search_attr == 0)
4623 {
4624 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004625 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 saved_attr2 = char_attr; /* save current attr */
4627 }
4628 mb_c = c;
4629 mb_utf8 = FALSE;
4630 mb_l = 1;
4631 }
4632
4633 }
4634#endif
4635 ++ptr;
4636
4637 if (extra_check)
4638 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004639#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004640 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004641#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004642
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004643#ifdef FEAT_TERMINAL
4644 if (get_term_attr)
4645 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004646 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004647
4648 if (!attr_pri)
4649 char_attr = syntax_attr;
4650 else
4651 char_attr = hl_combine_attr(syntax_attr, char_attr);
4652 }
4653#endif
4654
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004655#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 /* Get syntax attribute, unless still at the start of the line
4657 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004658 v = (long)(ptr - line);
4659 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
4661 /* Get the syntax attribute for the character. If there
4662 * is an error, disable syntax highlighting. */
4663 save_did_emsg = did_emsg;
4664 did_emsg = FALSE;
4665
Bram Moolenaar217ad922005-03-20 22:37:15 +00004666 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004667# ifdef FEAT_SPELL
4668 has_spell ? &can_spell :
4669# endif
4670 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004673 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004674 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004675 has_syntax = FALSE;
4676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 else
4678 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004679#ifdef SYN_TIME_LIMIT
4680 if (wp->w_s->b_syn_slow)
4681 has_syntax = FALSE;
4682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
4684 /* Need to get the line again, a multi-line regexp may
4685 * have made it invalid. */
4686 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4687 ptr = line + v;
4688
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004689 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004691 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004692 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004693# ifdef FEAT_CONCEAL
4694 /* no concealing past the end of the line, it interferes
4695 * with line highlighting */
4696 if (c == NUL)
4697 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004698 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004699 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004702#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004703
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004704#ifdef FEAT_TEXT_PROP
4705 if (text_props != NULL)
4706 {
4707 int pi;
4708
4709 // Check if any active property ends.
4710 for (pi = 0; pi < text_props_active; ++pi)
4711 {
4712 int tpi = text_prop_idxs[pi];
4713
4714 if (col >= text_props[tpi].tp_col - 1
4715 + text_props[tpi].tp_len)
4716 {
4717 if (pi + 1 < text_props_active)
4718 mch_memmove(text_prop_idxs + pi,
4719 text_prop_idxs + pi + 1,
4720 sizeof(int)
4721 * (text_props_active - (pi + 1)));
4722 --text_props_active;
4723 --pi;
4724 }
4725 }
4726
4727 // Add any text property that starts in this column.
4728 while (text_prop_next < text_prop_count
4729 && col >= text_props[text_prop_next].tp_col - 1)
4730 text_prop_idxs[text_props_active++] = text_prop_next++;
4731
4732 text_prop_type = NULL;
4733 if (text_props_active > 0)
4734 {
4735 int max_priority = INT_MIN;
4736 int max_col = 0;
4737
4738 // Get the property type with the highest priority
4739 // and/or starting last.
4740 for (pi = 0; pi < text_props_active; ++pi)
4741 {
4742 int tpi = text_prop_idxs[pi];
4743 proptype_T *pt;
4744
4745 pt = text_prop_type_by_id(
4746 curwin->w_buffer, text_props[tpi].tp_type);
4747 if (pt != NULL
4748 && (pt->pt_priority > max_priority
4749 || (pt->pt_priority == max_priority
4750 && text_props[tpi].tp_col >= max_col)))
4751 {
4752 text_prop_type = pt;
4753 max_priority = pt->pt_priority;
4754 max_col = text_props[tpi].tp_col;
4755 }
4756 }
4757 if (text_prop_type != NULL)
4758 text_prop_attr =
4759 syn_id2attr(text_prop_type->pt_hl_id);
4760 }
4761 }
4762#endif
4763
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004764#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004765 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004766 * Only do this when there is no syntax highlighting, the
4767 * @Spell cluster is not used or the current syntax item
4768 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004769 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004770 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004771 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004772# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004773 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004774 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004775# endif
4776 if (c != 0 && (
4777# ifdef FEAT_SYN_HL
4778 !has_syntax ||
4779# endif
4780 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004781 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004782 char_u *prev_ptr, *p;
4783 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004784 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004785# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004786 if (has_mbyte)
4787 {
4788 prev_ptr = ptr - mb_l;
4789 v -= mb_l - 1;
4790 }
4791 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004792# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004793 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004794
4795 /* Use nextline[] if possible, it has the start of the
4796 * next line concatenated. */
4797 if ((prev_ptr - line) - nextlinecol >= 0)
4798 p = nextline + (prev_ptr - line) - nextlinecol;
4799 else
4800 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004801 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004802 len = spell_check(wp, p, &spell_hlf, &cap_col,
4803 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004804 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004805
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004806 /* In Insert mode only highlight a word that
4807 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004808 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004809 && (State & INSERT) != 0
4810 && wp->w_cursor.lnum == lnum
4811 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004812 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004813 && wp->w_cursor.col < (colnr_T)word_end)
4814 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004815 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004816 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004817 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004818
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004819 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004820 && (p - nextline) + len > nextline_idx)
4821 {
4822 /* Remember that the good word continues at the
4823 * start of the next line. */
4824 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004825 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004826 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004827
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004828 /* Turn index into actual attributes. */
4829 if (spell_hlf != HLF_COUNT)
4830 spell_attr = highlight_attr[spell_hlf];
4831
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004832 if (cap_col > 0)
4833 {
4834 if (p != prev_ptr
4835 && (p - nextline) + cap_col >= nextline_idx)
4836 {
4837 /* Remember that the word in the next line
4838 * must start with a capital. */
4839 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004840 cap_col = (int)((p - nextline) + cap_col
4841 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004842 }
4843 else
4844 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004845 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004846 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004847 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004848 }
4849 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004850 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004851 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004852 char_attr = hl_combine_attr(char_attr, spell_attr);
4853 else
4854 char_attr = hl_combine_attr(spell_attr, char_attr);
4855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856#endif
4857#ifdef FEAT_LINEBREAK
4858 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004859 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004861 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004862 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004864# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004865 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004866# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004867 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004869 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004871 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004872
Bram Moolenaar597a4222014-06-25 14:39:50 +02004873 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004874 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004875 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004876 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004877# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004878 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004879 wp->w_buffer->b_p_vts_array) - 1;
4880# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004881 n_extra = (int)wp->w_buffer->b_p_ts
4882 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004883# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004884
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004885# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004886 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004887# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004889# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004890 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004891 {
4892#ifdef FEAT_CONCEAL
4893 if (c == TAB)
4894 /* See "Tab alignment" below. */
4895 FIX_FOR_BOGUSCOLS;
4896#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004897 if (!wp->w_p_list)
4898 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901#endif
4902
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004903 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4904 */
4905 if (wp->w_p_list
4906 && (((c == 160
4907#ifdef FEAT_MBYTE
4908 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4909#endif
4910 ) && lcs_nbsp)
4911 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4912 {
4913 c = (c == ' ') ? lcs_space : lcs_nbsp;
4914 if (area_attr == 0 && search_attr == 0)
4915 {
4916 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004917 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004918 saved_attr2 = char_attr; /* save current attr */
4919 }
4920#ifdef FEAT_MBYTE
4921 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004922 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004923 {
4924 mb_utf8 = TRUE;
4925 u8cc[0] = 0;
4926 c = 0xc0;
4927 }
4928 else
4929 mb_utf8 = FALSE;
4930#endif
4931 }
4932
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4934 {
4935 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004936 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
4938 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004939 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 saved_attr2 = char_attr; /* save current attr */
4941 }
4942#ifdef FEAT_MBYTE
4943 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004944 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
4946 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004947 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004948 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950 else
4951 mb_utf8 = FALSE;
4952#endif
4953 }
4954 }
4955
4956 /*
4957 * Handling of non-printable characters.
4958 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004959 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
4961 /*
4962 * when getting a character from the file, we may have to
4963 * turn it into something else on the way to putting it
4964 * into "ScreenLines".
4965 */
4966 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4967 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004968 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004969 long vcol_adjusted = vcol; /* removed showbreak length */
4970#ifdef FEAT_LINEBREAK
4971 /* only adjust the tab_len, when at the first column
4972 * after the showbreak value was drawn */
4973 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4974 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004977#ifdef FEAT_VARTABS
4978 tab_len = tabstop_padding(vcol_adjusted,
4979 wp->w_buffer->b_p_ts,
4980 wp->w_buffer->b_p_vts_array) - 1;
4981#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004982 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004983 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4984#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004985
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004986#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004987 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004988#endif
4989 /* tab amount depends on current column */
4990 n_extra = tab_len;
4991#ifdef FEAT_LINEBREAK
4992 else
4993 {
4994 char_u *p;
4995 int len = n_extra;
4996 int i;
4997 int saved_nextra = n_extra;
4998
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004999#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005000 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005001 /* there are characters to conceal */
5002 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005003 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5004 */
5005 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5006 && n_extra > tab_len)
5007 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005008#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005009
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005010 /* if n_extra > 0, it gives the number of chars, to
5011 * use for a tab, else we need to calculate the width
5012 * for a tab */
5013#ifdef FEAT_MBYTE
5014 len = (tab_len * mb_char2len(lcs_tab2));
5015 if (n_extra > 0)
5016 len += n_extra - tab_len;
5017#endif
5018 c = lcs_tab1;
5019 p = alloc((unsigned)(len + 1));
5020 vim_memset(p, ' ', len);
5021 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005022 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005023 p_extra_free = p;
5024 for (i = 0; i < tab_len; i++)
5025 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005026 if (*p == NUL)
5027 {
5028 tab_len = i;
5029 break;
5030 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005031#ifdef FEAT_MBYTE
5032 mb_char2bytes(lcs_tab2, p);
5033 p += mb_char2len(lcs_tab2);
5034 n_extra += mb_char2len(lcs_tab2)
5035 - (saved_nextra > 0 ? 1 : 0);
5036#else
5037 p[i] = lcs_tab2;
5038#endif
5039 }
5040 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005041#ifdef FEAT_CONCEAL
5042 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5043 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005044 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005045 n_extra -= vcol_off;
5046#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005047 }
5048#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005049#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005050 {
5051 int vc_saved = vcol_off;
5052
5053 /* Tab alignment should be identical regardless of
5054 * 'conceallevel' value. So tab compensates of all
5055 * previous concealed characters, and thus resets
5056 * vcol_off and boguscols accumulated so far in the
5057 * line. Note that the tab can be longer than
5058 * 'tabstop' when there are concealed characters. */
5059 FIX_FOR_BOGUSCOLS;
5060
5061 /* Make sure, the highlighting for the tab char will be
5062 * correctly set further below (effectively reverts the
5063 * FIX_FOR_BOGSUCOLS macro */
5064 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005065 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005066 tab_len += vc_saved;
5067 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069#ifdef FEAT_MBYTE
5070 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5071#endif
5072 if (wp->w_p_list)
5073 {
5074 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005075#ifdef FEAT_LINEBREAK
5076 if (wp->w_p_lbr)
5077 c_extra = NUL; /* using p_extra from above */
5078 else
5079#endif
5080 c_extra = lcs_tab2;
5081 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005082 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 saved_attr2 = char_attr; /* save current attr */
5084#ifdef FEAT_MBYTE
5085 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005086 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005089 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005090 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092#endif
5093 }
5094 else
5095 {
5096 c_extra = ' ';
5097 c = ' ';
5098 }
5099 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005100 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005101 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005102 || ((fromcol >= 0 || fromcol_prev >= 0)
5103 && tocol > vcol
5104 && VIsual_mode != Ctrl_V
5105 && (
5106# ifdef FEAT_RIGHTLEFT
5107 wp->w_p_rl ? (col >= 0) :
5108# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005109 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005110 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005111 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005112 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005113 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005115 /* Display a '$' after the line or highlight an extra
5116 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5118 /* For a diff line the highlighting continues after the
5119 * "$". */
5120 if (
5121# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005122 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123# ifdef LINE_ATTR
5124 &&
5125# endif
5126# endif
5127# ifdef LINE_ATTR
5128 line_attr == 0
5129# endif
5130 )
5131#endif
5132 {
5133#ifdef FEAT_VIRTUALEDIT
5134 /* In virtualedit, visual selections may extend
5135 * beyond end of line. */
5136 if (area_highlighting && virtual_active()
5137 && tocol != MAXCOL && vcol < tocol)
5138 n_extra = 0;
5139 else
5140#endif
5141 {
5142 p_extra = at_end_str;
5143 n_extra = 1;
5144 c_extra = NUL;
5145 }
5146 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005147 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005148 c = lcs_eol;
5149 else
5150 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 lcs_eol_one = -1;
5152 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005153 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005155 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 n_attr = 1;
5157 }
5158#ifdef FEAT_MBYTE
5159 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005160 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
5162 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005163 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005164 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166 else
5167 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5168#endif
5169 }
5170 else if (c != NUL)
5171 {
5172 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005173 if (n_extra == 0)
5174 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175#ifdef FEAT_RIGHTLEFT
5176 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5177 rl_mirror(p_extra); /* reverse "<12>" */
5178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005180#ifdef FEAT_LINEBREAK
5181 if (wp->w_p_lbr)
5182 {
5183 char_u *p;
5184
5185 c = *p_extra;
5186 p = alloc((unsigned)n_extra + 1);
5187 vim_memset(p, ' ', n_extra);
5188 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5189 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005190 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005191 p_extra_free = p_extra = p;
5192 }
5193 else
5194#endif
5195 {
5196 n_extra = byte2cells(c) - 1;
5197 c = *p_extra++;
5198 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005199 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
5201 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005202 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 saved_attr2 = char_attr; /* save current attr */
5204 }
5205#ifdef FEAT_MBYTE
5206 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5207#endif
5208 }
5209#ifdef FEAT_VIRTUALEDIT
5210 else if (VIsual_active
5211 && (VIsual_mode == Ctrl_V
5212 || VIsual_mode == 'v')
5213 && virtual_active()
5214 && tocol != MAXCOL
5215 && vcol < tocol
5216 && (
5217# ifdef FEAT_RIGHTLEFT
5218 wp->w_p_rl ? (col >= 0) :
5219# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005220 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 c = ' ';
5223 --ptr; /* put it back at the NUL */
5224 }
5225#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005226#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 else if ((
5228# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005229 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005231# ifdef FEAT_TERMINAL
5232 term_attr != 0 ||
5233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 ) && (
5236# ifdef FEAT_RIGHTLEFT
5237 wp->w_p_rl ? (col >= 0) :
5238# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005239 (col
5240# ifdef FEAT_CONCEAL
5241 - boguscols
5242# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005243 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 {
5245 /* Highlight until the right side of the window */
5246 c = ' ';
5247 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005248
5249 /* Remember we do the char for line highlighting. */
5250 ++did_line_attr;
5251
5252 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005253 if (line_attr != 0 && char_attr == search_attr
5254 && (did_line_attr > 1
5255 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005256 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257# ifdef FEAT_DIFF
5258 if (diff_hlf == HLF_TXD)
5259 {
5260 diff_hlf = HLF_CHD;
5261 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005262 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005263 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005264 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5265 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005266 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
5269# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005270# ifdef FEAT_TERMINAL
5271 if (term_attr != 0)
5272 {
5273 char_attr = term_attr;
5274 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5275 char_attr = hl_combine_attr(char_attr,
5276 HL_ATTR(HLF_CUL));
5277 }
5278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280#endif
5281 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005282
5283#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005284 if ( wp->w_p_cole > 0
5285 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005286 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005287 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005288 && !(lnum_in_visual_area
5289 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005290 {
5291 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005292 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005293 && (syn_get_sub_char() != NUL || match_conc
5294 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005295 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005296 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005297 /* First time at this concealed item: display one
5298 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005299 if (match_conc)
5300 c = match_conc;
5301 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005302 c = syn_get_sub_char();
5303 else if (lcs_conceal != NUL)
5304 c = lcs_conceal;
5305 else
5306 c = ' ';
5307
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005308 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005309
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005310 if (n_extra > 0)
5311 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005312 vcol += n_extra;
5313 if (wp->w_p_wrap && n_extra > 0)
5314 {
5315# ifdef FEAT_RIGHTLEFT
5316 if (wp->w_p_rl)
5317 {
5318 col -= n_extra;
5319 boguscols -= n_extra;
5320 }
5321 else
5322# endif
5323 {
5324 boguscols += n_extra;
5325 col += n_extra;
5326 }
5327 }
5328 n_extra = 0;
5329 n_attr = 0;
5330 }
5331 else if (n_skip == 0)
5332 {
5333 is_concealing = TRUE;
5334 n_skip = 1;
5335 }
5336# ifdef FEAT_MBYTE
5337 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005338 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005339 {
5340 mb_utf8 = TRUE;
5341 u8cc[0] = 0;
5342 c = 0xc0;
5343 }
5344 else
5345 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5346# endif
5347 }
5348 else
5349 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005350 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005351 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005352 }
5353#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005356#ifdef FEAT_CONCEAL
5357 /* In the cursor line and we may be concealing characters: correct
5358 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005359 if (!did_wcol && draw_state == WL_LINE
5360 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005361 && conceal_cursor_line(wp)
5362 && (int)wp->w_virtcol <= vcol + n_skip)
5363 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005364# ifdef FEAT_RIGHTLEFT
5365 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005366 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005367 else
5368# endif
5369 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005370 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005371 did_wcol = TRUE;
5372 }
5373#endif
5374
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 /* Don't override visual selection highlighting. */
5376 if (n_attr > 0
5377 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005378 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 char_attr = extra_attr;
5380
Bram Moolenaar81695252004-12-29 20:58:21 +00005381#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 /* XIM don't send preedit_start and preedit_end, but they send
5383 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5384 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005385 if (p_imst == IM_ON_THE_SPOT
5386 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005387 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 && (State & INSERT)
5389 && !p_imdisable
5390 && im_is_preediting()
5391 && draw_state == WL_LINE)
5392 {
5393 colnr_T tcol;
5394
5395 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005396 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 else
5398 tcol = preedit_end_col;
5399 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5400 {
5401 if (feedback_old_attr < 0)
5402 {
5403 feedback_col = 0;
5404 feedback_old_attr = char_attr;
5405 }
5406 char_attr = im_get_feedback_attr(feedback_col);
5407 if (char_attr < 0)
5408 char_attr = feedback_old_attr;
5409 feedback_col++;
5410 }
5411 else if (feedback_old_attr >= 0)
5412 {
5413 char_attr = feedback_old_attr;
5414 feedback_old_attr = -1;
5415 feedback_col = 0;
5416 }
5417 }
5418#endif
5419 /*
5420 * Handle the case where we are in column 0 but not on the first
5421 * character of the line and the user wants us to show us a
5422 * special character (via 'listchars' option "precedes:<char>".
5423 */
5424 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005425 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5427#ifdef FEAT_DIFF
5428 && filler_todo <= 0
5429#endif
5430 && draw_state > WL_NR
5431 && c != NUL)
5432 {
5433 c = lcs_prec;
5434 lcs_prec_todo = NUL;
5435#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005436 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5437 {
5438 /* Double-width character being overwritten by the "precedes"
5439 * character, need to fill up half the character. */
5440 c_extra = MB_FILLER_CHAR;
5441 n_extra = 1;
5442 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005443 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005446 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
5448 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005449 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005450 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
5452 else
5453 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5454#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005455 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
5457 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005458 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 n_attr3 = 1;
5460 }
5461 }
5462
5463 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005464 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005466 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005467#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005468 || did_line_attr == 1
5469#endif
5470 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005472#ifdef FEAT_SEARCH_EXTRA
5473 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005474
5475 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005476 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005477 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005478#endif
5479
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005480 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 * highlight match at end of line. If it's beyond the last
5482 * char on the screen, just overwrite that one (tricky!) Not
5483 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005484#ifdef FEAT_SEARCH_EXTRA
5485 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005486 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005487 prevcol_hl_flag = TRUE;
5488 else
5489 {
5490 cur = wp->w_match_head;
5491 while (cur != NULL)
5492 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005493 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005494 {
5495 prevcol_hl_flag = TRUE;
5496 break;
5497 }
5498 cur = cur->next;
5499 }
5500 }
5501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005503 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005504 && (VIsual_mode != Ctrl_V
5505 || lnum == VIsual.lnum
5506 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005507 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508#ifdef FEAT_SEARCH_EXTRA
5509 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005510 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005511# ifdef FEAT_SYN_HL
5512 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5513 && !(wp == curwin && VIsual_active))
5514# endif
5515# ifdef FEAT_DIFF
5516 && diff_hlf == (hlf_T)0
5517# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005518# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005519 && did_line_attr <= 1
5520# endif
5521 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522#endif
5523 ))
5524 {
5525 int n = 0;
5526
5527#ifdef FEAT_RIGHTLEFT
5528 if (wp->w_p_rl)
5529 {
5530 if (col < 0)
5531 n = 1;
5532 }
5533 else
5534#endif
5535 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005536 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 n = -1;
5538 }
5539 if (n != 0)
5540 {
5541 /* At the window boundary, highlight the last character
5542 * instead (better than nothing). */
5543 off += n;
5544 col += n;
5545 }
5546 else
5547 {
5548 /* Add a blank character to highlight. */
5549 ScreenLines[off] = ' ';
5550#ifdef FEAT_MBYTE
5551 if (enc_utf8)
5552 ScreenLinesUC[off] = 0;
5553#endif
5554 }
5555#ifdef FEAT_SEARCH_EXTRA
5556 if (area_attr == 0)
5557 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005558 /* Use attributes from match with highest priority among
5559 * 'search_hl' and the match list. */
5560 char_attr = search_hl.attr;
5561 cur = wp->w_match_head;
5562 shl_flag = FALSE;
5563 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005564 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005565 if (shl_flag == FALSE
5566 && ((cur != NULL
5567 && cur->priority > SEARCH_HL_PRIORITY)
5568 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005569 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005570 shl = &search_hl;
5571 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005572 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005573 else
5574 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005575 if ((ptr - line) - 1 == (long)shl->startcol
5576 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005577 char_attr = shl->attr;
5578 if (shl != &search_hl && cur != NULL)
5579 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582#endif
5583 ScreenAttrs[off] = char_attr;
5584#ifdef FEAT_RIGHTLEFT
5585 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005588 --off;
5589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 else
5591#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005594 ++off;
5595 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005596 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005597#ifdef FEAT_SYN_HL
5598 eol_hl_off = 1;
5599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
Bram Moolenaar91170f82006-05-05 21:15:17 +00005603 /*
5604 * At end of the text line.
5605 */
5606 if (c == NUL)
5607 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005608#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005609 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005610 if (wp->w_p_wrap)
5611 v = wp->w_skipcol;
5612 else
5613 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005614
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005615 /* check if line ends before left margin */
5616 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005617 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005618#ifdef FEAT_CONCEAL
5619 /* Get rid of the boguscols now, we want to draw until the right
5620 * edge for 'cursorcolumn'. */
5621 col -= boguscols;
5622 boguscols = 0;
5623#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005624
5625 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005626 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005627
5628 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005629 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5630 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005631 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005632 && lnum != wp->w_cursor.lnum)
5633 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005634# ifdef FEAT_RIGHTLEFT
5635 && !wp->w_p_rl
5636# endif
5637 )
5638 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005639 int rightmost_vcol = 0;
5640 int i;
5641
5642 if (wp->w_p_cuc)
5643 rightmost_vcol = wp->w_virtcol;
5644 if (draw_color_col)
5645 /* determine rightmost colorcolumn to possibly draw */
5646 for (i = 0; color_cols[i] >= 0; ++i)
5647 if (rightmost_vcol < color_cols[i])
5648 rightmost_vcol = color_cols[i];
5649
Bram Moolenaar02631462017-09-22 15:20:32 +02005650 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005651 {
5652 ScreenLines[off] = ' ';
5653#ifdef FEAT_MBYTE
5654 if (enc_utf8)
5655 ScreenLinesUC[off] = 0;
5656#endif
5657 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005658 if (draw_color_col)
5659 draw_color_col = advance_color_col(VCOL_HLC,
5660 &color_cols);
5661
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005662 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005663 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005664 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005665 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005666 else
5667 ScreenAttrs[off++] = 0;
5668
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005669 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005670 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005671
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005672 ++vcol;
5673 }
5674 }
5675#endif
5676
Bram Moolenaar53f81742017-09-22 14:35:51 +02005677 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005678 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 row++;
5680
5681 /*
5682 * Update w_cline_height and w_cline_folded if the cursor line was
5683 * updated (saves a call to plines() later).
5684 */
5685 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5686 {
5687 curwin->w_cline_row = startrow;
5688 curwin->w_cline_height = row - startrow;
5689#ifdef FEAT_FOLDING
5690 curwin->w_cline_folded = FALSE;
5691#endif
5692 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5693 }
5694
5695 break;
5696 }
5697
5698 /* line continues beyond line end */
5699 if (lcs_ext
5700 && !wp->w_p_wrap
5701#ifdef FEAT_DIFF
5702 && filler_todo <= 0
5703#endif
5704 && (
5705#ifdef FEAT_RIGHTLEFT
5706 wp->w_p_rl ? col == 0 :
5707#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005708 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005710 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5712 {
5713 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005714 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715#ifdef FEAT_MBYTE
5716 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005717 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005720 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005721 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723 else
5724 mb_utf8 = FALSE;
5725#endif
5726 }
5727
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005728#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005729 /* advance to the next 'colorcolumn' */
5730 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005731 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005732
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005733 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005734 * highlight the cursor position itself.
5735 * Also highlight the 'colorcolumn' if it is different than
5736 * 'cursorcolumn' */
5737 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005738 if (draw_state == WL_LINE && !lnum_in_visual_area
5739 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005740 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005741 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005742 && lnum != wp->w_cursor.lnum)
5743 {
5744 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005745 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005746 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005747 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005748 {
5749 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005750 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005751 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005752 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005753#endif
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 /*
5756 * Store character to be displayed.
5757 * Skip characters that are left of the screen for 'nowrap'.
5758 */
5759 vcol_prev = vcol;
5760 if (draw_state < WL_LINE || n_skip <= 0)
5761 {
5762 /*
5763 * Store the character.
5764 */
5765#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5766 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5767 {
5768 /* A double-wide character is: put first halve in left cell. */
5769 --off;
5770 --col;
5771 }
5772#endif
5773 ScreenLines[off] = c;
5774#ifdef FEAT_MBYTE
5775 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005776 {
5777 if ((mb_c & 0xff00) == 0x8e00)
5778 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 else if (enc_utf8)
5782 {
5783 if (mb_utf8)
5784 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005785 int i;
5786
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005788 if ((c & 0xff) == 0)
5789 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005790 for (i = 0; i < Screen_mco; ++i)
5791 {
5792 ScreenLinesC[i][off] = u8cc[i];
5793 if (u8cc[i] == 0)
5794 break;
5795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
5797 else
5798 ScreenLinesUC[off] = 0;
5799 }
5800 if (multi_attr)
5801 {
5802 ScreenAttrs[off] = multi_attr;
5803 multi_attr = 0;
5804 }
5805 else
5806#endif
5807 ScreenAttrs[off] = char_attr;
5808
5809#ifdef FEAT_MBYTE
5810 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5811 {
5812 /* Need to fill two screen columns. */
5813 ++off;
5814 ++col;
5815 if (enc_utf8)
5816 /* UTF-8: Put a 0 in the second screen char. */
5817 ScreenLines[off] = 0;
5818 else
5819 /* DBCS: Put second byte in the second screen char. */
5820 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005821 if (draw_state > WL_NR
5822#ifdef FEAT_DIFF
5823 && filler_todo <= 0
5824#endif
5825 )
5826 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 /* When "tocol" is halfway a character, set it to the end of
5828 * the character, otherwise highlighting won't stop. */
5829 if (tocol == vcol)
5830 ++tocol;
5831#ifdef FEAT_RIGHTLEFT
5832 if (wp->w_p_rl)
5833 {
5834 /* now it's time to backup one cell */
5835 --off;
5836 --col;
5837 }
5838#endif
5839 }
5840#endif
5841#ifdef FEAT_RIGHTLEFT
5842 if (wp->w_p_rl)
5843 {
5844 --off;
5845 --col;
5846 }
5847 else
5848#endif
5849 {
5850 ++off;
5851 ++col;
5852 }
5853 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005854#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005855 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005856 {
5857 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005858 ++vcol_off;
5859 if (n_extra > 0)
5860 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005861 if (wp->w_p_wrap)
5862 {
5863 /*
5864 * Special voodoo required if 'wrap' is on.
5865 *
5866 * Advance the column indicator to force the line
5867 * drawing to wrap early. This will make the line
5868 * take up the same screen space when parts are concealed,
5869 * so that cursor line computations aren't messed up.
5870 *
5871 * To avoid the fictitious advance of 'col' causing
5872 * trailing junk to be written out of the screen line
5873 * we are building, 'boguscols' keeps track of the number
5874 * of bad columns we have advanced.
5875 */
5876 if (n_extra > 0)
5877 {
5878 vcol += n_extra;
5879# ifdef FEAT_RIGHTLEFT
5880 if (wp->w_p_rl)
5881 {
5882 col -= n_extra;
5883 boguscols -= n_extra;
5884 }
5885 else
5886# endif
5887 {
5888 col += n_extra;
5889 boguscols += n_extra;
5890 }
5891 n_extra = 0;
5892 n_attr = 0;
5893 }
5894
5895
5896# ifdef FEAT_MBYTE
5897 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5898 {
5899 /* Need to fill two screen columns. */
5900# ifdef FEAT_RIGHTLEFT
5901 if (wp->w_p_rl)
5902 {
5903 --boguscols;
5904 --col;
5905 }
5906 else
5907# endif
5908 {
5909 ++boguscols;
5910 ++col;
5911 }
5912 }
5913# endif
5914
5915# ifdef FEAT_RIGHTLEFT
5916 if (wp->w_p_rl)
5917 {
5918 --boguscols;
5919 --col;
5920 }
5921 else
5922# endif
5923 {
5924 ++boguscols;
5925 ++col;
5926 }
5927 }
5928 else
5929 {
5930 if (n_extra > 0)
5931 {
5932 vcol += n_extra;
5933 n_extra = 0;
5934 n_attr = 0;
5935 }
5936 }
5937
5938 }
5939#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 else
5941 --n_skip;
5942
Bram Moolenaar64486672010-05-16 15:46:46 +02005943 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5944 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005945 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946#ifdef FEAT_DIFF
5947 && filler_todo <= 0
5948#endif
5949 )
5950 ++vcol;
5951
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005952#ifdef FEAT_SYN_HL
5953 if (vcol_save_attr >= 0)
5954 char_attr = vcol_save_attr;
5955#endif
5956
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 /* restore attributes after "predeces" in 'listchars' */
5958 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5959 char_attr = saved_attr3;
5960
5961 /* restore attributes after last 'listchars' or 'number' char */
5962 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5963 char_attr = saved_attr2;
5964
5965 /*
5966 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005967 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 */
5969 if ((
5970#ifdef FEAT_RIGHTLEFT
5971 wp->w_p_rl ? (col < 0) :
5972#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005973 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 && (*ptr != NUL
5975#ifdef FEAT_DIFF
5976 || filler_todo > 0
5977#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005978 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5980 )
5981 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005982#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005983 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005984 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005985 boguscols = 0;
5986#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005987 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005988 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 ++row;
5991 ++screen_row;
5992
5993 /* When not wrapping and finished diff lines, or when displayed
5994 * '$' and highlighting until last column, break here. */
5995 if ((!wp->w_p_wrap
5996#ifdef FEAT_DIFF
5997 && filler_todo <= 0
5998#endif
5999 ) || lcs_eol_one == -1)
6000 break;
6001
6002 /* When the window is too narrow draw all "@" lines. */
6003 if (draw_state != WL_LINE
6004#ifdef FEAT_DIFF
6005 && filler_todo <= 0
6006#endif
6007 )
6008 {
6009 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 row = endrow;
6012 }
6013
6014 /* When line got too long for screen break here. */
6015 if (row == endrow)
6016 {
6017 ++row;
6018 break;
6019 }
6020
6021 if (screen_cur_row == screen_row - 1
6022#ifdef FEAT_DIFF
6023 && filler_todo <= 0
6024#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006025 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 {
6027 /* Remember that the line wraps, used for modeless copy. */
6028 LineWraps[screen_row - 1] = TRUE;
6029
6030 /*
6031 * Special trick to make copy/paste of wrapped lines work with
6032 * xterm/screen: write an extra character beyond the end of
6033 * the line. This will work with all terminal types
6034 * (regardless of the xn,am settings).
6035 * Only do this on a fast tty.
6036 * Only do this if the cursor is on the current line
6037 * (something has been written in it).
6038 * Don't do this for the GUI.
6039 * Don't do this for double-width characters.
6040 * Don't do this for a window not at the right screen border.
6041 */
6042 if (p_tf
6043#ifdef FEAT_GUI
6044 && !gui.in_use
6045#endif
6046#ifdef FEAT_MBYTE
6047 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006048 && ((*mb_off2cells)(LineOffset[screen_row],
6049 LineOffset[screen_row] + screen_Columns)
6050 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006052 + (int)Columns - 2,
6053 LineOffset[screen_row] + screen_Columns)
6054 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055#endif
6056 )
6057 {
6058 /* First make sure we are at the end of the screen line,
6059 * then output the same character again to let the
6060 * terminal know about the wrap. If the terminal doesn't
6061 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006062 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 screen_char(LineOffset[screen_row - 1]
6064 + (unsigned)Columns - 1,
6065 screen_row - 1, (int)(Columns - 1));
6066
6067#ifdef FEAT_MBYTE
6068 /* When there is a multi-byte character, just output a
6069 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006070 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6071 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 out_char(' ');
6073 else
6074#endif
6075 out_char(ScreenLines[LineOffset[screen_row - 1]
6076 + (Columns - 1)]);
6077 /* force a redraw of the first char on the next line */
6078 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6079 screen_start(); /* don't know where cursor is now */
6080 }
6081 }
6082
6083 col = 0;
6084 off = (unsigned)(current_ScreenLine - ScreenLines);
6085#ifdef FEAT_RIGHTLEFT
6086 if (wp->w_p_rl)
6087 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006088 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 off += col;
6090 }
6091#endif
6092
6093 /* reset the drawing state for the start of a wrapped line */
6094 draw_state = WL_START;
6095 saved_n_extra = n_extra;
6096 saved_p_extra = p_extra;
6097 saved_c_extra = c_extra;
6098 saved_char_attr = char_attr;
6099 n_extra = 0;
6100 lcs_prec_todo = lcs_prec;
6101#ifdef FEAT_LINEBREAK
6102# ifdef FEAT_DIFF
6103 if (filler_todo <= 0)
6104# endif
6105 need_showbreak = TRUE;
6106#endif
6107#ifdef FEAT_DIFF
6108 --filler_todo;
6109 /* When the filler lines are actually below the last line of the
6110 * file, don't draw the line itself, break here. */
6111 if (filler_todo == 0 && wp->w_botfill)
6112 break;
6113#endif
6114 }
6115
6116 } /* for every character in the line */
6117
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006118#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006119 /* After an empty line check first word for capital. */
6120 if (*skipwhite(line) == NUL)
6121 {
6122 capcol_lnum = lnum + 1;
6123 cap_col = 0;
6124 }
6125#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006126#ifdef FEAT_TEXT_PROP
6127 vim_free(text_props);
6128 vim_free(text_prop_idxs);
6129#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006130
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006131 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 return row;
6133}
6134
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006135#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006136/*
6137 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006138 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006139 */
6140 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006141comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006142{
6143 int i;
6144
6145 for (i = 0; i < Screen_mco; ++i)
6146 {
6147 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6148 return TRUE;
6149 if (ScreenLinesC[i][off_from] == 0)
6150 break;
6151 }
6152 return FALSE;
6153}
6154#endif
6155
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156/*
6157 * Check whether the given character needs redrawing:
6158 * - the (first byte of the) character is different
6159 * - the attributes are different
6160 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006161 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 */
6163 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006164char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
6166 if (cols > 0
6167 && ((ScreenLines[off_from] != ScreenLines[off_to]
6168 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6169
6170#ifdef FEAT_MBYTE
6171 || (enc_dbcs != 0
6172 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6173 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6174 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6175 : (cols > 1 && ScreenLines[off_from + 1]
6176 != ScreenLines[off_to + 1])))
6177 || (enc_utf8
6178 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6179 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006180 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006181 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6182 && ScreenLines[off_from + 1]
6183 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#endif
6185 ))
6186 return TRUE;
6187 return FALSE;
6188}
6189
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006190#if defined(FEAT_TERMINAL) || defined(PROTO)
6191/*
6192 * Return the index in ScreenLines[] for the current screen line.
6193 */
6194 int
6195screen_get_current_line_off()
6196{
6197 return (int)(current_ScreenLine - ScreenLines);
6198}
6199#endif
6200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201/*
6202 * Move one "cooked" screen line to the screen, but only the characters that
6203 * have actually changed. Handle insert/delete character.
6204 * "coloff" gives the first column on the screen for this line.
6205 * "endcol" gives the columns where valid characters are.
6206 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6207 * needs to be cleared, negative otherwise.
6208 * "rlflag" is TRUE in a rightleft window:
6209 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6210 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6211 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006212 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006213screen_line(
6214 int row,
6215 int coloff,
6216 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006217 int clear_width,
6218 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
6220 unsigned off_from;
6221 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006222#ifdef FEAT_MBYTE
6223 unsigned max_off_from;
6224 unsigned max_off_to;
6225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 int force = FALSE; /* force update rest of the line */
6229 int redraw_this /* bool: does character need redraw? */
6230#ifdef FEAT_GUI
6231 = TRUE /* For GUI when while-loop empty */
6232#endif
6233 ;
6234 int redraw_next; /* redraw_this for next character */
6235#ifdef FEAT_MBYTE
6236 int clear_next = FALSE;
6237 int char_cells; /* 1: normal char */
6238 /* 2: occupies two display cells */
6239# define CHAR_CELLS char_cells
6240#else
6241# define CHAR_CELLS 1
6242#endif
6243
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006244 /* Check for illegal row and col, just in case. */
6245 if (row >= Rows)
6246 row = Rows - 1;
6247 if (endcol > Columns)
6248 endcol = Columns;
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250# ifdef FEAT_CLIPBOARD
6251 clip_may_clear_selection(row, row);
6252# endif
6253
6254 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6255 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006256#ifdef FEAT_MBYTE
6257 max_off_from = off_from + screen_Columns;
6258 max_off_to = LineOffset[row] + screen_Columns;
6259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261#ifdef FEAT_RIGHTLEFT
6262 if (rlflag)
6263 {
6264 /* Clear rest first, because it's left of the text. */
6265 if (clear_width > 0)
6266 {
6267 while (col <= endcol && ScreenLines[off_to] == ' '
6268 && ScreenAttrs[off_to] == 0
6269# ifdef FEAT_MBYTE
6270 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6271# endif
6272 )
6273 {
6274 ++off_to;
6275 ++col;
6276 }
6277 if (col <= endcol)
6278 screen_fill(row, row + 1, col + coloff,
6279 endcol + coloff + 1, ' ', ' ', 0);
6280 }
6281 col = endcol + 1;
6282 off_to = LineOffset[row] + col + coloff;
6283 off_from += col;
6284 endcol = (clear_width > 0 ? clear_width : -clear_width);
6285 }
6286#endif /* FEAT_RIGHTLEFT */
6287
6288 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6289
6290 while (col < endcol)
6291 {
6292#ifdef FEAT_MBYTE
6293 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006294 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 else
6296 char_cells = 1;
6297#endif
6298
6299 redraw_this = redraw_next;
6300 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6301 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6302
6303#ifdef FEAT_GUI
6304 /* If the next character was bold, then redraw the current character to
6305 * remove any pixels that might have spilt over into us. This only
6306 * happens in the GUI.
6307 */
6308 if (redraw_next && gui.in_use)
6309 {
6310 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006311 if (hl > HL_ALL)
6312 hl = syn_attr2attr(hl);
6313 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 redraw_this = TRUE;
6315 }
6316#endif
6317
6318 if (redraw_this)
6319 {
6320 /*
6321 * Special handling when 'xs' termcap flag set (hpterm):
6322 * Attributes for characters are stored at the position where the
6323 * cursor is when writing the highlighting code. The
6324 * start-highlighting code must be written with the cursor on the
6325 * first highlighted character. The stop-highlighting code must
6326 * be written with the cursor just after the last highlighted
6327 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006328 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 * to clear the rest of the line, and force redrawing it
6330 * completely.
6331 */
6332 if ( p_wiv
6333 && !force
6334#ifdef FEAT_GUI
6335 && !gui.in_use
6336#endif
6337 && ScreenAttrs[off_to] != 0
6338 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6339 {
6340 /*
6341 * Need to remove highlighting attributes here.
6342 */
6343 windgoto(row, col + coloff);
6344 out_str(T_CE); /* clear rest of this screen line */
6345 screen_start(); /* don't know where cursor is now */
6346 force = TRUE; /* force redraw of rest of the line */
6347 redraw_next = TRUE; /* or else next char would miss out */
6348
6349 /*
6350 * If the previous character was highlighted, need to stop
6351 * highlighting at this character.
6352 */
6353 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6354 {
6355 screen_attr = ScreenAttrs[off_to - 1];
6356 term_windgoto(row, col + coloff);
6357 screen_stop_highlight();
6358 }
6359 else
6360 screen_attr = 0; /* highlighting has stopped */
6361 }
6362#ifdef FEAT_MBYTE
6363 if (enc_dbcs != 0)
6364 {
6365 /* Check if overwriting a double-byte with a single-byte or
6366 * the other way around requires another character to be
6367 * redrawn. For UTF-8 this isn't needed, because comparing
6368 * ScreenLinesUC[] is sufficient. */
6369 if (char_cells == 1
6370 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006371 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 {
6373 /* Writing a single-cell character over a double-cell
6374 * character: need to redraw the next cell. */
6375 ScreenLines[off_to + 1] = 0;
6376 redraw_next = TRUE;
6377 }
6378 else if (char_cells == 2
6379 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006380 && (*mb_off2cells)(off_to, max_off_to) == 1
6381 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 {
6383 /* Writing the second half of a double-cell character over
6384 * a double-cell character: need to redraw the second
6385 * cell. */
6386 ScreenLines[off_to + 2] = 0;
6387 redraw_next = TRUE;
6388 }
6389
6390 if (enc_dbcs == DBCS_JPNU)
6391 ScreenLines2[off_to] = ScreenLines2[off_from];
6392 }
6393 /* When writing a single-width character over a double-width
6394 * character and at the end of the redrawn text, need to clear out
6395 * the right halve of the old character.
6396 * Also required when writing the right halve of a double-width
6397 * char over the left halve of an existing one. */
6398 if (has_mbyte && col + char_cells == endcol
6399 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006400 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006402 && (*mb_off2cells)(off_to, max_off_to) == 1
6403 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 clear_next = TRUE;
6405#endif
6406
6407 ScreenLines[off_to] = ScreenLines[off_from];
6408#ifdef FEAT_MBYTE
6409 if (enc_utf8)
6410 {
6411 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6412 if (ScreenLinesUC[off_from] != 0)
6413 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006414 int i;
6415
6416 for (i = 0; i < Screen_mco; ++i)
6417 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419 }
6420 if (char_cells == 2)
6421 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6422#endif
6423
6424#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006425 /* The bold trick makes a single column of pixels appear in the
6426 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 * character should be redrawn too. This happens for our own GUI
6428 * and for some xterms. */
6429 if (
6430# ifdef FEAT_GUI
6431 gui.in_use
6432# endif
6433# if defined(FEAT_GUI) && defined(UNIX)
6434 ||
6435# endif
6436# ifdef UNIX
6437 term_is_xterm
6438# endif
6439 )
6440 {
6441 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006442 if (hl > HL_ALL)
6443 hl = syn_attr2attr(hl);
6444 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 redraw_next = TRUE;
6446 }
6447#endif
6448 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6449#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006450 /* For simplicity set the attributes of second half of a
6451 * double-wide character equal to the first half. */
6452 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006454
6455 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 else
6458#endif
6459 screen_char(off_to, row, col + coloff);
6460 }
6461 else if ( p_wiv
6462#ifdef FEAT_GUI
6463 && !gui.in_use
6464#endif
6465 && col + coloff > 0)
6466 {
6467 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6468 {
6469 /*
6470 * Don't output stop-highlight when moving the cursor, it will
6471 * stop the highlighting when it should continue.
6472 */
6473 screen_attr = 0;
6474 }
6475 else if (screen_attr != 0)
6476 screen_stop_highlight();
6477 }
6478
6479 off_to += CHAR_CELLS;
6480 off_from += CHAR_CELLS;
6481 col += CHAR_CELLS;
6482 }
6483
6484#ifdef FEAT_MBYTE
6485 if (clear_next)
6486 {
6487 /* Clear the second half of a double-wide character of which the left
6488 * half was overwritten with a single-wide character. */
6489 ScreenLines[off_to] = ' ';
6490 if (enc_utf8)
6491 ScreenLinesUC[off_to] = 0;
6492 screen_char(off_to, row, col + coloff);
6493 }
6494#endif
6495
6496 if (clear_width > 0
6497#ifdef FEAT_RIGHTLEFT
6498 && !rlflag
6499#endif
6500 )
6501 {
6502#ifdef FEAT_GUI
6503 int startCol = col;
6504#endif
6505
6506 /* blank out the rest of the line */
6507 while (col < clear_width && ScreenLines[off_to] == ' '
6508 && ScreenAttrs[off_to] == 0
6509#ifdef FEAT_MBYTE
6510 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6511#endif
6512 )
6513 {
6514 ++off_to;
6515 ++col;
6516 }
6517 if (col < clear_width)
6518 {
6519#ifdef FEAT_GUI
6520 /*
6521 * In the GUI, clearing the rest of the line may leave pixels
6522 * behind if the first character cleared was bold. Some bold
6523 * fonts spill over the left. In this case we redraw the previous
6524 * character too. If we didn't skip any blanks above, then we
6525 * only redraw if the character wasn't already redrawn anyway.
6526 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006527 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
6529 hl = ScreenAttrs[off_to];
6530 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006531 {
6532 int prev_cells = 1;
6533# ifdef FEAT_MBYTE
6534 if (enc_utf8)
6535 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6536 * that its width is 2. */
6537 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6538 else if (enc_dbcs != 0)
6539 {
6540 /* find previous character by counting from first
6541 * column and get its width. */
6542 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006543 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006544
6545 while (off < off_to)
6546 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006547 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006548 off += prev_cells;
6549 }
6550 }
6551
6552 if (enc_dbcs != 0 && prev_cells > 1)
6553 screen_char_2(off_to - prev_cells, row,
6554 col + coloff - prev_cells);
6555 else
6556# endif
6557 screen_char(off_to - prev_cells, row,
6558 col + coloff - prev_cells);
6559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 }
6561#endif
6562 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6563 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 off_to += clear_width - col;
6565 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 }
6567 }
6568
6569 if (clear_width > 0)
6570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 /* For a window that's left of another, draw the separator char. */
6572 if (col + coloff < Columns)
6573 {
6574 int c;
6575
6576 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006577 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006578#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006579 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6580 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 || ScreenAttrs[off_to] != hl)
6583 {
6584 ScreenLines[off_to] = c;
6585 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006586#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 if (enc_utf8)
6588 {
6589 if (c >= 0x80)
6590 {
6591 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006592 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594 else
6595 ScreenLinesUC[off_to] = 0;
6596 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 screen_char(off_to, row, col + coloff);
6599 }
6600 }
6601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 LineWraps[row] = FALSE;
6603 }
6604}
6605
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006606#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006608 * Mirror text "str" for right-left displaying.
6609 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006611 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006612rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 char_u *p1, *p2;
6615 int t;
6616
6617 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6618 {
6619 t = *p1;
6620 *p1 = *p2;
6621 *p2 = t;
6622 }
6623}
6624#endif
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626/*
6627 * mark all status lines for redraw; used after first :cd
6628 */
6629 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006630status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 win_T *wp;
6633
Bram Moolenaar29323592016-07-24 22:04:11 +02006634 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (wp->w_status_height)
6636 {
6637 wp->w_redr_status = TRUE;
6638 redraw_later(VALID);
6639 }
6640}
6641
6642/*
6643 * mark all status lines of the current buffer for redraw
6644 */
6645 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006646status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
6648 win_T *wp;
6649
Bram Moolenaar29323592016-07-24 22:04:11 +02006650 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6652 {
6653 wp->w_redr_status = TRUE;
6654 redraw_later(VALID);
6655 }
6656}
6657
6658/*
6659 * Redraw all status lines that need to be redrawn.
6660 */
6661 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006662redraw_statuslines(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_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006668 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006669 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006670 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
Bram Moolenaar4033c552017-09-16 20:54:51 +02006673#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674/*
6675 * Redraw all status lines at the bottom of frame "frp".
6676 */
6677 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006678win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 if (frp->fr_layout == FR_LEAF)
6681 frp->fr_win->w_redr_status = TRUE;
6682 else if (frp->fr_layout == FR_ROW)
6683 {
6684 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6685 win_redraw_last_status(frp);
6686 }
6687 else /* frp->fr_layout == FR_COL */
6688 {
6689 frp = frp->fr_child;
6690 while (frp->fr_next != NULL)
6691 frp = frp->fr_next;
6692 win_redraw_last_status(frp);
6693 }
6694}
6695#endif
6696
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697/*
6698 * Draw the verticap separator right of window "wp" starting with line "row".
6699 */
6700 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006701draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
6703 int hl;
6704 int c;
6705
6706 if (wp->w_vsep_width)
6707 {
6708 /* draw the vertical separator right of this window */
6709 c = fillchar_vsep(&hl);
6710 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6711 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6712 c, ' ', hl);
6713 }
6714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006717static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006720 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 */
6722 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006723status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 int len = 0;
6726
6727#ifdef FEAT_MENU
6728 int emenu = (xp->xp_context == EXPAND_MENUS
6729 || xp->xp_context == EXPAND_MENUNAMES);
6730
6731 /* Check for menu separators - replace with '|'. */
6732 if (emenu && menu_is_separator(s))
6733 return 1;
6734#endif
6735
6736 while (*s != NUL)
6737 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006738 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006739 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006740 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 }
6742
6743 return len;
6744}
6745
6746/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006747 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006748 * These are backslashes used for escaping. Do show backslashes in help tags.
6749 */
6750 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006751skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006752{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006753 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006754#ifdef FEAT_MENU
6755 || ((xp->xp_context == EXPAND_MENUS
6756 || xp->xp_context == EXPAND_MENUNAMES)
6757 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6758#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006759 )
6760 {
6761#ifndef BACKSLASH_IN_FILENAME
6762 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6763 return 2;
6764#endif
6765 return 1;
6766 }
6767 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006768}
6769
6770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 * Show wildchar matches in the status line.
6772 * Show at least the "match" item.
6773 * We start at item 'first_match' in the list and show all matches that fit.
6774 *
6775 * If inversion is possible we use it. Else '=' characters are used.
6776 */
6777 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006778win_redr_status_matches(
6779 expand_T *xp,
6780 int num_matches,
6781 char_u **matches, /* list of matches */
6782 int match,
6783 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6786 int row;
6787 char_u *buf;
6788 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006789 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 int fillchar;
6791 int attr;
6792 int i;
6793 int highlight = TRUE;
6794 char_u *selstart = NULL;
6795 int selstart_col = 0;
6796 char_u *selend = NULL;
6797 static int first_match = 0;
6798 int add_left = FALSE;
6799 char_u *s;
6800#ifdef FEAT_MENU
6801 int emenu;
6802#endif
6803#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6804 int l;
6805#endif
6806
6807 if (matches == NULL) /* interrupted completion? */
6808 return;
6809
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006810#ifdef FEAT_MBYTE
6811 if (has_mbyte)
6812 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6813 else
6814#endif
6815 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 if (buf == NULL)
6817 return;
6818
6819 if (match == -1) /* don't show match but original text */
6820 {
6821 match = 0;
6822 highlight = FALSE;
6823 }
6824 /* count 1 for the ending ">" */
6825 clen = status_match_len(xp, L_MATCH(match)) + 3;
6826 if (match == 0)
6827 first_match = 0;
6828 else if (match < first_match)
6829 {
6830 /* jumping left, as far as we can go */
6831 first_match = match;
6832 add_left = TRUE;
6833 }
6834 else
6835 {
6836 /* check if match fits on the screen */
6837 for (i = first_match; i < match; ++i)
6838 clen += status_match_len(xp, L_MATCH(i)) + 2;
6839 if (first_match > 0)
6840 clen += 2;
6841 /* jumping right, put match at the left */
6842 if ((long)clen > Columns)
6843 {
6844 first_match = match;
6845 /* if showing the last match, we can add some on the left */
6846 clen = 2;
6847 for (i = match; i < num_matches; ++i)
6848 {
6849 clen += status_match_len(xp, L_MATCH(i)) + 2;
6850 if ((long)clen >= Columns)
6851 break;
6852 }
6853 if (i == num_matches)
6854 add_left = TRUE;
6855 }
6856 }
6857 if (add_left)
6858 while (first_match > 0)
6859 {
6860 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6861 if ((long)clen >= Columns)
6862 break;
6863 --first_match;
6864 }
6865
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006866 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
6868 if (first_match == 0)
6869 {
6870 *buf = NUL;
6871 len = 0;
6872 }
6873 else
6874 {
6875 STRCPY(buf, "< ");
6876 len = 2;
6877 }
6878 clen = len;
6879
6880 i = first_match;
6881 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6882 {
6883 if (i == match)
6884 {
6885 selstart = buf + len;
6886 selstart_col = clen;
6887 }
6888
6889 s = L_MATCH(i);
6890 /* Check for menu separators - replace with '|' */
6891#ifdef FEAT_MENU
6892 emenu = (xp->xp_context == EXPAND_MENUS
6893 || xp->xp_context == EXPAND_MENUNAMES);
6894 if (emenu && menu_is_separator(s))
6895 {
6896 STRCPY(buf + len, transchar('|'));
6897 l = (int)STRLEN(buf + len);
6898 len += l;
6899 clen += l;
6900 }
6901 else
6902#endif
6903 for ( ; *s != NUL; ++s)
6904 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006905 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 clen += ptr2cells(s);
6907#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006908 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {
6910 STRNCPY(buf + len, s, l);
6911 s += l - 1;
6912 len += l;
6913 }
6914 else
6915#endif
6916 {
6917 STRCPY(buf + len, transchar_byte(*s));
6918 len += (int)STRLEN(buf + len);
6919 }
6920 }
6921 if (i == match)
6922 selend = buf + len;
6923
6924 *(buf + len++) = ' ';
6925 *(buf + len++) = ' ';
6926 clen += 2;
6927 if (++i == num_matches)
6928 break;
6929 }
6930
6931 if (i != num_matches)
6932 {
6933 *(buf + len++) = '>';
6934 ++clen;
6935 }
6936
6937 buf[len] = NUL;
6938
6939 row = cmdline_row - 1;
6940 if (row >= 0)
6941 {
6942 if (wild_menu_showing == 0)
6943 {
6944 if (msg_scrolled > 0)
6945 {
6946 /* Put the wildmenu just above the command line. If there is
6947 * no room, scroll the screen one line up. */
6948 if (cmdline_row == Rows - 1)
6949 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006950 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 ++msg_scrolled;
6952 }
6953 else
6954 {
6955 ++cmdline_row;
6956 ++row;
6957 }
6958 wild_menu_showing = WM_SCROLLED;
6959 }
6960 else
6961 {
6962 /* Create status line if needed by setting 'laststatus' to 2.
6963 * Set 'winminheight' to zero to avoid that the window is
6964 * resized. */
6965 if (lastwin->w_status_height == 0)
6966 {
6967 save_p_ls = p_ls;
6968 save_p_wmh = p_wmh;
6969 p_ls = 2;
6970 p_wmh = 0;
6971 last_status(FALSE);
6972 }
6973 wild_menu_showing = WM_SHOWN;
6974 }
6975 }
6976
6977 screen_puts(buf, row, 0, attr);
6978 if (selstart != NULL && highlight)
6979 {
6980 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006981 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 }
6983
6984 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6985 }
6986
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 vim_free(buf);
6989}
6990#endif
6991
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992/*
6993 * Redraw the status line of window wp.
6994 *
6995 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006996 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6997 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006999 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007000win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
7002 int row;
7003 char_u *p;
7004 int len;
7005 int fillchar;
7006 int attr;
7007 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007008 static int busy = FALSE;
7009
7010 /* It's possible to get here recursively when 'statusline' (indirectly)
7011 * invokes ":redrawstatus". Simply ignore the call then. */
7012 if (busy)
7013 return;
7014 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016 wp->w_redr_status = FALSE;
7017 if (wp->w_status_height == 0)
7018 {
7019 /* no status line, can only be last window */
7020 redraw_cmdline = TRUE;
7021 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007022 else if (!redrawing()
7023#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007024 // don't update status line when popup menu is visible and may be
7025 // drawn over it, unless it will be redrawn later
7026 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007027#endif
7028 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {
7030 /* Don't redraw right now, do it later. */
7031 wp->w_redr_status = TRUE;
7032 }
7033#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007034 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
7036 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007037 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039#endif
7040 else
7041 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007042 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007044 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 p = NameBuff;
7046 len = (int)STRLEN(p);
7047
Bram Moolenaard85f2712017-07-28 21:51:57 +02007048 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049#ifdef FEAT_QUICKFIX
7050 || wp->w_p_pvw
7051#endif
7052 || bufIsChanged(wp->w_buffer)
7053 || wp->w_buffer->b_p_ro)
7054 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007055 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007057 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 len += (int)STRLEN(p + len);
7059 }
7060#ifdef FEAT_QUICKFIX
7061 if (wp->w_p_pvw)
7062 {
7063 STRCPY(p + len, _("[Preview]"));
7064 len += (int)STRLEN(p + len);
7065 }
7066#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007067 if (bufIsChanged(wp->w_buffer)
7068#ifdef FEAT_TERMINAL
7069 && !bt_terminal(wp->w_buffer)
7070#endif
7071 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {
7073 STRCPY(p + len, "[+]");
7074 len += 3;
7075 }
7076 if (wp->w_buffer->b_p_ro)
7077 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007078 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007079 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081
Bram Moolenaar02631462017-09-22 15:20:32 +02007082 this_ru_col = ru_col - (Columns - wp->w_width);
7083 if (this_ru_col < (wp->w_width + 1) / 2)
7084 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 if (this_ru_col <= 1)
7086 {
7087 p = (char_u *)"<"; /* No room for file name! */
7088 len = 1;
7089 }
7090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091#ifdef FEAT_MBYTE
7092 if (has_mbyte)
7093 {
7094 int clen = 0, i;
7095
7096 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007097 clen = mb_string2cells(p, -1);
7098
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 /* Find first character that will fit.
7100 * Going from start to end is much faster for DBCS. */
7101 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007102 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 clen -= (*mb_ptr2cells)(p + i);
7104 len = clen;
7105 if (i > 0)
7106 {
7107 p = p + i - 1;
7108 *p = '<';
7109 ++len;
7110 }
7111
7112 }
7113 else
7114#endif
7115 if (len > this_ru_col - 1)
7116 {
7117 p += len - (this_ru_col - 1);
7118 *p = '<';
7119 len = this_ru_col - 1;
7120 }
7121
7122 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007123 screen_puts(p, row, wp->w_wincol, attr);
7124 screen_fill(row, row + 1, len + wp->w_wincol,
7125 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007127 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7129 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007130 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
7132#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007133 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134#endif
7135 }
7136
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 /*
7138 * May need to draw the character below the vertical separator.
7139 */
7140 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7141 {
7142 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007143 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 else
7145 fillchar = fillchar_vsep(&attr);
7146 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7147 attr);
7148 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007149 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150}
7151
Bram Moolenaar238a5642006-02-21 22:12:05 +00007152#ifdef FEAT_STL_OPT
7153/*
7154 * Redraw the status line according to 'statusline' and take care of any
7155 * errors encountered.
7156 */
7157 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007158redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007159{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007160 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007161 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007162
7163 /* When called recursively return. This can happen when the statusline
7164 * contains an expression that triggers a redraw. */
7165 if (entered)
7166 return;
7167 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007168
Bram Moolenaara742e082016-04-05 21:10:38 +02007169 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007170 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007171 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007172 {
7173 /* When there is an error disable the statusline, otherwise the
7174 * display is messed up with errors and a redraw triggers the problem
7175 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007176 set_string_option_direct((char_u *)"statusline", -1,
7177 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007178 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007179 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007180 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007181 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007182}
7183#endif
7184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185/*
7186 * Return TRUE if the status line of window "wp" is connected to the status
7187 * line of the window right of it. If not, then it's a vertical separator.
7188 * Only call if (wp->w_vsep_width != 0).
7189 */
7190 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007191stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 frame_T *fr;
7194
7195 fr = wp->w_frame;
7196 while (fr->fr_parent != NULL)
7197 {
7198 if (fr->fr_parent->fr_layout == FR_COL)
7199 {
7200 if (fr->fr_next != NULL)
7201 break;
7202 }
7203 else
7204 {
7205 if (fr->fr_next != NULL)
7206 return TRUE;
7207 }
7208 fr = fr->fr_parent;
7209 }
7210 return FALSE;
7211}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214/*
7215 * Get the value to show for the language mappings, active 'keymap'.
7216 */
7217 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007218get_keymap_str(
7219 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007220 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007221 char_u *buf, /* buffer for the result */
7222 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 char_u *p;
7225
7226 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7227 return FALSE;
7228
7229 {
7230#ifdef FEAT_EVAL
7231 buf_T *old_curbuf = curbuf;
7232 win_T *old_curwin = curwin;
7233 char_u *s;
7234
7235 curbuf = wp->w_buffer;
7236 curwin = wp;
7237 STRCPY(buf, "b:keymap_name"); /* must be writable */
7238 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007239 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 --emsg_skip;
7241 curbuf = old_curbuf;
7242 curwin = old_curwin;
7243 if (p == NULL || *p == NUL)
7244#endif
7245 {
7246#ifdef FEAT_KEYMAP
7247 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7248 p = wp->w_buffer->b_p_keymap;
7249 else
7250#endif
7251 p = (char_u *)"lang";
7252 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007253 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 buf[0] = NUL;
7255#ifdef FEAT_EVAL
7256 vim_free(s);
7257#endif
7258 }
7259 return buf[0] != NUL;
7260}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
7262#if defined(FEAT_STL_OPT) || defined(PROTO)
7263/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007264 * Redraw the status line or ruler of window "wp".
7265 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 */
7267 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007268win_redr_custom(
7269 win_T *wp,
7270 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007272 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 int attr;
7274 int curattr;
7275 int row;
7276 int col = 0;
7277 int maxwidth;
7278 int width;
7279 int n;
7280 int len;
7281 int fillchar;
7282 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007283 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007285 struct stl_hlrec hltab[STL_MAX_ITEM];
7286 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007287 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007288 win_T *ewp;
7289 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
Bram Moolenaar1d633412013-12-11 15:52:01 +01007291 /* There is a tiny chance that this gets called recursively: When
7292 * redrawing a status line triggers redrawing the ruler or tabline.
7293 * Avoid trouble by not allowing recursion. */
7294 if (entered)
7295 return;
7296 entered = TRUE;
7297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007299 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007301 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007302 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007303 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007304 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007305 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007306 maxwidth = Columns;
7307# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007308 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007311 else
7312 {
7313 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007314 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007315 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007316
7317 if (draw_ruler)
7318 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007319 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007320 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007321 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007322 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007323 if (*++stl == '-')
7324 stl++;
7325 if (atoi((char *)stl))
7326 while (VIM_ISDIGIT(*stl))
7327 stl++;
7328 if (*stl++ != '(')
7329 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007330 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007331 col = ru_col - (Columns - wp->w_width);
7332 if (col < (wp->w_width + 1) / 2)
7333 col = (wp->w_width + 1) / 2;
7334 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007335 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007336 {
7337 row = Rows - 1;
7338 --maxwidth; /* writing in last column may cause scrolling */
7339 fillchar = ' ';
7340 attr = 0;
7341 }
7342
7343# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007344 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007345# endif
7346 }
7347 else
7348 {
7349 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007350 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007351 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007352 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007353# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007354 use_sandbox = was_set_insecurely((char_u *)"statusline",
7355 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007356# endif
7357 }
7358
Bram Moolenaar53f81742017-09-22 14:35:51 +02007359 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007360 }
7361
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007363 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
Bram Moolenaar61452852011-02-01 18:01:11 +01007365 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7366 * the cursor away and back. */
7367 ewp = wp == NULL ? curwin : wp;
7368 p_crb_save = ewp->w_p_crb;
7369 ewp->w_p_crb = FALSE;
7370
Bram Moolenaar362f3562009-11-03 16:20:34 +00007371 /* Make a copy, because the statusline may include a function call that
7372 * might change the option value and free the memory. */
7373 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007374 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007375 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007376 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007377 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007378 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007380 /* Make all characters printable. */
7381 p = transstr(buf);
7382 if (p != NULL)
7383 {
7384 vim_strncpy(buf, p, sizeof(buf) - 1);
7385 vim_free(p);
7386 }
7387
7388 /* fill up with "fillchar" */
7389 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007390 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 {
7392#ifdef FEAT_MBYTE
7393 len += (*mb_char2bytes)(fillchar, buf + len);
7394#else
7395 buf[len++] = fillchar;
7396#endif
7397 ++width;
7398 }
7399 buf[len] = NUL;
7400
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007401 /*
7402 * Draw each snippet with the specified highlighting.
7403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 curattr = attr;
7405 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007406 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007408 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 screen_puts_len(p, len, row, col, curattr);
7410 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007411 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007413 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007415 else if (hltab[n].userhl < 0)
7416 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007417#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007418 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7419 && wp->w_status_height != 0)
7420 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007421 else if (wp != NULL && bt_terminal(wp->w_buffer)
7422 && wp->w_status_height != 0)
7423 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007424#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007425 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007426 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007428 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007431
7432 if (wp == NULL)
7433 {
7434 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7435 col = 0;
7436 len = 0;
7437 p = buf;
7438 fillchar = 0;
7439 for (n = 0; tabtab[n].start != NULL; n++)
7440 {
7441 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7442 while (col < len)
7443 TabPageIdxs[col++] = fillchar;
7444 p = tabtab[n].start;
7445 fillchar = tabtab[n].userhl;
7446 }
7447 while (col < Columns)
7448 TabPageIdxs[col++] = fillchar;
7449 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007450
7451theend:
7452 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453}
7454
7455#endif /* FEAT_STL_OPT */
7456
7457/*
7458 * Output a single character directly to the screen and update ScreenLines.
7459 */
7460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007461screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 char_u buf[MB_MAXBYTES + 1];
7464
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007465#ifdef FEAT_MBYTE
7466 if (has_mbyte)
7467 buf[(*mb_char2bytes)(c, buf)] = NUL;
7468 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007470 {
7471 buf[0] = c;
7472 buf[1] = NUL;
7473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 screen_puts(buf, row, col, attr);
7475}
7476
7477/*
7478 * Get a single character directly from ScreenLines into "bytes[]".
7479 * Also return its attribute in *attrp;
7480 */
7481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007482screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
7484 unsigned off;
7485
7486 /* safety check */
7487 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7488 {
7489 off = LineOffset[row] + col;
7490 *attrp = ScreenAttrs[off];
7491 bytes[0] = ScreenLines[off];
7492 bytes[1] = NUL;
7493
7494#ifdef FEAT_MBYTE
7495 if (enc_utf8 && ScreenLinesUC[off] != 0)
7496 bytes[utfc_char2bytes(off, bytes)] = NUL;
7497 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7498 {
7499 bytes[0] = ScreenLines[off];
7500 bytes[1] = ScreenLines2[off];
7501 bytes[2] = NUL;
7502 }
7503 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7504 {
7505 bytes[1] = ScreenLines[off + 1];
7506 bytes[2] = NUL;
7507 }
7508#endif
7509 }
7510}
7511
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007512#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007513/*
7514 * Return TRUE if composing characters for screen posn "off" differs from
7515 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007516 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007517 */
7518 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007519screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007520{
7521 int i;
7522
7523 for (i = 0; i < Screen_mco; ++i)
7524 {
7525 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7526 return TRUE;
7527 if (u8cc[i] == 0)
7528 break;
7529 }
7530 return FALSE;
7531}
7532#endif
7533
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534/*
7535 * Put string '*text' on the screen at position 'row' and 'col', with
7536 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7537 * Note: only outputs within one row, message is truncated at screen boundary!
7538 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7539 */
7540 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007541screen_puts(
7542 char_u *text,
7543 int row,
7544 int col,
7545 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
7547 screen_puts_len(text, -1, row, col, attr);
7548}
7549
7550/*
7551 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7552 * a NUL.
7553 */
7554 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007555screen_puts_len(
7556 char_u *text,
7557 int textlen,
7558 int row,
7559 int col,
7560 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561{
7562 unsigned off;
7563 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007564 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 int c;
7566#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007567 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 int mbyte_blen = 1;
7569 int mbyte_cells = 1;
7570 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007571 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 int clear_next_cell = FALSE;
7573# ifdef FEAT_ARABIC
7574 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007575 int pc, nc, nc1;
7576 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577# endif
7578#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007579#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7580 int force_redraw_this;
7581 int force_redraw_next = FALSE;
7582#endif
7583 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584
7585 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7586 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007587 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588
Bram Moolenaarc236c162008-07-13 17:41:49 +00007589#ifdef FEAT_MBYTE
7590 /* When drawing over the right halve of a double-wide char clear out the
7591 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007592 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007593# ifdef FEAT_GUI
7594 && !gui.in_use
7595# endif
7596 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007597 {
7598 ScreenLines[off - 1] = ' ';
7599 ScreenAttrs[off - 1] = 0;
7600 if (enc_utf8)
7601 {
7602 ScreenLinesUC[off - 1] = 0;
7603 ScreenLinesC[0][off - 1] = 0;
7604 }
7605 /* redraw the previous cell, make it empty */
7606 screen_char(off - 1, row, col - 1);
7607 /* force the cell at "col" to be redrawn */
7608 force_redraw_next = TRUE;
7609 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007610#endif
7611
Bram Moolenaar367329b2007-08-30 11:53:22 +00007612#ifdef FEAT_MBYTE
7613 max_off = LineOffset[row] + screen_Columns;
7614#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007615 while (col < screen_Columns
7616 && (len < 0 || (int)(ptr - text) < len)
7617 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 {
7619 c = *ptr;
7620#ifdef FEAT_MBYTE
7621 /* check if this is the first byte of a multibyte */
7622 if (has_mbyte)
7623 {
7624 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007625 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007627 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7629 mbyte_cells = 1;
7630 else if (enc_dbcs != 0)
7631 mbyte_cells = mbyte_blen;
7632 else /* enc_utf8 */
7633 {
7634 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007635 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 (int)((text + len) - ptr));
7637 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007638 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007640# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 /* Non-BMP character: display as ? or fullwidth ?. */
7642 if (u8c >= 0x10000)
7643 {
7644 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7645 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007646 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649# ifdef FEAT_ARABIC
7650 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7651 {
7652 /* Do Arabic shaping. */
7653 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7654 {
7655 /* Past end of string to be displayed. */
7656 nc = NUL;
7657 nc1 = NUL;
7658 }
7659 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007660 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007661 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7662 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007663 nc1 = pcc[0];
7664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 pc = prev_c;
7666 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007667 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 }
7669 else
7670 prev_c = u8c;
7671# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007672 if (col + mbyte_cells > screen_Columns)
7673 {
7674 /* Only 1 cell left, but character requires 2 cells:
7675 * display a '>' in the last column to avoid wrapping. */
7676 c = '>';
7677 mbyte_cells = 1;
7678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680 }
7681#endif
7682
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007683#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7684 force_redraw_this = force_redraw_next;
7685 force_redraw_next = FALSE;
7686#endif
7687
7688 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689#ifdef FEAT_MBYTE
7690 || (mbyte_cells == 2
7691 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7692 || (enc_dbcs == DBCS_JPNU
7693 && c == 0x8e
7694 && ScreenLines2[off] != ptr[1])
7695 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007696 && (ScreenLinesUC[off] !=
7697 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7698 || (ScreenLinesUC[off] != 0
7699 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700#endif
7701 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007702 || exmode_active;
7703
7704 if (need_redraw
7705#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7706 || force_redraw_this
7707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 )
7709 {
7710#if defined(FEAT_GUI) || defined(UNIX)
7711 /* The bold trick makes a single row of pixels appear in the next
7712 * character. When a bold character is removed, the next
7713 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007714 * and for some xterms. */
7715 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716# ifdef FEAT_GUI
7717 gui.in_use
7718# endif
7719# if defined(FEAT_GUI) && defined(UNIX)
7720 ||
7721# endif
7722# ifdef UNIX
7723 term_is_xterm
7724# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007725 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007727 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007729 if (n > HL_ALL)
7730 n = syn_attr2attr(n);
7731 if (n & HL_BOLD)
7732 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 }
7734#endif
7735#ifdef FEAT_MBYTE
7736 /* When at the end of the text and overwriting a two-cell
7737 * character with a one-cell character, need to clear the next
7738 * cell. Also when overwriting the left halve of a two-cell char
7739 * with the right halve of a two-cell char. Do this only once
7740 * (mb_off2cells() may return 2 on the right halve). */
7741 if (clear_next_cell)
7742 clear_next_cell = FALSE;
7743 else if (has_mbyte
7744 && (len < 0 ? ptr[mbyte_blen] == NUL
7745 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007746 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007748 && (*mb_off2cells)(off, max_off) == 1
7749 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 clear_next_cell = TRUE;
7751
7752 /* Make sure we never leave a second byte of a double-byte behind,
7753 * it confuses mb_off2cells(). */
7754 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007755 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007757 && (*mb_off2cells)(off, max_off) == 1
7758 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 ScreenLines[off + mbyte_blen] = 0;
7760#endif
7761 ScreenLines[off] = c;
7762 ScreenAttrs[off] = attr;
7763#ifdef FEAT_MBYTE
7764 if (enc_utf8)
7765 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007766 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 ScreenLinesUC[off] = 0;
7768 else
7769 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007770 int i;
7771
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007773 for (i = 0; i < Screen_mco; ++i)
7774 {
7775 ScreenLinesC[i][off] = u8cc[i];
7776 if (u8cc[i] == 0)
7777 break;
7778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
7780 if (mbyte_cells == 2)
7781 {
7782 ScreenLines[off + 1] = 0;
7783 ScreenAttrs[off + 1] = attr;
7784 }
7785 screen_char(off, row, col);
7786 }
7787 else if (mbyte_cells == 2)
7788 {
7789 ScreenLines[off + 1] = ptr[1];
7790 ScreenAttrs[off + 1] = attr;
7791 screen_char_2(off, row, col);
7792 }
7793 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7794 {
7795 ScreenLines2[off] = ptr[1];
7796 screen_char(off, row, col);
7797 }
7798 else
7799#endif
7800 screen_char(off, row, col);
7801 }
7802#ifdef FEAT_MBYTE
7803 if (has_mbyte)
7804 {
7805 off += mbyte_cells;
7806 col += mbyte_cells;
7807 ptr += mbyte_blen;
7808 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007809 {
7810 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007812 len = -1;
7813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 }
7815 else
7816#endif
7817 {
7818 ++off;
7819 ++col;
7820 ++ptr;
7821 }
7822 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007823
7824#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7825 /* If we detected the next character needs to be redrawn, but the text
7826 * doesn't extend up to there, update the character here. */
7827 if (force_redraw_next && col < screen_Columns)
7828 {
7829# ifdef FEAT_MBYTE
7830 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7831 screen_char_2(off, row, col);
7832 else
7833# endif
7834 screen_char(off, row, col);
7835 }
7836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837}
7838
7839#ifdef FEAT_SEARCH_EXTRA
7840/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007841 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 */
7843 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007844start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 if (p_hls && !no_hlsearch)
7847 {
7848 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007849 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007850# ifdef FEAT_RELTIME
7851 /* Set the time limit to 'redrawtime'. */
7852 profile_setlimit(p_rdt, &search_hl.tm);
7853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 }
7855}
7856
7857/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007858 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 */
7860 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007861end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 if (search_hl.rm.regprog != NULL)
7864 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007865 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 search_hl.rm.regprog = NULL;
7867 }
7868}
7869
7870/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007871 * Init for calling prepare_search_hl().
7872 */
7873 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007874init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007875{
7876 matchitem_T *cur;
7877
7878 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7879 * match */
7880 cur = wp->w_match_head;
7881 while (cur != NULL)
7882 {
7883 cur->hl.rm = cur->match;
7884 if (cur->hlg_id == 0)
7885 cur->hl.attr = 0;
7886 else
7887 cur->hl.attr = syn_id2attr(cur->hlg_id);
7888 cur->hl.buf = wp->w_buffer;
7889 cur->hl.lnum = 0;
7890 cur->hl.first_lnum = 0;
7891# ifdef FEAT_RELTIME
7892 /* Set the time limit to 'redrawtime'. */
7893 profile_setlimit(p_rdt, &(cur->hl.tm));
7894# endif
7895 cur = cur->next;
7896 }
7897 search_hl.buf = wp->w_buffer;
7898 search_hl.lnum = 0;
7899 search_hl.first_lnum = 0;
7900 /* time limit is set at the toplevel, for all windows */
7901}
7902
7903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 * Advance to the match in window "wp" line "lnum" or past it.
7905 */
7906 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007907prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007909 matchitem_T *cur; /* points to the match list */
7910 match_T *shl; /* points to search_hl or a match */
7911 int shl_flag; /* flag to indicate whether search_hl
7912 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007913 int pos_inprogress; /* marks that position match search is
7914 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 int n;
7916
7917 /*
7918 * When using a multi-line pattern, start searching at the top
7919 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007920 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007922 cur = wp->w_match_head;
7923 shl_flag = FALSE;
7924 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007926 if (shl_flag == FALSE)
7927 {
7928 shl = &search_hl;
7929 shl_flag = TRUE;
7930 }
7931 else
7932 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 if (shl->rm.regprog != NULL
7934 && shl->lnum == 0
7935 && re_multiline(shl->rm.regprog))
7936 {
7937 if (shl->first_lnum == 0)
7938 {
7939# ifdef FEAT_FOLDING
7940 for (shl->first_lnum = lnum;
7941 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7942 if (hasFoldingWin(wp, shl->first_lnum - 1,
7943 NULL, NULL, TRUE, NULL))
7944 break;
7945# else
7946 shl->first_lnum = wp->w_topline;
7947# endif
7948 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007949 if (cur != NULL)
7950 cur->pos.cur = 0;
7951 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007953 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7954 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007956 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7957 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007958 pos_inprogress = cur == NULL || cur->pos.cur == 0
7959 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 if (shl->lnum != 0)
7961 {
7962 shl->first_lnum = shl->lnum
7963 + shl->rm.endpos[0].lnum
7964 - shl->rm.startpos[0].lnum;
7965 n = shl->rm.endpos[0].col;
7966 }
7967 else
7968 {
7969 ++shl->first_lnum;
7970 n = 0;
7971 }
7972 }
7973 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007974 if (shl != &search_hl && cur != NULL)
7975 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 }
7977}
7978
7979/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007980 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 * Uses shl->buf.
7982 * Sets shl->lnum and shl->rm contents.
7983 * Note: Assumes a previous match is always before "lnum", unless
7984 * shl->lnum is zero.
7985 * Careful: Any pointers for buffer lines will become invalid.
7986 */
7987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007988next_search_hl(
7989 win_T *win,
7990 match_T *shl, /* points to search_hl or a match */
7991 linenr_T lnum,
7992 colnr_T mincol, /* minimal column for a match */
7993 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994{
7995 linenr_T l;
7996 colnr_T matchcol;
7997 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007998 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008000 // for :{range}s/pat only highlight inside the range
8001 if (lnum < search_first_line || lnum > search_last_line)
8002 {
8003 shl->lnum = 0;
8004 return;
8005 }
8006
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (shl->lnum != 0)
8008 {
8009 /* Check for three situations:
8010 * 1. If the "lnum" is below a previous match, start a new search.
8011 * 2. If the previous match includes "mincol", use it.
8012 * 3. Continue after the previous match.
8013 */
8014 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8015 if (lnum > l)
8016 shl->lnum = 0;
8017 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8018 return;
8019 }
8020
8021 /*
8022 * Repeat searching for a match until one is found that includes "mincol"
8023 * or none is found in this line.
8024 */
8025 called_emsg = FALSE;
8026 for (;;)
8027 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008028#ifdef FEAT_RELTIME
8029 /* Stop searching after passing the time limit. */
8030 if (profile_passed_limit(&(shl->tm)))
8031 {
8032 shl->lnum = 0; /* no match found in time */
8033 break;
8034 }
8035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 /* Three situations:
8037 * 1. No useful previous match: search from start of line.
8038 * 2. Not Vi compatible or empty match: continue at next character.
8039 * Break the loop if this is beyond the end of the line.
8040 * 3. Vi compatible searching: continue at end of previous match.
8041 */
8042 if (shl->lnum == 0)
8043 matchcol = 0;
8044 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8045 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008046 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008048 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008049
8050 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008051 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008052 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008054 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 shl->lnum = 0;
8056 break;
8057 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008058#ifdef FEAT_MBYTE
8059 if (has_mbyte)
8060 matchcol += mb_ptr2len(ml);
8061 else
8062#endif
8063 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 }
8065 else
8066 matchcol = shl->rm.endpos[0].col;
8067
8068 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008069 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008071 /* Remember whether shl->rm is using a copy of the regprog in
8072 * cur->match. */
8073 int regprog_is_copy = (shl != &search_hl && cur != NULL
8074 && shl == &cur->hl
8075 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008076 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008077
Bram Moolenaarb3414592014-06-17 17:48:32 +02008078 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8079 matchcol,
8080#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008081 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008082#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008083 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084#endif
8085 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008086 /* Copy the regprog, in case it got freed and recompiled. */
8087 if (regprog_is_copy)
8088 cur->match.regprog = cur->hl.rm.regprog;
8089
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008090 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008091 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008092 /* Error while handling regexp: stop using this regexp. */
8093 if (shl == &search_hl)
8094 {
8095 /* don't free regprog in the match list, it's a copy */
8096 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008097 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008098 }
8099 shl->rm.regprog = NULL;
8100 shl->lnum = 0;
8101 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8102 message */
8103 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008104 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008105 }
8106 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008107 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008108 else
8109 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 if (nmatched == 0)
8111 {
8112 shl->lnum = 0; /* no match found */
8113 break;
8114 }
8115 if (shl->rm.startpos[0].lnum > 0
8116 || shl->rm.startpos[0].col >= mincol
8117 || nmatched > 1
8118 || shl->rm.endpos[0].col > mincol)
8119 {
8120 shl->lnum += shl->rm.startpos[0].lnum;
8121 break; /* useful match found */
8122 }
8123 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008124
8125 // Restore called_emsg for assert_fails().
8126 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128
Bram Moolenaar85077472016-10-16 14:35:48 +02008129/*
8130 * If there is a match fill "shl" and return one.
8131 * Return zero otherwise.
8132 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008133 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008134next_search_hl_pos(
8135 match_T *shl, /* points to a match */
8136 linenr_T lnum,
8137 posmatch_T *posmatch, /* match positions */
8138 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008139{
8140 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008141 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008142
Bram Moolenaarb3414592014-06-17 17:48:32 +02008143 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8144 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008145 llpos_T *pos = &posmatch->pos[i];
8146
8147 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008148 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008149 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008150 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008151 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008152 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008153 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008154 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008155 /* if this match comes before the one at "found" then swap
8156 * them */
8157 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008158 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008159 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008160
Bram Moolenaar85077472016-10-16 14:35:48 +02008161 *pos = posmatch->pos[found];
8162 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008163 }
8164 }
8165 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008166 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008167 }
8168 }
8169 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008170 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008171 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008172 colnr_T start = posmatch->pos[found].col == 0
8173 ? 0 : posmatch->pos[found].col - 1;
8174 colnr_T end = posmatch->pos[found].col == 0
8175 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008176
Bram Moolenaar85077472016-10-16 14:35:48 +02008177 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008178 shl->rm.startpos[0].lnum = 0;
8179 shl->rm.startpos[0].col = start;
8180 shl->rm.endpos[0].lnum = 0;
8181 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008182 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008183 posmatch->cur = found + 1;
8184 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008185 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008186 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008187}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008188#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008189
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008191screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 attrentry_T *aep = NULL;
8194
8195 screen_attr = attr;
8196 if (full_screen
8197#ifdef WIN3264
8198 && termcap_active
8199#endif
8200 )
8201 {
8202#ifdef FEAT_GUI
8203 if (gui.in_use)
8204 {
8205 char buf[20];
8206
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008207 /* The GUI handles this internally. */
8208 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 OUT_STR(buf);
8210 }
8211 else
8212#endif
8213 {
8214 if (attr > HL_ALL) /* special HL attr. */
8215 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008216 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 aep = syn_cterm_attr2entry(attr);
8218 else
8219 aep = syn_term_attr2entry(attr);
8220 if (aep == NULL) /* did ":syntax clear" */
8221 attr = 0;
8222 else
8223 attr = aep->ae_attr;
8224 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008225 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008227 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008228#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008229 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8230 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8231 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008232#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008233 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008234 /* If the Normal FG color has BOLD attribute and the new HL
8235 * has a FG color defined, clear BOLD. */
8236 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008237 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008239 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008240 out_str(T_UCS);
8241 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008242 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8243 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008245 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008247 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008249 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008250 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
8252 /*
8253 * Output the color or start string after bold etc., in case the
8254 * bold etc. override the color setting.
8255 */
8256 if (aep != NULL)
8257 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008258#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008259 /* When 'termguicolors' is set but fg or bg is unset,
8260 * fall back to the cterm colors. This helps for SpellBad,
8261 * where the GUI uses a red undercurl. */
8262 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008264 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008265 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008266 }
8267 else
8268#endif
8269 if (t_colors > 1)
8270 {
8271 if (aep->ae_u.cterm.fg_color)
8272 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8273 }
8274#ifdef FEAT_TERMGUICOLORS
8275 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8276 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008277 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008278 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 }
8280 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008281#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008282 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008284 if (aep->ae_u.cterm.bg_color)
8285 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8286 }
8287
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008288 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008289 {
8290 if (aep->ae_u.term.start != NULL)
8291 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 }
8293 }
8294 }
8295 }
8296}
8297
8298 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008299screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 int do_ME = FALSE; /* output T_ME code */
8302
8303 if (screen_attr != 0
8304#ifdef WIN3264
8305 && termcap_active
8306#endif
8307 )
8308 {
8309#ifdef FEAT_GUI
8310 if (gui.in_use)
8311 {
8312 char buf[20];
8313
8314 /* use internal GUI code */
8315 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8316 OUT_STR(buf);
8317 }
8318 else
8319#endif
8320 {
8321 if (screen_attr > HL_ALL) /* special HL attr. */
8322 {
8323 attrentry_T *aep;
8324
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008325 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
8327 /*
8328 * Assume that t_me restores the original colors!
8329 */
8330 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008331 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008332#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008333 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8334 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8335 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008336#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008337 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008338#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008339 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8340 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8341 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008342#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008343 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 do_ME = TRUE;
8345 }
8346 else
8347 {
8348 aep = syn_term_attr2entry(screen_attr);
8349 if (aep != NULL && aep->ae_u.term.stop != NULL)
8350 {
8351 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8352 do_ME = TRUE;
8353 else
8354 out_str(aep->ae_u.term.stop);
8355 }
8356 }
8357 if (aep == NULL) /* did ":syntax clear" */
8358 screen_attr = 0;
8359 else
8360 screen_attr = aep->ae_attr;
8361 }
8362
8363 /*
8364 * Often all ending-codes are equal to T_ME. Avoid outputting the
8365 * same sequence several times.
8366 */
8367 if (screen_attr & HL_STANDOUT)
8368 {
8369 if (STRCMP(T_SE, T_ME) == 0)
8370 do_ME = TRUE;
8371 else
8372 out_str(T_SE);
8373 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008374 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008375 {
8376 if (STRCMP(T_UCE, T_ME) == 0)
8377 do_ME = TRUE;
8378 else
8379 out_str(T_UCE);
8380 }
8381 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008382 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {
8384 if (STRCMP(T_UE, T_ME) == 0)
8385 do_ME = TRUE;
8386 else
8387 out_str(T_UE);
8388 }
8389 if (screen_attr & HL_ITALIC)
8390 {
8391 if (STRCMP(T_CZR, T_ME) == 0)
8392 do_ME = TRUE;
8393 else
8394 out_str(T_CZR);
8395 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008396 if (screen_attr & HL_STRIKETHROUGH)
8397 {
8398 if (STRCMP(T_STE, T_ME) == 0)
8399 do_ME = TRUE;
8400 else
8401 out_str(T_STE);
8402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8404 out_str(T_ME);
8405
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008406#ifdef FEAT_TERMGUICOLORS
8407 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008409 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008410 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008411 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008412 term_bg_rgb_color(cterm_normal_bg_gui_color);
8413 }
8414 else
8415#endif
8416 {
8417 if (t_colors > 1)
8418 {
8419 /* set Normal cterm colors */
8420 if (cterm_normal_fg_color != 0)
8421 term_fg_color(cterm_normal_fg_color - 1);
8422 if (cterm_normal_bg_color != 0)
8423 term_bg_color(cterm_normal_bg_color - 1);
8424 if (cterm_normal_fg_bold)
8425 out_str(T_MD);
8426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 }
8428 }
8429 }
8430 screen_attr = 0;
8431}
8432
8433/*
8434 * Reset the colors for a cterm. Used when leaving Vim.
8435 * The machine specific code may override this again.
8436 */
8437 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008438reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008440 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {
8442 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008443#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008444 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8445 || cterm_normal_bg_gui_color != INVALCOLOR)
8446 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008447#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {
8451 out_str(T_OP);
8452 screen_attr = -1;
8453 }
8454 if (cterm_normal_fg_bold)
8455 {
8456 out_str(T_ME);
8457 screen_attr = -1;
8458 }
8459 }
8460}
8461
8462/*
8463 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8464 * using the attributes from ScreenAttrs["off"].
8465 */
8466 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008467screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
8469 int attr;
8470
8471 /* Check for illegal values, just in case (could happen just after
8472 * resizing). */
8473 if (row >= screen_Rows || col >= screen_Columns)
8474 return;
8475
Bram Moolenaar494838a2015-02-10 19:20:37 +01008476 /* Outputting a character in the last cell on the screen may scroll the
8477 * screen up. Only do it when the "xn" termcap property is set, otherwise
8478 * mark the character invalid (update it when scrolled up). */
8479 if (*T_XN == NUL
8480 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481#ifdef FEAT_RIGHTLEFT
8482 /* account for first command-line character in rightleft mode */
8483 && !cmdmsg_rl
8484#endif
8485 )
8486 {
8487 ScreenAttrs[off] = (sattr_T)-1;
8488 return;
8489 }
8490
8491 /*
8492 * Stop highlighting first, so it's easier to move the cursor.
8493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 if (screen_char_attr != 0)
8495 attr = screen_char_attr;
8496 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 attr = ScreenAttrs[off];
8498 if (screen_attr != attr)
8499 screen_stop_highlight();
8500
8501 windgoto(row, col);
8502
8503 if (screen_attr != attr)
8504 screen_start_highlight(attr);
8505
8506#ifdef FEAT_MBYTE
8507 if (enc_utf8 && ScreenLinesUC[off] != 0)
8508 {
8509 char_u buf[MB_MAXBYTES + 1];
8510
Bram Moolenaarcb070082016-04-02 22:14:51 +02008511 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008512 {
8513 if (*p_ambw == 'd'
8514# ifdef FEAT_GUI
8515 && !gui.in_use
8516# endif
8517 )
8518 {
8519 /* Clear the two screen cells. If the character is actually
8520 * single width it won't change the second cell. */
8521 out_str((char_u *)" ");
8522 term_windgoto(row, col);
8523 }
8524 /* not sure where the cursor is after drawing the ambiguous width
8525 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008526 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008527 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008528 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008530
8531 /* Convert the UTF-8 character to bytes and write it. */
8532 buf[utfc_char2bytes(off, buf)] = NUL;
8533 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 }
8535 else
8536#endif
8537 {
8538#ifdef FEAT_MBYTE
8539 out_flush_check();
8540#endif
8541 out_char(ScreenLines[off]);
8542#ifdef FEAT_MBYTE
8543 /* double-byte character in single-width cell */
8544 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8545 out_char(ScreenLines2[off]);
8546#endif
8547 }
8548
8549 screen_cur_col++;
8550}
8551
8552#ifdef FEAT_MBYTE
8553
8554/*
8555 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8556 * on the screen at position 'row' and 'col'.
8557 * The attributes of the first byte is used for all. This is required to
8558 * output the two bytes of a double-byte character with nothing in between.
8559 */
8560 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008561screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
8563 /* Check for illegal values (could be wrong when screen was resized). */
8564 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8565 return;
8566
8567 /* Outputting the last character on the screen may scrollup the screen.
8568 * Don't to it! Mark the character invalid (update it when scrolled up) */
8569 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8570 {
8571 ScreenAttrs[off] = (sattr_T)-1;
8572 return;
8573 }
8574
8575 /* Output the first byte normally (positions the cursor), then write the
8576 * second byte directly. */
8577 screen_char(off, row, col);
8578 out_char(ScreenLines[off + 1]);
8579 ++screen_cur_col;
8580}
8581#endif
8582
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583/*
8584 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8585 * This uses the contents of ScreenLines[] and doesn't change it.
8586 */
8587 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008588screen_draw_rectangle(
8589 int row,
8590 int col,
8591 int height,
8592 int width,
8593 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
8595 int r, c;
8596 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008597#ifdef FEAT_MBYTE
8598 int max_off;
8599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008601 /* Can't use ScreenLines unless initialized */
8602 if (ScreenLines == NULL)
8603 return;
8604
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 if (invert)
8606 screen_char_attr = HL_INVERSE;
8607 for (r = row; r < row + height; ++r)
8608 {
8609 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008610#ifdef FEAT_MBYTE
8611 max_off = off + screen_Columns;
8612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 for (c = col; c < col + width; ++c)
8614 {
8615#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008616 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {
8618 screen_char_2(off + c, r, c);
8619 ++c;
8620 }
8621 else
8622#endif
8623 {
8624 screen_char(off + c, r, c);
8625#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008626 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 ++c;
8628#endif
8629 }
8630 }
8631 }
8632 screen_char_attr = 0;
8633}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635/*
8636 * Redraw the characters for a vertically split window.
8637 */
8638 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008639redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
8641 int col;
8642 int width;
8643
8644# ifdef FEAT_CLIPBOARD
8645 clip_may_clear_selection(row, end - 1);
8646# endif
8647
8648 if (wp == NULL)
8649 {
8650 col = 0;
8651 width = Columns;
8652 }
8653 else
8654 {
8655 col = wp->w_wincol;
8656 width = wp->w_width;
8657 }
8658 screen_draw_rectangle(row, col, end - row, width, FALSE);
8659}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008661 static void
8662space_to_screenline(int off, int attr)
8663{
8664 ScreenLines[off] = ' ';
8665 ScreenAttrs[off] = attr;
8666# ifdef FEAT_MBYTE
8667 if (enc_utf8)
8668 ScreenLinesUC[off] = 0;
8669# endif
8670}
8671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672/*
8673 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8674 * with character 'c1' in first column followed by 'c2' in the other columns.
8675 * Use attributes 'attr'.
8676 */
8677 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008678screen_fill(
8679 int start_row,
8680 int end_row,
8681 int start_col,
8682 int end_col,
8683 int c1,
8684 int c2,
8685 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686{
8687 int row;
8688 int col;
8689 int off;
8690 int end_off;
8691 int did_delete;
8692 int c;
8693 int norm_term;
8694#if defined(FEAT_GUI) || defined(UNIX)
8695 int force_next = FALSE;
8696#endif
8697
8698 if (end_row > screen_Rows) /* safety check */
8699 end_row = screen_Rows;
8700 if (end_col > screen_Columns) /* safety check */
8701 end_col = screen_Columns;
8702 if (ScreenLines == NULL
8703 || start_row >= end_row
8704 || start_col >= end_col) /* nothing to do */
8705 return;
8706
8707 /* it's a "normal" terminal when not in a GUI or cterm */
8708 norm_term = (
8709#ifdef FEAT_GUI
8710 !gui.in_use &&
8711#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008712 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 for (row = start_row; row < end_row; ++row)
8714 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008715#ifdef FEAT_MBYTE
8716 if (has_mbyte
8717# ifdef FEAT_GUI
8718 && !gui.in_use
8719# endif
8720 )
8721 {
8722 /* When drawing over the right halve of a double-wide char clear
8723 * out the left halve. When drawing over the left halve of a
8724 * double wide-char clear out the right halve. Only needed in a
8725 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008726 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008727 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008728 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008729 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008730 }
8731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 /*
8733 * Try to use delete-line termcap code, when no attributes or in a
8734 * "normal" terminal, where a bold/italic space is just a
8735 * space.
8736 */
8737 did_delete = FALSE;
8738 if (c2 == ' '
8739 && end_col == Columns
8740 && can_clear(T_CE)
8741 && (attr == 0
8742 || (norm_term
8743 && attr <= HL_ALL
8744 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8745 {
8746 /*
8747 * check if we really need to clear something
8748 */
8749 col = start_col;
8750 if (c1 != ' ') /* don't clear first char */
8751 ++col;
8752
8753 off = LineOffset[row] + col;
8754 end_off = LineOffset[row] + end_col;
8755
8756 /* skip blanks (used often, keep it fast!) */
8757#ifdef FEAT_MBYTE
8758 if (enc_utf8)
8759 while (off < end_off && ScreenLines[off] == ' '
8760 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8761 ++off;
8762 else
8763#endif
8764 while (off < end_off && ScreenLines[off] == ' '
8765 && ScreenAttrs[off] == 0)
8766 ++off;
8767 if (off < end_off) /* something to be cleared */
8768 {
8769 col = off - LineOffset[row];
8770 screen_stop_highlight();
8771 term_windgoto(row, col);/* clear rest of this screen line */
8772 out_str(T_CE);
8773 screen_start(); /* don't know where cursor is now */
8774 col = end_col - col;
8775 while (col--) /* clear chars in ScreenLines */
8776 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008777 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 ++off;
8779 }
8780 }
8781 did_delete = TRUE; /* the chars are cleared now */
8782 }
8783
8784 off = LineOffset[row] + start_col;
8785 c = c1;
8786 for (col = start_col; col < end_col; ++col)
8787 {
8788 if (ScreenLines[off] != c
8789#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008790 || (enc_utf8 && (int)ScreenLinesUC[off]
8791 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792#endif
8793 || ScreenAttrs[off] != attr
8794#if defined(FEAT_GUI) || defined(UNIX)
8795 || force_next
8796#endif
8797 )
8798 {
8799#if defined(FEAT_GUI) || defined(UNIX)
8800 /* The bold trick may make a single row of pixels appear in
8801 * the next character. When a bold character is removed, the
8802 * next character should be redrawn too. This happens for our
8803 * own GUI and for some xterms. */
8804 if (
8805# ifdef FEAT_GUI
8806 gui.in_use
8807# endif
8808# if defined(FEAT_GUI) && defined(UNIX)
8809 ||
8810# endif
8811# ifdef UNIX
8812 term_is_xterm
8813# endif
8814 )
8815 {
8816 if (ScreenLines[off] != ' '
8817 && (ScreenAttrs[off] > HL_ALL
8818 || ScreenAttrs[off] & HL_BOLD))
8819 force_next = TRUE;
8820 else
8821 force_next = FALSE;
8822 }
8823#endif
8824 ScreenLines[off] = c;
8825#ifdef FEAT_MBYTE
8826 if (enc_utf8)
8827 {
8828 if (c >= 0x80)
8829 {
8830 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008831 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 }
8833 else
8834 ScreenLinesUC[off] = 0;
8835 }
8836#endif
8837 ScreenAttrs[off] = attr;
8838 if (!did_delete || c != ' ')
8839 screen_char(off, row, col);
8840 }
8841 ++off;
8842 if (col == start_col)
8843 {
8844 if (did_delete)
8845 break;
8846 c = c2;
8847 }
8848 }
8849 if (end_col == Columns)
8850 LineWraps[row] = FALSE;
8851 if (row == Rows - 1) /* overwritten the command line */
8852 {
8853 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008854 if (start_col == 0 && end_col == Columns
8855 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008857 if (start_col == 0)
8858 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 }
8860 }
8861}
8862
8863/*
8864 * Check if there should be a delay. Used before clearing or redrawing the
8865 * screen or the command line.
8866 */
8867 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008868check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
8870 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8871 && !did_wait_return
8872 && emsg_silent == 0)
8873 {
8874 out_flush();
8875 ui_delay(1000L, TRUE);
8876 emsg_on_display = FALSE;
8877 if (check_msg_scroll)
8878 msg_scroll = FALSE;
8879 }
8880}
8881
8882/*
8883 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008884 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 * Returns TRUE if there is a valid screen to write to.
8886 * Returns FALSE when starting up and screen not initialized yet.
8887 */
8888 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008889screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008891 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 return (ScreenLines != NULL);
8893}
8894
8895/*
8896 * Resize the shell to Rows and Columns.
8897 * Allocate ScreenLines[] and associated items.
8898 *
8899 * There may be some time between setting Rows and Columns and (re)allocating
8900 * ScreenLines[]. This happens when starting up and when (manually) changing
8901 * the shell size. Always use screen_Rows and screen_Columns to access items
8902 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8903 * final size of the shell is needed.
8904 */
8905 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008906screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907{
8908 int new_row, old_row;
8909#ifdef FEAT_GUI
8910 int old_Rows;
8911#endif
8912 win_T *wp;
8913 int outofmem = FALSE;
8914 int len;
8915 schar_T *new_ScreenLines;
8916#ifdef FEAT_MBYTE
8917 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008918 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008920 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921#endif
8922 sattr_T *new_ScreenAttrs;
8923 unsigned *new_LineOffset;
8924 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008925 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008926 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008928 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008929 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008931retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 /*
8933 * Allocation of the screen buffers is done only when the size changes and
8934 * when Rows and Columns have been set and we have started doing full
8935 * screen stuff.
8936 */
8937 if ((ScreenLines != NULL
8938 && Rows == screen_Rows
8939 && Columns == screen_Columns
8940#ifdef FEAT_MBYTE
8941 && enc_utf8 == (ScreenLinesUC != NULL)
8942 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008943 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944#endif
8945 )
8946 || Rows == 0
8947 || Columns == 0
8948 || (!full_screen && ScreenLines == NULL))
8949 return;
8950
8951 /*
8952 * It's possible that we produce an out-of-memory message below, which
8953 * will cause this function to be called again. To break the loop, just
8954 * return here.
8955 */
8956 if (entered)
8957 return;
8958 entered = TRUE;
8959
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008960 /*
8961 * Note that the window sizes are updated before reallocating the arrays,
8962 * thus we must not redraw here!
8963 */
8964 ++RedrawingDisabled;
8965
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 win_new_shellsize(); /* fit the windows in the new sized shell */
8967
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 comp_col(); /* recompute columns for shown command and ruler */
8969
8970 /*
8971 * We're changing the size of the screen.
8972 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8973 * - Move lines from the old arrays into the new arrays, clear extra
8974 * lines (unless the screen is going to be cleared).
8975 * - Free the old arrays.
8976 *
8977 * If anything fails, make ScreenLines NULL, so we don't do anything!
8978 * Continuing with the old ScreenLines may result in a crash, because the
8979 * size is wrong.
8980 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008981 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008983 if (aucmd_win != NULL)
8984 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
8986 new_ScreenLines = (schar_T *)lalloc((long_u)(
8987 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8988#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008989 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 if (enc_utf8)
8991 {
8992 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8993 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008994 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008995 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8997 }
8998 if (enc_dbcs == DBCS_JPNU)
8999 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9000 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9001#endif
9002 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9003 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9004 new_LineOffset = (unsigned *)lalloc((long_u)(
9005 Rows * sizeof(unsigned)), FALSE);
9006 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009007 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009009 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 {
9011 if (win_alloc_lines(wp) == FAIL)
9012 {
9013 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009014 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 }
9016 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009017 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9018 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009019 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009020give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009022#ifdef FEAT_MBYTE
9023 for (i = 0; i < p_mco; ++i)
9024 if (new_ScreenLinesC[i] == NULL)
9025 break;
9026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 if (new_ScreenLines == NULL
9028#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009029 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9031#endif
9032 || new_ScreenAttrs == NULL
9033 || new_LineOffset == NULL
9034 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009035 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 || outofmem)
9037 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009038 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009039 {
9040 /* guess the size */
9041 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9042
9043 /* Remember we did this to avoid getting outofmem messages over
9044 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009045 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009046 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009047 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009049 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009050 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009051 VIM_CLEAR(new_ScreenLinesC[i]);
9052 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009054 VIM_CLEAR(new_ScreenAttrs);
9055 VIM_CLEAR(new_LineOffset);
9056 VIM_CLEAR(new_LineWraps);
9057 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 }
9059 else
9060 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009061 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009062
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 for (new_row = 0; new_row < Rows; ++new_row)
9064 {
9065 new_LineOffset[new_row] = new_row * Columns;
9066 new_LineWraps[new_row] = FALSE;
9067
9068 /*
9069 * If the screen is not going to be cleared, copy as much as
9070 * possible from the old screen to the new one and clear the rest
9071 * (used when resizing the window at the "--more--" prompt or when
9072 * executing an external command, for the GUI).
9073 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009074 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 {
9076 (void)vim_memset(new_ScreenLines + new_row * Columns,
9077 ' ', (size_t)Columns * sizeof(schar_T));
9078#ifdef FEAT_MBYTE
9079 if (enc_utf8)
9080 {
9081 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9082 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009083 for (i = 0; i < p_mco; ++i)
9084 (void)vim_memset(new_ScreenLinesC[i]
9085 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 0, (size_t)Columns * sizeof(u8char_T));
9087 }
9088 if (enc_dbcs == DBCS_JPNU)
9089 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9090 0, (size_t)Columns * sizeof(schar_T));
9091#endif
9092 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9093 0, (size_t)Columns * sizeof(sattr_T));
9094 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009095 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 {
9097 if (screen_Columns < Columns)
9098 len = screen_Columns;
9099 else
9100 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009101#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009102 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009103 * may be invalid now. Also when p_mco changes. */
9104 if (!(enc_utf8 && ScreenLinesUC == NULL)
9105 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009106#endif
9107 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9108 ScreenLines + LineOffset[old_row],
9109 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009111 if (enc_utf8 && ScreenLinesUC != NULL
9112 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 {
9114 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9115 ScreenLinesUC + LineOffset[old_row],
9116 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009117 for (i = 0; i < p_mco; ++i)
9118 mch_memmove(new_ScreenLinesC[i]
9119 + new_LineOffset[new_row],
9120 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 (size_t)len * sizeof(u8char_T));
9122 }
9123 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9124 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9125 ScreenLines2 + LineOffset[old_row],
9126 (size_t)len * sizeof(schar_T));
9127#endif
9128 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9129 ScreenAttrs + LineOffset[old_row],
9130 (size_t)len * sizeof(sattr_T));
9131 }
9132 }
9133 }
9134 /* Use the last line of the screen for the current line. */
9135 current_ScreenLine = new_ScreenLines + Rows * Columns;
9136 }
9137
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009138 free_screenlines();
9139
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 ScreenLines = new_ScreenLines;
9141#ifdef FEAT_MBYTE
9142 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009143 for (i = 0; i < p_mco; ++i)
9144 ScreenLinesC[i] = new_ScreenLinesC[i];
9145 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 ScreenLines2 = new_ScreenLines2;
9147#endif
9148 ScreenAttrs = new_ScreenAttrs;
9149 LineOffset = new_LineOffset;
9150 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009151 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152
9153 /* It's important that screen_Rows and screen_Columns reflect the actual
9154 * size of ScreenLines[]. Set them before calling anything. */
9155#ifdef FEAT_GUI
9156 old_Rows = screen_Rows;
9157#endif
9158 screen_Rows = Rows;
9159 screen_Columns = Columns;
9160
9161 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009162 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 screenclear2();
9164
9165#ifdef FEAT_GUI
9166 else if (gui.in_use
9167 && !gui.starting
9168 && ScreenLines != NULL
9169 && old_Rows != Rows)
9170 {
9171 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9172 /*
9173 * Adjust the position of the cursor, for when executing an external
9174 * command.
9175 */
9176 if (msg_row >= Rows) /* Rows got smaller */
9177 msg_row = Rows - 1; /* put cursor at last row */
9178 else if (Rows > old_Rows) /* Rows got bigger */
9179 msg_row += Rows - old_Rows; /* put cursor in same place */
9180 if (msg_col >= Columns) /* Columns got smaller */
9181 msg_col = Columns - 1; /* put cursor at last column */
9182 }
9183#endif
9184
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009186 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009187
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009188 /*
9189 * Do not apply autocommands more than 3 times to avoid an endless loop
9190 * in case applying autocommands always changes Rows or Columns.
9191 */
9192 if (starting == 0 && ++retry_count <= 3)
9193 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009194 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009195 /* In rare cases, autocommands may have altered Rows or Columns,
9196 * jump back to check if we need to allocate the screen again. */
9197 goto retry;
9198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199}
9200
9201 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009202free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009203{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009204#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009205 int i;
9206
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009207 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009208 for (i = 0; i < Screen_mco; ++i)
9209 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009210 vim_free(ScreenLines2);
9211#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009212 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009213 vim_free(ScreenAttrs);
9214 vim_free(LineOffset);
9215 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009216 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009217}
9218
9219 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009220screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221{
9222 check_for_delay(FALSE);
9223 screenalloc(FALSE); /* allocate screen buffers if size changed */
9224 screenclear2(); /* clear the screen */
9225}
9226
9227 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009228screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229{
9230 int i;
9231
9232 if (starting == NO_SCREEN || ScreenLines == NULL
9233#ifdef FEAT_GUI
9234 || (gui.in_use && gui.starting)
9235#endif
9236 )
9237 return;
9238
9239#ifdef FEAT_GUI
9240 if (!gui.in_use)
9241#endif
9242 screen_attr = -1; /* force setting the Normal colors */
9243 screen_stop_highlight(); /* don't want highlighting here */
9244
9245#ifdef FEAT_CLIPBOARD
9246 /* disable selection without redrawing it */
9247 clip_scroll_selection(9999);
9248#endif
9249
9250 /* blank out ScreenLines */
9251 for (i = 0; i < Rows; ++i)
9252 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009253 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 LineWraps[i] = FALSE;
9255 }
9256
9257 if (can_clear(T_CL))
9258 {
9259 out_str(T_CL); /* clear the display */
9260 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009261 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 }
9263 else
9264 {
9265 /* can't clear the screen, mark all chars with invalid attributes */
9266 for (i = 0; i < Rows; ++i)
9267 lineinvalid(LineOffset[i], (int)Columns);
9268 clear_cmdline = TRUE;
9269 }
9270
9271 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9272
9273 win_rest_invalid(firstwin);
9274 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009275 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 if (must_redraw == CLEAR) /* no need to clear again */
9277 must_redraw = NOT_VALID;
9278 compute_cmdrow();
9279 msg_row = cmdline_row; /* put cursor on last line for messages */
9280 msg_col = 0;
9281 screen_start(); /* don't know where cursor is now */
9282 msg_scrolled = 0; /* can't scroll back */
9283 msg_didany = FALSE;
9284 msg_didout = FALSE;
9285}
9286
9287/*
9288 * Clear one line in ScreenLines.
9289 */
9290 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009291lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292{
9293 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9294#ifdef FEAT_MBYTE
9295 if (enc_utf8)
9296 (void)vim_memset(ScreenLinesUC + off, 0,
9297 (size_t)width * sizeof(u8char_T));
9298#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009299 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300}
9301
9302/*
9303 * Mark one line in ScreenLines invalid by setting the attributes to an
9304 * invalid value.
9305 */
9306 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009307lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9310}
9311
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312/*
9313 * Copy part of a Screenline for vertically split window "wp".
9314 */
9315 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009316linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
9318 unsigned off_to = LineOffset[to] + wp->w_wincol;
9319 unsigned off_from = LineOffset[from] + wp->w_wincol;
9320
9321 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9322 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009323#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 if (enc_utf8)
9325 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009326 int i;
9327
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9329 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009330 for (i = 0; i < p_mco; ++i)
9331 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9332 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 }
9334 if (enc_dbcs == DBCS_JPNU)
9335 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9336 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9339 wp->w_width * sizeof(sattr_T));
9340}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
9342/*
9343 * Return TRUE if clearing with term string "p" would work.
9344 * It can't work when the string is empty or it won't set the right background.
9345 */
9346 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009347can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348{
9349 return (*p != NUL && (t_colors <= 1
9350#ifdef FEAT_GUI
9351 || gui.in_use
9352#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009353#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009354 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009355 || (!p_tgc && cterm_normal_bg_color == 0)
9356#else
9357 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009358#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009359 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360}
9361
9362/*
9363 * Reset cursor position. Use whenever cursor was moved because of outputting
9364 * something directly to the screen (shell commands) or a terminal control
9365 * code.
9366 */
9367 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009368screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 screen_cur_row = screen_cur_col = 9999;
9371}
9372
9373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 * Move the cursor to position "row","col" in the screen.
9375 * This tries to find the most efficient way to move, minimizing the number of
9376 * characters sent to the terminal.
9377 */
9378 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009379windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009381 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 int i;
9383 int plan;
9384 int cost;
9385 int wouldbe_col;
9386 int noinvcurs;
9387 char_u *bs;
9388 int goto_cost;
9389 int attr;
9390
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009391#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9393
9394#define PLAN_LE 1
9395#define PLAN_CR 2
9396#define PLAN_NL 3
9397#define PLAN_WRITE 4
9398 /* Can't use ScreenLines unless initialized */
9399 if (ScreenLines == NULL)
9400 return;
9401
9402 if (col != screen_cur_col || row != screen_cur_row)
9403 {
9404 /* Check for valid position. */
9405 if (row < 0) /* window without text lines? */
9406 row = 0;
9407 if (row >= screen_Rows)
9408 row = screen_Rows - 1;
9409 if (col >= screen_Columns)
9410 col = screen_Columns - 1;
9411
9412 /* check if no cursor movement is allowed in highlight mode */
9413 if (screen_attr && *T_MS == NUL)
9414 noinvcurs = HIGHL_COST;
9415 else
9416 noinvcurs = 0;
9417 goto_cost = GOTO_COST + noinvcurs;
9418
9419 /*
9420 * Plan how to do the positioning:
9421 * 1. Use CR to move it to column 0, same row.
9422 * 2. Use T_LE to move it a few columns to the left.
9423 * 3. Use NL to move a few lines down, column 0.
9424 * 4. Move a few columns to the right with T_ND or by writing chars.
9425 *
9426 * Don't do this if the cursor went beyond the last column, the cursor
9427 * position is unknown then (some terminals wrap, some don't )
9428 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009429 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 * characters to move the cursor to the right.
9431 */
9432 if (row >= screen_cur_row && screen_cur_col < Columns)
9433 {
9434 /*
9435 * If the cursor is in the same row, bigger col, we can use CR
9436 * or T_LE.
9437 */
9438 bs = NULL; /* init for GCC */
9439 attr = screen_attr;
9440 if (row == screen_cur_row && col < screen_cur_col)
9441 {
9442 /* "le" is preferred over "bc", because "bc" is obsolete */
9443 if (*T_LE)
9444 bs = T_LE; /* "cursor left" */
9445 else
9446 bs = T_BC; /* "backspace character (old) */
9447 if (*bs)
9448 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9449 else
9450 cost = 999;
9451 if (col + 1 < cost) /* using CR is less characters */
9452 {
9453 plan = PLAN_CR;
9454 wouldbe_col = 0;
9455 cost = 1; /* CR is just one character */
9456 }
9457 else
9458 {
9459 plan = PLAN_LE;
9460 wouldbe_col = col;
9461 }
9462 if (noinvcurs) /* will stop highlighting */
9463 {
9464 cost += noinvcurs;
9465 attr = 0;
9466 }
9467 }
9468
9469 /*
9470 * If the cursor is above where we want to be, we can use CR LF.
9471 */
9472 else if (row > screen_cur_row)
9473 {
9474 plan = PLAN_NL;
9475 wouldbe_col = 0;
9476 cost = (row - screen_cur_row) * 2; /* CR LF */
9477 if (noinvcurs) /* will stop highlighting */
9478 {
9479 cost += noinvcurs;
9480 attr = 0;
9481 }
9482 }
9483
9484 /*
9485 * If the cursor is in the same row, smaller col, just use write.
9486 */
9487 else
9488 {
9489 plan = PLAN_WRITE;
9490 wouldbe_col = screen_cur_col;
9491 cost = 0;
9492 }
9493
9494 /*
9495 * Check if any characters that need to be written have the
9496 * correct attributes. Also avoid UTF-8 characters.
9497 */
9498 i = col - wouldbe_col;
9499 if (i > 0)
9500 cost += i;
9501 if (cost < goto_cost && i > 0)
9502 {
9503 /*
9504 * Check if the attributes are correct without additionally
9505 * stopping highlighting.
9506 */
9507 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9508 while (i && *p++ == attr)
9509 --i;
9510 if (i != 0)
9511 {
9512 /*
9513 * Try if it works when highlighting is stopped here.
9514 */
9515 if (*--p == 0)
9516 {
9517 cost += noinvcurs;
9518 while (i && *p++ == 0)
9519 --i;
9520 }
9521 if (i != 0)
9522 cost = 999; /* different attributes, don't do it */
9523 }
9524#ifdef FEAT_MBYTE
9525 if (enc_utf8)
9526 {
9527 /* Don't use an UTF-8 char for positioning, it's slow. */
9528 for (i = wouldbe_col; i < col; ++i)
9529 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9530 {
9531 cost = 999;
9532 break;
9533 }
9534 }
9535#endif
9536 }
9537
9538 /*
9539 * We can do it without term_windgoto()!
9540 */
9541 if (cost < goto_cost)
9542 {
9543 if (plan == PLAN_LE)
9544 {
9545 if (noinvcurs)
9546 screen_stop_highlight();
9547 while (screen_cur_col > col)
9548 {
9549 out_str(bs);
9550 --screen_cur_col;
9551 }
9552 }
9553 else if (plan == PLAN_CR)
9554 {
9555 if (noinvcurs)
9556 screen_stop_highlight();
9557 out_char('\r');
9558 screen_cur_col = 0;
9559 }
9560 else if (plan == PLAN_NL)
9561 {
9562 if (noinvcurs)
9563 screen_stop_highlight();
9564 while (screen_cur_row < row)
9565 {
9566 out_char('\n');
9567 ++screen_cur_row;
9568 }
9569 screen_cur_col = 0;
9570 }
9571
9572 i = col - screen_cur_col;
9573 if (i > 0)
9574 {
9575 /*
9576 * Use cursor-right if it's one character only. Avoids
9577 * removing a line of pixels from the last bold char, when
9578 * using the bold trick in the GUI.
9579 */
9580 if (T_ND[0] != NUL && T_ND[1] == NUL)
9581 {
9582 while (i-- > 0)
9583 out_char(*T_ND);
9584 }
9585 else
9586 {
9587 int off;
9588
9589 off = LineOffset[row] + screen_cur_col;
9590 while (i-- > 0)
9591 {
9592 if (ScreenAttrs[off] != screen_attr)
9593 screen_stop_highlight();
9594#ifdef FEAT_MBYTE
9595 out_flush_check();
9596#endif
9597 out_char(ScreenLines[off]);
9598#ifdef FEAT_MBYTE
9599 if (enc_dbcs == DBCS_JPNU
9600 && ScreenLines[off] == 0x8e)
9601 out_char(ScreenLines2[off]);
9602#endif
9603 ++off;
9604 }
9605 }
9606 }
9607 }
9608 }
9609 else
9610 cost = 999;
9611
9612 if (cost >= goto_cost)
9613 {
9614 if (noinvcurs)
9615 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009616 if (row == screen_cur_row && (col > screen_cur_col)
9617 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 term_cursor_right(col - screen_cur_col);
9619 else
9620 term_windgoto(row, col);
9621 }
9622 screen_cur_row = row;
9623 screen_cur_col = col;
9624 }
9625}
9626
9627/*
9628 * Set cursor to its position in the current window.
9629 */
9630 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009631setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009633 setcursor_mayforce(FALSE);
9634}
9635
9636/*
9637 * Set cursor to its position in the current window.
9638 * When "force" is TRUE also when not redrawing.
9639 */
9640 void
9641setcursor_mayforce(int force)
9642{
9643 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 {
9645 validate_cursor();
9646 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009647 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009649 /* With 'rightleft' set and the cursor on a double-wide
9650 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009651 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009653 (has_mbyte
9654 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9655 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656# endif
9657 1)) :
9658#endif
9659 curwin->w_wcol));
9660 }
9661}
9662
9663
9664/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009665 * Insert 'line_count' lines at 'row' in window 'wp'.
9666 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9667 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 * scrolling.
9669 * Returns FAIL if the lines are not inserted, OK for success.
9670 */
9671 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009672win_ins_lines(
9673 win_T *wp,
9674 int row,
9675 int line_count,
9676 int invalid,
9677 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678{
9679 int did_delete;
9680 int nextrow;
9681 int lastrow;
9682 int retval;
9683
9684 if (invalid)
9685 wp->w_lines_valid = 0;
9686
9687 if (wp->w_height < 5)
9688 return FAIL;
9689
9690 if (line_count > wp->w_height - row)
9691 line_count = wp->w_height - row;
9692
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009693 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 if (retval != MAYBE)
9695 return retval;
9696
9697 /*
9698 * If there is a next window or a status line, we first try to delete the
9699 * lines at the bottom to avoid messing what is after the window.
9700 * If this fails and there are following windows, don't do anything to avoid
9701 * messing up those windows, better just redraw.
9702 */
9703 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 if (wp->w_next != NULL || wp->w_status_height)
9705 {
9706 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009707 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 did_delete = TRUE;
9709 else if (wp->w_next)
9710 return FAIL;
9711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 /*
9713 * if no lines deleted, blank the lines that will end up below the window
9714 */
9715 if (!did_delete)
9716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009719 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 lastrow = nextrow + line_count;
9721 if (lastrow > Rows)
9722 lastrow = Rows;
9723 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009724 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 ' ', ' ', 0);
9726 }
9727
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009728 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 == FAIL)
9730 {
9731 /* deletion will have messed up other windows */
9732 if (did_delete)
9733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 win_rest_invalid(W_NEXT(wp));
9736 }
9737 return FAIL;
9738 }
9739
9740 return OK;
9741}
9742
9743/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009744 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9746 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9747 * scrolling
9748 * Return OK for success, FAIL if the lines are not deleted.
9749 */
9750 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009751win_del_lines(
9752 win_T *wp,
9753 int row,
9754 int line_count,
9755 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009756 int mayclear,
9757 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758{
9759 int retval;
9760
9761 if (invalid)
9762 wp->w_lines_valid = 0;
9763
9764 if (line_count > wp->w_height - row)
9765 line_count = wp->w_height - row;
9766
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009767 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 if (retval != MAYBE)
9769 return retval;
9770
9771 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009772 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 return FAIL;
9774
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 /*
9776 * If there are windows or status lines below, try to put them at the
9777 * correct place. If we can't do that, they have to be redrawn.
9778 */
9779 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9780 {
9781 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009782 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 {
9784 wp->w_redr_status = TRUE;
9785 win_rest_invalid(wp->w_next);
9786 }
9787 }
9788 /*
9789 * If this is the last window and there is no status line, redraw the
9790 * command line later.
9791 */
9792 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 redraw_cmdline = TRUE;
9794 return OK;
9795}
9796
9797/*
9798 * Common code for win_ins_lines() and win_del_lines().
9799 * Returns OK or FAIL when the work has been done.
9800 * Returns MAYBE when not finished yet.
9801 */
9802 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009803win_do_lines(
9804 win_T *wp,
9805 int row,
9806 int line_count,
9807 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009808 int del,
9809 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810{
9811 int retval;
9812
9813 if (!redrawing() || line_count <= 0)
9814 return FAIL;
9815
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009816 /* When inserting lines would result in loss of command output, just redraw
9817 * the lines. */
9818 if (no_win_do_lines_ins && !del)
9819 return FAIL;
9820
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009822 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009824 if (!no_win_do_lines_ins)
9825 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 return FAIL;
9827 }
9828
9829 /*
9830 * Delete all remaining lines
9831 */
9832 if (row + line_count >= wp->w_height)
9833 {
9834 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009835 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 ' ', ' ', 0);
9837 return OK;
9838 }
9839
9840 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009841 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009843 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009845 if (!no_win_do_lines_ins)
9846 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847
9848 /*
9849 * If the terminal can set a scroll region, use that.
9850 * Always do this in a vertically split window. This will redraw from
9851 * ScreenLines[] when t_CV isn't defined. That's faster than using
9852 * win_line().
9853 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009854 * a character in the lower right corner of the scroll region may cause a
9855 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009857 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 scroll_region_set(wp, row);
9861 if (del)
9862 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009863 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 else
9865 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009866 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 scroll_region_reset();
9869 return retval;
9870 }
9871
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9873 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874
9875 return MAYBE;
9876}
9877
9878/*
9879 * window 'wp' and everything after it is messed up, mark it for redraw
9880 */
9881 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009882win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 {
9886 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 wp->w_redr_status = TRUE;
9888 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 }
9890 redraw_cmdline = TRUE;
9891}
9892
9893/*
9894 * The rest of the routines in this file perform screen manipulations. The
9895 * given operation is performed physically on the screen. The corresponding
9896 * change is also made to the internal screen image. In this way, the editor
9897 * anticipates the effect of editing changes on the appearance of the screen.
9898 * That way, when we call screenupdate a complete redraw isn't usually
9899 * necessary. Another advantage is that we can keep adding code to anticipate
9900 * screen changes, and in the meantime, everything still works.
9901 */
9902
9903/*
9904 * types for inserting or deleting lines
9905 */
9906#define USE_T_CAL 1
9907#define USE_T_CDL 2
9908#define USE_T_AL 3
9909#define USE_T_CE 4
9910#define USE_T_DL 5
9911#define USE_T_SR 6
9912#define USE_NL 7
9913#define USE_T_CD 8
9914#define USE_REDRAW 9
9915
9916/*
9917 * insert lines on the screen and update ScreenLines[]
9918 * 'end' is the line after the scrolled part. Normally it is Rows.
9919 * When scrolling region used 'off' is the offset from the top for the region.
9920 * 'row' and 'end' are relative to the start of the region.
9921 *
9922 * return FAIL for failure, OK for success.
9923 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009924 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009925screen_ins_lines(
9926 int off,
9927 int row,
9928 int line_count,
9929 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009930 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009931 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932{
9933 int i;
9934 int j;
9935 unsigned temp;
9936 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009937 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 int type;
9939 int result_empty;
9940 int can_ce = can_clear(T_CE);
9941
9942 /*
9943 * FAIL if
9944 * - there is no valid screen
9945 * - the screen has to be redrawn completely
9946 * - the line count is less than one
9947 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009948 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009950 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9951#ifdef FEAT_CLIPBOARD
9952 || (clip_star.state != SELECT_CLEARED
9953 && redrawing_for_callback > 0)
9954#endif
9955 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 return FAIL;
9957
9958 /*
9959 * There are seven ways to insert lines:
9960 * 0. When in a vertically split window and t_CV isn't set, redraw the
9961 * characters from ScreenLines[].
9962 * 1. Use T_CD (clear to end of display) if it exists and the result of
9963 * the insert is just empty lines
9964 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9965 * present or line_count > 1. It looks better if we do all the inserts
9966 * at once.
9967 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9968 * insert is just empty lines and T_CE is not present or line_count >
9969 * 1.
9970 * 4. Use T_AL (insert line) if it exists.
9971 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9972 * just empty lines.
9973 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9974 * just empty lines.
9975 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9976 * the 'da' flag is not set or we have clear line capability.
9977 * 8. redraw the characters from ScreenLines[].
9978 *
9979 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9980 * the scrollbar for the window. It does have insert line, use that if it
9981 * exists.
9982 */
9983 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9985 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009986 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 type = USE_T_CD;
9988 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9989 type = USE_T_CAL;
9990 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9991 type = USE_T_CDL;
9992 else if (*T_AL != NUL)
9993 type = USE_T_AL;
9994 else if (can_ce && result_empty)
9995 type = USE_T_CE;
9996 else if (*T_DL != NUL && result_empty)
9997 type = USE_T_DL;
9998 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9999 type = USE_T_SR;
10000 else
10001 return FAIL;
10002
10003 /*
10004 * For clearing the lines screen_del_lines() is used. This will also take
10005 * care of t_db if necessary.
10006 */
10007 if (type == USE_T_CD || type == USE_T_CDL ||
10008 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010009 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010
10011 /*
10012 * If text is retained below the screen, first clear or delete as many
10013 * lines at the bottom of the window as are about to be inserted so that
10014 * the deleted lines won't later surface during a screen_del_lines.
10015 */
10016 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010017 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
10019#ifdef FEAT_CLIPBOARD
10020 /* Remove a modeless selection when inserting lines halfway the screen
10021 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010022 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010023 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 else
10025 clip_scroll_selection(-line_count);
10026#endif
10027
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028#ifdef FEAT_GUI
10029 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10030 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010031 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032#endif
10033
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010034 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10035 cursor_col = wp->w_wincol;
10036
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 if (*T_CCS != NUL) /* cursor relative to region */
10038 cursor_row = row;
10039 else
10040 cursor_row = row + off;
10041
10042 /*
10043 * Shift LineOffset[] line_count down to reflect the inserted lines.
10044 * Clear the inserted lines in ScreenLines[].
10045 */
10046 row += off;
10047 end += off;
10048 for (i = 0; i < line_count; ++i)
10049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 if (wp != NULL && wp->w_width != Columns)
10051 {
10052 /* need to copy part of a line */
10053 j = end - 1 - i;
10054 while ((j -= line_count) >= row)
10055 linecopy(j + line_count, j, wp);
10056 j += line_count;
10057 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010058 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10059 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 else
10061 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10062 LineWraps[j] = FALSE;
10063 }
10064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 {
10066 j = end - 1 - i;
10067 temp = LineOffset[j];
10068 while ((j -= line_count) >= row)
10069 {
10070 LineOffset[j + line_count] = LineOffset[j];
10071 LineWraps[j + line_count] = LineWraps[j];
10072 }
10073 LineOffset[j + line_count] = temp;
10074 LineWraps[j + line_count] = FALSE;
10075 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010076 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 else
10078 lineinvalid(temp, (int)Columns);
10079 }
10080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
10082 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010083 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010084 if (clear_attr != 0)
10085 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 /* redraw the characters */
10088 if (type == USE_REDRAW)
10089 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010090 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 {
10092 term_append_lines(line_count);
10093 screen_start(); /* don't know where cursor is now */
10094 }
10095 else
10096 {
10097 for (i = 0; i < line_count; i++)
10098 {
10099 if (type == USE_T_AL)
10100 {
10101 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010102 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 out_str(T_AL);
10104 }
10105 else /* type == USE_T_SR */
10106 out_str(T_SR);
10107 screen_start(); /* don't know where cursor is now */
10108 }
10109 }
10110
10111 /*
10112 * With scroll-reverse and 'da' flag set we need to clear the lines that
10113 * have been scrolled down into the region.
10114 */
10115 if (type == USE_T_SR && *T_DA)
10116 {
10117 for (i = 0; i < line_count; ++i)
10118 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010119 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 out_str(T_CE);
10121 screen_start(); /* don't know where cursor is now */
10122 }
10123 }
10124
10125#ifdef FEAT_GUI
10126 gui_can_update_cursor();
10127 if (gui.in_use)
10128 out_flush(); /* always flush after a scroll */
10129#endif
10130 return OK;
10131}
10132
10133/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010134 * Delete lines on the screen and update ScreenLines[].
10135 * "end" is the line after the scrolled part. Normally it is Rows.
10136 * When scrolling region used "off" is the offset from the top for the region.
10137 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 *
10139 * Return OK for success, FAIL if the lines are not deleted.
10140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010142screen_del_lines(
10143 int off,
10144 int row,
10145 int line_count,
10146 int end,
10147 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010148 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010149 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
10151 int j;
10152 int i;
10153 unsigned temp;
10154 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010155 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 int cursor_end;
10157 int result_empty; /* result is empty until end of region */
10158 int can_delete; /* deleting line codes can be used */
10159 int type;
10160
10161 /*
10162 * FAIL if
10163 * - there is no valid screen
10164 * - the screen has to be redrawn completely
10165 * - the line count is less than one
10166 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010167 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010169 if (!screen_valid(TRUE) || line_count <= 0
10170 || (!force && line_count > p_ttyscroll)
10171#ifdef FEAT_CLIPBOARD
10172 || (clip_star.state != SELECT_CLEARED
10173 && redrawing_for_callback > 0)
10174#endif
10175 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 return FAIL;
10177
10178 /*
10179 * Check if the rest of the current region will become empty.
10180 */
10181 result_empty = row + line_count >= end;
10182
10183 /*
10184 * We can delete lines only when 'db' flag not set or when 'ce' option
10185 * available.
10186 */
10187 can_delete = (*T_DB == NUL || can_clear(T_CE));
10188
10189 /*
10190 * There are six ways to delete lines:
10191 * 0. When in a vertically split window and t_CV isn't set, redraw the
10192 * characters from ScreenLines[].
10193 * 1. Use T_CD if it exists and the result is empty.
10194 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10195 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10196 * none of the other ways work.
10197 * 4. Use T_CE (erase line) if the result is empty.
10198 * 5. Use T_DL (delete line) if it exists.
10199 * 6. redraw the characters from ScreenLines[].
10200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10202 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010203 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 type = USE_T_CD;
10205#if defined(__BEOS__) && defined(BEOS_DR8)
10206 /*
10207 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10208 * its internal termcap... this works okay for tests which test *T_DB !=
10209 * NUL. It has the disadvantage that the user cannot use any :set t_*
10210 * command to get T_DB (back) to empty_option, only :set term=... will do
10211 * the trick...
10212 * Anyway, this hack will hopefully go away with the next OS release.
10213 * (Olaf Seibert)
10214 */
10215 else if (row == 0 && T_DB == empty_option
10216 && (line_count == 1 || *T_CDL == NUL))
10217#else
10218 else if (row == 0 && (
10219#ifndef AMIGA
10220 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10221 * up, so use delete-line command */
10222 line_count == 1 ||
10223#endif
10224 *T_CDL == NUL))
10225#endif
10226 type = USE_NL;
10227 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10228 type = USE_T_CDL;
10229 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010230 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 type = USE_T_CE;
10232 else if (*T_DL != NUL && can_delete)
10233 type = USE_T_DL;
10234 else if (*T_CDL != NUL && can_delete)
10235 type = USE_T_CDL;
10236 else
10237 return FAIL;
10238
10239#ifdef FEAT_CLIPBOARD
10240 /* Remove a modeless selection when deleting lines halfway the screen or
10241 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010242 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010243 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 else
10245 clip_scroll_selection(line_count);
10246#endif
10247
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248#ifdef FEAT_GUI
10249 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10250 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010251 gui_dont_update_cursor(gui.cursor_row >= row + off
10252 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253#endif
10254
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010255 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10256 cursor_col = wp->w_wincol;
10257
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 if (*T_CCS != NUL) /* cursor relative to region */
10259 {
10260 cursor_row = row;
10261 cursor_end = end;
10262 }
10263 else
10264 {
10265 cursor_row = row + off;
10266 cursor_end = end + off;
10267 }
10268
10269 /*
10270 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10271 * Clear the inserted lines in ScreenLines[].
10272 */
10273 row += off;
10274 end += off;
10275 for (i = 0; i < line_count; ++i)
10276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 if (wp != NULL && wp->w_width != Columns)
10278 {
10279 /* need to copy part of a line */
10280 j = row + i;
10281 while ((j += line_count) <= end - 1)
10282 linecopy(j - line_count, j, wp);
10283 j -= line_count;
10284 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010285 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10286 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 else
10288 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10289 LineWraps[j] = FALSE;
10290 }
10291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 {
10293 /* whole width, moving the line pointers is faster */
10294 j = row + i;
10295 temp = LineOffset[j];
10296 while ((j += line_count) <= end - 1)
10297 {
10298 LineOffset[j - line_count] = LineOffset[j];
10299 LineWraps[j - line_count] = LineWraps[j];
10300 }
10301 LineOffset[j - line_count] = temp;
10302 LineWraps[j - line_count] = FALSE;
10303 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010304 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 else
10306 lineinvalid(temp, (int)Columns);
10307 }
10308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010310 if (screen_attr != clear_attr)
10311 screen_stop_highlight();
10312 if (clear_attr != 0)
10313 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 /* redraw the characters */
10316 if (type == USE_REDRAW)
10317 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010318 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010320 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 out_str(T_CD);
10322 screen_start(); /* don't know where cursor is now */
10323 }
10324 else if (type == USE_T_CDL)
10325 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010326 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 term_delete_lines(line_count);
10328 screen_start(); /* don't know where cursor is now */
10329 }
10330 /*
10331 * Deleting lines at top of the screen or scroll region: Just scroll
10332 * the whole screen (scroll region) up by outputting newlines on the
10333 * last line.
10334 */
10335 else if (type == USE_NL)
10336 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010337 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 for (i = line_count; --i >= 0; )
10339 out_char('\n'); /* cursor will remain on same line */
10340 }
10341 else
10342 {
10343 for (i = line_count; --i >= 0; )
10344 {
10345 if (type == USE_T_DL)
10346 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010347 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 out_str(T_DL); /* delete a line */
10349 }
10350 else /* type == USE_T_CE */
10351 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010352 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 out_str(T_CE); /* erase a line */
10354 }
10355 screen_start(); /* don't know where cursor is now */
10356 }
10357 }
10358
10359 /*
10360 * If the 'db' flag is set, we need to clear the lines that have been
10361 * scrolled up at the bottom of the region.
10362 */
10363 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10364 {
10365 for (i = line_count; i > 0; --i)
10366 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010367 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 out_str(T_CE); /* erase a line */
10369 screen_start(); /* don't know where cursor is now */
10370 }
10371 }
10372
10373#ifdef FEAT_GUI
10374 gui_can_update_cursor();
10375 if (gui.in_use)
10376 out_flush(); /* always flush after a scroll */
10377#endif
10378
10379 return OK;
10380}
10381
10382/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010383 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 *
10385 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10386 * If clear_cmdline is FALSE there may be a message there that needs to be
10387 * cleared only if a mode is shown.
10388 * Return the length of the message (0 if no message).
10389 */
10390 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010391showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392{
10393 int need_clear;
10394 int length = 0;
10395 int do_mode;
10396 int attr;
10397 int nwr_save;
10398#ifdef FEAT_INS_EXPAND
10399 int sub_attr;
10400#endif
10401
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010402 do_mode = ((p_smd && msg_silent == 0)
10403 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010404 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010405 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010406 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 {
10408 /*
10409 * Don't show mode right now, when not redrawing or inside a mapping.
10410 * Call char_avail() only when we are going to show something, because
10411 * it takes a bit of time.
10412 */
10413 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10414 {
10415 redraw_cmdline = TRUE; /* show mode later */
10416 return 0;
10417 }
10418
10419 nwr_save = need_wait_return;
10420
10421 /* wait a bit before overwriting an important message */
10422 check_for_delay(FALSE);
10423
10424 /* if the cmdline is more than one line high, erase top lines */
10425 need_clear = clear_cmdline;
10426 if (clear_cmdline && cmdline_row < Rows - 1)
10427 msg_clr_cmdline(); /* will reset clear_cmdline */
10428
10429 /* Position on the last line in the window, column 0 */
10430 msg_pos_mode();
10431 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010432 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 if (do_mode)
10434 {
10435 MSG_PUTS_ATTR("--", attr);
10436#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010437 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010438# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010439 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010440# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010441 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010442# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010443 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010444# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 MSG_PUTS_ATTR(" IM", attr);
10446# else
10447 MSG_PUTS_ATTR(" XIM", attr);
10448# endif
10449#endif
10450#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10451 if (gui.in_use)
10452 {
10453 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010454 {
10455 /* HANGUL */
10456 if (enc_utf8)
10457 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10458 else
10459 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 }
10462#endif
10463#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010464 /* CTRL-X in Insert mode */
10465 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 {
10467 /* These messages can get long, avoid a wrap in a narrow
10468 * window. Prefer showing edit_submode_extra. */
10469 length = (Rows - msg_row) * Columns - 3;
10470 if (edit_submode_extra != NULL)
10471 length -= vim_strsize(edit_submode_extra);
10472 if (length > 0)
10473 {
10474 if (edit_submode_pre != NULL)
10475 length -= vim_strsize(edit_submode_pre);
10476 if (length - vim_strsize(edit_submode) > 0)
10477 {
10478 if (edit_submode_pre != NULL)
10479 msg_puts_attr(edit_submode_pre, attr);
10480 msg_puts_attr(edit_submode, attr);
10481 }
10482 if (edit_submode_extra != NULL)
10483 {
10484 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10485 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010486 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 else
10488 sub_attr = attr;
10489 msg_puts_attr(edit_submode_extra, sub_attr);
10490 }
10491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 }
10493 else
10494#endif
10495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 if (State & VREPLACE_FLAG)
10497 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010498 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10500 else if (State & INSERT)
10501 {
10502#ifdef FEAT_RIGHTLEFT
10503 if (p_ri)
10504 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10505#endif
10506 MSG_PUTS_ATTR(_(" INSERT"), attr);
10507 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010508 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 MSG_PUTS_ATTR(_(" (insert)"), attr);
10510 else if (restart_edit == 'R')
10511 MSG_PUTS_ATTR(_(" (replace)"), attr);
10512 else if (restart_edit == 'V')
10513 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10514#ifdef FEAT_RIGHTLEFT
10515 if (p_hkmap)
10516 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10517# ifdef FEAT_FKMAP
10518 if (p_fkmap)
10519 MSG_PUTS_ATTR(farsi_text_5, attr);
10520# endif
10521#endif
10522#ifdef FEAT_KEYMAP
10523 if (State & LANGMAP)
10524 {
10525# ifdef FEAT_ARABIC
10526 if (curwin->w_p_arab)
10527 MSG_PUTS_ATTR(_(" Arabic"), attr);
10528 else
10529# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010530 if (get_keymap_str(curwin, (char_u *)" (%s)",
10531 NameBuff, MAXPATHL))
10532 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 }
10534#endif
10535 if ((State & INSERT) && p_paste)
10536 MSG_PUTS_ATTR(_(" (paste)"), attr);
10537
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 if (VIsual_active)
10539 {
10540 char *p;
10541
10542 /* Don't concatenate separate words to avoid translation
10543 * problems. */
10544 switch ((VIsual_select ? 4 : 0)
10545 + (VIsual_mode == Ctrl_V) * 2
10546 + (VIsual_mode == 'V'))
10547 {
10548 case 0: p = N_(" VISUAL"); break;
10549 case 1: p = N_(" VISUAL LINE"); break;
10550 case 2: p = N_(" VISUAL BLOCK"); break;
10551 case 4: p = N_(" SELECT"); break;
10552 case 5: p = N_(" SELECT LINE"); break;
10553 default: p = N_(" SELECT BLOCK"); break;
10554 }
10555 MSG_PUTS_ATTR(_(p), attr);
10556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 MSG_PUTS_ATTR(" --", attr);
10558 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010559
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 need_clear = TRUE;
10561 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010562 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563#ifdef FEAT_INS_EXPAND
10564 && edit_submode == NULL /* otherwise it gets too long */
10565#endif
10566 )
10567 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010568 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 need_clear = TRUE;
10570 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010571
10572 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 if (need_clear || clear_cmdline)
10574 msg_clr_eos();
10575 msg_didout = FALSE; /* overwrite this message */
10576 length = msg_col;
10577 msg_col = 0;
10578 need_wait_return = nwr_save; /* never ask for hit-return for this */
10579 }
10580 else if (clear_cmdline && msg_silent == 0)
10581 /* Clear the whole command line. Will reset "clear_cmdline". */
10582 msg_clr_cmdline();
10583
10584#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 /* In Visual mode the size of the selected area must be redrawn. */
10586 if (VIsual_active)
10587 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588
10589 /* If the last window has no status line, the ruler is after the mode
10590 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010591 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010592 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593#endif
10594 redraw_cmdline = FALSE;
10595 clear_cmdline = FALSE;
10596
10597 return length;
10598}
10599
10600/*
10601 * Position for a mode message.
10602 */
10603 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010604msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605{
10606 msg_col = 0;
10607 msg_row = Rows - 1;
10608}
10609
10610/*
10611 * Delete mode message. Used when ESC is typed which is expected to end
10612 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010613 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 */
10615 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010616unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617{
10618 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010619 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 */
10621 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10622 redraw_cmdline = TRUE; /* delete mode later */
10623 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010624 clearmode();
10625}
10626
10627/*
10628 * Clear the mode message.
10629 */
10630 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010631clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010632{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010633 int save_msg_row = msg_row;
10634 int save_msg_col = msg_col;
10635
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010636 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010637 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010638 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010639 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010640
10641 msg_col = save_msg_col;
10642 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643}
10644
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010645 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010646recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010647{
10648 MSG_PUTS_ATTR(_("recording"), attr);
10649 if (!shortmess(SHM_RECORDING))
10650 {
10651 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010652 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010653 MSG_PUTS_ATTR(s, attr);
10654 }
10655}
10656
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010657/*
10658 * Draw the tab pages line at the top of the Vim window.
10659 */
10660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010661draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010662{
10663 int tabcount = 0;
10664 tabpage_T *tp;
10665 int tabwidth;
10666 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010667 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010668 int attr;
10669 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010670 win_T *cwp;
10671 int wincount;
10672 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010673 int c;
10674 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010675 int attr_sel = HL_ATTR(HLF_TPS);
10676 int attr_nosel = HL_ATTR(HLF_TP);
10677 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010678 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010679 int room;
10680 int use_sep_chars = (t_colors < 8
10681#ifdef FEAT_GUI
10682 && !gui.in_use
10683#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010684#ifdef FEAT_TERMGUICOLORS
10685 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010686#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010687 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010688
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010689 if (ScreenLines == NULL)
10690 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010691 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010692
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010693#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010694 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010695 if (gui_use_tabline())
10696 {
10697 gui_update_tabline();
10698 return;
10699 }
10700#endif
10701
10702 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010703 return;
10704
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010705#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010706
10707 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10708 for (scol = 0; scol < Columns; ++scol)
10709 TabPageIdxs[scol] = 0;
10710
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010711 /* Use the 'tabline' option if it's set. */
10712 if (*p_tal != NUL)
10713 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010714 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010715
10716 /* Check for an error. If there is one we would loop in redrawing the
10717 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010718 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010719 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010720 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010721 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010722 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010723 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010724 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010725 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010726#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010727 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010728 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010729 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010730
Bram Moolenaar238a5642006-02-21 22:12:05 +000010731 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10732 if (tabwidth < 6)
10733 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010734
Bram Moolenaar238a5642006-02-21 22:12:05 +000010735 attr = attr_nosel;
10736 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010737 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010738 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10739 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010740 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010741 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010742
Bram Moolenaar238a5642006-02-21 22:12:05 +000010743 if (tp->tp_topframe == topframe)
10744 attr = attr_sel;
10745 if (use_sep_chars && col > 0)
10746 screen_putchar('|', 0, col++, attr);
10747
10748 if (tp->tp_topframe != topframe)
10749 attr = attr_nosel;
10750
10751 screen_putchar(' ', 0, col++, attr);
10752
10753 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010754 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010755 cwp = curwin;
10756 wp = firstwin;
10757 }
10758 else
10759 {
10760 cwp = tp->tp_curwin;
10761 wp = tp->tp_firstwin;
10762 }
10763
10764 modified = FALSE;
10765 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10766 if (bufIsChanged(wp->w_buffer))
10767 modified = TRUE;
10768 if (modified || wincount > 1)
10769 {
10770 if (wincount > 1)
10771 {
10772 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010773 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010774 if (col + len >= Columns - 3)
10775 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010776 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010777#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010778 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010779#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010780 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010781#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010782 );
10783 col += len;
10784 }
10785 if (modified)
10786 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10787 screen_putchar(' ', 0, col++, attr);
10788 }
10789
10790 room = scol - col + tabwidth - 1;
10791 if (room > 0)
10792 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010793 /* Get buffer name in NameBuff[] */
10794 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010795 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010796 len = vim_strsize(NameBuff);
10797 p = NameBuff;
10798#ifdef FEAT_MBYTE
10799 if (has_mbyte)
10800 while (len > room)
10801 {
10802 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010803 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010804 }
10805 else
10806#endif
10807 if (len > room)
10808 {
10809 p += len - room;
10810 len = room;
10811 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010812 if (len > Columns - col - 1)
10813 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010814
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010815 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010816 col += len;
10817 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010818 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010819
10820 /* Store the tab page number in TabPageIdxs[], so that
10821 * jump_to_mouse() knows where each one is. */
10822 ++tabcount;
10823 while (scol < col)
10824 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010825 }
10826
Bram Moolenaar238a5642006-02-21 22:12:05 +000010827 if (use_sep_chars)
10828 c = '_';
10829 else
10830 c = ' ';
10831 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010832
10833 /* Put an "X" for closing the current tab if there are several. */
10834 if (first_tabpage->tp_next != NULL)
10835 {
10836 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10837 TabPageIdxs[Columns - 1] = -999;
10838 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010839 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010840
10841 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10842 * set. */
10843 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010844}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010845
10846/*
10847 * Get buffer name for "buf" into NameBuff[].
10848 * Takes care of special buffer names and translates special characters.
10849 */
10850 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010851get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010852{
10853 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010854 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010855 else
10856 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10857 trans_characters(NameBuff, MAXPATHL);
10858}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010859
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860/*
10861 * Get the character to use in a status line. Get its attributes in "*attr".
10862 */
10863 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010864fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865{
10866 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010867
10868#ifdef FEAT_TERMINAL
10869 if (bt_terminal(wp->w_buffer))
10870 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010871 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010872 {
10873 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010874 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010875 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010876 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010877 {
10878 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010879 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010880 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010881 }
10882 else
10883#endif
10884 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010886 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 fill = fill_stl;
10888 }
10889 else
10890 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010891 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 fill = fill_stlnc;
10893 }
10894 /* Use fill when there is highlighting, and highlighting of current
10895 * window differs, or the fillchars differ, or this is not the
10896 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010897 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010898 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 || (fill_stl != fill_stlnc)))
10900 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010901 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 return '^';
10903 return '=';
10904}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906/*
10907 * Get the character to use in a separator between vertically split windows.
10908 * Get its attributes in "*attr".
10909 */
10910 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010911fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010913 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 if (*attr == 0 && fill_vert == ' ')
10915 return '|';
10916 else
10917 return fill_vert;
10918}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919
10920/*
10921 * Return TRUE if redrawing should currently be done.
10922 */
10923 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010924redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010926#ifdef FEAT_EVAL
10927 if (disable_redraw_for_testing)
10928 return 0;
10929 else
10930#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010931 return ((!RedrawingDisabled
10932#ifdef FEAT_EVAL
10933 || ignore_redraw_flag_for_testing
10934#endif
10935 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936}
10937
10938/*
10939 * Return TRUE if printing messages should currently be done.
10940 */
10941 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010942messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943{
10944 return (!(p_lz && char_avail() && !KeyTyped));
10945}
10946
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010947#ifdef FEAT_MENU
10948/*
10949 * Draw the window toolbar.
10950 */
10951 static void
10952redraw_win_toolbar(win_T *wp)
10953{
10954 vimmenu_T *menu;
10955 int item_idx = 0;
10956 int item_count = 0;
10957 int col = 0;
10958 int next_col;
10959 int off = (int)(current_ScreenLine - ScreenLines);
10960 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10961 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10962
10963 vim_free(wp->w_winbar_items);
10964 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10965 ++item_count;
10966 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10967 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10968
10969 /* TODO: use fewer spaces if there is not enough room */
10970 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010971 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010972 {
10973 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010974 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010975 break;
10976 if (col > 1)
10977 {
10978 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010979 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010980 break;
10981 }
10982
10983 wp->w_winbar_items[item_idx].wb_startcol = col;
10984 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010985 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010986 break;
10987
10988 next_col = text_to_screenline(wp, menu->name, col);
10989 while (col < next_col)
10990 {
10991 ScreenAttrs[off + col] = button_attr;
10992 ++col;
10993 }
10994 wp->w_winbar_items[item_idx].wb_endcol = col;
10995 wp->w_winbar_items[item_idx].wb_menu = menu;
10996 ++item_idx;
10997
Bram Moolenaar02631462017-09-22 15:20:32 +020010998 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010999 break;
11000 space_to_screenline(off + col, button_attr);
11001 ++col;
11002 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011003 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011004 {
11005 space_to_screenline(off + col, fill_attr);
11006 ++col;
11007 }
11008 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11009
Bram Moolenaar02631462017-09-22 15:20:32 +020011010 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11011 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011012}
11013#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011014
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015/*
11016 * Show current status info in ruler and various other places
11017 * If always is FALSE, only show ruler if position has changed.
11018 */
11019 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011020showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021{
11022 if (!always && !redrawing())
11023 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011024#ifdef FEAT_INS_EXPAND
11025 if (pum_visible())
11026 {
11027 /* Don't redraw right now, do it later. */
11028 curwin->w_redr_status = TRUE;
11029 return;
11030 }
11031#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011032#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011033 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011034 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011035 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 else
11038#endif
11039#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011040 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041#endif
11042
11043#ifdef FEAT_TITLE
11044 if (need_maketitle
11045# ifdef FEAT_STL_OPT
11046 || (p_icon && (stl_syntax & STL_IN_ICON))
11047 || (p_title && (stl_syntax & STL_IN_TITLE))
11048# endif
11049 )
11050 maketitle();
11051#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011052 /* Redraw the tab pages line if needed. */
11053 if (redraw_tabline)
11054 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055}
11056
11057#ifdef FEAT_CMDL_INFO
11058 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011059win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011061#define RULER_BUF_LEN 70
11062 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 int row;
11064 int fillchar;
11065 int attr;
11066 int empty_line = FALSE;
11067 colnr_T virtcol;
11068 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011069 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 int this_ru_col;
11072 int off = 0;
11073 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074
11075 /* If 'ruler' off or redrawing disabled, don't do anything */
11076 if (!p_ru)
11077 return;
11078
11079 /*
11080 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11081 * after deleting lines, before cursor.lnum is corrected.
11082 */
11083 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11084 return;
11085
11086#ifdef FEAT_INS_EXPAND
11087 /* Don't draw the ruler while doing insert-completion, it might overwrite
11088 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 if (edit_submode != NULL)
11091 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011092 // Don't draw the ruler when the popup menu is visible, it may overlap.
11093 // Except when the popup menu will be redrawn anyway.
11094 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011095 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096#endif
11097
11098#ifdef FEAT_STL_OPT
11099 if (*p_ruf)
11100 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011101 int save_called_emsg = called_emsg;
11102
11103 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011105 if (called_emsg)
11106 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011107 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011108 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 return;
11110 }
11111#endif
11112
11113 /*
11114 * Check if not in Insert mode and the line is empty (will show "0-1").
11115 */
11116 if (!(State & INSERT)
11117 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11118 empty_line = TRUE;
11119
11120 /*
11121 * Only draw the ruler when something changed.
11122 */
11123 validate_virtcol_win(wp);
11124 if ( redraw_cmdline
11125 || always
11126 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11127 || wp->w_cursor.col != wp->w_ru_cursor.col
11128 || wp->w_virtcol != wp->w_ru_virtcol
11129#ifdef FEAT_VIRTUALEDIT
11130 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11131#endif
11132 || wp->w_topline != wp->w_ru_topline
11133 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11134#ifdef FEAT_DIFF
11135 || wp->w_topfill != wp->w_ru_topfill
11136#endif
11137 || empty_line != wp->w_ru_empty)
11138 {
11139 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 if (wp->w_status_height)
11141 {
11142 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011143 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011144 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011145 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 }
11147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 {
11149 row = Rows - 1;
11150 fillchar = ' ';
11151 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 width = Columns;
11153 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 }
11155
11156 /* In list mode virtcol needs to be recomputed */
11157 virtcol = wp->w_virtcol;
11158 if (wp->w_p_list && lcs_tab1 == NUL)
11159 {
11160 wp->w_p_list = FALSE;
11161 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11162 wp->w_p_list = TRUE;
11163 }
11164
11165 /*
11166 * Some sprintfs return the length, some return a pointer.
11167 * To avoid portability problems we use strlen() here.
11168 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011169 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11171 ? 0L
11172 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011173 len = STRLEN(buffer);
11174 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11176 (int)virtcol + 1);
11177
11178 /*
11179 * Add a "50%" if there is room for it.
11180 * On the last line, don't print in the last column (scrolls the
11181 * screen up on some terminals).
11182 */
11183 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011184 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 this_ru_col = ru_col - (Columns - width);
11189 if (this_ru_col < 0)
11190 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 /* Never use more than half the window/screen width, leave the other
11192 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011193 if (this_ru_col < (width + 1) / 2)
11194 this_ru_col = (width + 1) / 2;
11195 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011197 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011198 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 {
11200#ifdef FEAT_MBYTE
11201 if (has_mbyte)
11202 i += (*mb_char2bytes)(fillchar, buffer + i);
11203 else
11204#endif
11205 buffer[i++] = fillchar;
11206 ++o;
11207 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011208 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 }
11210 /* Truncate at window boundary. */
11211#ifdef FEAT_MBYTE
11212 if (has_mbyte)
11213 {
11214 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011215 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 {
11217 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011218 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 {
11220 buffer[i] = NUL;
11221 break;
11222 }
11223 }
11224 }
11225 else
11226#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011227 if (this_ru_col + (int)STRLEN(buffer) > width)
11228 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229
Bram Moolenaar4033c552017-09-16 20:54:51 +020011230 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 i = redraw_cmdline;
11232 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011233 this_ru_col + off + (int)STRLEN(buffer),
11234 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 fillchar, fillchar, attr);
11236 /* don't redraw the cmdline because of showing the ruler */
11237 redraw_cmdline = i;
11238 wp->w_ru_cursor = wp->w_cursor;
11239 wp->w_ru_virtcol = wp->w_virtcol;
11240 wp->w_ru_empty = empty_line;
11241 wp->w_ru_topline = wp->w_topline;
11242 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11243#ifdef FEAT_DIFF
11244 wp->w_ru_topfill = wp->w_topfill;
11245#endif
11246 }
11247}
11248#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011249
11250#if defined(FEAT_LINEBREAK) || defined(PROTO)
11251/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011252 * Return the width of the 'number' and 'relativenumber' column.
11253 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011254 * Otherwise it depends on 'numberwidth' and the line count.
11255 */
11256 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011257number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011258{
11259 int n;
11260 linenr_T lnum;
11261
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011262 if (wp->w_p_rnu && !wp->w_p_nu)
11263 /* cursor line shows "0" */
11264 lnum = wp->w_height;
11265 else
11266 /* cursor line shows absolute line number */
11267 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011268
Bram Moolenaar6b314672015-03-20 15:42:10 +010011269 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011270 return wp->w_nrwidth_width;
11271 wp->w_nrwidth_line_count = lnum;
11272
11273 n = 0;
11274 do
11275 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011276 lnum /= 10;
11277 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011278 } while (lnum > 0);
11279
11280 /* 'numberwidth' gives the minimal width plus one */
11281 if (n < wp->w_p_nuw - 1)
11282 n = wp->w_p_nuw - 1;
11283
11284 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011285 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011286 return n;
11287}
11288#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011289
11290/*
11291 * Return the current cursor column. This is the actual position on the
11292 * screen. First column is 0.
11293 */
11294 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011295screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011296{
11297 return screen_cur_col;
11298}
11299
11300/*
11301 * Return the current cursor row. This is the actual position on the screen.
11302 * First row is 0.
11303 */
11304 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011305screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011306{
11307 return screen_cur_row;
11308}