blob: 1cf90ee0502b9618310cc443eb2ab97760ebd241 [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
3131#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003132 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003133# define SPWORDLEN 150
3134 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003135 int nextlinecol = 0; /* column where nextline[] starts */
3136 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003137 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003138 int spell_attr = 0; /* attributes desired by spelling */
3139 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003140 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3141 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003142 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003143 static int cap_col = -1; /* column to check for Cap word */
3144 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003145 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#endif
Bram Moolenaarc7875392018-09-13 14:57:41 +02003147 int extra_check = 0; // has syntax or linebreak
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#ifdef FEAT_MBYTE
3149 int multi_attr = 0; /* attributes desired by multibyte */
3150 int mb_l = 1; /* multi-byte byte length */
3151 int mb_c = 0; /* decoded multi-byte character */
3152 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003153 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154#endif
3155#ifdef FEAT_DIFF
3156 int filler_lines; /* nr of filler lines to be drawn */
3157 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003158 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int change_start = MAXCOL; /* first col of changed area */
3160 int change_end = -1; /* last col of changed area */
3161#endif
3162 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3163#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003164 int need_showbreak = FALSE; /* overlong line, skipping first x
3165 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003167#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003168 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003170 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#endif
3172#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003173 matchitem_T *cur; /* points to the match list */
3174 match_T *shl; /* points to search_hl or a match */
3175 int shl_flag; /* flag to indicate whether search_hl
3176 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003177 int pos_inprogress; /* marks that position match search is
3178 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003179 int prevcol_hl_flag; /* flag to indicate whether prevcol
3180 equals startcol of search_hl or one
3181 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#endif
3183#ifdef FEAT_ARABIC
3184 int prev_c = 0; /* previous Arabic character */
3185 int prev_c1 = 0; /* first composing char for prev_c */
3186#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003187#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003188 int did_line_attr = 0;
3189#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003190#ifdef FEAT_TERMINAL
3191 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003192 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 /* draw_state: items that are drawn in sequence: */
3196#define WL_START 0 /* nothing done yet */
3197#ifdef FEAT_CMDWIN
3198# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3199#else
3200# define WL_CMDLINE WL_START
3201#endif
3202#ifdef FEAT_FOLDING
3203# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3204#else
3205# define WL_FOLD WL_CMDLINE
3206#endif
3207#ifdef FEAT_SIGNS
3208# define WL_SIGN WL_FOLD + 1 /* column for signs */
3209#else
3210# define WL_SIGN WL_FOLD /* column for signs */
3211#endif
3212#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003213#ifdef FEAT_LINEBREAK
3214# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003216# define WL_BRI WL_NR
3217#endif
3218#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3219# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3220#else
3221# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222#endif
3223#define WL_LINE WL_SBR + 1 /* text in the line */
3224 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003225#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 int feedback_col = 0;
3227 int feedback_old_attr = -1;
3228#endif
3229
Bram Moolenaar860cae12010-06-05 23:22:07 +02003230#ifdef FEAT_CONCEAL
3231 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003232 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003233 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003234 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003235 int is_concealing = FALSE;
3236 int boguscols = 0; /* nonexistent columns added to force
3237 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003238 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003239 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003240 int match_conc = 0; /* cchar for match functions */
3241 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003242 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003243# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003244# define FIX_FOR_BOGUSCOLS \
3245 { \
3246 n_extra += vcol_off; \
3247 vcol -= vcol_off; \
3248 vcol_off = 0; \
3249 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003250 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003251 boguscols = 0; \
3252 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003253#else
3254# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
3257 if (startrow > endrow) /* past the end already! */
3258 return startrow;
3259
3260 row = startrow;
3261 screen_row = row + W_WINROW(wp);
3262
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003263 if (!number_only)
3264 {
3265 /*
3266 * To speed up the loop below, set extra_check when there is linebreak,
3267 * trailing white space and/or syntax processing to be done.
3268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003270 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#endif
3272#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003273 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003274# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003275 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003276# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003277 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003279 /* Prepare for syntax highlighting in this line. When there is an
3280 * error, stop syntax highlighting. */
3281 save_did_emsg = did_emsg;
3282 did_emsg = FALSE;
3283 syntax_start(wp, lnum);
3284 if (did_emsg)
3285 wp->w_s->b_syn_error = TRUE;
3286 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003287 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003288 did_emsg = save_did_emsg;
3289#ifdef SYN_TIME_LIMIT
3290 if (!wp->w_s->b_syn_slow)
3291#endif
3292 {
3293 has_syntax = TRUE;
3294 extra_check = TRUE;
3295 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003298
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003299 /* Check for columns to display for 'colorcolumn'. */
3300 color_cols = wp->w_p_cc_cols;
3301 if (color_cols != NULL)
3302 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003303#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003304
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003305#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003306 if (term_show_buffer(wp->w_buffer))
3307 {
3308 extra_check = TRUE;
3309 get_term_attr = TRUE;
3310 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3311 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003312#endif
3313
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003314#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003315 if (wp->w_p_spell
3316 && *wp->w_s->b_p_spl != NUL
3317 && wp->w_s->b_langp.ga_len > 0
3318 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003319 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003320 /* Prepare for spell checking. */
3321 has_spell = TRUE;
3322 extra_check = TRUE;
3323
Bram Moolenaar7701f302018-10-02 21:20:32 +02003324 /* Get the start of the next line, so that words that wrap to the
3325 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003326 * Trick: skip a few chars for C/shell/Vim comments */
3327 nextline[SPWORDLEN] = NUL;
3328 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3329 {
3330 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3331 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3332 }
3333
Bram Moolenaar7701f302018-10-02 21:20:32 +02003334 /* When a word wrapped from the previous line the start of the
3335 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003336 if (lnum == checked_lnum)
3337 cur_checked_col = checked_col;
3338 checked_lnum = 0;
3339
3340 /* When there was a sentence end in the previous line may require a
3341 * word starting with capital in this line. In line 1 always check
3342 * the first word. */
3343 if (lnum != capcol_lnum)
3344 cap_col = -1;
3345 if (lnum == 1)
3346 cap_col = 0;
3347 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#endif
3350
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003351 /*
3352 * handle visual active in this window
3353 */
3354 fromcol = -10;
3355 tocol = MAXCOL;
3356 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003358 /* Visual is after curwin->w_cursor */
3359 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 top = &curwin->w_cursor;
3362 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003364 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003366 top = &VIsual;
3367 bot = &curwin->w_cursor;
3368 }
3369 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3370 if (VIsual_mode == Ctrl_V) /* block mode */
3371 {
3372 if (lnum_in_visual_area)
3373 {
3374 fromcol = wp->w_old_cursor_fcol;
3375 tocol = wp->w_old_cursor_lcol;
3376 }
3377 }
3378 else /* non-block mode */
3379 {
3380 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003382 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003384 if (VIsual_mode == 'V') /* linewise */
3385 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 else
3387 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003388 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3389 if (gchar_pos(top) == NUL)
3390 tocol = fromcol + 1;
3391 }
3392 }
3393 if (VIsual_mode != 'V' && lnum == bot->lnum)
3394 {
3395 if (*p_sel == 'e' && bot->col == 0
3396#ifdef FEAT_VIRTUALEDIT
3397 && bot->coladd == 0
3398#endif
3399 )
3400 {
3401 fromcol = -10;
3402 tocol = MAXCOL;
3403 }
3404 else if (bot->col == MAXCOL)
3405 tocol = MAXCOL;
3406 else
3407 {
3408 pos = *bot;
3409 if (*p_sel == 'e')
3410 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3411 else
3412 {
3413 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3414 ++tocol;
3415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417 }
3418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003420 /* Check if the character under the cursor should not be inverted */
3421 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003422#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003424#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003425 )
3426 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003428 /* if inverting in this line set area_highlighting */
3429 if (fromcol >= 0)
3430 {
3431 area_highlighting = TRUE;
3432 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003434 if ((clip_star.available && !clip_star.owned
3435 && clip_isautosel_star())
3436 || (clip_plus.available && !clip_plus.owned
3437 && clip_isautosel_plus()))
3438 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003443 /*
3444 * handle 'incsearch' and ":s///c" highlighting
3445 */
3446 else if (highlight_match
3447 && wp == curwin
3448 && lnum >= curwin->w_cursor.lnum
3449 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003451 if (lnum == curwin->w_cursor.lnum)
3452 getvcol(curwin, &(curwin->w_cursor),
3453 (colnr_T *)&fromcol, NULL, NULL);
3454 else
3455 fromcol = 0;
3456 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3457 {
3458 pos.lnum = lnum;
3459 pos.col = search_match_endcol;
3460 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3461 }
3462 else
3463 tocol = MAXCOL;
3464 /* do at least one character; happens when past end of line */
3465 if (fromcol == tocol)
3466 tocol = fromcol + 1;
3467 area_highlighting = TRUE;
3468 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471
3472#ifdef FEAT_DIFF
3473 filler_lines = diff_check(wp, lnum);
3474 if (filler_lines < 0)
3475 {
3476 if (filler_lines == -1)
3477 {
3478 if (diff_find_change(wp, lnum, &change_start, &change_end))
3479 diff_hlf = HLF_ADD; /* added line */
3480 else if (change_start == 0)
3481 diff_hlf = HLF_TXD; /* changed text */
3482 else
3483 diff_hlf = HLF_CHD; /* changed line */
3484 }
3485 else
3486 diff_hlf = HLF_ADD; /* added line */
3487 filler_lines = 0;
3488 area_highlighting = TRUE;
3489 }
3490 if (lnum == wp->w_topline)
3491 filler_lines = wp->w_topfill;
3492 filler_todo = filler_lines;
3493#endif
3494
3495#ifdef LINE_ATTR
3496# ifdef FEAT_SIGNS
3497 /* If this line has a sign with line highlighting set line_attr. */
3498 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3499 if (v != 0)
3500 line_attr = sign_get_attr((int)v, TRUE);
3501# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003502# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003504 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003505 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506# endif
3507 if (line_attr != 0)
3508 area_highlighting = TRUE;
3509#endif
3510
3511 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3512 ptr = line;
3513
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003514#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003515 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003516 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003517 /* For checking first word with a capital skip white space. */
3518 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003519 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003520
Bram Moolenaar30abd282005-06-22 22:35:10 +00003521 /* To be able to spell-check over line boundaries copy the end of the
3522 * current line into nextline[]. Above the start of the next line was
3523 * copied to nextline[SPWORDLEN]. */
3524 if (nextline[SPWORDLEN] == NUL)
3525 {
3526 /* No next line or it is empty. */
3527 nextlinecol = MAXCOL;
3528 nextline_idx = 0;
3529 }
3530 else
3531 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003532 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003533 if (v < SPWORDLEN)
3534 {
3535 /* Short line, use it completely and append the start of the
3536 * next line. */
3537 nextlinecol = 0;
3538 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003539 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003540 nextline_idx = v + 1;
3541 }
3542 else
3543 {
3544 /* Long line, use only the last SPWORDLEN bytes. */
3545 nextlinecol = v - SPWORDLEN;
3546 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3547 nextline_idx = SPWORDLEN + 1;
3548 }
3549 }
3550 }
3551#endif
3552
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003553 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003555 if (lcs_space || lcs_trail)
3556 extra_check = TRUE;
3557 /* find start of trailing whitespace */
3558 if (lcs_trail)
3559 {
3560 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003561 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003562 --trailcol;
3563 trailcol += (colnr_T) (ptr - line);
3564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566
3567 /*
3568 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3569 * first character to be displayed.
3570 */
3571 if (wp->w_p_wrap)
3572 v = wp->w_skipcol;
3573 else
3574 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003575 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
3577#ifdef FEAT_MBYTE
3578 char_u *prev_ptr = ptr;
3579#endif
3580 while (vcol < v && *ptr != NUL)
3581 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003582 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 vcol += c;
3584#ifdef FEAT_MBYTE
3585 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003587 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003590 /* When:
3591 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003592 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003593 * - 'virtualedit' is set, or
3594 * - the visual mode is active,
3595 * the end of the line may be before the start of the displayed part.
3596 */
3597 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003598#ifdef FEAT_SYN_HL
3599 wp->w_p_cuc || draw_color_col ||
3600#endif
3601#ifdef FEAT_VIRTUALEDIT
3602 virtual_active() ||
3603#endif
3604 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 /* Handle a character that's not completely on the screen: Put ptr at
3610 * that character but skip the first few screen characters. */
3611 if (vcol > v)
3612 {
3613 vcol -= c;
3614#ifdef FEAT_MBYTE
3615 ptr = prev_ptr;
3616#else
3617 --ptr;
3618#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003619 /* If the character fits on the screen, don't need to skip it.
3620 * Except for a TAB. */
3621 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003622#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003623 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003624#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003625 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003626 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
3628
3629 /*
3630 * Adjust for when the inverted text is before the screen,
3631 * and when the start of the inverted text is before the screen.
3632 */
3633 if (tocol <= vcol)
3634 fromcol = 0;
3635 else if (fromcol >= 0 && fromcol < vcol)
3636 fromcol = vcol;
3637
3638#ifdef FEAT_LINEBREAK
3639 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3640 if (wp->w_p_wrap)
3641 need_showbreak = TRUE;
3642#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003643#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003644 /* When spell checking a word we need to figure out the start of the
3645 * word and if it's badly spelled or not. */
3646 if (has_spell)
3647 {
3648 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003649 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003650 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003651
3652 pos = wp->w_cursor;
3653 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003654 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003655 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003656
3657 /* spell_move_to() may call ml_get() and make "line" invalid */
3658 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3659 ptr = line + linecol;
3660
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003661 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003662 {
3663 /* no bad word found at line start, don't check until end of a
3664 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003665 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003666 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003667 }
3668 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003669 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003670 /* bad word found, use attributes until end of word */
3671 word_end = wp->w_cursor.col + len + 1;
3672
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003673 /* Turn index into actual attributes. */
3674 if (spell_hlf != HLF_COUNT)
3675 spell_attr = highlight_attr[spell_hlf];
3676 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003677 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003678
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003679# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003680 /* Need to restart syntax highlighting for this line. */
3681 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003682 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003683# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003684 }
3685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687
3688 /*
3689 * Correct highlighting for cursor that can't be disabled.
3690 * Avoids having to check this for each character.
3691 */
3692 if (fromcol >= 0)
3693 {
3694 if (noinvcur)
3695 {
3696 if ((colnr_T)fromcol == wp->w_virtcol)
3697 {
3698 /* highlighting starts at cursor, let it start just after the
3699 * cursor */
3700 fromcol_prev = fromcol;
3701 fromcol = -1;
3702 }
3703 else if ((colnr_T)fromcol < wp->w_virtcol)
3704 /* restart highlighting after the cursor */
3705 fromcol_prev = wp->w_virtcol;
3706 }
3707 if (fromcol >= tocol)
3708 fromcol = -1;
3709 }
3710
3711#ifdef FEAT_SEARCH_EXTRA
3712 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003713 * Handle highlighting the last used search pattern and matches.
3714 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003716 cur = wp->w_match_head;
3717 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003718 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003720 if (shl_flag == FALSE)
3721 {
3722 shl = &search_hl;
3723 shl_flag = TRUE;
3724 }
3725 else
3726 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003727 shl->startcol = MAXCOL;
3728 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003730 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003731 v = (long)(ptr - line);
3732 if (cur != NULL)
3733 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003734 next_search_hl(wp, shl, lnum, (colnr_T)v,
3735 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003736
3737 /* Need to get the line again, a multi-line regexp may have made it
3738 * invalid. */
3739 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3740 ptr = line + v;
3741
3742 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003744 if (shl->lnum == lnum)
3745 shl->startcol = shl->rm.startpos[0].col;
3746 else
3747 shl->startcol = 0;
3748 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3749 - shl->rm.startpos[0].lnum)
3750 shl->endcol = shl->rm.endpos[0].col;
3751 else
3752 shl->endcol = MAXCOL;
3753 /* Highlight one character for an empty match. */
3754 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003757 if (has_mbyte && line[shl->endcol] != NUL)
3758 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3759 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003761 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003763 if ((long)shl->startcol < v) /* match at leftcol */
3764 {
3765 shl->attr_cur = shl->attr;
3766 search_attr = shl->attr;
3767 }
3768 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003770 if (shl != &search_hl && cur != NULL)
3771 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773#endif
3774
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003775#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003776 /* Cursor line highlighting for 'cursorline' in the current window. Not
3777 * when Visual mode is active, because it's not clear what is selected
3778 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003779 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003780 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003781 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003782 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003783 area_highlighting = TRUE;
3784 }
3785#endif
3786
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003787 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 col = 0;
3789#ifdef FEAT_RIGHTLEFT
3790 if (wp->w_p_rl)
3791 {
3792 /* Rightleft window: process the text in the normal direction, but put
3793 * it in current_ScreenLine[] from right to left. Start at the
3794 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003795 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 off += col;
3797 }
3798#endif
3799
3800 /*
3801 * Repeat for the whole displayed line.
3802 */
3803 for (;;)
3804 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003805#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003806 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 /* Skip this quickly when working on the text. */
3809 if (draw_state != WL_LINE)
3810 {
3811#ifdef FEAT_CMDWIN
3812 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3813 {
3814 draw_state = WL_CMDLINE;
3815 if (cmdwin_type != 0 && wp == curwin)
3816 {
3817 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003819 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003820 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 }
3823#endif
3824
3825#ifdef FEAT_FOLDING
3826 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3827 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003828 int fdc = compute_foldcolumn(wp, 0);
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003831 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003833 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003834 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003835 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003836 p_extra_free = alloc(12 + 1);
3837
3838 if (p_extra_free != NULL)
3839 {
3840 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3841 n_extra = fdc;
3842 p_extra_free[n_extra] = NUL;
3843 p_extra = p_extra_free;
3844 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003845 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 }
3849#endif
3850
3851#ifdef FEAT_SIGNS
3852 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3853 {
3854 draw_state = WL_SIGN;
3855 /* Show the sign column when there are any signs in this
3856 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003857 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003859 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003861 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862# endif
3863
3864 /* Draw two cells with the sign value or blank. */
3865 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003866 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 n_extra = 2;
3868
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003869 if (row == startrow
3870#ifdef FEAT_DIFF
3871 + filler_lines && filler_todo <= 0
3872#endif
3873 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
3875 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3876 SIGN_TEXT);
3877# ifdef FEAT_SIGN_ICONS
3878 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3879 SIGN_ICON);
3880 if (gui.in_use && icon_sign != 0)
3881 {
3882 /* Use the image in this position. */
3883 c_extra = SIGN_BYTE;
3884# ifdef FEAT_NETBEANS_INTG
3885 if (buf_signcount(wp->w_buffer, lnum) > 1)
3886 c_extra = MULTISIGN_BYTE;
3887# endif
3888 char_attr = icon_sign;
3889 }
3890 else
3891# endif
3892 if (text_sign != 0)
3893 {
3894 p_extra = sign_get_text(text_sign);
3895 if (p_extra != NULL)
3896 {
3897 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003898 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 char_attr = sign_get_attr(text_sign, FALSE);
3901 }
3902 }
3903 }
3904 }
3905#endif
3906
3907 if (draw_state == WL_NR - 1 && n_extra == 0)
3908 {
3909 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003910 /* Display the absolute or relative line number. After the
3911 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3912 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 && (row == startrow
3914#ifdef FEAT_DIFF
3915 + filler_lines
3916#endif
3917 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3918 {
3919 /* Draw the line number (empty space after wrapping). */
3920 if (row == startrow
3921#ifdef FEAT_DIFF
3922 + filler_lines
3923#endif
3924 )
3925 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003926 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003927 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003928
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003929 if (wp->w_p_nu && !wp->w_p_rnu)
3930 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003931 num = (long)lnum;
3932 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003933 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003934 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003935 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003936 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003937 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003938 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003939 num = lnum;
3940 fmt = "%-*ld ";
3941 }
3942 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003943
Bram Moolenaar700e7342013-01-30 12:31:36 +01003944 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003945 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (wp->w_skipcol > 0)
3947 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3948 *p_extra = '-';
3949#ifdef FEAT_RIGHTLEFT
3950 if (wp->w_p_rl) /* reverse line numbers */
3951 rl_mirror(extra);
3952#endif
3953 p_extra = extra;
3954 c_extra = NUL;
3955 }
3956 else
3957 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003958 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003959 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003960#ifdef FEAT_SYN_HL
3961 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003962 * the current line differently.
3963 * TODO: Can we use CursorLine instead of CursorLineNr
3964 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003965 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003966 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003967 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 }
3971
Bram Moolenaar597a4222014-06-25 14:39:50 +02003972#ifdef FEAT_LINEBREAK
3973 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3974 && n_extra == 0 && *p_sbr != NUL)
3975 /* draw indent after showbreak value */
3976 draw_state = WL_BRI;
3977 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3978 /* After the showbreak, draw the breakindent */
3979 draw_state = WL_BRI - 1;
3980
3981 /* draw 'breakindent': indent wrapped text accordingly */
3982 if (draw_state == WL_BRI - 1 && n_extra == 0)
3983 {
3984 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003985 /* if need_showbreak is set, breakindent also applies */
3986 if (wp->w_p_bri && n_extra == 0
3987 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003988# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003989 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003990# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003991 )
3992 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003993 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003994# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003995 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003996 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003997 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003998# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003999 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4000 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004001 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004002# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004003 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004004# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004005 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004006 c_extra = ' ';
4007 n_extra = get_breakindent_win(wp,
4008 ml_get_buf(wp->w_buffer, lnum, FALSE));
4009 /* Correct end of highlighted area for 'breakindent',
4010 * required when 'linebreak' is also set. */
4011 if (tocol == vcol)
4012 tocol += n_extra;
4013 }
4014 }
4015#endif
4016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4018 if (draw_state == WL_SBR - 1 && n_extra == 0)
4019 {
4020 draw_state = WL_SBR;
4021# ifdef FEAT_DIFF
4022 if (filler_todo > 0)
4023 {
4024 /* Draw "deleted" diff line(s). */
4025 if (char2cells(fill_diff) > 1)
4026 c_extra = '-';
4027 else
4028 c_extra = fill_diff;
4029# ifdef FEAT_RIGHTLEFT
4030 if (wp->w_p_rl)
4031 n_extra = col + 1;
4032 else
4033# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004034 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004035 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
4037# endif
4038# ifdef FEAT_LINEBREAK
4039 if (*p_sbr != NUL && need_showbreak)
4040 {
4041 /* Draw 'showbreak' at the start of each broken line. */
4042 p_extra = p_sbr;
4043 c_extra = NUL;
4044 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004045 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004047 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 /* Correct end of highlighted area for 'showbreak',
4049 * required when 'linebreak' is also set. */
4050 if (tocol == vcol)
4051 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004052#ifdef FEAT_SYN_HL
4053 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004054 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004055 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004056 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059# endif
4060 }
4061#endif
4062
4063 if (draw_state == WL_LINE - 1 && n_extra == 0)
4064 {
4065 draw_state = WL_LINE;
4066 if (saved_n_extra)
4067 {
4068 /* Continue item from end of wrapped line. */
4069 n_extra = saved_n_extra;
4070 c_extra = saved_c_extra;
4071 p_extra = saved_p_extra;
4072 char_attr = saved_char_attr;
4073 }
4074 else
4075 char_attr = 0;
4076 }
4077 }
4078
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004079 // When still displaying '$' of change command, stop at cursor.
4080 // When only displaying the (relative) line number and that's done,
4081 // stop here.
4082 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004083 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084#ifdef FEAT_DIFF
4085 && filler_todo <= 0
4086#endif
4087 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004088 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004090 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004091 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004092 /* Pretend we have finished updating the window. Except when
4093 * 'cursorcolumn' is set. */
4094#ifdef FEAT_SYN_HL
4095 if (wp->w_p_cuc)
4096 row = wp->w_cline_row + wp->w_cline_height;
4097 else
4098#endif
4099 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 break;
4101 }
4102
4103 if (draw_state == WL_LINE && area_highlighting)
4104 {
4105 /* handle Visual or match highlighting in this line */
4106 if (vcol == fromcol
4107#ifdef FEAT_MBYTE
4108 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4109 && (*mb_ptr2cells)(ptr) > 1)
4110#endif
4111 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004112 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 && vcol < tocol))
4114 area_attr = attr; /* start highlighting */
4115 else if (area_attr != 0
4116 && (vcol == tocol
4117 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120#ifdef FEAT_SEARCH_EXTRA
4121 if (!n_extra)
4122 {
4123 /*
4124 * Check for start/end of search pattern match.
4125 * After end, check for start/end of next match.
4126 * When another match, have to check for start again.
4127 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004128 * Do this for 'search_hl' and the match list (ordered by
4129 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004131 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004132 cur = wp->w_match_head;
4133 shl_flag = FALSE;
4134 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004136 if (shl_flag == FALSE
4137 && ((cur != NULL
4138 && cur->priority > SEARCH_HL_PRIORITY)
4139 || cur == NULL))
4140 {
4141 shl = &search_hl;
4142 shl_flag = TRUE;
4143 }
4144 else
4145 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004146 if (cur != NULL)
4147 cur->pos.cur = 0;
4148 pos_inprogress = TRUE;
4149 while (shl->rm.regprog != NULL
4150 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004152 if (shl->startcol != MAXCOL
4153 && v >= (long)shl->startcol
4154 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004156#ifdef FEAT_MBYTE
4157 int tmp_col = v + MB_PTR2LEN(ptr);
4158
4159 if (shl->endcol < tmp_col)
4160 shl->endcol = tmp_col;
4161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004163#ifdef FEAT_CONCEAL
4164 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4165 == cur->hlg_id)
4166 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004167 has_match_conc =
4168 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004169 match_conc = cur->conceal_char;
4170 }
4171 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004172 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004175 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
4177 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004178 next_search_hl(wp, shl, lnum, (colnr_T)v,
4179 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004180 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004181 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 /* Need to get the line again, a multi-line regexp
4184 * may have made it invalid. */
4185 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4186 ptr = line + v;
4187
4188 if (shl->lnum == lnum)
4189 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004190 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004192 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004194 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004196 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
4198 /* highlight empty match, try again after
4199 * it */
4200#ifdef FEAT_MBYTE
4201 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004202 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004203 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 else
4205#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004206 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208
4209 /* Loop to check if the match starts at the
4210 * current position */
4211 continue;
4212 }
4213 }
4214 break;
4215 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004216 if (shl != &search_hl && cur != NULL)
4217 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004219
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004220 /* Use attributes from match with highest priority among
4221 * 'search_hl' and the match list. */
4222 search_attr = search_hl.attr_cur;
4223 cur = wp->w_match_head;
4224 shl_flag = FALSE;
4225 while (cur != NULL || shl_flag == FALSE)
4226 {
4227 if (shl_flag == FALSE
4228 && ((cur != NULL
4229 && cur->priority > SEARCH_HL_PRIORITY)
4230 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004231 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004232 shl = &search_hl;
4233 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004234 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004235 else
4236 shl = &cur->hl;
4237 if (shl->attr_cur != 0)
4238 search_attr = shl->attr_cur;
4239 if (shl != &search_hl && cur != NULL)
4240 cur = cur->next;
4241 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004242 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004243 if (*ptr == NUL && (did_line_attr >= 1
4244 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004245 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247#endif
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004250 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004252 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4253 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004255 if (diff_hlf == HLF_TXD && ptr - line > change_end
4256 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004258 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004259 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004260 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004263
4264 /* Decide which of the highlight attributes to use. */
4265 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004266#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004267 if (area_attr != 0)
4268 char_attr = hl_combine_attr(line_attr, area_attr);
4269 else if (search_attr != 0)
4270 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004271 /* Use line_attr when not in the Visual or 'incsearch' area
4272 * (area_attr may be 0 when "noinvcur" is set). */
4273 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004274 || vcol < fromcol || vcol_prev < fromcol_prev
4275 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004276 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004277#else
4278 if (area_attr != 0)
4279 char_attr = area_attr;
4280 else if (search_attr != 0)
4281 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004282#endif
4283 else
4284 {
4285 attr_pri = FALSE;
4286#ifdef FEAT_SYN_HL
4287 if (has_syntax)
4288 char_attr = syntax_attr;
4289 else
4290#endif
4291 char_attr = 0;
4292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294
4295 /*
4296 * Get the next character to put on the screen.
4297 */
4298 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004299 * The "p_extra" points to the extra stuff that is inserted to
4300 * represent special characters (non-printable stuff) and other
4301 * things. When all characters are the same, c_extra is used.
4302 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4303 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4305 */
4306 if (n_extra > 0)
4307 {
4308 if (c_extra != NUL)
4309 {
4310 c = c_extra;
4311#ifdef FEAT_MBYTE
4312 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004313 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004316 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004317 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 else
4320 mb_utf8 = FALSE;
4321#endif
4322 }
4323 else
4324 {
4325 c = *p_extra;
4326#ifdef FEAT_MBYTE
4327 if (has_mbyte)
4328 {
4329 mb_c = c;
4330 if (enc_utf8)
4331 {
4332 /* If the UTF-8 character is more than one byte:
4333 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004334 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 mb_utf8 = FALSE;
4336 if (mb_l > n_extra)
4337 mb_l = 1;
4338 else if (mb_l > 1)
4339 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004340 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004342 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 }
4345 else
4346 {
4347 /* if this is a DBCS character, put it in "mb_c" */
4348 mb_l = MB_BYTE2LEN(c);
4349 if (mb_l >= n_extra)
4350 mb_l = 1;
4351 else if (mb_l > 1)
4352 mb_c = (c << 8) + p_extra[1];
4353 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004354 if (mb_l == 0) /* at the NUL at end-of-line */
4355 mb_l = 1;
4356
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 /* If a double-width char doesn't fit display a '>' in the
4358 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004359 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360# ifdef FEAT_RIGHTLEFT
4361 wp->w_p_rl ? (col <= 0) :
4362# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004363 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 && (*mb_char2cells)(mb_c) == 2)
4365 {
4366 c = '>';
4367 mb_c = c;
4368 mb_l = 1;
4369 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004370 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 /* put the pointer back to output the double-width
4372 * character at the start of the next line. */
4373 ++n_extra;
4374 --p_extra;
4375 }
4376 else
4377 {
4378 n_extra -= mb_l - 1;
4379 p_extra += mb_l - 1;
4380 }
4381 }
4382#endif
4383 ++p_extra;
4384 }
4385 --n_extra;
4386 }
4387 else
4388 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004389#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004390 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004391#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004392
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004393 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004394 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 /*
4396 * Get a character from the line itself.
4397 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004398 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004399#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004400 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402#ifdef FEAT_MBYTE
4403 if (has_mbyte)
4404 {
4405 mb_c = c;
4406 if (enc_utf8)
4407 {
4408 /* If the UTF-8 character is more than one byte: Decode it
4409 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004410 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 mb_utf8 = FALSE;
4412 if (mb_l > 1)
4413 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004414 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 /* Overlong encoded ASCII or ASCII with composing char
4416 * is displayed normally, except a NUL. */
4417 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004418 {
4419 c = mb_c;
4420# ifdef FEAT_LINEBREAK
4421 c0 = mb_c;
4422# endif
4423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004425
4426 /* At start of the line we can have a composing char.
4427 * Draw it as a space with a composing char. */
4428 if (utf_iscomposing(mb_c))
4429 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004430 int i;
4431
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004432 for (i = Screen_mco - 1; i > 0; --i)
4433 u8cc[i] = u8cc[i - 1];
4434 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004435 mb_c = ' ';
4436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438
4439 if ((mb_l == 1 && c >= 0x80)
4440 || (mb_l >= 1 && mb_c == 0)
4441 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004442# ifdef UNICODE16
4443 || mb_c >= 0x10000
4444# endif
4445 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
4447 /*
4448 * Illegal UTF-8 byte: display as <xx>.
4449 * Non-BMP character : display as ? or fullwidth ?.
4450 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004451# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
4455 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004456# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 if (wp->w_p_rl) /* reverse */
4458 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004459# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004461# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else if (utf_char2cells(mb_c) != 2)
4463 STRCPY(extra, "?");
4464 else
4465 /* 0xff1f in UTF-8: full-width '?' */
4466 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 p_extra = extra;
4470 c = *p_extra;
4471 mb_c = mb_ptr2char_adv(&p_extra);
4472 mb_utf8 = (c >= 0x80);
4473 n_extra = (int)STRLEN(p_extra);
4474 c_extra = NUL;
4475 if (area_attr == 0 && search_attr == 0)
4476 {
4477 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004478 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 saved_attr2 = char_attr; /* save current attr */
4480 }
4481 }
4482 else if (mb_l == 0) /* at the NUL at end-of-line */
4483 mb_l = 1;
4484#ifdef FEAT_ARABIC
4485 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4486 {
4487 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004488 int pc, pc1, nc;
4489 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 /* The idea of what is the previous and next
4492 * character depends on 'rightleft'. */
4493 if (wp->w_p_rl)
4494 {
4495 pc = prev_c;
4496 pc1 = prev_c1;
4497 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004498 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 else
4501 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004502 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004504 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506 prev_c = mb_c;
4507
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004508 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510 else
4511 prev_c = mb_c;
4512#endif
4513 }
4514 else /* enc_dbcs */
4515 {
4516 mb_l = MB_BYTE2LEN(c);
4517 if (mb_l == 0) /* at the NUL at end-of-line */
4518 mb_l = 1;
4519 else if (mb_l > 1)
4520 {
4521 /* We assume a second byte below 32 is illegal.
4522 * Hopefully this is OK for all double-byte encodings!
4523 */
4524 if (ptr[1] >= 32)
4525 mb_c = (c << 8) + ptr[1];
4526 else
4527 {
4528 if (ptr[1] == NUL)
4529 {
4530 /* head byte at end of line */
4531 mb_l = 1;
4532 transchar_nonprint(extra, c);
4533 }
4534 else
4535 {
4536 /* illegal tail byte */
4537 mb_l = 2;
4538 STRCPY(extra, "XX");
4539 }
4540 p_extra = extra;
4541 n_extra = (int)STRLEN(extra) - 1;
4542 c_extra = NUL;
4543 c = *p_extra++;
4544 if (area_attr == 0 && search_attr == 0)
4545 {
4546 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004547 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 saved_attr2 = char_attr; /* save current attr */
4549 }
4550 mb_c = c;
4551 }
4552 }
4553 }
4554 /* If a double-width char doesn't fit display a '>' in the
4555 * last column; the character is displayed at the start of the
4556 * next line. */
4557 if ((
4558# ifdef FEAT_RIGHTLEFT
4559 wp->w_p_rl ? (col <= 0) :
4560# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004561 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 && (*mb_char2cells)(mb_c) == 2)
4563 {
4564 c = '>';
4565 mb_c = c;
4566 mb_utf8 = FALSE;
4567 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004568 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 /* Put pointer back so that the character will be
4570 * displayed at the start of the next line. */
4571 --ptr;
4572 }
4573 else if (*ptr != NUL)
4574 ptr += mb_l - 1;
4575
4576 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004577 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004578 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004579 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004582 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 c = ' ';
4584 if (area_attr == 0 && search_attr == 0)
4585 {
4586 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004587 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 saved_attr2 = char_attr; /* save current attr */
4589 }
4590 mb_c = c;
4591 mb_utf8 = FALSE;
4592 mb_l = 1;
4593 }
4594
4595 }
4596#endif
4597 ++ptr;
4598
4599 if (extra_check)
4600 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004601#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004602 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004603#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004604
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004605#ifdef FEAT_TERMINAL
4606 if (get_term_attr)
4607 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004608 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004609
4610 if (!attr_pri)
4611 char_attr = syntax_attr;
4612 else
4613 char_attr = hl_combine_attr(syntax_attr, char_attr);
4614 }
4615#endif
4616
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004617#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 /* Get syntax attribute, unless still at the start of the line
4619 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004620 v = (long)(ptr - line);
4621 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
4623 /* Get the syntax attribute for the character. If there
4624 * is an error, disable syntax highlighting. */
4625 save_did_emsg = did_emsg;
4626 did_emsg = FALSE;
4627
Bram Moolenaar217ad922005-03-20 22:37:15 +00004628 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004629# ifdef FEAT_SPELL
4630 has_spell ? &can_spell :
4631# endif
4632 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
4634 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004635 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004636 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004637 has_syntax = FALSE;
4638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 else
4640 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004641#ifdef SYN_TIME_LIMIT
4642 if (wp->w_s->b_syn_slow)
4643 has_syntax = FALSE;
4644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
4646 /* Need to get the line again, a multi-line regexp may
4647 * have made it invalid. */
4648 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4649 ptr = line + v;
4650
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004651 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004653 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004654 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004655# ifdef FEAT_CONCEAL
4656 /* no concealing past the end of the line, it interferes
4657 * with line highlighting */
4658 if (c == NUL)
4659 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004660 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004661 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004662# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004664#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004665
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004666#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004667 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004668 * Only do this when there is no syntax highlighting, the
4669 * @Spell cluster is not used or the current syntax item
4670 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004671 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004672 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004673 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004674# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004675 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004676 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004677# endif
4678 if (c != 0 && (
4679# ifdef FEAT_SYN_HL
4680 !has_syntax ||
4681# endif
4682 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004683 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004684 char_u *prev_ptr, *p;
4685 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004686 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004687# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004688 if (has_mbyte)
4689 {
4690 prev_ptr = ptr - mb_l;
4691 v -= mb_l - 1;
4692 }
4693 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004694# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004695 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004696
4697 /* Use nextline[] if possible, it has the start of the
4698 * next line concatenated. */
4699 if ((prev_ptr - line) - nextlinecol >= 0)
4700 p = nextline + (prev_ptr - line) - nextlinecol;
4701 else
4702 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004703 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004704 len = spell_check(wp, p, &spell_hlf, &cap_col,
4705 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004706 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004707
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004708 /* In Insert mode only highlight a word that
4709 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004710 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004711 && (State & INSERT) != 0
4712 && wp->w_cursor.lnum == lnum
4713 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004714 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004715 && wp->w_cursor.col < (colnr_T)word_end)
4716 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004717 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004718 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004719 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004720
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004721 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004722 && (p - nextline) + len > nextline_idx)
4723 {
4724 /* Remember that the good word continues at the
4725 * start of the next line. */
4726 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004727 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004728 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004729
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004730 /* Turn index into actual attributes. */
4731 if (spell_hlf != HLF_COUNT)
4732 spell_attr = highlight_attr[spell_hlf];
4733
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004734 if (cap_col > 0)
4735 {
4736 if (p != prev_ptr
4737 && (p - nextline) + cap_col >= nextline_idx)
4738 {
4739 /* Remember that the word in the next line
4740 * must start with a capital. */
4741 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004742 cap_col = (int)((p - nextline) + cap_col
4743 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004744 }
4745 else
4746 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004747 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004748 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004749 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004750 }
4751 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004752 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004753 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004754 char_attr = hl_combine_attr(char_attr, spell_attr);
4755 else
4756 char_attr = hl_combine_attr(spell_attr, char_attr);
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#endif
4759#ifdef FEAT_LINEBREAK
4760 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004761 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004763 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004764 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004766# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004767 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004768# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004769 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004771 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004773 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004774
Bram Moolenaar597a4222014-06-25 14:39:50 +02004775 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004776 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004777 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004778 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004779# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004780 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004781 wp->w_buffer->b_p_vts_array) - 1;
4782# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004783 n_extra = (int)wp->w_buffer->b_p_ts
4784 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004785# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004786
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004787# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004788 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004789# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004791# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004792 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004793 {
4794#ifdef FEAT_CONCEAL
4795 if (c == TAB)
4796 /* See "Tab alignment" below. */
4797 FIX_FOR_BOGUSCOLS;
4798#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004799 if (!wp->w_p_list)
4800 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803#endif
4804
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004805 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4806 */
4807 if (wp->w_p_list
4808 && (((c == 160
4809#ifdef FEAT_MBYTE
4810 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4811#endif
4812 ) && lcs_nbsp)
4813 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4814 {
4815 c = (c == ' ') ? lcs_space : lcs_nbsp;
4816 if (area_attr == 0 && search_attr == 0)
4817 {
4818 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004819 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004820 saved_attr2 = char_attr; /* save current attr */
4821 }
4822#ifdef FEAT_MBYTE
4823 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004824 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004825 {
4826 mb_utf8 = TRUE;
4827 u8cc[0] = 0;
4828 c = 0xc0;
4829 }
4830 else
4831 mb_utf8 = FALSE;
4832#endif
4833 }
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4836 {
4837 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004838 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
4840 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004841 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 saved_attr2 = char_attr; /* save current attr */
4843 }
4844#ifdef FEAT_MBYTE
4845 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004846 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
4848 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004849 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004850 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852 else
4853 mb_utf8 = FALSE;
4854#endif
4855 }
4856 }
4857
4858 /*
4859 * Handling of non-printable characters.
4860 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004861 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 {
4863 /*
4864 * when getting a character from the file, we may have to
4865 * turn it into something else on the way to putting it
4866 * into "ScreenLines".
4867 */
4868 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4869 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004870 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004871 long vcol_adjusted = vcol; /* removed showbreak length */
4872#ifdef FEAT_LINEBREAK
4873 /* only adjust the tab_len, when at the first column
4874 * after the showbreak value was drawn */
4875 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4876 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004879#ifdef FEAT_VARTABS
4880 tab_len = tabstop_padding(vcol_adjusted,
4881 wp->w_buffer->b_p_ts,
4882 wp->w_buffer->b_p_vts_array) - 1;
4883#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004884 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004885 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4886#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004887
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004888#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004889 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004890#endif
4891 /* tab amount depends on current column */
4892 n_extra = tab_len;
4893#ifdef FEAT_LINEBREAK
4894 else
4895 {
4896 char_u *p;
4897 int len = n_extra;
4898 int i;
4899 int saved_nextra = n_extra;
4900
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004901#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004902 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004903 /* there are characters to conceal */
4904 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004905 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4906 */
4907 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4908 && n_extra > tab_len)
4909 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004910#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004911
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004912 /* if n_extra > 0, it gives the number of chars, to
4913 * use for a tab, else we need to calculate the width
4914 * for a tab */
4915#ifdef FEAT_MBYTE
4916 len = (tab_len * mb_char2len(lcs_tab2));
4917 if (n_extra > 0)
4918 len += n_extra - tab_len;
4919#endif
4920 c = lcs_tab1;
4921 p = alloc((unsigned)(len + 1));
4922 vim_memset(p, ' ', len);
4923 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004924 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004925 p_extra_free = p;
4926 for (i = 0; i < tab_len; i++)
4927 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004928 if (*p == NUL)
4929 {
4930 tab_len = i;
4931 break;
4932 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004933#ifdef FEAT_MBYTE
4934 mb_char2bytes(lcs_tab2, p);
4935 p += mb_char2len(lcs_tab2);
4936 n_extra += mb_char2len(lcs_tab2)
4937 - (saved_nextra > 0 ? 1 : 0);
4938#else
4939 p[i] = lcs_tab2;
4940#endif
4941 }
4942 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004943#ifdef FEAT_CONCEAL
4944 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4945 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004946 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004947 n_extra -= vcol_off;
4948#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004949 }
4950#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004951#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004952 {
4953 int vc_saved = vcol_off;
4954
4955 /* Tab alignment should be identical regardless of
4956 * 'conceallevel' value. So tab compensates of all
4957 * previous concealed characters, and thus resets
4958 * vcol_off and boguscols accumulated so far in the
4959 * line. Note that the tab can be longer than
4960 * 'tabstop' when there are concealed characters. */
4961 FIX_FOR_BOGUSCOLS;
4962
4963 /* Make sure, the highlighting for the tab char will be
4964 * correctly set further below (effectively reverts the
4965 * FIX_FOR_BOGSUCOLS macro */
4966 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004967 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004968 tab_len += vc_saved;
4969 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971#ifdef FEAT_MBYTE
4972 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4973#endif
4974 if (wp->w_p_list)
4975 {
4976 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004977#ifdef FEAT_LINEBREAK
4978 if (wp->w_p_lbr)
4979 c_extra = NUL; /* using p_extra from above */
4980 else
4981#endif
4982 c_extra = lcs_tab2;
4983 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004984 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 saved_attr2 = char_attr; /* save current attr */
4986#ifdef FEAT_MBYTE
4987 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004988 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004991 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004992 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994#endif
4995 }
4996 else
4997 {
4998 c_extra = ' ';
4999 c = ' ';
5000 }
5001 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005002 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005003 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005004 || ((fromcol >= 0 || fromcol_prev >= 0)
5005 && tocol > vcol
5006 && VIsual_mode != Ctrl_V
5007 && (
5008# ifdef FEAT_RIGHTLEFT
5009 wp->w_p_rl ? (col >= 0) :
5010# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005011 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005012 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005013 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005014 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005015 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005017 /* Display a '$' after the line or highlight an extra
5018 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5020 /* For a diff line the highlighting continues after the
5021 * "$". */
5022 if (
5023# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005024 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025# ifdef LINE_ATTR
5026 &&
5027# endif
5028# endif
5029# ifdef LINE_ATTR
5030 line_attr == 0
5031# endif
5032 )
5033#endif
5034 {
5035#ifdef FEAT_VIRTUALEDIT
5036 /* In virtualedit, visual selections may extend
5037 * beyond end of line. */
5038 if (area_highlighting && virtual_active()
5039 && tocol != MAXCOL && vcol < tocol)
5040 n_extra = 0;
5041 else
5042#endif
5043 {
5044 p_extra = at_end_str;
5045 n_extra = 1;
5046 c_extra = NUL;
5047 }
5048 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005049 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005050 c = lcs_eol;
5051 else
5052 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 lcs_eol_one = -1;
5054 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005055 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005057 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 n_attr = 1;
5059 }
5060#ifdef FEAT_MBYTE
5061 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005062 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005065 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005066 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068 else
5069 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5070#endif
5071 }
5072 else if (c != NUL)
5073 {
5074 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005075 if (n_extra == 0)
5076 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077#ifdef FEAT_RIGHTLEFT
5078 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5079 rl_mirror(p_extra); /* reverse "<12>" */
5080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005082#ifdef FEAT_LINEBREAK
5083 if (wp->w_p_lbr)
5084 {
5085 char_u *p;
5086
5087 c = *p_extra;
5088 p = alloc((unsigned)n_extra + 1);
5089 vim_memset(p, ' ', n_extra);
5090 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5091 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005092 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005093 p_extra_free = p_extra = p;
5094 }
5095 else
5096#endif
5097 {
5098 n_extra = byte2cells(c) - 1;
5099 c = *p_extra++;
5100 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005101 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
5103 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005104 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 saved_attr2 = char_attr; /* save current attr */
5106 }
5107#ifdef FEAT_MBYTE
5108 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5109#endif
5110 }
5111#ifdef FEAT_VIRTUALEDIT
5112 else if (VIsual_active
5113 && (VIsual_mode == Ctrl_V
5114 || VIsual_mode == 'v')
5115 && virtual_active()
5116 && tocol != MAXCOL
5117 && vcol < tocol
5118 && (
5119# ifdef FEAT_RIGHTLEFT
5120 wp->w_p_rl ? (col >= 0) :
5121# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005122 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
5124 c = ' ';
5125 --ptr; /* put it back at the NUL */
5126 }
5127#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005128#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 else if ((
5130# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005131 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005133# ifdef FEAT_TERMINAL
5134 term_attr != 0 ||
5135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 ) && (
5138# ifdef FEAT_RIGHTLEFT
5139 wp->w_p_rl ? (col >= 0) :
5140# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005141 (col
5142# ifdef FEAT_CONCEAL
5143 - boguscols
5144# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005145 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 /* Highlight until the right side of the window */
5148 c = ' ';
5149 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005150
5151 /* Remember we do the char for line highlighting. */
5152 ++did_line_attr;
5153
5154 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005155 if (line_attr != 0 && char_attr == search_attr
5156 && (did_line_attr > 1
5157 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005158 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159# ifdef FEAT_DIFF
5160 if (diff_hlf == HLF_TXD)
5161 {
5162 diff_hlf = HLF_CHD;
5163 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005164 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005165 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005166 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5167 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005168 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005172# ifdef FEAT_TERMINAL
5173 if (term_attr != 0)
5174 {
5175 char_attr = term_attr;
5176 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5177 char_attr = hl_combine_attr(char_attr,
5178 HL_ATTR(HLF_CUL));
5179 }
5180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182#endif
5183 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005184
5185#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005186 if ( wp->w_p_cole > 0
5187 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005188 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005189 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005190 && !(lnum_in_visual_area
5191 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005192 {
5193 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005194 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005195 && (syn_get_sub_char() != NUL || match_conc
5196 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005197 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005198 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005199 /* First time at this concealed item: display one
5200 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005201 if (match_conc)
5202 c = match_conc;
5203 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005204 c = syn_get_sub_char();
5205 else if (lcs_conceal != NUL)
5206 c = lcs_conceal;
5207 else
5208 c = ' ';
5209
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005210 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005211
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005212 if (n_extra > 0)
5213 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005214 vcol += n_extra;
5215 if (wp->w_p_wrap && n_extra > 0)
5216 {
5217# ifdef FEAT_RIGHTLEFT
5218 if (wp->w_p_rl)
5219 {
5220 col -= n_extra;
5221 boguscols -= n_extra;
5222 }
5223 else
5224# endif
5225 {
5226 boguscols += n_extra;
5227 col += n_extra;
5228 }
5229 }
5230 n_extra = 0;
5231 n_attr = 0;
5232 }
5233 else if (n_skip == 0)
5234 {
5235 is_concealing = TRUE;
5236 n_skip = 1;
5237 }
5238# ifdef FEAT_MBYTE
5239 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005240 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005241 {
5242 mb_utf8 = TRUE;
5243 u8cc[0] = 0;
5244 c = 0xc0;
5245 }
5246 else
5247 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5248# endif
5249 }
5250 else
5251 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005252 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005253 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005254 }
5255#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
5257
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005258#ifdef FEAT_CONCEAL
5259 /* In the cursor line and we may be concealing characters: correct
5260 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005261 if (!did_wcol && draw_state == WL_LINE
5262 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005263 && conceal_cursor_line(wp)
5264 && (int)wp->w_virtcol <= vcol + n_skip)
5265 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005266# ifdef FEAT_RIGHTLEFT
5267 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005268 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005269 else
5270# endif
5271 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005272 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005273 did_wcol = TRUE;
5274 }
5275#endif
5276
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 /* Don't override visual selection highlighting. */
5278 if (n_attr > 0
5279 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005280 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 char_attr = extra_attr;
5282
Bram Moolenaar81695252004-12-29 20:58:21 +00005283#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 /* XIM don't send preedit_start and preedit_end, but they send
5285 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5286 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005287 if (p_imst == IM_ON_THE_SPOT
5288 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005289 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 && (State & INSERT)
5291 && !p_imdisable
5292 && im_is_preediting()
5293 && draw_state == WL_LINE)
5294 {
5295 colnr_T tcol;
5296
5297 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005298 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 else
5300 tcol = preedit_end_col;
5301 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5302 {
5303 if (feedback_old_attr < 0)
5304 {
5305 feedback_col = 0;
5306 feedback_old_attr = char_attr;
5307 }
5308 char_attr = im_get_feedback_attr(feedback_col);
5309 if (char_attr < 0)
5310 char_attr = feedback_old_attr;
5311 feedback_col++;
5312 }
5313 else if (feedback_old_attr >= 0)
5314 {
5315 char_attr = feedback_old_attr;
5316 feedback_old_attr = -1;
5317 feedback_col = 0;
5318 }
5319 }
5320#endif
5321 /*
5322 * Handle the case where we are in column 0 but not on the first
5323 * character of the line and the user wants us to show us a
5324 * special character (via 'listchars' option "precedes:<char>".
5325 */
5326 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005327 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5329#ifdef FEAT_DIFF
5330 && filler_todo <= 0
5331#endif
5332 && draw_state > WL_NR
5333 && c != NUL)
5334 {
5335 c = lcs_prec;
5336 lcs_prec_todo = NUL;
5337#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005338 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5339 {
5340 /* Double-width character being overwritten by the "precedes"
5341 * character, need to fill up half the character. */
5342 c_extra = MB_FILLER_CHAR;
5343 n_extra = 1;
5344 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005345 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005348 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005351 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005352 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
5354 else
5355 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5356#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005357 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
5359 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005360 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 n_attr3 = 1;
5362 }
5363 }
5364
5365 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005366 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005368 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005369#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005370 || did_line_attr == 1
5371#endif
5372 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005374#ifdef FEAT_SEARCH_EXTRA
5375 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005376
5377 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005378 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005379 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005380#endif
5381
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005382 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 * highlight match at end of line. If it's beyond the last
5384 * char on the screen, just overwrite that one (tricky!) Not
5385 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005386#ifdef FEAT_SEARCH_EXTRA
5387 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005388 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005389 prevcol_hl_flag = TRUE;
5390 else
5391 {
5392 cur = wp->w_match_head;
5393 while (cur != NULL)
5394 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005395 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005396 {
5397 prevcol_hl_flag = TRUE;
5398 break;
5399 }
5400 cur = cur->next;
5401 }
5402 }
5403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005405 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005406 && (VIsual_mode != Ctrl_V
5407 || lnum == VIsual.lnum
5408 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005409 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#ifdef FEAT_SEARCH_EXTRA
5411 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005412 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005413# ifdef FEAT_SYN_HL
5414 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5415 && !(wp == curwin && VIsual_active))
5416# endif
5417# ifdef FEAT_DIFF
5418 && diff_hlf == (hlf_T)0
5419# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005420# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005421 && did_line_attr <= 1
5422# endif
5423 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424#endif
5425 ))
5426 {
5427 int n = 0;
5428
5429#ifdef FEAT_RIGHTLEFT
5430 if (wp->w_p_rl)
5431 {
5432 if (col < 0)
5433 n = 1;
5434 }
5435 else
5436#endif
5437 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005438 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 n = -1;
5440 }
5441 if (n != 0)
5442 {
5443 /* At the window boundary, highlight the last character
5444 * instead (better than nothing). */
5445 off += n;
5446 col += n;
5447 }
5448 else
5449 {
5450 /* Add a blank character to highlight. */
5451 ScreenLines[off] = ' ';
5452#ifdef FEAT_MBYTE
5453 if (enc_utf8)
5454 ScreenLinesUC[off] = 0;
5455#endif
5456 }
5457#ifdef FEAT_SEARCH_EXTRA
5458 if (area_attr == 0)
5459 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005460 /* Use attributes from match with highest priority among
5461 * 'search_hl' and the match list. */
5462 char_attr = search_hl.attr;
5463 cur = wp->w_match_head;
5464 shl_flag = FALSE;
5465 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005466 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005467 if (shl_flag == FALSE
5468 && ((cur != NULL
5469 && cur->priority > SEARCH_HL_PRIORITY)
5470 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005471 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005472 shl = &search_hl;
5473 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005474 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005475 else
5476 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005477 if ((ptr - line) - 1 == (long)shl->startcol
5478 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005479 char_attr = shl->attr;
5480 if (shl != &search_hl && cur != NULL)
5481 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484#endif
5485 ScreenAttrs[off] = char_attr;
5486#ifdef FEAT_RIGHTLEFT
5487 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005490 --off;
5491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 else
5493#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005496 ++off;
5497 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005498 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005499#ifdef FEAT_SYN_HL
5500 eol_hl_off = 1;
5501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
Bram Moolenaar91170f82006-05-05 21:15:17 +00005505 /*
5506 * At end of the text line.
5507 */
5508 if (c == NUL)
5509 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005510#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005511 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005512 if (wp->w_p_wrap)
5513 v = wp->w_skipcol;
5514 else
5515 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005516
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005517 /* check if line ends before left margin */
5518 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005519 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005520#ifdef FEAT_CONCEAL
5521 /* Get rid of the boguscols now, we want to draw until the right
5522 * edge for 'cursorcolumn'. */
5523 col -= boguscols;
5524 boguscols = 0;
5525#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005526
5527 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005528 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005529
5530 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005531 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5532 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005533 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005534 && lnum != wp->w_cursor.lnum)
5535 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005536# ifdef FEAT_RIGHTLEFT
5537 && !wp->w_p_rl
5538# endif
5539 )
5540 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005541 int rightmost_vcol = 0;
5542 int i;
5543
5544 if (wp->w_p_cuc)
5545 rightmost_vcol = wp->w_virtcol;
5546 if (draw_color_col)
5547 /* determine rightmost colorcolumn to possibly draw */
5548 for (i = 0; color_cols[i] >= 0; ++i)
5549 if (rightmost_vcol < color_cols[i])
5550 rightmost_vcol = color_cols[i];
5551
Bram Moolenaar02631462017-09-22 15:20:32 +02005552 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005553 {
5554 ScreenLines[off] = ' ';
5555#ifdef FEAT_MBYTE
5556 if (enc_utf8)
5557 ScreenLinesUC[off] = 0;
5558#endif
5559 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005560 if (draw_color_col)
5561 draw_color_col = advance_color_col(VCOL_HLC,
5562 &color_cols);
5563
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005564 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005565 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005566 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005567 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005568 else
5569 ScreenAttrs[off++] = 0;
5570
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005571 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005572 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005573
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005574 ++vcol;
5575 }
5576 }
5577#endif
5578
Bram Moolenaar53f81742017-09-22 14:35:51 +02005579 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005580 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 row++;
5582
5583 /*
5584 * Update w_cline_height and w_cline_folded if the cursor line was
5585 * updated (saves a call to plines() later).
5586 */
5587 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5588 {
5589 curwin->w_cline_row = startrow;
5590 curwin->w_cline_height = row - startrow;
5591#ifdef FEAT_FOLDING
5592 curwin->w_cline_folded = FALSE;
5593#endif
5594 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5595 }
5596
5597 break;
5598 }
5599
5600 /* line continues beyond line end */
5601 if (lcs_ext
5602 && !wp->w_p_wrap
5603#ifdef FEAT_DIFF
5604 && filler_todo <= 0
5605#endif
5606 && (
5607#ifdef FEAT_RIGHTLEFT
5608 wp->w_p_rl ? col == 0 :
5609#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005610 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005612 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5614 {
5615 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005616 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617#ifdef FEAT_MBYTE
5618 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005619 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005622 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005623 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
5625 else
5626 mb_utf8 = FALSE;
5627#endif
5628 }
5629
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005630#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005631 /* advance to the next 'colorcolumn' */
5632 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005633 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005634
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005635 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005636 * highlight the cursor position itself.
5637 * Also highlight the 'colorcolumn' if it is different than
5638 * 'cursorcolumn' */
5639 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005640 if (draw_state == WL_LINE && !lnum_in_visual_area
5641 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005642 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005643 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005644 && lnum != wp->w_cursor.lnum)
5645 {
5646 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005647 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005648 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005649 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005650 {
5651 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005652 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005653 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005654 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005655#endif
5656
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 /*
5658 * Store character to be displayed.
5659 * Skip characters that are left of the screen for 'nowrap'.
5660 */
5661 vcol_prev = vcol;
5662 if (draw_state < WL_LINE || n_skip <= 0)
5663 {
5664 /*
5665 * Store the character.
5666 */
5667#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5668 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5669 {
5670 /* A double-wide character is: put first halve in left cell. */
5671 --off;
5672 --col;
5673 }
5674#endif
5675 ScreenLines[off] = c;
5676#ifdef FEAT_MBYTE
5677 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005678 {
5679 if ((mb_c & 0xff00) == 0x8e00)
5680 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 else if (enc_utf8)
5684 {
5685 if (mb_utf8)
5686 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005687 int i;
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005690 if ((c & 0xff) == 0)
5691 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005692 for (i = 0; i < Screen_mco; ++i)
5693 {
5694 ScreenLinesC[i][off] = u8cc[i];
5695 if (u8cc[i] == 0)
5696 break;
5697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699 else
5700 ScreenLinesUC[off] = 0;
5701 }
5702 if (multi_attr)
5703 {
5704 ScreenAttrs[off] = multi_attr;
5705 multi_attr = 0;
5706 }
5707 else
5708#endif
5709 ScreenAttrs[off] = char_attr;
5710
5711#ifdef FEAT_MBYTE
5712 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5713 {
5714 /* Need to fill two screen columns. */
5715 ++off;
5716 ++col;
5717 if (enc_utf8)
5718 /* UTF-8: Put a 0 in the second screen char. */
5719 ScreenLines[off] = 0;
5720 else
5721 /* DBCS: Put second byte in the second screen char. */
5722 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005723 if (draw_state > WL_NR
5724#ifdef FEAT_DIFF
5725 && filler_todo <= 0
5726#endif
5727 )
5728 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 /* When "tocol" is halfway a character, set it to the end of
5730 * the character, otherwise highlighting won't stop. */
5731 if (tocol == vcol)
5732 ++tocol;
5733#ifdef FEAT_RIGHTLEFT
5734 if (wp->w_p_rl)
5735 {
5736 /* now it's time to backup one cell */
5737 --off;
5738 --col;
5739 }
5740#endif
5741 }
5742#endif
5743#ifdef FEAT_RIGHTLEFT
5744 if (wp->w_p_rl)
5745 {
5746 --off;
5747 --col;
5748 }
5749 else
5750#endif
5751 {
5752 ++off;
5753 ++col;
5754 }
5755 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005756#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005757 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005758 {
5759 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005760 ++vcol_off;
5761 if (n_extra > 0)
5762 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005763 if (wp->w_p_wrap)
5764 {
5765 /*
5766 * Special voodoo required if 'wrap' is on.
5767 *
5768 * Advance the column indicator to force the line
5769 * drawing to wrap early. This will make the line
5770 * take up the same screen space when parts are concealed,
5771 * so that cursor line computations aren't messed up.
5772 *
5773 * To avoid the fictitious advance of 'col' causing
5774 * trailing junk to be written out of the screen line
5775 * we are building, 'boguscols' keeps track of the number
5776 * of bad columns we have advanced.
5777 */
5778 if (n_extra > 0)
5779 {
5780 vcol += n_extra;
5781# ifdef FEAT_RIGHTLEFT
5782 if (wp->w_p_rl)
5783 {
5784 col -= n_extra;
5785 boguscols -= n_extra;
5786 }
5787 else
5788# endif
5789 {
5790 col += n_extra;
5791 boguscols += n_extra;
5792 }
5793 n_extra = 0;
5794 n_attr = 0;
5795 }
5796
5797
5798# ifdef FEAT_MBYTE
5799 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5800 {
5801 /* Need to fill two screen columns. */
5802# ifdef FEAT_RIGHTLEFT
5803 if (wp->w_p_rl)
5804 {
5805 --boguscols;
5806 --col;
5807 }
5808 else
5809# endif
5810 {
5811 ++boguscols;
5812 ++col;
5813 }
5814 }
5815# endif
5816
5817# ifdef FEAT_RIGHTLEFT
5818 if (wp->w_p_rl)
5819 {
5820 --boguscols;
5821 --col;
5822 }
5823 else
5824# endif
5825 {
5826 ++boguscols;
5827 ++col;
5828 }
5829 }
5830 else
5831 {
5832 if (n_extra > 0)
5833 {
5834 vcol += n_extra;
5835 n_extra = 0;
5836 n_attr = 0;
5837 }
5838 }
5839
5840 }
5841#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 else
5843 --n_skip;
5844
Bram Moolenaar64486672010-05-16 15:46:46 +02005845 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5846 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005847 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#ifdef FEAT_DIFF
5849 && filler_todo <= 0
5850#endif
5851 )
5852 ++vcol;
5853
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005854#ifdef FEAT_SYN_HL
5855 if (vcol_save_attr >= 0)
5856 char_attr = vcol_save_attr;
5857#endif
5858
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 /* restore attributes after "predeces" in 'listchars' */
5860 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5861 char_attr = saved_attr3;
5862
5863 /* restore attributes after last 'listchars' or 'number' char */
5864 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5865 char_attr = saved_attr2;
5866
5867 /*
5868 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005869 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 */
5871 if ((
5872#ifdef FEAT_RIGHTLEFT
5873 wp->w_p_rl ? (col < 0) :
5874#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005875 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 && (*ptr != NUL
5877#ifdef FEAT_DIFF
5878 || filler_todo > 0
5879#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005880 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5882 )
5883 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005884#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005885 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005886 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005887 boguscols = 0;
5888#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005889 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005890 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 ++row;
5893 ++screen_row;
5894
5895 /* When not wrapping and finished diff lines, or when displayed
5896 * '$' and highlighting until last column, break here. */
5897 if ((!wp->w_p_wrap
5898#ifdef FEAT_DIFF
5899 && filler_todo <= 0
5900#endif
5901 ) || lcs_eol_one == -1)
5902 break;
5903
5904 /* When the window is too narrow draw all "@" lines. */
5905 if (draw_state != WL_LINE
5906#ifdef FEAT_DIFF
5907 && filler_todo <= 0
5908#endif
5909 )
5910 {
5911 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 row = endrow;
5914 }
5915
5916 /* When line got too long for screen break here. */
5917 if (row == endrow)
5918 {
5919 ++row;
5920 break;
5921 }
5922
5923 if (screen_cur_row == screen_row - 1
5924#ifdef FEAT_DIFF
5925 && filler_todo <= 0
5926#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005927 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
5929 /* Remember that the line wraps, used for modeless copy. */
5930 LineWraps[screen_row - 1] = TRUE;
5931
5932 /*
5933 * Special trick to make copy/paste of wrapped lines work with
5934 * xterm/screen: write an extra character beyond the end of
5935 * the line. This will work with all terminal types
5936 * (regardless of the xn,am settings).
5937 * Only do this on a fast tty.
5938 * Only do this if the cursor is on the current line
5939 * (something has been written in it).
5940 * Don't do this for the GUI.
5941 * Don't do this for double-width characters.
5942 * Don't do this for a window not at the right screen border.
5943 */
5944 if (p_tf
5945#ifdef FEAT_GUI
5946 && !gui.in_use
5947#endif
5948#ifdef FEAT_MBYTE
5949 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005950 && ((*mb_off2cells)(LineOffset[screen_row],
5951 LineOffset[screen_row] + screen_Columns)
5952 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005954 + (int)Columns - 2,
5955 LineOffset[screen_row] + screen_Columns)
5956 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#endif
5958 )
5959 {
5960 /* First make sure we are at the end of the screen line,
5961 * then output the same character again to let the
5962 * terminal know about the wrap. If the terminal doesn't
5963 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005964 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 screen_char(LineOffset[screen_row - 1]
5966 + (unsigned)Columns - 1,
5967 screen_row - 1, (int)(Columns - 1));
5968
5969#ifdef FEAT_MBYTE
5970 /* When there is a multi-byte character, just output a
5971 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005972 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5973 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 out_char(' ');
5975 else
5976#endif
5977 out_char(ScreenLines[LineOffset[screen_row - 1]
5978 + (Columns - 1)]);
5979 /* force a redraw of the first char on the next line */
5980 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5981 screen_start(); /* don't know where cursor is now */
5982 }
5983 }
5984
5985 col = 0;
5986 off = (unsigned)(current_ScreenLine - ScreenLines);
5987#ifdef FEAT_RIGHTLEFT
5988 if (wp->w_p_rl)
5989 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005990 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 off += col;
5992 }
5993#endif
5994
5995 /* reset the drawing state for the start of a wrapped line */
5996 draw_state = WL_START;
5997 saved_n_extra = n_extra;
5998 saved_p_extra = p_extra;
5999 saved_c_extra = c_extra;
6000 saved_char_attr = char_attr;
6001 n_extra = 0;
6002 lcs_prec_todo = lcs_prec;
6003#ifdef FEAT_LINEBREAK
6004# ifdef FEAT_DIFF
6005 if (filler_todo <= 0)
6006# endif
6007 need_showbreak = TRUE;
6008#endif
6009#ifdef FEAT_DIFF
6010 --filler_todo;
6011 /* When the filler lines are actually below the last line of the
6012 * file, don't draw the line itself, break here. */
6013 if (filler_todo == 0 && wp->w_botfill)
6014 break;
6015#endif
6016 }
6017
6018 } /* for every character in the line */
6019
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006020#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006021 /* After an empty line check first word for capital. */
6022 if (*skipwhite(line) == NUL)
6023 {
6024 capcol_lnum = lnum + 1;
6025 cap_col = 0;
6026 }
6027#endif
6028
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006029 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 return row;
6031}
6032
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006033#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006034/*
6035 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006036 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006037 */
6038 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006039comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006040{
6041 int i;
6042
6043 for (i = 0; i < Screen_mco; ++i)
6044 {
6045 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6046 return TRUE;
6047 if (ScreenLinesC[i][off_from] == 0)
6048 break;
6049 }
6050 return FALSE;
6051}
6052#endif
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054/*
6055 * Check whether the given character needs redrawing:
6056 * - the (first byte of the) character is different
6057 * - the attributes are different
6058 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006059 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 */
6061 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006062char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
6064 if (cols > 0
6065 && ((ScreenLines[off_from] != ScreenLines[off_to]
6066 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6067
6068#ifdef FEAT_MBYTE
6069 || (enc_dbcs != 0
6070 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6071 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6072 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6073 : (cols > 1 && ScreenLines[off_from + 1]
6074 != ScreenLines[off_to + 1])))
6075 || (enc_utf8
6076 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6077 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006078 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006079 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6080 && ScreenLines[off_from + 1]
6081 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082#endif
6083 ))
6084 return TRUE;
6085 return FALSE;
6086}
6087
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006088#if defined(FEAT_TERMINAL) || defined(PROTO)
6089/*
6090 * Return the index in ScreenLines[] for the current screen line.
6091 */
6092 int
6093screen_get_current_line_off()
6094{
6095 return (int)(current_ScreenLine - ScreenLines);
6096}
6097#endif
6098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099/*
6100 * Move one "cooked" screen line to the screen, but only the characters that
6101 * have actually changed. Handle insert/delete character.
6102 * "coloff" gives the first column on the screen for this line.
6103 * "endcol" gives the columns where valid characters are.
6104 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6105 * needs to be cleared, negative otherwise.
6106 * "rlflag" is TRUE in a rightleft window:
6107 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6108 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6109 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006110 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006111screen_line(
6112 int row,
6113 int coloff,
6114 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006115 int clear_width,
6116 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
6118 unsigned off_from;
6119 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006120#ifdef FEAT_MBYTE
6121 unsigned max_off_from;
6122 unsigned max_off_to;
6123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 int force = FALSE; /* force update rest of the line */
6127 int redraw_this /* bool: does character need redraw? */
6128#ifdef FEAT_GUI
6129 = TRUE /* For GUI when while-loop empty */
6130#endif
6131 ;
6132 int redraw_next; /* redraw_this for next character */
6133#ifdef FEAT_MBYTE
6134 int clear_next = FALSE;
6135 int char_cells; /* 1: normal char */
6136 /* 2: occupies two display cells */
6137# define CHAR_CELLS char_cells
6138#else
6139# define CHAR_CELLS 1
6140#endif
6141
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006142 /* Check for illegal row and col, just in case. */
6143 if (row >= Rows)
6144 row = Rows - 1;
6145 if (endcol > Columns)
6146 endcol = Columns;
6147
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148# ifdef FEAT_CLIPBOARD
6149 clip_may_clear_selection(row, row);
6150# endif
6151
6152 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6153 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006154#ifdef FEAT_MBYTE
6155 max_off_from = off_from + screen_Columns;
6156 max_off_to = LineOffset[row] + screen_Columns;
6157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159#ifdef FEAT_RIGHTLEFT
6160 if (rlflag)
6161 {
6162 /* Clear rest first, because it's left of the text. */
6163 if (clear_width > 0)
6164 {
6165 while (col <= endcol && ScreenLines[off_to] == ' '
6166 && ScreenAttrs[off_to] == 0
6167# ifdef FEAT_MBYTE
6168 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6169# endif
6170 )
6171 {
6172 ++off_to;
6173 ++col;
6174 }
6175 if (col <= endcol)
6176 screen_fill(row, row + 1, col + coloff,
6177 endcol + coloff + 1, ' ', ' ', 0);
6178 }
6179 col = endcol + 1;
6180 off_to = LineOffset[row] + col + coloff;
6181 off_from += col;
6182 endcol = (clear_width > 0 ? clear_width : -clear_width);
6183 }
6184#endif /* FEAT_RIGHTLEFT */
6185
6186 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6187
6188 while (col < endcol)
6189 {
6190#ifdef FEAT_MBYTE
6191 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006192 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 else
6194 char_cells = 1;
6195#endif
6196
6197 redraw_this = redraw_next;
6198 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6199 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6200
6201#ifdef FEAT_GUI
6202 /* If the next character was bold, then redraw the current character to
6203 * remove any pixels that might have spilt over into us. This only
6204 * happens in the GUI.
6205 */
6206 if (redraw_next && gui.in_use)
6207 {
6208 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006209 if (hl > HL_ALL)
6210 hl = syn_attr2attr(hl);
6211 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 redraw_this = TRUE;
6213 }
6214#endif
6215
6216 if (redraw_this)
6217 {
6218 /*
6219 * Special handling when 'xs' termcap flag set (hpterm):
6220 * Attributes for characters are stored at the position where the
6221 * cursor is when writing the highlighting code. The
6222 * start-highlighting code must be written with the cursor on the
6223 * first highlighted character. The stop-highlighting code must
6224 * be written with the cursor just after the last highlighted
6225 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006226 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 * to clear the rest of the line, and force redrawing it
6228 * completely.
6229 */
6230 if ( p_wiv
6231 && !force
6232#ifdef FEAT_GUI
6233 && !gui.in_use
6234#endif
6235 && ScreenAttrs[off_to] != 0
6236 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6237 {
6238 /*
6239 * Need to remove highlighting attributes here.
6240 */
6241 windgoto(row, col + coloff);
6242 out_str(T_CE); /* clear rest of this screen line */
6243 screen_start(); /* don't know where cursor is now */
6244 force = TRUE; /* force redraw of rest of the line */
6245 redraw_next = TRUE; /* or else next char would miss out */
6246
6247 /*
6248 * If the previous character was highlighted, need to stop
6249 * highlighting at this character.
6250 */
6251 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6252 {
6253 screen_attr = ScreenAttrs[off_to - 1];
6254 term_windgoto(row, col + coloff);
6255 screen_stop_highlight();
6256 }
6257 else
6258 screen_attr = 0; /* highlighting has stopped */
6259 }
6260#ifdef FEAT_MBYTE
6261 if (enc_dbcs != 0)
6262 {
6263 /* Check if overwriting a double-byte with a single-byte or
6264 * the other way around requires another character to be
6265 * redrawn. For UTF-8 this isn't needed, because comparing
6266 * ScreenLinesUC[] is sufficient. */
6267 if (char_cells == 1
6268 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006269 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 /* Writing a single-cell character over a double-cell
6272 * character: need to redraw the next cell. */
6273 ScreenLines[off_to + 1] = 0;
6274 redraw_next = TRUE;
6275 }
6276 else if (char_cells == 2
6277 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006278 && (*mb_off2cells)(off_to, max_off_to) == 1
6279 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
6281 /* Writing the second half of a double-cell character over
6282 * a double-cell character: need to redraw the second
6283 * cell. */
6284 ScreenLines[off_to + 2] = 0;
6285 redraw_next = TRUE;
6286 }
6287
6288 if (enc_dbcs == DBCS_JPNU)
6289 ScreenLines2[off_to] = ScreenLines2[off_from];
6290 }
6291 /* When writing a single-width character over a double-width
6292 * character and at the end of the redrawn text, need to clear out
6293 * the right halve of the old character.
6294 * Also required when writing the right halve of a double-width
6295 * char over the left halve of an existing one. */
6296 if (has_mbyte && col + char_cells == endcol
6297 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006298 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006300 && (*mb_off2cells)(off_to, max_off_to) == 1
6301 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 clear_next = TRUE;
6303#endif
6304
6305 ScreenLines[off_to] = ScreenLines[off_from];
6306#ifdef FEAT_MBYTE
6307 if (enc_utf8)
6308 {
6309 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6310 if (ScreenLinesUC[off_from] != 0)
6311 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006312 int i;
6313
6314 for (i = 0; i < Screen_mco; ++i)
6315 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 }
6317 }
6318 if (char_cells == 2)
6319 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6320#endif
6321
6322#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006323 /* The bold trick makes a single column of pixels appear in the
6324 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 * character should be redrawn too. This happens for our own GUI
6326 * and for some xterms. */
6327 if (
6328# ifdef FEAT_GUI
6329 gui.in_use
6330# endif
6331# if defined(FEAT_GUI) && defined(UNIX)
6332 ||
6333# endif
6334# ifdef UNIX
6335 term_is_xterm
6336# endif
6337 )
6338 {
6339 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006340 if (hl > HL_ALL)
6341 hl = syn_attr2attr(hl);
6342 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 redraw_next = TRUE;
6344 }
6345#endif
6346 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6347#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006348 /* For simplicity set the attributes of second half of a
6349 * double-wide character equal to the first half. */
6350 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006352
6353 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 else
6356#endif
6357 screen_char(off_to, row, col + coloff);
6358 }
6359 else if ( p_wiv
6360#ifdef FEAT_GUI
6361 && !gui.in_use
6362#endif
6363 && col + coloff > 0)
6364 {
6365 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6366 {
6367 /*
6368 * Don't output stop-highlight when moving the cursor, it will
6369 * stop the highlighting when it should continue.
6370 */
6371 screen_attr = 0;
6372 }
6373 else if (screen_attr != 0)
6374 screen_stop_highlight();
6375 }
6376
6377 off_to += CHAR_CELLS;
6378 off_from += CHAR_CELLS;
6379 col += CHAR_CELLS;
6380 }
6381
6382#ifdef FEAT_MBYTE
6383 if (clear_next)
6384 {
6385 /* Clear the second half of a double-wide character of which the left
6386 * half was overwritten with a single-wide character. */
6387 ScreenLines[off_to] = ' ';
6388 if (enc_utf8)
6389 ScreenLinesUC[off_to] = 0;
6390 screen_char(off_to, row, col + coloff);
6391 }
6392#endif
6393
6394 if (clear_width > 0
6395#ifdef FEAT_RIGHTLEFT
6396 && !rlflag
6397#endif
6398 )
6399 {
6400#ifdef FEAT_GUI
6401 int startCol = col;
6402#endif
6403
6404 /* blank out the rest of the line */
6405 while (col < clear_width && ScreenLines[off_to] == ' '
6406 && ScreenAttrs[off_to] == 0
6407#ifdef FEAT_MBYTE
6408 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6409#endif
6410 )
6411 {
6412 ++off_to;
6413 ++col;
6414 }
6415 if (col < clear_width)
6416 {
6417#ifdef FEAT_GUI
6418 /*
6419 * In the GUI, clearing the rest of the line may leave pixels
6420 * behind if the first character cleared was bold. Some bold
6421 * fonts spill over the left. In this case we redraw the previous
6422 * character too. If we didn't skip any blanks above, then we
6423 * only redraw if the character wasn't already redrawn anyway.
6424 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006425 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
6427 hl = ScreenAttrs[off_to];
6428 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006429 {
6430 int prev_cells = 1;
6431# ifdef FEAT_MBYTE
6432 if (enc_utf8)
6433 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6434 * that its width is 2. */
6435 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6436 else if (enc_dbcs != 0)
6437 {
6438 /* find previous character by counting from first
6439 * column and get its width. */
6440 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006441 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006442
6443 while (off < off_to)
6444 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006445 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006446 off += prev_cells;
6447 }
6448 }
6449
6450 if (enc_dbcs != 0 && prev_cells > 1)
6451 screen_char_2(off_to - prev_cells, row,
6452 col + coloff - prev_cells);
6453 else
6454# endif
6455 screen_char(off_to - prev_cells, row,
6456 col + coloff - prev_cells);
6457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 }
6459#endif
6460 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6461 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 off_to += clear_width - col;
6463 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 }
6465 }
6466
6467 if (clear_width > 0)
6468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 /* For a window that's left of another, draw the separator char. */
6470 if (col + coloff < Columns)
6471 {
6472 int c;
6473
6474 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006475 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006476#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006477 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6478 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 || ScreenAttrs[off_to] != hl)
6481 {
6482 ScreenLines[off_to] = c;
6483 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006484#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 if (enc_utf8)
6486 {
6487 if (c >= 0x80)
6488 {
6489 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006490 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 }
6492 else
6493 ScreenLinesUC[off_to] = 0;
6494 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 screen_char(off_to, row, col + coloff);
6497 }
6498 }
6499 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 LineWraps[row] = FALSE;
6501 }
6502}
6503
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006504#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006506 * Mirror text "str" for right-left displaying.
6507 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006510rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
6512 char_u *p1, *p2;
6513 int t;
6514
6515 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6516 {
6517 t = *p1;
6518 *p1 = *p2;
6519 *p2 = t;
6520 }
6521}
6522#endif
6523
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524/*
6525 * mark all status lines for redraw; used after first :cd
6526 */
6527 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006528status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
6530 win_T *wp;
6531
Bram Moolenaar29323592016-07-24 22:04:11 +02006532 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (wp->w_status_height)
6534 {
6535 wp->w_redr_status = TRUE;
6536 redraw_later(VALID);
6537 }
6538}
6539
6540/*
6541 * mark all status lines of the current buffer for redraw
6542 */
6543 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006544status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 win_T *wp;
6547
Bram Moolenaar29323592016-07-24 22:04:11 +02006548 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6550 {
6551 wp->w_redr_status = TRUE;
6552 redraw_later(VALID);
6553 }
6554}
6555
6556/*
6557 * Redraw all status lines that need to be redrawn.
6558 */
6559 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006560redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562 win_T *wp;
6563
Bram Moolenaar29323592016-07-24 22:04:11 +02006564 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006566 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006567 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006568 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
Bram Moolenaar4033c552017-09-16 20:54:51 +02006571#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572/*
6573 * Redraw all status lines at the bottom of frame "frp".
6574 */
6575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006576win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 if (frp->fr_layout == FR_LEAF)
6579 frp->fr_win->w_redr_status = TRUE;
6580 else if (frp->fr_layout == FR_ROW)
6581 {
6582 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6583 win_redraw_last_status(frp);
6584 }
6585 else /* frp->fr_layout == FR_COL */
6586 {
6587 frp = frp->fr_child;
6588 while (frp->fr_next != NULL)
6589 frp = frp->fr_next;
6590 win_redraw_last_status(frp);
6591 }
6592}
6593#endif
6594
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595/*
6596 * Draw the verticap separator right of window "wp" starting with line "row".
6597 */
6598 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006599draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
6601 int hl;
6602 int c;
6603
6604 if (wp->w_vsep_width)
6605 {
6606 /* draw the vertical separator right of this window */
6607 c = fillchar_vsep(&hl);
6608 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6609 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6610 c, ' ', hl);
6611 }
6612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
6614#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006615static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616
6617/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006618 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 */
6620 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006621status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 int len = 0;
6624
6625#ifdef FEAT_MENU
6626 int emenu = (xp->xp_context == EXPAND_MENUS
6627 || xp->xp_context == EXPAND_MENUNAMES);
6628
6629 /* Check for menu separators - replace with '|'. */
6630 if (emenu && menu_is_separator(s))
6631 return 1;
6632#endif
6633
6634 while (*s != NUL)
6635 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006636 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006637 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006638 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
6640
6641 return len;
6642}
6643
6644/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006645 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006646 * These are backslashes used for escaping. Do show backslashes in help tags.
6647 */
6648 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006649skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006650{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006651 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006652#ifdef FEAT_MENU
6653 || ((xp->xp_context == EXPAND_MENUS
6654 || xp->xp_context == EXPAND_MENUNAMES)
6655 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6656#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006657 )
6658 {
6659#ifndef BACKSLASH_IN_FILENAME
6660 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6661 return 2;
6662#endif
6663 return 1;
6664 }
6665 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006666}
6667
6668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 * Show wildchar matches in the status line.
6670 * Show at least the "match" item.
6671 * We start at item 'first_match' in the list and show all matches that fit.
6672 *
6673 * If inversion is possible we use it. Else '=' characters are used.
6674 */
6675 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006676win_redr_status_matches(
6677 expand_T *xp,
6678 int num_matches,
6679 char_u **matches, /* list of matches */
6680 int match,
6681 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6684 int row;
6685 char_u *buf;
6686 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006687 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 int fillchar;
6689 int attr;
6690 int i;
6691 int highlight = TRUE;
6692 char_u *selstart = NULL;
6693 int selstart_col = 0;
6694 char_u *selend = NULL;
6695 static int first_match = 0;
6696 int add_left = FALSE;
6697 char_u *s;
6698#ifdef FEAT_MENU
6699 int emenu;
6700#endif
6701#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6702 int l;
6703#endif
6704
6705 if (matches == NULL) /* interrupted completion? */
6706 return;
6707
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006708#ifdef FEAT_MBYTE
6709 if (has_mbyte)
6710 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6711 else
6712#endif
6713 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 if (buf == NULL)
6715 return;
6716
6717 if (match == -1) /* don't show match but original text */
6718 {
6719 match = 0;
6720 highlight = FALSE;
6721 }
6722 /* count 1 for the ending ">" */
6723 clen = status_match_len(xp, L_MATCH(match)) + 3;
6724 if (match == 0)
6725 first_match = 0;
6726 else if (match < first_match)
6727 {
6728 /* jumping left, as far as we can go */
6729 first_match = match;
6730 add_left = TRUE;
6731 }
6732 else
6733 {
6734 /* check if match fits on the screen */
6735 for (i = first_match; i < match; ++i)
6736 clen += status_match_len(xp, L_MATCH(i)) + 2;
6737 if (first_match > 0)
6738 clen += 2;
6739 /* jumping right, put match at the left */
6740 if ((long)clen > Columns)
6741 {
6742 first_match = match;
6743 /* if showing the last match, we can add some on the left */
6744 clen = 2;
6745 for (i = match; i < num_matches; ++i)
6746 {
6747 clen += status_match_len(xp, L_MATCH(i)) + 2;
6748 if ((long)clen >= Columns)
6749 break;
6750 }
6751 if (i == num_matches)
6752 add_left = TRUE;
6753 }
6754 }
6755 if (add_left)
6756 while (first_match > 0)
6757 {
6758 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6759 if ((long)clen >= Columns)
6760 break;
6761 --first_match;
6762 }
6763
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006764 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766 if (first_match == 0)
6767 {
6768 *buf = NUL;
6769 len = 0;
6770 }
6771 else
6772 {
6773 STRCPY(buf, "< ");
6774 len = 2;
6775 }
6776 clen = len;
6777
6778 i = first_match;
6779 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6780 {
6781 if (i == match)
6782 {
6783 selstart = buf + len;
6784 selstart_col = clen;
6785 }
6786
6787 s = L_MATCH(i);
6788 /* Check for menu separators - replace with '|' */
6789#ifdef FEAT_MENU
6790 emenu = (xp->xp_context == EXPAND_MENUS
6791 || xp->xp_context == EXPAND_MENUNAMES);
6792 if (emenu && menu_is_separator(s))
6793 {
6794 STRCPY(buf + len, transchar('|'));
6795 l = (int)STRLEN(buf + len);
6796 len += l;
6797 clen += l;
6798 }
6799 else
6800#endif
6801 for ( ; *s != NUL; ++s)
6802 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006803 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 clen += ptr2cells(s);
6805#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006806 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
6808 STRNCPY(buf + len, s, l);
6809 s += l - 1;
6810 len += l;
6811 }
6812 else
6813#endif
6814 {
6815 STRCPY(buf + len, transchar_byte(*s));
6816 len += (int)STRLEN(buf + len);
6817 }
6818 }
6819 if (i == match)
6820 selend = buf + len;
6821
6822 *(buf + len++) = ' ';
6823 *(buf + len++) = ' ';
6824 clen += 2;
6825 if (++i == num_matches)
6826 break;
6827 }
6828
6829 if (i != num_matches)
6830 {
6831 *(buf + len++) = '>';
6832 ++clen;
6833 }
6834
6835 buf[len] = NUL;
6836
6837 row = cmdline_row - 1;
6838 if (row >= 0)
6839 {
6840 if (wild_menu_showing == 0)
6841 {
6842 if (msg_scrolled > 0)
6843 {
6844 /* Put the wildmenu just above the command line. If there is
6845 * no room, scroll the screen one line up. */
6846 if (cmdline_row == Rows - 1)
6847 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006848 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 ++msg_scrolled;
6850 }
6851 else
6852 {
6853 ++cmdline_row;
6854 ++row;
6855 }
6856 wild_menu_showing = WM_SCROLLED;
6857 }
6858 else
6859 {
6860 /* Create status line if needed by setting 'laststatus' to 2.
6861 * Set 'winminheight' to zero to avoid that the window is
6862 * resized. */
6863 if (lastwin->w_status_height == 0)
6864 {
6865 save_p_ls = p_ls;
6866 save_p_wmh = p_wmh;
6867 p_ls = 2;
6868 p_wmh = 0;
6869 last_status(FALSE);
6870 }
6871 wild_menu_showing = WM_SHOWN;
6872 }
6873 }
6874
6875 screen_puts(buf, row, 0, attr);
6876 if (selstart != NULL && highlight)
6877 {
6878 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006879 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
6881
6882 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6883 }
6884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 vim_free(buf);
6887}
6888#endif
6889
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890/*
6891 * Redraw the status line of window wp.
6892 *
6893 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006894 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6895 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006897 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006898win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
6900 int row;
6901 char_u *p;
6902 int len;
6903 int fillchar;
6904 int attr;
6905 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006906 static int busy = FALSE;
6907
6908 /* It's possible to get here recursively when 'statusline' (indirectly)
6909 * invokes ":redrawstatus". Simply ignore the call then. */
6910 if (busy)
6911 return;
6912 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
6914 wp->w_redr_status = FALSE;
6915 if (wp->w_status_height == 0)
6916 {
6917 /* no status line, can only be last window */
6918 redraw_cmdline = TRUE;
6919 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006920 else if (!redrawing()
6921#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006922 // don't update status line when popup menu is visible and may be
6923 // drawn over it, unless it will be redrawn later
6924 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006925#endif
6926 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {
6928 /* Don't redraw right now, do it later. */
6929 wp->w_redr_status = TRUE;
6930 }
6931#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006932 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
6934 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006935 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 }
6937#endif
6938 else
6939 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006940 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006942 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 p = NameBuff;
6944 len = (int)STRLEN(p);
6945
Bram Moolenaard85f2712017-07-28 21:51:57 +02006946 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947#ifdef FEAT_QUICKFIX
6948 || wp->w_p_pvw
6949#endif
6950 || bufIsChanged(wp->w_buffer)
6951 || wp->w_buffer->b_p_ro)
6952 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006953 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006955 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 len += (int)STRLEN(p + len);
6957 }
6958#ifdef FEAT_QUICKFIX
6959 if (wp->w_p_pvw)
6960 {
6961 STRCPY(p + len, _("[Preview]"));
6962 len += (int)STRLEN(p + len);
6963 }
6964#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006965 if (bufIsChanged(wp->w_buffer)
6966#ifdef FEAT_TERMINAL
6967 && !bt_terminal(wp->w_buffer)
6968#endif
6969 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {
6971 STRCPY(p + len, "[+]");
6972 len += 3;
6973 }
6974 if (wp->w_buffer->b_p_ro)
6975 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006976 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006977 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 }
6979
Bram Moolenaar02631462017-09-22 15:20:32 +02006980 this_ru_col = ru_col - (Columns - wp->w_width);
6981 if (this_ru_col < (wp->w_width + 1) / 2)
6982 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (this_ru_col <= 1)
6984 {
6985 p = (char_u *)"<"; /* No room for file name! */
6986 len = 1;
6987 }
6988 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989#ifdef FEAT_MBYTE
6990 if (has_mbyte)
6991 {
6992 int clen = 0, i;
6993
6994 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006995 clen = mb_string2cells(p, -1);
6996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 /* Find first character that will fit.
6998 * Going from start to end is much faster for DBCS. */
6999 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007000 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 clen -= (*mb_ptr2cells)(p + i);
7002 len = clen;
7003 if (i > 0)
7004 {
7005 p = p + i - 1;
7006 *p = '<';
7007 ++len;
7008 }
7009
7010 }
7011 else
7012#endif
7013 if (len > this_ru_col - 1)
7014 {
7015 p += len - (this_ru_col - 1);
7016 *p = '<';
7017 len = this_ru_col - 1;
7018 }
7019
7020 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007021 screen_puts(p, row, wp->w_wincol, attr);
7022 screen_fill(row, row + 1, len + wp->w_wincol,
7023 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007025 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7027 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007028 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
7030#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007031 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032#endif
7033 }
7034
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 /*
7036 * May need to draw the character below the vertical separator.
7037 */
7038 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7039 {
7040 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007041 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 else
7043 fillchar = fillchar_vsep(&attr);
7044 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7045 attr);
7046 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007047 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048}
7049
Bram Moolenaar238a5642006-02-21 22:12:05 +00007050#ifdef FEAT_STL_OPT
7051/*
7052 * Redraw the status line according to 'statusline' and take care of any
7053 * errors encountered.
7054 */
7055 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007056redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007057{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007058 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007059 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007060
7061 /* When called recursively return. This can happen when the statusline
7062 * contains an expression that triggers a redraw. */
7063 if (entered)
7064 return;
7065 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007066
Bram Moolenaara742e082016-04-05 21:10:38 +02007067 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007068 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007069 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007070 {
7071 /* When there is an error disable the statusline, otherwise the
7072 * display is messed up with errors and a redraw triggers the problem
7073 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007074 set_string_option_direct((char_u *)"statusline", -1,
7075 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007076 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007077 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007078 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007079 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007080}
7081#endif
7082
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083/*
7084 * Return TRUE if the status line of window "wp" is connected to the status
7085 * line of the window right of it. If not, then it's a vertical separator.
7086 * Only call if (wp->w_vsep_width != 0).
7087 */
7088 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007089stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
7091 frame_T *fr;
7092
7093 fr = wp->w_frame;
7094 while (fr->fr_parent != NULL)
7095 {
7096 if (fr->fr_parent->fr_layout == FR_COL)
7097 {
7098 if (fr->fr_next != NULL)
7099 break;
7100 }
7101 else
7102 {
7103 if (fr->fr_next != NULL)
7104 return TRUE;
7105 }
7106 fr = fr->fr_parent;
7107 }
7108 return FALSE;
7109}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112/*
7113 * Get the value to show for the language mappings, active 'keymap'.
7114 */
7115 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007116get_keymap_str(
7117 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007118 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007119 char_u *buf, /* buffer for the result */
7120 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 char_u *p;
7123
7124 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7125 return FALSE;
7126
7127 {
7128#ifdef FEAT_EVAL
7129 buf_T *old_curbuf = curbuf;
7130 win_T *old_curwin = curwin;
7131 char_u *s;
7132
7133 curbuf = wp->w_buffer;
7134 curwin = wp;
7135 STRCPY(buf, "b:keymap_name"); /* must be writable */
7136 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007137 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 --emsg_skip;
7139 curbuf = old_curbuf;
7140 curwin = old_curwin;
7141 if (p == NULL || *p == NUL)
7142#endif
7143 {
7144#ifdef FEAT_KEYMAP
7145 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7146 p = wp->w_buffer->b_p_keymap;
7147 else
7148#endif
7149 p = (char_u *)"lang";
7150 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007151 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 buf[0] = NUL;
7153#ifdef FEAT_EVAL
7154 vim_free(s);
7155#endif
7156 }
7157 return buf[0] != NUL;
7158}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
7160#if defined(FEAT_STL_OPT) || defined(PROTO)
7161/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162 * Redraw the status line or ruler of window "wp".
7163 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 */
7165 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007166win_redr_custom(
7167 win_T *wp,
7168 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007170 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 int attr;
7172 int curattr;
7173 int row;
7174 int col = 0;
7175 int maxwidth;
7176 int width;
7177 int n;
7178 int len;
7179 int fillchar;
7180 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007181 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007183 struct stl_hlrec hltab[STL_MAX_ITEM];
7184 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007185 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007186 win_T *ewp;
7187 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar1d633412013-12-11 15:52:01 +01007189 /* There is a tiny chance that this gets called recursively: When
7190 * redrawing a status line triggers redrawing the ruler or tabline.
7191 * Avoid trouble by not allowing recursion. */
7192 if (entered)
7193 return;
7194 entered = TRUE;
7195
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007197 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007200 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007201 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007202 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007203 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204 maxwidth = Columns;
7205# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007206 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007209 else
7210 {
7211 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007212 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007213 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007214
7215 if (draw_ruler)
7216 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007217 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007218 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007219 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007220 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007221 if (*++stl == '-')
7222 stl++;
7223 if (atoi((char *)stl))
7224 while (VIM_ISDIGIT(*stl))
7225 stl++;
7226 if (*stl++ != '(')
7227 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007228 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007229 col = ru_col - (Columns - wp->w_width);
7230 if (col < (wp->w_width + 1) / 2)
7231 col = (wp->w_width + 1) / 2;
7232 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007233 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007234 {
7235 row = Rows - 1;
7236 --maxwidth; /* writing in last column may cause scrolling */
7237 fillchar = ' ';
7238 attr = 0;
7239 }
7240
7241# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007242 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007243# endif
7244 }
7245 else
7246 {
7247 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007248 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007249 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007250 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007251# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007252 use_sandbox = was_set_insecurely((char_u *)"statusline",
7253 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007254# endif
7255 }
7256
Bram Moolenaar53f81742017-09-22 14:35:51 +02007257 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007258 }
7259
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007261 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262
Bram Moolenaar61452852011-02-01 18:01:11 +01007263 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7264 * the cursor away and back. */
7265 ewp = wp == NULL ? curwin : wp;
7266 p_crb_save = ewp->w_p_crb;
7267 ewp->w_p_crb = FALSE;
7268
Bram Moolenaar362f3562009-11-03 16:20:34 +00007269 /* Make a copy, because the statusline may include a function call that
7270 * might change the option value and free the memory. */
7271 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007272 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007273 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007274 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007275 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007276 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007278 /* Make all characters printable. */
7279 p = transstr(buf);
7280 if (p != NULL)
7281 {
7282 vim_strncpy(buf, p, sizeof(buf) - 1);
7283 vim_free(p);
7284 }
7285
7286 /* fill up with "fillchar" */
7287 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007288 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
7290#ifdef FEAT_MBYTE
7291 len += (*mb_char2bytes)(fillchar, buf + len);
7292#else
7293 buf[len++] = fillchar;
7294#endif
7295 ++width;
7296 }
7297 buf[len] = NUL;
7298
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007299 /*
7300 * Draw each snippet with the specified highlighting.
7301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 curattr = attr;
7303 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007304 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007306 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 screen_puts_len(p, len, row, col, curattr);
7308 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007309 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007311 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007313 else if (hltab[n].userhl < 0)
7314 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007315#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007316 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7317 && wp->w_status_height != 0)
7318 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007319 else if (wp != NULL && bt_terminal(wp->w_buffer)
7320 && wp->w_status_height != 0)
7321 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007322#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007323 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007324 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007326 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 }
7328 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007329
7330 if (wp == NULL)
7331 {
7332 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7333 col = 0;
7334 len = 0;
7335 p = buf;
7336 fillchar = 0;
7337 for (n = 0; tabtab[n].start != NULL; n++)
7338 {
7339 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7340 while (col < len)
7341 TabPageIdxs[col++] = fillchar;
7342 p = tabtab[n].start;
7343 fillchar = tabtab[n].userhl;
7344 }
7345 while (col < Columns)
7346 TabPageIdxs[col++] = fillchar;
7347 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007348
7349theend:
7350 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351}
7352
7353#endif /* FEAT_STL_OPT */
7354
7355/*
7356 * Output a single character directly to the screen and update ScreenLines.
7357 */
7358 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007359screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 char_u buf[MB_MAXBYTES + 1];
7362
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007363#ifdef FEAT_MBYTE
7364 if (has_mbyte)
7365 buf[(*mb_char2bytes)(c, buf)] = NUL;
7366 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007368 {
7369 buf[0] = c;
7370 buf[1] = NUL;
7371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 screen_puts(buf, row, col, attr);
7373}
7374
7375/*
7376 * Get a single character directly from ScreenLines into "bytes[]".
7377 * Also return its attribute in *attrp;
7378 */
7379 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007380screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
7382 unsigned off;
7383
7384 /* safety check */
7385 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7386 {
7387 off = LineOffset[row] + col;
7388 *attrp = ScreenAttrs[off];
7389 bytes[0] = ScreenLines[off];
7390 bytes[1] = NUL;
7391
7392#ifdef FEAT_MBYTE
7393 if (enc_utf8 && ScreenLinesUC[off] != 0)
7394 bytes[utfc_char2bytes(off, bytes)] = NUL;
7395 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7396 {
7397 bytes[0] = ScreenLines[off];
7398 bytes[1] = ScreenLines2[off];
7399 bytes[2] = NUL;
7400 }
7401 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7402 {
7403 bytes[1] = ScreenLines[off + 1];
7404 bytes[2] = NUL;
7405 }
7406#endif
7407 }
7408}
7409
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007410#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007411/*
7412 * Return TRUE if composing characters for screen posn "off" differs from
7413 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007414 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007415 */
7416 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007417screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007418{
7419 int i;
7420
7421 for (i = 0; i < Screen_mco; ++i)
7422 {
7423 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7424 return TRUE;
7425 if (u8cc[i] == 0)
7426 break;
7427 }
7428 return FALSE;
7429}
7430#endif
7431
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432/*
7433 * Put string '*text' on the screen at position 'row' and 'col', with
7434 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7435 * Note: only outputs within one row, message is truncated at screen boundary!
7436 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7437 */
7438 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007439screen_puts(
7440 char_u *text,
7441 int row,
7442 int col,
7443 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
7445 screen_puts_len(text, -1, row, col, attr);
7446}
7447
7448/*
7449 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7450 * a NUL.
7451 */
7452 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007453screen_puts_len(
7454 char_u *text,
7455 int textlen,
7456 int row,
7457 int col,
7458 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459{
7460 unsigned off;
7461 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007462 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 int c;
7464#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007465 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 int mbyte_blen = 1;
7467 int mbyte_cells = 1;
7468 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007469 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 int clear_next_cell = FALSE;
7471# ifdef FEAT_ARABIC
7472 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007473 int pc, nc, nc1;
7474 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475# endif
7476#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007477#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7478 int force_redraw_this;
7479 int force_redraw_next = FALSE;
7480#endif
7481 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482
7483 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7484 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007485 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486
Bram Moolenaarc236c162008-07-13 17:41:49 +00007487#ifdef FEAT_MBYTE
7488 /* When drawing over the right halve of a double-wide char clear out the
7489 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007490 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007491# ifdef FEAT_GUI
7492 && !gui.in_use
7493# endif
7494 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007495 {
7496 ScreenLines[off - 1] = ' ';
7497 ScreenAttrs[off - 1] = 0;
7498 if (enc_utf8)
7499 {
7500 ScreenLinesUC[off - 1] = 0;
7501 ScreenLinesC[0][off - 1] = 0;
7502 }
7503 /* redraw the previous cell, make it empty */
7504 screen_char(off - 1, row, col - 1);
7505 /* force the cell at "col" to be redrawn */
7506 force_redraw_next = TRUE;
7507 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007508#endif
7509
Bram Moolenaar367329b2007-08-30 11:53:22 +00007510#ifdef FEAT_MBYTE
7511 max_off = LineOffset[row] + screen_Columns;
7512#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007513 while (col < screen_Columns
7514 && (len < 0 || (int)(ptr - text) < len)
7515 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 {
7517 c = *ptr;
7518#ifdef FEAT_MBYTE
7519 /* check if this is the first byte of a multibyte */
7520 if (has_mbyte)
7521 {
7522 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007523 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007525 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7527 mbyte_cells = 1;
7528 else if (enc_dbcs != 0)
7529 mbyte_cells = mbyte_blen;
7530 else /* enc_utf8 */
7531 {
7532 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007533 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 (int)((text + len) - ptr));
7535 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007536 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007538# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 /* Non-BMP character: display as ? or fullwidth ?. */
7540 if (u8c >= 0x10000)
7541 {
7542 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7543 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007544 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547# ifdef FEAT_ARABIC
7548 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7549 {
7550 /* Do Arabic shaping. */
7551 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7552 {
7553 /* Past end of string to be displayed. */
7554 nc = NUL;
7555 nc1 = NUL;
7556 }
7557 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007558 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007559 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7560 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007561 nc1 = pcc[0];
7562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 pc = prev_c;
7564 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007565 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 }
7567 else
7568 prev_c = u8c;
7569# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007570 if (col + mbyte_cells > screen_Columns)
7571 {
7572 /* Only 1 cell left, but character requires 2 cells:
7573 * display a '>' in the last column to avoid wrapping. */
7574 c = '>';
7575 mbyte_cells = 1;
7576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 }
7578 }
7579#endif
7580
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007581#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7582 force_redraw_this = force_redraw_next;
7583 force_redraw_next = FALSE;
7584#endif
7585
7586 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587#ifdef FEAT_MBYTE
7588 || (mbyte_cells == 2
7589 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7590 || (enc_dbcs == DBCS_JPNU
7591 && c == 0x8e
7592 && ScreenLines2[off] != ptr[1])
7593 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007594 && (ScreenLinesUC[off] !=
7595 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7596 || (ScreenLinesUC[off] != 0
7597 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598#endif
7599 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007600 || exmode_active;
7601
7602 if (need_redraw
7603#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7604 || force_redraw_this
7605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 )
7607 {
7608#if defined(FEAT_GUI) || defined(UNIX)
7609 /* The bold trick makes a single row of pixels appear in the next
7610 * character. When a bold character is removed, the next
7611 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007612 * and for some xterms. */
7613 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614# ifdef FEAT_GUI
7615 gui.in_use
7616# endif
7617# if defined(FEAT_GUI) && defined(UNIX)
7618 ||
7619# endif
7620# ifdef UNIX
7621 term_is_xterm
7622# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007623 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007625 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007627 if (n > HL_ALL)
7628 n = syn_attr2attr(n);
7629 if (n & HL_BOLD)
7630 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632#endif
7633#ifdef FEAT_MBYTE
7634 /* When at the end of the text and overwriting a two-cell
7635 * character with a one-cell character, need to clear the next
7636 * cell. Also when overwriting the left halve of a two-cell char
7637 * with the right halve of a two-cell char. Do this only once
7638 * (mb_off2cells() may return 2 on the right halve). */
7639 if (clear_next_cell)
7640 clear_next_cell = FALSE;
7641 else if (has_mbyte
7642 && (len < 0 ? ptr[mbyte_blen] == NUL
7643 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007644 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007646 && (*mb_off2cells)(off, max_off) == 1
7647 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 clear_next_cell = TRUE;
7649
7650 /* Make sure we never leave a second byte of a double-byte behind,
7651 * it confuses mb_off2cells(). */
7652 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007653 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007655 && (*mb_off2cells)(off, max_off) == 1
7656 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 ScreenLines[off + mbyte_blen] = 0;
7658#endif
7659 ScreenLines[off] = c;
7660 ScreenAttrs[off] = attr;
7661#ifdef FEAT_MBYTE
7662 if (enc_utf8)
7663 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007664 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 ScreenLinesUC[off] = 0;
7666 else
7667 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007668 int i;
7669
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007671 for (i = 0; i < Screen_mco; ++i)
7672 {
7673 ScreenLinesC[i][off] = u8cc[i];
7674 if (u8cc[i] == 0)
7675 break;
7676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678 if (mbyte_cells == 2)
7679 {
7680 ScreenLines[off + 1] = 0;
7681 ScreenAttrs[off + 1] = attr;
7682 }
7683 screen_char(off, row, col);
7684 }
7685 else if (mbyte_cells == 2)
7686 {
7687 ScreenLines[off + 1] = ptr[1];
7688 ScreenAttrs[off + 1] = attr;
7689 screen_char_2(off, row, col);
7690 }
7691 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7692 {
7693 ScreenLines2[off] = ptr[1];
7694 screen_char(off, row, col);
7695 }
7696 else
7697#endif
7698 screen_char(off, row, col);
7699 }
7700#ifdef FEAT_MBYTE
7701 if (has_mbyte)
7702 {
7703 off += mbyte_cells;
7704 col += mbyte_cells;
7705 ptr += mbyte_blen;
7706 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007707 {
7708 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007710 len = -1;
7711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
7713 else
7714#endif
7715 {
7716 ++off;
7717 ++col;
7718 ++ptr;
7719 }
7720 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007721
7722#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7723 /* If we detected the next character needs to be redrawn, but the text
7724 * doesn't extend up to there, update the character here. */
7725 if (force_redraw_next && col < screen_Columns)
7726 {
7727# ifdef FEAT_MBYTE
7728 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7729 screen_char_2(off, row, col);
7730 else
7731# endif
7732 screen_char(off, row, col);
7733 }
7734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735}
7736
7737#ifdef FEAT_SEARCH_EXTRA
7738/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007739 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 */
7741 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007742start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
7744 if (p_hls && !no_hlsearch)
7745 {
7746 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007747 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007748# ifdef FEAT_RELTIME
7749 /* Set the time limit to 'redrawtime'. */
7750 profile_setlimit(p_rdt, &search_hl.tm);
7751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 }
7753}
7754
7755/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007756 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 */
7758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007759end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
7761 if (search_hl.rm.regprog != NULL)
7762 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007763 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 search_hl.rm.regprog = NULL;
7765 }
7766}
7767
7768/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007769 * Init for calling prepare_search_hl().
7770 */
7771 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007772init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007773{
7774 matchitem_T *cur;
7775
7776 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7777 * match */
7778 cur = wp->w_match_head;
7779 while (cur != NULL)
7780 {
7781 cur->hl.rm = cur->match;
7782 if (cur->hlg_id == 0)
7783 cur->hl.attr = 0;
7784 else
7785 cur->hl.attr = syn_id2attr(cur->hlg_id);
7786 cur->hl.buf = wp->w_buffer;
7787 cur->hl.lnum = 0;
7788 cur->hl.first_lnum = 0;
7789# ifdef FEAT_RELTIME
7790 /* Set the time limit to 'redrawtime'. */
7791 profile_setlimit(p_rdt, &(cur->hl.tm));
7792# endif
7793 cur = cur->next;
7794 }
7795 search_hl.buf = wp->w_buffer;
7796 search_hl.lnum = 0;
7797 search_hl.first_lnum = 0;
7798 /* time limit is set at the toplevel, for all windows */
7799}
7800
7801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 * Advance to the match in window "wp" line "lnum" or past it.
7803 */
7804 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007805prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007807 matchitem_T *cur; /* points to the match list */
7808 match_T *shl; /* points to search_hl or a match */
7809 int shl_flag; /* flag to indicate whether search_hl
7810 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007811 int pos_inprogress; /* marks that position match search is
7812 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 int n;
7814
7815 /*
7816 * When using a multi-line pattern, start searching at the top
7817 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007818 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007820 cur = wp->w_match_head;
7821 shl_flag = FALSE;
7822 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007824 if (shl_flag == FALSE)
7825 {
7826 shl = &search_hl;
7827 shl_flag = TRUE;
7828 }
7829 else
7830 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 if (shl->rm.regprog != NULL
7832 && shl->lnum == 0
7833 && re_multiline(shl->rm.regprog))
7834 {
7835 if (shl->first_lnum == 0)
7836 {
7837# ifdef FEAT_FOLDING
7838 for (shl->first_lnum = lnum;
7839 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7840 if (hasFoldingWin(wp, shl->first_lnum - 1,
7841 NULL, NULL, TRUE, NULL))
7842 break;
7843# else
7844 shl->first_lnum = wp->w_topline;
7845# endif
7846 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007847 if (cur != NULL)
7848 cur->pos.cur = 0;
7849 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007851 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7852 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007854 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7855 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007856 pos_inprogress = cur == NULL || cur->pos.cur == 0
7857 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 if (shl->lnum != 0)
7859 {
7860 shl->first_lnum = shl->lnum
7861 + shl->rm.endpos[0].lnum
7862 - shl->rm.startpos[0].lnum;
7863 n = shl->rm.endpos[0].col;
7864 }
7865 else
7866 {
7867 ++shl->first_lnum;
7868 n = 0;
7869 }
7870 }
7871 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007872 if (shl != &search_hl && cur != NULL)
7873 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 }
7875}
7876
7877/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007878 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 * Uses shl->buf.
7880 * Sets shl->lnum and shl->rm contents.
7881 * Note: Assumes a previous match is always before "lnum", unless
7882 * shl->lnum is zero.
7883 * Careful: Any pointers for buffer lines will become invalid.
7884 */
7885 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007886next_search_hl(
7887 win_T *win,
7888 match_T *shl, /* points to search_hl or a match */
7889 linenr_T lnum,
7890 colnr_T mincol, /* minimal column for a match */
7891 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
7893 linenr_T l;
7894 colnr_T matchcol;
7895 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007896 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007898 // for :{range}s/pat only highlight inside the range
7899 if (lnum < search_first_line || lnum > search_last_line)
7900 {
7901 shl->lnum = 0;
7902 return;
7903 }
7904
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 if (shl->lnum != 0)
7906 {
7907 /* Check for three situations:
7908 * 1. If the "lnum" is below a previous match, start a new search.
7909 * 2. If the previous match includes "mincol", use it.
7910 * 3. Continue after the previous match.
7911 */
7912 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7913 if (lnum > l)
7914 shl->lnum = 0;
7915 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7916 return;
7917 }
7918
7919 /*
7920 * Repeat searching for a match until one is found that includes "mincol"
7921 * or none is found in this line.
7922 */
7923 called_emsg = FALSE;
7924 for (;;)
7925 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007926#ifdef FEAT_RELTIME
7927 /* Stop searching after passing the time limit. */
7928 if (profile_passed_limit(&(shl->tm)))
7929 {
7930 shl->lnum = 0; /* no match found in time */
7931 break;
7932 }
7933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 /* Three situations:
7935 * 1. No useful previous match: search from start of line.
7936 * 2. Not Vi compatible or empty match: continue at next character.
7937 * Break the loop if this is beyond the end of the line.
7938 * 3. Vi compatible searching: continue at end of previous match.
7939 */
7940 if (shl->lnum == 0)
7941 matchcol = 0;
7942 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7943 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007944 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007946 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007947
7948 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007949 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007950 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007952 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 shl->lnum = 0;
7954 break;
7955 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007956#ifdef FEAT_MBYTE
7957 if (has_mbyte)
7958 matchcol += mb_ptr2len(ml);
7959 else
7960#endif
7961 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
7963 else
7964 matchcol = shl->rm.endpos[0].col;
7965
7966 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007967 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007969 /* Remember whether shl->rm is using a copy of the regprog in
7970 * cur->match. */
7971 int regprog_is_copy = (shl != &search_hl && cur != NULL
7972 && shl == &cur->hl
7973 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007974 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007975
Bram Moolenaarb3414592014-06-17 17:48:32 +02007976 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7977 matchcol,
7978#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007979 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007980#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007981 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982#endif
7983 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007984 /* Copy the regprog, in case it got freed and recompiled. */
7985 if (regprog_is_copy)
7986 cur->match.regprog = cur->hl.rm.regprog;
7987
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007988 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007989 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 /* Error while handling regexp: stop using this regexp. */
7991 if (shl == &search_hl)
7992 {
7993 /* don't free regprog in the match list, it's a copy */
7994 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007995 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007996 }
7997 shl->rm.regprog = NULL;
7998 shl->lnum = 0;
7999 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8000 message */
8001 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008002 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008003 }
8004 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008005 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008006 else
8007 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 if (nmatched == 0)
8009 {
8010 shl->lnum = 0; /* no match found */
8011 break;
8012 }
8013 if (shl->rm.startpos[0].lnum > 0
8014 || shl->rm.startpos[0].col >= mincol
8015 || nmatched > 1
8016 || shl->rm.endpos[0].col > mincol)
8017 {
8018 shl->lnum += shl->rm.startpos[0].lnum;
8019 break; /* useful match found */
8020 }
8021 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008022
8023 // Restore called_emsg for assert_fails().
8024 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026
Bram Moolenaar85077472016-10-16 14:35:48 +02008027/*
8028 * If there is a match fill "shl" and return one.
8029 * Return zero otherwise.
8030 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008031 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008032next_search_hl_pos(
8033 match_T *shl, /* points to a match */
8034 linenr_T lnum,
8035 posmatch_T *posmatch, /* match positions */
8036 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008037{
8038 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008039 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008040
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8042 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008043 llpos_T *pos = &posmatch->pos[i];
8044
8045 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008046 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008047 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008048 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008049 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008050 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008051 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008052 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008053 /* if this match comes before the one at "found" then swap
8054 * them */
8055 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008056 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008057 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008058
Bram Moolenaar85077472016-10-16 14:35:48 +02008059 *pos = posmatch->pos[found];
8060 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008061 }
8062 }
8063 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008064 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008065 }
8066 }
8067 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008068 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008069 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008070 colnr_T start = posmatch->pos[found].col == 0
8071 ? 0 : posmatch->pos[found].col - 1;
8072 colnr_T end = posmatch->pos[found].col == 0
8073 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008074
Bram Moolenaar85077472016-10-16 14:35:48 +02008075 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008076 shl->rm.startpos[0].lnum = 0;
8077 shl->rm.startpos[0].col = start;
8078 shl->rm.endpos[0].lnum = 0;
8079 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008080 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008081 posmatch->cur = found + 1;
8082 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008083 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008084 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008085}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008086#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008087
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008089screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090{
8091 attrentry_T *aep = NULL;
8092
8093 screen_attr = attr;
8094 if (full_screen
8095#ifdef WIN3264
8096 && termcap_active
8097#endif
8098 )
8099 {
8100#ifdef FEAT_GUI
8101 if (gui.in_use)
8102 {
8103 char buf[20];
8104
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008105 /* The GUI handles this internally. */
8106 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 OUT_STR(buf);
8108 }
8109 else
8110#endif
8111 {
8112 if (attr > HL_ALL) /* special HL attr. */
8113 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008114 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 aep = syn_cterm_attr2entry(attr);
8116 else
8117 aep = syn_term_attr2entry(attr);
8118 if (aep == NULL) /* did ":syntax clear" */
8119 attr = 0;
8120 else
8121 attr = aep->ae_attr;
8122 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008123 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008125 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008126#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008127 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8128 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8129 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008130#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008131 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008132 /* If the Normal FG color has BOLD attribute and the new HL
8133 * has a FG color defined, clear BOLD. */
8134 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008135 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008137 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008138 out_str(T_UCS);
8139 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008140 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8141 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008143 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008145 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008147 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008148 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149
8150 /*
8151 * Output the color or start string after bold etc., in case the
8152 * bold etc. override the color setting.
8153 */
8154 if (aep != NULL)
8155 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008156#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008157 /* When 'termguicolors' is set but fg or bg is unset,
8158 * fall back to the cterm colors. This helps for SpellBad,
8159 * where the GUI uses a red undercurl. */
8160 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008162 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008163 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008164 }
8165 else
8166#endif
8167 if (t_colors > 1)
8168 {
8169 if (aep->ae_u.cterm.fg_color)
8170 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8171 }
8172#ifdef FEAT_TERMGUICOLORS
8173 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8174 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008175 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008176 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 }
8178 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008179#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008180 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008182 if (aep->ae_u.cterm.bg_color)
8183 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8184 }
8185
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008186 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008187 {
8188 if (aep->ae_u.term.start != NULL)
8189 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 }
8191 }
8192 }
8193 }
8194}
8195
8196 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008197screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198{
8199 int do_ME = FALSE; /* output T_ME code */
8200
8201 if (screen_attr != 0
8202#ifdef WIN3264
8203 && termcap_active
8204#endif
8205 )
8206 {
8207#ifdef FEAT_GUI
8208 if (gui.in_use)
8209 {
8210 char buf[20];
8211
8212 /* use internal GUI code */
8213 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8214 OUT_STR(buf);
8215 }
8216 else
8217#endif
8218 {
8219 if (screen_attr > HL_ALL) /* special HL attr. */
8220 {
8221 attrentry_T *aep;
8222
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008223 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {
8225 /*
8226 * Assume that t_me restores the original colors!
8227 */
8228 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008229 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008230#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008231 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8232 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8233 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008234#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008235 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008236#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008237 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8238 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8239 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008240#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008241 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 do_ME = TRUE;
8243 }
8244 else
8245 {
8246 aep = syn_term_attr2entry(screen_attr);
8247 if (aep != NULL && aep->ae_u.term.stop != NULL)
8248 {
8249 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8250 do_ME = TRUE;
8251 else
8252 out_str(aep->ae_u.term.stop);
8253 }
8254 }
8255 if (aep == NULL) /* did ":syntax clear" */
8256 screen_attr = 0;
8257 else
8258 screen_attr = aep->ae_attr;
8259 }
8260
8261 /*
8262 * Often all ending-codes are equal to T_ME. Avoid outputting the
8263 * same sequence several times.
8264 */
8265 if (screen_attr & HL_STANDOUT)
8266 {
8267 if (STRCMP(T_SE, T_ME) == 0)
8268 do_ME = TRUE;
8269 else
8270 out_str(T_SE);
8271 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008272 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008273 {
8274 if (STRCMP(T_UCE, T_ME) == 0)
8275 do_ME = TRUE;
8276 else
8277 out_str(T_UCE);
8278 }
8279 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008280 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {
8282 if (STRCMP(T_UE, T_ME) == 0)
8283 do_ME = TRUE;
8284 else
8285 out_str(T_UE);
8286 }
8287 if (screen_attr & HL_ITALIC)
8288 {
8289 if (STRCMP(T_CZR, T_ME) == 0)
8290 do_ME = TRUE;
8291 else
8292 out_str(T_CZR);
8293 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008294 if (screen_attr & HL_STRIKETHROUGH)
8295 {
8296 if (STRCMP(T_STE, T_ME) == 0)
8297 do_ME = TRUE;
8298 else
8299 out_str(T_STE);
8300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8302 out_str(T_ME);
8303
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008304#ifdef FEAT_TERMGUICOLORS
8305 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008307 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008308 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008309 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008310 term_bg_rgb_color(cterm_normal_bg_gui_color);
8311 }
8312 else
8313#endif
8314 {
8315 if (t_colors > 1)
8316 {
8317 /* set Normal cterm colors */
8318 if (cterm_normal_fg_color != 0)
8319 term_fg_color(cterm_normal_fg_color - 1);
8320 if (cterm_normal_bg_color != 0)
8321 term_bg_color(cterm_normal_bg_color - 1);
8322 if (cterm_normal_fg_bold)
8323 out_str(T_MD);
8324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 }
8326 }
8327 }
8328 screen_attr = 0;
8329}
8330
8331/*
8332 * Reset the colors for a cterm. Used when leaving Vim.
8333 * The machine specific code may override this again.
8334 */
8335 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008336reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008338 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {
8340 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008341#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008342 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8343 || cterm_normal_bg_gui_color != INVALCOLOR)
8344 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008345#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {
8349 out_str(T_OP);
8350 screen_attr = -1;
8351 }
8352 if (cterm_normal_fg_bold)
8353 {
8354 out_str(T_ME);
8355 screen_attr = -1;
8356 }
8357 }
8358}
8359
8360/*
8361 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8362 * using the attributes from ScreenAttrs["off"].
8363 */
8364 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008365screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
8367 int attr;
8368
8369 /* Check for illegal values, just in case (could happen just after
8370 * resizing). */
8371 if (row >= screen_Rows || col >= screen_Columns)
8372 return;
8373
Bram Moolenaar494838a2015-02-10 19:20:37 +01008374 /* Outputting a character in the last cell on the screen may scroll the
8375 * screen up. Only do it when the "xn" termcap property is set, otherwise
8376 * mark the character invalid (update it when scrolled up). */
8377 if (*T_XN == NUL
8378 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379#ifdef FEAT_RIGHTLEFT
8380 /* account for first command-line character in rightleft mode */
8381 && !cmdmsg_rl
8382#endif
8383 )
8384 {
8385 ScreenAttrs[off] = (sattr_T)-1;
8386 return;
8387 }
8388
8389 /*
8390 * Stop highlighting first, so it's easier to move the cursor.
8391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 if (screen_char_attr != 0)
8393 attr = screen_char_attr;
8394 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 attr = ScreenAttrs[off];
8396 if (screen_attr != attr)
8397 screen_stop_highlight();
8398
8399 windgoto(row, col);
8400
8401 if (screen_attr != attr)
8402 screen_start_highlight(attr);
8403
8404#ifdef FEAT_MBYTE
8405 if (enc_utf8 && ScreenLinesUC[off] != 0)
8406 {
8407 char_u buf[MB_MAXBYTES + 1];
8408
Bram Moolenaarcb070082016-04-02 22:14:51 +02008409 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008410 {
8411 if (*p_ambw == 'd'
8412# ifdef FEAT_GUI
8413 && !gui.in_use
8414# endif
8415 )
8416 {
8417 /* Clear the two screen cells. If the character is actually
8418 * single width it won't change the second cell. */
8419 out_str((char_u *)" ");
8420 term_windgoto(row, col);
8421 }
8422 /* not sure where the cursor is after drawing the ambiguous width
8423 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008424 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008425 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008426 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008428
8429 /* Convert the UTF-8 character to bytes and write it. */
8430 buf[utfc_char2bytes(off, buf)] = NUL;
8431 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 }
8433 else
8434#endif
8435 {
8436#ifdef FEAT_MBYTE
8437 out_flush_check();
8438#endif
8439 out_char(ScreenLines[off]);
8440#ifdef FEAT_MBYTE
8441 /* double-byte character in single-width cell */
8442 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8443 out_char(ScreenLines2[off]);
8444#endif
8445 }
8446
8447 screen_cur_col++;
8448}
8449
8450#ifdef FEAT_MBYTE
8451
8452/*
8453 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8454 * on the screen at position 'row' and 'col'.
8455 * The attributes of the first byte is used for all. This is required to
8456 * output the two bytes of a double-byte character with nothing in between.
8457 */
8458 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008459screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 /* Check for illegal values (could be wrong when screen was resized). */
8462 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8463 return;
8464
8465 /* Outputting the last character on the screen may scrollup the screen.
8466 * Don't to it! Mark the character invalid (update it when scrolled up) */
8467 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8468 {
8469 ScreenAttrs[off] = (sattr_T)-1;
8470 return;
8471 }
8472
8473 /* Output the first byte normally (positions the cursor), then write the
8474 * second byte directly. */
8475 screen_char(off, row, col);
8476 out_char(ScreenLines[off + 1]);
8477 ++screen_cur_col;
8478}
8479#endif
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481/*
8482 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8483 * This uses the contents of ScreenLines[] and doesn't change it.
8484 */
8485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008486screen_draw_rectangle(
8487 int row,
8488 int col,
8489 int height,
8490 int width,
8491 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 int r, c;
8494 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008495#ifdef FEAT_MBYTE
8496 int max_off;
8497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008499 /* Can't use ScreenLines unless initialized */
8500 if (ScreenLines == NULL)
8501 return;
8502
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 if (invert)
8504 screen_char_attr = HL_INVERSE;
8505 for (r = row; r < row + height; ++r)
8506 {
8507 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008508#ifdef FEAT_MBYTE
8509 max_off = off + screen_Columns;
8510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 for (c = col; c < col + width; ++c)
8512 {
8513#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008514 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {
8516 screen_char_2(off + c, r, c);
8517 ++c;
8518 }
8519 else
8520#endif
8521 {
8522 screen_char(off + c, r, c);
8523#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008524 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 ++c;
8526#endif
8527 }
8528 }
8529 }
8530 screen_char_attr = 0;
8531}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533/*
8534 * Redraw the characters for a vertically split window.
8535 */
8536 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008537redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 int col;
8540 int width;
8541
8542# ifdef FEAT_CLIPBOARD
8543 clip_may_clear_selection(row, end - 1);
8544# endif
8545
8546 if (wp == NULL)
8547 {
8548 col = 0;
8549 width = Columns;
8550 }
8551 else
8552 {
8553 col = wp->w_wincol;
8554 width = wp->w_width;
8555 }
8556 screen_draw_rectangle(row, col, end - row, width, FALSE);
8557}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008559 static void
8560space_to_screenline(int off, int attr)
8561{
8562 ScreenLines[off] = ' ';
8563 ScreenAttrs[off] = attr;
8564# ifdef FEAT_MBYTE
8565 if (enc_utf8)
8566 ScreenLinesUC[off] = 0;
8567# endif
8568}
8569
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570/*
8571 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8572 * with character 'c1' in first column followed by 'c2' in the other columns.
8573 * Use attributes 'attr'.
8574 */
8575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008576screen_fill(
8577 int start_row,
8578 int end_row,
8579 int start_col,
8580 int end_col,
8581 int c1,
8582 int c2,
8583 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
8585 int row;
8586 int col;
8587 int off;
8588 int end_off;
8589 int did_delete;
8590 int c;
8591 int norm_term;
8592#if defined(FEAT_GUI) || defined(UNIX)
8593 int force_next = FALSE;
8594#endif
8595
8596 if (end_row > screen_Rows) /* safety check */
8597 end_row = screen_Rows;
8598 if (end_col > screen_Columns) /* safety check */
8599 end_col = screen_Columns;
8600 if (ScreenLines == NULL
8601 || start_row >= end_row
8602 || start_col >= end_col) /* nothing to do */
8603 return;
8604
8605 /* it's a "normal" terminal when not in a GUI or cterm */
8606 norm_term = (
8607#ifdef FEAT_GUI
8608 !gui.in_use &&
8609#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008610 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 for (row = start_row; row < end_row; ++row)
8612 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008613#ifdef FEAT_MBYTE
8614 if (has_mbyte
8615# ifdef FEAT_GUI
8616 && !gui.in_use
8617# endif
8618 )
8619 {
8620 /* When drawing over the right halve of a double-wide char clear
8621 * out the left halve. When drawing over the left halve of a
8622 * double wide-char clear out the right halve. Only needed in a
8623 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008624 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008625 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008626 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008627 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008628 }
8629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 /*
8631 * Try to use delete-line termcap code, when no attributes or in a
8632 * "normal" terminal, where a bold/italic space is just a
8633 * space.
8634 */
8635 did_delete = FALSE;
8636 if (c2 == ' '
8637 && end_col == Columns
8638 && can_clear(T_CE)
8639 && (attr == 0
8640 || (norm_term
8641 && attr <= HL_ALL
8642 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8643 {
8644 /*
8645 * check if we really need to clear something
8646 */
8647 col = start_col;
8648 if (c1 != ' ') /* don't clear first char */
8649 ++col;
8650
8651 off = LineOffset[row] + col;
8652 end_off = LineOffset[row] + end_col;
8653
8654 /* skip blanks (used often, keep it fast!) */
8655#ifdef FEAT_MBYTE
8656 if (enc_utf8)
8657 while (off < end_off && ScreenLines[off] == ' '
8658 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8659 ++off;
8660 else
8661#endif
8662 while (off < end_off && ScreenLines[off] == ' '
8663 && ScreenAttrs[off] == 0)
8664 ++off;
8665 if (off < end_off) /* something to be cleared */
8666 {
8667 col = off - LineOffset[row];
8668 screen_stop_highlight();
8669 term_windgoto(row, col);/* clear rest of this screen line */
8670 out_str(T_CE);
8671 screen_start(); /* don't know where cursor is now */
8672 col = end_col - col;
8673 while (col--) /* clear chars in ScreenLines */
8674 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008675 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 ++off;
8677 }
8678 }
8679 did_delete = TRUE; /* the chars are cleared now */
8680 }
8681
8682 off = LineOffset[row] + start_col;
8683 c = c1;
8684 for (col = start_col; col < end_col; ++col)
8685 {
8686 if (ScreenLines[off] != c
8687#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008688 || (enc_utf8 && (int)ScreenLinesUC[off]
8689 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690#endif
8691 || ScreenAttrs[off] != attr
8692#if defined(FEAT_GUI) || defined(UNIX)
8693 || force_next
8694#endif
8695 )
8696 {
8697#if defined(FEAT_GUI) || defined(UNIX)
8698 /* The bold trick may make a single row of pixels appear in
8699 * the next character. When a bold character is removed, the
8700 * next character should be redrawn too. This happens for our
8701 * own GUI and for some xterms. */
8702 if (
8703# ifdef FEAT_GUI
8704 gui.in_use
8705# endif
8706# if defined(FEAT_GUI) && defined(UNIX)
8707 ||
8708# endif
8709# ifdef UNIX
8710 term_is_xterm
8711# endif
8712 )
8713 {
8714 if (ScreenLines[off] != ' '
8715 && (ScreenAttrs[off] > HL_ALL
8716 || ScreenAttrs[off] & HL_BOLD))
8717 force_next = TRUE;
8718 else
8719 force_next = FALSE;
8720 }
8721#endif
8722 ScreenLines[off] = c;
8723#ifdef FEAT_MBYTE
8724 if (enc_utf8)
8725 {
8726 if (c >= 0x80)
8727 {
8728 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008729 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 }
8731 else
8732 ScreenLinesUC[off] = 0;
8733 }
8734#endif
8735 ScreenAttrs[off] = attr;
8736 if (!did_delete || c != ' ')
8737 screen_char(off, row, col);
8738 }
8739 ++off;
8740 if (col == start_col)
8741 {
8742 if (did_delete)
8743 break;
8744 c = c2;
8745 }
8746 }
8747 if (end_col == Columns)
8748 LineWraps[row] = FALSE;
8749 if (row == Rows - 1) /* overwritten the command line */
8750 {
8751 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008752 if (start_col == 0 && end_col == Columns
8753 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008755 if (start_col == 0)
8756 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 }
8758 }
8759}
8760
8761/*
8762 * Check if there should be a delay. Used before clearing or redrawing the
8763 * screen or the command line.
8764 */
8765 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008766check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
8768 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8769 && !did_wait_return
8770 && emsg_silent == 0)
8771 {
8772 out_flush();
8773 ui_delay(1000L, TRUE);
8774 emsg_on_display = FALSE;
8775 if (check_msg_scroll)
8776 msg_scroll = FALSE;
8777 }
8778}
8779
8780/*
8781 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008782 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 * Returns TRUE if there is a valid screen to write to.
8784 * Returns FALSE when starting up and screen not initialized yet.
8785 */
8786 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008787screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008789 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 return (ScreenLines != NULL);
8791}
8792
8793/*
8794 * Resize the shell to Rows and Columns.
8795 * Allocate ScreenLines[] and associated items.
8796 *
8797 * There may be some time between setting Rows and Columns and (re)allocating
8798 * ScreenLines[]. This happens when starting up and when (manually) changing
8799 * the shell size. Always use screen_Rows and screen_Columns to access items
8800 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8801 * final size of the shell is needed.
8802 */
8803 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008804screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
8806 int new_row, old_row;
8807#ifdef FEAT_GUI
8808 int old_Rows;
8809#endif
8810 win_T *wp;
8811 int outofmem = FALSE;
8812 int len;
8813 schar_T *new_ScreenLines;
8814#ifdef FEAT_MBYTE
8815 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008816 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008818 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819#endif
8820 sattr_T *new_ScreenAttrs;
8821 unsigned *new_LineOffset;
8822 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008823 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008824 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008826 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008827 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008829retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 /*
8831 * Allocation of the screen buffers is done only when the size changes and
8832 * when Rows and Columns have been set and we have started doing full
8833 * screen stuff.
8834 */
8835 if ((ScreenLines != NULL
8836 && Rows == screen_Rows
8837 && Columns == screen_Columns
8838#ifdef FEAT_MBYTE
8839 && enc_utf8 == (ScreenLinesUC != NULL)
8840 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008841 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842#endif
8843 )
8844 || Rows == 0
8845 || Columns == 0
8846 || (!full_screen && ScreenLines == NULL))
8847 return;
8848
8849 /*
8850 * It's possible that we produce an out-of-memory message below, which
8851 * will cause this function to be called again. To break the loop, just
8852 * return here.
8853 */
8854 if (entered)
8855 return;
8856 entered = TRUE;
8857
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008858 /*
8859 * Note that the window sizes are updated before reallocating the arrays,
8860 * thus we must not redraw here!
8861 */
8862 ++RedrawingDisabled;
8863
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 win_new_shellsize(); /* fit the windows in the new sized shell */
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 comp_col(); /* recompute columns for shown command and ruler */
8867
8868 /*
8869 * We're changing the size of the screen.
8870 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8871 * - Move lines from the old arrays into the new arrays, clear extra
8872 * lines (unless the screen is going to be cleared).
8873 * - Free the old arrays.
8874 *
8875 * If anything fails, make ScreenLines NULL, so we don't do anything!
8876 * Continuing with the old ScreenLines may result in a crash, because the
8877 * size is wrong.
8878 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008879 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008881 if (aucmd_win != NULL)
8882 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
8884 new_ScreenLines = (schar_T *)lalloc((long_u)(
8885 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8886#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008887 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 if (enc_utf8)
8889 {
8890 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8891 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008892 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008893 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8895 }
8896 if (enc_dbcs == DBCS_JPNU)
8897 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8898 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8899#endif
8900 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8901 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8902 new_LineOffset = (unsigned *)lalloc((long_u)(
8903 Rows * sizeof(unsigned)), FALSE);
8904 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008905 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008907 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 {
8909 if (win_alloc_lines(wp) == FAIL)
8910 {
8911 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008912 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
8914 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008915 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8916 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008917 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008918give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008920#ifdef FEAT_MBYTE
8921 for (i = 0; i < p_mco; ++i)
8922 if (new_ScreenLinesC[i] == NULL)
8923 break;
8924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 if (new_ScreenLines == NULL
8926#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008927 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8929#endif
8930 || new_ScreenAttrs == NULL
8931 || new_LineOffset == NULL
8932 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008933 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 || outofmem)
8935 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008936 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008937 {
8938 /* guess the size */
8939 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8940
8941 /* Remember we did this to avoid getting outofmem messages over
8942 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008943 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008944 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008945 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008947 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008948 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008949 VIM_CLEAR(new_ScreenLinesC[i]);
8950 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008952 VIM_CLEAR(new_ScreenAttrs);
8953 VIM_CLEAR(new_LineOffset);
8954 VIM_CLEAR(new_LineWraps);
8955 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 }
8957 else
8958 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008959 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 for (new_row = 0; new_row < Rows; ++new_row)
8962 {
8963 new_LineOffset[new_row] = new_row * Columns;
8964 new_LineWraps[new_row] = FALSE;
8965
8966 /*
8967 * If the screen is not going to be cleared, copy as much as
8968 * possible from the old screen to the new one and clear the rest
8969 * (used when resizing the window at the "--more--" prompt or when
8970 * executing an external command, for the GUI).
8971 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008972 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 {
8974 (void)vim_memset(new_ScreenLines + new_row * Columns,
8975 ' ', (size_t)Columns * sizeof(schar_T));
8976#ifdef FEAT_MBYTE
8977 if (enc_utf8)
8978 {
8979 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8980 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008981 for (i = 0; i < p_mco; ++i)
8982 (void)vim_memset(new_ScreenLinesC[i]
8983 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 0, (size_t)Columns * sizeof(u8char_T));
8985 }
8986 if (enc_dbcs == DBCS_JPNU)
8987 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8988 0, (size_t)Columns * sizeof(schar_T));
8989#endif
8990 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8991 0, (size_t)Columns * sizeof(sattr_T));
8992 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008993 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 {
8995 if (screen_Columns < Columns)
8996 len = screen_Columns;
8997 else
8998 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008999#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009000 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009001 * may be invalid now. Also when p_mco changes. */
9002 if (!(enc_utf8 && ScreenLinesUC == NULL)
9003 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009004#endif
9005 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9006 ScreenLines + LineOffset[old_row],
9007 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 if (enc_utf8 && ScreenLinesUC != NULL
9010 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 {
9012 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9013 ScreenLinesUC + LineOffset[old_row],
9014 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009015 for (i = 0; i < p_mco; ++i)
9016 mch_memmove(new_ScreenLinesC[i]
9017 + new_LineOffset[new_row],
9018 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 (size_t)len * sizeof(u8char_T));
9020 }
9021 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9022 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9023 ScreenLines2 + LineOffset[old_row],
9024 (size_t)len * sizeof(schar_T));
9025#endif
9026 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9027 ScreenAttrs + LineOffset[old_row],
9028 (size_t)len * sizeof(sattr_T));
9029 }
9030 }
9031 }
9032 /* Use the last line of the screen for the current line. */
9033 current_ScreenLine = new_ScreenLines + Rows * Columns;
9034 }
9035
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009036 free_screenlines();
9037
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 ScreenLines = new_ScreenLines;
9039#ifdef FEAT_MBYTE
9040 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009041 for (i = 0; i < p_mco; ++i)
9042 ScreenLinesC[i] = new_ScreenLinesC[i];
9043 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 ScreenLines2 = new_ScreenLines2;
9045#endif
9046 ScreenAttrs = new_ScreenAttrs;
9047 LineOffset = new_LineOffset;
9048 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009049 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
9051 /* It's important that screen_Rows and screen_Columns reflect the actual
9052 * size of ScreenLines[]. Set them before calling anything. */
9053#ifdef FEAT_GUI
9054 old_Rows = screen_Rows;
9055#endif
9056 screen_Rows = Rows;
9057 screen_Columns = Columns;
9058
9059 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009060 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 screenclear2();
9062
9063#ifdef FEAT_GUI
9064 else if (gui.in_use
9065 && !gui.starting
9066 && ScreenLines != NULL
9067 && old_Rows != Rows)
9068 {
9069 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9070 /*
9071 * Adjust the position of the cursor, for when executing an external
9072 * command.
9073 */
9074 if (msg_row >= Rows) /* Rows got smaller */
9075 msg_row = Rows - 1; /* put cursor at last row */
9076 else if (Rows > old_Rows) /* Rows got bigger */
9077 msg_row += Rows - old_Rows; /* put cursor in same place */
9078 if (msg_col >= Columns) /* Columns got smaller */
9079 msg_col = Columns - 1; /* put cursor at last column */
9080 }
9081#endif
9082
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009084 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009085
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009086 /*
9087 * Do not apply autocommands more than 3 times to avoid an endless loop
9088 * in case applying autocommands always changes Rows or Columns.
9089 */
9090 if (starting == 0 && ++retry_count <= 3)
9091 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009092 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009093 /* In rare cases, autocommands may have altered Rows or Columns,
9094 * jump back to check if we need to allocate the screen again. */
9095 goto retry;
9096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097}
9098
9099 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009100free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009101{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009102#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009103 int i;
9104
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009105 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009106 for (i = 0; i < Screen_mco; ++i)
9107 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009108 vim_free(ScreenLines2);
9109#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009110 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009111 vim_free(ScreenAttrs);
9112 vim_free(LineOffset);
9113 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009114 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009115}
9116
9117 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009118screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
9120 check_for_delay(FALSE);
9121 screenalloc(FALSE); /* allocate screen buffers if size changed */
9122 screenclear2(); /* clear the screen */
9123}
9124
9125 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009126screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 int i;
9129
9130 if (starting == NO_SCREEN || ScreenLines == NULL
9131#ifdef FEAT_GUI
9132 || (gui.in_use && gui.starting)
9133#endif
9134 )
9135 return;
9136
9137#ifdef FEAT_GUI
9138 if (!gui.in_use)
9139#endif
9140 screen_attr = -1; /* force setting the Normal colors */
9141 screen_stop_highlight(); /* don't want highlighting here */
9142
9143#ifdef FEAT_CLIPBOARD
9144 /* disable selection without redrawing it */
9145 clip_scroll_selection(9999);
9146#endif
9147
9148 /* blank out ScreenLines */
9149 for (i = 0; i < Rows; ++i)
9150 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009151 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 LineWraps[i] = FALSE;
9153 }
9154
9155 if (can_clear(T_CL))
9156 {
9157 out_str(T_CL); /* clear the display */
9158 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009159 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 }
9161 else
9162 {
9163 /* can't clear the screen, mark all chars with invalid attributes */
9164 for (i = 0; i < Rows; ++i)
9165 lineinvalid(LineOffset[i], (int)Columns);
9166 clear_cmdline = TRUE;
9167 }
9168
9169 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9170
9171 win_rest_invalid(firstwin);
9172 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009173 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 if (must_redraw == CLEAR) /* no need to clear again */
9175 must_redraw = NOT_VALID;
9176 compute_cmdrow();
9177 msg_row = cmdline_row; /* put cursor on last line for messages */
9178 msg_col = 0;
9179 screen_start(); /* don't know where cursor is now */
9180 msg_scrolled = 0; /* can't scroll back */
9181 msg_didany = FALSE;
9182 msg_didout = FALSE;
9183}
9184
9185/*
9186 * Clear one line in ScreenLines.
9187 */
9188 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009189lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190{
9191 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9192#ifdef FEAT_MBYTE
9193 if (enc_utf8)
9194 (void)vim_memset(ScreenLinesUC + off, 0,
9195 (size_t)width * sizeof(u8char_T));
9196#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009197 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198}
9199
9200/*
9201 * Mark one line in ScreenLines invalid by setting the attributes to an
9202 * invalid value.
9203 */
9204 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009205lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206{
9207 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9208}
9209
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210/*
9211 * Copy part of a Screenline for vertically split window "wp".
9212 */
9213 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009214linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215{
9216 unsigned off_to = LineOffset[to] + wp->w_wincol;
9217 unsigned off_from = LineOffset[from] + wp->w_wincol;
9218
9219 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9220 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009221#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 if (enc_utf8)
9223 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009224 int i;
9225
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9227 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009228 for (i = 0; i < p_mco; ++i)
9229 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9230 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 }
9232 if (enc_dbcs == DBCS_JPNU)
9233 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9234 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9237 wp->w_width * sizeof(sattr_T));
9238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
9240/*
9241 * Return TRUE if clearing with term string "p" would work.
9242 * It can't work when the string is empty or it won't set the right background.
9243 */
9244 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009245can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246{
9247 return (*p != NUL && (t_colors <= 1
9248#ifdef FEAT_GUI
9249 || gui.in_use
9250#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009251#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009252 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009253 || (!p_tgc && cterm_normal_bg_color == 0)
9254#else
9255 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009256#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009257 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258}
9259
9260/*
9261 * Reset cursor position. Use whenever cursor was moved because of outputting
9262 * something directly to the screen (shell commands) or a terminal control
9263 * code.
9264 */
9265 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009266screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267{
9268 screen_cur_row = screen_cur_col = 9999;
9269}
9270
9271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 * Move the cursor to position "row","col" in the screen.
9273 * This tries to find the most efficient way to move, minimizing the number of
9274 * characters sent to the terminal.
9275 */
9276 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009277windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009279 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 int i;
9281 int plan;
9282 int cost;
9283 int wouldbe_col;
9284 int noinvcurs;
9285 char_u *bs;
9286 int goto_cost;
9287 int attr;
9288
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009289#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9291
9292#define PLAN_LE 1
9293#define PLAN_CR 2
9294#define PLAN_NL 3
9295#define PLAN_WRITE 4
9296 /* Can't use ScreenLines unless initialized */
9297 if (ScreenLines == NULL)
9298 return;
9299
9300 if (col != screen_cur_col || row != screen_cur_row)
9301 {
9302 /* Check for valid position. */
9303 if (row < 0) /* window without text lines? */
9304 row = 0;
9305 if (row >= screen_Rows)
9306 row = screen_Rows - 1;
9307 if (col >= screen_Columns)
9308 col = screen_Columns - 1;
9309
9310 /* check if no cursor movement is allowed in highlight mode */
9311 if (screen_attr && *T_MS == NUL)
9312 noinvcurs = HIGHL_COST;
9313 else
9314 noinvcurs = 0;
9315 goto_cost = GOTO_COST + noinvcurs;
9316
9317 /*
9318 * Plan how to do the positioning:
9319 * 1. Use CR to move it to column 0, same row.
9320 * 2. Use T_LE to move it a few columns to the left.
9321 * 3. Use NL to move a few lines down, column 0.
9322 * 4. Move a few columns to the right with T_ND or by writing chars.
9323 *
9324 * Don't do this if the cursor went beyond the last column, the cursor
9325 * position is unknown then (some terminals wrap, some don't )
9326 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009327 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 * characters to move the cursor to the right.
9329 */
9330 if (row >= screen_cur_row && screen_cur_col < Columns)
9331 {
9332 /*
9333 * If the cursor is in the same row, bigger col, we can use CR
9334 * or T_LE.
9335 */
9336 bs = NULL; /* init for GCC */
9337 attr = screen_attr;
9338 if (row == screen_cur_row && col < screen_cur_col)
9339 {
9340 /* "le" is preferred over "bc", because "bc" is obsolete */
9341 if (*T_LE)
9342 bs = T_LE; /* "cursor left" */
9343 else
9344 bs = T_BC; /* "backspace character (old) */
9345 if (*bs)
9346 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9347 else
9348 cost = 999;
9349 if (col + 1 < cost) /* using CR is less characters */
9350 {
9351 plan = PLAN_CR;
9352 wouldbe_col = 0;
9353 cost = 1; /* CR is just one character */
9354 }
9355 else
9356 {
9357 plan = PLAN_LE;
9358 wouldbe_col = col;
9359 }
9360 if (noinvcurs) /* will stop highlighting */
9361 {
9362 cost += noinvcurs;
9363 attr = 0;
9364 }
9365 }
9366
9367 /*
9368 * If the cursor is above where we want to be, we can use CR LF.
9369 */
9370 else if (row > screen_cur_row)
9371 {
9372 plan = PLAN_NL;
9373 wouldbe_col = 0;
9374 cost = (row - screen_cur_row) * 2; /* CR LF */
9375 if (noinvcurs) /* will stop highlighting */
9376 {
9377 cost += noinvcurs;
9378 attr = 0;
9379 }
9380 }
9381
9382 /*
9383 * If the cursor is in the same row, smaller col, just use write.
9384 */
9385 else
9386 {
9387 plan = PLAN_WRITE;
9388 wouldbe_col = screen_cur_col;
9389 cost = 0;
9390 }
9391
9392 /*
9393 * Check if any characters that need to be written have the
9394 * correct attributes. Also avoid UTF-8 characters.
9395 */
9396 i = col - wouldbe_col;
9397 if (i > 0)
9398 cost += i;
9399 if (cost < goto_cost && i > 0)
9400 {
9401 /*
9402 * Check if the attributes are correct without additionally
9403 * stopping highlighting.
9404 */
9405 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9406 while (i && *p++ == attr)
9407 --i;
9408 if (i != 0)
9409 {
9410 /*
9411 * Try if it works when highlighting is stopped here.
9412 */
9413 if (*--p == 0)
9414 {
9415 cost += noinvcurs;
9416 while (i && *p++ == 0)
9417 --i;
9418 }
9419 if (i != 0)
9420 cost = 999; /* different attributes, don't do it */
9421 }
9422#ifdef FEAT_MBYTE
9423 if (enc_utf8)
9424 {
9425 /* Don't use an UTF-8 char for positioning, it's slow. */
9426 for (i = wouldbe_col; i < col; ++i)
9427 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9428 {
9429 cost = 999;
9430 break;
9431 }
9432 }
9433#endif
9434 }
9435
9436 /*
9437 * We can do it without term_windgoto()!
9438 */
9439 if (cost < goto_cost)
9440 {
9441 if (plan == PLAN_LE)
9442 {
9443 if (noinvcurs)
9444 screen_stop_highlight();
9445 while (screen_cur_col > col)
9446 {
9447 out_str(bs);
9448 --screen_cur_col;
9449 }
9450 }
9451 else if (plan == PLAN_CR)
9452 {
9453 if (noinvcurs)
9454 screen_stop_highlight();
9455 out_char('\r');
9456 screen_cur_col = 0;
9457 }
9458 else if (plan == PLAN_NL)
9459 {
9460 if (noinvcurs)
9461 screen_stop_highlight();
9462 while (screen_cur_row < row)
9463 {
9464 out_char('\n');
9465 ++screen_cur_row;
9466 }
9467 screen_cur_col = 0;
9468 }
9469
9470 i = col - screen_cur_col;
9471 if (i > 0)
9472 {
9473 /*
9474 * Use cursor-right if it's one character only. Avoids
9475 * removing a line of pixels from the last bold char, when
9476 * using the bold trick in the GUI.
9477 */
9478 if (T_ND[0] != NUL && T_ND[1] == NUL)
9479 {
9480 while (i-- > 0)
9481 out_char(*T_ND);
9482 }
9483 else
9484 {
9485 int off;
9486
9487 off = LineOffset[row] + screen_cur_col;
9488 while (i-- > 0)
9489 {
9490 if (ScreenAttrs[off] != screen_attr)
9491 screen_stop_highlight();
9492#ifdef FEAT_MBYTE
9493 out_flush_check();
9494#endif
9495 out_char(ScreenLines[off]);
9496#ifdef FEAT_MBYTE
9497 if (enc_dbcs == DBCS_JPNU
9498 && ScreenLines[off] == 0x8e)
9499 out_char(ScreenLines2[off]);
9500#endif
9501 ++off;
9502 }
9503 }
9504 }
9505 }
9506 }
9507 else
9508 cost = 999;
9509
9510 if (cost >= goto_cost)
9511 {
9512 if (noinvcurs)
9513 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009514 if (row == screen_cur_row && (col > screen_cur_col)
9515 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 term_cursor_right(col - screen_cur_col);
9517 else
9518 term_windgoto(row, col);
9519 }
9520 screen_cur_row = row;
9521 screen_cur_col = col;
9522 }
9523}
9524
9525/*
9526 * Set cursor to its position in the current window.
9527 */
9528 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009529setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009531 setcursor_mayforce(FALSE);
9532}
9533
9534/*
9535 * Set cursor to its position in the current window.
9536 * When "force" is TRUE also when not redrawing.
9537 */
9538 void
9539setcursor_mayforce(int force)
9540{
9541 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 {
9543 validate_cursor();
9544 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009545 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009547 /* With 'rightleft' set and the cursor on a double-wide
9548 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009549 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009551 (has_mbyte
9552 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9553 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554# endif
9555 1)) :
9556#endif
9557 curwin->w_wcol));
9558 }
9559}
9560
9561
9562/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009563 * Insert 'line_count' lines at 'row' in window 'wp'.
9564 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9565 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 * scrolling.
9567 * Returns FAIL if the lines are not inserted, OK for success.
9568 */
9569 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009570win_ins_lines(
9571 win_T *wp,
9572 int row,
9573 int line_count,
9574 int invalid,
9575 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
9577 int did_delete;
9578 int nextrow;
9579 int lastrow;
9580 int retval;
9581
9582 if (invalid)
9583 wp->w_lines_valid = 0;
9584
9585 if (wp->w_height < 5)
9586 return FAIL;
9587
9588 if (line_count > wp->w_height - row)
9589 line_count = wp->w_height - row;
9590
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009591 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 if (retval != MAYBE)
9593 return retval;
9594
9595 /*
9596 * If there is a next window or a status line, we first try to delete the
9597 * lines at the bottom to avoid messing what is after the window.
9598 * If this fails and there are following windows, don't do anything to avoid
9599 * messing up those windows, better just redraw.
9600 */
9601 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 if (wp->w_next != NULL || wp->w_status_height)
9603 {
9604 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009605 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 did_delete = TRUE;
9607 else if (wp->w_next)
9608 return FAIL;
9609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 /*
9611 * if no lines deleted, blank the lines that will end up below the window
9612 */
9613 if (!did_delete)
9614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009617 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 lastrow = nextrow + line_count;
9619 if (lastrow > Rows)
9620 lastrow = Rows;
9621 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009622 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 ' ', ' ', 0);
9624 }
9625
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009626 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 == FAIL)
9628 {
9629 /* deletion will have messed up other windows */
9630 if (did_delete)
9631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 win_rest_invalid(W_NEXT(wp));
9634 }
9635 return FAIL;
9636 }
9637
9638 return OK;
9639}
9640
9641/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009642 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9644 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9645 * scrolling
9646 * Return OK for success, FAIL if the lines are not deleted.
9647 */
9648 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009649win_del_lines(
9650 win_T *wp,
9651 int row,
9652 int line_count,
9653 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009654 int mayclear,
9655 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 int retval;
9658
9659 if (invalid)
9660 wp->w_lines_valid = 0;
9661
9662 if (line_count > wp->w_height - row)
9663 line_count = wp->w_height - row;
9664
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009665 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 if (retval != MAYBE)
9667 return retval;
9668
9669 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009670 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 return FAIL;
9672
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 /*
9674 * If there are windows or status lines below, try to put them at the
9675 * correct place. If we can't do that, they have to be redrawn.
9676 */
9677 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9678 {
9679 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009680 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 {
9682 wp->w_redr_status = TRUE;
9683 win_rest_invalid(wp->w_next);
9684 }
9685 }
9686 /*
9687 * If this is the last window and there is no status line, redraw the
9688 * command line later.
9689 */
9690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 redraw_cmdline = TRUE;
9692 return OK;
9693}
9694
9695/*
9696 * Common code for win_ins_lines() and win_del_lines().
9697 * Returns OK or FAIL when the work has been done.
9698 * Returns MAYBE when not finished yet.
9699 */
9700 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009701win_do_lines(
9702 win_T *wp,
9703 int row,
9704 int line_count,
9705 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009706 int del,
9707 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
9709 int retval;
9710
9711 if (!redrawing() || line_count <= 0)
9712 return FAIL;
9713
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009714 /* When inserting lines would result in loss of command output, just redraw
9715 * the lines. */
9716 if (no_win_do_lines_ins && !del)
9717 return FAIL;
9718
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009720 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009722 if (!no_win_do_lines_ins)
9723 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 return FAIL;
9725 }
9726
9727 /*
9728 * Delete all remaining lines
9729 */
9730 if (row + line_count >= wp->w_height)
9731 {
9732 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009733 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 ' ', ' ', 0);
9735 return OK;
9736 }
9737
9738 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009739 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009741 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009743 if (!no_win_do_lines_ins)
9744 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745
9746 /*
9747 * If the terminal can set a scroll region, use that.
9748 * Always do this in a vertically split window. This will redraw from
9749 * ScreenLines[] when t_CV isn't defined. That's faster than using
9750 * win_line().
9751 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009752 * a character in the lower right corner of the scroll region may cause a
9753 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009755 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 scroll_region_set(wp, row);
9759 if (del)
9760 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009761 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 else
9763 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009764 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 scroll_region_reset();
9767 return retval;
9768 }
9769
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9771 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772
9773 return MAYBE;
9774}
9775
9776/*
9777 * window 'wp' and everything after it is messed up, mark it for redraw
9778 */
9779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009780win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 {
9784 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 wp->w_redr_status = TRUE;
9786 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 }
9788 redraw_cmdline = TRUE;
9789}
9790
9791/*
9792 * The rest of the routines in this file perform screen manipulations. The
9793 * given operation is performed physically on the screen. The corresponding
9794 * change is also made to the internal screen image. In this way, the editor
9795 * anticipates the effect of editing changes on the appearance of the screen.
9796 * That way, when we call screenupdate a complete redraw isn't usually
9797 * necessary. Another advantage is that we can keep adding code to anticipate
9798 * screen changes, and in the meantime, everything still works.
9799 */
9800
9801/*
9802 * types for inserting or deleting lines
9803 */
9804#define USE_T_CAL 1
9805#define USE_T_CDL 2
9806#define USE_T_AL 3
9807#define USE_T_CE 4
9808#define USE_T_DL 5
9809#define USE_T_SR 6
9810#define USE_NL 7
9811#define USE_T_CD 8
9812#define USE_REDRAW 9
9813
9814/*
9815 * insert lines on the screen and update ScreenLines[]
9816 * 'end' is the line after the scrolled part. Normally it is Rows.
9817 * When scrolling region used 'off' is the offset from the top for the region.
9818 * 'row' and 'end' are relative to the start of the region.
9819 *
9820 * return FAIL for failure, OK for success.
9821 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009822 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009823screen_ins_lines(
9824 int off,
9825 int row,
9826 int line_count,
9827 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009828 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009829 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
9831 int i;
9832 int j;
9833 unsigned temp;
9834 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009835 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 int type;
9837 int result_empty;
9838 int can_ce = can_clear(T_CE);
9839
9840 /*
9841 * FAIL if
9842 * - there is no valid screen
9843 * - the screen has to be redrawn completely
9844 * - the line count is less than one
9845 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009846 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009848 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9849#ifdef FEAT_CLIPBOARD
9850 || (clip_star.state != SELECT_CLEARED
9851 && redrawing_for_callback > 0)
9852#endif
9853 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 return FAIL;
9855
9856 /*
9857 * There are seven ways to insert lines:
9858 * 0. When in a vertically split window and t_CV isn't set, redraw the
9859 * characters from ScreenLines[].
9860 * 1. Use T_CD (clear to end of display) if it exists and the result of
9861 * the insert is just empty lines
9862 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9863 * present or line_count > 1. It looks better if we do all the inserts
9864 * at once.
9865 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9866 * insert is just empty lines and T_CE is not present or line_count >
9867 * 1.
9868 * 4. Use T_AL (insert line) if it exists.
9869 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9870 * just empty lines.
9871 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9872 * just empty lines.
9873 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9874 * the 'da' flag is not set or we have clear line capability.
9875 * 8. redraw the characters from ScreenLines[].
9876 *
9877 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9878 * the scrollbar for the window. It does have insert line, use that if it
9879 * exists.
9880 */
9881 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9883 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009884 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 type = USE_T_CD;
9886 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9887 type = USE_T_CAL;
9888 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9889 type = USE_T_CDL;
9890 else if (*T_AL != NUL)
9891 type = USE_T_AL;
9892 else if (can_ce && result_empty)
9893 type = USE_T_CE;
9894 else if (*T_DL != NUL && result_empty)
9895 type = USE_T_DL;
9896 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9897 type = USE_T_SR;
9898 else
9899 return FAIL;
9900
9901 /*
9902 * For clearing the lines screen_del_lines() is used. This will also take
9903 * care of t_db if necessary.
9904 */
9905 if (type == USE_T_CD || type == USE_T_CDL ||
9906 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009907 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908
9909 /*
9910 * If text is retained below the screen, first clear or delete as many
9911 * lines at the bottom of the window as are about to be inserted so that
9912 * the deleted lines won't later surface during a screen_del_lines.
9913 */
9914 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009915 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916
9917#ifdef FEAT_CLIPBOARD
9918 /* Remove a modeless selection when inserting lines halfway the screen
9919 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009920 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009921 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 else
9923 clip_scroll_selection(-line_count);
9924#endif
9925
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926#ifdef FEAT_GUI
9927 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9928 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009929 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930#endif
9931
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009932 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9933 cursor_col = wp->w_wincol;
9934
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 if (*T_CCS != NUL) /* cursor relative to region */
9936 cursor_row = row;
9937 else
9938 cursor_row = row + off;
9939
9940 /*
9941 * Shift LineOffset[] line_count down to reflect the inserted lines.
9942 * Clear the inserted lines in ScreenLines[].
9943 */
9944 row += off;
9945 end += off;
9946 for (i = 0; i < line_count; ++i)
9947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 if (wp != NULL && wp->w_width != Columns)
9949 {
9950 /* need to copy part of a line */
9951 j = end - 1 - i;
9952 while ((j -= line_count) >= row)
9953 linecopy(j + line_count, j, wp);
9954 j += line_count;
9955 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009956 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9957 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 else
9959 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9960 LineWraps[j] = FALSE;
9961 }
9962 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 {
9964 j = end - 1 - i;
9965 temp = LineOffset[j];
9966 while ((j -= line_count) >= row)
9967 {
9968 LineOffset[j + line_count] = LineOffset[j];
9969 LineWraps[j + line_count] = LineWraps[j];
9970 }
9971 LineOffset[j + line_count] = temp;
9972 LineWraps[j + line_count] = FALSE;
9973 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009974 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 else
9976 lineinvalid(temp, (int)Columns);
9977 }
9978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979
9980 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009981 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009982 if (clear_attr != 0)
9983 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 /* redraw the characters */
9986 if (type == USE_REDRAW)
9987 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009988 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 {
9990 term_append_lines(line_count);
9991 screen_start(); /* don't know where cursor is now */
9992 }
9993 else
9994 {
9995 for (i = 0; i < line_count; i++)
9996 {
9997 if (type == USE_T_AL)
9998 {
9999 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010000 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 out_str(T_AL);
10002 }
10003 else /* type == USE_T_SR */
10004 out_str(T_SR);
10005 screen_start(); /* don't know where cursor is now */
10006 }
10007 }
10008
10009 /*
10010 * With scroll-reverse and 'da' flag set we need to clear the lines that
10011 * have been scrolled down into the region.
10012 */
10013 if (type == USE_T_SR && *T_DA)
10014 {
10015 for (i = 0; i < line_count; ++i)
10016 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010017 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 out_str(T_CE);
10019 screen_start(); /* don't know where cursor is now */
10020 }
10021 }
10022
10023#ifdef FEAT_GUI
10024 gui_can_update_cursor();
10025 if (gui.in_use)
10026 out_flush(); /* always flush after a scroll */
10027#endif
10028 return OK;
10029}
10030
10031/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010032 * Delete lines on the screen and update ScreenLines[].
10033 * "end" is the line after the scrolled part. Normally it is Rows.
10034 * When scrolling region used "off" is the offset from the top for the region.
10035 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 *
10037 * Return OK for success, FAIL if the lines are not deleted.
10038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010040screen_del_lines(
10041 int off,
10042 int row,
10043 int line_count,
10044 int end,
10045 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010046 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010047 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048{
10049 int j;
10050 int i;
10051 unsigned temp;
10052 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010053 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 int cursor_end;
10055 int result_empty; /* result is empty until end of region */
10056 int can_delete; /* deleting line codes can be used */
10057 int type;
10058
10059 /*
10060 * FAIL if
10061 * - there is no valid screen
10062 * - the screen has to be redrawn completely
10063 * - the line count is less than one
10064 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010065 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010067 if (!screen_valid(TRUE) || line_count <= 0
10068 || (!force && line_count > p_ttyscroll)
10069#ifdef FEAT_CLIPBOARD
10070 || (clip_star.state != SELECT_CLEARED
10071 && redrawing_for_callback > 0)
10072#endif
10073 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 return FAIL;
10075
10076 /*
10077 * Check if the rest of the current region will become empty.
10078 */
10079 result_empty = row + line_count >= end;
10080
10081 /*
10082 * We can delete lines only when 'db' flag not set or when 'ce' option
10083 * available.
10084 */
10085 can_delete = (*T_DB == NUL || can_clear(T_CE));
10086
10087 /*
10088 * There are six ways to delete lines:
10089 * 0. When in a vertically split window and t_CV isn't set, redraw the
10090 * characters from ScreenLines[].
10091 * 1. Use T_CD if it exists and the result is empty.
10092 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10093 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10094 * none of the other ways work.
10095 * 4. Use T_CE (erase line) if the result is empty.
10096 * 5. Use T_DL (delete line) if it exists.
10097 * 6. redraw the characters from ScreenLines[].
10098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10100 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010101 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 type = USE_T_CD;
10103#if defined(__BEOS__) && defined(BEOS_DR8)
10104 /*
10105 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10106 * its internal termcap... this works okay for tests which test *T_DB !=
10107 * NUL. It has the disadvantage that the user cannot use any :set t_*
10108 * command to get T_DB (back) to empty_option, only :set term=... will do
10109 * the trick...
10110 * Anyway, this hack will hopefully go away with the next OS release.
10111 * (Olaf Seibert)
10112 */
10113 else if (row == 0 && T_DB == empty_option
10114 && (line_count == 1 || *T_CDL == NUL))
10115#else
10116 else if (row == 0 && (
10117#ifndef AMIGA
10118 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10119 * up, so use delete-line command */
10120 line_count == 1 ||
10121#endif
10122 *T_CDL == NUL))
10123#endif
10124 type = USE_NL;
10125 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10126 type = USE_T_CDL;
10127 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010128 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 type = USE_T_CE;
10130 else if (*T_DL != NUL && can_delete)
10131 type = USE_T_DL;
10132 else if (*T_CDL != NUL && can_delete)
10133 type = USE_T_CDL;
10134 else
10135 return FAIL;
10136
10137#ifdef FEAT_CLIPBOARD
10138 /* Remove a modeless selection when deleting lines halfway the screen or
10139 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010140 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010141 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 else
10143 clip_scroll_selection(line_count);
10144#endif
10145
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146#ifdef FEAT_GUI
10147 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10148 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010149 gui_dont_update_cursor(gui.cursor_row >= row + off
10150 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#endif
10152
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010153 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10154 cursor_col = wp->w_wincol;
10155
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 if (*T_CCS != NUL) /* cursor relative to region */
10157 {
10158 cursor_row = row;
10159 cursor_end = end;
10160 }
10161 else
10162 {
10163 cursor_row = row + off;
10164 cursor_end = end + off;
10165 }
10166
10167 /*
10168 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10169 * Clear the inserted lines in ScreenLines[].
10170 */
10171 row += off;
10172 end += off;
10173 for (i = 0; i < line_count; ++i)
10174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 if (wp != NULL && wp->w_width != Columns)
10176 {
10177 /* need to copy part of a line */
10178 j = row + i;
10179 while ((j += line_count) <= end - 1)
10180 linecopy(j - line_count, j, wp);
10181 j -= line_count;
10182 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010183 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10184 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 else
10186 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10187 LineWraps[j] = FALSE;
10188 }
10189 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 {
10191 /* whole width, moving the line pointers is faster */
10192 j = row + i;
10193 temp = LineOffset[j];
10194 while ((j += line_count) <= end - 1)
10195 {
10196 LineOffset[j - line_count] = LineOffset[j];
10197 LineWraps[j - line_count] = LineWraps[j];
10198 }
10199 LineOffset[j - line_count] = temp;
10200 LineWraps[j - line_count] = FALSE;
10201 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010202 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 else
10204 lineinvalid(temp, (int)Columns);
10205 }
10206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010208 if (screen_attr != clear_attr)
10209 screen_stop_highlight();
10210 if (clear_attr != 0)
10211 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 /* redraw the characters */
10214 if (type == USE_REDRAW)
10215 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010216 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010218 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 out_str(T_CD);
10220 screen_start(); /* don't know where cursor is now */
10221 }
10222 else if (type == USE_T_CDL)
10223 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010224 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 term_delete_lines(line_count);
10226 screen_start(); /* don't know where cursor is now */
10227 }
10228 /*
10229 * Deleting lines at top of the screen or scroll region: Just scroll
10230 * the whole screen (scroll region) up by outputting newlines on the
10231 * last line.
10232 */
10233 else if (type == USE_NL)
10234 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010235 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 for (i = line_count; --i >= 0; )
10237 out_char('\n'); /* cursor will remain on same line */
10238 }
10239 else
10240 {
10241 for (i = line_count; --i >= 0; )
10242 {
10243 if (type == USE_T_DL)
10244 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010245 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 out_str(T_DL); /* delete a line */
10247 }
10248 else /* type == USE_T_CE */
10249 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010250 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 out_str(T_CE); /* erase a line */
10252 }
10253 screen_start(); /* don't know where cursor is now */
10254 }
10255 }
10256
10257 /*
10258 * If the 'db' flag is set, we need to clear the lines that have been
10259 * scrolled up at the bottom of the region.
10260 */
10261 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10262 {
10263 for (i = line_count; i > 0; --i)
10264 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010265 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 out_str(T_CE); /* erase a line */
10267 screen_start(); /* don't know where cursor is now */
10268 }
10269 }
10270
10271#ifdef FEAT_GUI
10272 gui_can_update_cursor();
10273 if (gui.in_use)
10274 out_flush(); /* always flush after a scroll */
10275#endif
10276
10277 return OK;
10278}
10279
10280/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010281 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 *
10283 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10284 * If clear_cmdline is FALSE there may be a message there that needs to be
10285 * cleared only if a mode is shown.
10286 * Return the length of the message (0 if no message).
10287 */
10288 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010289showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290{
10291 int need_clear;
10292 int length = 0;
10293 int do_mode;
10294 int attr;
10295 int nwr_save;
10296#ifdef FEAT_INS_EXPAND
10297 int sub_attr;
10298#endif
10299
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010300 do_mode = ((p_smd && msg_silent == 0)
10301 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010302 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010303 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010304 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 {
10306 /*
10307 * Don't show mode right now, when not redrawing or inside a mapping.
10308 * Call char_avail() only when we are going to show something, because
10309 * it takes a bit of time.
10310 */
10311 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10312 {
10313 redraw_cmdline = TRUE; /* show mode later */
10314 return 0;
10315 }
10316
10317 nwr_save = need_wait_return;
10318
10319 /* wait a bit before overwriting an important message */
10320 check_for_delay(FALSE);
10321
10322 /* if the cmdline is more than one line high, erase top lines */
10323 need_clear = clear_cmdline;
10324 if (clear_cmdline && cmdline_row < Rows - 1)
10325 msg_clr_cmdline(); /* will reset clear_cmdline */
10326
10327 /* Position on the last line in the window, column 0 */
10328 msg_pos_mode();
10329 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010330 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 if (do_mode)
10332 {
10333 MSG_PUTS_ATTR("--", attr);
10334#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010335 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010336# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010337 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010338# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010339 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010340# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010341 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010342# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 MSG_PUTS_ATTR(" IM", attr);
10344# else
10345 MSG_PUTS_ATTR(" XIM", attr);
10346# endif
10347#endif
10348#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10349 if (gui.in_use)
10350 {
10351 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010352 {
10353 /* HANGUL */
10354 if (enc_utf8)
10355 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10356 else
10357 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 }
10360#endif
10361#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010362 /* CTRL-X in Insert mode */
10363 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 {
10365 /* These messages can get long, avoid a wrap in a narrow
10366 * window. Prefer showing edit_submode_extra. */
10367 length = (Rows - msg_row) * Columns - 3;
10368 if (edit_submode_extra != NULL)
10369 length -= vim_strsize(edit_submode_extra);
10370 if (length > 0)
10371 {
10372 if (edit_submode_pre != NULL)
10373 length -= vim_strsize(edit_submode_pre);
10374 if (length - vim_strsize(edit_submode) > 0)
10375 {
10376 if (edit_submode_pre != NULL)
10377 msg_puts_attr(edit_submode_pre, attr);
10378 msg_puts_attr(edit_submode, attr);
10379 }
10380 if (edit_submode_extra != NULL)
10381 {
10382 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10383 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010384 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 else
10386 sub_attr = attr;
10387 msg_puts_attr(edit_submode_extra, sub_attr);
10388 }
10389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 }
10391 else
10392#endif
10393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 if (State & VREPLACE_FLAG)
10395 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010396 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10398 else if (State & INSERT)
10399 {
10400#ifdef FEAT_RIGHTLEFT
10401 if (p_ri)
10402 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10403#endif
10404 MSG_PUTS_ATTR(_(" INSERT"), attr);
10405 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010406 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 MSG_PUTS_ATTR(_(" (insert)"), attr);
10408 else if (restart_edit == 'R')
10409 MSG_PUTS_ATTR(_(" (replace)"), attr);
10410 else if (restart_edit == 'V')
10411 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10412#ifdef FEAT_RIGHTLEFT
10413 if (p_hkmap)
10414 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10415# ifdef FEAT_FKMAP
10416 if (p_fkmap)
10417 MSG_PUTS_ATTR(farsi_text_5, attr);
10418# endif
10419#endif
10420#ifdef FEAT_KEYMAP
10421 if (State & LANGMAP)
10422 {
10423# ifdef FEAT_ARABIC
10424 if (curwin->w_p_arab)
10425 MSG_PUTS_ATTR(_(" Arabic"), attr);
10426 else
10427# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010428 if (get_keymap_str(curwin, (char_u *)" (%s)",
10429 NameBuff, MAXPATHL))
10430 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 }
10432#endif
10433 if ((State & INSERT) && p_paste)
10434 MSG_PUTS_ATTR(_(" (paste)"), attr);
10435
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 if (VIsual_active)
10437 {
10438 char *p;
10439
10440 /* Don't concatenate separate words to avoid translation
10441 * problems. */
10442 switch ((VIsual_select ? 4 : 0)
10443 + (VIsual_mode == Ctrl_V) * 2
10444 + (VIsual_mode == 'V'))
10445 {
10446 case 0: p = N_(" VISUAL"); break;
10447 case 1: p = N_(" VISUAL LINE"); break;
10448 case 2: p = N_(" VISUAL BLOCK"); break;
10449 case 4: p = N_(" SELECT"); break;
10450 case 5: p = N_(" SELECT LINE"); break;
10451 default: p = N_(" SELECT BLOCK"); break;
10452 }
10453 MSG_PUTS_ATTR(_(p), attr);
10454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 MSG_PUTS_ATTR(" --", attr);
10456 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010457
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 need_clear = TRUE;
10459 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010460 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461#ifdef FEAT_INS_EXPAND
10462 && edit_submode == NULL /* otherwise it gets too long */
10463#endif
10464 )
10465 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010466 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 need_clear = TRUE;
10468 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010469
10470 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 if (need_clear || clear_cmdline)
10472 msg_clr_eos();
10473 msg_didout = FALSE; /* overwrite this message */
10474 length = msg_col;
10475 msg_col = 0;
10476 need_wait_return = nwr_save; /* never ask for hit-return for this */
10477 }
10478 else if (clear_cmdline && msg_silent == 0)
10479 /* Clear the whole command line. Will reset "clear_cmdline". */
10480 msg_clr_cmdline();
10481
10482#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 /* In Visual mode the size of the selected area must be redrawn. */
10484 if (VIsual_active)
10485 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486
10487 /* If the last window has no status line, the ruler is after the mode
10488 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010489 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010490 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491#endif
10492 redraw_cmdline = FALSE;
10493 clear_cmdline = FALSE;
10494
10495 return length;
10496}
10497
10498/*
10499 * Position for a mode message.
10500 */
10501 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010502msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504 msg_col = 0;
10505 msg_row = Rows - 1;
10506}
10507
10508/*
10509 * Delete mode message. Used when ESC is typed which is expected to end
10510 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010511 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 */
10513 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010514unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515{
10516 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010517 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 */
10519 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10520 redraw_cmdline = TRUE; /* delete mode later */
10521 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010522 clearmode();
10523}
10524
10525/*
10526 * Clear the mode message.
10527 */
10528 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010529clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010530{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010531 int save_msg_row = msg_row;
10532 int save_msg_col = msg_col;
10533
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010534 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010535 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010536 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010537 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010538
10539 msg_col = save_msg_col;
10540 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541}
10542
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010543 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010544recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010545{
10546 MSG_PUTS_ATTR(_("recording"), attr);
10547 if (!shortmess(SHM_RECORDING))
10548 {
10549 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010550 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010551 MSG_PUTS_ATTR(s, attr);
10552 }
10553}
10554
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010555/*
10556 * Draw the tab pages line at the top of the Vim window.
10557 */
10558 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010559draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010560{
10561 int tabcount = 0;
10562 tabpage_T *tp;
10563 int tabwidth;
10564 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010565 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010566 int attr;
10567 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010568 win_T *cwp;
10569 int wincount;
10570 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010571 int c;
10572 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010573 int attr_sel = HL_ATTR(HLF_TPS);
10574 int attr_nosel = HL_ATTR(HLF_TP);
10575 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010576 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010577 int room;
10578 int use_sep_chars = (t_colors < 8
10579#ifdef FEAT_GUI
10580 && !gui.in_use
10581#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010582#ifdef FEAT_TERMGUICOLORS
10583 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010584#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010585 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010586
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010587 if (ScreenLines == NULL)
10588 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010589 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010590
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010591#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010592 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010593 if (gui_use_tabline())
10594 {
10595 gui_update_tabline();
10596 return;
10597 }
10598#endif
10599
10600 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010601 return;
10602
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010603#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010604
10605 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10606 for (scol = 0; scol < Columns; ++scol)
10607 TabPageIdxs[scol] = 0;
10608
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010609 /* Use the 'tabline' option if it's set. */
10610 if (*p_tal != NUL)
10611 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010612 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613
10614 /* Check for an error. If there is one we would loop in redrawing the
10615 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010616 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010617 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010618 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010619 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010620 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010621 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010622 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010624#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010625 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010626 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010628
Bram Moolenaar238a5642006-02-21 22:12:05 +000010629 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10630 if (tabwidth < 6)
10631 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010632
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 attr = attr_nosel;
10634 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010635 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010636 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10637 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010638 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010640
Bram Moolenaar238a5642006-02-21 22:12:05 +000010641 if (tp->tp_topframe == topframe)
10642 attr = attr_sel;
10643 if (use_sep_chars && col > 0)
10644 screen_putchar('|', 0, col++, attr);
10645
10646 if (tp->tp_topframe != topframe)
10647 attr = attr_nosel;
10648
10649 screen_putchar(' ', 0, col++, attr);
10650
10651 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010652 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010653 cwp = curwin;
10654 wp = firstwin;
10655 }
10656 else
10657 {
10658 cwp = tp->tp_curwin;
10659 wp = tp->tp_firstwin;
10660 }
10661
10662 modified = FALSE;
10663 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10664 if (bufIsChanged(wp->w_buffer))
10665 modified = TRUE;
10666 if (modified || wincount > 1)
10667 {
10668 if (wincount > 1)
10669 {
10670 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010671 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010672 if (col + len >= Columns - 3)
10673 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010674 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010675#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010676 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010677#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010678 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010679#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010680 );
10681 col += len;
10682 }
10683 if (modified)
10684 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10685 screen_putchar(' ', 0, col++, attr);
10686 }
10687
10688 room = scol - col + tabwidth - 1;
10689 if (room > 0)
10690 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010691 /* Get buffer name in NameBuff[] */
10692 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010693 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694 len = vim_strsize(NameBuff);
10695 p = NameBuff;
10696#ifdef FEAT_MBYTE
10697 if (has_mbyte)
10698 while (len > room)
10699 {
10700 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010701 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010702 }
10703 else
10704#endif
10705 if (len > room)
10706 {
10707 p += len - room;
10708 len = room;
10709 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010710 if (len > Columns - col - 1)
10711 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010712
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010713 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010714 col += len;
10715 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010716 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010717
10718 /* Store the tab page number in TabPageIdxs[], so that
10719 * jump_to_mouse() knows where each one is. */
10720 ++tabcount;
10721 while (scol < col)
10722 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010723 }
10724
Bram Moolenaar238a5642006-02-21 22:12:05 +000010725 if (use_sep_chars)
10726 c = '_';
10727 else
10728 c = ' ';
10729 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010730
10731 /* Put an "X" for closing the current tab if there are several. */
10732 if (first_tabpage->tp_next != NULL)
10733 {
10734 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10735 TabPageIdxs[Columns - 1] = -999;
10736 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010737 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010738
10739 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10740 * set. */
10741 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010742}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010743
10744/*
10745 * Get buffer name for "buf" into NameBuff[].
10746 * Takes care of special buffer names and translates special characters.
10747 */
10748 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010749get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010750{
10751 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010752 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010753 else
10754 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10755 trans_characters(NameBuff, MAXPATHL);
10756}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010757
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758/*
10759 * Get the character to use in a status line. Get its attributes in "*attr".
10760 */
10761 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010762fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763{
10764 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010765
10766#ifdef FEAT_TERMINAL
10767 if (bt_terminal(wp->w_buffer))
10768 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010769 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010770 {
10771 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010772 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010773 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010774 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010775 {
10776 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010777 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010778 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010779 }
10780 else
10781#endif
10782 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010784 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 fill = fill_stl;
10786 }
10787 else
10788 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010789 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 fill = fill_stlnc;
10791 }
10792 /* Use fill when there is highlighting, and highlighting of current
10793 * window differs, or the fillchars differ, or this is not the
10794 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010795 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010796 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 || (fill_stl != fill_stlnc)))
10798 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010799 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 return '^';
10801 return '=';
10802}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804/*
10805 * Get the character to use in a separator between vertically split windows.
10806 * Get its attributes in "*attr".
10807 */
10808 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010809fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010811 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 if (*attr == 0 && fill_vert == ' ')
10813 return '|';
10814 else
10815 return fill_vert;
10816}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817
10818/*
10819 * Return TRUE if redrawing should currently be done.
10820 */
10821 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010822redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010824#ifdef FEAT_EVAL
10825 if (disable_redraw_for_testing)
10826 return 0;
10827 else
10828#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010829 return ((!RedrawingDisabled
10830#ifdef FEAT_EVAL
10831 || ignore_redraw_flag_for_testing
10832#endif
10833 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834}
10835
10836/*
10837 * Return TRUE if printing messages should currently be done.
10838 */
10839 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010840messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841{
10842 return (!(p_lz && char_avail() && !KeyTyped));
10843}
10844
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010845#ifdef FEAT_MENU
10846/*
10847 * Draw the window toolbar.
10848 */
10849 static void
10850redraw_win_toolbar(win_T *wp)
10851{
10852 vimmenu_T *menu;
10853 int item_idx = 0;
10854 int item_count = 0;
10855 int col = 0;
10856 int next_col;
10857 int off = (int)(current_ScreenLine - ScreenLines);
10858 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10859 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10860
10861 vim_free(wp->w_winbar_items);
10862 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10863 ++item_count;
10864 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10865 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10866
10867 /* TODO: use fewer spaces if there is not enough room */
10868 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010869 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010870 {
10871 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010872 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010873 break;
10874 if (col > 1)
10875 {
10876 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010877 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010878 break;
10879 }
10880
10881 wp->w_winbar_items[item_idx].wb_startcol = col;
10882 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010883 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010884 break;
10885
10886 next_col = text_to_screenline(wp, menu->name, col);
10887 while (col < next_col)
10888 {
10889 ScreenAttrs[off + col] = button_attr;
10890 ++col;
10891 }
10892 wp->w_winbar_items[item_idx].wb_endcol = col;
10893 wp->w_winbar_items[item_idx].wb_menu = menu;
10894 ++item_idx;
10895
Bram Moolenaar02631462017-09-22 15:20:32 +020010896 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010897 break;
10898 space_to_screenline(off + col, button_attr);
10899 ++col;
10900 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010901 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010902 {
10903 space_to_screenline(off + col, fill_attr);
10904 ++col;
10905 }
10906 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10907
Bram Moolenaar02631462017-09-22 15:20:32 +020010908 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10909 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010910}
10911#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010912
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913/*
10914 * Show current status info in ruler and various other places
10915 * If always is FALSE, only show ruler if position has changed.
10916 */
10917 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010918showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919{
10920 if (!always && !redrawing())
10921 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010922#ifdef FEAT_INS_EXPAND
10923 if (pum_visible())
10924 {
10925 /* Don't redraw right now, do it later. */
10926 curwin->w_redr_status = TRUE;
10927 return;
10928 }
10929#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010930#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010931 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010932 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010933 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 else
10936#endif
10937#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010938 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939#endif
10940
10941#ifdef FEAT_TITLE
10942 if (need_maketitle
10943# ifdef FEAT_STL_OPT
10944 || (p_icon && (stl_syntax & STL_IN_ICON))
10945 || (p_title && (stl_syntax & STL_IN_TITLE))
10946# endif
10947 )
10948 maketitle();
10949#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010950 /* Redraw the tab pages line if needed. */
10951 if (redraw_tabline)
10952 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953}
10954
10955#ifdef FEAT_CMDL_INFO
10956 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010957win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010959#define RULER_BUF_LEN 70
10960 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 int row;
10962 int fillchar;
10963 int attr;
10964 int empty_line = FALSE;
10965 colnr_T virtcol;
10966 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010967 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 int this_ru_col;
10970 int off = 0;
10971 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972
10973 /* If 'ruler' off or redrawing disabled, don't do anything */
10974 if (!p_ru)
10975 return;
10976
10977 /*
10978 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10979 * after deleting lines, before cursor.lnum is corrected.
10980 */
10981 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10982 return;
10983
10984#ifdef FEAT_INS_EXPAND
10985 /* Don't draw the ruler while doing insert-completion, it might overwrite
10986 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 if (edit_submode != NULL)
10989 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010990 // Don't draw the ruler when the popup menu is visible, it may overlap.
10991 // Except when the popup menu will be redrawn anyway.
10992 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010993 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994#endif
10995
10996#ifdef FEAT_STL_OPT
10997 if (*p_ruf)
10998 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010999 int save_called_emsg = called_emsg;
11000
11001 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011003 if (called_emsg)
11004 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011005 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011006 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 return;
11008 }
11009#endif
11010
11011 /*
11012 * Check if not in Insert mode and the line is empty (will show "0-1").
11013 */
11014 if (!(State & INSERT)
11015 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11016 empty_line = TRUE;
11017
11018 /*
11019 * Only draw the ruler when something changed.
11020 */
11021 validate_virtcol_win(wp);
11022 if ( redraw_cmdline
11023 || always
11024 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11025 || wp->w_cursor.col != wp->w_ru_cursor.col
11026 || wp->w_virtcol != wp->w_ru_virtcol
11027#ifdef FEAT_VIRTUALEDIT
11028 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11029#endif
11030 || wp->w_topline != wp->w_ru_topline
11031 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11032#ifdef FEAT_DIFF
11033 || wp->w_topfill != wp->w_ru_topfill
11034#endif
11035 || empty_line != wp->w_ru_empty)
11036 {
11037 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 if (wp->w_status_height)
11039 {
11040 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011041 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011042 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011043 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 }
11045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 {
11047 row = Rows - 1;
11048 fillchar = ' ';
11049 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 width = Columns;
11051 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 }
11053
11054 /* In list mode virtcol needs to be recomputed */
11055 virtcol = wp->w_virtcol;
11056 if (wp->w_p_list && lcs_tab1 == NUL)
11057 {
11058 wp->w_p_list = FALSE;
11059 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11060 wp->w_p_list = TRUE;
11061 }
11062
11063 /*
11064 * Some sprintfs return the length, some return a pointer.
11065 * To avoid portability problems we use strlen() here.
11066 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011067 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11069 ? 0L
11070 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011071 len = STRLEN(buffer);
11072 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11074 (int)virtcol + 1);
11075
11076 /*
11077 * Add a "50%" if there is room for it.
11078 * On the last line, don't print in the last column (scrolls the
11079 * screen up on some terminals).
11080 */
11081 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011082 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 this_ru_col = ru_col - (Columns - width);
11087 if (this_ru_col < 0)
11088 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 /* Never use more than half the window/screen width, leave the other
11090 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011091 if (this_ru_col < (width + 1) / 2)
11092 this_ru_col = (width + 1) / 2;
11093 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011095 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011096 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 {
11098#ifdef FEAT_MBYTE
11099 if (has_mbyte)
11100 i += (*mb_char2bytes)(fillchar, buffer + i);
11101 else
11102#endif
11103 buffer[i++] = fillchar;
11104 ++o;
11105 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011106 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 }
11108 /* Truncate at window boundary. */
11109#ifdef FEAT_MBYTE
11110 if (has_mbyte)
11111 {
11112 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011113 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 {
11115 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011116 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 {
11118 buffer[i] = NUL;
11119 break;
11120 }
11121 }
11122 }
11123 else
11124#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011125 if (this_ru_col + (int)STRLEN(buffer) > width)
11126 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127
Bram Moolenaar4033c552017-09-16 20:54:51 +020011128 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 i = redraw_cmdline;
11130 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011131 this_ru_col + off + (int)STRLEN(buffer),
11132 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 fillchar, fillchar, attr);
11134 /* don't redraw the cmdline because of showing the ruler */
11135 redraw_cmdline = i;
11136 wp->w_ru_cursor = wp->w_cursor;
11137 wp->w_ru_virtcol = wp->w_virtcol;
11138 wp->w_ru_empty = empty_line;
11139 wp->w_ru_topline = wp->w_topline;
11140 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11141#ifdef FEAT_DIFF
11142 wp->w_ru_topfill = wp->w_topfill;
11143#endif
11144 }
11145}
11146#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011147
11148#if defined(FEAT_LINEBREAK) || defined(PROTO)
11149/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011150 * Return the width of the 'number' and 'relativenumber' column.
11151 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011152 * Otherwise it depends on 'numberwidth' and the line count.
11153 */
11154 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011155number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011156{
11157 int n;
11158 linenr_T lnum;
11159
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011160 if (wp->w_p_rnu && !wp->w_p_nu)
11161 /* cursor line shows "0" */
11162 lnum = wp->w_height;
11163 else
11164 /* cursor line shows absolute line number */
11165 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011166
Bram Moolenaar6b314672015-03-20 15:42:10 +010011167 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011168 return wp->w_nrwidth_width;
11169 wp->w_nrwidth_line_count = lnum;
11170
11171 n = 0;
11172 do
11173 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011174 lnum /= 10;
11175 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011176 } while (lnum > 0);
11177
11178 /* 'numberwidth' gives the minimal width plus one */
11179 if (n < wp->w_p_nuw - 1)
11180 n = wp->w_p_nuw - 1;
11181
11182 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011183 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011184 return n;
11185}
11186#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011187
11188/*
11189 * Return the current cursor column. This is the actual position on the
11190 * screen. First column is 0.
11191 */
11192 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011193screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011194{
11195 return screen_cur_col;
11196}
11197
11198/*
11199 * Return the current cursor row. This is the actual position on the screen.
11200 * First row is 0.
11201 */
11202 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011203screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011204{
11205 return screen_cur_row;
11206}