blob: e9405b4f8e16344ef3f25921fbcc6efe88fa6a1f [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 }
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100241 // This may be needed when switching tabs.
242 if (must_redraw < type)
243 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244}
245
246/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000247 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 */
249 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100250redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251{
252 redraw_buf_later(curbuf, type);
253}
254
255 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100256redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257{
258 win_T *wp;
259
260 FOR_ALL_WINDOWS(wp)
261 {
262 if (wp->w_buffer == buf)
263 redraw_win_later(wp, type);
264 }
265}
266
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200267 void
268redraw_buf_and_status_later(buf_T *buf, int type)
269{
270 win_T *wp;
271
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200272#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200273 if (wild_menu_showing != 0)
274 /* Don't redraw while the command line completion is displayed, it
275 * would disappear. */
276 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200277#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200278 FOR_ALL_WINDOWS(wp)
279 {
280 if (wp->w_buffer == buf)
281 {
282 redraw_win_later(wp, type);
283 wp->w_redr_status = TRUE;
284 }
285 }
286}
287
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200289 * Redraw as soon as possible. When the command line is not scrolled redraw
290 * right away and restore what was on the command line.
291 * Return a code indicating what happened.
292 */
293 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100294redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295{
296 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200297 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200298 int r;
299 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200300 schar_T *screenline; /* copy from ScreenLines[] */
301 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302#ifdef FEAT_MBYTE
303 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200304 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200306 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307#endif
308
309 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200310 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200311 return ret;
312
313 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200314 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200315 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200316 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200318 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319 if (screenline == NULL || screenattr == NULL)
320 ret = 2;
321#ifdef FEAT_MBYTE
322 if (enc_utf8)
323 {
324 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 if (screenlineUC == NULL)
327 ret = 2;
328 for (i = 0; i < p_mco; ++i)
329 {
330 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200331 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200332 if (screenlineC[i] == NULL)
333 ret = 2;
334 }
335 }
336 if (enc_dbcs == DBCS_JPNU)
337 {
338 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 if (screenline2 == NULL)
341 ret = 2;
342 }
343#endif
344
345 if (ret != 2)
346 {
347 /* Save the text displayed in the command line area. */
348 for (r = 0; r < rows; ++r)
349 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200352 (size_t)cols * sizeof(schar_T));
353 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200354 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200355 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200356#ifdef FEAT_MBYTE
357 if (enc_utf8)
358 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 mch_memmove(screenlineC[i] + r * cols,
364 ScreenLinesC[i] + LineOffset[cmdline_row + r],
365 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 }
367 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371#endif
372 }
373
374 update_screen(0);
375 ret = 3;
376
377 if (must_redraw == 0)
378 {
379 int off = (int)(current_ScreenLine - ScreenLines);
380
381 /* Restore the text displayed in the command line area. */
382 for (r = 0; r < rows; ++r)
383 {
384 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200385 screenline + r * cols,
386 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200388 screenattr + r * cols,
389 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390#ifdef FEAT_MBYTE
391 if (enc_utf8)
392 {
393 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200394 screenlineUC + r * cols,
395 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200396 for (i = 0; i < p_mco; ++i)
397 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200398 screenlineC[i] + r * cols,
399 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200400 }
401 if (enc_dbcs == DBCS_JPNU)
402 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200403 screenline2 + r * cols,
404 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200405#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200406 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408 ret = 4;
409 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411
412 vim_free(screenline);
413 vim_free(screenattr);
414#ifdef FEAT_MBYTE
415 if (enc_utf8)
416 {
417 vim_free(screenlineUC);
418 for (i = 0; i < p_mco; ++i)
419 vim_free(screenlineC[i]);
420 }
421 if (enc_dbcs == DBCS_JPNU)
422 vim_free(screenline2);
423#endif
424
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200425 /* Show the intro message when appropriate. */
426 maybe_intro_message();
427
428 setcursor();
429
Bram Moolenaar2951b772013-07-03 12:45:31 +0200430 return ret;
431}
432
433/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100434 * Invoked after an asynchronous callback is called.
435 * If an echo command was used the cursor needs to be put back where
436 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200437 * If "call_update_screen" is FALSE don't call update_screen() when at the
438 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439 */
440 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200441redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100442{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200443 ++redrawing_for_callback;
444
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100445 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200446 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100447 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200448 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200449 // Don't redraw when in prompt_for_number().
450 if (cmdline_row > 0)
451 {
452 // Redrawing only works when the screen didn't scroll. Don't clear
453 // wildmenu entries.
454 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200455#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200456 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200457#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200458 && call_update_screen)
459 update_screen(0);
460
461 // Redraw in the same position, so that the user can continue
462 // editing the command.
463 redrawcmdline_ex(FALSE);
464 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200465 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200466 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100467 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200468 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100470 setcursor();
471 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100472 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100473#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100474 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200475 // Don't update the cursor when it is blinking and off to avoid
476 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100477 out_flush_cursor(FALSE, FALSE);
478 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100479#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100480 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200481
482 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483}
484
485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 * Changed something in the current window, at buffer line "lnum", that
487 * requires that line and possibly other lines to be redrawn.
488 * Used when entering/leaving Insert mode with the cursor on a folded line.
489 * Used to remove the "$" from a change command.
490 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
491 * may become invalid and the whole window will have to be redrawn.
492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100494redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200495 win_T *wp,
Bram Moolenaar05540972016-01-30 20:31:25 +0100496 linenr_T lnum,
497 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499#ifdef FEAT_FOLDING
500 int i;
501#endif
502
Bram Moolenaar90a99792018-09-12 21:52:18 +0200503 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
504 wp->w_redraw_top = lnum;
505 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
506 wp->w_redraw_bot = lnum;
507 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508
509#ifdef FEAT_FOLDING
510 if (invalid)
511 {
512 /* A w_lines[] entry for this lnum has become invalid. */
Bram Moolenaar90a99792018-09-12 21:52:18 +0200513 i = find_wl_entry(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (i >= 0)
Bram Moolenaar90a99792018-09-12 21:52:18 +0200515 wp->w_lines[i].wl_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 }
517#endif
518}
519
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200520 void
521reset_updating_screen(int may_resize_shell UNUSED)
522{
523 updating_screen = FALSE;
524#ifdef FEAT_GUI
525 if (may_resize_shell)
526 gui_may_resize_shell();
527#endif
528#ifdef FEAT_TERMINAL
529 term_check_channel_closed_recently();
530#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200531
532#ifdef HAVE_DROP_FILE
533 // If handle_drop() was called while updating_screen was TRUE need to
534 // handle the drop now.
535 handle_any_postponed_drop();
536#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200537}
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200540 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 */
542 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100543update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 redraw_curbuf_later(type);
546 update_screen(type);
547}
548
549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 * Based on the current value of curwin->w_topline, transfer a screenfull
551 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200552 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200554 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200555update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200557 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 win_T *wp;
559 static int did_intro = FALSE;
560#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
561 int did_one;
562#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200563#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200564 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200565 int gui_cursor_col;
566 int gui_cursor_row;
567#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200568 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000570 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200572 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200574 if (type == VALID_NO_UPDATE)
575 {
576 no_update = TRUE;
577 type = 0;
578 }
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (must_redraw)
581 {
582 if (type < must_redraw) /* use maximal type */
583 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000584
585 /* must_redraw is reset here, so that when we run into some weird
586 * reason to redraw while busy redrawing (e.g., asynchronous
587 * scrolling), or update_topline() in win_update() will cause a
588 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 must_redraw = 0;
590 }
591
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200592 /* May need to update w_lines[]. */
593 if (curwin->w_lines_valid == 0 && type < NOT_VALID
594#ifdef FEAT_TERMINAL
595 && !term_do_update_window(curwin)
596#endif
597 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 type = NOT_VALID;
599
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000600 /* Postpone the redrawing when it's not needed and when being called
601 * recursively. */
602 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 {
604 redraw_later(type); /* remember type for next time */
605 must_redraw = type;
606 if (type > INVERTED_ALL)
607 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200608 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610
611 updating_screen = TRUE;
612#ifdef FEAT_SYN_HL
613 ++display_tick; /* let syntax code know we're in a next round of
614 * display updating */
615#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200616 if (no_update)
617 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 /*
620 * if the screen was scrolled up when displaying a message, scroll it down
621 */
622 if (msg_scrolled)
623 {
624 clear_cmdline = TRUE;
625 if (msg_scrolled > Rows - 5) /* clearing is faster */
626 type = CLEAR;
627 else if (type != CLEAR)
628 {
629 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200630 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
631 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 type = CLEAR;
633 FOR_ALL_WINDOWS(wp)
634 {
635 if (W_WINROW(wp) < msg_scrolled)
636 {
637 if (W_WINROW(wp) + wp->w_height > msg_scrolled
638 && wp->w_redr_type < REDRAW_TOP
639 && wp->w_lines_valid > 0
640 && wp->w_topline == wp->w_lines[0].wl_lnum)
641 {
642 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
643 wp->w_redr_type = REDRAW_TOP;
644 }
645 else
646 {
647 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200648 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
649 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 }
652 }
653 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200654 if (!no_update)
655 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000656 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 }
658 msg_scrolled = 0;
659 need_wait_return = FALSE;
660 }
661
662 /* reset cmdline_row now (may have been changed temporarily) */
663 compute_cmdrow();
664
665 /* Check for changed highlighting */
666 if (need_highlight_changed)
667 highlight_changed();
668
669 if (type == CLEAR) /* first clear screen */
670 {
671 screenclear(); /* will reset clear_cmdline */
672 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200673 /* must_redraw may be set indirectly, avoid another redraw later */
674 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 }
676
677 if (clear_cmdline) /* going to clear cmdline (done below) */
678 check_for_delay(FALSE);
679
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000680#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200681 /* Force redraw when width of 'number' or 'relativenumber' column
682 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000683 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200684 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
685 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000686 curwin->w_redr_type = NOT_VALID;
687#endif
688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 /*
690 * Only start redrawing if there is really something to do.
691 */
692 if (type == INVERTED)
693 update_curswant();
694 if (curwin->w_redr_type < type
695 && !((type == VALID
696 && curwin->w_lines[0].wl_valid
697#ifdef FEAT_DIFF
698 && curwin->w_topfill == curwin->w_old_topfill
699 && curwin->w_botfill == curwin->w_old_botfill
700#endif
701 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000703 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
705 && curwin->w_old_visual_mode == VIsual_mode
706 && (curwin->w_valid & VALID_VIRTCOL)
707 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 ))
709 curwin->w_redr_type = type;
710
Bram Moolenaar5a305422006-04-28 22:38:25 +0000711 /* Redraw the tab pages line if needed. */
712 if (redraw_tabline || type >= NOT_VALID)
713 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715#ifdef FEAT_SYN_HL
716 /*
717 * Correct stored syntax highlighting info for changes in each displayed
718 * buffer. Each buffer must only be done once.
719 */
720 FOR_ALL_WINDOWS(wp)
721 {
722 if (wp->w_buffer->b_mod_set)
723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 win_T *wwp;
725
726 /* Check if we already did this buffer. */
727 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
728 if (wwp->w_buffer == wp->w_buffer)
729 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200730 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 syn_stack_apply_changes(wp->w_buffer);
732 }
733 }
734#endif
735
736 /*
737 * Go from top to bottom through the windows, redrawing the ones that need
738 * it.
739 */
740#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
741 did_one = FALSE;
742#endif
743#ifdef FEAT_SEARCH_EXTRA
744 search_hl.rm.regprog = NULL;
745#endif
746 FOR_ALL_WINDOWS(wp)
747 {
748 if (wp->w_redr_type != 0)
749 {
750 cursor_off();
751#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
752 if (!did_one)
753 {
754 did_one = TRUE;
755# ifdef FEAT_SEARCH_EXTRA
756 start_search_hl();
757# endif
758# ifdef FEAT_CLIPBOARD
759 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200760 if (clip_star.available && clip_isautosel_star())
761 clip_update_selection(&clip_star);
762 if (clip_plus.available && clip_isautosel_plus())
763 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764# endif
765#ifdef FEAT_GUI
766 /* Remove the cursor before starting to do anything, because
767 * scrolling may make it difficult to redraw the text under
768 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200769 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200770 {
771 gui_cursor_col = gui.cursor_col;
772 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200774 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#endif
777 }
778#endif
779 win_update(wp);
780 }
781
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 /* redraw status line after the window to minimize cursor movement */
783 if (wp->w_redr_status)
784 {
785 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200786 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
789#if defined(FEAT_SEARCH_EXTRA)
790 end_search_hl();
791#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100792#ifdef FEAT_INS_EXPAND
793 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200794 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 /* Reset b_mod_set flags. Going through all windows is probably faster
798 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200799 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200802 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
804 /* Clear or redraw the command line. Done last, because scrolling may
805 * mess up the command line. */
806 if (clear_cmdline || redraw_cmdline)
807 showmode();
808
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200809 if (no_update)
810 --no_win_do_lines_ins;
811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200813 if (!did_intro)
814 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 did_intro = TRUE;
816
817#ifdef FEAT_GUI
818 /* Redraw the cursor and update the scrollbars when all screen updating is
819 * done. */
820 if (gui.in_use)
821 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200822 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200823 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100824 mch_disable_flush();
825 out_flush(); /* required before updating the cursor */
826 mch_enable_flush();
827
Bram Moolenaar144445d2016-07-08 21:41:54 +0200828 /* Put the GUI position where the cursor was, gui_update_cursor()
829 * uses that. */
830 gui.col = gui_cursor_col;
831 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200832# ifdef FEAT_MBYTE
833 gui.col = mb_fix_col(gui.col, gui.row);
834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100836 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200837 screen_cur_col = gui.col;
838 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200839 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100840 else
841 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 gui_update_scrollbars(FALSE);
843 }
844#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200845 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846}
847
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100848#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
849/*
850 * Prepare for updating one or more windows.
851 * Caller must check for "updating_screen" already set to avoid recursiveness.
852 */
853 static void
854update_prepare(void)
855{
856 cursor_off();
857 updating_screen = TRUE;
858#ifdef FEAT_GUI
859 /* Remove the cursor before starting to do anything, because scrolling may
860 * make it difficult to redraw the text under it. */
861 if (gui.in_use)
862 gui_undraw_cursor();
863#endif
864#ifdef FEAT_SEARCH_EXTRA
865 start_search_hl();
866#endif
867}
868
869/*
870 * Finish updating one or more windows.
871 */
872 static void
873update_finish(void)
874{
875 if (redraw_cmdline)
876 showmode();
877
878# ifdef FEAT_SEARCH_EXTRA
879 end_search_hl();
880# endif
881
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200882 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100883
884# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100885 /* Redraw the cursor and update the scrollbars when all screen updating is
886 * done. */
887 if (gui.in_use)
888 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100889 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100890 gui_update_scrollbars(FALSE);
891 }
892# endif
893}
894#endif
895
Bram Moolenaar860cae12010-06-05 23:22:07 +0200896#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200897/*
898 * Return TRUE if the cursor line in window "wp" may be concealed, according
899 * to the 'concealcursor' option.
900 */
901 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100902conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200903{
904 int c;
905
906 if (*wp->w_p_cocu == NUL)
907 return FALSE;
908 if (get_real_state() & VISUAL)
909 c = 'v';
910 else if (State & INSERT)
911 c = 'i';
912 else if (State & NORMAL)
913 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200914 else if (State & CMDLINE)
915 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200916 else
917 return FALSE;
918 return vim_strchr(wp->w_p_cocu, c) != NULL;
919}
920
921/*
922 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
923 */
924 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200925conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200926{
927 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
928 {
929 need_cursor_line_redraw = TRUE;
930 /* Need to recompute cursor column, e.g., when starting Visual mode
931 * without concealing. */
932 curs_columns(TRUE);
933 }
934}
935
Bram Moolenaar860cae12010-06-05 23:22:07 +0200936 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100937update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200938{
939 int row;
940 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200941#ifdef SYN_TIME_LIMIT
942 proftime_T syntax_tm;
943#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200944
Bram Moolenaar908be432016-05-24 10:51:30 +0200945 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100946 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200947 return;
948
Bram Moolenaar860cae12010-06-05 23:22:07 +0200949 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200950 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200952#ifdef SYN_TIME_LIMIT
953 /* Set the time limit to 'redrawtime'. */
954 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200955 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200956#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100957 update_prepare();
958
Bram Moolenaar860cae12010-06-05 23:22:07 +0200959 row = 0;
960 for (j = 0; j < wp->w_lines_valid; ++j)
961 {
962 if (lnum == wp->w_lines[j].wl_lnum)
963 {
964 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200965# ifdef FEAT_SEARCH_EXTRA
966 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200967 prepare_search_hl(wp, lnum);
968# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200969 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
970 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200971 break;
972 }
973 row += wp->w_lines[j].wl_size;
974 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100975
976 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200977
978#ifdef SYN_TIME_LIMIT
979 syn_set_timeout(NULL);
980#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200981 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200982 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984#endif
985
986#if defined(FEAT_SIGNS) || defined(PROTO)
987 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100988update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990 win_T *wp;
991 int doit = FALSE;
992
993# ifdef FEAT_FOLDING
994 win_foldinfo.fi_level = 0;
995# endif
996
997 /* update/delete a specific mark */
998 FOR_ALL_WINDOWS(wp)
999 {
1000 if (buf != NULL && lnum > 0)
1001 {
1002 if (wp->w_buffer == buf && lnum >= wp->w_topline
1003 && lnum < wp->w_botline)
1004 {
1005 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1006 wp->w_redraw_top = lnum;
1007 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1008 wp->w_redraw_bot = lnum;
1009 redraw_win_later(wp, VALID);
1010 }
1011 }
1012 else
1013 redraw_win_later(wp, VALID);
1014 if (wp->w_redr_type != 0)
1015 doit = TRUE;
1016 }
1017
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001018 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001019 * happening (recursive call), messages on the screen or still starting up.
1020 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001021 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001022 || State == ASKMORE || State == HITRETURN
1023 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001024#ifdef FEAT_GUI
1025 || gui.starting
1026#endif
1027 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 return;
1029
1030 /* update all windows that need updating */
1031 update_prepare();
1032
Bram Moolenaar29323592016-07-24 22:04:11 +02001033 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 {
1035 if (wp->w_redr_type != 0)
1036 win_update(wp);
1037 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001038 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
1041 update_finish();
1042}
1043#endif
1044
1045
1046#if defined(FEAT_GUI) || defined(PROTO)
1047/*
1048 * Update a single window, its status line and maybe the command line msg.
1049 * Used for the GUI scrollbar.
1050 */
1051 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001052updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001054 /* return if already busy updating */
1055 if (updating_screen)
1056 return;
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 update_prepare();
1059
1060#ifdef FEAT_CLIPBOARD
1061 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001062 if (clip_star.available && clip_isautosel_star())
1063 clip_update_selection(&clip_star);
1064 if (clip_plus.available && clip_isautosel_plus())
1065 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001067
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001069
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001070 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001071 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001072 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (wp->w_redr_status
1075# ifdef FEAT_CMDL_INFO
1076 || p_ru
1077# endif
1078# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001079 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080# endif
1081 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001082 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
1084 update_finish();
1085}
1086#endif
1087
1088/*
1089 * Update a single window.
1090 *
1091 * This may cause the windows below it also to be redrawn (when clearing the
1092 * screen or scrolling lines).
1093 *
1094 * How the window is redrawn depends on wp->w_redr_type. Each type also
1095 * implies the one below it.
1096 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001097 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1099 * INVERTED redraw the changed part of the Visual area
1100 * INVERTED_ALL redraw the whole Visual area
1101 * VALID 1. scroll up/down to adjust for a changed w_topline
1102 * 2. update lines at the top when scrolled down
1103 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001104 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * b_mod_top and b_mod_bot.
1106 * - if wp->w_redraw_top non-zero, redraw lines between
1107 * wp->w_redraw_top and wp->w_redr_bot.
1108 * - continue redrawing when syntax status is invalid.
1109 * 4. if scrolled up, update lines at the bottom.
1110 * This results in three areas that may need updating:
1111 * top: from first row to top_end (when scrolled down)
1112 * mid: from mid_start to mid_end (update inversion or changed text)
1113 * bot: from bot_start to last row (when scrolled up)
1114 */
1115 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001116win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 buf_T *buf = wp->w_buffer;
1119 int type;
1120 int top_end = 0; /* Below last row of the top area that needs
1121 updating. 0 when no top area updating. */
1122 int mid_start = 999;/* first row of the mid area that needs
1123 updating. 999 when no mid area updating. */
1124 int mid_end = 0; /* Below last row of the mid area that needs
1125 updating. 0 when no mid area updating. */
1126 int bot_start = 999;/* first row of the bot area that needs
1127 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 int scrolled_down = FALSE; /* TRUE when scrolled down when
1129 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001131 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 int top_to_mod = FALSE; /* redraw above mod_top */
1133#endif
1134
1135 int row; /* current window row to display */
1136 linenr_T lnum; /* current buffer lnum to display */
1137 int idx; /* current index in w_lines[] */
1138 int srow; /* starting row of the current line */
1139
1140 int eof = FALSE; /* if TRUE, we hit the end of the file */
1141 int didline = FALSE; /* if TRUE, we finished the last line */
1142 int i;
1143 long j;
1144 static int recursive = FALSE; /* being called recursively */
1145 int old_botline = wp->w_botline;
1146#ifdef FEAT_FOLDING
1147 long fold_count;
1148#endif
1149#ifdef FEAT_SYN_HL
1150 /* remember what happened to the previous line, to know if
1151 * check_visual_highlight() can be used */
1152#define DID_NONE 1 /* didn't update a line */
1153#define DID_LINE 2 /* updated a normal line */
1154#define DID_FOLD 3 /* updated a folded line */
1155 int did_update = DID_NONE;
1156 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1157#endif
1158 linenr_T mod_top = 0;
1159 linenr_T mod_bot = 0;
1160#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1161 int save_got_int;
1162#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001163#ifdef SYN_TIME_LIMIT
1164 proftime_T syntax_tm;
1165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
1167 type = wp->w_redr_type;
1168
1169 if (type == NOT_VALID)
1170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 wp->w_lines_valid = 0;
1173 }
1174
1175 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001176 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {
1178 wp->w_redr_type = 0;
1179 return;
1180 }
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 /* Window is zero-width: Only need to draw the separator. */
1183 if (wp->w_width == 0)
1184 {
1185 /* draw the vertical separator right of this window */
1186 draw_vsep_win(wp, 0);
1187 wp->w_redr_type = 0;
1188 return;
1189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001191#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001192 // If this window contains a terminal, redraw works completely differently.
1193 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001194 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001195 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001196# ifdef FEAT_MENU
1197 /* Draw the window toolbar, if there is one. */
1198 if (winbar_height(wp) > 0)
1199 redraw_win_toolbar(wp);
1200# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001201 wp->w_redr_type = 0;
1202 return;
1203 }
1204#endif
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001207 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208#endif
1209
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001210#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001211 /* Force redraw when width of 'number' or 'relativenumber' column
1212 * changes. */
1213 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001214 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001215 {
1216 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001217 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001218 }
1219 else
1220#endif
1221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1223 {
1224 /*
1225 * When there are both inserted/deleted lines and specific lines to be
1226 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1227 * everything (only happens when redrawing is off for while).
1228 */
1229 type = NOT_VALID;
1230 }
1231 else
1232 {
1233 /*
1234 * Set mod_top to the first line that needs displaying because of
1235 * changes. Set mod_bot to the first line after the changes.
1236 */
1237 mod_top = wp->w_redraw_top;
1238 if (wp->w_redraw_bot != 0)
1239 mod_bot = wp->w_redraw_bot + 1;
1240 else
1241 mod_bot = 0;
1242 wp->w_redraw_top = 0; /* reset for next time */
1243 wp->w_redraw_bot = 0;
1244 if (buf->b_mod_set)
1245 {
1246 if (mod_top == 0 || mod_top > buf->b_mod_top)
1247 {
1248 mod_top = buf->b_mod_top;
1249#ifdef FEAT_SYN_HL
1250 /* Need to redraw lines above the change that may be included
1251 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001252 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001254 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (mod_top < 1)
1256 mod_top = 1;
1257 }
1258#endif
1259 }
1260 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1261 mod_bot = buf->b_mod_bot;
1262
1263#ifdef FEAT_SEARCH_EXTRA
1264 /* When 'hlsearch' is on and using a multi-line search pattern, a
1265 * change in one line may make the Search highlighting in a
1266 * previous line invalid. Simple solution: redraw all visible
1267 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001268 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001270 if (search_hl.rm.regprog != NULL
1271 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001273 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001274 {
1275 cur = wp->w_match_head;
1276 while (cur != NULL)
1277 {
1278 if (cur->match.regprog != NULL
1279 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001280 {
1281 top_to_mod = TRUE;
1282 break;
1283 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001284 cur = cur->next;
1285 }
1286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287#endif
1288 }
1289#ifdef FEAT_FOLDING
1290 if (mod_top != 0 && hasAnyFolding(wp))
1291 {
1292 linenr_T lnumt, lnumb;
1293
1294 /*
1295 * A change in a line can cause lines above it to become folded or
1296 * unfolded. Find the top most buffer line that may be affected.
1297 * If the line was previously folded and displayed, get the first
1298 * line of that fold. If the line is folded now, get the first
1299 * folded line. Use the minimum of these two.
1300 */
1301
1302 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1303 * the line below it. If there is no valid entry, use w_topline.
1304 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1305 * to this line. If there is no valid entry, use MAXLNUM. */
1306 lnumt = wp->w_topline;
1307 lnumb = MAXLNUM;
1308 for (i = 0; i < wp->w_lines_valid; ++i)
1309 if (wp->w_lines[i].wl_valid)
1310 {
1311 if (wp->w_lines[i].wl_lastlnum < mod_top)
1312 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1313 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1314 {
1315 lnumb = wp->w_lines[i].wl_lnum;
1316 /* When there is a fold column it might need updating
1317 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001318 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 ++lnumb;
1320 }
1321 }
1322
1323 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1324 if (mod_top > lnumt)
1325 mod_top = lnumt;
1326
1327 /* Now do the same for the bottom line (one above mod_bot). */
1328 --mod_bot;
1329 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1330 ++mod_bot;
1331 if (mod_bot < lnumb)
1332 mod_bot = lnumb;
1333 }
1334#endif
1335
1336 /* When a change starts above w_topline and the end is below
1337 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001338 * If the end of the change is above w_topline: do like no change was
1339 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (mod_top != 0 && mod_top < wp->w_topline)
1341 {
1342 if (mod_bot > wp->w_topline)
1343 mod_top = wp->w_topline;
1344#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001345 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 top_end = 1;
1347#endif
1348 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001349
1350 /* When line numbers are displayed need to redraw all lines below
1351 * inserted/deleted lines. */
1352 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1353 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355
1356 /*
1357 * When only displaying the lines at the top, set top_end. Used when
1358 * window has scrolled down for msg_scrolled.
1359 */
1360 if (type == REDRAW_TOP)
1361 {
1362 j = 0;
1363 for (i = 0; i < wp->w_lines_valid; ++i)
1364 {
1365 j += wp->w_lines[i].wl_size;
1366 if (j >= wp->w_upd_rows)
1367 {
1368 top_end = j;
1369 break;
1370 }
1371 }
1372 if (top_end == 0)
1373 /* not found (cannot happen?): redraw everything */
1374 type = NOT_VALID;
1375 else
1376 /* top area defined, the rest is VALID */
1377 type = VALID;
1378 }
1379
Bram Moolenaar367329b2007-08-30 11:53:22 +00001380 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001381 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1382 * non-zero and thus not FALSE) will indicate that screenclear() was not
1383 * called. */
1384 if (screen_cleared)
1385 screen_cleared = MAYBE;
1386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /*
1388 * If there are no changes on the screen that require a complete redraw,
1389 * handle three cases:
1390 * 1: we are off the top of the screen by a few lines: scroll down
1391 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1392 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1393 * w_lines[] that needs updating.
1394 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001395 if ((type == VALID || type == SOME_VALID
1396 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#ifdef FEAT_DIFF
1398 && !wp->w_botfill && !wp->w_old_botfill
1399#endif
1400 )
1401 {
1402 if (mod_top != 0 && wp->w_topline == mod_top)
1403 {
1404 /*
1405 * w_topline is the first changed line, the scrolling will be done
1406 * further down.
1407 */
1408 }
1409 else if (wp->w_lines[0].wl_valid
1410 && (wp->w_topline < wp->w_lines[0].wl_lnum
1411#ifdef FEAT_DIFF
1412 || (wp->w_topline == wp->w_lines[0].wl_lnum
1413 && wp->w_topfill > wp->w_old_topfill)
1414#endif
1415 ))
1416 {
1417 /*
1418 * New topline is above old topline: May scroll down.
1419 */
1420#ifdef FEAT_FOLDING
1421 if (hasAnyFolding(wp))
1422 {
1423 linenr_T ln;
1424
1425 /* count the number of lines we are off, counting a sequence
1426 * of folded lines as one */
1427 j = 0;
1428 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1429 {
1430 ++j;
1431 if (j >= wp->w_height - 2)
1432 break;
1433 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1434 }
1435 }
1436 else
1437#endif
1438 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1439 if (j < wp->w_height - 2) /* not too far off */
1440 {
1441 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1442#ifdef FEAT_DIFF
1443 /* insert extra lines for previously invisible filler lines */
1444 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1445 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1446 - wp->w_old_topfill;
1447#endif
1448 if (i < wp->w_height - 2) /* less than a screen off */
1449 {
1450 /*
1451 * Try to insert the correct number of lines.
1452 * If not the last window, delete the lines at the bottom.
1453 * win_ins_lines may fail when the terminal can't do it.
1454 */
1455 if (i > 0)
1456 check_for_delay(FALSE);
1457 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1458 {
1459 if (wp->w_lines_valid != 0)
1460 {
1461 /* Need to update rows that are new, stop at the
1462 * first one that scrolled down. */
1463 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
1466 /* Move the entries that were scrolled, disable
1467 * the entries for the lines to be redrawn. */
1468 if ((wp->w_lines_valid += j) > wp->w_height)
1469 wp->w_lines_valid = wp->w_height;
1470 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1471 wp->w_lines[idx] = wp->w_lines[idx - j];
1472 while (idx >= 0)
1473 wp->w_lines[idx--].wl_valid = FALSE;
1474 }
1475 }
1476 else
1477 mid_start = 0; /* redraw all lines */
1478 }
1479 else
1480 mid_start = 0; /* redraw all lines */
1481 }
1482 else
1483 mid_start = 0; /* redraw all lines */
1484 }
1485 else
1486 {
1487 /*
1488 * New topline is at or below old topline: May scroll up.
1489 * When topline didn't change, find first entry in w_lines[] that
1490 * needs updating.
1491 */
1492
1493 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1494 j = -1;
1495 row = 0;
1496 for (i = 0; i < wp->w_lines_valid; i++)
1497 {
1498 if (wp->w_lines[i].wl_valid
1499 && wp->w_lines[i].wl_lnum == wp->w_topline)
1500 {
1501 j = i;
1502 break;
1503 }
1504 row += wp->w_lines[i].wl_size;
1505 }
1506 if (j == -1)
1507 {
1508 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1509 * lines */
1510 mid_start = 0;
1511 }
1512 else
1513 {
1514 /*
1515 * Try to delete the correct number of lines.
1516 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1517 */
1518#ifdef FEAT_DIFF
1519 /* If the topline didn't change, delete old filler lines,
1520 * otherwise delete filler lines of the new topline... */
1521 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1522 row += wp->w_old_topfill;
1523 else
1524 row += diff_check_fill(wp, wp->w_topline);
1525 /* ... but don't delete new filler lines. */
1526 row -= wp->w_topfill;
1527#endif
1528 if (row > 0)
1529 {
1530 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001531 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1532 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 bot_start = wp->w_height - row;
1534 else
1535 mid_start = 0; /* redraw all lines */
1536 }
1537 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1538 {
1539 /*
1540 * Skip the lines (below the deleted lines) that are still
1541 * valid and don't need redrawing. Copy their info
1542 * upwards, to compensate for the deleted lines. Set
1543 * bot_start to the first row that needs redrawing.
1544 */
1545 bot_start = 0;
1546 idx = 0;
1547 for (;;)
1548 {
1549 wp->w_lines[idx] = wp->w_lines[j];
1550 /* stop at line that didn't fit, unless it is still
1551 * valid (no lines deleted) */
1552 if (row > 0 && bot_start + row
1553 + (int)wp->w_lines[j].wl_size > wp->w_height)
1554 {
1555 wp->w_lines_valid = idx + 1;
1556 break;
1557 }
1558 bot_start += wp->w_lines[idx++].wl_size;
1559
1560 /* stop at the last valid entry in w_lines[].wl_size */
1561 if (++j >= wp->w_lines_valid)
1562 {
1563 wp->w_lines_valid = idx;
1564 break;
1565 }
1566 }
1567#ifdef FEAT_DIFF
1568 /* Correct the first entry for filler lines at the top
1569 * when it won't get updated below. */
1570 if (wp->w_p_diff && bot_start > 0)
1571 wp->w_lines[0].wl_size =
1572 plines_win_nofill(wp, wp->w_topline, TRUE)
1573 + wp->w_topfill;
1574#endif
1575 }
1576 }
1577 }
1578
1579 /* When starting redraw in the first line, redraw all lines. When
1580 * there is only one window it's probably faster to clear the screen
1581 * first. */
1582 if (mid_start == 0)
1583 {
1584 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001585 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001586 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001587 /* Clear the screen when it was not done by win_del_lines() or
1588 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1589 * then. */
1590 if (screen_cleared != TRUE)
1591 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001592 /* The screen was cleared, redraw the tab pages line. */
1593 if (redraw_tabline)
1594 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001597
1598 /* When win_del_lines() or win_ins_lines() caused the screen to be
1599 * cleared (only happens for the first window) or when screenclear()
1600 * was called directly above, "must_redraw" will have been set to
1601 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1602 if (screen_cleared == TRUE)
1603 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
1605 else
1606 {
1607 /* Not VALID or INVERTED: redraw all lines. */
1608 mid_start = 0;
1609 mid_end = wp->w_height;
1610 }
1611
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001612 if (type == SOME_VALID)
1613 {
1614 /* SOME_VALID: redraw all lines. */
1615 mid_start = 0;
1616 mid_end = wp->w_height;
1617 type = NOT_VALID;
1618 }
1619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 /* check if we are updating or removing the inverted part */
1621 if ((VIsual_active && buf == curwin->w_buffer)
1622 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1623 {
1624 linenr_T from, to;
1625
1626 if (VIsual_active)
1627 {
1628 if (VIsual_active
1629 && (VIsual_mode != wp->w_old_visual_mode
1630 || type == INVERTED_ALL))
1631 {
1632 /*
1633 * If the type of Visual selection changed, redraw the whole
1634 * selection. Also when the ownership of the X selection is
1635 * gained or lost.
1636 */
1637 if (curwin->w_cursor.lnum < VIsual.lnum)
1638 {
1639 from = curwin->w_cursor.lnum;
1640 to = VIsual.lnum;
1641 }
1642 else
1643 {
1644 from = VIsual.lnum;
1645 to = curwin->w_cursor.lnum;
1646 }
1647 /* redraw more when the cursor moved as well */
1648 if (wp->w_old_cursor_lnum < from)
1649 from = wp->w_old_cursor_lnum;
1650 if (wp->w_old_cursor_lnum > to)
1651 to = wp->w_old_cursor_lnum;
1652 if (wp->w_old_visual_lnum < from)
1653 from = wp->w_old_visual_lnum;
1654 if (wp->w_old_visual_lnum > to)
1655 to = wp->w_old_visual_lnum;
1656 }
1657 else
1658 {
1659 /*
1660 * Find the line numbers that need to be updated: The lines
1661 * between the old cursor position and the current cursor
1662 * position. Also check if the Visual position changed.
1663 */
1664 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1665 {
1666 from = curwin->w_cursor.lnum;
1667 to = wp->w_old_cursor_lnum;
1668 }
1669 else
1670 {
1671 from = wp->w_old_cursor_lnum;
1672 to = curwin->w_cursor.lnum;
1673 if (from == 0) /* Visual mode just started */
1674 from = to;
1675 }
1676
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001677 if (VIsual.lnum != wp->w_old_visual_lnum
1678 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 if (wp->w_old_visual_lnum < from
1681 && wp->w_old_visual_lnum != 0)
1682 from = wp->w_old_visual_lnum;
1683 if (wp->w_old_visual_lnum > to)
1684 to = wp->w_old_visual_lnum;
1685 if (VIsual.lnum < from)
1686 from = VIsual.lnum;
1687 if (VIsual.lnum > to)
1688 to = VIsual.lnum;
1689 }
1690 }
1691
1692 /*
1693 * If in block mode and changed column or curwin->w_curswant:
1694 * update all lines.
1695 * First compute the actual start and end column.
1696 */
1697 if (VIsual_mode == Ctrl_V)
1698 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001699 colnr_T fromc, toc;
1700#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1701 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaar404406a2014-10-09 13:24:43 +02001703 if (curwin->w_p_lbr)
1704 ve_flags = VE_ALL;
1705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001707#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1708 ve_flags = save_ve_flags;
1709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 ++toc;
1711 if (curwin->w_curswant == MAXCOL)
1712 toc = MAXCOL;
1713
1714 if (fromc != wp->w_old_cursor_fcol
1715 || toc != wp->w_old_cursor_lcol)
1716 {
1717 if (from > VIsual.lnum)
1718 from = VIsual.lnum;
1719 if (to < VIsual.lnum)
1720 to = VIsual.lnum;
1721 }
1722 wp->w_old_cursor_fcol = fromc;
1723 wp->w_old_cursor_lcol = toc;
1724 }
1725 }
1726 else
1727 {
1728 /* Use the line numbers of the old Visual area. */
1729 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1730 {
1731 from = wp->w_old_cursor_lnum;
1732 to = wp->w_old_visual_lnum;
1733 }
1734 else
1735 {
1736 from = wp->w_old_visual_lnum;
1737 to = wp->w_old_cursor_lnum;
1738 }
1739 }
1740
1741 /*
1742 * There is no need to update lines above the top of the window.
1743 */
1744 if (from < wp->w_topline)
1745 from = wp->w_topline;
1746
1747 /*
1748 * If we know the value of w_botline, use it to restrict the update to
1749 * the lines that are visible in the window.
1750 */
1751 if (wp->w_valid & VALID_BOTLINE)
1752 {
1753 if (from >= wp->w_botline)
1754 from = wp->w_botline - 1;
1755 if (to >= wp->w_botline)
1756 to = wp->w_botline - 1;
1757 }
1758
1759 /*
1760 * Find the minimal part to be updated.
1761 * Watch out for scrolling that made entries in w_lines[] invalid.
1762 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1763 * top_end; need to redraw from top_end to the "to" line.
1764 * A middle mouse click with a Visual selection may change the text
1765 * above the Visual area and reset wl_valid, do count these for
1766 * mid_end (in srow).
1767 */
1768 if (mid_start > 0)
1769 {
1770 lnum = wp->w_topline;
1771 idx = 0;
1772 srow = 0;
1773 if (scrolled_down)
1774 mid_start = top_end;
1775 else
1776 mid_start = 0;
1777 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1778 {
1779 if (wp->w_lines[idx].wl_valid)
1780 mid_start += wp->w_lines[idx].wl_size;
1781 else if (!scrolled_down)
1782 srow += wp->w_lines[idx].wl_size;
1783 ++idx;
1784# ifdef FEAT_FOLDING
1785 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1786 lnum = wp->w_lines[idx].wl_lnum;
1787 else
1788# endif
1789 ++lnum;
1790 }
1791 srow += mid_start;
1792 mid_end = wp->w_height;
1793 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1794 {
1795 if (wp->w_lines[idx].wl_valid
1796 && wp->w_lines[idx].wl_lnum >= to + 1)
1797 {
1798 /* Only update until first row of this line */
1799 mid_end = srow;
1800 break;
1801 }
1802 srow += wp->w_lines[idx].wl_size;
1803 }
1804 }
1805 }
1806
1807 if (VIsual_active && buf == curwin->w_buffer)
1808 {
1809 wp->w_old_visual_mode = VIsual_mode;
1810 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1811 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001812 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 wp->w_old_curswant = curwin->w_curswant;
1814 }
1815 else
1816 {
1817 wp->w_old_visual_mode = 0;
1818 wp->w_old_cursor_lnum = 0;
1819 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001820 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1824 /* reset got_int, otherwise regexp won't work */
1825 save_got_int = got_int;
1826 got_int = 0;
1827#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001828#ifdef SYN_TIME_LIMIT
1829 /* Set the time limit to 'redrawtime'. */
1830 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001831 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#ifdef FEAT_FOLDING
1834 win_foldinfo.fi_level = 0;
1835#endif
1836
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001837#ifdef FEAT_MENU
1838 /*
1839 * Draw the window toolbar, if there is one.
1840 * TODO: only when needed.
1841 */
1842 if (winbar_height(wp) > 0)
1843 redraw_win_toolbar(wp);
1844#endif
1845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 /*
1847 * Update all the window rows.
1848 */
1849 idx = 0; /* first entry in w_lines[].wl_size */
1850 row = 0;
1851 srow = 0;
1852 lnum = wp->w_topline; /* first line shown in window */
1853 for (;;)
1854 {
1855 /* stop updating when reached the end of the window (check for _past_
1856 * the end of the window is at the end of the loop) */
1857 if (row == wp->w_height)
1858 {
1859 didline = TRUE;
1860 break;
1861 }
1862
1863 /* stop updating when hit the end of the file */
1864 if (lnum > buf->b_ml.ml_line_count)
1865 {
1866 eof = TRUE;
1867 break;
1868 }
1869
1870 /* Remember the starting row of the line that is going to be dealt
1871 * with. It is used further down when the line doesn't fit. */
1872 srow = row;
1873
1874 /*
1875 * Update a line when it is in an area that needs updating, when it
1876 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001877 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 * win_del_lines(), check if the current line includes it.
1879 * When syntax folding is being used, the saved syntax states will
1880 * already have been updated, we can't see where the syntax state is
1881 * the same again, just update until the end of the window.
1882 */
1883 if (row < top_end
1884 || (row >= mid_start && row < mid_end)
1885#ifdef FEAT_SEARCH_EXTRA
1886 || top_to_mod
1887#endif
1888 || idx >= wp->w_lines_valid
1889 || (row + wp->w_lines[idx].wl_size > bot_start)
1890 || (mod_top != 0
1891 && (lnum == mod_top
1892 || (lnum >= mod_top
1893 && (lnum < mod_bot
1894#ifdef FEAT_SYN_HL
1895 || did_update == DID_FOLD
1896 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001897 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 && (
1899# ifdef FEAT_FOLDING
1900 (foldmethodIsSyntax(wp)
1901 && hasAnyFolding(wp)) ||
1902# endif
1903 syntax_check_changed(lnum)))
1904#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001905#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001906 /* match in fixed position might need redraw
1907 * if lines were inserted or deleted */
1908 || (wp->w_match_head != NULL
1909 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 )))))
1912 {
1913#ifdef FEAT_SEARCH_EXTRA
1914 if (lnum == mod_top)
1915 top_to_mod = FALSE;
1916#endif
1917
1918 /*
1919 * When at start of changed lines: May scroll following lines
1920 * up or down to minimize redrawing.
1921 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001922 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
1924 if (lnum == mod_top
1925 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001926 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
1928 int old_rows = 0;
1929 int new_rows = 0;
1930 int xtra_rows;
1931 linenr_T l;
1932
1933 /* Count the old number of window rows, using w_lines[], which
1934 * should still contain the sizes for the lines as they are
1935 * currently displayed. */
1936 for (i = idx; i < wp->w_lines_valid; ++i)
1937 {
1938 /* Only valid lines have a meaningful wl_lnum. Invalid
1939 * lines are part of the changed area. */
1940 if (wp->w_lines[i].wl_valid
1941 && wp->w_lines[i].wl_lnum == mod_bot)
1942 break;
1943 old_rows += wp->w_lines[i].wl_size;
1944#ifdef FEAT_FOLDING
1945 if (wp->w_lines[i].wl_valid
1946 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1947 {
1948 /* Must have found the last valid entry above mod_bot.
1949 * Add following invalid entries. */
1950 ++i;
1951 while (i < wp->w_lines_valid
1952 && !wp->w_lines[i].wl_valid)
1953 old_rows += wp->w_lines[i++].wl_size;
1954 break;
1955 }
1956#endif
1957 }
1958
1959 if (i >= wp->w_lines_valid)
1960 {
1961 /* We can't find a valid line below the changed lines,
1962 * need to redraw until the end of the window.
1963 * Inserting/deleting lines has no use. */
1964 bot_start = 0;
1965 }
1966 else
1967 {
1968 /* Able to count old number of rows: Count new window
1969 * rows, and may insert/delete lines */
1970 j = idx;
1971 for (l = lnum; l < mod_bot; ++l)
1972 {
1973#ifdef FEAT_FOLDING
1974 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1975 ++new_rows;
1976 else
1977#endif
1978#ifdef FEAT_DIFF
1979 if (l == wp->w_topline)
1980 new_rows += plines_win_nofill(wp, l, TRUE)
1981 + wp->w_topfill;
1982 else
1983#endif
1984 new_rows += plines_win(wp, l, TRUE);
1985 ++j;
1986 if (new_rows > wp->w_height - row - 2)
1987 {
1988 /* it's getting too much, must redraw the rest */
1989 new_rows = 9999;
1990 break;
1991 }
1992 }
1993 xtra_rows = new_rows - old_rows;
1994 if (xtra_rows < 0)
1995 {
1996 /* May scroll text up. If there is not enough
1997 * remaining text or scrolling fails, must redraw the
1998 * rest. If scrolling works, must redraw the text
1999 * below the scrolled text. */
2000 if (row - xtra_rows >= wp->w_height - 2)
2001 mod_bot = MAXLNUM;
2002 else
2003 {
2004 check_for_delay(FALSE);
2005 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002006 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 mod_bot = MAXLNUM;
2008 else
2009 bot_start = wp->w_height + xtra_rows;
2010 }
2011 }
2012 else if (xtra_rows > 0)
2013 {
2014 /* May scroll text down. If there is not enough
2015 * remaining text of scrolling fails, must redraw the
2016 * rest. */
2017 if (row + xtra_rows >= wp->w_height - 2)
2018 mod_bot = MAXLNUM;
2019 else
2020 {
2021 check_for_delay(FALSE);
2022 if (win_ins_lines(wp, row + old_rows,
2023 xtra_rows, FALSE, FALSE) == FAIL)
2024 mod_bot = MAXLNUM;
2025 else if (top_end > row + old_rows)
2026 /* Scrolled the part at the top that requires
2027 * updating down. */
2028 top_end += xtra_rows;
2029 }
2030 }
2031
2032 /* When not updating the rest, may need to move w_lines[]
2033 * entries. */
2034 if (mod_bot != MAXLNUM && i != j)
2035 {
2036 if (j < i)
2037 {
2038 int x = row + new_rows;
2039
2040 /* move entries in w_lines[] upwards */
2041 for (;;)
2042 {
2043 /* stop at last valid entry in w_lines[] */
2044 if (i >= wp->w_lines_valid)
2045 {
2046 wp->w_lines_valid = j;
2047 break;
2048 }
2049 wp->w_lines[j] = wp->w_lines[i];
2050 /* stop at a line that won't fit */
2051 if (x + (int)wp->w_lines[j].wl_size
2052 > wp->w_height)
2053 {
2054 wp->w_lines_valid = j + 1;
2055 break;
2056 }
2057 x += wp->w_lines[j++].wl_size;
2058 ++i;
2059 }
2060 if (bot_start > x)
2061 bot_start = x;
2062 }
2063 else /* j > i */
2064 {
2065 /* move entries in w_lines[] downwards */
2066 j -= i;
2067 wp->w_lines_valid += j;
2068 if (wp->w_lines_valid > wp->w_height)
2069 wp->w_lines_valid = wp->w_height;
2070 for (i = wp->w_lines_valid; i - j >= idx; --i)
2071 wp->w_lines[i] = wp->w_lines[i - j];
2072
2073 /* The w_lines[] entries for inserted lines are
2074 * now invalid, but wl_size may be used above.
2075 * Reset to zero. */
2076 while (i >= idx)
2077 {
2078 wp->w_lines[i].wl_size = 0;
2079 wp->w_lines[i--].wl_valid = FALSE;
2080 }
2081 }
2082 }
2083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085
2086#ifdef FEAT_FOLDING
2087 /*
2088 * When lines are folded, display one line for all of them.
2089 * Otherwise, display normally (can be several display lines when
2090 * 'wrap' is on).
2091 */
2092 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2093 if (fold_count != 0)
2094 {
2095 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2096 ++row;
2097 --fold_count;
2098 wp->w_lines[idx].wl_folded = TRUE;
2099 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2100# ifdef FEAT_SYN_HL
2101 did_update = DID_FOLD;
2102# endif
2103 }
2104 else
2105#endif
2106 if (idx < wp->w_lines_valid
2107 && wp->w_lines[idx].wl_valid
2108 && wp->w_lines[idx].wl_lnum == lnum
2109 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002110 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 && srow + wp->w_lines[idx].wl_size > wp->w_height
2112#ifdef FEAT_DIFF
2113 && diff_check_fill(wp, lnum) == 0
2114#endif
2115 )
2116 {
2117 /* This line is not going to fit. Don't draw anything here,
2118 * will draw "@ " lines below. */
2119 row = wp->w_height + 1;
2120 }
2121 else
2122 {
2123#ifdef FEAT_SEARCH_EXTRA
2124 prepare_search_hl(wp, lnum);
2125#endif
2126#ifdef FEAT_SYN_HL
2127 /* Let the syntax stuff know we skipped a few lines. */
2128 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002129 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 syntax_end_parsing(syntax_last_parsed + 1);
2131#endif
2132
2133 /*
2134 * Display one line.
2135 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002136 row = win_line(wp, lnum, srow, wp->w_height,
2137 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
2139#ifdef FEAT_FOLDING
2140 wp->w_lines[idx].wl_folded = FALSE;
2141 wp->w_lines[idx].wl_lastlnum = lnum;
2142#endif
2143#ifdef FEAT_SYN_HL
2144 did_update = DID_LINE;
2145 syntax_last_parsed = lnum;
2146#endif
2147 }
2148
2149 wp->w_lines[idx].wl_lnum = lnum;
2150 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002151
2152 /* Past end of the window or end of the screen. Note that after
2153 * resizing wp->w_height may be end up too big. That's a problem
2154 * elsewhere, but prevent a crash here. */
2155 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002158 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2160 ++idx;
2161 break;
2162 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002163 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 wp->w_lines[idx].wl_size = row - srow;
2165 ++idx;
2166#ifdef FEAT_FOLDING
2167 lnum += fold_count + 1;
2168#else
2169 ++lnum;
2170#endif
2171 }
2172 else
2173 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002174 if (wp->w_p_rnu)
2175 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002176#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002177 // 'relativenumber' set: The text doesn't need to be drawn, but
2178 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002179 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2180 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002181 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002182 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002183#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002184 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002185 }
2186
2187 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 row += wp->w_lines[idx++].wl_size;
2189 if (row > wp->w_height) /* past end of screen */
2190 break;
2191#ifdef FEAT_FOLDING
2192 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2193#else
2194 ++lnum;
2195#endif
2196#ifdef FEAT_SYN_HL
2197 did_update = DID_NONE;
2198#endif
2199 }
2200
2201 if (lnum > buf->b_ml.ml_line_count)
2202 {
2203 eof = TRUE;
2204 break;
2205 }
2206 }
2207 /*
2208 * End of loop over all window lines.
2209 */
2210
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002211#ifdef FEAT_VTP
2212 /* Rewrite the character at the end of the screen line. */
2213 if (use_vtp())
2214 {
2215 int i;
2216
2217 for (i = 0; i < Rows; ++i)
2218# ifdef FEAT_MBYTE
2219 if (enc_utf8)
2220 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2221 LineOffset[i] + screen_Columns) > 1)
2222 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2223 else
2224 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2225 else
2226# endif
2227 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2228 }
2229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
2231 if (idx > wp->w_lines_valid)
2232 wp->w_lines_valid = idx;
2233
2234#ifdef FEAT_SYN_HL
2235 /*
2236 * Let the syntax stuff know we stop parsing here.
2237 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002238 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 syntax_end_parsing(syntax_last_parsed + 1);
2240#endif
2241
2242 /*
2243 * If we didn't hit the end of the file, and we didn't finish the last
2244 * line we were working on, then the line didn't fit.
2245 */
2246 wp->w_empty_rows = 0;
2247#ifdef FEAT_DIFF
2248 wp->w_filler_rows = 0;
2249#endif
2250 if (!eof && !didline)
2251 {
2252 if (lnum == wp->w_topline)
2253 {
2254 /*
2255 * Single line that does not fit!
2256 * Don't overwrite it, it can be edited.
2257 */
2258 wp->w_botline = lnum + 1;
2259 }
2260#ifdef FEAT_DIFF
2261 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2262 {
2263 /* Window ends in filler lines. */
2264 wp->w_botline = lnum;
2265 wp->w_filler_rows = wp->w_height - srow;
2266 }
2267#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002268 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2269 {
2270 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2271
2272 /*
2273 * Last line isn't finished: Display "@@@" in the last screen line.
2274 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002275 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002276 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002277 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002278 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002279 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002280 set_empty_rows(wp, srow);
2281 wp->w_botline = lnum;
2282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2284 {
2285 /*
2286 * Last line isn't finished: Display "@@@" at the end.
2287 */
2288 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2289 W_WINROW(wp) + wp->w_height,
2290 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002291 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 set_empty_rows(wp, srow);
2293 wp->w_botline = lnum;
2294 }
2295 else
2296 {
2297 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2298 wp->w_botline = lnum;
2299 }
2300 }
2301 else
2302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (eof) /* we hit the end of the file */
2305 {
2306 wp->w_botline = buf->b_ml.ml_line_count + 1;
2307#ifdef FEAT_DIFF
2308 j = diff_check_fill(wp, wp->w_botline);
2309 if (j > 0 && !wp->w_botfill)
2310 {
2311 /*
2312 * Display filler lines at the end of the file
2313 */
2314 if (char2cells(fill_diff) > 1)
2315 i = '-';
2316 else
2317 i = fill_diff;
2318 if (row + j > wp->w_height)
2319 j = wp->w_height - row;
2320 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2321 row += j;
2322 }
2323#endif
2324 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002325 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 wp->w_botline = lnum;
2327
2328 /* make sure the rest of the screen is blank */
2329 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002330 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002333#ifdef SYN_TIME_LIMIT
2334 syn_set_timeout(NULL);
2335#endif
2336
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 /* Reset the type of redrawing required, the window has been updated. */
2338 wp->w_redr_type = 0;
2339#ifdef FEAT_DIFF
2340 wp->w_old_topfill = wp->w_topfill;
2341 wp->w_old_botfill = wp->w_botfill;
2342#endif
2343
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002344 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 /*
2347 * There is a trick with w_botline. If we invalidate it on each
2348 * change that might modify it, this will cause a lot of expensive
2349 * calls to plines() in update_topline() each time. Therefore the
2350 * value of w_botline is often approximated, and this value is used to
2351 * compute the value of w_topline. If the value of w_botline was
2352 * wrong, check that the value of w_topline is correct (cursor is on
2353 * the visible part of the text). If it's not, we need to redraw
2354 * again. Mostly this just means scrolling up a few lines, so it
2355 * doesn't look too bad. Only do this for the current window (where
2356 * changes are relevant).
2357 */
2358 wp->w_valid |= VALID_BOTLINE;
2359 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2360 {
2361 recursive = TRUE;
2362 curwin->w_valid &= ~VALID_TOPLINE;
2363 update_topline(); /* may invalidate w_botline again */
2364 if (must_redraw != 0)
2365 {
2366 /* Don't update for changes in buffer again. */
2367 i = curbuf->b_mod_set;
2368 curbuf->b_mod_set = FALSE;
2369 win_update(curwin);
2370 must_redraw = 0;
2371 curbuf->b_mod_set = i;
2372 }
2373 recursive = FALSE;
2374 }
2375 }
2376
2377#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2378 /* restore got_int, unless CTRL-C was hit while redrawing */
2379 if (!got_int)
2380 got_int = save_got_int;
2381#endif
2382}
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384/*
2385 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2386 * as the filler character.
2387 */
2388 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002389win_draw_end(
2390 win_T *wp,
2391 int c1,
2392 int c2,
2393 int row,
2394 int endrow,
2395 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2398 int n = 0;
2399# define FDC_OFF n
2400#else
2401# define FDC_OFF 0
2402#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002403#ifdef FEAT_FOLDING
2404 int fdc = compute_foldcolumn(wp, 0);
2405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407#ifdef FEAT_RIGHTLEFT
2408 if (wp->w_p_rl)
2409 {
2410 /* No check for cmdline window: should never be right-left. */
2411# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002412 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
2414 if (n > 0)
2415 {
2416 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002417 if (n > wp->w_width)
2418 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2420 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002421 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423# endif
2424# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002425 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 int nn = n + 2;
2428
2429 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002430 if (nn > wp->w_width)
2431 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2433 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002434 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 n = nn;
2436 }
2437# endif
2438 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002439 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002440 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2442 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002443 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
2445 else
2446#endif
2447 {
2448#ifdef FEAT_CMDWIN
2449 if (cmdwin_type != 0 && wp == curwin)
2450 {
2451 /* draw the cmdline character in the leftmost column */
2452 n = 1;
2453 if (n > wp->w_width)
2454 n = wp->w_width;
2455 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002456 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002457 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459#endif
2460#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002461 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002463 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002466 if (nn > wp->w_width)
2467 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002469 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002470 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 n = nn;
2472 }
2473#endif
2474#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002475 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
2477 int nn = n + 2;
2478
2479 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002480 if (nn > wp->w_width)
2481 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002483 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002484 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 n = nn;
2486 }
2487#endif
2488 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002489 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002490 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 set_empty_rows(wp, row);
2493}
2494
Bram Moolenaar1a384422010-07-14 19:53:30 +02002495#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002496/*
2497 * Advance **color_cols and return TRUE when there are columns to draw.
2498 */
2499 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002500advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002501{
2502 while (**color_cols >= 0 && vcol > **color_cols)
2503 ++*color_cols;
2504 return (**color_cols >= 0);
2505}
2506#endif
2507
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002508#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2509/*
2510 * Copy "text" to ScreenLines using "attr".
2511 * Returns the next screen column.
2512 */
2513 static int
2514text_to_screenline(win_T *wp, char_u *text, int col)
2515{
2516 int off = (int)(current_ScreenLine - ScreenLines);
2517
2518#ifdef FEAT_MBYTE
2519 if (has_mbyte)
2520 {
2521 int cells;
2522 int u8c, u8cc[MAX_MCO];
2523 int i;
2524 int idx;
2525 int c_len;
2526 char_u *p;
2527# ifdef FEAT_ARABIC
2528 int prev_c = 0; /* previous Arabic character */
2529 int prev_c1 = 0; /* first composing char for prev_c */
2530# endif
2531
2532# ifdef FEAT_RIGHTLEFT
2533 if (wp->w_p_rl)
2534 idx = off;
2535 else
2536# endif
2537 idx = off + col;
2538
2539 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2540 for (p = text; *p != NUL; )
2541 {
2542 cells = (*mb_ptr2cells)(p);
2543 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002544 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002545# ifdef FEAT_RIGHTLEFT
2546 - (wp->w_p_rl ? col : 0)
2547# endif
2548 )
2549 break;
2550 ScreenLines[idx] = *p;
2551 if (enc_utf8)
2552 {
2553 u8c = utfc_ptr2char(p, u8cc);
2554 if (*p < 0x80 && u8cc[0] == 0)
2555 {
2556 ScreenLinesUC[idx] = 0;
2557#ifdef FEAT_ARABIC
2558 prev_c = u8c;
2559#endif
2560 }
2561 else
2562 {
2563#ifdef FEAT_ARABIC
2564 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2565 {
2566 /* Do Arabic shaping. */
2567 int pc, pc1, nc;
2568 int pcc[MAX_MCO];
2569 int firstbyte = *p;
2570
2571 /* The idea of what is the previous and next
2572 * character depends on 'rightleft'. */
2573 if (wp->w_p_rl)
2574 {
2575 pc = prev_c;
2576 pc1 = prev_c1;
2577 nc = utf_ptr2char(p + c_len);
2578 prev_c1 = u8cc[0];
2579 }
2580 else
2581 {
2582 pc = utfc_ptr2char(p + c_len, pcc);
2583 nc = prev_c;
2584 pc1 = pcc[0];
2585 }
2586 prev_c = u8c;
2587
2588 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2589 pc, pc1, nc);
2590 ScreenLines[idx] = firstbyte;
2591 }
2592 else
2593 prev_c = u8c;
2594#endif
2595 /* Non-BMP character: display as ? or fullwidth ?. */
2596#ifdef UNICODE16
2597 if (u8c >= 0x10000)
2598 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2599 else
2600#endif
2601 ScreenLinesUC[idx] = u8c;
2602 for (i = 0; i < Screen_mco; ++i)
2603 {
2604 ScreenLinesC[i][idx] = u8cc[i];
2605 if (u8cc[i] == 0)
2606 break;
2607 }
2608 }
2609 if (cells > 1)
2610 ScreenLines[idx + 1] = 0;
2611 }
2612 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2613 /* double-byte single width character */
2614 ScreenLines2[idx] = p[1];
2615 else if (cells > 1)
2616 /* double-width character */
2617 ScreenLines[idx + 1] = p[1];
2618 col += cells;
2619 idx += cells;
2620 p += c_len;
2621 }
2622 }
2623 else
2624#endif
2625 {
2626 int len = (int)STRLEN(text);
2627
Bram Moolenaar02631462017-09-22 15:20:32 +02002628 if (len > wp->w_width - col)
2629 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002630 if (len > 0)
2631 {
2632#ifdef FEAT_RIGHTLEFT
2633 if (wp->w_p_rl)
2634 STRNCPY(current_ScreenLine, text, len);
2635 else
2636#endif
2637 STRNCPY(current_ScreenLine + col, text, len);
2638 col += len;
2639 }
2640 }
2641 return col;
2642}
2643#endif
2644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645#ifdef FEAT_FOLDING
2646/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002647 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2648 * space is available for window "wp", minus "col".
2649 */
2650 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002651compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002652{
2653 int fdc = wp->w_p_fdc;
2654 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002655 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002656
2657 if (fdc > wwidth - (col + wmw))
2658 fdc = wwidth - (col + wmw);
2659 return fdc;
2660}
2661
2662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 * Display one folded line.
2664 */
2665 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002666fold_line(
2667 win_T *wp,
2668 long fold_count,
2669 foldinfo_T *foldinfo,
2670 linenr_T lnum,
2671 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002673 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 pos_T *top, *bot;
2675 linenr_T lnume = lnum + fold_count - 1;
2676 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002677 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 int col;
2680 int txtcol;
2681 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002682 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684 /* Build the fold line:
2685 * 1. Add the cmdwin_type for the command-line window
2686 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002687 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 * 4. Compose the text
2689 * 5. Add the text
2690 * 6. set highlighting for the Visual area an other text
2691 */
2692 col = 0;
2693
2694 /*
2695 * 1. Add the cmdwin_type for the command-line window
2696 * Ignores 'rightleft', this window is never right-left.
2697 */
2698#ifdef FEAT_CMDWIN
2699 if (cmdwin_type != 0 && wp == curwin)
2700 {
2701 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002702 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703#ifdef FEAT_MBYTE
2704 if (enc_utf8)
2705 ScreenLinesUC[off] = 0;
2706#endif
2707 ++col;
2708 }
2709#endif
2710
2711 /*
2712 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002713 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002715 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (fdc > 0)
2717 {
2718 fill_foldcolumn(buf, wp, TRUE, lnum);
2719#ifdef FEAT_RIGHTLEFT
2720 if (wp->w_p_rl)
2721 {
2722 int i;
2723
Bram Moolenaar02631462017-09-22 15:20:32 +02002724 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002725 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 /* reverse the fold column */
2727 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002728 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 else
2731#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002732 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 col += fdc;
2734 }
2735
2736#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002737# define RL_MEMSET(p, v, l) \
2738 do { \
2739 if (wp->w_p_rl) \
2740 for (ri = 0; ri < l; ++ri) \
2741 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2742 else \
2743 for (ri = 0; ri < l; ++ri) \
2744 ScreenAttrs[off + (p) + ri] = v; \
2745 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002747# define RL_MEMSET(p, v, l) \
2748 do { \
2749 for (ri = 0; ri < l; ++ri) \
2750 ScreenAttrs[off + (p) + ri] = v; \
2751 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752#endif
2753
Bram Moolenaar64486672010-05-16 15:46:46 +02002754 /* Set all attributes of the 'number' or 'relativenumber' column and the
2755 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002756 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
2758#ifdef FEAT_SIGNS
2759 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002760 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002762 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (len > 0)
2764 {
2765 if (len > 2)
2766 len = 2;
2767# ifdef FEAT_RIGHTLEFT
2768 if (wp->w_p_rl)
2769 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002770 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002771 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
2773# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002774 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 col += len;
2776 }
2777 }
2778#endif
2779
2780 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002781 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002783 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002785 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (len > 0)
2787 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002788 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002789 long num;
2790 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002791
2792 if (len > w + 1)
2793 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002794
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002795 if (wp->w_p_nu && !wp->w_p_rnu)
2796 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002797 num = (long)lnum;
2798 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002799 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002800 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002801 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002802 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002803 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002804 /* 'number' + 'relativenumber': cursor line shows absolute
2805 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002806 num = lnum;
2807 fmt = "%-*ld ";
2808 }
2809 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002810
Bram Moolenaar700e7342013-01-30 12:31:36 +01002811 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812#ifdef FEAT_RIGHTLEFT
2813 if (wp->w_p_rl)
2814 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002815 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002816 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 else
2818#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002819 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 col += len;
2821 }
2822 }
2823
2824 /*
2825 * 4. Compose the folded-line string with 'foldtext', if set.
2826 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002827 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
2829 txtcol = col; /* remember where text starts */
2830
2831 /*
2832 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2833 * Right-left text is put in columns 0 - number-col, normal text is put
2834 * in columns number-col - window-width.
2835 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002836 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 /* Fill the rest of the line with the fold filler */
2839#ifdef FEAT_RIGHTLEFT
2840 if (wp->w_p_rl)
2841 col -= txtcol;
2842#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002843 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844#ifdef FEAT_RIGHTLEFT
2845 - (wp->w_p_rl ? txtcol : 0)
2846#endif
2847 )
2848 {
2849#ifdef FEAT_MBYTE
2850 if (enc_utf8)
2851 {
2852 if (fill_fold >= 0x80)
2853 {
2854 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002855 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002856 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002861 ScreenLines[off + col] = fill_fold;
2862 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002863 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002865 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002867 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869
2870 if (text != buf)
2871 vim_free(text);
2872
2873 /*
2874 * 6. set highlighting for the Visual area an other text.
2875 * If all folded lines are in the Visual area, highlight the line.
2876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2878 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002879 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 {
2881 /* Visual is after curwin->w_cursor */
2882 top = &curwin->w_cursor;
2883 bot = &VIsual;
2884 }
2885 else
2886 {
2887 /* Visual is before curwin->w_cursor */
2888 top = &VIsual;
2889 bot = &curwin->w_cursor;
2890 }
2891 if (lnum >= top->lnum
2892 && lnume <= bot->lnum
2893 && (VIsual_mode != 'v'
2894 || ((lnum > top->lnum
2895 || (lnum == top->lnum
2896 && top->col == 0))
2897 && (lnume < bot->lnum
2898 || (lnume == bot->lnum
2899 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002900 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
2902 if (VIsual_mode == Ctrl_V)
2903 {
2904 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002905 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002907 if (wp->w_old_cursor_lcol != MAXCOL
2908 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 len = wp->w_old_cursor_lcol;
2911 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002912 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002913 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002914 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916 }
2917 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002920 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 }
2923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002925#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002926 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2927 if (wp->w_p_cc_cols)
2928 {
2929 int i = 0;
2930 int j = wp->w_p_cc_cols[i];
2931 int old_txtcol = txtcol;
2932
2933 while (j > -1)
2934 {
2935 txtcol += j;
2936 if (wp->w_p_wrap)
2937 txtcol -= wp->w_skipcol;
2938 else
2939 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002940 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002941 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002942 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002943 txtcol = old_txtcol;
2944 j = wp->w_p_cc_cols[++i];
2945 }
2946 }
2947
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002948 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002949 if (wp->w_p_cuc)
2950 {
2951 txtcol += wp->w_virtcol;
2952 if (wp->w_p_wrap)
2953 txtcol -= wp->w_skipcol;
2954 else
2955 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002956 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002957 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002958 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002959 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
Bram Moolenaar02631462017-09-22 15:20:32 +02002962 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2963 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
2965 /*
2966 * Update w_cline_height and w_cline_folded if the cursor line was
2967 * updated (saves a call to plines() later).
2968 */
2969 if (wp == curwin
2970 && lnum <= curwin->w_cursor.lnum
2971 && lnume >= curwin->w_cursor.lnum)
2972 {
2973 curwin->w_cline_row = row;
2974 curwin->w_cline_height = 1;
2975 curwin->w_cline_folded = TRUE;
2976 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2977 }
2978}
2979
2980/*
2981 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2982 */
2983 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002984copy_text_attr(
2985 int off,
2986 char_u *buf,
2987 int len,
2988 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002990 int i;
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 mch_memmove(ScreenLines + off, buf, (size_t)len);
2993# ifdef FEAT_MBYTE
2994 if (enc_utf8)
2995 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2996# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002997 for (i = 0; i < len; ++i)
2998 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999}
3000
3001/*
3002 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003003 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 */
3005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003006fill_foldcolumn(
3007 char_u *p,
3008 win_T *wp,
3009 int closed, /* TRUE of FALSE */
3010 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 int i = 0;
3013 int level;
3014 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003015 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003016 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
3018 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003019 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
3021 level = win_foldinfo.fi_level;
3022 if (level > 0)
3023 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003024 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003025 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003026
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 /* If the column is too narrow, we start at the lowest level that
3028 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003029 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (first_level < 1)
3031 first_level = 1;
3032
Bram Moolenaar1c934292015-01-27 16:39:29 +01003033 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 {
3035 if (win_foldinfo.fi_lnum == lnum
3036 && first_level + i >= win_foldinfo.fi_low_level)
3037 p[i] = '-';
3038 else if (first_level == 1)
3039 p[i] = '|';
3040 else if (first_level + i <= 9)
3041 p[i] = '0' + first_level + i;
3042 else
3043 p[i] = '>';
3044 if (first_level + i == level)
3045 break;
3046 }
3047 }
3048 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003049 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050}
3051#endif /* FEAT_FOLDING */
3052
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003053#ifdef FEAT_TEXT_PROP
3054static textprop_T *current_text_props = NULL;
3055static buf_T *current_buf = NULL;
3056
3057 static int
3058#ifdef __BORLANDC__
3059_RTLENTRYF
3060#endif
3061text_prop_compare(const void *s1, const void *s2)
3062{
3063 int idx1, idx2;
3064 proptype_T *pt1, *pt2;
3065 colnr_T col1, col2;
3066
3067 idx1 = *(int *)s1;
3068 idx2 = *(int *)s2;
3069 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3070 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3071 if (pt1 == pt2)
3072 return 0;
3073 if (pt1 == NULL)
3074 return -1;
3075 if (pt2 == NULL)
3076 return 1;
3077 if (pt1->pt_priority != pt2->pt_priority)
3078 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3079 col1 = current_text_props[idx1].tp_col;
3080 col2 = current_text_props[idx2].tp_col;
3081 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3082}
3083#endif
3084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085/*
3086 * Display line "lnum" of window 'wp' on the screen.
3087 * Start at row "startrow", stop when "endrow" is reached.
3088 * wp->w_virtcol needs to be valid.
3089 *
3090 * Return the number of last row the line occupies.
3091 */
3092 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003093win_line(
3094 win_T *wp,
3095 linenr_T lnum,
3096 int startrow,
3097 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003098 int nochange UNUSED, // not updating for changed text
3099 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003101 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3103 int c = 0; /* init for GCC */
3104 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003105#ifdef FEAT_LINEBREAK
3106 long vcol_sbr = -1; /* virtual column after showbreak */
3107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 long vcol_prev = -1; /* "vcol" of previous character */
3109 char_u *line; /* current line */
3110 char_u *ptr; /* current position in "line" */
3111 int row; /* row in the window, excl w_winrow */
3112 int screen_row; /* row on the screen, incl w_winrow */
3113
3114 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3115 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003116 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003117 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 int c_extra = NUL; /* extra chars, all the same */
3119 int extra_attr = 0; /* attributes when n_extra != 0 */
3120 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3121 displaying lcs_eol at end-of-line */
3122 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3123 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3124
3125 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3126 int saved_n_extra = 0;
3127 char_u *saved_p_extra = NULL;
3128 int saved_c_extra = 0;
3129 int saved_char_attr = 0;
3130
3131 int n_attr = 0; /* chars with special attr */
3132 int saved_attr2 = 0; /* char_attr saved for n_attr */
3133 int n_attr3 = 0; /* chars with overruling special attr */
3134 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3135
3136 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3137
3138 int fromcol, tocol; /* start/end of inverting */
3139 int fromcol_prev = -2; /* start of inverting after cursor */
3140 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003142 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 pos_T pos;
3144 long v;
3145
3146 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003147 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3149 in this line */
3150 int attr = 0; /* attributes for area highlighting */
3151 int area_attr = 0; /* attributes desired by highlighting */
3152 int search_attr = 0; /* attributes desired by 'hlsearch' */
3153#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003154 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 int syntax_attr = 0; /* attributes desired by syntax */
3156 int has_syntax = FALSE; /* this buffer has syntax highl. */
3157 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003158 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003159 int draw_color_col = FALSE; /* highlight colorcolumn */
3160 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003161#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003162#ifdef FEAT_TEXT_PROP
3163 int text_prop_count;
3164 int text_prop_next = 0; // next text property to use
3165 textprop_T *text_props = NULL;
3166 int *text_prop_idxs = NULL;
3167 int text_props_active = 0;
3168 proptype_T *text_prop_type = NULL;
3169 int text_prop_attr = 0;
3170#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003171#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003172 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003173# define SPWORDLEN 150
3174 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003175 int nextlinecol = 0; /* column where nextline[] starts */
3176 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003177 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003178 int spell_attr = 0; /* attributes desired by spelling */
3179 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003180 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3181 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003182 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003183 static int cap_col = -1; /* column to check for Cap word */
3184 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003185 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003187 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188#ifdef FEAT_MBYTE
3189 int multi_attr = 0; /* attributes desired by multibyte */
3190 int mb_l = 1; /* multi-byte byte length */
3191 int mb_c = 0; /* decoded multi-byte character */
3192 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003193 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194#endif
3195#ifdef FEAT_DIFF
3196 int filler_lines; /* nr of filler lines to be drawn */
3197 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003198 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 int change_start = MAXCOL; /* first col of changed area */
3200 int change_end = -1; /* last col of changed area */
3201#endif
3202 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3203#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003204 int need_showbreak = FALSE; /* overlong line, skipping first x
3205 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003207#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003208 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003210 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211#endif
3212#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003213 matchitem_T *cur; /* points to the match list */
3214 match_T *shl; /* points to search_hl or a match */
3215 int shl_flag; /* flag to indicate whether search_hl
3216 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003217 int pos_inprogress; /* marks that position match search is
3218 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003219 int prevcol_hl_flag; /* flag to indicate whether prevcol
3220 equals startcol of search_hl or one
3221 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222#endif
3223#ifdef FEAT_ARABIC
3224 int prev_c = 0; /* previous Arabic character */
3225 int prev_c1 = 0; /* first composing char for prev_c */
3226#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003227#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003228 int did_line_attr = 0;
3229#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003230#ifdef FEAT_TERMINAL
3231 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003232 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234
3235 /* draw_state: items that are drawn in sequence: */
3236#define WL_START 0 /* nothing done yet */
3237#ifdef FEAT_CMDWIN
3238# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3239#else
3240# define WL_CMDLINE WL_START
3241#endif
3242#ifdef FEAT_FOLDING
3243# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3244#else
3245# define WL_FOLD WL_CMDLINE
3246#endif
3247#ifdef FEAT_SIGNS
3248# define WL_SIGN WL_FOLD + 1 /* column for signs */
3249#else
3250# define WL_SIGN WL_FOLD /* column for signs */
3251#endif
3252#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003253#ifdef FEAT_LINEBREAK
3254# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003256# define WL_BRI WL_NR
3257#endif
3258#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3259# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3260#else
3261# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#endif
3263#define WL_LINE WL_SBR + 1 /* text in the line */
3264 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003265#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 int feedback_col = 0;
3267 int feedback_old_attr = -1;
3268#endif
3269
Bram Moolenaar860cae12010-06-05 23:22:07 +02003270#ifdef FEAT_CONCEAL
3271 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003272 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003273 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003274 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003275 int is_concealing = FALSE;
3276 int boguscols = 0; /* nonexistent columns added to force
3277 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003278 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003279 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003280 int match_conc = 0; /* cchar for match functions */
3281 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003282 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003283# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003284# define FIX_FOR_BOGUSCOLS \
3285 { \
3286 n_extra += vcol_off; \
3287 vcol -= vcol_off; \
3288 vcol_off = 0; \
3289 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003290 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003291 boguscols = 0; \
3292 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003293#else
3294# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
3297 if (startrow > endrow) /* past the end already! */
3298 return startrow;
3299
3300 row = startrow;
3301 screen_row = row + W_WINROW(wp);
3302
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003303 if (!number_only)
3304 {
3305 /*
3306 * To speed up the loop below, set extra_check when there is linebreak,
3307 * trailing white space and/or syntax processing to be done.
3308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003310 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#endif
3312#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003313 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003314# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003315 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003316# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003317 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003319 /* Prepare for syntax highlighting in this line. When there is an
3320 * error, stop syntax highlighting. */
3321 save_did_emsg = did_emsg;
3322 did_emsg = FALSE;
3323 syntax_start(wp, lnum);
3324 if (did_emsg)
3325 wp->w_s->b_syn_error = TRUE;
3326 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003327 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003328 did_emsg = save_did_emsg;
3329#ifdef SYN_TIME_LIMIT
3330 if (!wp->w_s->b_syn_slow)
3331#endif
3332 {
3333 has_syntax = TRUE;
3334 extra_check = TRUE;
3335 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003338
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003339 /* Check for columns to display for 'colorcolumn'. */
3340 color_cols = wp->w_p_cc_cols;
3341 if (color_cols != NULL)
3342 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003343#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003344
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003345#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003346 if (term_show_buffer(wp->w_buffer))
3347 {
3348 extra_check = TRUE;
3349 get_term_attr = TRUE;
3350 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3351 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003352#endif
3353
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003354#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003355 if (wp->w_p_spell
3356 && *wp->w_s->b_p_spl != NUL
3357 && wp->w_s->b_langp.ga_len > 0
3358 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003359 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003360 /* Prepare for spell checking. */
3361 has_spell = TRUE;
3362 extra_check = TRUE;
3363
Bram Moolenaar7701f302018-10-02 21:20:32 +02003364 /* Get the start of the next line, so that words that wrap to the
3365 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003366 * Trick: skip a few chars for C/shell/Vim comments */
3367 nextline[SPWORDLEN] = NUL;
3368 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3369 {
3370 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3371 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3372 }
3373
Bram Moolenaar7701f302018-10-02 21:20:32 +02003374 /* When a word wrapped from the previous line the start of the
3375 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003376 if (lnum == checked_lnum)
3377 cur_checked_col = checked_col;
3378 checked_lnum = 0;
3379
3380 /* When there was a sentence end in the previous line may require a
3381 * word starting with capital in this line. In line 1 always check
3382 * the first word. */
3383 if (lnum != capcol_lnum)
3384 cap_col = -1;
3385 if (lnum == 1)
3386 cap_col = 0;
3387 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#endif
3390
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003391 /*
3392 * handle visual active in this window
3393 */
3394 fromcol = -10;
3395 tocol = MAXCOL;
3396 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003398 /* Visual is after curwin->w_cursor */
3399 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003401 top = &curwin->w_cursor;
3402 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003404 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003406 top = &VIsual;
3407 bot = &curwin->w_cursor;
3408 }
3409 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3410 if (VIsual_mode == Ctrl_V) /* block mode */
3411 {
3412 if (lnum_in_visual_area)
3413 {
3414 fromcol = wp->w_old_cursor_fcol;
3415 tocol = wp->w_old_cursor_lcol;
3416 }
3417 }
3418 else /* non-block mode */
3419 {
3420 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003422 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003424 if (VIsual_mode == 'V') /* linewise */
3425 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 else
3427 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003428 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3429 if (gchar_pos(top) == NUL)
3430 tocol = fromcol + 1;
3431 }
3432 }
3433 if (VIsual_mode != 'V' && lnum == bot->lnum)
3434 {
3435 if (*p_sel == 'e' && bot->col == 0
3436#ifdef FEAT_VIRTUALEDIT
3437 && bot->coladd == 0
3438#endif
3439 )
3440 {
3441 fromcol = -10;
3442 tocol = MAXCOL;
3443 }
3444 else if (bot->col == MAXCOL)
3445 tocol = MAXCOL;
3446 else
3447 {
3448 pos = *bot;
3449 if (*p_sel == 'e')
3450 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3451 else
3452 {
3453 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3454 ++tocol;
3455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
3457 }
3458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003460 /* Check if the character under the cursor should not be inverted */
3461 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003462#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003463 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003464#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003465 )
3466 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003468 /* if inverting in this line set area_highlighting */
3469 if (fromcol >= 0)
3470 {
3471 area_highlighting = TRUE;
3472 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003474 if ((clip_star.available && !clip_star.owned
3475 && clip_isautosel_star())
3476 || (clip_plus.available && !clip_plus.owned
3477 && clip_isautosel_plus()))
3478 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003483 /*
3484 * handle 'incsearch' and ":s///c" highlighting
3485 */
3486 else if (highlight_match
3487 && wp == curwin
3488 && lnum >= curwin->w_cursor.lnum
3489 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003491 if (lnum == curwin->w_cursor.lnum)
3492 getvcol(curwin, &(curwin->w_cursor),
3493 (colnr_T *)&fromcol, NULL, NULL);
3494 else
3495 fromcol = 0;
3496 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3497 {
3498 pos.lnum = lnum;
3499 pos.col = search_match_endcol;
3500 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3501 }
3502 else
3503 tocol = MAXCOL;
3504 /* do at least one character; happens when past end of line */
3505 if (fromcol == tocol)
3506 tocol = fromcol + 1;
3507 area_highlighting = TRUE;
3508 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511
3512#ifdef FEAT_DIFF
3513 filler_lines = diff_check(wp, lnum);
3514 if (filler_lines < 0)
3515 {
3516 if (filler_lines == -1)
3517 {
3518 if (diff_find_change(wp, lnum, &change_start, &change_end))
3519 diff_hlf = HLF_ADD; /* added line */
3520 else if (change_start == 0)
3521 diff_hlf = HLF_TXD; /* changed text */
3522 else
3523 diff_hlf = HLF_CHD; /* changed line */
3524 }
3525 else
3526 diff_hlf = HLF_ADD; /* added line */
3527 filler_lines = 0;
3528 area_highlighting = TRUE;
3529 }
3530 if (lnum == wp->w_topline)
3531 filler_lines = wp->w_topfill;
3532 filler_todo = filler_lines;
3533#endif
3534
3535#ifdef LINE_ATTR
3536# ifdef FEAT_SIGNS
3537 /* If this line has a sign with line highlighting set line_attr. */
3538 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3539 if (v != 0)
3540 line_attr = sign_get_attr((int)v, TRUE);
3541# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003542# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003544 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003545 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546# endif
3547 if (line_attr != 0)
3548 area_highlighting = TRUE;
3549#endif
3550
3551 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3552 ptr = line;
3553
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003554#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003555 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003556 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003557 /* For checking first word with a capital skip white space. */
3558 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003559 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003560
Bram Moolenaar30abd282005-06-22 22:35:10 +00003561 /* To be able to spell-check over line boundaries copy the end of the
3562 * current line into nextline[]. Above the start of the next line was
3563 * copied to nextline[SPWORDLEN]. */
3564 if (nextline[SPWORDLEN] == NUL)
3565 {
3566 /* No next line or it is empty. */
3567 nextlinecol = MAXCOL;
3568 nextline_idx = 0;
3569 }
3570 else
3571 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003572 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003573 if (v < SPWORDLEN)
3574 {
3575 /* Short line, use it completely and append the start of the
3576 * next line. */
3577 nextlinecol = 0;
3578 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003579 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003580 nextline_idx = v + 1;
3581 }
3582 else
3583 {
3584 /* Long line, use only the last SPWORDLEN bytes. */
3585 nextlinecol = v - SPWORDLEN;
3586 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3587 nextline_idx = SPWORDLEN + 1;
3588 }
3589 }
3590 }
3591#endif
3592
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003593 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003595 if (lcs_space || lcs_trail)
3596 extra_check = TRUE;
3597 /* find start of trailing whitespace */
3598 if (lcs_trail)
3599 {
3600 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003601 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003602 --trailcol;
3603 trailcol += (colnr_T) (ptr - line);
3604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606
3607 /*
3608 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3609 * first character to be displayed.
3610 */
3611 if (wp->w_p_wrap)
3612 v = wp->w_skipcol;
3613 else
3614 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003615 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617#ifdef FEAT_MBYTE
3618 char_u *prev_ptr = ptr;
3619#endif
3620 while (vcol < v && *ptr != NUL)
3621 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003622 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 vcol += c;
3624#ifdef FEAT_MBYTE
3625 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003627 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003630 /* When:
3631 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003632 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003633 * - 'virtualedit' is set, or
3634 * - the visual mode is active,
3635 * the end of the line may be before the start of the displayed part.
3636 */
3637 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003638#ifdef FEAT_SYN_HL
3639 wp->w_p_cuc || draw_color_col ||
3640#endif
3641#ifdef FEAT_VIRTUALEDIT
3642 virtual_active() ||
3643#endif
3644 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
3649 /* Handle a character that's not completely on the screen: Put ptr at
3650 * that character but skip the first few screen characters. */
3651 if (vcol > v)
3652 {
3653 vcol -= c;
3654#ifdef FEAT_MBYTE
3655 ptr = prev_ptr;
3656#else
3657 --ptr;
3658#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003659 /* If the character fits on the screen, don't need to skip it.
3660 * Except for a TAB. */
3661 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003662#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003663 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003664#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003665 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003666 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668
3669 /*
3670 * Adjust for when the inverted text is before the screen,
3671 * and when the start of the inverted text is before the screen.
3672 */
3673 if (tocol <= vcol)
3674 fromcol = 0;
3675 else if (fromcol >= 0 && fromcol < vcol)
3676 fromcol = vcol;
3677
3678#ifdef FEAT_LINEBREAK
3679 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3680 if (wp->w_p_wrap)
3681 need_showbreak = TRUE;
3682#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003683#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003684 /* When spell checking a word we need to figure out the start of the
3685 * word and if it's badly spelled or not. */
3686 if (has_spell)
3687 {
3688 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003689 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003690 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003691
3692 pos = wp->w_cursor;
3693 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003694 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003695 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003696
3697 /* spell_move_to() may call ml_get() and make "line" invalid */
3698 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3699 ptr = line + linecol;
3700
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003701 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003702 {
3703 /* no bad word found at line start, don't check until end of a
3704 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003705 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003706 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003707 }
3708 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003709 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003710 /* bad word found, use attributes until end of word */
3711 word_end = wp->w_cursor.col + len + 1;
3712
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003713 /* Turn index into actual attributes. */
3714 if (spell_hlf != HLF_COUNT)
3715 spell_attr = highlight_attr[spell_hlf];
3716 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003717 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003718
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003719# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003720 /* Need to restart syntax highlighting for this line. */
3721 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003722 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003723# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003724 }
3725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727
3728 /*
3729 * Correct highlighting for cursor that can't be disabled.
3730 * Avoids having to check this for each character.
3731 */
3732 if (fromcol >= 0)
3733 {
3734 if (noinvcur)
3735 {
3736 if ((colnr_T)fromcol == wp->w_virtcol)
3737 {
3738 /* highlighting starts at cursor, let it start just after the
3739 * cursor */
3740 fromcol_prev = fromcol;
3741 fromcol = -1;
3742 }
3743 else if ((colnr_T)fromcol < wp->w_virtcol)
3744 /* restart highlighting after the cursor */
3745 fromcol_prev = wp->w_virtcol;
3746 }
3747 if (fromcol >= tocol)
3748 fromcol = -1;
3749 }
3750
3751#ifdef FEAT_SEARCH_EXTRA
3752 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003753 * Handle highlighting the last used search pattern and matches.
3754 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003756 cur = wp->w_match_head;
3757 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003758 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003760 if (shl_flag == FALSE)
3761 {
3762 shl = &search_hl;
3763 shl_flag = TRUE;
3764 }
3765 else
3766 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003767 shl->startcol = MAXCOL;
3768 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003770 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003771 v = (long)(ptr - line);
3772 if (cur != NULL)
3773 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003774 next_search_hl(wp, shl, lnum, (colnr_T)v,
3775 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003776
3777 /* Need to get the line again, a multi-line regexp may have made it
3778 * invalid. */
3779 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3780 ptr = line + v;
3781
3782 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003784 if (shl->lnum == lnum)
3785 shl->startcol = shl->rm.startpos[0].col;
3786 else
3787 shl->startcol = 0;
3788 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3789 - shl->rm.startpos[0].lnum)
3790 shl->endcol = shl->rm.endpos[0].col;
3791 else
3792 shl->endcol = MAXCOL;
3793 /* Highlight one character for an empty match. */
3794 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003797 if (has_mbyte && line[shl->endcol] != NUL)
3798 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3799 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003801 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003803 if ((long)shl->startcol < v) /* match at leftcol */
3804 {
3805 shl->attr_cur = shl->attr;
3806 search_attr = shl->attr;
3807 }
3808 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003810 if (shl != &search_hl && cur != NULL)
3811 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
3813#endif
3814
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003815#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003816 /* Cursor line highlighting for 'cursorline' in the current window. Not
3817 * when Visual mode is active, because it's not clear what is selected
3818 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003819 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003820 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003821 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003822 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003823 area_highlighting = TRUE;
3824 }
3825#endif
3826
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003827#ifdef FEAT_TEXT_PROP
3828 {
3829 char_u *prop_start;
3830
3831 text_prop_count = get_text_props(wp->w_buffer, lnum,
3832 &prop_start, FALSE);
3833 if (text_prop_count > 0)
3834 {
3835 // Make a copy of the properties, so that they are properly
3836 // aligned.
3837 text_props = (textprop_T *)alloc(
3838 text_prop_count * sizeof(textprop_T));
3839 if (text_props != NULL)
3840 mch_memmove(text_props, prop_start,
3841 text_prop_count * sizeof(textprop_T));
3842
3843 // Allocate an array for the indexes.
3844 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3845 area_highlighting = TRUE;
3846 extra_check = TRUE;
3847 }
3848 }
3849#endif
3850
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003851 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 col = 0;
3853#ifdef FEAT_RIGHTLEFT
3854 if (wp->w_p_rl)
3855 {
3856 /* Rightleft window: process the text in the normal direction, but put
3857 * it in current_ScreenLine[] from right to left. Start at the
3858 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003859 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 off += col;
3861 }
3862#endif
3863
3864 /*
3865 * Repeat for the whole displayed line.
3866 */
3867 for (;;)
3868 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003869#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003870 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 /* Skip this quickly when working on the text. */
3873 if (draw_state != WL_LINE)
3874 {
3875#ifdef FEAT_CMDWIN
3876 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3877 {
3878 draw_state = WL_CMDLINE;
3879 if (cmdwin_type != 0 && wp == curwin)
3880 {
3881 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003883 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003884 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886 }
3887#endif
3888
3889#ifdef FEAT_FOLDING
3890 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3891 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003892 int fdc = compute_foldcolumn(wp, 0);
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003895 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003897 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003898 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003899 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003900 p_extra_free = alloc(12 + 1);
3901
3902 if (p_extra_free != NULL)
3903 {
3904 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3905 n_extra = fdc;
3906 p_extra_free[n_extra] = NUL;
3907 p_extra = p_extra_free;
3908 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003909 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912 }
3913#endif
3914
3915#ifdef FEAT_SIGNS
3916 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3917 {
3918 draw_state = WL_SIGN;
3919 /* Show the sign column when there are any signs in this
3920 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003921 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003923 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003925 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926# endif
3927
3928 /* Draw two cells with the sign value or blank. */
3929 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003930 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 n_extra = 2;
3932
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003933 if (row == startrow
3934#ifdef FEAT_DIFF
3935 + filler_lines && filler_todo <= 0
3936#endif
3937 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
3939 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3940 SIGN_TEXT);
3941# ifdef FEAT_SIGN_ICONS
3942 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3943 SIGN_ICON);
3944 if (gui.in_use && icon_sign != 0)
3945 {
3946 /* Use the image in this position. */
3947 c_extra = SIGN_BYTE;
3948# ifdef FEAT_NETBEANS_INTG
3949 if (buf_signcount(wp->w_buffer, lnum) > 1)
3950 c_extra = MULTISIGN_BYTE;
3951# endif
3952 char_attr = icon_sign;
3953 }
3954 else
3955# endif
3956 if (text_sign != 0)
3957 {
3958 p_extra = sign_get_text(text_sign);
3959 if (p_extra != NULL)
3960 {
3961 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003962 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 char_attr = sign_get_attr(text_sign, FALSE);
3965 }
3966 }
3967 }
3968 }
3969#endif
3970
3971 if (draw_state == WL_NR - 1 && n_extra == 0)
3972 {
3973 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003974 /* Display the absolute or relative line number. After the
3975 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3976 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 && (row == startrow
3978#ifdef FEAT_DIFF
3979 + filler_lines
3980#endif
3981 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3982 {
3983 /* Draw the line number (empty space after wrapping). */
3984 if (row == startrow
3985#ifdef FEAT_DIFF
3986 + filler_lines
3987#endif
3988 )
3989 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003990 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003991 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003992
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003993 if (wp->w_p_nu && !wp->w_p_rnu)
3994 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003995 num = (long)lnum;
3996 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003997 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003998 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003999 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004000 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004001 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004002 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004003 num = lnum;
4004 fmt = "%-*ld ";
4005 }
4006 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004007
Bram Moolenaar700e7342013-01-30 12:31:36 +01004008 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004009 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (wp->w_skipcol > 0)
4011 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4012 *p_extra = '-';
4013#ifdef FEAT_RIGHTLEFT
4014 if (wp->w_p_rl) /* reverse line numbers */
4015 rl_mirror(extra);
4016#endif
4017 p_extra = extra;
4018 c_extra = NUL;
4019 }
4020 else
4021 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004022 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004023 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004024#ifdef FEAT_SYN_HL
4025 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004026 * the current line differently.
4027 * TODO: Can we use CursorLine instead of CursorLineNr
4028 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004029 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004030 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004031 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 }
4035
Bram Moolenaar597a4222014-06-25 14:39:50 +02004036#ifdef FEAT_LINEBREAK
4037 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4038 && n_extra == 0 && *p_sbr != NUL)
4039 /* draw indent after showbreak value */
4040 draw_state = WL_BRI;
4041 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4042 /* After the showbreak, draw the breakindent */
4043 draw_state = WL_BRI - 1;
4044
4045 /* draw 'breakindent': indent wrapped text accordingly */
4046 if (draw_state == WL_BRI - 1 && n_extra == 0)
4047 {
4048 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004049 /* if need_showbreak is set, breakindent also applies */
4050 if (wp->w_p_bri && n_extra == 0
4051 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004052# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004053 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004054# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004055 )
4056 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004057 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004058# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004059 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004060 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004061 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004062# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004063 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4064 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004065 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004066# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004067 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004068# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004069 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004070 c_extra = ' ';
4071 n_extra = get_breakindent_win(wp,
4072 ml_get_buf(wp->w_buffer, lnum, FALSE));
4073 /* Correct end of highlighted area for 'breakindent',
4074 * required when 'linebreak' is also set. */
4075 if (tocol == vcol)
4076 tocol += n_extra;
4077 }
4078 }
4079#endif
4080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4082 if (draw_state == WL_SBR - 1 && n_extra == 0)
4083 {
4084 draw_state = WL_SBR;
4085# ifdef FEAT_DIFF
4086 if (filler_todo > 0)
4087 {
4088 /* Draw "deleted" diff line(s). */
4089 if (char2cells(fill_diff) > 1)
4090 c_extra = '-';
4091 else
4092 c_extra = fill_diff;
4093# ifdef FEAT_RIGHTLEFT
4094 if (wp->w_p_rl)
4095 n_extra = col + 1;
4096 else
4097# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004098 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004099 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101# endif
4102# ifdef FEAT_LINEBREAK
4103 if (*p_sbr != NUL && need_showbreak)
4104 {
4105 /* Draw 'showbreak' at the start of each broken line. */
4106 p_extra = p_sbr;
4107 c_extra = NUL;
4108 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004109 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004111 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 /* Correct end of highlighted area for 'showbreak',
4113 * required when 'linebreak' is also set. */
4114 if (tocol == vcol)
4115 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004116#ifdef FEAT_SYN_HL
4117 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004118 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004119 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004120 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123# endif
4124 }
4125#endif
4126
4127 if (draw_state == WL_LINE - 1 && n_extra == 0)
4128 {
4129 draw_state = WL_LINE;
4130 if (saved_n_extra)
4131 {
4132 /* Continue item from end of wrapped line. */
4133 n_extra = saved_n_extra;
4134 c_extra = saved_c_extra;
4135 p_extra = saved_p_extra;
4136 char_attr = saved_char_attr;
4137 }
4138 else
4139 char_attr = 0;
4140 }
4141 }
4142
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004143 // When still displaying '$' of change command, stop at cursor.
4144 // When only displaying the (relative) line number and that's done,
4145 // stop here.
4146 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004147 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148#ifdef FEAT_DIFF
4149 && filler_todo <= 0
4150#endif
4151 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004152 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004154 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004155 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004156 /* Pretend we have finished updating the window. Except when
4157 * 'cursorcolumn' is set. */
4158#ifdef FEAT_SYN_HL
4159 if (wp->w_p_cuc)
4160 row = wp->w_cline_row + wp->w_cline_height;
4161 else
4162#endif
4163 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 break;
4165 }
4166
Bram Moolenaar637532b2019-01-03 21:44:40 +01004167 if (draw_state == WL_LINE && (area_highlighting
4168#ifdef FEAT_SPELL
4169 || has_spell
4170#endif
4171 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
4173 /* handle Visual or match highlighting in this line */
4174 if (vcol == fromcol
4175#ifdef FEAT_MBYTE
4176 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4177 && (*mb_ptr2cells)(ptr) > 1)
4178#endif
4179 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004180 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 && vcol < tocol))
4182 area_attr = attr; /* start highlighting */
4183 else if (area_attr != 0
4184 && (vcol == tocol
4185 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188#ifdef FEAT_SEARCH_EXTRA
4189 if (!n_extra)
4190 {
4191 /*
4192 * Check for start/end of search pattern match.
4193 * After end, check for start/end of next match.
4194 * When another match, have to check for start again.
4195 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004196 * Do this for 'search_hl' and the match list (ordered by
4197 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004199 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004200 cur = wp->w_match_head;
4201 shl_flag = FALSE;
4202 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004204 if (shl_flag == FALSE
4205 && ((cur != NULL
4206 && cur->priority > SEARCH_HL_PRIORITY)
4207 || cur == NULL))
4208 {
4209 shl = &search_hl;
4210 shl_flag = TRUE;
4211 }
4212 else
4213 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004214 if (cur != NULL)
4215 cur->pos.cur = 0;
4216 pos_inprogress = TRUE;
4217 while (shl->rm.regprog != NULL
4218 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004220 if (shl->startcol != MAXCOL
4221 && v >= (long)shl->startcol
4222 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004224#ifdef FEAT_MBYTE
4225 int tmp_col = v + MB_PTR2LEN(ptr);
4226
4227 if (shl->endcol < tmp_col)
4228 shl->endcol = tmp_col;
4229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004231#ifdef FEAT_CONCEAL
4232 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4233 == cur->hlg_id)
4234 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004235 has_match_conc =
4236 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004237 match_conc = cur->conceal_char;
4238 }
4239 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004240 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004243 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004246 next_search_hl(wp, shl, lnum, (colnr_T)v,
4247 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004248 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004249 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /* Need to get the line again, a multi-line regexp
4252 * may have made it invalid. */
4253 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4254 ptr = line + v;
4255
4256 if (shl->lnum == lnum)
4257 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004258 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004260 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004262 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004264 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 {
4266 /* highlight empty match, try again after
4267 * it */
4268#ifdef FEAT_MBYTE
4269 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004270 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004271 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 else
4273#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004274 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276
4277 /* Loop to check if the match starts at the
4278 * current position */
4279 continue;
4280 }
4281 }
4282 break;
4283 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004284 if (shl != &search_hl && cur != NULL)
4285 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004287
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004288 /* Use attributes from match with highest priority among
4289 * 'search_hl' and the match list. */
4290 search_attr = search_hl.attr_cur;
4291 cur = wp->w_match_head;
4292 shl_flag = FALSE;
4293 while (cur != NULL || shl_flag == FALSE)
4294 {
4295 if (shl_flag == FALSE
4296 && ((cur != NULL
4297 && cur->priority > SEARCH_HL_PRIORITY)
4298 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004299 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004300 shl = &search_hl;
4301 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004302 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004303 else
4304 shl = &cur->hl;
4305 if (shl->attr_cur != 0)
4306 search_attr = shl->attr_cur;
4307 if (shl != &search_hl && cur != NULL)
4308 cur = cur->next;
4309 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004310 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004311 if (*ptr == NUL && (did_line_attr >= 1
4312 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004313 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315#endif
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004318 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004320 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4321 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004323 if (diff_hlf == HLF_TXD && ptr - line > change_end
4324 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004326 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004327 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004328 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004331
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004332#ifdef FEAT_TEXT_PROP
4333 if (text_props != NULL)
4334 {
4335 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004336 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004337
4338 // Check if any active property ends.
4339 for (pi = 0; pi < text_props_active; ++pi)
4340 {
4341 int tpi = text_prop_idxs[pi];
4342
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004343 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004344 + text_props[tpi].tp_len)
4345 {
4346 if (pi + 1 < text_props_active)
4347 mch_memmove(text_prop_idxs + pi,
4348 text_prop_idxs + pi + 1,
4349 sizeof(int)
4350 * (text_props_active - (pi + 1)));
4351 --text_props_active;
4352 --pi;
4353 }
4354 }
4355
4356 // Add any text property that starts in this column.
4357 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004358 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004359 text_prop_idxs[text_props_active++] = text_prop_next++;
4360
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004361 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004362 if (text_props_active > 0)
4363 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004364 // Sort the properties on priority and/or starting last.
4365 // Then combine the attributes, highest priority last.
4366 current_text_props = text_props;
4367 current_buf = wp->w_buffer;
4368 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4369 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004370
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004371 for (pi = 0; pi < text_props_active; ++pi)
4372 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004373 int tpi = text_prop_idxs[pi];
4374 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004375
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004376 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004377 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004378 int pt_attr = syn_id2attr(pt->pt_hl_id);
4379
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004380 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004381 if (text_prop_attr == 0)
4382 text_prop_attr = pt_attr;
4383 else
4384 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004385 }
4386 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004387 }
4388 }
4389#endif
4390
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004391 /* Decide which of the highlight attributes to use. */
4392 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004393#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004394 if (area_attr != 0)
4395 char_attr = hl_combine_attr(line_attr, area_attr);
4396 else if (search_attr != 0)
4397 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004398 /* Use line_attr when not in the Visual or 'incsearch' area
4399 * (area_attr may be 0 when "noinvcur" is set). */
4400 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004401 || vcol < fromcol || vcol_prev < fromcol_prev
4402 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004403 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004404#else
4405 if (area_attr != 0)
4406 char_attr = area_attr;
4407 else if (search_attr != 0)
4408 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004409#endif
4410 else
4411 {
4412 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004413#ifdef FEAT_TEXT_PROP
4414 if (text_prop_type != NULL)
4415 char_attr = text_prop_attr;
4416 else
4417#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004418#ifdef FEAT_SYN_HL
4419 if (has_syntax)
4420 char_attr = syntax_attr;
4421 else
4422#endif
4423 char_attr = 0;
4424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426
4427 /*
4428 * Get the next character to put on the screen.
4429 */
4430 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004431 * The "p_extra" points to the extra stuff that is inserted to
4432 * represent special characters (non-printable stuff) and other
4433 * things. When all characters are the same, c_extra is used.
4434 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4435 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4437 */
4438 if (n_extra > 0)
4439 {
4440 if (c_extra != NUL)
4441 {
4442 c = c_extra;
4443#ifdef FEAT_MBYTE
4444 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004445 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
4447 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004448 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004449 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
4451 else
4452 mb_utf8 = FALSE;
4453#endif
4454 }
4455 else
4456 {
4457 c = *p_extra;
4458#ifdef FEAT_MBYTE
4459 if (has_mbyte)
4460 {
4461 mb_c = c;
4462 if (enc_utf8)
4463 {
4464 /* If the UTF-8 character is more than one byte:
4465 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004466 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 mb_utf8 = FALSE;
4468 if (mb_l > n_extra)
4469 mb_l = 1;
4470 else if (mb_l > 1)
4471 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004472 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004474 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476 }
4477 else
4478 {
4479 /* if this is a DBCS character, put it in "mb_c" */
4480 mb_l = MB_BYTE2LEN(c);
4481 if (mb_l >= n_extra)
4482 mb_l = 1;
4483 else if (mb_l > 1)
4484 mb_c = (c << 8) + p_extra[1];
4485 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004486 if (mb_l == 0) /* at the NUL at end-of-line */
4487 mb_l = 1;
4488
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 /* If a double-width char doesn't fit display a '>' in the
4490 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004491 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492# ifdef FEAT_RIGHTLEFT
4493 wp->w_p_rl ? (col <= 0) :
4494# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004495 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 && (*mb_char2cells)(mb_c) == 2)
4497 {
4498 c = '>';
4499 mb_c = c;
4500 mb_l = 1;
4501 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004502 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 /* put the pointer back to output the double-width
4504 * character at the start of the next line. */
4505 ++n_extra;
4506 --p_extra;
4507 }
4508 else
4509 {
4510 n_extra -= mb_l - 1;
4511 p_extra += mb_l - 1;
4512 }
4513 }
4514#endif
4515 ++p_extra;
4516 }
4517 --n_extra;
4518 }
4519 else
4520 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004521#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004522 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004523#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004524
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004525 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004526 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 /*
4528 * Get a character from the line itself.
4529 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004530 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004531#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004532 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534#ifdef FEAT_MBYTE
4535 if (has_mbyte)
4536 {
4537 mb_c = c;
4538 if (enc_utf8)
4539 {
4540 /* If the UTF-8 character is more than one byte: Decode it
4541 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004542 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 mb_utf8 = FALSE;
4544 if (mb_l > 1)
4545 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004546 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 /* Overlong encoded ASCII or ASCII with composing char
4548 * is displayed normally, except a NUL. */
4549 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004550 {
4551 c = mb_c;
4552# ifdef FEAT_LINEBREAK
4553 c0 = mb_c;
4554# endif
4555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004557
4558 /* At start of the line we can have a composing char.
4559 * Draw it as a space with a composing char. */
4560 if (utf_iscomposing(mb_c))
4561 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004562 int i;
4563
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004564 for (i = Screen_mco - 1; i > 0; --i)
4565 u8cc[i] = u8cc[i - 1];
4566 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004567 mb_c = ' ';
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570
4571 if ((mb_l == 1 && c >= 0x80)
4572 || (mb_l >= 1 && mb_c == 0)
4573 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004574# ifdef UNICODE16
4575 || mb_c >= 0x10000
4576# endif
4577 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
4579 /*
4580 * Illegal UTF-8 byte: display as <xx>.
4581 * Non-BMP character : display as ? or fullwidth ?.
4582 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004583# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
4587 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004588# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (wp->w_p_rl) /* reverse */
4590 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004593# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 else if (utf_char2cells(mb_c) != 2)
4595 STRCPY(extra, "?");
4596 else
4597 /* 0xff1f in UTF-8: full-width '?' */
4598 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004599# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
4601 p_extra = extra;
4602 c = *p_extra;
4603 mb_c = mb_ptr2char_adv(&p_extra);
4604 mb_utf8 = (c >= 0x80);
4605 n_extra = (int)STRLEN(p_extra);
4606 c_extra = NUL;
4607 if (area_attr == 0 && search_attr == 0)
4608 {
4609 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004610 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 saved_attr2 = char_attr; /* save current attr */
4612 }
4613 }
4614 else if (mb_l == 0) /* at the NUL at end-of-line */
4615 mb_l = 1;
4616#ifdef FEAT_ARABIC
4617 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4618 {
4619 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004620 int pc, pc1, nc;
4621 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623 /* The idea of what is the previous and next
4624 * character depends on 'rightleft'. */
4625 if (wp->w_p_rl)
4626 {
4627 pc = prev_c;
4628 pc1 = prev_c1;
4629 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004630 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 else
4633 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004634 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004636 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638 prev_c = mb_c;
4639
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004640 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 else
4643 prev_c = mb_c;
4644#endif
4645 }
4646 else /* enc_dbcs */
4647 {
4648 mb_l = MB_BYTE2LEN(c);
4649 if (mb_l == 0) /* at the NUL at end-of-line */
4650 mb_l = 1;
4651 else if (mb_l > 1)
4652 {
4653 /* We assume a second byte below 32 is illegal.
4654 * Hopefully this is OK for all double-byte encodings!
4655 */
4656 if (ptr[1] >= 32)
4657 mb_c = (c << 8) + ptr[1];
4658 else
4659 {
4660 if (ptr[1] == NUL)
4661 {
4662 /* head byte at end of line */
4663 mb_l = 1;
4664 transchar_nonprint(extra, c);
4665 }
4666 else
4667 {
4668 /* illegal tail byte */
4669 mb_l = 2;
4670 STRCPY(extra, "XX");
4671 }
4672 p_extra = extra;
4673 n_extra = (int)STRLEN(extra) - 1;
4674 c_extra = NUL;
4675 c = *p_extra++;
4676 if (area_attr == 0 && search_attr == 0)
4677 {
4678 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004679 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 saved_attr2 = char_attr; /* save current attr */
4681 }
4682 mb_c = c;
4683 }
4684 }
4685 }
4686 /* If a double-width char doesn't fit display a '>' in the
4687 * last column; the character is displayed at the start of the
4688 * next line. */
4689 if ((
4690# ifdef FEAT_RIGHTLEFT
4691 wp->w_p_rl ? (col <= 0) :
4692# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004693 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 && (*mb_char2cells)(mb_c) == 2)
4695 {
4696 c = '>';
4697 mb_c = c;
4698 mb_utf8 = FALSE;
4699 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004700 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 /* Put pointer back so that the character will be
4702 * displayed at the start of the next line. */
4703 --ptr;
4704 }
4705 else if (*ptr != NUL)
4706 ptr += mb_l - 1;
4707
4708 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004709 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004710 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004711 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004714 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 c = ' ';
4716 if (area_attr == 0 && search_attr == 0)
4717 {
4718 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004719 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 saved_attr2 = char_attr; /* save current attr */
4721 }
4722 mb_c = c;
4723 mb_utf8 = FALSE;
4724 mb_l = 1;
4725 }
4726
4727 }
4728#endif
4729 ++ptr;
4730
4731 if (extra_check)
4732 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004733#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004734 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004735#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004736
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004737#ifdef FEAT_TERMINAL
4738 if (get_term_attr)
4739 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004740 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004741
4742 if (!attr_pri)
4743 char_attr = syntax_attr;
4744 else
4745 char_attr = hl_combine_attr(syntax_attr, char_attr);
4746 }
4747#endif
4748
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004749#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004750 // Get syntax attribute, unless still at the start of the line
4751 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004752 v = (long)(ptr - line);
4753 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
4755 /* Get the syntax attribute for the character. If there
4756 * is an error, disable syntax highlighting. */
4757 save_did_emsg = did_emsg;
4758 did_emsg = FALSE;
4759
Bram Moolenaar217ad922005-03-20 22:37:15 +00004760 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004761# ifdef FEAT_SPELL
4762 has_spell ? &can_spell :
4763# endif
4764 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004767 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004768 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004769 has_syntax = FALSE;
4770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 else
4772 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004773#ifdef SYN_TIME_LIMIT
4774 if (wp->w_s->b_syn_slow)
4775 has_syntax = FALSE;
4776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
4778 /* Need to get the line again, a multi-line regexp may
4779 * have made it invalid. */
4780 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4781 ptr = line + v;
4782
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004783# ifdef FEAT_TEXT_PROP
4784 // Text properties overrule syntax highlighting.
4785 if (text_prop_attr == 0)
4786#endif
4787 {
4788 if (!attr_pri)
4789 char_attr = syntax_attr;
4790 else
4791 char_attr = hl_combine_attr(syntax_attr, char_attr);
4792 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004793# ifdef FEAT_CONCEAL
4794 /* no concealing past the end of the line, it interferes
4795 * with line highlighting */
4796 if (c == NUL)
4797 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004798 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004799 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004802#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004803
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004804#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004805 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004806 * Only do this when there is no syntax highlighting, the
4807 * @Spell cluster is not used or the current syntax item
4808 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004809 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004810 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004811 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004812 if (c != 0 && (
4813# ifdef FEAT_SYN_HL
4814 !has_syntax ||
4815# endif
4816 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004817 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004818 char_u *prev_ptr, *p;
4819 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004820 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004821# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004822 if (has_mbyte)
4823 {
4824 prev_ptr = ptr - mb_l;
4825 v -= mb_l - 1;
4826 }
4827 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004828# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004829 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004830
4831 /* Use nextline[] if possible, it has the start of the
4832 * next line concatenated. */
4833 if ((prev_ptr - line) - nextlinecol >= 0)
4834 p = nextline + (prev_ptr - line) - nextlinecol;
4835 else
4836 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004837 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004838 len = spell_check(wp, p, &spell_hlf, &cap_col,
4839 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004840 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004841
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004842 /* In Insert mode only highlight a word that
4843 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004844 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004845 && (State & INSERT) != 0
4846 && wp->w_cursor.lnum == lnum
4847 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004848 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004849 && wp->w_cursor.col < (colnr_T)word_end)
4850 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004851 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004852 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004853 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004854
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004855 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004856 && (p - nextline) + len > nextline_idx)
4857 {
4858 /* Remember that the good word continues at the
4859 * start of the next line. */
4860 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004861 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004862 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004863
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004864 /* Turn index into actual attributes. */
4865 if (spell_hlf != HLF_COUNT)
4866 spell_attr = highlight_attr[spell_hlf];
4867
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004868 if (cap_col > 0)
4869 {
4870 if (p != prev_ptr
4871 && (p - nextline) + cap_col >= nextline_idx)
4872 {
4873 /* Remember that the word in the next line
4874 * must start with a capital. */
4875 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004876 cap_col = (int)((p - nextline) + cap_col
4877 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004878 }
4879 else
4880 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004881 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004882 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004883 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004884 }
4885 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004886 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004887 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004888 char_attr = hl_combine_attr(char_attr, spell_attr);
4889 else
4890 char_attr = hl_combine_attr(spell_attr, char_attr);
4891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#endif
4893#ifdef FEAT_LINEBREAK
4894 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004895 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004897 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004898 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004900# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004901 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004902# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004903 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004905 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004907 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004908
Bram Moolenaar597a4222014-06-25 14:39:50 +02004909 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004910 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004911 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004912 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004913# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004914 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004915 wp->w_buffer->b_p_vts_array) - 1;
4916# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004917 n_extra = (int)wp->w_buffer->b_p_ts
4918 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004919# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004920
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004921# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004922 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004923# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004925# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004926 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004927 {
4928#ifdef FEAT_CONCEAL
4929 if (c == TAB)
4930 /* See "Tab alignment" below. */
4931 FIX_FOR_BOGUSCOLS;
4932#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004933 if (!wp->w_p_list)
4934 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937#endif
4938
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004939 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4940 */
4941 if (wp->w_p_list
4942 && (((c == 160
4943#ifdef FEAT_MBYTE
4944 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4945#endif
4946 ) && lcs_nbsp)
4947 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4948 {
4949 c = (c == ' ') ? lcs_space : lcs_nbsp;
4950 if (area_attr == 0 && search_attr == 0)
4951 {
4952 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004953 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004954 saved_attr2 = char_attr; /* save current attr */
4955 }
4956#ifdef FEAT_MBYTE
4957 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004958 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004959 {
4960 mb_utf8 = TRUE;
4961 u8cc[0] = 0;
4962 c = 0xc0;
4963 }
4964 else
4965 mb_utf8 = FALSE;
4966#endif
4967 }
4968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4970 {
4971 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004972 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
4974 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004975 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 saved_attr2 = char_attr; /* save current attr */
4977 }
4978#ifdef FEAT_MBYTE
4979 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004980 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004983 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004984 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986 else
4987 mb_utf8 = FALSE;
4988#endif
4989 }
4990 }
4991
4992 /*
4993 * Handling of non-printable characters.
4994 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004995 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 /*
4998 * when getting a character from the file, we may have to
4999 * turn it into something else on the way to putting it
5000 * into "ScreenLines".
5001 */
5002 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5003 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005004 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005005 long vcol_adjusted = vcol; /* removed showbreak length */
5006#ifdef FEAT_LINEBREAK
5007 /* only adjust the tab_len, when at the first column
5008 * after the showbreak value was drawn */
5009 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5010 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005013#ifdef FEAT_VARTABS
5014 tab_len = tabstop_padding(vcol_adjusted,
5015 wp->w_buffer->b_p_ts,
5016 wp->w_buffer->b_p_vts_array) - 1;
5017#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005018 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005019 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5020#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005021
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005022#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005023 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005024#endif
5025 /* tab amount depends on current column */
5026 n_extra = tab_len;
5027#ifdef FEAT_LINEBREAK
5028 else
5029 {
5030 char_u *p;
5031 int len = n_extra;
5032 int i;
5033 int saved_nextra = n_extra;
5034
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005035#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005036 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005037 /* there are characters to conceal */
5038 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005039 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5040 */
5041 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5042 && n_extra > tab_len)
5043 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005044#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005045
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005046 /* if n_extra > 0, it gives the number of chars, to
5047 * use for a tab, else we need to calculate the width
5048 * for a tab */
5049#ifdef FEAT_MBYTE
5050 len = (tab_len * mb_char2len(lcs_tab2));
5051 if (n_extra > 0)
5052 len += n_extra - tab_len;
5053#endif
5054 c = lcs_tab1;
5055 p = alloc((unsigned)(len + 1));
5056 vim_memset(p, ' ', len);
5057 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005058 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005059 p_extra_free = p;
5060 for (i = 0; i < tab_len; i++)
5061 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005062 if (*p == NUL)
5063 {
5064 tab_len = i;
5065 break;
5066 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005067#ifdef FEAT_MBYTE
5068 mb_char2bytes(lcs_tab2, p);
5069 p += mb_char2len(lcs_tab2);
5070 n_extra += mb_char2len(lcs_tab2)
5071 - (saved_nextra > 0 ? 1 : 0);
5072#else
5073 p[i] = lcs_tab2;
5074#endif
5075 }
5076 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005077#ifdef FEAT_CONCEAL
5078 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5079 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005080 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005081 n_extra -= vcol_off;
5082#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005083 }
5084#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005085#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005086 {
5087 int vc_saved = vcol_off;
5088
5089 /* Tab alignment should be identical regardless of
5090 * 'conceallevel' value. So tab compensates of all
5091 * previous concealed characters, and thus resets
5092 * vcol_off and boguscols accumulated so far in the
5093 * line. Note that the tab can be longer than
5094 * 'tabstop' when there are concealed characters. */
5095 FIX_FOR_BOGUSCOLS;
5096
5097 /* Make sure, the highlighting for the tab char will be
5098 * correctly set further below (effectively reverts the
5099 * FIX_FOR_BOGSUCOLS macro */
5100 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005101 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005102 tab_len += vc_saved;
5103 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105#ifdef FEAT_MBYTE
5106 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5107#endif
5108 if (wp->w_p_list)
5109 {
5110 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005111#ifdef FEAT_LINEBREAK
5112 if (wp->w_p_lbr)
5113 c_extra = NUL; /* using p_extra from above */
5114 else
5115#endif
5116 c_extra = lcs_tab2;
5117 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005118 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 saved_attr2 = char_attr; /* save current attr */
5120#ifdef FEAT_MBYTE
5121 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005122 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
5124 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005125 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005126 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128#endif
5129 }
5130 else
5131 {
5132 c_extra = ' ';
5133 c = ' ';
5134 }
5135 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005136 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005137 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005138 || ((fromcol >= 0 || fromcol_prev >= 0)
5139 && tocol > vcol
5140 && VIsual_mode != Ctrl_V
5141 && (
5142# ifdef FEAT_RIGHTLEFT
5143 wp->w_p_rl ? (col >= 0) :
5144# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005145 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005146 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005147 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005148 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005149 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005151 /* Display a '$' after the line or highlight an extra
5152 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5154 /* For a diff line the highlighting continues after the
5155 * "$". */
5156 if (
5157# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005158 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159# ifdef LINE_ATTR
5160 &&
5161# endif
5162# endif
5163# ifdef LINE_ATTR
5164 line_attr == 0
5165# endif
5166 )
5167#endif
5168 {
5169#ifdef FEAT_VIRTUALEDIT
5170 /* In virtualedit, visual selections may extend
5171 * beyond end of line. */
5172 if (area_highlighting && virtual_active()
5173 && tocol != MAXCOL && vcol < tocol)
5174 n_extra = 0;
5175 else
5176#endif
5177 {
5178 p_extra = at_end_str;
5179 n_extra = 1;
5180 c_extra = NUL;
5181 }
5182 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005183 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005184 c = lcs_eol;
5185 else
5186 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 lcs_eol_one = -1;
5188 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005189 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005191 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 n_attr = 1;
5193 }
5194#ifdef FEAT_MBYTE
5195 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005196 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
5198 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005199 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005200 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 else
5203 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5204#endif
5205 }
5206 else if (c != NUL)
5207 {
5208 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005209 if (n_extra == 0)
5210 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#ifdef FEAT_RIGHTLEFT
5212 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5213 rl_mirror(p_extra); /* reverse "<12>" */
5214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005216#ifdef FEAT_LINEBREAK
5217 if (wp->w_p_lbr)
5218 {
5219 char_u *p;
5220
5221 c = *p_extra;
5222 p = alloc((unsigned)n_extra + 1);
5223 vim_memset(p, ' ', n_extra);
5224 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5225 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005226 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005227 p_extra_free = p_extra = p;
5228 }
5229 else
5230#endif
5231 {
5232 n_extra = byte2cells(c) - 1;
5233 c = *p_extra++;
5234 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005235 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
5237 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005238 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 saved_attr2 = char_attr; /* save current attr */
5240 }
5241#ifdef FEAT_MBYTE
5242 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5243#endif
5244 }
5245#ifdef FEAT_VIRTUALEDIT
5246 else if (VIsual_active
5247 && (VIsual_mode == Ctrl_V
5248 || VIsual_mode == 'v')
5249 && virtual_active()
5250 && tocol != MAXCOL
5251 && vcol < tocol
5252 && (
5253# ifdef FEAT_RIGHTLEFT
5254 wp->w_p_rl ? (col >= 0) :
5255# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005256 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 c = ' ';
5259 --ptr; /* put it back at the NUL */
5260 }
5261#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005262#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 else if ((
5264# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005265 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005267# ifdef FEAT_TERMINAL
5268 term_attr != 0 ||
5269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 ) && (
5272# ifdef FEAT_RIGHTLEFT
5273 wp->w_p_rl ? (col >= 0) :
5274# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005275 (col
5276# ifdef FEAT_CONCEAL
5277 - boguscols
5278# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005279 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
5281 /* Highlight until the right side of the window */
5282 c = ' ';
5283 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005284
5285 /* Remember we do the char for line highlighting. */
5286 ++did_line_attr;
5287
5288 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005289 if (line_attr != 0 && char_attr == search_attr
5290 && (did_line_attr > 1
5291 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005292 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293# ifdef FEAT_DIFF
5294 if (diff_hlf == HLF_TXD)
5295 {
5296 diff_hlf = HLF_CHD;
5297 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005298 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005299 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005300 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5301 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005302 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005306# ifdef FEAT_TERMINAL
5307 if (term_attr != 0)
5308 {
5309 char_attr = term_attr;
5310 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5311 char_attr = hl_combine_attr(char_attr,
5312 HL_ATTR(HLF_CUL));
5313 }
5314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316#endif
5317 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005318
5319#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005320 if ( wp->w_p_cole > 0
5321 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005322 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005323 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005324 && !(lnum_in_visual_area
5325 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005326 {
5327 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005328 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005329 && (syn_get_sub_char() != NUL || match_conc
5330 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005331 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005332 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005333 /* First time at this concealed item: display one
5334 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005335 if (match_conc)
5336 c = match_conc;
5337 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005338 c = syn_get_sub_char();
5339 else if (lcs_conceal != NUL)
5340 c = lcs_conceal;
5341 else
5342 c = ' ';
5343
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005344 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005345
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005346 if (n_extra > 0)
5347 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005348 vcol += n_extra;
5349 if (wp->w_p_wrap && n_extra > 0)
5350 {
5351# ifdef FEAT_RIGHTLEFT
5352 if (wp->w_p_rl)
5353 {
5354 col -= n_extra;
5355 boguscols -= n_extra;
5356 }
5357 else
5358# endif
5359 {
5360 boguscols += n_extra;
5361 col += n_extra;
5362 }
5363 }
5364 n_extra = 0;
5365 n_attr = 0;
5366 }
5367 else if (n_skip == 0)
5368 {
5369 is_concealing = TRUE;
5370 n_skip = 1;
5371 }
5372# ifdef FEAT_MBYTE
5373 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005374 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005375 {
5376 mb_utf8 = TRUE;
5377 u8cc[0] = 0;
5378 c = 0xc0;
5379 }
5380 else
5381 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5382# endif
5383 }
5384 else
5385 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005386 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005387 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005388 }
5389#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005392#ifdef FEAT_CONCEAL
5393 /* In the cursor line and we may be concealing characters: correct
5394 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005395 if (!did_wcol && draw_state == WL_LINE
5396 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005397 && conceal_cursor_line(wp)
5398 && (int)wp->w_virtcol <= vcol + n_skip)
5399 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005400# ifdef FEAT_RIGHTLEFT
5401 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005402 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005403 else
5404# endif
5405 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005406 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005407 did_wcol = TRUE;
5408 }
5409#endif
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 /* Don't override visual selection highlighting. */
5412 if (n_attr > 0
5413 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005414 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 char_attr = extra_attr;
5416
Bram Moolenaar81695252004-12-29 20:58:21 +00005417#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 /* XIM don't send preedit_start and preedit_end, but they send
5419 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5420 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005421 if (p_imst == IM_ON_THE_SPOT
5422 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005423 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 && (State & INSERT)
5425 && !p_imdisable
5426 && im_is_preediting()
5427 && draw_state == WL_LINE)
5428 {
5429 colnr_T tcol;
5430
5431 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005432 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 else
5434 tcol = preedit_end_col;
5435 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5436 {
5437 if (feedback_old_attr < 0)
5438 {
5439 feedback_col = 0;
5440 feedback_old_attr = char_attr;
5441 }
5442 char_attr = im_get_feedback_attr(feedback_col);
5443 if (char_attr < 0)
5444 char_attr = feedback_old_attr;
5445 feedback_col++;
5446 }
5447 else if (feedback_old_attr >= 0)
5448 {
5449 char_attr = feedback_old_attr;
5450 feedback_old_attr = -1;
5451 feedback_col = 0;
5452 }
5453 }
5454#endif
5455 /*
5456 * Handle the case where we are in column 0 but not on the first
5457 * character of the line and the user wants us to show us a
5458 * special character (via 'listchars' option "precedes:<char>".
5459 */
5460 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005461 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5463#ifdef FEAT_DIFF
5464 && filler_todo <= 0
5465#endif
5466 && draw_state > WL_NR
5467 && c != NUL)
5468 {
5469 c = lcs_prec;
5470 lcs_prec_todo = NUL;
5471#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005472 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5473 {
5474 /* Double-width character being overwritten by the "precedes"
5475 * character, need to fill up half the character. */
5476 c_extra = MB_FILLER_CHAR;
5477 n_extra = 1;
5478 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005479 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005482 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
5484 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005485 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005486 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 else
5489 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5490#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005491 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {
5493 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005494 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 n_attr3 = 1;
5496 }
5497 }
5498
5499 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005500 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005502 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005503#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005504 || did_line_attr == 1
5505#endif
5506 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005508#ifdef FEAT_SEARCH_EXTRA
5509 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005510
5511 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005512 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005513 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005514#endif
5515
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005516 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 * highlight match at end of line. If it's beyond the last
5518 * char on the screen, just overwrite that one (tricky!) Not
5519 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005520#ifdef FEAT_SEARCH_EXTRA
5521 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005522 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005523 prevcol_hl_flag = TRUE;
5524 else
5525 {
5526 cur = wp->w_match_head;
5527 while (cur != NULL)
5528 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005529 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005530 {
5531 prevcol_hl_flag = TRUE;
5532 break;
5533 }
5534 cur = cur->next;
5535 }
5536 }
5537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005539 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005540 && (VIsual_mode != Ctrl_V
5541 || lnum == VIsual.lnum
5542 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005543 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544#ifdef FEAT_SEARCH_EXTRA
5545 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005546 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005547# ifdef FEAT_SYN_HL
5548 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5549 && !(wp == curwin && VIsual_active))
5550# endif
5551# ifdef FEAT_DIFF
5552 && diff_hlf == (hlf_T)0
5553# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005554# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005555 && did_line_attr <= 1
5556# endif
5557 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558#endif
5559 ))
5560 {
5561 int n = 0;
5562
5563#ifdef FEAT_RIGHTLEFT
5564 if (wp->w_p_rl)
5565 {
5566 if (col < 0)
5567 n = 1;
5568 }
5569 else
5570#endif
5571 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005572 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 n = -1;
5574 }
5575 if (n != 0)
5576 {
5577 /* At the window boundary, highlight the last character
5578 * instead (better than nothing). */
5579 off += n;
5580 col += n;
5581 }
5582 else
5583 {
5584 /* Add a blank character to highlight. */
5585 ScreenLines[off] = ' ';
5586#ifdef FEAT_MBYTE
5587 if (enc_utf8)
5588 ScreenLinesUC[off] = 0;
5589#endif
5590 }
5591#ifdef FEAT_SEARCH_EXTRA
5592 if (area_attr == 0)
5593 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005594 /* Use attributes from match with highest priority among
5595 * 'search_hl' and the match list. */
5596 char_attr = search_hl.attr;
5597 cur = wp->w_match_head;
5598 shl_flag = FALSE;
5599 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005600 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005601 if (shl_flag == FALSE
5602 && ((cur != NULL
5603 && cur->priority > SEARCH_HL_PRIORITY)
5604 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005605 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005606 shl = &search_hl;
5607 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005608 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005609 else
5610 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005611 if ((ptr - line) - 1 == (long)shl->startcol
5612 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005613 char_attr = shl->attr;
5614 if (shl != &search_hl && cur != NULL)
5615 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
5618#endif
5619 ScreenAttrs[off] = char_attr;
5620#ifdef FEAT_RIGHTLEFT
5621 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005624 --off;
5625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 else
5627#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005630 ++off;
5631 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005632 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005633#ifdef FEAT_SYN_HL
5634 eol_hl_off = 1;
5635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
Bram Moolenaar91170f82006-05-05 21:15:17 +00005639 /*
5640 * At end of the text line.
5641 */
5642 if (c == NUL)
5643 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005644#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005645 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005646 if (wp->w_p_wrap)
5647 v = wp->w_skipcol;
5648 else
5649 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005650
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005651 /* check if line ends before left margin */
5652 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005653 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005654#ifdef FEAT_CONCEAL
5655 /* Get rid of the boguscols now, we want to draw until the right
5656 * edge for 'cursorcolumn'. */
5657 col -= boguscols;
5658 boguscols = 0;
5659#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005660
5661 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005662 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005663
5664 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005665 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5666 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005667 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005668 && lnum != wp->w_cursor.lnum)
5669 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005670# ifdef FEAT_RIGHTLEFT
5671 && !wp->w_p_rl
5672# endif
5673 )
5674 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005675 int rightmost_vcol = 0;
5676 int i;
5677
5678 if (wp->w_p_cuc)
5679 rightmost_vcol = wp->w_virtcol;
5680 if (draw_color_col)
5681 /* determine rightmost colorcolumn to possibly draw */
5682 for (i = 0; color_cols[i] >= 0; ++i)
5683 if (rightmost_vcol < color_cols[i])
5684 rightmost_vcol = color_cols[i];
5685
Bram Moolenaar02631462017-09-22 15:20:32 +02005686 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005687 {
5688 ScreenLines[off] = ' ';
5689#ifdef FEAT_MBYTE
5690 if (enc_utf8)
5691 ScreenLinesUC[off] = 0;
5692#endif
5693 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005694 if (draw_color_col)
5695 draw_color_col = advance_color_col(VCOL_HLC,
5696 &color_cols);
5697
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005698 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005699 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005700 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005701 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005702 else
5703 ScreenAttrs[off++] = 0;
5704
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005705 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005706 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005707
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005708 ++vcol;
5709 }
5710 }
5711#endif
5712
Bram Moolenaar53f81742017-09-22 14:35:51 +02005713 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005714 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 row++;
5716
5717 /*
5718 * Update w_cline_height and w_cline_folded if the cursor line was
5719 * updated (saves a call to plines() later).
5720 */
5721 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5722 {
5723 curwin->w_cline_row = startrow;
5724 curwin->w_cline_height = row - startrow;
5725#ifdef FEAT_FOLDING
5726 curwin->w_cline_folded = FALSE;
5727#endif
5728 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5729 }
5730
5731 break;
5732 }
5733
5734 /* line continues beyond line end */
5735 if (lcs_ext
5736 && !wp->w_p_wrap
5737#ifdef FEAT_DIFF
5738 && filler_todo <= 0
5739#endif
5740 && (
5741#ifdef FEAT_RIGHTLEFT
5742 wp->w_p_rl ? col == 0 :
5743#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005744 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005746 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5748 {
5749 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005750 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#ifdef FEAT_MBYTE
5752 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005753 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005756 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005757 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759 else
5760 mb_utf8 = FALSE;
5761#endif
5762 }
5763
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005764#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005765 /* advance to the next 'colorcolumn' */
5766 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005767 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005768
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005769 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005770 * highlight the cursor position itself.
5771 * Also highlight the 'colorcolumn' if it is different than
5772 * 'cursorcolumn' */
5773 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005774 if (draw_state == WL_LINE && !lnum_in_visual_area
5775 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005776 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005777 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005778 && lnum != wp->w_cursor.lnum)
5779 {
5780 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005781 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005782 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005783 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005784 {
5785 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005786 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005787 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005788 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005789#endif
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 /*
5792 * Store character to be displayed.
5793 * Skip characters that are left of the screen for 'nowrap'.
5794 */
5795 vcol_prev = vcol;
5796 if (draw_state < WL_LINE || n_skip <= 0)
5797 {
5798 /*
5799 * Store the character.
5800 */
5801#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5802 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5803 {
5804 /* A double-wide character is: put first halve in left cell. */
5805 --off;
5806 --col;
5807 }
5808#endif
5809 ScreenLines[off] = c;
5810#ifdef FEAT_MBYTE
5811 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005812 {
5813 if ((mb_c & 0xff00) == 0x8e00)
5814 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 else if (enc_utf8)
5818 {
5819 if (mb_utf8)
5820 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005821 int i;
5822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005824 if ((c & 0xff) == 0)
5825 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005826 for (i = 0; i < Screen_mco; ++i)
5827 {
5828 ScreenLinesC[i][off] = u8cc[i];
5829 if (u8cc[i] == 0)
5830 break;
5831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
5833 else
5834 ScreenLinesUC[off] = 0;
5835 }
5836 if (multi_attr)
5837 {
5838 ScreenAttrs[off] = multi_attr;
5839 multi_attr = 0;
5840 }
5841 else
5842#endif
5843 ScreenAttrs[off] = char_attr;
5844
5845#ifdef FEAT_MBYTE
5846 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5847 {
5848 /* Need to fill two screen columns. */
5849 ++off;
5850 ++col;
5851 if (enc_utf8)
5852 /* UTF-8: Put a 0 in the second screen char. */
5853 ScreenLines[off] = 0;
5854 else
5855 /* DBCS: Put second byte in the second screen char. */
5856 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005857 if (draw_state > WL_NR
5858#ifdef FEAT_DIFF
5859 && filler_todo <= 0
5860#endif
5861 )
5862 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 /* When "tocol" is halfway a character, set it to the end of
5864 * the character, otherwise highlighting won't stop. */
5865 if (tocol == vcol)
5866 ++tocol;
5867#ifdef FEAT_RIGHTLEFT
5868 if (wp->w_p_rl)
5869 {
5870 /* now it's time to backup one cell */
5871 --off;
5872 --col;
5873 }
5874#endif
5875 }
5876#endif
5877#ifdef FEAT_RIGHTLEFT
5878 if (wp->w_p_rl)
5879 {
5880 --off;
5881 --col;
5882 }
5883 else
5884#endif
5885 {
5886 ++off;
5887 ++col;
5888 }
5889 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005890#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005891 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005892 {
5893 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005894 ++vcol_off;
5895 if (n_extra > 0)
5896 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005897 if (wp->w_p_wrap)
5898 {
5899 /*
5900 * Special voodoo required if 'wrap' is on.
5901 *
5902 * Advance the column indicator to force the line
5903 * drawing to wrap early. This will make the line
5904 * take up the same screen space when parts are concealed,
5905 * so that cursor line computations aren't messed up.
5906 *
5907 * To avoid the fictitious advance of 'col' causing
5908 * trailing junk to be written out of the screen line
5909 * we are building, 'boguscols' keeps track of the number
5910 * of bad columns we have advanced.
5911 */
5912 if (n_extra > 0)
5913 {
5914 vcol += n_extra;
5915# ifdef FEAT_RIGHTLEFT
5916 if (wp->w_p_rl)
5917 {
5918 col -= n_extra;
5919 boguscols -= n_extra;
5920 }
5921 else
5922# endif
5923 {
5924 col += n_extra;
5925 boguscols += n_extra;
5926 }
5927 n_extra = 0;
5928 n_attr = 0;
5929 }
5930
5931
5932# ifdef FEAT_MBYTE
5933 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5934 {
5935 /* Need to fill two screen columns. */
5936# ifdef FEAT_RIGHTLEFT
5937 if (wp->w_p_rl)
5938 {
5939 --boguscols;
5940 --col;
5941 }
5942 else
5943# endif
5944 {
5945 ++boguscols;
5946 ++col;
5947 }
5948 }
5949# endif
5950
5951# ifdef FEAT_RIGHTLEFT
5952 if (wp->w_p_rl)
5953 {
5954 --boguscols;
5955 --col;
5956 }
5957 else
5958# endif
5959 {
5960 ++boguscols;
5961 ++col;
5962 }
5963 }
5964 else
5965 {
5966 if (n_extra > 0)
5967 {
5968 vcol += n_extra;
5969 n_extra = 0;
5970 n_attr = 0;
5971 }
5972 }
5973
5974 }
5975#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 else
5977 --n_skip;
5978
Bram Moolenaar64486672010-05-16 15:46:46 +02005979 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5980 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005981 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982#ifdef FEAT_DIFF
5983 && filler_todo <= 0
5984#endif
5985 )
5986 ++vcol;
5987
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005988#ifdef FEAT_SYN_HL
5989 if (vcol_save_attr >= 0)
5990 char_attr = vcol_save_attr;
5991#endif
5992
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 /* restore attributes after "predeces" in 'listchars' */
5994 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5995 char_attr = saved_attr3;
5996
5997 /* restore attributes after last 'listchars' or 'number' char */
5998 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5999 char_attr = saved_attr2;
6000
6001 /*
6002 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006003 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 */
6005 if ((
6006#ifdef FEAT_RIGHTLEFT
6007 wp->w_p_rl ? (col < 0) :
6008#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006009 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 && (*ptr != NUL
6011#ifdef FEAT_DIFF
6012 || filler_todo > 0
6013#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006014 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6016 )
6017 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006018#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006019 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02006020 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006021 boguscols = 0;
6022#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006023 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02006024 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 ++row;
6027 ++screen_row;
6028
6029 /* When not wrapping and finished diff lines, or when displayed
6030 * '$' and highlighting until last column, break here. */
6031 if ((!wp->w_p_wrap
6032#ifdef FEAT_DIFF
6033 && filler_todo <= 0
6034#endif
6035 ) || lcs_eol_one == -1)
6036 break;
6037
6038 /* When the window is too narrow draw all "@" lines. */
6039 if (draw_state != WL_LINE
6040#ifdef FEAT_DIFF
6041 && filler_todo <= 0
6042#endif
6043 )
6044 {
6045 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 row = endrow;
6048 }
6049
6050 /* When line got too long for screen break here. */
6051 if (row == endrow)
6052 {
6053 ++row;
6054 break;
6055 }
6056
6057 if (screen_cur_row == screen_row - 1
6058#ifdef FEAT_DIFF
6059 && filler_todo <= 0
6060#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006061 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 {
6063 /* Remember that the line wraps, used for modeless copy. */
6064 LineWraps[screen_row - 1] = TRUE;
6065
6066 /*
6067 * Special trick to make copy/paste of wrapped lines work with
6068 * xterm/screen: write an extra character beyond the end of
6069 * the line. This will work with all terminal types
6070 * (regardless of the xn,am settings).
6071 * Only do this on a fast tty.
6072 * Only do this if the cursor is on the current line
6073 * (something has been written in it).
6074 * Don't do this for the GUI.
6075 * Don't do this for double-width characters.
6076 * Don't do this for a window not at the right screen border.
6077 */
6078 if (p_tf
6079#ifdef FEAT_GUI
6080 && !gui.in_use
6081#endif
6082#ifdef FEAT_MBYTE
6083 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006084 && ((*mb_off2cells)(LineOffset[screen_row],
6085 LineOffset[screen_row] + screen_Columns)
6086 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006088 + (int)Columns - 2,
6089 LineOffset[screen_row] + screen_Columns)
6090 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091#endif
6092 )
6093 {
6094 /* First make sure we are at the end of the screen line,
6095 * then output the same character again to let the
6096 * terminal know about the wrap. If the terminal doesn't
6097 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006098 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 screen_char(LineOffset[screen_row - 1]
6100 + (unsigned)Columns - 1,
6101 screen_row - 1, (int)(Columns - 1));
6102
6103#ifdef FEAT_MBYTE
6104 /* When there is a multi-byte character, just output a
6105 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006106 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6107 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 out_char(' ');
6109 else
6110#endif
6111 out_char(ScreenLines[LineOffset[screen_row - 1]
6112 + (Columns - 1)]);
6113 /* force a redraw of the first char on the next line */
6114 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6115 screen_start(); /* don't know where cursor is now */
6116 }
6117 }
6118
6119 col = 0;
6120 off = (unsigned)(current_ScreenLine - ScreenLines);
6121#ifdef FEAT_RIGHTLEFT
6122 if (wp->w_p_rl)
6123 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006124 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 off += col;
6126 }
6127#endif
6128
6129 /* reset the drawing state for the start of a wrapped line */
6130 draw_state = WL_START;
6131 saved_n_extra = n_extra;
6132 saved_p_extra = p_extra;
6133 saved_c_extra = c_extra;
6134 saved_char_attr = char_attr;
6135 n_extra = 0;
6136 lcs_prec_todo = lcs_prec;
6137#ifdef FEAT_LINEBREAK
6138# ifdef FEAT_DIFF
6139 if (filler_todo <= 0)
6140# endif
6141 need_showbreak = TRUE;
6142#endif
6143#ifdef FEAT_DIFF
6144 --filler_todo;
6145 /* When the filler lines are actually below the last line of the
6146 * file, don't draw the line itself, break here. */
6147 if (filler_todo == 0 && wp->w_botfill)
6148 break;
6149#endif
6150 }
6151
6152 } /* for every character in the line */
6153
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006154#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006155 /* After an empty line check first word for capital. */
6156 if (*skipwhite(line) == NUL)
6157 {
6158 capcol_lnum = lnum + 1;
6159 cap_col = 0;
6160 }
6161#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006162#ifdef FEAT_TEXT_PROP
6163 vim_free(text_props);
6164 vim_free(text_prop_idxs);
6165#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006166
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006167 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 return row;
6169}
6170
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006171#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006172/*
6173 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006174 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006175 */
6176 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006177comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006178{
6179 int i;
6180
6181 for (i = 0; i < Screen_mco; ++i)
6182 {
6183 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6184 return TRUE;
6185 if (ScreenLinesC[i][off_from] == 0)
6186 break;
6187 }
6188 return FALSE;
6189}
6190#endif
6191
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192/*
6193 * Check whether the given character needs redrawing:
6194 * - the (first byte of the) character is different
6195 * - the attributes are different
6196 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006197 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 */
6199 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006200char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 if (cols > 0
6203 && ((ScreenLines[off_from] != ScreenLines[off_to]
6204 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6205
6206#ifdef FEAT_MBYTE
6207 || (enc_dbcs != 0
6208 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6209 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6210 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6211 : (cols > 1 && ScreenLines[off_from + 1]
6212 != ScreenLines[off_to + 1])))
6213 || (enc_utf8
6214 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6215 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006216 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006217 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6218 && ScreenLines[off_from + 1]
6219 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220#endif
6221 ))
6222 return TRUE;
6223 return FALSE;
6224}
6225
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006226#if defined(FEAT_TERMINAL) || defined(PROTO)
6227/*
6228 * Return the index in ScreenLines[] for the current screen line.
6229 */
6230 int
6231screen_get_current_line_off()
6232{
6233 return (int)(current_ScreenLine - ScreenLines);
6234}
6235#endif
6236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237/*
6238 * Move one "cooked" screen line to the screen, but only the characters that
6239 * have actually changed. Handle insert/delete character.
6240 * "coloff" gives the first column on the screen for this line.
6241 * "endcol" gives the columns where valid characters are.
6242 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6243 * needs to be cleared, negative otherwise.
6244 * "rlflag" is TRUE in a rightleft window:
6245 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6246 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6247 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006249screen_line(
6250 int row,
6251 int coloff,
6252 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006253 int clear_width,
6254 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255{
6256 unsigned off_from;
6257 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006258#ifdef FEAT_MBYTE
6259 unsigned max_off_from;
6260 unsigned max_off_to;
6261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 int force = FALSE; /* force update rest of the line */
6265 int redraw_this /* bool: does character need redraw? */
6266#ifdef FEAT_GUI
6267 = TRUE /* For GUI when while-loop empty */
6268#endif
6269 ;
6270 int redraw_next; /* redraw_this for next character */
6271#ifdef FEAT_MBYTE
6272 int clear_next = FALSE;
6273 int char_cells; /* 1: normal char */
6274 /* 2: occupies two display cells */
6275# define CHAR_CELLS char_cells
6276#else
6277# define CHAR_CELLS 1
6278#endif
6279
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006280 /* Check for illegal row and col, just in case. */
6281 if (row >= Rows)
6282 row = Rows - 1;
6283 if (endcol > Columns)
6284 endcol = Columns;
6285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286# ifdef FEAT_CLIPBOARD
6287 clip_may_clear_selection(row, row);
6288# endif
6289
6290 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6291 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006292#ifdef FEAT_MBYTE
6293 max_off_from = off_from + screen_Columns;
6294 max_off_to = LineOffset[row] + screen_Columns;
6295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
6297#ifdef FEAT_RIGHTLEFT
6298 if (rlflag)
6299 {
6300 /* Clear rest first, because it's left of the text. */
6301 if (clear_width > 0)
6302 {
6303 while (col <= endcol && ScreenLines[off_to] == ' '
6304 && ScreenAttrs[off_to] == 0
6305# ifdef FEAT_MBYTE
6306 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6307# endif
6308 )
6309 {
6310 ++off_to;
6311 ++col;
6312 }
6313 if (col <= endcol)
6314 screen_fill(row, row + 1, col + coloff,
6315 endcol + coloff + 1, ' ', ' ', 0);
6316 }
6317 col = endcol + 1;
6318 off_to = LineOffset[row] + col + coloff;
6319 off_from += col;
6320 endcol = (clear_width > 0 ? clear_width : -clear_width);
6321 }
6322#endif /* FEAT_RIGHTLEFT */
6323
6324 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6325
6326 while (col < endcol)
6327 {
6328#ifdef FEAT_MBYTE
6329 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006330 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 else
6332 char_cells = 1;
6333#endif
6334
6335 redraw_this = redraw_next;
6336 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6337 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6338
6339#ifdef FEAT_GUI
6340 /* If the next character was bold, then redraw the current character to
6341 * remove any pixels that might have spilt over into us. This only
6342 * happens in the GUI.
6343 */
6344 if (redraw_next && gui.in_use)
6345 {
6346 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006347 if (hl > HL_ALL)
6348 hl = syn_attr2attr(hl);
6349 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 redraw_this = TRUE;
6351 }
6352#endif
6353
6354 if (redraw_this)
6355 {
6356 /*
6357 * Special handling when 'xs' termcap flag set (hpterm):
6358 * Attributes for characters are stored at the position where the
6359 * cursor is when writing the highlighting code. The
6360 * start-highlighting code must be written with the cursor on the
6361 * first highlighted character. The stop-highlighting code must
6362 * be written with the cursor just after the last highlighted
6363 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006364 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 * to clear the rest of the line, and force redrawing it
6366 * completely.
6367 */
6368 if ( p_wiv
6369 && !force
6370#ifdef FEAT_GUI
6371 && !gui.in_use
6372#endif
6373 && ScreenAttrs[off_to] != 0
6374 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6375 {
6376 /*
6377 * Need to remove highlighting attributes here.
6378 */
6379 windgoto(row, col + coloff);
6380 out_str(T_CE); /* clear rest of this screen line */
6381 screen_start(); /* don't know where cursor is now */
6382 force = TRUE; /* force redraw of rest of the line */
6383 redraw_next = TRUE; /* or else next char would miss out */
6384
6385 /*
6386 * If the previous character was highlighted, need to stop
6387 * highlighting at this character.
6388 */
6389 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6390 {
6391 screen_attr = ScreenAttrs[off_to - 1];
6392 term_windgoto(row, col + coloff);
6393 screen_stop_highlight();
6394 }
6395 else
6396 screen_attr = 0; /* highlighting has stopped */
6397 }
6398#ifdef FEAT_MBYTE
6399 if (enc_dbcs != 0)
6400 {
6401 /* Check if overwriting a double-byte with a single-byte or
6402 * the other way around requires another character to be
6403 * redrawn. For UTF-8 this isn't needed, because comparing
6404 * ScreenLinesUC[] is sufficient. */
6405 if (char_cells == 1
6406 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006407 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 {
6409 /* Writing a single-cell character over a double-cell
6410 * character: need to redraw the next cell. */
6411 ScreenLines[off_to + 1] = 0;
6412 redraw_next = TRUE;
6413 }
6414 else if (char_cells == 2
6415 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006416 && (*mb_off2cells)(off_to, max_off_to) == 1
6417 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 {
6419 /* Writing the second half of a double-cell character over
6420 * a double-cell character: need to redraw the second
6421 * cell. */
6422 ScreenLines[off_to + 2] = 0;
6423 redraw_next = TRUE;
6424 }
6425
6426 if (enc_dbcs == DBCS_JPNU)
6427 ScreenLines2[off_to] = ScreenLines2[off_from];
6428 }
6429 /* When writing a single-width character over a double-width
6430 * character and at the end of the redrawn text, need to clear out
6431 * the right halve of the old character.
6432 * Also required when writing the right halve of a double-width
6433 * char over the left halve of an existing one. */
6434 if (has_mbyte && col + char_cells == endcol
6435 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006436 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006438 && (*mb_off2cells)(off_to, max_off_to) == 1
6439 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 clear_next = TRUE;
6441#endif
6442
6443 ScreenLines[off_to] = ScreenLines[off_from];
6444#ifdef FEAT_MBYTE
6445 if (enc_utf8)
6446 {
6447 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6448 if (ScreenLinesUC[off_from] != 0)
6449 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006450 int i;
6451
6452 for (i = 0; i < Screen_mco; ++i)
6453 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
6455 }
6456 if (char_cells == 2)
6457 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6458#endif
6459
6460#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006461 /* The bold trick makes a single column of pixels appear in the
6462 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 * character should be redrawn too. This happens for our own GUI
6464 * and for some xterms. */
6465 if (
6466# ifdef FEAT_GUI
6467 gui.in_use
6468# endif
6469# if defined(FEAT_GUI) && defined(UNIX)
6470 ||
6471# endif
6472# ifdef UNIX
6473 term_is_xterm
6474# endif
6475 )
6476 {
6477 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006478 if (hl > HL_ALL)
6479 hl = syn_attr2attr(hl);
6480 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 redraw_next = TRUE;
6482 }
6483#endif
6484 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6485#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006486 /* For simplicity set the attributes of second half of a
6487 * double-wide character equal to the first half. */
6488 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006490
6491 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 else
6494#endif
6495 screen_char(off_to, row, col + coloff);
6496 }
6497 else if ( p_wiv
6498#ifdef FEAT_GUI
6499 && !gui.in_use
6500#endif
6501 && col + coloff > 0)
6502 {
6503 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6504 {
6505 /*
6506 * Don't output stop-highlight when moving the cursor, it will
6507 * stop the highlighting when it should continue.
6508 */
6509 screen_attr = 0;
6510 }
6511 else if (screen_attr != 0)
6512 screen_stop_highlight();
6513 }
6514
6515 off_to += CHAR_CELLS;
6516 off_from += CHAR_CELLS;
6517 col += CHAR_CELLS;
6518 }
6519
6520#ifdef FEAT_MBYTE
6521 if (clear_next)
6522 {
6523 /* Clear the second half of a double-wide character of which the left
6524 * half was overwritten with a single-wide character. */
6525 ScreenLines[off_to] = ' ';
6526 if (enc_utf8)
6527 ScreenLinesUC[off_to] = 0;
6528 screen_char(off_to, row, col + coloff);
6529 }
6530#endif
6531
6532 if (clear_width > 0
6533#ifdef FEAT_RIGHTLEFT
6534 && !rlflag
6535#endif
6536 )
6537 {
6538#ifdef FEAT_GUI
6539 int startCol = col;
6540#endif
6541
6542 /* blank out the rest of the line */
6543 while (col < clear_width && ScreenLines[off_to] == ' '
6544 && ScreenAttrs[off_to] == 0
6545#ifdef FEAT_MBYTE
6546 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6547#endif
6548 )
6549 {
6550 ++off_to;
6551 ++col;
6552 }
6553 if (col < clear_width)
6554 {
6555#ifdef FEAT_GUI
6556 /*
6557 * In the GUI, clearing the rest of the line may leave pixels
6558 * behind if the first character cleared was bold. Some bold
6559 * fonts spill over the left. In this case we redraw the previous
6560 * character too. If we didn't skip any blanks above, then we
6561 * only redraw if the character wasn't already redrawn anyway.
6562 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006563 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 {
6565 hl = ScreenAttrs[off_to];
6566 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006567 {
6568 int prev_cells = 1;
6569# ifdef FEAT_MBYTE
6570 if (enc_utf8)
6571 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6572 * that its width is 2. */
6573 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6574 else if (enc_dbcs != 0)
6575 {
6576 /* find previous character by counting from first
6577 * column and get its width. */
6578 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006579 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006580
6581 while (off < off_to)
6582 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006583 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006584 off += prev_cells;
6585 }
6586 }
6587
6588 if (enc_dbcs != 0 && prev_cells > 1)
6589 screen_char_2(off_to - prev_cells, row,
6590 col + coloff - prev_cells);
6591 else
6592# endif
6593 screen_char(off_to - prev_cells, row,
6594 col + coloff - prev_cells);
6595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 }
6597#endif
6598 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6599 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 off_to += clear_width - col;
6601 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 }
6603 }
6604
6605 if (clear_width > 0)
6606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 /* For a window that's left of another, draw the separator char. */
6608 if (col + coloff < Columns)
6609 {
6610 int c;
6611
6612 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006613 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006614#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006615 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6616 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 || ScreenAttrs[off_to] != hl)
6619 {
6620 ScreenLines[off_to] = c;
6621 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006622#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (enc_utf8)
6624 {
6625 if (c >= 0x80)
6626 {
6627 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006628 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 }
6630 else
6631 ScreenLinesUC[off_to] = 0;
6632 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 screen_char(off_to, row, col + coloff);
6635 }
6636 }
6637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 LineWraps[row] = FALSE;
6639 }
6640}
6641
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006642#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006644 * Mirror text "str" for right-left displaying.
6645 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006647 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006648rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649{
6650 char_u *p1, *p2;
6651 int t;
6652
6653 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6654 {
6655 t = *p1;
6656 *p1 = *p2;
6657 *p2 = t;
6658 }
6659}
6660#endif
6661
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662/*
6663 * mark all status lines for redraw; used after first :cd
6664 */
6665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006666status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 win_T *wp;
6669
Bram Moolenaar29323592016-07-24 22:04:11 +02006670 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 if (wp->w_status_height)
6672 {
6673 wp->w_redr_status = TRUE;
6674 redraw_later(VALID);
6675 }
6676}
6677
6678/*
6679 * mark all status lines of the current buffer for redraw
6680 */
6681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006682status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684 win_T *wp;
6685
Bram Moolenaar29323592016-07-24 22:04:11 +02006686 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6688 {
6689 wp->w_redr_status = TRUE;
6690 redraw_later(VALID);
6691 }
6692}
6693
6694/*
6695 * Redraw all status lines that need to be redrawn.
6696 */
6697 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006698redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 win_T *wp;
6701
Bram Moolenaar29323592016-07-24 22:04:11 +02006702 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006704 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006705 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006706 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
Bram Moolenaar4033c552017-09-16 20:54:51 +02006709#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710/*
6711 * Redraw all status lines at the bottom of frame "frp".
6712 */
6713 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006714win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
6716 if (frp->fr_layout == FR_LEAF)
6717 frp->fr_win->w_redr_status = TRUE;
6718 else if (frp->fr_layout == FR_ROW)
6719 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006720 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 win_redraw_last_status(frp);
6722 }
6723 else /* frp->fr_layout == FR_COL */
6724 {
6725 frp = frp->fr_child;
6726 while (frp->fr_next != NULL)
6727 frp = frp->fr_next;
6728 win_redraw_last_status(frp);
6729 }
6730}
6731#endif
6732
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733/*
6734 * Draw the verticap separator right of window "wp" starting with line "row".
6735 */
6736 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006737draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 int hl;
6740 int c;
6741
6742 if (wp->w_vsep_width)
6743 {
6744 /* draw the vertical separator right of this window */
6745 c = fillchar_vsep(&hl);
6746 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6747 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6748 c, ' ', hl);
6749 }
6750}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
6752#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006753static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
6755/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006756 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 */
6758 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006759status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761 int len = 0;
6762
6763#ifdef FEAT_MENU
6764 int emenu = (xp->xp_context == EXPAND_MENUS
6765 || xp->xp_context == EXPAND_MENUNAMES);
6766
6767 /* Check for menu separators - replace with '|'. */
6768 if (emenu && menu_is_separator(s))
6769 return 1;
6770#endif
6771
6772 while (*s != NUL)
6773 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006774 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006775 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006776 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 }
6778
6779 return len;
6780}
6781
6782/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006783 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006784 * These are backslashes used for escaping. Do show backslashes in help tags.
6785 */
6786 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006787skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006788{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006789 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006790#ifdef FEAT_MENU
6791 || ((xp->xp_context == EXPAND_MENUS
6792 || xp->xp_context == EXPAND_MENUNAMES)
6793 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6794#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006795 )
6796 {
6797#ifndef BACKSLASH_IN_FILENAME
6798 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6799 return 2;
6800#endif
6801 return 1;
6802 }
6803 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006804}
6805
6806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 * Show wildchar matches in the status line.
6808 * Show at least the "match" item.
6809 * We start at item 'first_match' in the list and show all matches that fit.
6810 *
6811 * If inversion is possible we use it. Else '=' characters are used.
6812 */
6813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006814win_redr_status_matches(
6815 expand_T *xp,
6816 int num_matches,
6817 char_u **matches, /* list of matches */
6818 int match,
6819 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6822 int row;
6823 char_u *buf;
6824 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006825 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 int fillchar;
6827 int attr;
6828 int i;
6829 int highlight = TRUE;
6830 char_u *selstart = NULL;
6831 int selstart_col = 0;
6832 char_u *selend = NULL;
6833 static int first_match = 0;
6834 int add_left = FALSE;
6835 char_u *s;
6836#ifdef FEAT_MENU
6837 int emenu;
6838#endif
6839#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6840 int l;
6841#endif
6842
6843 if (matches == NULL) /* interrupted completion? */
6844 return;
6845
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006846#ifdef FEAT_MBYTE
6847 if (has_mbyte)
6848 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6849 else
6850#endif
6851 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 if (buf == NULL)
6853 return;
6854
6855 if (match == -1) /* don't show match but original text */
6856 {
6857 match = 0;
6858 highlight = FALSE;
6859 }
6860 /* count 1 for the ending ">" */
6861 clen = status_match_len(xp, L_MATCH(match)) + 3;
6862 if (match == 0)
6863 first_match = 0;
6864 else if (match < first_match)
6865 {
6866 /* jumping left, as far as we can go */
6867 first_match = match;
6868 add_left = TRUE;
6869 }
6870 else
6871 {
6872 /* check if match fits on the screen */
6873 for (i = first_match; i < match; ++i)
6874 clen += status_match_len(xp, L_MATCH(i)) + 2;
6875 if (first_match > 0)
6876 clen += 2;
6877 /* jumping right, put match at the left */
6878 if ((long)clen > Columns)
6879 {
6880 first_match = match;
6881 /* if showing the last match, we can add some on the left */
6882 clen = 2;
6883 for (i = match; i < num_matches; ++i)
6884 {
6885 clen += status_match_len(xp, L_MATCH(i)) + 2;
6886 if ((long)clen >= Columns)
6887 break;
6888 }
6889 if (i == num_matches)
6890 add_left = TRUE;
6891 }
6892 }
6893 if (add_left)
6894 while (first_match > 0)
6895 {
6896 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6897 if ((long)clen >= Columns)
6898 break;
6899 --first_match;
6900 }
6901
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006902 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
6904 if (first_match == 0)
6905 {
6906 *buf = NUL;
6907 len = 0;
6908 }
6909 else
6910 {
6911 STRCPY(buf, "< ");
6912 len = 2;
6913 }
6914 clen = len;
6915
6916 i = first_match;
6917 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6918 {
6919 if (i == match)
6920 {
6921 selstart = buf + len;
6922 selstart_col = clen;
6923 }
6924
6925 s = L_MATCH(i);
6926 /* Check for menu separators - replace with '|' */
6927#ifdef FEAT_MENU
6928 emenu = (xp->xp_context == EXPAND_MENUS
6929 || xp->xp_context == EXPAND_MENUNAMES);
6930 if (emenu && menu_is_separator(s))
6931 {
6932 STRCPY(buf + len, transchar('|'));
6933 l = (int)STRLEN(buf + len);
6934 len += l;
6935 clen += l;
6936 }
6937 else
6938#endif
6939 for ( ; *s != NUL; ++s)
6940 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006941 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 clen += ptr2cells(s);
6943#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006944 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
6946 STRNCPY(buf + len, s, l);
6947 s += l - 1;
6948 len += l;
6949 }
6950 else
6951#endif
6952 {
6953 STRCPY(buf + len, transchar_byte(*s));
6954 len += (int)STRLEN(buf + len);
6955 }
6956 }
6957 if (i == match)
6958 selend = buf + len;
6959
6960 *(buf + len++) = ' ';
6961 *(buf + len++) = ' ';
6962 clen += 2;
6963 if (++i == num_matches)
6964 break;
6965 }
6966
6967 if (i != num_matches)
6968 {
6969 *(buf + len++) = '>';
6970 ++clen;
6971 }
6972
6973 buf[len] = NUL;
6974
6975 row = cmdline_row - 1;
6976 if (row >= 0)
6977 {
6978 if (wild_menu_showing == 0)
6979 {
6980 if (msg_scrolled > 0)
6981 {
6982 /* Put the wildmenu just above the command line. If there is
6983 * no room, scroll the screen one line up. */
6984 if (cmdline_row == Rows - 1)
6985 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006986 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 ++msg_scrolled;
6988 }
6989 else
6990 {
6991 ++cmdline_row;
6992 ++row;
6993 }
6994 wild_menu_showing = WM_SCROLLED;
6995 }
6996 else
6997 {
6998 /* Create status line if needed by setting 'laststatus' to 2.
6999 * Set 'winminheight' to zero to avoid that the window is
7000 * resized. */
7001 if (lastwin->w_status_height == 0)
7002 {
7003 save_p_ls = p_ls;
7004 save_p_wmh = p_wmh;
7005 p_ls = 2;
7006 p_wmh = 0;
7007 last_status(FALSE);
7008 }
7009 wild_menu_showing = WM_SHOWN;
7010 }
7011 }
7012
7013 screen_puts(buf, row, 0, attr);
7014 if (selstart != NULL && highlight)
7015 {
7016 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007017 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
7019
7020 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7021 }
7022
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 vim_free(buf);
7025}
7026#endif
7027
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028/*
7029 * Redraw the status line of window wp.
7030 *
7031 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007032 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7033 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007035 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007036win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037{
7038 int row;
7039 char_u *p;
7040 int len;
7041 int fillchar;
7042 int attr;
7043 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007044 static int busy = FALSE;
7045
7046 /* It's possible to get here recursively when 'statusline' (indirectly)
7047 * invokes ":redrawstatus". Simply ignore the call then. */
7048 if (busy)
7049 return;
7050 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
7052 wp->w_redr_status = FALSE;
7053 if (wp->w_status_height == 0)
7054 {
7055 /* no status line, can only be last window */
7056 redraw_cmdline = TRUE;
7057 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007058 else if (!redrawing()
7059#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007060 // don't update status line when popup menu is visible and may be
7061 // drawn over it, unless it will be redrawn later
7062 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007063#endif
7064 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
7066 /* Don't redraw right now, do it later. */
7067 wp->w_redr_status = TRUE;
7068 }
7069#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007070 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {
7072 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007073 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 }
7075#endif
7076 else
7077 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007078 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007080 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 p = NameBuff;
7082 len = (int)STRLEN(p);
7083
Bram Moolenaard85f2712017-07-28 21:51:57 +02007084 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085#ifdef FEAT_QUICKFIX
7086 || wp->w_p_pvw
7087#endif
7088 || bufIsChanged(wp->w_buffer)
7089 || wp->w_buffer->b_p_ro)
7090 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007091 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007093 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 len += (int)STRLEN(p + len);
7095 }
7096#ifdef FEAT_QUICKFIX
7097 if (wp->w_p_pvw)
7098 {
7099 STRCPY(p + len, _("[Preview]"));
7100 len += (int)STRLEN(p + len);
7101 }
7102#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007103 if (bufIsChanged(wp->w_buffer)
7104#ifdef FEAT_TERMINAL
7105 && !bt_terminal(wp->w_buffer)
7106#endif
7107 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {
7109 STRCPY(p + len, "[+]");
7110 len += 3;
7111 }
7112 if (wp->w_buffer->b_p_ro)
7113 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007114 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007115 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 }
7117
Bram Moolenaar02631462017-09-22 15:20:32 +02007118 this_ru_col = ru_col - (Columns - wp->w_width);
7119 if (this_ru_col < (wp->w_width + 1) / 2)
7120 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 if (this_ru_col <= 1)
7122 {
7123 p = (char_u *)"<"; /* No room for file name! */
7124 len = 1;
7125 }
7126 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127#ifdef FEAT_MBYTE
7128 if (has_mbyte)
7129 {
7130 int clen = 0, i;
7131
7132 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007133 clen = mb_string2cells(p, -1);
7134
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 /* Find first character that will fit.
7136 * Going from start to end is much faster for DBCS. */
7137 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007138 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 clen -= (*mb_ptr2cells)(p + i);
7140 len = clen;
7141 if (i > 0)
7142 {
7143 p = p + i - 1;
7144 *p = '<';
7145 ++len;
7146 }
7147
7148 }
7149 else
7150#endif
7151 if (len > this_ru_col - 1)
7152 {
7153 p += len - (this_ru_col - 1);
7154 *p = '<';
7155 len = this_ru_col - 1;
7156 }
7157
7158 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007159 screen_puts(p, row, wp->w_wincol, attr);
7160 screen_fill(row, row + 1, len + wp->w_wincol,
7161 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007163 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7165 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007166 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
7168#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007169 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170#endif
7171 }
7172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 /*
7174 * May need to draw the character below the vertical separator.
7175 */
7176 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7177 {
7178 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007179 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 else
7181 fillchar = fillchar_vsep(&attr);
7182 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7183 attr);
7184 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007185 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186}
7187
Bram Moolenaar238a5642006-02-21 22:12:05 +00007188#ifdef FEAT_STL_OPT
7189/*
7190 * Redraw the status line according to 'statusline' and take care of any
7191 * errors encountered.
7192 */
7193 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007194redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007195{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007196 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007197 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007198
7199 /* When called recursively return. This can happen when the statusline
7200 * contains an expression that triggers a redraw. */
7201 if (entered)
7202 return;
7203 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007204
Bram Moolenaara742e082016-04-05 21:10:38 +02007205 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007206 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007207 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007208 {
7209 /* When there is an error disable the statusline, otherwise the
7210 * display is messed up with errors and a redraw triggers the problem
7211 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007212 set_string_option_direct((char_u *)"statusline", -1,
7213 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007214 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007215 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007216 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007217 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007218}
7219#endif
7220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221/*
7222 * Return TRUE if the status line of window "wp" is connected to the status
7223 * line of the window right of it. If not, then it's a vertical separator.
7224 * Only call if (wp->w_vsep_width != 0).
7225 */
7226 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007227stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
7229 frame_T *fr;
7230
7231 fr = wp->w_frame;
7232 while (fr->fr_parent != NULL)
7233 {
7234 if (fr->fr_parent->fr_layout == FR_COL)
7235 {
7236 if (fr->fr_next != NULL)
7237 break;
7238 }
7239 else
7240 {
7241 if (fr->fr_next != NULL)
7242 return TRUE;
7243 }
7244 fr = fr->fr_parent;
7245 }
7246 return FALSE;
7247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250/*
7251 * Get the value to show for the language mappings, active 'keymap'.
7252 */
7253 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007254get_keymap_str(
7255 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007256 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007257 char_u *buf, /* buffer for the result */
7258 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
7260 char_u *p;
7261
7262 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7263 return FALSE;
7264
7265 {
7266#ifdef FEAT_EVAL
7267 buf_T *old_curbuf = curbuf;
7268 win_T *old_curwin = curwin;
7269 char_u *s;
7270
7271 curbuf = wp->w_buffer;
7272 curwin = wp;
7273 STRCPY(buf, "b:keymap_name"); /* must be writable */
7274 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007275 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 --emsg_skip;
7277 curbuf = old_curbuf;
7278 curwin = old_curwin;
7279 if (p == NULL || *p == NUL)
7280#endif
7281 {
7282#ifdef FEAT_KEYMAP
7283 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7284 p = wp->w_buffer->b_p_keymap;
7285 else
7286#endif
7287 p = (char_u *)"lang";
7288 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007289 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 buf[0] = NUL;
7291#ifdef FEAT_EVAL
7292 vim_free(s);
7293#endif
7294 }
7295 return buf[0] != NUL;
7296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
7298#if defined(FEAT_STL_OPT) || defined(PROTO)
7299/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007300 * Redraw the status line or ruler of window "wp".
7301 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 */
7303 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007304win_redr_custom(
7305 win_T *wp,
7306 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007308 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 int attr;
7310 int curattr;
7311 int row;
7312 int col = 0;
7313 int maxwidth;
7314 int width;
7315 int n;
7316 int len;
7317 int fillchar;
7318 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007319 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007321 struct stl_hlrec hltab[STL_MAX_ITEM];
7322 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007323 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007324 win_T *ewp;
7325 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326
Bram Moolenaar1d633412013-12-11 15:52:01 +01007327 /* There is a tiny chance that this gets called recursively: When
7328 * redrawing a status line triggers redrawing the ruler or tabline.
7329 * Avoid trouble by not allowing recursion. */
7330 if (entered)
7331 return;
7332 entered = TRUE;
7333
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007335 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007337 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007338 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007339 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007340 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007341 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007342 maxwidth = Columns;
7343# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007344 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007347 else
7348 {
7349 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007350 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007351 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007352
7353 if (draw_ruler)
7354 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007355 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007356 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007357 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007358 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007359 if (*++stl == '-')
7360 stl++;
7361 if (atoi((char *)stl))
7362 while (VIM_ISDIGIT(*stl))
7363 stl++;
7364 if (*stl++ != '(')
7365 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007366 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007367 col = ru_col - (Columns - wp->w_width);
7368 if (col < (wp->w_width + 1) / 2)
7369 col = (wp->w_width + 1) / 2;
7370 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007371 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007372 {
7373 row = Rows - 1;
7374 --maxwidth; /* writing in last column may cause scrolling */
7375 fillchar = ' ';
7376 attr = 0;
7377 }
7378
7379# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007380 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007381# endif
7382 }
7383 else
7384 {
7385 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007386 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007387 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007388 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007389# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007390 use_sandbox = was_set_insecurely((char_u *)"statusline",
7391 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007392# endif
7393 }
7394
Bram Moolenaar53f81742017-09-22 14:35:51 +02007395 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007396 }
7397
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007399 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaar61452852011-02-01 18:01:11 +01007401 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7402 * the cursor away and back. */
7403 ewp = wp == NULL ? curwin : wp;
7404 p_crb_save = ewp->w_p_crb;
7405 ewp->w_p_crb = FALSE;
7406
Bram Moolenaar362f3562009-11-03 16:20:34 +00007407 /* Make a copy, because the statusline may include a function call that
7408 * might change the option value and free the memory. */
7409 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007410 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007411 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007412 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007413 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007414 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007416 /* Make all characters printable. */
7417 p = transstr(buf);
7418 if (p != NULL)
7419 {
7420 vim_strncpy(buf, p, sizeof(buf) - 1);
7421 vim_free(p);
7422 }
7423
7424 /* fill up with "fillchar" */
7425 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007426 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 {
7428#ifdef FEAT_MBYTE
7429 len += (*mb_char2bytes)(fillchar, buf + len);
7430#else
7431 buf[len++] = fillchar;
7432#endif
7433 ++width;
7434 }
7435 buf[len] = NUL;
7436
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007437 /*
7438 * Draw each snippet with the specified highlighting.
7439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 curattr = attr;
7441 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007442 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007444 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 screen_puts_len(p, len, row, col, curattr);
7446 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007447 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007449 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007451 else if (hltab[n].userhl < 0)
7452 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007453#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007454 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7455 && wp->w_status_height != 0)
7456 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007457 else if (wp != NULL && bt_terminal(wp->w_buffer)
7458 && wp->w_status_height != 0)
7459 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007460#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007461 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007462 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007464 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 }
7466 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007467
7468 if (wp == NULL)
7469 {
7470 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7471 col = 0;
7472 len = 0;
7473 p = buf;
7474 fillchar = 0;
7475 for (n = 0; tabtab[n].start != NULL; n++)
7476 {
7477 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7478 while (col < len)
7479 TabPageIdxs[col++] = fillchar;
7480 p = tabtab[n].start;
7481 fillchar = tabtab[n].userhl;
7482 }
7483 while (col < Columns)
7484 TabPageIdxs[col++] = fillchar;
7485 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007486
7487theend:
7488 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
7491#endif /* FEAT_STL_OPT */
7492
7493/*
7494 * Output a single character directly to the screen and update ScreenLines.
7495 */
7496 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007497screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 char_u buf[MB_MAXBYTES + 1];
7500
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007501#ifdef FEAT_MBYTE
7502 if (has_mbyte)
7503 buf[(*mb_char2bytes)(c, buf)] = NUL;
7504 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007506 {
7507 buf[0] = c;
7508 buf[1] = NUL;
7509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 screen_puts(buf, row, col, attr);
7511}
7512
7513/*
7514 * Get a single character directly from ScreenLines into "bytes[]".
7515 * Also return its attribute in *attrp;
7516 */
7517 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007518screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519{
7520 unsigned off;
7521
7522 /* safety check */
7523 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7524 {
7525 off = LineOffset[row] + col;
7526 *attrp = ScreenAttrs[off];
7527 bytes[0] = ScreenLines[off];
7528 bytes[1] = NUL;
7529
7530#ifdef FEAT_MBYTE
7531 if (enc_utf8 && ScreenLinesUC[off] != 0)
7532 bytes[utfc_char2bytes(off, bytes)] = NUL;
7533 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7534 {
7535 bytes[0] = ScreenLines[off];
7536 bytes[1] = ScreenLines2[off];
7537 bytes[2] = NUL;
7538 }
7539 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7540 {
7541 bytes[1] = ScreenLines[off + 1];
7542 bytes[2] = NUL;
7543 }
7544#endif
7545 }
7546}
7547
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007548#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007549/*
7550 * Return TRUE if composing characters for screen posn "off" differs from
7551 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007552 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007553 */
7554 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007555screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007556{
7557 int i;
7558
7559 for (i = 0; i < Screen_mco; ++i)
7560 {
7561 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7562 return TRUE;
7563 if (u8cc[i] == 0)
7564 break;
7565 }
7566 return FALSE;
7567}
7568#endif
7569
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570/*
7571 * Put string '*text' on the screen at position 'row' and 'col', with
7572 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7573 * Note: only outputs within one row, message is truncated at screen boundary!
7574 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7575 */
7576 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007577screen_puts(
7578 char_u *text,
7579 int row,
7580 int col,
7581 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
7583 screen_puts_len(text, -1, row, col, attr);
7584}
7585
7586/*
7587 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7588 * a NUL.
7589 */
7590 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007591screen_puts_len(
7592 char_u *text,
7593 int textlen,
7594 int row,
7595 int col,
7596 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598 unsigned off;
7599 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007600 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 int c;
7602#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007603 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 int mbyte_blen = 1;
7605 int mbyte_cells = 1;
7606 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007607 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 int clear_next_cell = FALSE;
7609# ifdef FEAT_ARABIC
7610 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007611 int pc, nc, nc1;
7612 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613# endif
7614#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007615#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7616 int force_redraw_this;
7617 int force_redraw_next = FALSE;
7618#endif
7619 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
7621 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7622 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007623 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
Bram Moolenaarc236c162008-07-13 17:41:49 +00007625#ifdef FEAT_MBYTE
7626 /* When drawing over the right halve of a double-wide char clear out the
7627 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007628 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007629# ifdef FEAT_GUI
7630 && !gui.in_use
7631# endif
7632 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007633 {
7634 ScreenLines[off - 1] = ' ';
7635 ScreenAttrs[off - 1] = 0;
7636 if (enc_utf8)
7637 {
7638 ScreenLinesUC[off - 1] = 0;
7639 ScreenLinesC[0][off - 1] = 0;
7640 }
7641 /* redraw the previous cell, make it empty */
7642 screen_char(off - 1, row, col - 1);
7643 /* force the cell at "col" to be redrawn */
7644 force_redraw_next = TRUE;
7645 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007646#endif
7647
Bram Moolenaar367329b2007-08-30 11:53:22 +00007648#ifdef FEAT_MBYTE
7649 max_off = LineOffset[row] + screen_Columns;
7650#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007651 while (col < screen_Columns
7652 && (len < 0 || (int)(ptr - text) < len)
7653 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {
7655 c = *ptr;
7656#ifdef FEAT_MBYTE
7657 /* check if this is the first byte of a multibyte */
7658 if (has_mbyte)
7659 {
7660 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007661 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007663 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7665 mbyte_cells = 1;
7666 else if (enc_dbcs != 0)
7667 mbyte_cells = mbyte_blen;
7668 else /* enc_utf8 */
7669 {
7670 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007671 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 (int)((text + len) - ptr));
7673 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007674 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007676# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 /* Non-BMP character: display as ? or fullwidth ?. */
7678 if (u8c >= 0x10000)
7679 {
7680 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7681 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007682 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685# ifdef FEAT_ARABIC
7686 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7687 {
7688 /* Do Arabic shaping. */
7689 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7690 {
7691 /* Past end of string to be displayed. */
7692 nc = NUL;
7693 nc1 = NUL;
7694 }
7695 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007696 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007697 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7698 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007699 nc1 = pcc[0];
7700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 pc = prev_c;
7702 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007703 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 }
7705 else
7706 prev_c = u8c;
7707# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007708 if (col + mbyte_cells > screen_Columns)
7709 {
7710 /* Only 1 cell left, but character requires 2 cells:
7711 * display a '>' in the last column to avoid wrapping. */
7712 c = '>';
7713 mbyte_cells = 1;
7714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 }
7716 }
7717#endif
7718
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007719#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7720 force_redraw_this = force_redraw_next;
7721 force_redraw_next = FALSE;
7722#endif
7723
7724 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725#ifdef FEAT_MBYTE
7726 || (mbyte_cells == 2
7727 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7728 || (enc_dbcs == DBCS_JPNU
7729 && c == 0x8e
7730 && ScreenLines2[off] != ptr[1])
7731 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007732 && (ScreenLinesUC[off] !=
7733 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7734 || (ScreenLinesUC[off] != 0
7735 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736#endif
7737 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007738 || exmode_active;
7739
7740 if (need_redraw
7741#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7742 || force_redraw_this
7743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 )
7745 {
7746#if defined(FEAT_GUI) || defined(UNIX)
7747 /* The bold trick makes a single row of pixels appear in the next
7748 * character. When a bold character is removed, the next
7749 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007750 * and for some xterms. */
7751 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752# ifdef FEAT_GUI
7753 gui.in_use
7754# endif
7755# if defined(FEAT_GUI) && defined(UNIX)
7756 ||
7757# endif
7758# ifdef UNIX
7759 term_is_xterm
7760# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007761 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007763 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007765 if (n > HL_ALL)
7766 n = syn_attr2attr(n);
7767 if (n & HL_BOLD)
7768 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
7770#endif
7771#ifdef FEAT_MBYTE
7772 /* When at the end of the text and overwriting a two-cell
7773 * character with a one-cell character, need to clear the next
7774 * cell. Also when overwriting the left halve of a two-cell char
7775 * with the right halve of a two-cell char. Do this only once
7776 * (mb_off2cells() may return 2 on the right halve). */
7777 if (clear_next_cell)
7778 clear_next_cell = FALSE;
7779 else if (has_mbyte
7780 && (len < 0 ? ptr[mbyte_blen] == NUL
7781 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007782 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007784 && (*mb_off2cells)(off, max_off) == 1
7785 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 clear_next_cell = TRUE;
7787
7788 /* Make sure we never leave a second byte of a double-byte behind,
7789 * it confuses mb_off2cells(). */
7790 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007791 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007793 && (*mb_off2cells)(off, max_off) == 1
7794 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 ScreenLines[off + mbyte_blen] = 0;
7796#endif
7797 ScreenLines[off] = c;
7798 ScreenAttrs[off] = attr;
7799#ifdef FEAT_MBYTE
7800 if (enc_utf8)
7801 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007802 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 ScreenLinesUC[off] = 0;
7804 else
7805 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007806 int i;
7807
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007809 for (i = 0; i < Screen_mco; ++i)
7810 {
7811 ScreenLinesC[i][off] = u8cc[i];
7812 if (u8cc[i] == 0)
7813 break;
7814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 }
7816 if (mbyte_cells == 2)
7817 {
7818 ScreenLines[off + 1] = 0;
7819 ScreenAttrs[off + 1] = attr;
7820 }
7821 screen_char(off, row, col);
7822 }
7823 else if (mbyte_cells == 2)
7824 {
7825 ScreenLines[off + 1] = ptr[1];
7826 ScreenAttrs[off + 1] = attr;
7827 screen_char_2(off, row, col);
7828 }
7829 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7830 {
7831 ScreenLines2[off] = ptr[1];
7832 screen_char(off, row, col);
7833 }
7834 else
7835#endif
7836 screen_char(off, row, col);
7837 }
7838#ifdef FEAT_MBYTE
7839 if (has_mbyte)
7840 {
7841 off += mbyte_cells;
7842 col += mbyte_cells;
7843 ptr += mbyte_blen;
7844 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007845 {
7846 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007848 len = -1;
7849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 }
7851 else
7852#endif
7853 {
7854 ++off;
7855 ++col;
7856 ++ptr;
7857 }
7858 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007859
7860#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7861 /* If we detected the next character needs to be redrawn, but the text
7862 * doesn't extend up to there, update the character here. */
7863 if (force_redraw_next && col < screen_Columns)
7864 {
7865# ifdef FEAT_MBYTE
7866 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7867 screen_char_2(off, row, col);
7868 else
7869# endif
7870 screen_char(off, row, col);
7871 }
7872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873}
7874
7875#ifdef FEAT_SEARCH_EXTRA
7876/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007877 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 */
7879 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007880start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 if (p_hls && !no_hlsearch)
7883 {
7884 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007885 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007886# ifdef FEAT_RELTIME
7887 /* Set the time limit to 'redrawtime'. */
7888 profile_setlimit(p_rdt, &search_hl.tm);
7889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 }
7891}
7892
7893/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007894 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 */
7896 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007897end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 if (search_hl.rm.regprog != NULL)
7900 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007901 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 search_hl.rm.regprog = NULL;
7903 }
7904}
7905
7906/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007907 * Init for calling prepare_search_hl().
7908 */
7909 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007910init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007911{
7912 matchitem_T *cur;
7913
7914 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7915 * match */
7916 cur = wp->w_match_head;
7917 while (cur != NULL)
7918 {
7919 cur->hl.rm = cur->match;
7920 if (cur->hlg_id == 0)
7921 cur->hl.attr = 0;
7922 else
7923 cur->hl.attr = syn_id2attr(cur->hlg_id);
7924 cur->hl.buf = wp->w_buffer;
7925 cur->hl.lnum = 0;
7926 cur->hl.first_lnum = 0;
7927# ifdef FEAT_RELTIME
7928 /* Set the time limit to 'redrawtime'. */
7929 profile_setlimit(p_rdt, &(cur->hl.tm));
7930# endif
7931 cur = cur->next;
7932 }
7933 search_hl.buf = wp->w_buffer;
7934 search_hl.lnum = 0;
7935 search_hl.first_lnum = 0;
7936 /* time limit is set at the toplevel, for all windows */
7937}
7938
7939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 * Advance to the match in window "wp" line "lnum" or past it.
7941 */
7942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007943prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007945 matchitem_T *cur; /* points to the match list */
7946 match_T *shl; /* points to search_hl or a match */
7947 int shl_flag; /* flag to indicate whether search_hl
7948 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007949 int pos_inprogress; /* marks that position match search is
7950 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 int n;
7952
7953 /*
7954 * When using a multi-line pattern, start searching at the top
7955 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007956 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007958 cur = wp->w_match_head;
7959 shl_flag = FALSE;
7960 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007962 if (shl_flag == FALSE)
7963 {
7964 shl = &search_hl;
7965 shl_flag = TRUE;
7966 }
7967 else
7968 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (shl->rm.regprog != NULL
7970 && shl->lnum == 0
7971 && re_multiline(shl->rm.regprog))
7972 {
7973 if (shl->first_lnum == 0)
7974 {
7975# ifdef FEAT_FOLDING
7976 for (shl->first_lnum = lnum;
7977 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7978 if (hasFoldingWin(wp, shl->first_lnum - 1,
7979 NULL, NULL, TRUE, NULL))
7980 break;
7981# else
7982 shl->first_lnum = wp->w_topline;
7983# endif
7984 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985 if (cur != NULL)
7986 cur->pos.cur = 0;
7987 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007989 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7990 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007992 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7993 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007994 pos_inprogress = cur == NULL || cur->pos.cur == 0
7995 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 if (shl->lnum != 0)
7997 {
7998 shl->first_lnum = shl->lnum
7999 + shl->rm.endpos[0].lnum
8000 - shl->rm.startpos[0].lnum;
8001 n = shl->rm.endpos[0].col;
8002 }
8003 else
8004 {
8005 ++shl->first_lnum;
8006 n = 0;
8007 }
8008 }
8009 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008010 if (shl != &search_hl && cur != NULL)
8011 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 }
8013}
8014
8015/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008016 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 * Uses shl->buf.
8018 * Sets shl->lnum and shl->rm contents.
8019 * Note: Assumes a previous match is always before "lnum", unless
8020 * shl->lnum is zero.
8021 * Careful: Any pointers for buffer lines will become invalid.
8022 */
8023 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008024next_search_hl(
8025 win_T *win,
8026 match_T *shl, /* points to search_hl or a match */
8027 linenr_T lnum,
8028 colnr_T mincol, /* minimal column for a match */
8029 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
8031 linenr_T l;
8032 colnr_T matchcol;
8033 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008034 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008036 // for :{range}s/pat only highlight inside the range
8037 if (lnum < search_first_line || lnum > search_last_line)
8038 {
8039 shl->lnum = 0;
8040 return;
8041 }
8042
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (shl->lnum != 0)
8044 {
8045 /* Check for three situations:
8046 * 1. If the "lnum" is below a previous match, start a new search.
8047 * 2. If the previous match includes "mincol", use it.
8048 * 3. Continue after the previous match.
8049 */
8050 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8051 if (lnum > l)
8052 shl->lnum = 0;
8053 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8054 return;
8055 }
8056
8057 /*
8058 * Repeat searching for a match until one is found that includes "mincol"
8059 * or none is found in this line.
8060 */
8061 called_emsg = FALSE;
8062 for (;;)
8063 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008064#ifdef FEAT_RELTIME
8065 /* Stop searching after passing the time limit. */
8066 if (profile_passed_limit(&(shl->tm)))
8067 {
8068 shl->lnum = 0; /* no match found in time */
8069 break;
8070 }
8071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 /* Three situations:
8073 * 1. No useful previous match: search from start of line.
8074 * 2. Not Vi compatible or empty match: continue at next character.
8075 * Break the loop if this is beyond the end of the line.
8076 * 3. Vi compatible searching: continue at end of previous match.
8077 */
8078 if (shl->lnum == 0)
8079 matchcol = 0;
8080 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8081 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008082 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008084 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008085
8086 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008087 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008088 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008090 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 shl->lnum = 0;
8092 break;
8093 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008094#ifdef FEAT_MBYTE
8095 if (has_mbyte)
8096 matchcol += mb_ptr2len(ml);
8097 else
8098#endif
8099 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 }
8101 else
8102 matchcol = shl->rm.endpos[0].col;
8103
8104 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008105 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008107 /* Remember whether shl->rm is using a copy of the regprog in
8108 * cur->match. */
8109 int regprog_is_copy = (shl != &search_hl && cur != NULL
8110 && shl == &cur->hl
8111 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008112 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008113
Bram Moolenaarb3414592014-06-17 17:48:32 +02008114 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8115 matchcol,
8116#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008117 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008118#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008119 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008120#endif
8121 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008122 /* Copy the regprog, in case it got freed and recompiled. */
8123 if (regprog_is_copy)
8124 cur->match.regprog = cur->hl.rm.regprog;
8125
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008126 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008127 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008128 /* Error while handling regexp: stop using this regexp. */
8129 if (shl == &search_hl)
8130 {
8131 /* don't free regprog in the match list, it's a copy */
8132 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008133 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008134 }
8135 shl->rm.regprog = NULL;
8136 shl->lnum = 0;
8137 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8138 message */
8139 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008140 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008141 }
8142 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008143 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008144 else
8145 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 if (nmatched == 0)
8147 {
8148 shl->lnum = 0; /* no match found */
8149 break;
8150 }
8151 if (shl->rm.startpos[0].lnum > 0
8152 || shl->rm.startpos[0].col >= mincol
8153 || nmatched > 1
8154 || shl->rm.endpos[0].col > mincol)
8155 {
8156 shl->lnum += shl->rm.startpos[0].lnum;
8157 break; /* useful match found */
8158 }
8159 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008160
8161 // Restore called_emsg for assert_fails().
8162 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164
Bram Moolenaar85077472016-10-16 14:35:48 +02008165/*
8166 * If there is a match fill "shl" and return one.
8167 * Return zero otherwise.
8168 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008169 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008170next_search_hl_pos(
8171 match_T *shl, /* points to a match */
8172 linenr_T lnum,
8173 posmatch_T *posmatch, /* match positions */
8174 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008175{
8176 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008177 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008178
Bram Moolenaarb3414592014-06-17 17:48:32 +02008179 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8180 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008181 llpos_T *pos = &posmatch->pos[i];
8182
8183 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008184 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008185 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008186 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008187 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008188 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008189 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008190 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008191 /* if this match comes before the one at "found" then swap
8192 * them */
8193 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008194 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008195 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008196
Bram Moolenaar85077472016-10-16 14:35:48 +02008197 *pos = posmatch->pos[found];
8198 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008199 }
8200 }
8201 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008202 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008203 }
8204 }
8205 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008206 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008207 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008208 colnr_T start = posmatch->pos[found].col == 0
8209 ? 0 : posmatch->pos[found].col - 1;
8210 colnr_T end = posmatch->pos[found].col == 0
8211 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008212
Bram Moolenaar85077472016-10-16 14:35:48 +02008213 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008214 shl->rm.startpos[0].lnum = 0;
8215 shl->rm.startpos[0].col = start;
8216 shl->rm.endpos[0].lnum = 0;
8217 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008218 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008219 posmatch->cur = found + 1;
8220 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008221 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008222 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008223}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008224#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008225
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008227screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 attrentry_T *aep = NULL;
8230
8231 screen_attr = attr;
8232 if (full_screen
8233#ifdef WIN3264
8234 && termcap_active
8235#endif
8236 )
8237 {
8238#ifdef FEAT_GUI
8239 if (gui.in_use)
8240 {
8241 char buf[20];
8242
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008243 /* The GUI handles this internally. */
8244 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 OUT_STR(buf);
8246 }
8247 else
8248#endif
8249 {
8250 if (attr > HL_ALL) /* special HL attr. */
8251 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008252 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 aep = syn_cterm_attr2entry(attr);
8254 else
8255 aep = syn_term_attr2entry(attr);
8256 if (aep == NULL) /* did ":syntax clear" */
8257 attr = 0;
8258 else
8259 attr = aep->ae_attr;
8260 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008261 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008263 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008264#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008265 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8266 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8267 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008268#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008269 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008270 /* If the Normal FG color has BOLD attribute and the new HL
8271 * has a FG color defined, clear BOLD. */
8272 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008273 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008275 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008276 out_str(T_UCS);
8277 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008278 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8279 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008281 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008283 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008285 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008286 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287
8288 /*
8289 * Output the color or start string after bold etc., in case the
8290 * bold etc. override the color setting.
8291 */
8292 if (aep != NULL)
8293 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008294#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008295 /* When 'termguicolors' is set but fg or bg is unset,
8296 * fall back to the cterm colors. This helps for SpellBad,
8297 * where the GUI uses a red undercurl. */
8298 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008300 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008301 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008302 }
8303 else
8304#endif
8305 if (t_colors > 1)
8306 {
8307 if (aep->ae_u.cterm.fg_color)
8308 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8309 }
8310#ifdef FEAT_TERMGUICOLORS
8311 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8312 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008313 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008314 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 }
8316 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008317#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008318 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008320 if (aep->ae_u.cterm.bg_color)
8321 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8322 }
8323
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008324 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008325 {
8326 if (aep->ae_u.term.start != NULL)
8327 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 }
8329 }
8330 }
8331 }
8332}
8333
8334 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008335screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
8337 int do_ME = FALSE; /* output T_ME code */
8338
8339 if (screen_attr != 0
8340#ifdef WIN3264
8341 && termcap_active
8342#endif
8343 )
8344 {
8345#ifdef FEAT_GUI
8346 if (gui.in_use)
8347 {
8348 char buf[20];
8349
8350 /* use internal GUI code */
8351 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8352 OUT_STR(buf);
8353 }
8354 else
8355#endif
8356 {
8357 if (screen_attr > HL_ALL) /* special HL attr. */
8358 {
8359 attrentry_T *aep;
8360
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008361 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {
8363 /*
8364 * Assume that t_me restores the original colors!
8365 */
8366 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008367 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008368#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008369 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8370 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8371 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008372#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008373 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008374#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008375 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8376 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8377 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008378#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008379 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 do_ME = TRUE;
8381 }
8382 else
8383 {
8384 aep = syn_term_attr2entry(screen_attr);
8385 if (aep != NULL && aep->ae_u.term.stop != NULL)
8386 {
8387 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8388 do_ME = TRUE;
8389 else
8390 out_str(aep->ae_u.term.stop);
8391 }
8392 }
8393 if (aep == NULL) /* did ":syntax clear" */
8394 screen_attr = 0;
8395 else
8396 screen_attr = aep->ae_attr;
8397 }
8398
8399 /*
8400 * Often all ending-codes are equal to T_ME. Avoid outputting the
8401 * same sequence several times.
8402 */
8403 if (screen_attr & HL_STANDOUT)
8404 {
8405 if (STRCMP(T_SE, T_ME) == 0)
8406 do_ME = TRUE;
8407 else
8408 out_str(T_SE);
8409 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008410 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008411 {
8412 if (STRCMP(T_UCE, T_ME) == 0)
8413 do_ME = TRUE;
8414 else
8415 out_str(T_UCE);
8416 }
8417 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008418 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {
8420 if (STRCMP(T_UE, T_ME) == 0)
8421 do_ME = TRUE;
8422 else
8423 out_str(T_UE);
8424 }
8425 if (screen_attr & HL_ITALIC)
8426 {
8427 if (STRCMP(T_CZR, T_ME) == 0)
8428 do_ME = TRUE;
8429 else
8430 out_str(T_CZR);
8431 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008432 if (screen_attr & HL_STRIKETHROUGH)
8433 {
8434 if (STRCMP(T_STE, T_ME) == 0)
8435 do_ME = TRUE;
8436 else
8437 out_str(T_STE);
8438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8440 out_str(T_ME);
8441
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008442#ifdef FEAT_TERMGUICOLORS
8443 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008445 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008446 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008447 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008448 term_bg_rgb_color(cterm_normal_bg_gui_color);
8449 }
8450 else
8451#endif
8452 {
8453 if (t_colors > 1)
8454 {
8455 /* set Normal cterm colors */
8456 if (cterm_normal_fg_color != 0)
8457 term_fg_color(cterm_normal_fg_color - 1);
8458 if (cterm_normal_bg_color != 0)
8459 term_bg_color(cterm_normal_bg_color - 1);
8460 if (cterm_normal_fg_bold)
8461 out_str(T_MD);
8462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 }
8464 }
8465 }
8466 screen_attr = 0;
8467}
8468
8469/*
8470 * Reset the colors for a cterm. Used when leaving Vim.
8471 * The machine specific code may override this again.
8472 */
8473 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008474reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008476 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
8478 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008479#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008480 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8481 || cterm_normal_bg_gui_color != INVALCOLOR)
8482 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008483#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {
8487 out_str(T_OP);
8488 screen_attr = -1;
8489 }
8490 if (cterm_normal_fg_bold)
8491 {
8492 out_str(T_ME);
8493 screen_attr = -1;
8494 }
8495 }
8496}
8497
8498/*
8499 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8500 * using the attributes from ScreenAttrs["off"].
8501 */
8502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008503screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
8505 int attr;
8506
8507 /* Check for illegal values, just in case (could happen just after
8508 * resizing). */
8509 if (row >= screen_Rows || col >= screen_Columns)
8510 return;
8511
Bram Moolenaar494838a2015-02-10 19:20:37 +01008512 /* Outputting a character in the last cell on the screen may scroll the
8513 * screen up. Only do it when the "xn" termcap property is set, otherwise
8514 * mark the character invalid (update it when scrolled up). */
8515 if (*T_XN == NUL
8516 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517#ifdef FEAT_RIGHTLEFT
8518 /* account for first command-line character in rightleft mode */
8519 && !cmdmsg_rl
8520#endif
8521 )
8522 {
8523 ScreenAttrs[off] = (sattr_T)-1;
8524 return;
8525 }
8526
8527 /*
8528 * Stop highlighting first, so it's easier to move the cursor.
8529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 if (screen_char_attr != 0)
8531 attr = screen_char_attr;
8532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 attr = ScreenAttrs[off];
8534 if (screen_attr != attr)
8535 screen_stop_highlight();
8536
8537 windgoto(row, col);
8538
8539 if (screen_attr != attr)
8540 screen_start_highlight(attr);
8541
8542#ifdef FEAT_MBYTE
8543 if (enc_utf8 && ScreenLinesUC[off] != 0)
8544 {
8545 char_u buf[MB_MAXBYTES + 1];
8546
Bram Moolenaarcb070082016-04-02 22:14:51 +02008547 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008548 {
8549 if (*p_ambw == 'd'
8550# ifdef FEAT_GUI
8551 && !gui.in_use
8552# endif
8553 )
8554 {
8555 /* Clear the two screen cells. If the character is actually
8556 * single width it won't change the second cell. */
8557 out_str((char_u *)" ");
8558 term_windgoto(row, col);
8559 }
8560 /* not sure where the cursor is after drawing the ambiguous width
8561 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008562 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008563 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008564 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008566
8567 /* Convert the UTF-8 character to bytes and write it. */
8568 buf[utfc_char2bytes(off, buf)] = NUL;
8569 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 }
8571 else
8572#endif
8573 {
8574#ifdef FEAT_MBYTE
8575 out_flush_check();
8576#endif
8577 out_char(ScreenLines[off]);
8578#ifdef FEAT_MBYTE
8579 /* double-byte character in single-width cell */
8580 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8581 out_char(ScreenLines2[off]);
8582#endif
8583 }
8584
8585 screen_cur_col++;
8586}
8587
8588#ifdef FEAT_MBYTE
8589
8590/*
8591 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8592 * on the screen at position 'row' and 'col'.
8593 * The attributes of the first byte is used for all. This is required to
8594 * output the two bytes of a double-byte character with nothing in between.
8595 */
8596 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008597screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599 /* Check for illegal values (could be wrong when screen was resized). */
8600 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8601 return;
8602
8603 /* Outputting the last character on the screen may scrollup the screen.
8604 * Don't to it! Mark the character invalid (update it when scrolled up) */
8605 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8606 {
8607 ScreenAttrs[off] = (sattr_T)-1;
8608 return;
8609 }
8610
8611 /* Output the first byte normally (positions the cursor), then write the
8612 * second byte directly. */
8613 screen_char(off, row, col);
8614 out_char(ScreenLines[off + 1]);
8615 ++screen_cur_col;
8616}
8617#endif
8618
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619/*
8620 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8621 * This uses the contents of ScreenLines[] and doesn't change it.
8622 */
8623 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008624screen_draw_rectangle(
8625 int row,
8626 int col,
8627 int height,
8628 int width,
8629 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 int r, c;
8632 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008633#ifdef FEAT_MBYTE
8634 int max_off;
8635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008637 /* Can't use ScreenLines unless initialized */
8638 if (ScreenLines == NULL)
8639 return;
8640
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 if (invert)
8642 screen_char_attr = HL_INVERSE;
8643 for (r = row; r < row + height; ++r)
8644 {
8645 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008646#ifdef FEAT_MBYTE
8647 max_off = off + screen_Columns;
8648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 for (c = col; c < col + width; ++c)
8650 {
8651#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008652 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 {
8654 screen_char_2(off + c, r, c);
8655 ++c;
8656 }
8657 else
8658#endif
8659 {
8660 screen_char(off + c, r, c);
8661#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008662 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 ++c;
8664#endif
8665 }
8666 }
8667 }
8668 screen_char_attr = 0;
8669}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671/*
8672 * Redraw the characters for a vertically split window.
8673 */
8674 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008675redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 int col;
8678 int width;
8679
8680# ifdef FEAT_CLIPBOARD
8681 clip_may_clear_selection(row, end - 1);
8682# endif
8683
8684 if (wp == NULL)
8685 {
8686 col = 0;
8687 width = Columns;
8688 }
8689 else
8690 {
8691 col = wp->w_wincol;
8692 width = wp->w_width;
8693 }
8694 screen_draw_rectangle(row, col, end - row, width, FALSE);
8695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008697 static void
8698space_to_screenline(int off, int attr)
8699{
8700 ScreenLines[off] = ' ';
8701 ScreenAttrs[off] = attr;
8702# ifdef FEAT_MBYTE
8703 if (enc_utf8)
8704 ScreenLinesUC[off] = 0;
8705# endif
8706}
8707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708/*
8709 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8710 * with character 'c1' in first column followed by 'c2' in the other columns.
8711 * Use attributes 'attr'.
8712 */
8713 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008714screen_fill(
8715 int start_row,
8716 int end_row,
8717 int start_col,
8718 int end_col,
8719 int c1,
8720 int c2,
8721 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722{
8723 int row;
8724 int col;
8725 int off;
8726 int end_off;
8727 int did_delete;
8728 int c;
8729 int norm_term;
8730#if defined(FEAT_GUI) || defined(UNIX)
8731 int force_next = FALSE;
8732#endif
8733
8734 if (end_row > screen_Rows) /* safety check */
8735 end_row = screen_Rows;
8736 if (end_col > screen_Columns) /* safety check */
8737 end_col = screen_Columns;
8738 if (ScreenLines == NULL
8739 || start_row >= end_row
8740 || start_col >= end_col) /* nothing to do */
8741 return;
8742
8743 /* it's a "normal" terminal when not in a GUI or cterm */
8744 norm_term = (
8745#ifdef FEAT_GUI
8746 !gui.in_use &&
8747#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008748 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 for (row = start_row; row < end_row; ++row)
8750 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008751#ifdef FEAT_MBYTE
8752 if (has_mbyte
8753# ifdef FEAT_GUI
8754 && !gui.in_use
8755# endif
8756 )
8757 {
8758 /* When drawing over the right halve of a double-wide char clear
8759 * out the left halve. When drawing over the left halve of a
8760 * double wide-char clear out the right halve. Only needed in a
8761 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008762 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008763 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008764 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008765 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008766 }
8767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 /*
8769 * Try to use delete-line termcap code, when no attributes or in a
8770 * "normal" terminal, where a bold/italic space is just a
8771 * space.
8772 */
8773 did_delete = FALSE;
8774 if (c2 == ' '
8775 && end_col == Columns
8776 && can_clear(T_CE)
8777 && (attr == 0
8778 || (norm_term
8779 && attr <= HL_ALL
8780 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8781 {
8782 /*
8783 * check if we really need to clear something
8784 */
8785 col = start_col;
8786 if (c1 != ' ') /* don't clear first char */
8787 ++col;
8788
8789 off = LineOffset[row] + col;
8790 end_off = LineOffset[row] + end_col;
8791
8792 /* skip blanks (used often, keep it fast!) */
8793#ifdef FEAT_MBYTE
8794 if (enc_utf8)
8795 while (off < end_off && ScreenLines[off] == ' '
8796 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8797 ++off;
8798 else
8799#endif
8800 while (off < end_off && ScreenLines[off] == ' '
8801 && ScreenAttrs[off] == 0)
8802 ++off;
8803 if (off < end_off) /* something to be cleared */
8804 {
8805 col = off - LineOffset[row];
8806 screen_stop_highlight();
8807 term_windgoto(row, col);/* clear rest of this screen line */
8808 out_str(T_CE);
8809 screen_start(); /* don't know where cursor is now */
8810 col = end_col - col;
8811 while (col--) /* clear chars in ScreenLines */
8812 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008813 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 ++off;
8815 }
8816 }
8817 did_delete = TRUE; /* the chars are cleared now */
8818 }
8819
8820 off = LineOffset[row] + start_col;
8821 c = c1;
8822 for (col = start_col; col < end_col; ++col)
8823 {
8824 if (ScreenLines[off] != c
8825#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008826 || (enc_utf8 && (int)ScreenLinesUC[off]
8827 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828#endif
8829 || ScreenAttrs[off] != attr
8830#if defined(FEAT_GUI) || defined(UNIX)
8831 || force_next
8832#endif
8833 )
8834 {
8835#if defined(FEAT_GUI) || defined(UNIX)
8836 /* The bold trick may make a single row of pixels appear in
8837 * the next character. When a bold character is removed, the
8838 * next character should be redrawn too. This happens for our
8839 * own GUI and for some xterms. */
8840 if (
8841# ifdef FEAT_GUI
8842 gui.in_use
8843# endif
8844# if defined(FEAT_GUI) && defined(UNIX)
8845 ||
8846# endif
8847# ifdef UNIX
8848 term_is_xterm
8849# endif
8850 )
8851 {
8852 if (ScreenLines[off] != ' '
8853 && (ScreenAttrs[off] > HL_ALL
8854 || ScreenAttrs[off] & HL_BOLD))
8855 force_next = TRUE;
8856 else
8857 force_next = FALSE;
8858 }
8859#endif
8860 ScreenLines[off] = c;
8861#ifdef FEAT_MBYTE
8862 if (enc_utf8)
8863 {
8864 if (c >= 0x80)
8865 {
8866 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008867 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 }
8869 else
8870 ScreenLinesUC[off] = 0;
8871 }
8872#endif
8873 ScreenAttrs[off] = attr;
8874 if (!did_delete || c != ' ')
8875 screen_char(off, row, col);
8876 }
8877 ++off;
8878 if (col == start_col)
8879 {
8880 if (did_delete)
8881 break;
8882 c = c2;
8883 }
8884 }
8885 if (end_col == Columns)
8886 LineWraps[row] = FALSE;
8887 if (row == Rows - 1) /* overwritten the command line */
8888 {
8889 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008890 if (start_col == 0 && end_col == Columns
8891 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008893 if (start_col == 0)
8894 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
8896 }
8897}
8898
8899/*
8900 * Check if there should be a delay. Used before clearing or redrawing the
8901 * screen or the command line.
8902 */
8903 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008904check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8907 && !did_wait_return
8908 && emsg_silent == 0)
8909 {
8910 out_flush();
8911 ui_delay(1000L, TRUE);
8912 emsg_on_display = FALSE;
8913 if (check_msg_scroll)
8914 msg_scroll = FALSE;
8915 }
8916}
8917
8918/*
8919 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008920 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 * Returns TRUE if there is a valid screen to write to.
8922 * Returns FALSE when starting up and screen not initialized yet.
8923 */
8924 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008925screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008927 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 return (ScreenLines != NULL);
8929}
8930
8931/*
8932 * Resize the shell to Rows and Columns.
8933 * Allocate ScreenLines[] and associated items.
8934 *
8935 * There may be some time between setting Rows and Columns and (re)allocating
8936 * ScreenLines[]. This happens when starting up and when (manually) changing
8937 * the shell size. Always use screen_Rows and screen_Columns to access items
8938 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8939 * final size of the shell is needed.
8940 */
8941 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008942screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
8944 int new_row, old_row;
8945#ifdef FEAT_GUI
8946 int old_Rows;
8947#endif
8948 win_T *wp;
8949 int outofmem = FALSE;
8950 int len;
8951 schar_T *new_ScreenLines;
8952#ifdef FEAT_MBYTE
8953 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008954 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008956 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957#endif
8958 sattr_T *new_ScreenAttrs;
8959 unsigned *new_LineOffset;
8960 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008961 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008962 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008964 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008965 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008967retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 /*
8969 * Allocation of the screen buffers is done only when the size changes and
8970 * when Rows and Columns have been set and we have started doing full
8971 * screen stuff.
8972 */
8973 if ((ScreenLines != NULL
8974 && Rows == screen_Rows
8975 && Columns == screen_Columns
8976#ifdef FEAT_MBYTE
8977 && enc_utf8 == (ScreenLinesUC != NULL)
8978 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008979 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980#endif
8981 )
8982 || Rows == 0
8983 || Columns == 0
8984 || (!full_screen && ScreenLines == NULL))
8985 return;
8986
8987 /*
8988 * It's possible that we produce an out-of-memory message below, which
8989 * will cause this function to be called again. To break the loop, just
8990 * return here.
8991 */
8992 if (entered)
8993 return;
8994 entered = TRUE;
8995
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008996 /*
8997 * Note that the window sizes are updated before reallocating the arrays,
8998 * thus we must not redraw here!
8999 */
9000 ++RedrawingDisabled;
9001
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 win_new_shellsize(); /* fit the windows in the new sized shell */
9003
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 comp_col(); /* recompute columns for shown command and ruler */
9005
9006 /*
9007 * We're changing the size of the screen.
9008 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9009 * - Move lines from the old arrays into the new arrays, clear extra
9010 * lines (unless the screen is going to be cleared).
9011 * - Free the old arrays.
9012 *
9013 * If anything fails, make ScreenLines NULL, so we don't do anything!
9014 * Continuing with the old ScreenLines may result in a crash, because the
9015 * size is wrong.
9016 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009017 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009019 if (aucmd_win != NULL)
9020 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021
9022 new_ScreenLines = (schar_T *)lalloc((long_u)(
9023 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9024#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01009025 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 if (enc_utf8)
9027 {
9028 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
9029 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009030 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01009031 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
9033 }
9034 if (enc_dbcs == DBCS_JPNU)
9035 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9036 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9037#endif
9038 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9039 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9040 new_LineOffset = (unsigned *)lalloc((long_u)(
9041 Rows * sizeof(unsigned)), FALSE);
9042 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009043 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009045 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 {
9047 if (win_alloc_lines(wp) == FAIL)
9048 {
9049 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009050 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 }
9052 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009053 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9054 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009055 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009056give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009058#ifdef FEAT_MBYTE
9059 for (i = 0; i < p_mco; ++i)
9060 if (new_ScreenLinesC[i] == NULL)
9061 break;
9062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 if (new_ScreenLines == NULL
9064#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009065 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9067#endif
9068 || new_ScreenAttrs == NULL
9069 || new_LineOffset == NULL
9070 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009071 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 || outofmem)
9073 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009074 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009075 {
9076 /* guess the size */
9077 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9078
9079 /* Remember we did this to avoid getting outofmem messages over
9080 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009081 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009082 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009083 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009085 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009086 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009087 VIM_CLEAR(new_ScreenLinesC[i]);
9088 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009090 VIM_CLEAR(new_ScreenAttrs);
9091 VIM_CLEAR(new_LineOffset);
9092 VIM_CLEAR(new_LineWraps);
9093 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 }
9095 else
9096 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009097 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009098
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 for (new_row = 0; new_row < Rows; ++new_row)
9100 {
9101 new_LineOffset[new_row] = new_row * Columns;
9102 new_LineWraps[new_row] = FALSE;
9103
9104 /*
9105 * If the screen is not going to be cleared, copy as much as
9106 * possible from the old screen to the new one and clear the rest
9107 * (used when resizing the window at the "--more--" prompt or when
9108 * executing an external command, for the GUI).
9109 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009110 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 {
9112 (void)vim_memset(new_ScreenLines + new_row * Columns,
9113 ' ', (size_t)Columns * sizeof(schar_T));
9114#ifdef FEAT_MBYTE
9115 if (enc_utf8)
9116 {
9117 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9118 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009119 for (i = 0; i < p_mco; ++i)
9120 (void)vim_memset(new_ScreenLinesC[i]
9121 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 0, (size_t)Columns * sizeof(u8char_T));
9123 }
9124 if (enc_dbcs == DBCS_JPNU)
9125 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9126 0, (size_t)Columns * sizeof(schar_T));
9127#endif
9128 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9129 0, (size_t)Columns * sizeof(sattr_T));
9130 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009131 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 {
9133 if (screen_Columns < Columns)
9134 len = screen_Columns;
9135 else
9136 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009137#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009138 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009139 * may be invalid now. Also when p_mco changes. */
9140 if (!(enc_utf8 && ScreenLinesUC == NULL)
9141 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009142#endif
9143 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9144 ScreenLines + LineOffset[old_row],
9145 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009147 if (enc_utf8 && ScreenLinesUC != NULL
9148 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 {
9150 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9151 ScreenLinesUC + LineOffset[old_row],
9152 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009153 for (i = 0; i < p_mco; ++i)
9154 mch_memmove(new_ScreenLinesC[i]
9155 + new_LineOffset[new_row],
9156 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 (size_t)len * sizeof(u8char_T));
9158 }
9159 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9160 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9161 ScreenLines2 + LineOffset[old_row],
9162 (size_t)len * sizeof(schar_T));
9163#endif
9164 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9165 ScreenAttrs + LineOffset[old_row],
9166 (size_t)len * sizeof(sattr_T));
9167 }
9168 }
9169 }
9170 /* Use the last line of the screen for the current line. */
9171 current_ScreenLine = new_ScreenLines + Rows * Columns;
9172 }
9173
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009174 free_screenlines();
9175
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 ScreenLines = new_ScreenLines;
9177#ifdef FEAT_MBYTE
9178 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009179 for (i = 0; i < p_mco; ++i)
9180 ScreenLinesC[i] = new_ScreenLinesC[i];
9181 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 ScreenLines2 = new_ScreenLines2;
9183#endif
9184 ScreenAttrs = new_ScreenAttrs;
9185 LineOffset = new_LineOffset;
9186 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009187 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188
9189 /* It's important that screen_Rows and screen_Columns reflect the actual
9190 * size of ScreenLines[]. Set them before calling anything. */
9191#ifdef FEAT_GUI
9192 old_Rows = screen_Rows;
9193#endif
9194 screen_Rows = Rows;
9195 screen_Columns = Columns;
9196
9197 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009198 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 screenclear2();
9200
9201#ifdef FEAT_GUI
9202 else if (gui.in_use
9203 && !gui.starting
9204 && ScreenLines != NULL
9205 && old_Rows != Rows)
9206 {
9207 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9208 /*
9209 * Adjust the position of the cursor, for when executing an external
9210 * command.
9211 */
9212 if (msg_row >= Rows) /* Rows got smaller */
9213 msg_row = Rows - 1; /* put cursor at last row */
9214 else if (Rows > old_Rows) /* Rows got bigger */
9215 msg_row += Rows - old_Rows; /* put cursor in same place */
9216 if (msg_col >= Columns) /* Columns got smaller */
9217 msg_col = Columns - 1; /* put cursor at last column */
9218 }
9219#endif
9220
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009222 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009223
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009224 /*
9225 * Do not apply autocommands more than 3 times to avoid an endless loop
9226 * in case applying autocommands always changes Rows or Columns.
9227 */
9228 if (starting == 0 && ++retry_count <= 3)
9229 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009230 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009231 /* In rare cases, autocommands may have altered Rows or Columns,
9232 * jump back to check if we need to allocate the screen again. */
9233 goto retry;
9234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235}
9236
9237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009238free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009239{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009240#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009241 int i;
9242
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009243 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009244 for (i = 0; i < Screen_mco; ++i)
9245 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009246 vim_free(ScreenLines2);
9247#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009248 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009249 vim_free(ScreenAttrs);
9250 vim_free(LineOffset);
9251 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009252 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009253}
9254
9255 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009256screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
9258 check_for_delay(FALSE);
9259 screenalloc(FALSE); /* allocate screen buffers if size changed */
9260 screenclear2(); /* clear the screen */
9261}
9262
9263 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009264screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265{
9266 int i;
9267
9268 if (starting == NO_SCREEN || ScreenLines == NULL
9269#ifdef FEAT_GUI
9270 || (gui.in_use && gui.starting)
9271#endif
9272 )
9273 return;
9274
9275#ifdef FEAT_GUI
9276 if (!gui.in_use)
9277#endif
9278 screen_attr = -1; /* force setting the Normal colors */
9279 screen_stop_highlight(); /* don't want highlighting here */
9280
9281#ifdef FEAT_CLIPBOARD
9282 /* disable selection without redrawing it */
9283 clip_scroll_selection(9999);
9284#endif
9285
9286 /* blank out ScreenLines */
9287 for (i = 0; i < Rows; ++i)
9288 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009289 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 LineWraps[i] = FALSE;
9291 }
9292
9293 if (can_clear(T_CL))
9294 {
9295 out_str(T_CL); /* clear the display */
9296 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009297 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 }
9299 else
9300 {
9301 /* can't clear the screen, mark all chars with invalid attributes */
9302 for (i = 0; i < Rows; ++i)
9303 lineinvalid(LineOffset[i], (int)Columns);
9304 clear_cmdline = TRUE;
9305 }
9306
9307 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9308
9309 win_rest_invalid(firstwin);
9310 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009311 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 if (must_redraw == CLEAR) /* no need to clear again */
9313 must_redraw = NOT_VALID;
9314 compute_cmdrow();
9315 msg_row = cmdline_row; /* put cursor on last line for messages */
9316 msg_col = 0;
9317 screen_start(); /* don't know where cursor is now */
9318 msg_scrolled = 0; /* can't scroll back */
9319 msg_didany = FALSE;
9320 msg_didout = FALSE;
9321}
9322
9323/*
9324 * Clear one line in ScreenLines.
9325 */
9326 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009327lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328{
9329 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9330#ifdef FEAT_MBYTE
9331 if (enc_utf8)
9332 (void)vim_memset(ScreenLinesUC + off, 0,
9333 (size_t)width * sizeof(u8char_T));
9334#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009335 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336}
9337
9338/*
9339 * Mark one line in ScreenLines invalid by setting the attributes to an
9340 * invalid value.
9341 */
9342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009343lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344{
9345 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9346}
9347
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348/*
9349 * Copy part of a Screenline for vertically split window "wp".
9350 */
9351 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009352linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
9354 unsigned off_to = LineOffset[to] + wp->w_wincol;
9355 unsigned off_from = LineOffset[from] + wp->w_wincol;
9356
9357 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9358 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009359#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 if (enc_utf8)
9361 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009362 int i;
9363
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9365 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009366 for (i = 0; i < p_mco; ++i)
9367 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9368 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 }
9370 if (enc_dbcs == DBCS_JPNU)
9371 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9372 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9375 wp->w_width * sizeof(sattr_T));
9376}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
9378/*
9379 * Return TRUE if clearing with term string "p" would work.
9380 * It can't work when the string is empty or it won't set the right background.
9381 */
9382 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009383can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385 return (*p != NUL && (t_colors <= 1
9386#ifdef FEAT_GUI
9387 || gui.in_use
9388#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009389#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009390 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009391 || (!p_tgc && cterm_normal_bg_color == 0)
9392#else
9393 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009394#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009395 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396}
9397
9398/*
9399 * Reset cursor position. Use whenever cursor was moved because of outputting
9400 * something directly to the screen (shell commands) or a terminal control
9401 * code.
9402 */
9403 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009404screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405{
9406 screen_cur_row = screen_cur_col = 9999;
9407}
9408
9409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 * Move the cursor to position "row","col" in the screen.
9411 * This tries to find the most efficient way to move, minimizing the number of
9412 * characters sent to the terminal.
9413 */
9414 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009415windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009417 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 int i;
9419 int plan;
9420 int cost;
9421 int wouldbe_col;
9422 int noinvcurs;
9423 char_u *bs;
9424 int goto_cost;
9425 int attr;
9426
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009427#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9429
9430#define PLAN_LE 1
9431#define PLAN_CR 2
9432#define PLAN_NL 3
9433#define PLAN_WRITE 4
9434 /* Can't use ScreenLines unless initialized */
9435 if (ScreenLines == NULL)
9436 return;
9437
9438 if (col != screen_cur_col || row != screen_cur_row)
9439 {
9440 /* Check for valid position. */
9441 if (row < 0) /* window without text lines? */
9442 row = 0;
9443 if (row >= screen_Rows)
9444 row = screen_Rows - 1;
9445 if (col >= screen_Columns)
9446 col = screen_Columns - 1;
9447
9448 /* check if no cursor movement is allowed in highlight mode */
9449 if (screen_attr && *T_MS == NUL)
9450 noinvcurs = HIGHL_COST;
9451 else
9452 noinvcurs = 0;
9453 goto_cost = GOTO_COST + noinvcurs;
9454
9455 /*
9456 * Plan how to do the positioning:
9457 * 1. Use CR to move it to column 0, same row.
9458 * 2. Use T_LE to move it a few columns to the left.
9459 * 3. Use NL to move a few lines down, column 0.
9460 * 4. Move a few columns to the right with T_ND or by writing chars.
9461 *
9462 * Don't do this if the cursor went beyond the last column, the cursor
9463 * position is unknown then (some terminals wrap, some don't )
9464 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009465 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 * characters to move the cursor to the right.
9467 */
9468 if (row >= screen_cur_row && screen_cur_col < Columns)
9469 {
9470 /*
9471 * If the cursor is in the same row, bigger col, we can use CR
9472 * or T_LE.
9473 */
9474 bs = NULL; /* init for GCC */
9475 attr = screen_attr;
9476 if (row == screen_cur_row && col < screen_cur_col)
9477 {
9478 /* "le" is preferred over "bc", because "bc" is obsolete */
9479 if (*T_LE)
9480 bs = T_LE; /* "cursor left" */
9481 else
9482 bs = T_BC; /* "backspace character (old) */
9483 if (*bs)
9484 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9485 else
9486 cost = 999;
9487 if (col + 1 < cost) /* using CR is less characters */
9488 {
9489 plan = PLAN_CR;
9490 wouldbe_col = 0;
9491 cost = 1; /* CR is just one character */
9492 }
9493 else
9494 {
9495 plan = PLAN_LE;
9496 wouldbe_col = col;
9497 }
9498 if (noinvcurs) /* will stop highlighting */
9499 {
9500 cost += noinvcurs;
9501 attr = 0;
9502 }
9503 }
9504
9505 /*
9506 * If the cursor is above where we want to be, we can use CR LF.
9507 */
9508 else if (row > screen_cur_row)
9509 {
9510 plan = PLAN_NL;
9511 wouldbe_col = 0;
9512 cost = (row - screen_cur_row) * 2; /* CR LF */
9513 if (noinvcurs) /* will stop highlighting */
9514 {
9515 cost += noinvcurs;
9516 attr = 0;
9517 }
9518 }
9519
9520 /*
9521 * If the cursor is in the same row, smaller col, just use write.
9522 */
9523 else
9524 {
9525 plan = PLAN_WRITE;
9526 wouldbe_col = screen_cur_col;
9527 cost = 0;
9528 }
9529
9530 /*
9531 * Check if any characters that need to be written have the
9532 * correct attributes. Also avoid UTF-8 characters.
9533 */
9534 i = col - wouldbe_col;
9535 if (i > 0)
9536 cost += i;
9537 if (cost < goto_cost && i > 0)
9538 {
9539 /*
9540 * Check if the attributes are correct without additionally
9541 * stopping highlighting.
9542 */
9543 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9544 while (i && *p++ == attr)
9545 --i;
9546 if (i != 0)
9547 {
9548 /*
9549 * Try if it works when highlighting is stopped here.
9550 */
9551 if (*--p == 0)
9552 {
9553 cost += noinvcurs;
9554 while (i && *p++ == 0)
9555 --i;
9556 }
9557 if (i != 0)
9558 cost = 999; /* different attributes, don't do it */
9559 }
9560#ifdef FEAT_MBYTE
9561 if (enc_utf8)
9562 {
9563 /* Don't use an UTF-8 char for positioning, it's slow. */
9564 for (i = wouldbe_col; i < col; ++i)
9565 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9566 {
9567 cost = 999;
9568 break;
9569 }
9570 }
9571#endif
9572 }
9573
9574 /*
9575 * We can do it without term_windgoto()!
9576 */
9577 if (cost < goto_cost)
9578 {
9579 if (plan == PLAN_LE)
9580 {
9581 if (noinvcurs)
9582 screen_stop_highlight();
9583 while (screen_cur_col > col)
9584 {
9585 out_str(bs);
9586 --screen_cur_col;
9587 }
9588 }
9589 else if (plan == PLAN_CR)
9590 {
9591 if (noinvcurs)
9592 screen_stop_highlight();
9593 out_char('\r');
9594 screen_cur_col = 0;
9595 }
9596 else if (plan == PLAN_NL)
9597 {
9598 if (noinvcurs)
9599 screen_stop_highlight();
9600 while (screen_cur_row < row)
9601 {
9602 out_char('\n');
9603 ++screen_cur_row;
9604 }
9605 screen_cur_col = 0;
9606 }
9607
9608 i = col - screen_cur_col;
9609 if (i > 0)
9610 {
9611 /*
9612 * Use cursor-right if it's one character only. Avoids
9613 * removing a line of pixels from the last bold char, when
9614 * using the bold trick in the GUI.
9615 */
9616 if (T_ND[0] != NUL && T_ND[1] == NUL)
9617 {
9618 while (i-- > 0)
9619 out_char(*T_ND);
9620 }
9621 else
9622 {
9623 int off;
9624
9625 off = LineOffset[row] + screen_cur_col;
9626 while (i-- > 0)
9627 {
9628 if (ScreenAttrs[off] != screen_attr)
9629 screen_stop_highlight();
9630#ifdef FEAT_MBYTE
9631 out_flush_check();
9632#endif
9633 out_char(ScreenLines[off]);
9634#ifdef FEAT_MBYTE
9635 if (enc_dbcs == DBCS_JPNU
9636 && ScreenLines[off] == 0x8e)
9637 out_char(ScreenLines2[off]);
9638#endif
9639 ++off;
9640 }
9641 }
9642 }
9643 }
9644 }
9645 else
9646 cost = 999;
9647
9648 if (cost >= goto_cost)
9649 {
9650 if (noinvcurs)
9651 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009652 if (row == screen_cur_row && (col > screen_cur_col)
9653 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 term_cursor_right(col - screen_cur_col);
9655 else
9656 term_windgoto(row, col);
9657 }
9658 screen_cur_row = row;
9659 screen_cur_col = col;
9660 }
9661}
9662
9663/*
9664 * Set cursor to its position in the current window.
9665 */
9666 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009667setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009669 setcursor_mayforce(FALSE);
9670}
9671
9672/*
9673 * Set cursor to its position in the current window.
9674 * When "force" is TRUE also when not redrawing.
9675 */
9676 void
9677setcursor_mayforce(int force)
9678{
9679 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 {
9681 validate_cursor();
9682 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009683 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009685 /* With 'rightleft' set and the cursor on a double-wide
9686 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009687 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009689 (has_mbyte
9690 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9691 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692# endif
9693 1)) :
9694#endif
9695 curwin->w_wcol));
9696 }
9697}
9698
9699
9700/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009701 * Insert 'line_count' lines at 'row' in window 'wp'.
9702 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9703 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 * scrolling.
9705 * Returns FAIL if the lines are not inserted, OK for success.
9706 */
9707 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009708win_ins_lines(
9709 win_T *wp,
9710 int row,
9711 int line_count,
9712 int invalid,
9713 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714{
9715 int did_delete;
9716 int nextrow;
9717 int lastrow;
9718 int retval;
9719
9720 if (invalid)
9721 wp->w_lines_valid = 0;
9722
9723 if (wp->w_height < 5)
9724 return FAIL;
9725
9726 if (line_count > wp->w_height - row)
9727 line_count = wp->w_height - row;
9728
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009729 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 if (retval != MAYBE)
9731 return retval;
9732
9733 /*
9734 * If there is a next window or a status line, we first try to delete the
9735 * lines at the bottom to avoid messing what is after the window.
9736 * If this fails and there are following windows, don't do anything to avoid
9737 * messing up those windows, better just redraw.
9738 */
9739 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 if (wp->w_next != NULL || wp->w_status_height)
9741 {
9742 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009743 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 did_delete = TRUE;
9745 else if (wp->w_next)
9746 return FAIL;
9747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 /*
9749 * if no lines deleted, blank the lines that will end up below the window
9750 */
9751 if (!did_delete)
9752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009755 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 lastrow = nextrow + line_count;
9757 if (lastrow > Rows)
9758 lastrow = Rows;
9759 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009760 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 ' ', ' ', 0);
9762 }
9763
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009764 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 == FAIL)
9766 {
9767 /* deletion will have messed up other windows */
9768 if (did_delete)
9769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 win_rest_invalid(W_NEXT(wp));
9772 }
9773 return FAIL;
9774 }
9775
9776 return OK;
9777}
9778
9779/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009780 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9782 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9783 * scrolling
9784 * Return OK for success, FAIL if the lines are not deleted.
9785 */
9786 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009787win_del_lines(
9788 win_T *wp,
9789 int row,
9790 int line_count,
9791 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009792 int mayclear,
9793 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795 int retval;
9796
9797 if (invalid)
9798 wp->w_lines_valid = 0;
9799
9800 if (line_count > wp->w_height - row)
9801 line_count = wp->w_height - row;
9802
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009803 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (retval != MAYBE)
9805 return retval;
9806
9807 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009808 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 return FAIL;
9810
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 /*
9812 * If there are windows or status lines below, try to put them at the
9813 * correct place. If we can't do that, they have to be redrawn.
9814 */
9815 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9816 {
9817 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009818 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 {
9820 wp->w_redr_status = TRUE;
9821 win_rest_invalid(wp->w_next);
9822 }
9823 }
9824 /*
9825 * If this is the last window and there is no status line, redraw the
9826 * command line later.
9827 */
9828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 redraw_cmdline = TRUE;
9830 return OK;
9831}
9832
9833/*
9834 * Common code for win_ins_lines() and win_del_lines().
9835 * Returns OK or FAIL when the work has been done.
9836 * Returns MAYBE when not finished yet.
9837 */
9838 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009839win_do_lines(
9840 win_T *wp,
9841 int row,
9842 int line_count,
9843 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009844 int del,
9845 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
9847 int retval;
9848
9849 if (!redrawing() || line_count <= 0)
9850 return FAIL;
9851
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009852 /* When inserting lines would result in loss of command output, just redraw
9853 * the lines. */
9854 if (no_win_do_lines_ins && !del)
9855 return FAIL;
9856
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009858 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009860 if (!no_win_do_lines_ins)
9861 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 return FAIL;
9863 }
9864
9865 /*
9866 * Delete all remaining lines
9867 */
9868 if (row + line_count >= wp->w_height)
9869 {
9870 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009871 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 ' ', ' ', 0);
9873 return OK;
9874 }
9875
9876 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009877 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009879 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009881 if (!no_win_do_lines_ins)
9882 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883
9884 /*
9885 * If the terminal can set a scroll region, use that.
9886 * Always do this in a vertically split window. This will redraw from
9887 * ScreenLines[] when t_CV isn't defined. That's faster than using
9888 * win_line().
9889 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009890 * a character in the lower right corner of the scroll region may cause a
9891 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009893 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 scroll_region_set(wp, row);
9897 if (del)
9898 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009899 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 else
9901 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009902 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 scroll_region_reset();
9905 return retval;
9906 }
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910
9911 return MAYBE;
9912}
9913
9914/*
9915 * window 'wp' and everything after it is messed up, mark it for redraw
9916 */
9917 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009918win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 {
9922 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 wp->w_redr_status = TRUE;
9924 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 }
9926 redraw_cmdline = TRUE;
9927}
9928
9929/*
9930 * The rest of the routines in this file perform screen manipulations. The
9931 * given operation is performed physically on the screen. The corresponding
9932 * change is also made to the internal screen image. In this way, the editor
9933 * anticipates the effect of editing changes on the appearance of the screen.
9934 * That way, when we call screenupdate a complete redraw isn't usually
9935 * necessary. Another advantage is that we can keep adding code to anticipate
9936 * screen changes, and in the meantime, everything still works.
9937 */
9938
9939/*
9940 * types for inserting or deleting lines
9941 */
9942#define USE_T_CAL 1
9943#define USE_T_CDL 2
9944#define USE_T_AL 3
9945#define USE_T_CE 4
9946#define USE_T_DL 5
9947#define USE_T_SR 6
9948#define USE_NL 7
9949#define USE_T_CD 8
9950#define USE_REDRAW 9
9951
9952/*
9953 * insert lines on the screen and update ScreenLines[]
9954 * 'end' is the line after the scrolled part. Normally it is Rows.
9955 * When scrolling region used 'off' is the offset from the top for the region.
9956 * 'row' and 'end' are relative to the start of the region.
9957 *
9958 * return FAIL for failure, OK for success.
9959 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009960 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009961screen_ins_lines(
9962 int off,
9963 int row,
9964 int line_count,
9965 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009966 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009967 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968{
9969 int i;
9970 int j;
9971 unsigned temp;
9972 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009973 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 int type;
9975 int result_empty;
9976 int can_ce = can_clear(T_CE);
9977
9978 /*
9979 * FAIL if
9980 * - there is no valid screen
9981 * - the screen has to be redrawn completely
9982 * - the line count is less than one
9983 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009984 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009986 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9987#ifdef FEAT_CLIPBOARD
9988 || (clip_star.state != SELECT_CLEARED
9989 && redrawing_for_callback > 0)
9990#endif
9991 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 return FAIL;
9993
9994 /*
9995 * There are seven ways to insert lines:
9996 * 0. When in a vertically split window and t_CV isn't set, redraw the
9997 * characters from ScreenLines[].
9998 * 1. Use T_CD (clear to end of display) if it exists and the result of
9999 * the insert is just empty lines
10000 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10001 * present or line_count > 1. It looks better if we do all the inserts
10002 * at once.
10003 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10004 * insert is just empty lines and T_CE is not present or line_count >
10005 * 1.
10006 * 4. Use T_AL (insert line) if it exists.
10007 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10008 * just empty lines.
10009 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10010 * just empty lines.
10011 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10012 * the 'da' flag is not set or we have clear line capability.
10013 * 8. redraw the characters from ScreenLines[].
10014 *
10015 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10016 * the scrollbar for the window. It does have insert line, use that if it
10017 * exists.
10018 */
10019 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10021 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010022 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 type = USE_T_CD;
10024 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10025 type = USE_T_CAL;
10026 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10027 type = USE_T_CDL;
10028 else if (*T_AL != NUL)
10029 type = USE_T_AL;
10030 else if (can_ce && result_empty)
10031 type = USE_T_CE;
10032 else if (*T_DL != NUL && result_empty)
10033 type = USE_T_DL;
10034 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10035 type = USE_T_SR;
10036 else
10037 return FAIL;
10038
10039 /*
10040 * For clearing the lines screen_del_lines() is used. This will also take
10041 * care of t_db if necessary.
10042 */
10043 if (type == USE_T_CD || type == USE_T_CDL ||
10044 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010045 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046
10047 /*
10048 * If text is retained below the screen, first clear or delete as many
10049 * lines at the bottom of the window as are about to be inserted so that
10050 * the deleted lines won't later surface during a screen_del_lines.
10051 */
10052 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010053 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054
10055#ifdef FEAT_CLIPBOARD
10056 /* Remove a modeless selection when inserting lines halfway the screen
10057 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010058 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010059 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 else
10061 clip_scroll_selection(-line_count);
10062#endif
10063
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064#ifdef FEAT_GUI
10065 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10066 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010067 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068#endif
10069
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010070 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10071 cursor_col = wp->w_wincol;
10072
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 if (*T_CCS != NUL) /* cursor relative to region */
10074 cursor_row = row;
10075 else
10076 cursor_row = row + off;
10077
10078 /*
10079 * Shift LineOffset[] line_count down to reflect the inserted lines.
10080 * Clear the inserted lines in ScreenLines[].
10081 */
10082 row += off;
10083 end += off;
10084 for (i = 0; i < line_count; ++i)
10085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 if (wp != NULL && wp->w_width != Columns)
10087 {
10088 /* need to copy part of a line */
10089 j = end - 1 - i;
10090 while ((j -= line_count) >= row)
10091 linecopy(j + line_count, j, wp);
10092 j += line_count;
10093 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010094 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10095 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 else
10097 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10098 LineWraps[j] = FALSE;
10099 }
10100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
10102 j = end - 1 - i;
10103 temp = LineOffset[j];
10104 while ((j -= line_count) >= row)
10105 {
10106 LineOffset[j + line_count] = LineOffset[j];
10107 LineWraps[j + line_count] = LineWraps[j];
10108 }
10109 LineOffset[j + line_count] = temp;
10110 LineWraps[j + line_count] = FALSE;
10111 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010112 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 else
10114 lineinvalid(temp, (int)Columns);
10115 }
10116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117
10118 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010119 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010120 if (clear_attr != 0)
10121 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 /* redraw the characters */
10124 if (type == USE_REDRAW)
10125 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010126 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 {
10128 term_append_lines(line_count);
10129 screen_start(); /* don't know where cursor is now */
10130 }
10131 else
10132 {
10133 for (i = 0; i < line_count; i++)
10134 {
10135 if (type == USE_T_AL)
10136 {
10137 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010138 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 out_str(T_AL);
10140 }
10141 else /* type == USE_T_SR */
10142 out_str(T_SR);
10143 screen_start(); /* don't know where cursor is now */
10144 }
10145 }
10146
10147 /*
10148 * With scroll-reverse and 'da' flag set we need to clear the lines that
10149 * have been scrolled down into the region.
10150 */
10151 if (type == USE_T_SR && *T_DA)
10152 {
10153 for (i = 0; i < line_count; ++i)
10154 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010155 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 out_str(T_CE);
10157 screen_start(); /* don't know where cursor is now */
10158 }
10159 }
10160
10161#ifdef FEAT_GUI
10162 gui_can_update_cursor();
10163 if (gui.in_use)
10164 out_flush(); /* always flush after a scroll */
10165#endif
10166 return OK;
10167}
10168
10169/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010170 * Delete lines on the screen and update ScreenLines[].
10171 * "end" is the line after the scrolled part. Normally it is Rows.
10172 * When scrolling region used "off" is the offset from the top for the region.
10173 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 *
10175 * Return OK for success, FAIL if the lines are not deleted.
10176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010178screen_del_lines(
10179 int off,
10180 int row,
10181 int line_count,
10182 int end,
10183 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010184 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010185 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186{
10187 int j;
10188 int i;
10189 unsigned temp;
10190 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010191 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 int cursor_end;
10193 int result_empty; /* result is empty until end of region */
10194 int can_delete; /* deleting line codes can be used */
10195 int type;
10196
10197 /*
10198 * FAIL if
10199 * - there is no valid screen
10200 * - the screen has to be redrawn completely
10201 * - the line count is less than one
10202 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010203 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010205 if (!screen_valid(TRUE) || line_count <= 0
10206 || (!force && line_count > p_ttyscroll)
10207#ifdef FEAT_CLIPBOARD
10208 || (clip_star.state != SELECT_CLEARED
10209 && redrawing_for_callback > 0)
10210#endif
10211 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 return FAIL;
10213
10214 /*
10215 * Check if the rest of the current region will become empty.
10216 */
10217 result_empty = row + line_count >= end;
10218
10219 /*
10220 * We can delete lines only when 'db' flag not set or when 'ce' option
10221 * available.
10222 */
10223 can_delete = (*T_DB == NUL || can_clear(T_CE));
10224
10225 /*
10226 * There are six ways to delete lines:
10227 * 0. When in a vertically split window and t_CV isn't set, redraw the
10228 * characters from ScreenLines[].
10229 * 1. Use T_CD if it exists and the result is empty.
10230 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10231 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10232 * none of the other ways work.
10233 * 4. Use T_CE (erase line) if the result is empty.
10234 * 5. Use T_DL (delete line) if it exists.
10235 * 6. redraw the characters from ScreenLines[].
10236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10238 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010239 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 type = USE_T_CD;
10241#if defined(__BEOS__) && defined(BEOS_DR8)
10242 /*
10243 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10244 * its internal termcap... this works okay for tests which test *T_DB !=
10245 * NUL. It has the disadvantage that the user cannot use any :set t_*
10246 * command to get T_DB (back) to empty_option, only :set term=... will do
10247 * the trick...
10248 * Anyway, this hack will hopefully go away with the next OS release.
10249 * (Olaf Seibert)
10250 */
10251 else if (row == 0 && T_DB == empty_option
10252 && (line_count == 1 || *T_CDL == NUL))
10253#else
10254 else if (row == 0 && (
10255#ifndef AMIGA
10256 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10257 * up, so use delete-line command */
10258 line_count == 1 ||
10259#endif
10260 *T_CDL == NUL))
10261#endif
10262 type = USE_NL;
10263 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10264 type = USE_T_CDL;
10265 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010266 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 type = USE_T_CE;
10268 else if (*T_DL != NUL && can_delete)
10269 type = USE_T_DL;
10270 else if (*T_CDL != NUL && can_delete)
10271 type = USE_T_CDL;
10272 else
10273 return FAIL;
10274
10275#ifdef FEAT_CLIPBOARD
10276 /* Remove a modeless selection when deleting lines halfway the screen or
10277 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010278 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010279 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 else
10281 clip_scroll_selection(line_count);
10282#endif
10283
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284#ifdef FEAT_GUI
10285 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10286 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010287 gui_dont_update_cursor(gui.cursor_row >= row + off
10288 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289#endif
10290
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010291 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10292 cursor_col = wp->w_wincol;
10293
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 if (*T_CCS != NUL) /* cursor relative to region */
10295 {
10296 cursor_row = row;
10297 cursor_end = end;
10298 }
10299 else
10300 {
10301 cursor_row = row + off;
10302 cursor_end = end + off;
10303 }
10304
10305 /*
10306 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10307 * Clear the inserted lines in ScreenLines[].
10308 */
10309 row += off;
10310 end += off;
10311 for (i = 0; i < line_count; ++i)
10312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 if (wp != NULL && wp->w_width != Columns)
10314 {
10315 /* need to copy part of a line */
10316 j = row + i;
10317 while ((j += line_count) <= end - 1)
10318 linecopy(j - line_count, j, wp);
10319 j -= line_count;
10320 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010321 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10322 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 else
10324 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10325 LineWraps[j] = FALSE;
10326 }
10327 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 {
10329 /* whole width, moving the line pointers is faster */
10330 j = row + i;
10331 temp = LineOffset[j];
10332 while ((j += line_count) <= end - 1)
10333 {
10334 LineOffset[j - line_count] = LineOffset[j];
10335 LineWraps[j - line_count] = LineWraps[j];
10336 }
10337 LineOffset[j - line_count] = temp;
10338 LineWraps[j - line_count] = FALSE;
10339 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010340 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 else
10342 lineinvalid(temp, (int)Columns);
10343 }
10344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010346 if (screen_attr != clear_attr)
10347 screen_stop_highlight();
10348 if (clear_attr != 0)
10349 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 /* redraw the characters */
10352 if (type == USE_REDRAW)
10353 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010354 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010356 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 out_str(T_CD);
10358 screen_start(); /* don't know where cursor is now */
10359 }
10360 else if (type == USE_T_CDL)
10361 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010362 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 term_delete_lines(line_count);
10364 screen_start(); /* don't know where cursor is now */
10365 }
10366 /*
10367 * Deleting lines at top of the screen or scroll region: Just scroll
10368 * the whole screen (scroll region) up by outputting newlines on the
10369 * last line.
10370 */
10371 else if (type == USE_NL)
10372 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010373 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 for (i = line_count; --i >= 0; )
10375 out_char('\n'); /* cursor will remain on same line */
10376 }
10377 else
10378 {
10379 for (i = line_count; --i >= 0; )
10380 {
10381 if (type == USE_T_DL)
10382 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010383 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 out_str(T_DL); /* delete a line */
10385 }
10386 else /* type == USE_T_CE */
10387 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010388 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 out_str(T_CE); /* erase a line */
10390 }
10391 screen_start(); /* don't know where cursor is now */
10392 }
10393 }
10394
10395 /*
10396 * If the 'db' flag is set, we need to clear the lines that have been
10397 * scrolled up at the bottom of the region.
10398 */
10399 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10400 {
10401 for (i = line_count; i > 0; --i)
10402 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010403 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 out_str(T_CE); /* erase a line */
10405 screen_start(); /* don't know where cursor is now */
10406 }
10407 }
10408
10409#ifdef FEAT_GUI
10410 gui_can_update_cursor();
10411 if (gui.in_use)
10412 out_flush(); /* always flush after a scroll */
10413#endif
10414
10415 return OK;
10416}
10417
10418/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010419 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 *
10421 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10422 * If clear_cmdline is FALSE there may be a message there that needs to be
10423 * cleared only if a mode is shown.
10424 * Return the length of the message (0 if no message).
10425 */
10426 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010427showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428{
10429 int need_clear;
10430 int length = 0;
10431 int do_mode;
10432 int attr;
10433 int nwr_save;
10434#ifdef FEAT_INS_EXPAND
10435 int sub_attr;
10436#endif
10437
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010438 do_mode = ((p_smd && msg_silent == 0)
10439 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010440 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010441 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010442 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 {
10444 /*
10445 * Don't show mode right now, when not redrawing or inside a mapping.
10446 * Call char_avail() only when we are going to show something, because
10447 * it takes a bit of time.
10448 */
10449 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10450 {
10451 redraw_cmdline = TRUE; /* show mode later */
10452 return 0;
10453 }
10454
10455 nwr_save = need_wait_return;
10456
10457 /* wait a bit before overwriting an important message */
10458 check_for_delay(FALSE);
10459
10460 /* if the cmdline is more than one line high, erase top lines */
10461 need_clear = clear_cmdline;
10462 if (clear_cmdline && cmdline_row < Rows - 1)
10463 msg_clr_cmdline(); /* will reset clear_cmdline */
10464
10465 /* Position on the last line in the window, column 0 */
10466 msg_pos_mode();
10467 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010468 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 if (do_mode)
10470 {
10471 MSG_PUTS_ATTR("--", attr);
10472#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010473 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010474# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010475 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010476# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010477 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010478# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010479 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010480# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 MSG_PUTS_ATTR(" IM", attr);
10482# else
10483 MSG_PUTS_ATTR(" XIM", attr);
10484# endif
10485#endif
10486#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10487 if (gui.in_use)
10488 {
10489 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010490 {
10491 /* HANGUL */
10492 if (enc_utf8)
10493 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10494 else
10495 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 }
10498#endif
10499#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010500 /* CTRL-X in Insert mode */
10501 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 {
10503 /* These messages can get long, avoid a wrap in a narrow
10504 * window. Prefer showing edit_submode_extra. */
10505 length = (Rows - msg_row) * Columns - 3;
10506 if (edit_submode_extra != NULL)
10507 length -= vim_strsize(edit_submode_extra);
10508 if (length > 0)
10509 {
10510 if (edit_submode_pre != NULL)
10511 length -= vim_strsize(edit_submode_pre);
10512 if (length - vim_strsize(edit_submode) > 0)
10513 {
10514 if (edit_submode_pre != NULL)
10515 msg_puts_attr(edit_submode_pre, attr);
10516 msg_puts_attr(edit_submode, attr);
10517 }
10518 if (edit_submode_extra != NULL)
10519 {
10520 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10521 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010522 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 else
10524 sub_attr = attr;
10525 msg_puts_attr(edit_submode_extra, sub_attr);
10526 }
10527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 }
10529 else
10530#endif
10531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 if (State & VREPLACE_FLAG)
10533 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010534 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10536 else if (State & INSERT)
10537 {
10538#ifdef FEAT_RIGHTLEFT
10539 if (p_ri)
10540 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10541#endif
10542 MSG_PUTS_ATTR(_(" INSERT"), attr);
10543 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010544 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 MSG_PUTS_ATTR(_(" (insert)"), attr);
10546 else if (restart_edit == 'R')
10547 MSG_PUTS_ATTR(_(" (replace)"), attr);
10548 else if (restart_edit == 'V')
10549 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10550#ifdef FEAT_RIGHTLEFT
10551 if (p_hkmap)
10552 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10553# ifdef FEAT_FKMAP
10554 if (p_fkmap)
10555 MSG_PUTS_ATTR(farsi_text_5, attr);
10556# endif
10557#endif
10558#ifdef FEAT_KEYMAP
10559 if (State & LANGMAP)
10560 {
10561# ifdef FEAT_ARABIC
10562 if (curwin->w_p_arab)
10563 MSG_PUTS_ATTR(_(" Arabic"), attr);
10564 else
10565# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010566 if (get_keymap_str(curwin, (char_u *)" (%s)",
10567 NameBuff, MAXPATHL))
10568 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 }
10570#endif
10571 if ((State & INSERT) && p_paste)
10572 MSG_PUTS_ATTR(_(" (paste)"), attr);
10573
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 if (VIsual_active)
10575 {
10576 char *p;
10577
10578 /* Don't concatenate separate words to avoid translation
10579 * problems. */
10580 switch ((VIsual_select ? 4 : 0)
10581 + (VIsual_mode == Ctrl_V) * 2
10582 + (VIsual_mode == 'V'))
10583 {
10584 case 0: p = N_(" VISUAL"); break;
10585 case 1: p = N_(" VISUAL LINE"); break;
10586 case 2: p = N_(" VISUAL BLOCK"); break;
10587 case 4: p = N_(" SELECT"); break;
10588 case 5: p = N_(" SELECT LINE"); break;
10589 default: p = N_(" SELECT BLOCK"); break;
10590 }
10591 MSG_PUTS_ATTR(_(p), attr);
10592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 MSG_PUTS_ATTR(" --", attr);
10594 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010595
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 need_clear = TRUE;
10597 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010598 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599#ifdef FEAT_INS_EXPAND
10600 && edit_submode == NULL /* otherwise it gets too long */
10601#endif
10602 )
10603 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010604 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 need_clear = TRUE;
10606 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010607
10608 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 if (need_clear || clear_cmdline)
10610 msg_clr_eos();
10611 msg_didout = FALSE; /* overwrite this message */
10612 length = msg_col;
10613 msg_col = 0;
10614 need_wait_return = nwr_save; /* never ask for hit-return for this */
10615 }
10616 else if (clear_cmdline && msg_silent == 0)
10617 /* Clear the whole command line. Will reset "clear_cmdline". */
10618 msg_clr_cmdline();
10619
10620#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 /* In Visual mode the size of the selected area must be redrawn. */
10622 if (VIsual_active)
10623 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624
10625 /* If the last window has no status line, the ruler is after the mode
10626 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010627 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010628 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629#endif
10630 redraw_cmdline = FALSE;
10631 clear_cmdline = FALSE;
10632
10633 return length;
10634}
10635
10636/*
10637 * Position for a mode message.
10638 */
10639 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010640msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641{
10642 msg_col = 0;
10643 msg_row = Rows - 1;
10644}
10645
10646/*
10647 * Delete mode message. Used when ESC is typed which is expected to end
10648 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010649 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 */
10651 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010652unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653{
10654 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010655 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 */
10657 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10658 redraw_cmdline = TRUE; /* delete mode later */
10659 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010660 clearmode();
10661}
10662
10663/*
10664 * Clear the mode message.
10665 */
10666 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010667clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010668{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010669 int save_msg_row = msg_row;
10670 int save_msg_col = msg_col;
10671
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010672 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010673 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010674 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010675 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010676
10677 msg_col = save_msg_col;
10678 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010681 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010682recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010683{
10684 MSG_PUTS_ATTR(_("recording"), attr);
10685 if (!shortmess(SHM_RECORDING))
10686 {
10687 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010688 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010689 MSG_PUTS_ATTR(s, attr);
10690 }
10691}
10692
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010693/*
10694 * Draw the tab pages line at the top of the Vim window.
10695 */
10696 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010697draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010698{
10699 int tabcount = 0;
10700 tabpage_T *tp;
10701 int tabwidth;
10702 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010703 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010704 int attr;
10705 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010706 win_T *cwp;
10707 int wincount;
10708 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010709 int c;
10710 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010711 int attr_sel = HL_ATTR(HLF_TPS);
10712 int attr_nosel = HL_ATTR(HLF_TP);
10713 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010714 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010715 int room;
10716 int use_sep_chars = (t_colors < 8
10717#ifdef FEAT_GUI
10718 && !gui.in_use
10719#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010720#ifdef FEAT_TERMGUICOLORS
10721 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010722#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010723 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010724
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010725 if (ScreenLines == NULL)
10726 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010727 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010728
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010729#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010730 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010731 if (gui_use_tabline())
10732 {
10733 gui_update_tabline();
10734 return;
10735 }
10736#endif
10737
10738 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010739 return;
10740
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010741#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010742
10743 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10744 for (scol = 0; scol < Columns; ++scol)
10745 TabPageIdxs[scol] = 0;
10746
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010747 /* Use the 'tabline' option if it's set. */
10748 if (*p_tal != NUL)
10749 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010750 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010751
10752 /* Check for an error. If there is one we would loop in redrawing the
10753 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010754 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010755 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010756 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010757 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010758 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010759 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010760 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010761 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010762#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010763 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010764 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010765 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010766
Bram Moolenaar238a5642006-02-21 22:12:05 +000010767 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10768 if (tabwidth < 6)
10769 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010770
Bram Moolenaar238a5642006-02-21 22:12:05 +000010771 attr = attr_nosel;
10772 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010773 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010774 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10775 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010776 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010777 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010778
Bram Moolenaar238a5642006-02-21 22:12:05 +000010779 if (tp->tp_topframe == topframe)
10780 attr = attr_sel;
10781 if (use_sep_chars && col > 0)
10782 screen_putchar('|', 0, col++, attr);
10783
10784 if (tp->tp_topframe != topframe)
10785 attr = attr_nosel;
10786
10787 screen_putchar(' ', 0, col++, attr);
10788
10789 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010790 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010791 cwp = curwin;
10792 wp = firstwin;
10793 }
10794 else
10795 {
10796 cwp = tp->tp_curwin;
10797 wp = tp->tp_firstwin;
10798 }
10799
10800 modified = FALSE;
10801 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10802 if (bufIsChanged(wp->w_buffer))
10803 modified = TRUE;
10804 if (modified || wincount > 1)
10805 {
10806 if (wincount > 1)
10807 {
10808 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010809 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010810 if (col + len >= Columns - 3)
10811 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010812 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010813#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010814 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010815#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010816 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010817#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010818 );
10819 col += len;
10820 }
10821 if (modified)
10822 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10823 screen_putchar(' ', 0, col++, attr);
10824 }
10825
10826 room = scol - col + tabwidth - 1;
10827 if (room > 0)
10828 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010829 /* Get buffer name in NameBuff[] */
10830 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010831 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010832 len = vim_strsize(NameBuff);
10833 p = NameBuff;
10834#ifdef FEAT_MBYTE
10835 if (has_mbyte)
10836 while (len > room)
10837 {
10838 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010839 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010840 }
10841 else
10842#endif
10843 if (len > room)
10844 {
10845 p += len - room;
10846 len = room;
10847 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010848 if (len > Columns - col - 1)
10849 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010850
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010851 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010852 col += len;
10853 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010854 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010855
10856 /* Store the tab page number in TabPageIdxs[], so that
10857 * jump_to_mouse() knows where each one is. */
10858 ++tabcount;
10859 while (scol < col)
10860 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010861 }
10862
Bram Moolenaar238a5642006-02-21 22:12:05 +000010863 if (use_sep_chars)
10864 c = '_';
10865 else
10866 c = ' ';
10867 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010868
10869 /* Put an "X" for closing the current tab if there are several. */
10870 if (first_tabpage->tp_next != NULL)
10871 {
10872 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10873 TabPageIdxs[Columns - 1] = -999;
10874 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010875 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010876
10877 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10878 * set. */
10879 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010880}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010881
10882/*
10883 * Get buffer name for "buf" into NameBuff[].
10884 * Takes care of special buffer names and translates special characters.
10885 */
10886 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010887get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010888{
10889 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010890 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010891 else
10892 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10893 trans_characters(NameBuff, MAXPATHL);
10894}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010895
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896/*
10897 * Get the character to use in a status line. Get its attributes in "*attr".
10898 */
10899 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010900fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901{
10902 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010903
10904#ifdef FEAT_TERMINAL
10905 if (bt_terminal(wp->w_buffer))
10906 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010907 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010908 {
10909 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010910 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010911 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010912 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010913 {
10914 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010915 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010916 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010917 }
10918 else
10919#endif
10920 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010922 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 fill = fill_stl;
10924 }
10925 else
10926 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010927 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 fill = fill_stlnc;
10929 }
10930 /* Use fill when there is highlighting, and highlighting of current
10931 * window differs, or the fillchars differ, or this is not the
10932 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010933 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010934 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 || (fill_stl != fill_stlnc)))
10936 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010937 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 return '^';
10939 return '=';
10940}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942/*
10943 * Get the character to use in a separator between vertically split windows.
10944 * Get its attributes in "*attr".
10945 */
10946 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010947fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010949 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 if (*attr == 0 && fill_vert == ' ')
10951 return '|';
10952 else
10953 return fill_vert;
10954}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955
10956/*
10957 * Return TRUE if redrawing should currently be done.
10958 */
10959 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010960redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010962#ifdef FEAT_EVAL
10963 if (disable_redraw_for_testing)
10964 return 0;
10965 else
10966#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010967 return ((!RedrawingDisabled
10968#ifdef FEAT_EVAL
10969 || ignore_redraw_flag_for_testing
10970#endif
10971 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972}
10973
10974/*
10975 * Return TRUE if printing messages should currently be done.
10976 */
10977 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010978messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979{
10980 return (!(p_lz && char_avail() && !KeyTyped));
10981}
10982
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010983#ifdef FEAT_MENU
10984/*
10985 * Draw the window toolbar.
10986 */
10987 static void
10988redraw_win_toolbar(win_T *wp)
10989{
10990 vimmenu_T *menu;
10991 int item_idx = 0;
10992 int item_count = 0;
10993 int col = 0;
10994 int next_col;
10995 int off = (int)(current_ScreenLine - ScreenLines);
10996 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10997 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10998
10999 vim_free(wp->w_winbar_items);
11000 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11001 ++item_count;
11002 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
11003 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
11004
11005 /* TODO: use fewer spaces if there is not enough room */
11006 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011007 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011008 {
11009 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011010 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011011 break;
11012 if (col > 1)
11013 {
11014 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011015 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011016 break;
11017 }
11018
11019 wp->w_winbar_items[item_idx].wb_startcol = col;
11020 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011021 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011022 break;
11023
11024 next_col = text_to_screenline(wp, menu->name, col);
11025 while (col < next_col)
11026 {
11027 ScreenAttrs[off + col] = button_attr;
11028 ++col;
11029 }
11030 wp->w_winbar_items[item_idx].wb_endcol = col;
11031 wp->w_winbar_items[item_idx].wb_menu = menu;
11032 ++item_idx;
11033
Bram Moolenaar02631462017-09-22 15:20:32 +020011034 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011035 break;
11036 space_to_screenline(off + col, button_attr);
11037 ++col;
11038 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011039 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011040 {
11041 space_to_screenline(off + col, fill_attr);
11042 ++col;
11043 }
11044 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11045
Bram Moolenaar02631462017-09-22 15:20:32 +020011046 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11047 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011048}
11049#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011050
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051/*
11052 * Show current status info in ruler and various other places
11053 * If always is FALSE, only show ruler if position has changed.
11054 */
11055 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011056showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057{
11058 if (!always && !redrawing())
11059 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011060#ifdef FEAT_INS_EXPAND
11061 if (pum_visible())
11062 {
11063 /* Don't redraw right now, do it later. */
11064 curwin->w_redr_status = TRUE;
11065 return;
11066 }
11067#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011068#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011069 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011070 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011071 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 else
11074#endif
11075#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011076 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077#endif
11078
11079#ifdef FEAT_TITLE
11080 if (need_maketitle
11081# ifdef FEAT_STL_OPT
11082 || (p_icon && (stl_syntax & STL_IN_ICON))
11083 || (p_title && (stl_syntax & STL_IN_TITLE))
11084# endif
11085 )
11086 maketitle();
11087#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011088 /* Redraw the tab pages line if needed. */
11089 if (redraw_tabline)
11090 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091}
11092
11093#ifdef FEAT_CMDL_INFO
11094 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011095win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011097#define RULER_BUF_LEN 70
11098 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 int row;
11100 int fillchar;
11101 int attr;
11102 int empty_line = FALSE;
11103 colnr_T virtcol;
11104 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011105 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 int this_ru_col;
11108 int off = 0;
11109 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110
11111 /* If 'ruler' off or redrawing disabled, don't do anything */
11112 if (!p_ru)
11113 return;
11114
11115 /*
11116 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11117 * after deleting lines, before cursor.lnum is corrected.
11118 */
11119 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11120 return;
11121
11122#ifdef FEAT_INS_EXPAND
11123 /* Don't draw the ruler while doing insert-completion, it might overwrite
11124 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 if (edit_submode != NULL)
11127 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011128 // Don't draw the ruler when the popup menu is visible, it may overlap.
11129 // Except when the popup menu will be redrawn anyway.
11130 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011131 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132#endif
11133
11134#ifdef FEAT_STL_OPT
11135 if (*p_ruf)
11136 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011137 int save_called_emsg = called_emsg;
11138
11139 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011141 if (called_emsg)
11142 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011143 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011144 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 return;
11146 }
11147#endif
11148
11149 /*
11150 * Check if not in Insert mode and the line is empty (will show "0-1").
11151 */
11152 if (!(State & INSERT)
11153 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11154 empty_line = TRUE;
11155
11156 /*
11157 * Only draw the ruler when something changed.
11158 */
11159 validate_virtcol_win(wp);
11160 if ( redraw_cmdline
11161 || always
11162 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11163 || wp->w_cursor.col != wp->w_ru_cursor.col
11164 || wp->w_virtcol != wp->w_ru_virtcol
11165#ifdef FEAT_VIRTUALEDIT
11166 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11167#endif
11168 || wp->w_topline != wp->w_ru_topline
11169 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11170#ifdef FEAT_DIFF
11171 || wp->w_topfill != wp->w_ru_topfill
11172#endif
11173 || empty_line != wp->w_ru_empty)
11174 {
11175 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 if (wp->w_status_height)
11177 {
11178 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011179 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011180 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011181 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 }
11183 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 {
11185 row = Rows - 1;
11186 fillchar = ' ';
11187 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 width = Columns;
11189 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 }
11191
11192 /* In list mode virtcol needs to be recomputed */
11193 virtcol = wp->w_virtcol;
11194 if (wp->w_p_list && lcs_tab1 == NUL)
11195 {
11196 wp->w_p_list = FALSE;
11197 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11198 wp->w_p_list = TRUE;
11199 }
11200
11201 /*
11202 * Some sprintfs return the length, some return a pointer.
11203 * To avoid portability problems we use strlen() here.
11204 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011205 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11207 ? 0L
11208 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011209 len = STRLEN(buffer);
11210 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11212 (int)virtcol + 1);
11213
11214 /*
11215 * Add a "50%" if there is room for it.
11216 * On the last line, don't print in the last column (scrolls the
11217 * screen up on some terminals).
11218 */
11219 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011220 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 this_ru_col = ru_col - (Columns - width);
11225 if (this_ru_col < 0)
11226 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 /* Never use more than half the window/screen width, leave the other
11228 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011229 if (this_ru_col < (width + 1) / 2)
11230 this_ru_col = (width + 1) / 2;
11231 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011233 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011234 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 {
11236#ifdef FEAT_MBYTE
11237 if (has_mbyte)
11238 i += (*mb_char2bytes)(fillchar, buffer + i);
11239 else
11240#endif
11241 buffer[i++] = fillchar;
11242 ++o;
11243 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011244 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 }
11246 /* Truncate at window boundary. */
11247#ifdef FEAT_MBYTE
11248 if (has_mbyte)
11249 {
11250 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011251 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 {
11253 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011254 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 {
11256 buffer[i] = NUL;
11257 break;
11258 }
11259 }
11260 }
11261 else
11262#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011263 if (this_ru_col + (int)STRLEN(buffer) > width)
11264 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265
Bram Moolenaar4033c552017-09-16 20:54:51 +020011266 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 i = redraw_cmdline;
11268 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011269 this_ru_col + off + (int)STRLEN(buffer),
11270 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 fillchar, fillchar, attr);
11272 /* don't redraw the cmdline because of showing the ruler */
11273 redraw_cmdline = i;
11274 wp->w_ru_cursor = wp->w_cursor;
11275 wp->w_ru_virtcol = wp->w_virtcol;
11276 wp->w_ru_empty = empty_line;
11277 wp->w_ru_topline = wp->w_topline;
11278 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11279#ifdef FEAT_DIFF
11280 wp->w_ru_topfill = wp->w_topfill;
11281#endif
11282 }
11283}
11284#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011285
11286#if defined(FEAT_LINEBREAK) || defined(PROTO)
11287/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011288 * Return the width of the 'number' and 'relativenumber' column.
11289 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011290 * Otherwise it depends on 'numberwidth' and the line count.
11291 */
11292 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011293number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011294{
11295 int n;
11296 linenr_T lnum;
11297
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011298 if (wp->w_p_rnu && !wp->w_p_nu)
11299 /* cursor line shows "0" */
11300 lnum = wp->w_height;
11301 else
11302 /* cursor line shows absolute line number */
11303 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011304
Bram Moolenaar6b314672015-03-20 15:42:10 +010011305 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011306 return wp->w_nrwidth_width;
11307 wp->w_nrwidth_line_count = lnum;
11308
11309 n = 0;
11310 do
11311 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011312 lnum /= 10;
11313 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011314 } while (lnum > 0);
11315
11316 /* 'numberwidth' gives the minimal width plus one */
11317 if (n < wp->w_p_nuw - 1)
11318 n = wp->w_p_nuw - 1;
11319
11320 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011321 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011322 return n;
11323}
11324#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011325
11326/*
11327 * Return the current cursor column. This is the actual position on the
11328 * screen. First column is 0.
11329 */
11330 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011331screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011332{
11333 return screen_cur_col;
11334}
11335
11336/*
11337 * Return the current cursor row. This is the actual position on the screen.
11338 * First row is 0.
11339 */
11340 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011341screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011342{
11343 return screen_cur_row;
11344}