blob: cd146fd6c1299b717dacaa0dff19ad8f3985baa4 [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 Moolenaar3633cf52017-07-31 22:29:35 +0200157static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200159#ifdef FEAT_MENU
160static void redraw_win_toolbar(win_T *wp);
161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200166static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169/* Ugly global: overrule attribute used by screen_char() */
170static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200172#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
173/* Can limit syntax highlight time to 'redrawtime'. */
174# define SYN_TIME_LIMIT 1
175#endif
176
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200177#ifdef FEAT_RIGHTLEFT
178# define HAS_RIGHTLEFT(x) x
179#else
180# define HAS_RIGHTLEFT(x) FALSE
181#endif
182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183/*
184 * Redraw the current window later, with update_screen(type).
185 * Set must_redraw only if not already set to a higher value.
186 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
187 */
188 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100189redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
191 redraw_win_later(curwin, type);
192}
193
194 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100195redraw_win_later(
196 win_T *wp,
197 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200199 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100214redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100232redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 win_T *wp;
235
236 FOR_ALL_WINDOWS(wp)
237 {
238 redraw_win_later(wp, type);
239 }
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100240 // This may be needed when switching tabs.
241 if (must_redraw < type)
242 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000246 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 */
248 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100249redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 redraw_buf_later(curbuf, type);
252}
253
254 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100255redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
257 win_T *wp;
258
259 FOR_ALL_WINDOWS(wp)
260 {
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
263 }
264}
265
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200266 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100267redraw_buf_line_later(buf_T *buf, linenr_T lnum)
268{
269 win_T *wp;
270
271 FOR_ALL_WINDOWS(wp)
272 if (wp->w_buffer == buf && lnum >= wp->w_topline
273 && lnum < wp->w_botline)
274 redrawWinline(wp, lnum);
275}
276
277 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200278redraw_buf_and_status_later(buf_T *buf, int type)
279{
280 win_T *wp;
281
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200282#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200283 if (wild_menu_showing != 0)
284 /* Don't redraw while the command line completion is displayed, it
285 * would disappear. */
286 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200287#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200288 FOR_ALL_WINDOWS(wp)
289 {
290 if (wp->w_buffer == buf)
291 {
292 redraw_win_later(wp, type);
293 wp->w_redr_status = TRUE;
294 }
295 }
296}
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 * Redraw as soon as possible. When the command line is not scrolled redraw
300 * right away and restore what was on the command line.
301 * Return a code indicating what happened.
302 */
303 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100304redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305{
306 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200307 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 int r;
309 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200310 schar_T *screenline; /* copy from ScreenLines[] */
311 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312#ifdef FEAT_MBYTE
313 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200314 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200315 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200316 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317#endif
318
319 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200320 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321 return ret;
322
323 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200324 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenline == NULL || screenattr == NULL)
330 ret = 2;
331#ifdef FEAT_MBYTE
332 if (enc_utf8)
333 {
334 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336 if (screenlineUC == NULL)
337 ret = 2;
338 for (i = 0; i < p_mco; ++i)
339 {
340 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 if (screenlineC[i] == NULL)
343 ret = 2;
344 }
345 }
346 if (enc_dbcs == DBCS_JPNU)
347 {
348 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 if (screenline2 == NULL)
351 ret = 2;
352 }
353#endif
354
355 if (ret != 2)
356 {
357 /* Save the text displayed in the command line area. */
358 for (r = 0; r < rows; ++r)
359 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 (size_t)cols * sizeof(schar_T));
363 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366#ifdef FEAT_MBYTE
367 if (enc_utf8)
368 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 mch_memmove(screenlineC[i] + r * cols,
374 ScreenLinesC[i] + LineOffset[cmdline_row + r],
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 }
377 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200380 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200381#endif
382 }
383
384 update_screen(0);
385 ret = 3;
386
387 if (must_redraw == 0)
388 {
389 int off = (int)(current_ScreenLine - ScreenLines);
390
391 /* Restore the text displayed in the command line area. */
392 for (r = 0; r < rows; ++r)
393 {
394 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenline + r * cols,
396 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200398 screenattr + r * cols,
399 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200400#ifdef FEAT_MBYTE
401 if (enc_utf8)
402 {
403 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineUC + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 for (i = 0; i < p_mco; ++i)
407 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200408 screenlineC[i] + r * cols,
409 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411 if (enc_dbcs == DBCS_JPNU)
412 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200413 screenline2 + r * cols,
414 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200416 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200417 }
418 ret = 4;
419 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200420 }
421
422 vim_free(screenline);
423 vim_free(screenattr);
424#ifdef FEAT_MBYTE
425 if (enc_utf8)
426 {
427 vim_free(screenlineUC);
428 for (i = 0; i < p_mco; ++i)
429 vim_free(screenlineC[i]);
430 }
431 if (enc_dbcs == DBCS_JPNU)
432 vim_free(screenline2);
433#endif
434
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200435 /* Show the intro message when appropriate. */
436 maybe_intro_message();
437
438 setcursor();
439
Bram Moolenaar2951b772013-07-03 12:45:31 +0200440 return ret;
441}
442
443/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100444 * Invoked after an asynchronous callback is called.
445 * If an echo command was used the cursor needs to be put back where
446 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200447 * If "call_update_screen" is FALSE don't call update_screen() when at the
448 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100449 */
450 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200451redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100452{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200453 ++redrawing_for_callback;
454
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100455 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200456 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100457 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200458 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200459 // Don't redraw when in prompt_for_number().
460 if (cmdline_row > 0)
461 {
462 // Redrawing only works when the screen didn't scroll. Don't clear
463 // wildmenu entries.
464 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200465#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200466 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200467#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200468 && call_update_screen)
469 update_screen(0);
470
471 // Redraw in the same position, so that the user can continue
472 // editing the command.
473 redrawcmdline_ex(FALSE);
474 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200475 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200476 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100477 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200478 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200479 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100480 setcursor();
481 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100482 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100484 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200485 // Don't update the cursor when it is blinking and off to avoid
486 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100487 out_flush_cursor(FALSE, FALSE);
488 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100489#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100490 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200491
492 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100493}
494
495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 * Changed something in the current window, at buffer line "lnum", that
497 * requires that line and possibly other lines to be redrawn.
498 * Used when entering/leaving Insert mode with the cursor on a folded line.
499 * Used to remove the "$" from a change command.
500 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
501 * may become invalid and the whole window will have to be redrawn.
502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100504redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200505 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100506 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200508 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
509 wp->w_redraw_top = lnum;
510 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
511 wp->w_redraw_bot = lnum;
512 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513}
514
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200515 void
516reset_updating_screen(int may_resize_shell UNUSED)
517{
518 updating_screen = FALSE;
519#ifdef FEAT_GUI
520 if (may_resize_shell)
521 gui_may_resize_shell();
522#endif
523#ifdef FEAT_TERMINAL
524 term_check_channel_closed_recently();
525#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200526
527#ifdef HAVE_DROP_FILE
528 // If handle_drop() was called while updating_screen was TRUE need to
529 // handle the drop now.
530 handle_any_postponed_drop();
531#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200532}
533
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 */
537 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100538update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
540 redraw_curbuf_later(type);
541 update_screen(type);
542}
543
544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * Based on the current value of curwin->w_topline, transfer a screenfull
546 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200547 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200549 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200550update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 win_T *wp;
554 static int did_intro = FALSE;
555#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
556 int did_one;
557#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200558#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200559 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560 int gui_cursor_col;
561 int gui_cursor_row;
562#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200563 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000565 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200569 if (type == VALID_NO_UPDATE)
570 {
571 no_update = TRUE;
572 type = 0;
573 }
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (must_redraw)
576 {
577 if (type < must_redraw) /* use maximal type */
578 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000579
580 /* must_redraw is reset here, so that when we run into some weird
581 * reason to redraw while busy redrawing (e.g., asynchronous
582 * scrolling), or update_topline() in win_update() will cause a
583 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 must_redraw = 0;
585 }
586
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200587 /* May need to update w_lines[]. */
588 if (curwin->w_lines_valid == 0 && type < NOT_VALID
589#ifdef FEAT_TERMINAL
590 && !term_do_update_window(curwin)
591#endif
592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 type = NOT_VALID;
594
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000595 /* Postpone the redrawing when it's not needed and when being called
596 * recursively. */
597 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
599 redraw_later(type); /* remember type for next time */
600 must_redraw = type;
601 if (type > INVERTED_ALL)
602 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200603 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 }
605
606 updating_screen = TRUE;
607#ifdef FEAT_SYN_HL
608 ++display_tick; /* let syntax code know we're in a next round of
609 * display updating */
610#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200611 if (no_update)
612 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614 /*
615 * if the screen was scrolled up when displaying a message, scroll it down
616 */
617 if (msg_scrolled)
618 {
619 clear_cmdline = TRUE;
620 if (msg_scrolled > Rows - 5) /* clearing is faster */
621 type = CLEAR;
622 else if (type != CLEAR)
623 {
624 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200625 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
626 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 type = CLEAR;
628 FOR_ALL_WINDOWS(wp)
629 {
630 if (W_WINROW(wp) < msg_scrolled)
631 {
632 if (W_WINROW(wp) + wp->w_height > msg_scrolled
633 && wp->w_redr_type < REDRAW_TOP
634 && wp->w_lines_valid > 0
635 && wp->w_topline == wp->w_lines[0].wl_lnum)
636 {
637 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
638 wp->w_redr_type = REDRAW_TOP;
639 }
640 else
641 {
642 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200643 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
644 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
647 }
648 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200649 if (!no_update)
650 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000651 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653 msg_scrolled = 0;
654 need_wait_return = FALSE;
655 }
656
657 /* reset cmdline_row now (may have been changed temporarily) */
658 compute_cmdrow();
659
660 /* Check for changed highlighting */
661 if (need_highlight_changed)
662 highlight_changed();
663
664 if (type == CLEAR) /* first clear screen */
665 {
666 screenclear(); /* will reset clear_cmdline */
667 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200668 /* must_redraw may be set indirectly, avoid another redraw later */
669 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671
672 if (clear_cmdline) /* going to clear cmdline (done below) */
673 check_for_delay(FALSE);
674
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000675#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200676 /* Force redraw when width of 'number' or 'relativenumber' column
677 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000678 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200679 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
680 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000681 curwin->w_redr_type = NOT_VALID;
682#endif
683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 /*
685 * Only start redrawing if there is really something to do.
686 */
687 if (type == INVERTED)
688 update_curswant();
689 if (curwin->w_redr_type < type
690 && !((type == VALID
691 && curwin->w_lines[0].wl_valid
692#ifdef FEAT_DIFF
693 && curwin->w_topfill == curwin->w_old_topfill
694 && curwin->w_botfill == curwin->w_old_botfill
695#endif
696 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000698 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
700 && curwin->w_old_visual_mode == VIsual_mode
701 && (curwin->w_valid & VALID_VIRTCOL)
702 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 ))
704 curwin->w_redr_type = type;
705
Bram Moolenaar5a305422006-04-28 22:38:25 +0000706 /* Redraw the tab pages line if needed. */
707 if (redraw_tabline || type >= NOT_VALID)
708 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef FEAT_SYN_HL
711 /*
712 * Correct stored syntax highlighting info for changes in each displayed
713 * buffer. Each buffer must only be done once.
714 */
715 FOR_ALL_WINDOWS(wp)
716 {
717 if (wp->w_buffer->b_mod_set)
718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 win_T *wwp;
720
721 /* Check if we already did this buffer. */
722 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
723 if (wwp->w_buffer == wp->w_buffer)
724 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200725 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 syn_stack_apply_changes(wp->w_buffer);
727 }
728 }
729#endif
730
731 /*
732 * Go from top to bottom through the windows, redrawing the ones that need
733 * it.
734 */
735#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
736 did_one = FALSE;
737#endif
738#ifdef FEAT_SEARCH_EXTRA
739 search_hl.rm.regprog = NULL;
740#endif
741 FOR_ALL_WINDOWS(wp)
742 {
743 if (wp->w_redr_type != 0)
744 {
745 cursor_off();
746#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
747 if (!did_one)
748 {
749 did_one = TRUE;
750# ifdef FEAT_SEARCH_EXTRA
751 start_search_hl();
752# endif
753# ifdef FEAT_CLIPBOARD
754 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200755 if (clip_star.available && clip_isautosel_star())
756 clip_update_selection(&clip_star);
757 if (clip_plus.available && clip_isautosel_plus())
758 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759# endif
760#ifdef FEAT_GUI
761 /* Remove the cursor before starting to do anything, because
762 * scrolling may make it difficult to redraw the text under
763 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200764 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200765 {
766 gui_cursor_col = gui.cursor_col;
767 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200769 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
772 }
773#endif
774 win_update(wp);
775 }
776
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 /* redraw status line after the window to minimize cursor movement */
778 if (wp->w_redr_status)
779 {
780 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200781 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784#if defined(FEAT_SEARCH_EXTRA)
785 end_search_hl();
786#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100787#ifdef FEAT_INS_EXPAND
788 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200789 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 /* Reset b_mod_set flags. Going through all windows is probably faster
793 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200794 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200797 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
799 /* Clear or redraw the command line. Done last, because scrolling may
800 * mess up the command line. */
801 if (clear_cmdline || redraw_cmdline)
802 showmode();
803
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200804 if (no_update)
805 --no_win_do_lines_ins;
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200808 if (!did_intro)
809 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 did_intro = TRUE;
811
812#ifdef FEAT_GUI
813 /* Redraw the cursor and update the scrollbars when all screen updating is
814 * done. */
815 if (gui.in_use)
816 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200817 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200818 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100819 mch_disable_flush();
820 out_flush(); /* required before updating the cursor */
821 mch_enable_flush();
822
Bram Moolenaar144445d2016-07-08 21:41:54 +0200823 /* Put the GUI position where the cursor was, gui_update_cursor()
824 * uses that. */
825 gui.col = gui_cursor_col;
826 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200827# ifdef FEAT_MBYTE
828 gui.col = mb_fix_col(gui.col, gui.row);
829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100831 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200832 screen_cur_col = gui.col;
833 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200834 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 else
836 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 gui_update_scrollbars(FALSE);
838 }
839#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200840 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841}
842
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100843#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
844/*
845 * Prepare for updating one or more windows.
846 * Caller must check for "updating_screen" already set to avoid recursiveness.
847 */
848 static void
849update_prepare(void)
850{
851 cursor_off();
852 updating_screen = TRUE;
853#ifdef FEAT_GUI
854 /* Remove the cursor before starting to do anything, because scrolling may
855 * make it difficult to redraw the text under it. */
856 if (gui.in_use)
857 gui_undraw_cursor();
858#endif
859#ifdef FEAT_SEARCH_EXTRA
860 start_search_hl();
861#endif
862}
863
864/*
865 * Finish updating one or more windows.
866 */
867 static void
868update_finish(void)
869{
870 if (redraw_cmdline)
871 showmode();
872
873# ifdef FEAT_SEARCH_EXTRA
874 end_search_hl();
875# endif
876
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200877 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100878
879# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880 /* Redraw the cursor and update the scrollbars when all screen updating is
881 * done. */
882 if (gui.in_use)
883 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100884 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100885 gui_update_scrollbars(FALSE);
886 }
887# endif
888}
889#endif
890
Bram Moolenaar860cae12010-06-05 23:22:07 +0200891#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200892/*
893 * Return TRUE if the cursor line in window "wp" may be concealed, according
894 * to the 'concealcursor' option.
895 */
896 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100897conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200898{
899 int c;
900
901 if (*wp->w_p_cocu == NUL)
902 return FALSE;
903 if (get_real_state() & VISUAL)
904 c = 'v';
905 else if (State & INSERT)
906 c = 'i';
907 else if (State & NORMAL)
908 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200909 else if (State & CMDLINE)
910 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200911 else
912 return FALSE;
913 return vim_strchr(wp->w_p_cocu, c) != NULL;
914}
915
916/*
917 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
918 */
919 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200920conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200921{
922 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
923 {
924 need_cursor_line_redraw = TRUE;
925 /* Need to recompute cursor column, e.g., when starting Visual mode
926 * without concealing. */
927 curs_columns(TRUE);
928 }
929}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#endif
931
932#if defined(FEAT_SIGNS) || defined(PROTO)
933 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 win_T *wp;
937 int doit = FALSE;
938
939# ifdef FEAT_FOLDING
940 win_foldinfo.fi_level = 0;
941# endif
942
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100943 // update/delete a specific sign
944 redraw_buf_line_later(buf, lnum);
945
946 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (wp->w_redr_type != 0)
949 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100951 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200952 * happening (recursive call), messages on the screen or still starting up.
953 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100954 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200955 || State == ASKMORE || State == HITRETURN
956 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100957#ifdef FEAT_GUI
958 || gui.starting
959#endif
960 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 return;
962
963 /* update all windows that need updating */
964 update_prepare();
965
Bram Moolenaar29323592016-07-24 22:04:11 +0200966 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
968 if (wp->w_redr_type != 0)
969 win_update(wp);
970 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200971 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974 update_finish();
975}
976#endif
977
978
979#if defined(FEAT_GUI) || defined(PROTO)
980/*
981 * Update a single window, its status line and maybe the command line msg.
982 * Used for the GUI scrollbar.
983 */
984 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100985updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000987 /* return if already busy updating */
988 if (updating_screen)
989 return;
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 update_prepare();
992
993#ifdef FEAT_CLIPBOARD
994 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200995 if (clip_star.available && clip_isautosel_star())
996 clip_update_selection(&clip_star);
997 if (clip_plus.available && clip_isautosel_plus())
998 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001002
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001003 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001004 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001005 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (wp->w_redr_status
1008# ifdef FEAT_CMDL_INFO
1009 || p_ru
1010# endif
1011# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001012 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013# endif
1014 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001015 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 update_finish();
1018}
1019#endif
1020
1021/*
1022 * Update a single window.
1023 *
1024 * This may cause the windows below it also to be redrawn (when clearing the
1025 * screen or scrolling lines).
1026 *
1027 * How the window is redrawn depends on wp->w_redr_type. Each type also
1028 * implies the one below it.
1029 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001030 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1032 * INVERTED redraw the changed part of the Visual area
1033 * INVERTED_ALL redraw the whole Visual area
1034 * VALID 1. scroll up/down to adjust for a changed w_topline
1035 * 2. update lines at the top when scrolled down
1036 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001037 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 * b_mod_top and b_mod_bot.
1039 * - if wp->w_redraw_top non-zero, redraw lines between
1040 * wp->w_redraw_top and wp->w_redr_bot.
1041 * - continue redrawing when syntax status is invalid.
1042 * 4. if scrolled up, update lines at the bottom.
1043 * This results in three areas that may need updating:
1044 * top: from first row to top_end (when scrolled down)
1045 * mid: from mid_start to mid_end (update inversion or changed text)
1046 * bot: from bot_start to last row (when scrolled up)
1047 */
1048 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001049win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
1051 buf_T *buf = wp->w_buffer;
1052 int type;
1053 int top_end = 0; /* Below last row of the top area that needs
1054 updating. 0 when no top area updating. */
1055 int mid_start = 999;/* first row of the mid area that needs
1056 updating. 999 when no mid area updating. */
1057 int mid_end = 0; /* Below last row of the mid area that needs
1058 updating. 0 when no mid area updating. */
1059 int bot_start = 999;/* first row of the bot area that needs
1060 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 int scrolled_down = FALSE; /* TRUE when scrolled down when
1062 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001064 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 int top_to_mod = FALSE; /* redraw above mod_top */
1066#endif
1067
1068 int row; /* current window row to display */
1069 linenr_T lnum; /* current buffer lnum to display */
1070 int idx; /* current index in w_lines[] */
1071 int srow; /* starting row of the current line */
1072
1073 int eof = FALSE; /* if TRUE, we hit the end of the file */
1074 int didline = FALSE; /* if TRUE, we finished the last line */
1075 int i;
1076 long j;
1077 static int recursive = FALSE; /* being called recursively */
1078 int old_botline = wp->w_botline;
1079#ifdef FEAT_FOLDING
1080 long fold_count;
1081#endif
1082#ifdef FEAT_SYN_HL
1083 /* remember what happened to the previous line, to know if
1084 * check_visual_highlight() can be used */
1085#define DID_NONE 1 /* didn't update a line */
1086#define DID_LINE 2 /* updated a normal line */
1087#define DID_FOLD 3 /* updated a folded line */
1088 int did_update = DID_NONE;
1089 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1090#endif
1091 linenr_T mod_top = 0;
1092 linenr_T mod_bot = 0;
1093#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1094 int save_got_int;
1095#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001096#ifdef SYN_TIME_LIMIT
1097 proftime_T syntax_tm;
1098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
1100 type = wp->w_redr_type;
1101
1102 if (type == NOT_VALID)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 wp->w_lines_valid = 0;
1106 }
1107
1108 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001109 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 wp->w_redr_type = 0;
1112 return;
1113 }
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 /* Window is zero-width: Only need to draw the separator. */
1116 if (wp->w_width == 0)
1117 {
1118 /* draw the vertical separator right of this window */
1119 draw_vsep_win(wp, 0);
1120 wp->w_redr_type = 0;
1121 return;
1122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001124#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001125 // If this window contains a terminal, redraw works completely differently.
1126 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001127 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001128 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001129# ifdef FEAT_MENU
1130 /* Draw the window toolbar, if there is one. */
1131 if (winbar_height(wp) > 0)
1132 redraw_win_toolbar(wp);
1133# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001134 wp->w_redr_type = 0;
1135 return;
1136 }
1137#endif
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001140 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#endif
1142
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001143#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001144 /* Force redraw when width of 'number' or 'relativenumber' column
1145 * changes. */
1146 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001147 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001148 {
1149 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001150 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001151 }
1152 else
1153#endif
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1156 {
1157 /*
1158 * When there are both inserted/deleted lines and specific lines to be
1159 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1160 * everything (only happens when redrawing is off for while).
1161 */
1162 type = NOT_VALID;
1163 }
1164 else
1165 {
1166 /*
1167 * Set mod_top to the first line that needs displaying because of
1168 * changes. Set mod_bot to the first line after the changes.
1169 */
1170 mod_top = wp->w_redraw_top;
1171 if (wp->w_redraw_bot != 0)
1172 mod_bot = wp->w_redraw_bot + 1;
1173 else
1174 mod_bot = 0;
1175 wp->w_redraw_top = 0; /* reset for next time */
1176 wp->w_redraw_bot = 0;
1177 if (buf->b_mod_set)
1178 {
1179 if (mod_top == 0 || mod_top > buf->b_mod_top)
1180 {
1181 mod_top = buf->b_mod_top;
1182#ifdef FEAT_SYN_HL
1183 /* Need to redraw lines above the change that may be included
1184 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001185 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001187 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (mod_top < 1)
1189 mod_top = 1;
1190 }
1191#endif
1192 }
1193 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1194 mod_bot = buf->b_mod_bot;
1195
1196#ifdef FEAT_SEARCH_EXTRA
1197 /* When 'hlsearch' is on and using a multi-line search pattern, a
1198 * change in one line may make the Search highlighting in a
1199 * previous line invalid. Simple solution: redraw all visible
1200 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001201 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001203 if (search_hl.rm.regprog != NULL
1204 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001206 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001207 {
1208 cur = wp->w_match_head;
1209 while (cur != NULL)
1210 {
1211 if (cur->match.regprog != NULL
1212 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001213 {
1214 top_to_mod = TRUE;
1215 break;
1216 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001217 cur = cur->next;
1218 }
1219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#endif
1221 }
1222#ifdef FEAT_FOLDING
1223 if (mod_top != 0 && hasAnyFolding(wp))
1224 {
1225 linenr_T lnumt, lnumb;
1226
1227 /*
1228 * A change in a line can cause lines above it to become folded or
1229 * unfolded. Find the top most buffer line that may be affected.
1230 * If the line was previously folded and displayed, get the first
1231 * line of that fold. If the line is folded now, get the first
1232 * folded line. Use the minimum of these two.
1233 */
1234
1235 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1236 * the line below it. If there is no valid entry, use w_topline.
1237 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1238 * to this line. If there is no valid entry, use MAXLNUM. */
1239 lnumt = wp->w_topline;
1240 lnumb = MAXLNUM;
1241 for (i = 0; i < wp->w_lines_valid; ++i)
1242 if (wp->w_lines[i].wl_valid)
1243 {
1244 if (wp->w_lines[i].wl_lastlnum < mod_top)
1245 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1246 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1247 {
1248 lnumb = wp->w_lines[i].wl_lnum;
1249 /* When there is a fold column it might need updating
1250 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001251 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 ++lnumb;
1253 }
1254 }
1255
1256 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1257 if (mod_top > lnumt)
1258 mod_top = lnumt;
1259
1260 /* Now do the same for the bottom line (one above mod_bot). */
1261 --mod_bot;
1262 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1263 ++mod_bot;
1264 if (mod_bot < lnumb)
1265 mod_bot = lnumb;
1266 }
1267#endif
1268
1269 /* When a change starts above w_topline and the end is below
1270 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001271 * If the end of the change is above w_topline: do like no change was
1272 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (mod_top != 0 && mod_top < wp->w_topline)
1274 {
1275 if (mod_bot > wp->w_topline)
1276 mod_top = wp->w_topline;
1277#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001278 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 top_end = 1;
1280#endif
1281 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001282
1283 /* When line numbers are displayed need to redraw all lines below
1284 * inserted/deleted lines. */
1285 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1286 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288
1289 /*
1290 * When only displaying the lines at the top, set top_end. Used when
1291 * window has scrolled down for msg_scrolled.
1292 */
1293 if (type == REDRAW_TOP)
1294 {
1295 j = 0;
1296 for (i = 0; i < wp->w_lines_valid; ++i)
1297 {
1298 j += wp->w_lines[i].wl_size;
1299 if (j >= wp->w_upd_rows)
1300 {
1301 top_end = j;
1302 break;
1303 }
1304 }
1305 if (top_end == 0)
1306 /* not found (cannot happen?): redraw everything */
1307 type = NOT_VALID;
1308 else
1309 /* top area defined, the rest is VALID */
1310 type = VALID;
1311 }
1312
Bram Moolenaar367329b2007-08-30 11:53:22 +00001313 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001314 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1315 * non-zero and thus not FALSE) will indicate that screenclear() was not
1316 * called. */
1317 if (screen_cleared)
1318 screen_cleared = MAYBE;
1319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 /*
1321 * If there are no changes on the screen that require a complete redraw,
1322 * handle three cases:
1323 * 1: we are off the top of the screen by a few lines: scroll down
1324 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1325 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1326 * w_lines[] that needs updating.
1327 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001328 if ((type == VALID || type == SOME_VALID
1329 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#ifdef FEAT_DIFF
1331 && !wp->w_botfill && !wp->w_old_botfill
1332#endif
1333 )
1334 {
1335 if (mod_top != 0 && wp->w_topline == mod_top)
1336 {
1337 /*
1338 * w_topline is the first changed line, the scrolling will be done
1339 * further down.
1340 */
1341 }
1342 else if (wp->w_lines[0].wl_valid
1343 && (wp->w_topline < wp->w_lines[0].wl_lnum
1344#ifdef FEAT_DIFF
1345 || (wp->w_topline == wp->w_lines[0].wl_lnum
1346 && wp->w_topfill > wp->w_old_topfill)
1347#endif
1348 ))
1349 {
1350 /*
1351 * New topline is above old topline: May scroll down.
1352 */
1353#ifdef FEAT_FOLDING
1354 if (hasAnyFolding(wp))
1355 {
1356 linenr_T ln;
1357
1358 /* count the number of lines we are off, counting a sequence
1359 * of folded lines as one */
1360 j = 0;
1361 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1362 {
1363 ++j;
1364 if (j >= wp->w_height - 2)
1365 break;
1366 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1367 }
1368 }
1369 else
1370#endif
1371 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1372 if (j < wp->w_height - 2) /* not too far off */
1373 {
1374 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1375#ifdef FEAT_DIFF
1376 /* insert extra lines for previously invisible filler lines */
1377 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1378 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1379 - wp->w_old_topfill;
1380#endif
1381 if (i < wp->w_height - 2) /* less than a screen off */
1382 {
1383 /*
1384 * Try to insert the correct number of lines.
1385 * If not the last window, delete the lines at the bottom.
1386 * win_ins_lines may fail when the terminal can't do it.
1387 */
1388 if (i > 0)
1389 check_for_delay(FALSE);
1390 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1391 {
1392 if (wp->w_lines_valid != 0)
1393 {
1394 /* Need to update rows that are new, stop at the
1395 * first one that scrolled down. */
1396 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
1399 /* Move the entries that were scrolled, disable
1400 * the entries for the lines to be redrawn. */
1401 if ((wp->w_lines_valid += j) > wp->w_height)
1402 wp->w_lines_valid = wp->w_height;
1403 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1404 wp->w_lines[idx] = wp->w_lines[idx - j];
1405 while (idx >= 0)
1406 wp->w_lines[idx--].wl_valid = FALSE;
1407 }
1408 }
1409 else
1410 mid_start = 0; /* redraw all lines */
1411 }
1412 else
1413 mid_start = 0; /* redraw all lines */
1414 }
1415 else
1416 mid_start = 0; /* redraw all lines */
1417 }
1418 else
1419 {
1420 /*
1421 * New topline is at or below old topline: May scroll up.
1422 * When topline didn't change, find first entry in w_lines[] that
1423 * needs updating.
1424 */
1425
1426 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1427 j = -1;
1428 row = 0;
1429 for (i = 0; i < wp->w_lines_valid; i++)
1430 {
1431 if (wp->w_lines[i].wl_valid
1432 && wp->w_lines[i].wl_lnum == wp->w_topline)
1433 {
1434 j = i;
1435 break;
1436 }
1437 row += wp->w_lines[i].wl_size;
1438 }
1439 if (j == -1)
1440 {
1441 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1442 * lines */
1443 mid_start = 0;
1444 }
1445 else
1446 {
1447 /*
1448 * Try to delete the correct number of lines.
1449 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1450 */
1451#ifdef FEAT_DIFF
1452 /* If the topline didn't change, delete old filler lines,
1453 * otherwise delete filler lines of the new topline... */
1454 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1455 row += wp->w_old_topfill;
1456 else
1457 row += diff_check_fill(wp, wp->w_topline);
1458 /* ... but don't delete new filler lines. */
1459 row -= wp->w_topfill;
1460#endif
1461 if (row > 0)
1462 {
1463 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001464 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1465 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 bot_start = wp->w_height - row;
1467 else
1468 mid_start = 0; /* redraw all lines */
1469 }
1470 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1471 {
1472 /*
1473 * Skip the lines (below the deleted lines) that are still
1474 * valid and don't need redrawing. Copy their info
1475 * upwards, to compensate for the deleted lines. Set
1476 * bot_start to the first row that needs redrawing.
1477 */
1478 bot_start = 0;
1479 idx = 0;
1480 for (;;)
1481 {
1482 wp->w_lines[idx] = wp->w_lines[j];
1483 /* stop at line that didn't fit, unless it is still
1484 * valid (no lines deleted) */
1485 if (row > 0 && bot_start + row
1486 + (int)wp->w_lines[j].wl_size > wp->w_height)
1487 {
1488 wp->w_lines_valid = idx + 1;
1489 break;
1490 }
1491 bot_start += wp->w_lines[idx++].wl_size;
1492
1493 /* stop at the last valid entry in w_lines[].wl_size */
1494 if (++j >= wp->w_lines_valid)
1495 {
1496 wp->w_lines_valid = idx;
1497 break;
1498 }
1499 }
1500#ifdef FEAT_DIFF
1501 /* Correct the first entry for filler lines at the top
1502 * when it won't get updated below. */
1503 if (wp->w_p_diff && bot_start > 0)
1504 wp->w_lines[0].wl_size =
1505 plines_win_nofill(wp, wp->w_topline, TRUE)
1506 + wp->w_topfill;
1507#endif
1508 }
1509 }
1510 }
1511
1512 /* When starting redraw in the first line, redraw all lines. When
1513 * there is only one window it's probably faster to clear the screen
1514 * first. */
1515 if (mid_start == 0)
1516 {
1517 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001518 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001519 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001520 /* Clear the screen when it was not done by win_del_lines() or
1521 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1522 * then. */
1523 if (screen_cleared != TRUE)
1524 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001525 /* The screen was cleared, redraw the tab pages line. */
1526 if (redraw_tabline)
1527 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001530
1531 /* When win_del_lines() or win_ins_lines() caused the screen to be
1532 * cleared (only happens for the first window) or when screenclear()
1533 * was called directly above, "must_redraw" will have been set to
1534 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1535 if (screen_cleared == TRUE)
1536 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 else
1539 {
1540 /* Not VALID or INVERTED: redraw all lines. */
1541 mid_start = 0;
1542 mid_end = wp->w_height;
1543 }
1544
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001545 if (type == SOME_VALID)
1546 {
1547 /* SOME_VALID: redraw all lines. */
1548 mid_start = 0;
1549 mid_end = wp->w_height;
1550 type = NOT_VALID;
1551 }
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 /* check if we are updating or removing the inverted part */
1554 if ((VIsual_active && buf == curwin->w_buffer)
1555 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1556 {
1557 linenr_T from, to;
1558
1559 if (VIsual_active)
1560 {
1561 if (VIsual_active
1562 && (VIsual_mode != wp->w_old_visual_mode
1563 || type == INVERTED_ALL))
1564 {
1565 /*
1566 * If the type of Visual selection changed, redraw the whole
1567 * selection. Also when the ownership of the X selection is
1568 * gained or lost.
1569 */
1570 if (curwin->w_cursor.lnum < VIsual.lnum)
1571 {
1572 from = curwin->w_cursor.lnum;
1573 to = VIsual.lnum;
1574 }
1575 else
1576 {
1577 from = VIsual.lnum;
1578 to = curwin->w_cursor.lnum;
1579 }
1580 /* redraw more when the cursor moved as well */
1581 if (wp->w_old_cursor_lnum < from)
1582 from = wp->w_old_cursor_lnum;
1583 if (wp->w_old_cursor_lnum > to)
1584 to = wp->w_old_cursor_lnum;
1585 if (wp->w_old_visual_lnum < from)
1586 from = wp->w_old_visual_lnum;
1587 if (wp->w_old_visual_lnum > to)
1588 to = wp->w_old_visual_lnum;
1589 }
1590 else
1591 {
1592 /*
1593 * Find the line numbers that need to be updated: The lines
1594 * between the old cursor position and the current cursor
1595 * position. Also check if the Visual position changed.
1596 */
1597 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1598 {
1599 from = curwin->w_cursor.lnum;
1600 to = wp->w_old_cursor_lnum;
1601 }
1602 else
1603 {
1604 from = wp->w_old_cursor_lnum;
1605 to = curwin->w_cursor.lnum;
1606 if (from == 0) /* Visual mode just started */
1607 from = to;
1608 }
1609
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001610 if (VIsual.lnum != wp->w_old_visual_lnum
1611 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 if (wp->w_old_visual_lnum < from
1614 && wp->w_old_visual_lnum != 0)
1615 from = wp->w_old_visual_lnum;
1616 if (wp->w_old_visual_lnum > to)
1617 to = wp->w_old_visual_lnum;
1618 if (VIsual.lnum < from)
1619 from = VIsual.lnum;
1620 if (VIsual.lnum > to)
1621 to = VIsual.lnum;
1622 }
1623 }
1624
1625 /*
1626 * If in block mode and changed column or curwin->w_curswant:
1627 * update all lines.
1628 * First compute the actual start and end column.
1629 */
1630 if (VIsual_mode == Ctrl_V)
1631 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001632 colnr_T fromc, toc;
1633#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1634 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaar404406a2014-10-09 13:24:43 +02001636 if (curwin->w_p_lbr)
1637 ve_flags = VE_ALL;
1638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001640#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1641 ve_flags = save_ve_flags;
1642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 ++toc;
1644 if (curwin->w_curswant == MAXCOL)
1645 toc = MAXCOL;
1646
1647 if (fromc != wp->w_old_cursor_fcol
1648 || toc != wp->w_old_cursor_lcol)
1649 {
1650 if (from > VIsual.lnum)
1651 from = VIsual.lnum;
1652 if (to < VIsual.lnum)
1653 to = VIsual.lnum;
1654 }
1655 wp->w_old_cursor_fcol = fromc;
1656 wp->w_old_cursor_lcol = toc;
1657 }
1658 }
1659 else
1660 {
1661 /* Use the line numbers of the old Visual area. */
1662 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1663 {
1664 from = wp->w_old_cursor_lnum;
1665 to = wp->w_old_visual_lnum;
1666 }
1667 else
1668 {
1669 from = wp->w_old_visual_lnum;
1670 to = wp->w_old_cursor_lnum;
1671 }
1672 }
1673
1674 /*
1675 * There is no need to update lines above the top of the window.
1676 */
1677 if (from < wp->w_topline)
1678 from = wp->w_topline;
1679
1680 /*
1681 * If we know the value of w_botline, use it to restrict the update to
1682 * the lines that are visible in the window.
1683 */
1684 if (wp->w_valid & VALID_BOTLINE)
1685 {
1686 if (from >= wp->w_botline)
1687 from = wp->w_botline - 1;
1688 if (to >= wp->w_botline)
1689 to = wp->w_botline - 1;
1690 }
1691
1692 /*
1693 * Find the minimal part to be updated.
1694 * Watch out for scrolling that made entries in w_lines[] invalid.
1695 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1696 * top_end; need to redraw from top_end to the "to" line.
1697 * A middle mouse click with a Visual selection may change the text
1698 * above the Visual area and reset wl_valid, do count these for
1699 * mid_end (in srow).
1700 */
1701 if (mid_start > 0)
1702 {
1703 lnum = wp->w_topline;
1704 idx = 0;
1705 srow = 0;
1706 if (scrolled_down)
1707 mid_start = top_end;
1708 else
1709 mid_start = 0;
1710 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1711 {
1712 if (wp->w_lines[idx].wl_valid)
1713 mid_start += wp->w_lines[idx].wl_size;
1714 else if (!scrolled_down)
1715 srow += wp->w_lines[idx].wl_size;
1716 ++idx;
1717# ifdef FEAT_FOLDING
1718 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1719 lnum = wp->w_lines[idx].wl_lnum;
1720 else
1721# endif
1722 ++lnum;
1723 }
1724 srow += mid_start;
1725 mid_end = wp->w_height;
1726 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1727 {
1728 if (wp->w_lines[idx].wl_valid
1729 && wp->w_lines[idx].wl_lnum >= to + 1)
1730 {
1731 /* Only update until first row of this line */
1732 mid_end = srow;
1733 break;
1734 }
1735 srow += wp->w_lines[idx].wl_size;
1736 }
1737 }
1738 }
1739
1740 if (VIsual_active && buf == curwin->w_buffer)
1741 {
1742 wp->w_old_visual_mode = VIsual_mode;
1743 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1744 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001745 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 wp->w_old_curswant = curwin->w_curswant;
1747 }
1748 else
1749 {
1750 wp->w_old_visual_mode = 0;
1751 wp->w_old_cursor_lnum = 0;
1752 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001753 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1757 /* reset got_int, otherwise regexp won't work */
1758 save_got_int = got_int;
1759 got_int = 0;
1760#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001761#ifdef SYN_TIME_LIMIT
1762 /* Set the time limit to 'redrawtime'. */
1763 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001764 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_FOLDING
1767 win_foldinfo.fi_level = 0;
1768#endif
1769
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001770#ifdef FEAT_MENU
1771 /*
1772 * Draw the window toolbar, if there is one.
1773 * TODO: only when needed.
1774 */
1775 if (winbar_height(wp) > 0)
1776 redraw_win_toolbar(wp);
1777#endif
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 /*
1780 * Update all the window rows.
1781 */
1782 idx = 0; /* first entry in w_lines[].wl_size */
1783 row = 0;
1784 srow = 0;
1785 lnum = wp->w_topline; /* first line shown in window */
1786 for (;;)
1787 {
1788 /* stop updating when reached the end of the window (check for _past_
1789 * the end of the window is at the end of the loop) */
1790 if (row == wp->w_height)
1791 {
1792 didline = TRUE;
1793 break;
1794 }
1795
1796 /* stop updating when hit the end of the file */
1797 if (lnum > buf->b_ml.ml_line_count)
1798 {
1799 eof = TRUE;
1800 break;
1801 }
1802
1803 /* Remember the starting row of the line that is going to be dealt
1804 * with. It is used further down when the line doesn't fit. */
1805 srow = row;
1806
1807 /*
1808 * Update a line when it is in an area that needs updating, when it
1809 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001810 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 * win_del_lines(), check if the current line includes it.
1812 * When syntax folding is being used, the saved syntax states will
1813 * already have been updated, we can't see where the syntax state is
1814 * the same again, just update until the end of the window.
1815 */
1816 if (row < top_end
1817 || (row >= mid_start && row < mid_end)
1818#ifdef FEAT_SEARCH_EXTRA
1819 || top_to_mod
1820#endif
1821 || idx >= wp->w_lines_valid
1822 || (row + wp->w_lines[idx].wl_size > bot_start)
1823 || (mod_top != 0
1824 && (lnum == mod_top
1825 || (lnum >= mod_top
1826 && (lnum < mod_bot
1827#ifdef FEAT_SYN_HL
1828 || did_update == DID_FOLD
1829 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001830 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 && (
1832# ifdef FEAT_FOLDING
1833 (foldmethodIsSyntax(wp)
1834 && hasAnyFolding(wp)) ||
1835# endif
1836 syntax_check_changed(lnum)))
1837#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001838#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001839 /* match in fixed position might need redraw
1840 * if lines were inserted or deleted */
1841 || (wp->w_match_head != NULL
1842 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 )))))
1845 {
1846#ifdef FEAT_SEARCH_EXTRA
1847 if (lnum == mod_top)
1848 top_to_mod = FALSE;
1849#endif
1850
1851 /*
1852 * When at start of changed lines: May scroll following lines
1853 * up or down to minimize redrawing.
1854 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001855 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 */
1857 if (lnum == mod_top
1858 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001859 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
1861 int old_rows = 0;
1862 int new_rows = 0;
1863 int xtra_rows;
1864 linenr_T l;
1865
1866 /* Count the old number of window rows, using w_lines[], which
1867 * should still contain the sizes for the lines as they are
1868 * currently displayed. */
1869 for (i = idx; i < wp->w_lines_valid; ++i)
1870 {
1871 /* Only valid lines have a meaningful wl_lnum. Invalid
1872 * lines are part of the changed area. */
1873 if (wp->w_lines[i].wl_valid
1874 && wp->w_lines[i].wl_lnum == mod_bot)
1875 break;
1876 old_rows += wp->w_lines[i].wl_size;
1877#ifdef FEAT_FOLDING
1878 if (wp->w_lines[i].wl_valid
1879 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1880 {
1881 /* Must have found the last valid entry above mod_bot.
1882 * Add following invalid entries. */
1883 ++i;
1884 while (i < wp->w_lines_valid
1885 && !wp->w_lines[i].wl_valid)
1886 old_rows += wp->w_lines[i++].wl_size;
1887 break;
1888 }
1889#endif
1890 }
1891
1892 if (i >= wp->w_lines_valid)
1893 {
1894 /* We can't find a valid line below the changed lines,
1895 * need to redraw until the end of the window.
1896 * Inserting/deleting lines has no use. */
1897 bot_start = 0;
1898 }
1899 else
1900 {
1901 /* Able to count old number of rows: Count new window
1902 * rows, and may insert/delete lines */
1903 j = idx;
1904 for (l = lnum; l < mod_bot; ++l)
1905 {
1906#ifdef FEAT_FOLDING
1907 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1908 ++new_rows;
1909 else
1910#endif
1911#ifdef FEAT_DIFF
1912 if (l == wp->w_topline)
1913 new_rows += plines_win_nofill(wp, l, TRUE)
1914 + wp->w_topfill;
1915 else
1916#endif
1917 new_rows += plines_win(wp, l, TRUE);
1918 ++j;
1919 if (new_rows > wp->w_height - row - 2)
1920 {
1921 /* it's getting too much, must redraw the rest */
1922 new_rows = 9999;
1923 break;
1924 }
1925 }
1926 xtra_rows = new_rows - old_rows;
1927 if (xtra_rows < 0)
1928 {
1929 /* May scroll text up. If there is not enough
1930 * remaining text or scrolling fails, must redraw the
1931 * rest. If scrolling works, must redraw the text
1932 * below the scrolled text. */
1933 if (row - xtra_rows >= wp->w_height - 2)
1934 mod_bot = MAXLNUM;
1935 else
1936 {
1937 check_for_delay(FALSE);
1938 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001939 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 mod_bot = MAXLNUM;
1941 else
1942 bot_start = wp->w_height + xtra_rows;
1943 }
1944 }
1945 else if (xtra_rows > 0)
1946 {
1947 /* May scroll text down. If there is not enough
1948 * remaining text of scrolling fails, must redraw the
1949 * rest. */
1950 if (row + xtra_rows >= wp->w_height - 2)
1951 mod_bot = MAXLNUM;
1952 else
1953 {
1954 check_for_delay(FALSE);
1955 if (win_ins_lines(wp, row + old_rows,
1956 xtra_rows, FALSE, FALSE) == FAIL)
1957 mod_bot = MAXLNUM;
1958 else if (top_end > row + old_rows)
1959 /* Scrolled the part at the top that requires
1960 * updating down. */
1961 top_end += xtra_rows;
1962 }
1963 }
1964
1965 /* When not updating the rest, may need to move w_lines[]
1966 * entries. */
1967 if (mod_bot != MAXLNUM && i != j)
1968 {
1969 if (j < i)
1970 {
1971 int x = row + new_rows;
1972
1973 /* move entries in w_lines[] upwards */
1974 for (;;)
1975 {
1976 /* stop at last valid entry in w_lines[] */
1977 if (i >= wp->w_lines_valid)
1978 {
1979 wp->w_lines_valid = j;
1980 break;
1981 }
1982 wp->w_lines[j] = wp->w_lines[i];
1983 /* stop at a line that won't fit */
1984 if (x + (int)wp->w_lines[j].wl_size
1985 > wp->w_height)
1986 {
1987 wp->w_lines_valid = j + 1;
1988 break;
1989 }
1990 x += wp->w_lines[j++].wl_size;
1991 ++i;
1992 }
1993 if (bot_start > x)
1994 bot_start = x;
1995 }
1996 else /* j > i */
1997 {
1998 /* move entries in w_lines[] downwards */
1999 j -= i;
2000 wp->w_lines_valid += j;
2001 if (wp->w_lines_valid > wp->w_height)
2002 wp->w_lines_valid = wp->w_height;
2003 for (i = wp->w_lines_valid; i - j >= idx; --i)
2004 wp->w_lines[i] = wp->w_lines[i - j];
2005
2006 /* The w_lines[] entries for inserted lines are
2007 * now invalid, but wl_size may be used above.
2008 * Reset to zero. */
2009 while (i >= idx)
2010 {
2011 wp->w_lines[i].wl_size = 0;
2012 wp->w_lines[i--].wl_valid = FALSE;
2013 }
2014 }
2015 }
2016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018
2019#ifdef FEAT_FOLDING
2020 /*
2021 * When lines are folded, display one line for all of them.
2022 * Otherwise, display normally (can be several display lines when
2023 * 'wrap' is on).
2024 */
2025 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2026 if (fold_count != 0)
2027 {
2028 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2029 ++row;
2030 --fold_count;
2031 wp->w_lines[idx].wl_folded = TRUE;
2032 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2033# ifdef FEAT_SYN_HL
2034 did_update = DID_FOLD;
2035# endif
2036 }
2037 else
2038#endif
2039 if (idx < wp->w_lines_valid
2040 && wp->w_lines[idx].wl_valid
2041 && wp->w_lines[idx].wl_lnum == lnum
2042 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002043 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 && srow + wp->w_lines[idx].wl_size > wp->w_height
2045#ifdef FEAT_DIFF
2046 && diff_check_fill(wp, lnum) == 0
2047#endif
2048 )
2049 {
2050 /* This line is not going to fit. Don't draw anything here,
2051 * will draw "@ " lines below. */
2052 row = wp->w_height + 1;
2053 }
2054 else
2055 {
2056#ifdef FEAT_SEARCH_EXTRA
2057 prepare_search_hl(wp, lnum);
2058#endif
2059#ifdef FEAT_SYN_HL
2060 /* Let the syntax stuff know we skipped a few lines. */
2061 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002062 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 syntax_end_parsing(syntax_last_parsed + 1);
2064#endif
2065
2066 /*
2067 * Display one line.
2068 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002069 row = win_line(wp, lnum, srow, wp->w_height,
2070 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072#ifdef FEAT_FOLDING
2073 wp->w_lines[idx].wl_folded = FALSE;
2074 wp->w_lines[idx].wl_lastlnum = lnum;
2075#endif
2076#ifdef FEAT_SYN_HL
2077 did_update = DID_LINE;
2078 syntax_last_parsed = lnum;
2079#endif
2080 }
2081
2082 wp->w_lines[idx].wl_lnum = lnum;
2083 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002084
2085 /* Past end of the window or end of the screen. Note that after
2086 * resizing wp->w_height may be end up too big. That's a problem
2087 * elsewhere, but prevent a crash here. */
2088 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
2090 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002091 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2093 ++idx;
2094 break;
2095 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002096 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 wp->w_lines[idx].wl_size = row - srow;
2098 ++idx;
2099#ifdef FEAT_FOLDING
2100 lnum += fold_count + 1;
2101#else
2102 ++lnum;
2103#endif
2104 }
2105 else
2106 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002107 if (wp->w_p_rnu)
2108 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002109#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002110 // 'relativenumber' set: The text doesn't need to be drawn, but
2111 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002112 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2113 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002114 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002115 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002116#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002117 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002118 }
2119
2120 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 row += wp->w_lines[idx++].wl_size;
2122 if (row > wp->w_height) /* past end of screen */
2123 break;
2124#ifdef FEAT_FOLDING
2125 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2126#else
2127 ++lnum;
2128#endif
2129#ifdef FEAT_SYN_HL
2130 did_update = DID_NONE;
2131#endif
2132 }
2133
2134 if (lnum > buf->b_ml.ml_line_count)
2135 {
2136 eof = TRUE;
2137 break;
2138 }
2139 }
2140 /*
2141 * End of loop over all window lines.
2142 */
2143
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002144#ifdef FEAT_VTP
2145 /* Rewrite the character at the end of the screen line. */
2146 if (use_vtp())
2147 {
2148 int i;
2149
2150 for (i = 0; i < Rows; ++i)
2151# ifdef FEAT_MBYTE
2152 if (enc_utf8)
2153 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2154 LineOffset[i] + screen_Columns) > 1)
2155 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2156 else
2157 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2158 else
2159# endif
2160 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2161 }
2162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
2164 if (idx > wp->w_lines_valid)
2165 wp->w_lines_valid = idx;
2166
2167#ifdef FEAT_SYN_HL
2168 /*
2169 * Let the syntax stuff know we stop parsing here.
2170 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002171 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 syntax_end_parsing(syntax_last_parsed + 1);
2173#endif
2174
2175 /*
2176 * If we didn't hit the end of the file, and we didn't finish the last
2177 * line we were working on, then the line didn't fit.
2178 */
2179 wp->w_empty_rows = 0;
2180#ifdef FEAT_DIFF
2181 wp->w_filler_rows = 0;
2182#endif
2183 if (!eof && !didline)
2184 {
2185 if (lnum == wp->w_topline)
2186 {
2187 /*
2188 * Single line that does not fit!
2189 * Don't overwrite it, it can be edited.
2190 */
2191 wp->w_botline = lnum + 1;
2192 }
2193#ifdef FEAT_DIFF
2194 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2195 {
2196 /* Window ends in filler lines. */
2197 wp->w_botline = lnum;
2198 wp->w_filler_rows = wp->w_height - srow;
2199 }
2200#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002201 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2202 {
2203 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2204
2205 /*
2206 * Last line isn't finished: Display "@@@" in the last screen line.
2207 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002208 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002209 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002210 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002211 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002212 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002213 set_empty_rows(wp, srow);
2214 wp->w_botline = lnum;
2215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2217 {
2218 /*
2219 * Last line isn't finished: Display "@@@" at the end.
2220 */
2221 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2222 W_WINROW(wp) + wp->w_height,
2223 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002224 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 set_empty_rows(wp, srow);
2226 wp->w_botline = lnum;
2227 }
2228 else
2229 {
2230 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2231 wp->w_botline = lnum;
2232 }
2233 }
2234 else
2235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (eof) /* we hit the end of the file */
2238 {
2239 wp->w_botline = buf->b_ml.ml_line_count + 1;
2240#ifdef FEAT_DIFF
2241 j = diff_check_fill(wp, wp->w_botline);
2242 if (j > 0 && !wp->w_botfill)
2243 {
2244 /*
2245 * Display filler lines at the end of the file
2246 */
2247 if (char2cells(fill_diff) > 1)
2248 i = '-';
2249 else
2250 i = fill_diff;
2251 if (row + j > wp->w_height)
2252 j = wp->w_height - row;
2253 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2254 row += j;
2255 }
2256#endif
2257 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002258 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 wp->w_botline = lnum;
2260
2261 /* make sure the rest of the screen is blank */
2262 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002263 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002266#ifdef SYN_TIME_LIMIT
2267 syn_set_timeout(NULL);
2268#endif
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 /* Reset the type of redrawing required, the window has been updated. */
2271 wp->w_redr_type = 0;
2272#ifdef FEAT_DIFF
2273 wp->w_old_topfill = wp->w_topfill;
2274 wp->w_old_botfill = wp->w_botfill;
2275#endif
2276
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002277 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 /*
2280 * There is a trick with w_botline. If we invalidate it on each
2281 * change that might modify it, this will cause a lot of expensive
2282 * calls to plines() in update_topline() each time. Therefore the
2283 * value of w_botline is often approximated, and this value is used to
2284 * compute the value of w_topline. If the value of w_botline was
2285 * wrong, check that the value of w_topline is correct (cursor is on
2286 * the visible part of the text). If it's not, we need to redraw
2287 * again. Mostly this just means scrolling up a few lines, so it
2288 * doesn't look too bad. Only do this for the current window (where
2289 * changes are relevant).
2290 */
2291 wp->w_valid |= VALID_BOTLINE;
2292 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2293 {
2294 recursive = TRUE;
2295 curwin->w_valid &= ~VALID_TOPLINE;
2296 update_topline(); /* may invalidate w_botline again */
2297 if (must_redraw != 0)
2298 {
2299 /* Don't update for changes in buffer again. */
2300 i = curbuf->b_mod_set;
2301 curbuf->b_mod_set = FALSE;
2302 win_update(curwin);
2303 must_redraw = 0;
2304 curbuf->b_mod_set = i;
2305 }
2306 recursive = FALSE;
2307 }
2308 }
2309
2310#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2311 /* restore got_int, unless CTRL-C was hit while redrawing */
2312 if (!got_int)
2313 got_int = save_got_int;
2314#endif
2315}
2316
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317/*
2318 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2319 * as the filler character.
2320 */
2321 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002322win_draw_end(
2323 win_T *wp,
2324 int c1,
2325 int c2,
2326 int row,
2327 int endrow,
2328 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2331 int n = 0;
2332# define FDC_OFF n
2333#else
2334# define FDC_OFF 0
2335#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002336#ifdef FEAT_FOLDING
2337 int fdc = compute_foldcolumn(wp, 0);
2338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
2340#ifdef FEAT_RIGHTLEFT
2341 if (wp->w_p_rl)
2342 {
2343 /* No check for cmdline window: should never be right-left. */
2344# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002345 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347 if (n > 0)
2348 {
2349 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002350 if (n > wp->w_width)
2351 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2353 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002354 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356# endif
2357# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002358 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 int nn = n + 2;
2361
2362 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002363 if (nn > wp->w_width)
2364 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2366 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002367 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 n = nn;
2369 }
2370# endif
2371 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002372 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002373 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2375 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002376 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378 else
2379#endif
2380 {
2381#ifdef FEAT_CMDWIN
2382 if (cmdwin_type != 0 && wp == curwin)
2383 {
2384 /* draw the cmdline character in the leftmost column */
2385 n = 1;
2386 if (n > wp->w_width)
2387 n = wp->w_width;
2388 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002389 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392#endif
2393#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002394 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002396 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002399 if (nn > wp->w_width)
2400 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002402 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002403 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 n = nn;
2405 }
2406#endif
2407#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002408 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 int nn = n + 2;
2411
2412 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002413 if (nn > wp->w_width)
2414 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002416 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002417 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 n = nn;
2419 }
2420#endif
2421 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002422 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002423 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425 set_empty_rows(wp, row);
2426}
2427
Bram Moolenaar1a384422010-07-14 19:53:30 +02002428#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002429/*
2430 * Advance **color_cols and return TRUE when there are columns to draw.
2431 */
2432 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002433advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002434{
2435 while (**color_cols >= 0 && vcol > **color_cols)
2436 ++*color_cols;
2437 return (**color_cols >= 0);
2438}
2439#endif
2440
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002441#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2442/*
2443 * Copy "text" to ScreenLines using "attr".
2444 * Returns the next screen column.
2445 */
2446 static int
2447text_to_screenline(win_T *wp, char_u *text, int col)
2448{
2449 int off = (int)(current_ScreenLine - ScreenLines);
2450
2451#ifdef FEAT_MBYTE
2452 if (has_mbyte)
2453 {
2454 int cells;
2455 int u8c, u8cc[MAX_MCO];
2456 int i;
2457 int idx;
2458 int c_len;
2459 char_u *p;
2460# ifdef FEAT_ARABIC
2461 int prev_c = 0; /* previous Arabic character */
2462 int prev_c1 = 0; /* first composing char for prev_c */
2463# endif
2464
2465# ifdef FEAT_RIGHTLEFT
2466 if (wp->w_p_rl)
2467 idx = off;
2468 else
2469# endif
2470 idx = off + col;
2471
2472 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2473 for (p = text; *p != NUL; )
2474 {
2475 cells = (*mb_ptr2cells)(p);
2476 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002477 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002478# ifdef FEAT_RIGHTLEFT
2479 - (wp->w_p_rl ? col : 0)
2480# endif
2481 )
2482 break;
2483 ScreenLines[idx] = *p;
2484 if (enc_utf8)
2485 {
2486 u8c = utfc_ptr2char(p, u8cc);
2487 if (*p < 0x80 && u8cc[0] == 0)
2488 {
2489 ScreenLinesUC[idx] = 0;
2490#ifdef FEAT_ARABIC
2491 prev_c = u8c;
2492#endif
2493 }
2494 else
2495 {
2496#ifdef FEAT_ARABIC
2497 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2498 {
2499 /* Do Arabic shaping. */
2500 int pc, pc1, nc;
2501 int pcc[MAX_MCO];
2502 int firstbyte = *p;
2503
2504 /* The idea of what is the previous and next
2505 * character depends on 'rightleft'. */
2506 if (wp->w_p_rl)
2507 {
2508 pc = prev_c;
2509 pc1 = prev_c1;
2510 nc = utf_ptr2char(p + c_len);
2511 prev_c1 = u8cc[0];
2512 }
2513 else
2514 {
2515 pc = utfc_ptr2char(p + c_len, pcc);
2516 nc = prev_c;
2517 pc1 = pcc[0];
2518 }
2519 prev_c = u8c;
2520
2521 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2522 pc, pc1, nc);
2523 ScreenLines[idx] = firstbyte;
2524 }
2525 else
2526 prev_c = u8c;
2527#endif
2528 /* Non-BMP character: display as ? or fullwidth ?. */
2529#ifdef UNICODE16
2530 if (u8c >= 0x10000)
2531 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2532 else
2533#endif
2534 ScreenLinesUC[idx] = u8c;
2535 for (i = 0; i < Screen_mco; ++i)
2536 {
2537 ScreenLinesC[i][idx] = u8cc[i];
2538 if (u8cc[i] == 0)
2539 break;
2540 }
2541 }
2542 if (cells > 1)
2543 ScreenLines[idx + 1] = 0;
2544 }
2545 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2546 /* double-byte single width character */
2547 ScreenLines2[idx] = p[1];
2548 else if (cells > 1)
2549 /* double-width character */
2550 ScreenLines[idx + 1] = p[1];
2551 col += cells;
2552 idx += cells;
2553 p += c_len;
2554 }
2555 }
2556 else
2557#endif
2558 {
2559 int len = (int)STRLEN(text);
2560
Bram Moolenaar02631462017-09-22 15:20:32 +02002561 if (len > wp->w_width - col)
2562 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002563 if (len > 0)
2564 {
2565#ifdef FEAT_RIGHTLEFT
2566 if (wp->w_p_rl)
2567 STRNCPY(current_ScreenLine, text, len);
2568 else
2569#endif
2570 STRNCPY(current_ScreenLine + col, text, len);
2571 col += len;
2572 }
2573 }
2574 return col;
2575}
2576#endif
2577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#ifdef FEAT_FOLDING
2579/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002580 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2581 * space is available for window "wp", minus "col".
2582 */
2583 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002584compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002585{
2586 int fdc = wp->w_p_fdc;
2587 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002588 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002589
2590 if (fdc > wwidth - (col + wmw))
2591 fdc = wwidth - (col + wmw);
2592 return fdc;
2593}
2594
2595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 * Display one folded line.
2597 */
2598 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002599fold_line(
2600 win_T *wp,
2601 long fold_count,
2602 foldinfo_T *foldinfo,
2603 linenr_T lnum,
2604 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002606 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 pos_T *top, *bot;
2608 linenr_T lnume = lnum + fold_count - 1;
2609 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002610 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 int col;
2613 int txtcol;
2614 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002615 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 /* Build the fold line:
2618 * 1. Add the cmdwin_type for the command-line window
2619 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002620 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 * 4. Compose the text
2622 * 5. Add the text
2623 * 6. set highlighting for the Visual area an other text
2624 */
2625 col = 0;
2626
2627 /*
2628 * 1. Add the cmdwin_type for the command-line window
2629 * Ignores 'rightleft', this window is never right-left.
2630 */
2631#ifdef FEAT_CMDWIN
2632 if (cmdwin_type != 0 && wp == curwin)
2633 {
2634 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002635 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#ifdef FEAT_MBYTE
2637 if (enc_utf8)
2638 ScreenLinesUC[off] = 0;
2639#endif
2640 ++col;
2641 }
2642#endif
2643
2644 /*
2645 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002646 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002648 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (fdc > 0)
2650 {
2651 fill_foldcolumn(buf, wp, TRUE, lnum);
2652#ifdef FEAT_RIGHTLEFT
2653 if (wp->w_p_rl)
2654 {
2655 int i;
2656
Bram Moolenaar02631462017-09-22 15:20:32 +02002657 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002658 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 /* reverse the fold column */
2660 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002661 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663 else
2664#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002665 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 col += fdc;
2667 }
2668
2669#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002670# define RL_MEMSET(p, v, l) \
2671 do { \
2672 if (wp->w_p_rl) \
2673 for (ri = 0; ri < l; ++ri) \
2674 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2675 else \
2676 for (ri = 0; ri < l; ++ri) \
2677 ScreenAttrs[off + (p) + ri] = v; \
2678 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002680# define RL_MEMSET(p, v, l) \
2681 do { \
2682 for (ri = 0; ri < l; ++ri) \
2683 ScreenAttrs[off + (p) + ri] = v; \
2684 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685#endif
2686
Bram Moolenaar64486672010-05-16 15:46:46 +02002687 /* Set all attributes of the 'number' or 'relativenumber' column and the
2688 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002689 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691#ifdef FEAT_SIGNS
2692 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002693 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002695 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (len > 0)
2697 {
2698 if (len > 2)
2699 len = 2;
2700# ifdef FEAT_RIGHTLEFT
2701 if (wp->w_p_rl)
2702 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002703 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002704 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else
2706# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002707 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 col += len;
2709 }
2710 }
2711#endif
2712
2713 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002714 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002716 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002718 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (len > 0)
2720 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002721 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002722 long num;
2723 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002724
2725 if (len > w + 1)
2726 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002727
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002728 if (wp->w_p_nu && !wp->w_p_rnu)
2729 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002730 num = (long)lnum;
2731 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002732 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002733 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002734 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002735 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002736 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002737 /* 'number' + 'relativenumber': cursor line shows absolute
2738 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002739 num = lnum;
2740 fmt = "%-*ld ";
2741 }
2742 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002743
Bram Moolenaar700e7342013-01-30 12:31:36 +01002744 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745#ifdef FEAT_RIGHTLEFT
2746 if (wp->w_p_rl)
2747 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002748 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002749 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 else
2751#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002752 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 col += len;
2754 }
2755 }
2756
2757 /*
2758 * 4. Compose the folded-line string with 'foldtext', if set.
2759 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002760 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 txtcol = col; /* remember where text starts */
2763
2764 /*
2765 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2766 * Right-left text is put in columns 0 - number-col, normal text is put
2767 * in columns number-col - window-width.
2768 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002769 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
2771 /* Fill the rest of the line with the fold filler */
2772#ifdef FEAT_RIGHTLEFT
2773 if (wp->w_p_rl)
2774 col -= txtcol;
2775#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002776 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#ifdef FEAT_RIGHTLEFT
2778 - (wp->w_p_rl ? txtcol : 0)
2779#endif
2780 )
2781 {
2782#ifdef FEAT_MBYTE
2783 if (enc_utf8)
2784 {
2785 if (fill_fold >= 0x80)
2786 {
2787 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002788 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002789 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002794 ScreenLines[off + col] = fill_fold;
2795 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002796 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002798 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002800 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802
2803 if (text != buf)
2804 vim_free(text);
2805
2806 /*
2807 * 6. set highlighting for the Visual area an other text.
2808 * If all folded lines are in the Visual area, highlight the line.
2809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2811 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002812 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
2814 /* Visual is after curwin->w_cursor */
2815 top = &curwin->w_cursor;
2816 bot = &VIsual;
2817 }
2818 else
2819 {
2820 /* Visual is before curwin->w_cursor */
2821 top = &VIsual;
2822 bot = &curwin->w_cursor;
2823 }
2824 if (lnum >= top->lnum
2825 && lnume <= bot->lnum
2826 && (VIsual_mode != 'v'
2827 || ((lnum > top->lnum
2828 || (lnum == top->lnum
2829 && top->col == 0))
2830 && (lnume < bot->lnum
2831 || (lnume == bot->lnum
2832 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002833 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
2835 if (VIsual_mode == Ctrl_V)
2836 {
2837 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002838 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002840 if (wp->w_old_cursor_lcol != MAXCOL
2841 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002842 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 len = wp->w_old_cursor_lcol;
2844 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002845 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002846 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002847 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849 }
2850 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002853 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
2856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002858#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002859 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2860 if (wp->w_p_cc_cols)
2861 {
2862 int i = 0;
2863 int j = wp->w_p_cc_cols[i];
2864 int old_txtcol = txtcol;
2865
2866 while (j > -1)
2867 {
2868 txtcol += j;
2869 if (wp->w_p_wrap)
2870 txtcol -= wp->w_skipcol;
2871 else
2872 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002873 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002874 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002875 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002876 txtcol = old_txtcol;
2877 j = wp->w_p_cc_cols[++i];
2878 }
2879 }
2880
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002881 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002882 if (wp->w_p_cuc)
2883 {
2884 txtcol += wp->w_virtcol;
2885 if (wp->w_p_wrap)
2886 txtcol -= wp->w_skipcol;
2887 else
2888 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002889 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002890 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002891 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002892 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894
Bram Moolenaar02631462017-09-22 15:20:32 +02002895 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2896 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 /*
2899 * Update w_cline_height and w_cline_folded if the cursor line was
2900 * updated (saves a call to plines() later).
2901 */
2902 if (wp == curwin
2903 && lnum <= curwin->w_cursor.lnum
2904 && lnume >= curwin->w_cursor.lnum)
2905 {
2906 curwin->w_cline_row = row;
2907 curwin->w_cline_height = 1;
2908 curwin->w_cline_folded = TRUE;
2909 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2910 }
2911}
2912
2913/*
2914 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2915 */
2916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002917copy_text_attr(
2918 int off,
2919 char_u *buf,
2920 int len,
2921 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002923 int i;
2924
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 mch_memmove(ScreenLines + off, buf, (size_t)len);
2926# ifdef FEAT_MBYTE
2927 if (enc_utf8)
2928 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2929# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002930 for (i = 0; i < len; ++i)
2931 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932}
2933
2934/*
2935 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002936 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 */
2938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002939fill_foldcolumn(
2940 char_u *p,
2941 win_T *wp,
2942 int closed, /* TRUE of FALSE */
2943 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
2945 int i = 0;
2946 int level;
2947 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002948 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002949 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002952 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 level = win_foldinfo.fi_level;
2955 if (level > 0)
2956 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002957 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002958 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002959
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 /* If the column is too narrow, we start at the lowest level that
2961 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002962 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (first_level < 1)
2964 first_level = 1;
2965
Bram Moolenaar1c934292015-01-27 16:39:29 +01002966 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
2968 if (win_foldinfo.fi_lnum == lnum
2969 && first_level + i >= win_foldinfo.fi_low_level)
2970 p[i] = '-';
2971 else if (first_level == 1)
2972 p[i] = '|';
2973 else if (first_level + i <= 9)
2974 p[i] = '0' + first_level + i;
2975 else
2976 p[i] = '>';
2977 if (first_level + i == level)
2978 break;
2979 }
2980 }
2981 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002982 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983}
2984#endif /* FEAT_FOLDING */
2985
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002986#ifdef FEAT_TEXT_PROP
2987static textprop_T *current_text_props = NULL;
2988static buf_T *current_buf = NULL;
2989
2990 static int
2991#ifdef __BORLANDC__
2992_RTLENTRYF
2993#endif
2994text_prop_compare(const void *s1, const void *s2)
2995{
2996 int idx1, idx2;
2997 proptype_T *pt1, *pt2;
2998 colnr_T col1, col2;
2999
3000 idx1 = *(int *)s1;
3001 idx2 = *(int *)s2;
3002 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3003 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3004 if (pt1 == pt2)
3005 return 0;
3006 if (pt1 == NULL)
3007 return -1;
3008 if (pt2 == NULL)
3009 return 1;
3010 if (pt1->pt_priority != pt2->pt_priority)
3011 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3012 col1 = current_text_props[idx1].tp_col;
3013 col2 = current_text_props[idx2].tp_col;
3014 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3015}
3016#endif
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018/*
3019 * Display line "lnum" of window 'wp' on the screen.
3020 * Start at row "startrow", stop when "endrow" is reached.
3021 * wp->w_virtcol needs to be valid.
3022 *
3023 * Return the number of last row the line occupies.
3024 */
3025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003026win_line(
3027 win_T *wp,
3028 linenr_T lnum,
3029 int startrow,
3030 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003031 int nochange UNUSED, // not updating for changed text
3032 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003034 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3036 int c = 0; /* init for GCC */
3037 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003038#ifdef FEAT_LINEBREAK
3039 long vcol_sbr = -1; /* virtual column after showbreak */
3040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 long vcol_prev = -1; /* "vcol" of previous character */
3042 char_u *line; /* current line */
3043 char_u *ptr; /* current position in "line" */
3044 int row; /* row in the window, excl w_winrow */
3045 int screen_row; /* row on the screen, incl w_winrow */
3046
3047 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3048 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003049 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003050 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003052 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 int extra_attr = 0; /* attributes when n_extra != 0 */
3054 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3055 displaying lcs_eol at end-of-line */
3056 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3057 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3058
3059 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3060 int saved_n_extra = 0;
3061 char_u *saved_p_extra = NULL;
3062 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003063 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 int saved_char_attr = 0;
3065
3066 int n_attr = 0; /* chars with special attr */
3067 int saved_attr2 = 0; /* char_attr saved for n_attr */
3068 int n_attr3 = 0; /* chars with overruling special attr */
3069 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3070
3071 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3072
3073 int fromcol, tocol; /* start/end of inverting */
3074 int fromcol_prev = -2; /* start of inverting after cursor */
3075 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003077 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 pos_T pos;
3079 long v;
3080
3081 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003082 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3084 in this line */
3085 int attr = 0; /* attributes for area highlighting */
3086 int area_attr = 0; /* attributes desired by highlighting */
3087 int search_attr = 0; /* attributes desired by 'hlsearch' */
3088#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003089 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int syntax_attr = 0; /* attributes desired by syntax */
3091 int has_syntax = FALSE; /* this buffer has syntax highl. */
3092 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003093 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003094 int draw_color_col = FALSE; /* highlight colorcolumn */
3095 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003096#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003097#ifdef FEAT_TEXT_PROP
3098 int text_prop_count;
3099 int text_prop_next = 0; // next text property to use
3100 textprop_T *text_props = NULL;
3101 int *text_prop_idxs = NULL;
3102 int text_props_active = 0;
3103 proptype_T *text_prop_type = NULL;
3104 int text_prop_attr = 0;
3105#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003106#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003107 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003108# define SPWORDLEN 150
3109 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003110 int nextlinecol = 0; /* column where nextline[] starts */
3111 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003112 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003113 int spell_attr = 0; /* attributes desired by spelling */
3114 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003115 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3116 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003117 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003118 static int cap_col = -1; /* column to check for Cap word */
3119 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003120 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003122 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#ifdef FEAT_MBYTE
3124 int multi_attr = 0; /* attributes desired by multibyte */
3125 int mb_l = 1; /* multi-byte byte length */
3126 int mb_c = 0; /* decoded multi-byte character */
3127 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003128 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129#endif
3130#ifdef FEAT_DIFF
3131 int filler_lines; /* nr of filler lines to be drawn */
3132 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003133 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 int change_start = MAXCOL; /* first col of changed area */
3135 int change_end = -1; /* last col of changed area */
3136#endif
3137 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3138#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003139 int need_showbreak = FALSE; /* overlong line, skipping first x
3140 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003142#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003143 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003145 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#endif
3147#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003148 matchitem_T *cur; /* points to the match list */
3149 match_T *shl; /* points to search_hl or a match */
3150 int shl_flag; /* flag to indicate whether search_hl
3151 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003152 int pos_inprogress; /* marks that position match search is
3153 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003154 int prevcol_hl_flag; /* flag to indicate whether prevcol
3155 equals startcol of search_hl or one
3156 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#endif
3158#ifdef FEAT_ARABIC
3159 int prev_c = 0; /* previous Arabic character */
3160 int prev_c1 = 0; /* first composing char for prev_c */
3161#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003162#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003163 int did_line_attr = 0;
3164#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003165#ifdef FEAT_TERMINAL
3166 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003167 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 /* draw_state: items that are drawn in sequence: */
3171#define WL_START 0 /* nothing done yet */
3172#ifdef FEAT_CMDWIN
3173# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3174#else
3175# define WL_CMDLINE WL_START
3176#endif
3177#ifdef FEAT_FOLDING
3178# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3179#else
3180# define WL_FOLD WL_CMDLINE
3181#endif
3182#ifdef FEAT_SIGNS
3183# define WL_SIGN WL_FOLD + 1 /* column for signs */
3184#else
3185# define WL_SIGN WL_FOLD /* column for signs */
3186#endif
3187#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003188#ifdef FEAT_LINEBREAK
3189# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003191# define WL_BRI WL_NR
3192#endif
3193#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3194# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3195#else
3196# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197#endif
3198#define WL_LINE WL_SBR + 1 /* text in the line */
3199 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003200#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 int feedback_col = 0;
3202 int feedback_old_attr = -1;
3203#endif
3204
Bram Moolenaar860cae12010-06-05 23:22:07 +02003205#ifdef FEAT_CONCEAL
3206 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003207 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003208 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003209 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003210 int is_concealing = FALSE;
3211 int boguscols = 0; /* nonexistent columns added to force
3212 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003213 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003214 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003215 int match_conc = 0; /* cchar for match functions */
3216 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003217 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003218# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003219# define FIX_FOR_BOGUSCOLS \
3220 { \
3221 n_extra += vcol_off; \
3222 vcol -= vcol_off; \
3223 vcol_off = 0; \
3224 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003225 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003226 boguscols = 0; \
3227 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003228#else
3229# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
3232 if (startrow > endrow) /* past the end already! */
3233 return startrow;
3234
3235 row = startrow;
3236 screen_row = row + W_WINROW(wp);
3237
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003238 if (!number_only)
3239 {
3240 /*
3241 * To speed up the loop below, set extra_check when there is linebreak,
3242 * trailing white space and/or syntax processing to be done.
3243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003245 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246#endif
3247#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003248 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003249# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003250 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003251# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003252 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003254 /* Prepare for syntax highlighting in this line. When there is an
3255 * error, stop syntax highlighting. */
3256 save_did_emsg = did_emsg;
3257 did_emsg = FALSE;
3258 syntax_start(wp, lnum);
3259 if (did_emsg)
3260 wp->w_s->b_syn_error = TRUE;
3261 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003262 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003263 did_emsg = save_did_emsg;
3264#ifdef SYN_TIME_LIMIT
3265 if (!wp->w_s->b_syn_slow)
3266#endif
3267 {
3268 has_syntax = TRUE;
3269 extra_check = TRUE;
3270 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003273
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003274 /* Check for columns to display for 'colorcolumn'. */
3275 color_cols = wp->w_p_cc_cols;
3276 if (color_cols != NULL)
3277 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003278#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003279
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003280#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003281 if (term_show_buffer(wp->w_buffer))
3282 {
3283 extra_check = TRUE;
3284 get_term_attr = TRUE;
3285 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3286 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003287#endif
3288
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003289#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003290 if (wp->w_p_spell
3291 && *wp->w_s->b_p_spl != NUL
3292 && wp->w_s->b_langp.ga_len > 0
3293 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003294 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003295 /* Prepare for spell checking. */
3296 has_spell = TRUE;
3297 extra_check = TRUE;
3298
Bram Moolenaar7701f302018-10-02 21:20:32 +02003299 /* Get the start of the next line, so that words that wrap to the
3300 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003301 * Trick: skip a few chars for C/shell/Vim comments */
3302 nextline[SPWORDLEN] = NUL;
3303 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3304 {
3305 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3306 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3307 }
3308
Bram Moolenaar7701f302018-10-02 21:20:32 +02003309 /* When a word wrapped from the previous line the start of the
3310 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003311 if (lnum == checked_lnum)
3312 cur_checked_col = checked_col;
3313 checked_lnum = 0;
3314
3315 /* When there was a sentence end in the previous line may require a
3316 * word starting with capital in this line. In line 1 always check
3317 * the first word. */
3318 if (lnum != capcol_lnum)
3319 cap_col = -1;
3320 if (lnum == 1)
3321 cap_col = 0;
3322 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
3325
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003326 /*
3327 * handle visual active in this window
3328 */
3329 fromcol = -10;
3330 tocol = MAXCOL;
3331 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003333 /* Visual is after curwin->w_cursor */
3334 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003336 top = &curwin->w_cursor;
3337 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003339 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003341 top = &VIsual;
3342 bot = &curwin->w_cursor;
3343 }
3344 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3345 if (VIsual_mode == Ctrl_V) /* block mode */
3346 {
3347 if (lnum_in_visual_area)
3348 {
3349 fromcol = wp->w_old_cursor_fcol;
3350 tocol = wp->w_old_cursor_lcol;
3351 }
3352 }
3353 else /* non-block mode */
3354 {
3355 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003359 if (VIsual_mode == 'V') /* linewise */
3360 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 else
3362 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3364 if (gchar_pos(top) == NUL)
3365 tocol = fromcol + 1;
3366 }
3367 }
3368 if (VIsual_mode != 'V' && lnum == bot->lnum)
3369 {
3370 if (*p_sel == 'e' && bot->col == 0
3371#ifdef FEAT_VIRTUALEDIT
3372 && bot->coladd == 0
3373#endif
3374 )
3375 {
3376 fromcol = -10;
3377 tocol = MAXCOL;
3378 }
3379 else if (bot->col == MAXCOL)
3380 tocol = MAXCOL;
3381 else
3382 {
3383 pos = *bot;
3384 if (*p_sel == 'e')
3385 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3386 else
3387 {
3388 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3389 ++tocol;
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 }
3393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003395 /* Check if the character under the cursor should not be inverted */
3396 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003397#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003398 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003399#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003400 )
3401 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003403 /* if inverting in this line set area_highlighting */
3404 if (fromcol >= 0)
3405 {
3406 area_highlighting = TRUE;
3407 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003409 if ((clip_star.available && !clip_star.owned
3410 && clip_isautosel_star())
3411 || (clip_plus.available && !clip_plus.owned
3412 && clip_isautosel_plus()))
3413 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003418 /*
3419 * handle 'incsearch' and ":s///c" highlighting
3420 */
3421 else if (highlight_match
3422 && wp == curwin
3423 && lnum >= curwin->w_cursor.lnum
3424 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003426 if (lnum == curwin->w_cursor.lnum)
3427 getvcol(curwin, &(curwin->w_cursor),
3428 (colnr_T *)&fromcol, NULL, NULL);
3429 else
3430 fromcol = 0;
3431 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3432 {
3433 pos.lnum = lnum;
3434 pos.col = search_match_endcol;
3435 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3436 }
3437 else
3438 tocol = MAXCOL;
3439 /* do at least one character; happens when past end of line */
3440 if (fromcol == tocol)
3441 tocol = fromcol + 1;
3442 area_highlighting = TRUE;
3443 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446
3447#ifdef FEAT_DIFF
3448 filler_lines = diff_check(wp, lnum);
3449 if (filler_lines < 0)
3450 {
3451 if (filler_lines == -1)
3452 {
3453 if (diff_find_change(wp, lnum, &change_start, &change_end))
3454 diff_hlf = HLF_ADD; /* added line */
3455 else if (change_start == 0)
3456 diff_hlf = HLF_TXD; /* changed text */
3457 else
3458 diff_hlf = HLF_CHD; /* changed line */
3459 }
3460 else
3461 diff_hlf = HLF_ADD; /* added line */
3462 filler_lines = 0;
3463 area_highlighting = TRUE;
3464 }
3465 if (lnum == wp->w_topline)
3466 filler_lines = wp->w_topfill;
3467 filler_todo = filler_lines;
3468#endif
3469
3470#ifdef LINE_ATTR
3471# ifdef FEAT_SIGNS
3472 /* If this line has a sign with line highlighting set line_attr. */
3473 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3474 if (v != 0)
3475 line_attr = sign_get_attr((int)v, TRUE);
3476# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003477# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003480 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481# endif
3482 if (line_attr != 0)
3483 area_highlighting = TRUE;
3484#endif
3485
3486 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3487 ptr = line;
3488
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003489#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003490 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003491 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003492 /* For checking first word with a capital skip white space. */
3493 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003494 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003495
Bram Moolenaar30abd282005-06-22 22:35:10 +00003496 /* To be able to spell-check over line boundaries copy the end of the
3497 * current line into nextline[]. Above the start of the next line was
3498 * copied to nextline[SPWORDLEN]. */
3499 if (nextline[SPWORDLEN] == NUL)
3500 {
3501 /* No next line or it is empty. */
3502 nextlinecol = MAXCOL;
3503 nextline_idx = 0;
3504 }
3505 else
3506 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003507 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003508 if (v < SPWORDLEN)
3509 {
3510 /* Short line, use it completely and append the start of the
3511 * next line. */
3512 nextlinecol = 0;
3513 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003514 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003515 nextline_idx = v + 1;
3516 }
3517 else
3518 {
3519 /* Long line, use only the last SPWORDLEN bytes. */
3520 nextlinecol = v - SPWORDLEN;
3521 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3522 nextline_idx = SPWORDLEN + 1;
3523 }
3524 }
3525 }
3526#endif
3527
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003528 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003530 if (lcs_space || lcs_trail)
3531 extra_check = TRUE;
3532 /* find start of trailing whitespace */
3533 if (lcs_trail)
3534 {
3535 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003536 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003537 --trailcol;
3538 trailcol += (colnr_T) (ptr - line);
3539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541
3542 /*
3543 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3544 * first character to be displayed.
3545 */
3546 if (wp->w_p_wrap)
3547 v = wp->w_skipcol;
3548 else
3549 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003550 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
3552#ifdef FEAT_MBYTE
3553 char_u *prev_ptr = ptr;
3554#endif
3555 while (vcol < v && *ptr != NUL)
3556 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003557 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 vcol += c;
3559#ifdef FEAT_MBYTE
3560 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003562 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003565 /* When:
3566 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003567 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003568 * - 'virtualedit' is set, or
3569 * - the visual mode is active,
3570 * the end of the line may be before the start of the displayed part.
3571 */
3572 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003573#ifdef FEAT_SYN_HL
3574 wp->w_p_cuc || draw_color_col ||
3575#endif
3576#ifdef FEAT_VIRTUALEDIT
3577 virtual_active() ||
3578#endif
3579 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 /* Handle a character that's not completely on the screen: Put ptr at
3585 * that character but skip the first few screen characters. */
3586 if (vcol > v)
3587 {
3588 vcol -= c;
3589#ifdef FEAT_MBYTE
3590 ptr = prev_ptr;
3591#else
3592 --ptr;
3593#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003594 /* If the character fits on the screen, don't need to skip it.
3595 * Except for a TAB. */
3596 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003597#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003598 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003599#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003600 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003601 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603
3604 /*
3605 * Adjust for when the inverted text is before the screen,
3606 * and when the start of the inverted text is before the screen.
3607 */
3608 if (tocol <= vcol)
3609 fromcol = 0;
3610 else if (fromcol >= 0 && fromcol < vcol)
3611 fromcol = vcol;
3612
3613#ifdef FEAT_LINEBREAK
3614 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3615 if (wp->w_p_wrap)
3616 need_showbreak = TRUE;
3617#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003618#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003619 /* When spell checking a word we need to figure out the start of the
3620 * word and if it's badly spelled or not. */
3621 if (has_spell)
3622 {
3623 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003624 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003625 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003626
3627 pos = wp->w_cursor;
3628 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003629 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003630 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003631
3632 /* spell_move_to() may call ml_get() and make "line" invalid */
3633 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3634 ptr = line + linecol;
3635
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003636 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003637 {
3638 /* no bad word found at line start, don't check until end of a
3639 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003640 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003641 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003642 }
3643 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003644 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003645 /* bad word found, use attributes until end of word */
3646 word_end = wp->w_cursor.col + len + 1;
3647
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003648 /* Turn index into actual attributes. */
3649 if (spell_hlf != HLF_COUNT)
3650 spell_attr = highlight_attr[spell_hlf];
3651 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003652 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003653
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003654# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003655 /* Need to restart syntax highlighting for this line. */
3656 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003657 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003658# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003659 }
3660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
3662
3663 /*
3664 * Correct highlighting for cursor that can't be disabled.
3665 * Avoids having to check this for each character.
3666 */
3667 if (fromcol >= 0)
3668 {
3669 if (noinvcur)
3670 {
3671 if ((colnr_T)fromcol == wp->w_virtcol)
3672 {
3673 /* highlighting starts at cursor, let it start just after the
3674 * cursor */
3675 fromcol_prev = fromcol;
3676 fromcol = -1;
3677 }
3678 else if ((colnr_T)fromcol < wp->w_virtcol)
3679 /* restart highlighting after the cursor */
3680 fromcol_prev = wp->w_virtcol;
3681 }
3682 if (fromcol >= tocol)
3683 fromcol = -1;
3684 }
3685
3686#ifdef FEAT_SEARCH_EXTRA
3687 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003688 * Handle highlighting the last used search pattern and matches.
3689 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003691 cur = wp->w_match_head;
3692 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003693 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003695 if (shl_flag == FALSE)
3696 {
3697 shl = &search_hl;
3698 shl_flag = TRUE;
3699 }
3700 else
3701 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003702 shl->startcol = MAXCOL;
3703 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003705 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003706 v = (long)(ptr - line);
3707 if (cur != NULL)
3708 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003709 next_search_hl(wp, shl, lnum, (colnr_T)v,
3710 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003711
3712 /* Need to get the line again, a multi-line regexp may have made it
3713 * invalid. */
3714 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3715 ptr = line + v;
3716
3717 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003719 if (shl->lnum == lnum)
3720 shl->startcol = shl->rm.startpos[0].col;
3721 else
3722 shl->startcol = 0;
3723 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3724 - shl->rm.startpos[0].lnum)
3725 shl->endcol = shl->rm.endpos[0].col;
3726 else
3727 shl->endcol = MAXCOL;
3728 /* Highlight one character for an empty match. */
3729 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003732 if (has_mbyte && line[shl->endcol] != NUL)
3733 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3734 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003736 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003738 if ((long)shl->startcol < v) /* match at leftcol */
3739 {
3740 shl->attr_cur = shl->attr;
3741 search_attr = shl->attr;
3742 }
3743 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003745 if (shl != &search_hl && cur != NULL)
3746 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748#endif
3749
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003750#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003751 /* Cursor line highlighting for 'cursorline' in the current window. Not
3752 * when Visual mode is active, because it's not clear what is selected
3753 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003754 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003755 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003756 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003757 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003758 area_highlighting = TRUE;
3759 }
3760#endif
3761
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003762#ifdef FEAT_TEXT_PROP
3763 {
3764 char_u *prop_start;
3765
3766 text_prop_count = get_text_props(wp->w_buffer, lnum,
3767 &prop_start, FALSE);
3768 if (text_prop_count > 0)
3769 {
3770 // Make a copy of the properties, so that they are properly
3771 // aligned.
3772 text_props = (textprop_T *)alloc(
3773 text_prop_count * sizeof(textprop_T));
3774 if (text_props != NULL)
3775 mch_memmove(text_props, prop_start,
3776 text_prop_count * sizeof(textprop_T));
3777
3778 // Allocate an array for the indexes.
3779 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3780 area_highlighting = TRUE;
3781 extra_check = TRUE;
3782 }
3783 }
3784#endif
3785
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003786 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 col = 0;
3788#ifdef FEAT_RIGHTLEFT
3789 if (wp->w_p_rl)
3790 {
3791 /* Rightleft window: process the text in the normal direction, but put
3792 * it in current_ScreenLine[] from right to left. Start at the
3793 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003794 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 off += col;
3796 }
3797#endif
3798
3799 /*
3800 * Repeat for the whole displayed line.
3801 */
3802 for (;;)
3803 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003804#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003805 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 /* Skip this quickly when working on the text. */
3808 if (draw_state != WL_LINE)
3809 {
3810#ifdef FEAT_CMDWIN
3811 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3812 {
3813 draw_state = WL_CMDLINE;
3814 if (cmdwin_type != 0 && wp == curwin)
3815 {
3816 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003818 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003819 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003820 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 }
3823#endif
3824
3825#ifdef FEAT_FOLDING
3826 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3827 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003828 int fdc = compute_foldcolumn(wp, 0);
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003831 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003833 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003834 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003835 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003836 p_extra_free = alloc(12 + 1);
3837
3838 if (p_extra_free != NULL)
3839 {
3840 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3841 n_extra = fdc;
3842 p_extra_free[n_extra] = NUL;
3843 p_extra = p_extra_free;
3844 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003845 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003846 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 }
3850#endif
3851
3852#ifdef FEAT_SIGNS
3853 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3854 {
3855 draw_state = WL_SIGN;
3856 /* Show the sign column when there are any signs in this
3857 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003858 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003860 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003862 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863# endif
3864
3865 /* Draw two cells with the sign value or blank. */
3866 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003867 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003868 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 n_extra = 2;
3870
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003871 if (row == startrow
3872#ifdef FEAT_DIFF
3873 + filler_lines && filler_todo <= 0
3874#endif
3875 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
3877 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3878 SIGN_TEXT);
3879# ifdef FEAT_SIGN_ICONS
3880 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3881 SIGN_ICON);
3882 if (gui.in_use && icon_sign != 0)
3883 {
3884 /* Use the image in this position. */
3885 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003886 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887# ifdef FEAT_NETBEANS_INTG
3888 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003891 c_final = NUL;
3892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893# endif
3894 char_attr = icon_sign;
3895 }
3896 else
3897# endif
3898 if (text_sign != 0)
3899 {
3900 p_extra = sign_get_text(text_sign);
3901 if (p_extra != NULL)
3902 {
3903 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003904 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003905 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907 char_attr = sign_get_attr(text_sign, FALSE);
3908 }
3909 }
3910 }
3911 }
3912#endif
3913
3914 if (draw_state == WL_NR - 1 && n_extra == 0)
3915 {
3916 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003917 /* Display the absolute or relative line number. After the
3918 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3919 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 && (row == startrow
3921#ifdef FEAT_DIFF
3922 + filler_lines
3923#endif
3924 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3925 {
3926 /* Draw the line number (empty space after wrapping). */
3927 if (row == startrow
3928#ifdef FEAT_DIFF
3929 + filler_lines
3930#endif
3931 )
3932 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003933 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003934 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003935
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003936 if (wp->w_p_nu && !wp->w_p_rnu)
3937 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003938 num = (long)lnum;
3939 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003940 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003941 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003942 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003943 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003944 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003945 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003946 num = lnum;
3947 fmt = "%-*ld ";
3948 }
3949 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003950
Bram Moolenaar700e7342013-01-30 12:31:36 +01003951 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003952 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (wp->w_skipcol > 0)
3954 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3955 *p_extra = '-';
3956#ifdef FEAT_RIGHTLEFT
3957 if (wp->w_p_rl) /* reverse line numbers */
3958 rl_mirror(extra);
3959#endif
3960 p_extra = extra;
3961 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003962 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003967 c_final = NUL;
3968 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003969 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003970 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003971#ifdef FEAT_SYN_HL
3972 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003973 * the current line differently.
3974 * TODO: Can we use CursorLine instead of CursorLineNr
3975 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003976 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003977 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003978 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981 }
3982
Bram Moolenaar597a4222014-06-25 14:39:50 +02003983#ifdef FEAT_LINEBREAK
3984 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3985 && n_extra == 0 && *p_sbr != NUL)
3986 /* draw indent after showbreak value */
3987 draw_state = WL_BRI;
3988 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3989 /* After the showbreak, draw the breakindent */
3990 draw_state = WL_BRI - 1;
3991
3992 /* draw 'breakindent': indent wrapped text accordingly */
3993 if (draw_state == WL_BRI - 1 && n_extra == 0)
3994 {
3995 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003996 /* if need_showbreak is set, breakindent also applies */
3997 if (wp->w_p_bri && n_extra == 0
3998 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003999# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004000 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004001# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004002 )
4003 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004004 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004005# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004006 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004007 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004008 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004009# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004010 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4011 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004012 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004013# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004014 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004015# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004016 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004017 c_extra = ' ';
4018 n_extra = get_breakindent_win(wp,
4019 ml_get_buf(wp->w_buffer, lnum, FALSE));
4020 /* Correct end of highlighted area for 'breakindent',
4021 * required when 'linebreak' is also set. */
4022 if (tocol == vcol)
4023 tocol += n_extra;
4024 }
4025 }
4026#endif
4027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4029 if (draw_state == WL_SBR - 1 && n_extra == 0)
4030 {
4031 draw_state = WL_SBR;
4032# ifdef FEAT_DIFF
4033 if (filler_todo > 0)
4034 {
4035 /* Draw "deleted" diff line(s). */
4036 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004039 c_final = NUL;
4040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004044 c_final = NUL;
4045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046# ifdef FEAT_RIGHTLEFT
4047 if (wp->w_p_rl)
4048 n_extra = col + 1;
4049 else
4050# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004051 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004052 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054# endif
4055# ifdef FEAT_LINEBREAK
4056 if (*p_sbr != NUL && need_showbreak)
4057 {
4058 /* Draw 'showbreak' at the start of each broken line. */
4059 p_extra = p_sbr;
4060 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004061 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004063 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004065 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 /* Correct end of highlighted area for 'showbreak',
4067 * required when 'linebreak' is also set. */
4068 if (tocol == vcol)
4069 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004070#ifdef FEAT_SYN_HL
4071 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004072 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004073 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004074 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077# endif
4078 }
4079#endif
4080
4081 if (draw_state == WL_LINE - 1 && n_extra == 0)
4082 {
4083 draw_state = WL_LINE;
4084 if (saved_n_extra)
4085 {
4086 /* Continue item from end of wrapped line. */
4087 n_extra = saved_n_extra;
4088 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004089 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 p_extra = saved_p_extra;
4091 char_attr = saved_char_attr;
4092 }
4093 else
4094 char_attr = 0;
4095 }
4096 }
4097
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004098 // When still displaying '$' of change command, stop at cursor.
4099 // When only displaying the (relative) line number and that's done,
4100 // stop here.
4101 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004102 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103#ifdef FEAT_DIFF
4104 && filler_todo <= 0
4105#endif
4106 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004107 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004109 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004110 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004111 /* Pretend we have finished updating the window. Except when
4112 * 'cursorcolumn' is set. */
4113#ifdef FEAT_SYN_HL
4114 if (wp->w_p_cuc)
4115 row = wp->w_cline_row + wp->w_cline_height;
4116 else
4117#endif
4118 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 break;
4120 }
4121
Bram Moolenaar637532b2019-01-03 21:44:40 +01004122 if (draw_state == WL_LINE && (area_highlighting
4123#ifdef FEAT_SPELL
4124 || has_spell
4125#endif
4126 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
4128 /* handle Visual or match highlighting in this line */
4129 if (vcol == fromcol
4130#ifdef FEAT_MBYTE
4131 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4132 && (*mb_ptr2cells)(ptr) > 1)
4133#endif
4134 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004135 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 && vcol < tocol))
4137 area_attr = attr; /* start highlighting */
4138 else if (area_attr != 0
4139 && (vcol == tocol
4140 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143#ifdef FEAT_SEARCH_EXTRA
4144 if (!n_extra)
4145 {
4146 /*
4147 * Check for start/end of search pattern match.
4148 * After end, check for start/end of next match.
4149 * When another match, have to check for start again.
4150 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004151 * Do this for 'search_hl' and the match list (ordered by
4152 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004154 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004155 cur = wp->w_match_head;
4156 shl_flag = FALSE;
4157 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004159 if (shl_flag == FALSE
4160 && ((cur != NULL
4161 && cur->priority > SEARCH_HL_PRIORITY)
4162 || cur == NULL))
4163 {
4164 shl = &search_hl;
4165 shl_flag = TRUE;
4166 }
4167 else
4168 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004169 if (cur != NULL)
4170 cur->pos.cur = 0;
4171 pos_inprogress = TRUE;
4172 while (shl->rm.regprog != NULL
4173 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004175 if (shl->startcol != MAXCOL
4176 && v >= (long)shl->startcol
4177 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004179#ifdef FEAT_MBYTE
4180 int tmp_col = v + MB_PTR2LEN(ptr);
4181
4182 if (shl->endcol < tmp_col)
4183 shl->endcol = tmp_col;
4184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004186#ifdef FEAT_CONCEAL
4187 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4188 == cur->hlg_id)
4189 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004190 has_match_conc =
4191 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004192 match_conc = cur->conceal_char;
4193 }
4194 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004195 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004198 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 {
4200 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004201 next_search_hl(wp, shl, lnum, (colnr_T)v,
4202 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004203 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004204 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
4206 /* Need to get the line again, a multi-line regexp
4207 * may have made it invalid. */
4208 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4209 ptr = line + v;
4210
4211 if (shl->lnum == lnum)
4212 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004213 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004215 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004217 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004219 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
4221 /* highlight empty match, try again after
4222 * it */
4223#ifdef FEAT_MBYTE
4224 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004225 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004226 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 else
4228#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004229 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231
4232 /* Loop to check if the match starts at the
4233 * current position */
4234 continue;
4235 }
4236 }
4237 break;
4238 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004239 if (shl != &search_hl && cur != NULL)
4240 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004242
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004243 /* Use attributes from match with highest priority among
4244 * 'search_hl' and the match list. */
4245 search_attr = search_hl.attr_cur;
4246 cur = wp->w_match_head;
4247 shl_flag = FALSE;
4248 while (cur != NULL || shl_flag == FALSE)
4249 {
4250 if (shl_flag == FALSE
4251 && ((cur != NULL
4252 && cur->priority > SEARCH_HL_PRIORITY)
4253 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004254 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004255 shl = &search_hl;
4256 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004257 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004258 else
4259 shl = &cur->hl;
4260 if (shl->attr_cur != 0)
4261 search_attr = shl->attr_cur;
4262 if (shl != &search_hl && cur != NULL)
4263 cur = cur->next;
4264 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004265 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004266 if (*ptr == NUL && (did_line_attr >= 1
4267 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004268 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270#endif
4271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004273 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004275 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4276 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004278 if (diff_hlf == HLF_TXD && ptr - line > change_end
4279 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004281 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004282 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004283 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004286
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004287#ifdef FEAT_TEXT_PROP
4288 if (text_props != NULL)
4289 {
4290 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004291 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004292
4293 // Check if any active property ends.
4294 for (pi = 0; pi < text_props_active; ++pi)
4295 {
4296 int tpi = text_prop_idxs[pi];
4297
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004298 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004299 + text_props[tpi].tp_len)
4300 {
4301 if (pi + 1 < text_props_active)
4302 mch_memmove(text_prop_idxs + pi,
4303 text_prop_idxs + pi + 1,
4304 sizeof(int)
4305 * (text_props_active - (pi + 1)));
4306 --text_props_active;
4307 --pi;
4308 }
4309 }
4310
4311 // Add any text property that starts in this column.
4312 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004313 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004314 text_prop_idxs[text_props_active++] = text_prop_next++;
4315
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004316 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004317 if (text_props_active > 0)
4318 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004319 // Sort the properties on priority and/or starting last.
4320 // Then combine the attributes, highest priority last.
4321 current_text_props = text_props;
4322 current_buf = wp->w_buffer;
4323 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4324 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004325
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004326 for (pi = 0; pi < text_props_active; ++pi)
4327 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004328 int tpi = text_prop_idxs[pi];
4329 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004330
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004331 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004332 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004333 int pt_attr = syn_id2attr(pt->pt_hl_id);
4334
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004335 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004336 if (text_prop_attr == 0)
4337 text_prop_attr = pt_attr;
4338 else
4339 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004340 }
4341 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004342 }
4343 }
4344#endif
4345
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004346 /* Decide which of the highlight attributes to use. */
4347 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004348#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004349 if (area_attr != 0)
4350 char_attr = hl_combine_attr(line_attr, area_attr);
4351 else if (search_attr != 0)
4352 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004353 /* Use line_attr when not in the Visual or 'incsearch' area
4354 * (area_attr may be 0 when "noinvcur" is set). */
4355 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004356 || vcol < fromcol || vcol_prev < fromcol_prev
4357 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004358 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004359#else
4360 if (area_attr != 0)
4361 char_attr = area_attr;
4362 else if (search_attr != 0)
4363 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004364#endif
4365 else
4366 {
4367 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004368#ifdef FEAT_TEXT_PROP
4369 if (text_prop_type != NULL)
4370 char_attr = text_prop_attr;
4371 else
4372#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004373#ifdef FEAT_SYN_HL
4374 if (has_syntax)
4375 char_attr = syntax_attr;
4376 else
4377#endif
4378 char_attr = 0;
4379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381
4382 /*
4383 * Get the next character to put on the screen.
4384 */
4385 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004386 * The "p_extra" points to the extra stuff that is inserted to
4387 * represent special characters (non-printable stuff) and other
4388 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004389 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004390 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4391 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4393 */
4394 if (n_extra > 0)
4395 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004396 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004398 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399#ifdef FEAT_MBYTE
4400 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004401 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
4403 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004404 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004405 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407 else
4408 mb_utf8 = FALSE;
4409#endif
4410 }
4411 else
4412 {
4413 c = *p_extra;
4414#ifdef FEAT_MBYTE
4415 if (has_mbyte)
4416 {
4417 mb_c = c;
4418 if (enc_utf8)
4419 {
4420 /* If the UTF-8 character is more than one byte:
4421 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004422 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 mb_utf8 = FALSE;
4424 if (mb_l > n_extra)
4425 mb_l = 1;
4426 else if (mb_l > 1)
4427 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004428 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004430 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 }
4433 else
4434 {
4435 /* if this is a DBCS character, put it in "mb_c" */
4436 mb_l = MB_BYTE2LEN(c);
4437 if (mb_l >= n_extra)
4438 mb_l = 1;
4439 else if (mb_l > 1)
4440 mb_c = (c << 8) + p_extra[1];
4441 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004442 if (mb_l == 0) /* at the NUL at end-of-line */
4443 mb_l = 1;
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 /* If a double-width char doesn't fit display a '>' in the
4446 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004447 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448# ifdef FEAT_RIGHTLEFT
4449 wp->w_p_rl ? (col <= 0) :
4450# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004451 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 && (*mb_char2cells)(mb_c) == 2)
4453 {
4454 c = '>';
4455 mb_c = c;
4456 mb_l = 1;
4457 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004458 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 /* put the pointer back to output the double-width
4460 * character at the start of the next line. */
4461 ++n_extra;
4462 --p_extra;
4463 }
4464 else
4465 {
4466 n_extra -= mb_l - 1;
4467 p_extra += mb_l - 1;
4468 }
4469 }
4470#endif
4471 ++p_extra;
4472 }
4473 --n_extra;
4474 }
4475 else
4476 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004477#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004478 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004479#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004480
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004481 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004482 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 /*
4484 * Get a character from the line itself.
4485 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004486 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004487#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004488 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490#ifdef FEAT_MBYTE
4491 if (has_mbyte)
4492 {
4493 mb_c = c;
4494 if (enc_utf8)
4495 {
4496 /* If the UTF-8 character is more than one byte: Decode it
4497 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004498 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 mb_utf8 = FALSE;
4500 if (mb_l > 1)
4501 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004502 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 /* Overlong encoded ASCII or ASCII with composing char
4504 * is displayed normally, except a NUL. */
4505 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004506 {
4507 c = mb_c;
4508# ifdef FEAT_LINEBREAK
4509 c0 = mb_c;
4510# endif
4511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004513
4514 /* At start of the line we can have a composing char.
4515 * Draw it as a space with a composing char. */
4516 if (utf_iscomposing(mb_c))
4517 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004518 int i;
4519
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004520 for (i = Screen_mco - 1; i > 0; --i)
4521 u8cc[i] = u8cc[i - 1];
4522 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004523 mb_c = ' ';
4524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526
4527 if ((mb_l == 1 && c >= 0x80)
4528 || (mb_l >= 1 && mb_c == 0)
4529 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004530# ifdef UNICODE16
4531 || mb_c >= 0x10000
4532# endif
4533 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
4535 /*
4536 * Illegal UTF-8 byte: display as <xx>.
4537 * Non-BMP character : display as ? or fullwidth ?.
4538 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004539# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 {
4543 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004544# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (wp->w_p_rl) /* reverse */
4546 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004549# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 else if (utf_char2cells(mb_c) != 2)
4551 STRCPY(extra, "?");
4552 else
4553 /* 0xff1f in UTF-8: full-width '?' */
4554 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557 p_extra = extra;
4558 c = *p_extra;
4559 mb_c = mb_ptr2char_adv(&p_extra);
4560 mb_utf8 = (c >= 0x80);
4561 n_extra = (int)STRLEN(p_extra);
4562 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004563 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if (area_attr == 0 && search_attr == 0)
4565 {
4566 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004567 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 saved_attr2 = char_attr; /* save current attr */
4569 }
4570 }
4571 else if (mb_l == 0) /* at the NUL at end-of-line */
4572 mb_l = 1;
4573#ifdef FEAT_ARABIC
4574 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4575 {
4576 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004577 int pc, pc1, nc;
4578 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
4580 /* The idea of what is the previous and next
4581 * character depends on 'rightleft'. */
4582 if (wp->w_p_rl)
4583 {
4584 pc = prev_c;
4585 pc1 = prev_c1;
4586 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004587 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
4589 else
4590 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004591 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004593 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595 prev_c = mb_c;
4596
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004597 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599 else
4600 prev_c = mb_c;
4601#endif
4602 }
4603 else /* enc_dbcs */
4604 {
4605 mb_l = MB_BYTE2LEN(c);
4606 if (mb_l == 0) /* at the NUL at end-of-line */
4607 mb_l = 1;
4608 else if (mb_l > 1)
4609 {
4610 /* We assume a second byte below 32 is illegal.
4611 * Hopefully this is OK for all double-byte encodings!
4612 */
4613 if (ptr[1] >= 32)
4614 mb_c = (c << 8) + ptr[1];
4615 else
4616 {
4617 if (ptr[1] == NUL)
4618 {
4619 /* head byte at end of line */
4620 mb_l = 1;
4621 transchar_nonprint(extra, c);
4622 }
4623 else
4624 {
4625 /* illegal tail byte */
4626 mb_l = 2;
4627 STRCPY(extra, "XX");
4628 }
4629 p_extra = extra;
4630 n_extra = (int)STRLEN(extra) - 1;
4631 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004632 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 c = *p_extra++;
4634 if (area_attr == 0 && search_attr == 0)
4635 {
4636 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004637 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 saved_attr2 = char_attr; /* save current attr */
4639 }
4640 mb_c = c;
4641 }
4642 }
4643 }
4644 /* If a double-width char doesn't fit display a '>' in the
4645 * last column; the character is displayed at the start of the
4646 * next line. */
4647 if ((
4648# ifdef FEAT_RIGHTLEFT
4649 wp->w_p_rl ? (col <= 0) :
4650# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004651 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 && (*mb_char2cells)(mb_c) == 2)
4653 {
4654 c = '>';
4655 mb_c = c;
4656 mb_utf8 = FALSE;
4657 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004658 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 /* Put pointer back so that the character will be
4660 * displayed at the start of the next line. */
4661 --ptr;
4662 }
4663 else if (*ptr != NUL)
4664 ptr += mb_l - 1;
4665
4666 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004667 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004668 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004669 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004672 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004673 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 c = ' ';
4675 if (area_attr == 0 && search_attr == 0)
4676 {
4677 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004678 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 saved_attr2 = char_attr; /* save current attr */
4680 }
4681 mb_c = c;
4682 mb_utf8 = FALSE;
4683 mb_l = 1;
4684 }
4685
4686 }
4687#endif
4688 ++ptr;
4689
4690 if (extra_check)
4691 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004692#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004693 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004694#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004695
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004696#ifdef FEAT_TERMINAL
4697 if (get_term_attr)
4698 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004699 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004700
4701 if (!attr_pri)
4702 char_attr = syntax_attr;
4703 else
4704 char_attr = hl_combine_attr(syntax_attr, char_attr);
4705 }
4706#endif
4707
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004708#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004709 // Get syntax attribute, unless still at the start of the line
4710 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004711 v = (long)(ptr - line);
4712 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
4714 /* Get the syntax attribute for the character. If there
4715 * is an error, disable syntax highlighting. */
4716 save_did_emsg = did_emsg;
4717 did_emsg = FALSE;
4718
Bram Moolenaar217ad922005-03-20 22:37:15 +00004719 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004720# ifdef FEAT_SPELL
4721 has_spell ? &can_spell :
4722# endif
4723 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
4725 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004726 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004727 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004728 has_syntax = FALSE;
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 else
4731 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004732#ifdef SYN_TIME_LIMIT
4733 if (wp->w_s->b_syn_slow)
4734 has_syntax = FALSE;
4735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737 /* Need to get the line again, a multi-line regexp may
4738 * have made it invalid. */
4739 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4740 ptr = line + v;
4741
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004742# ifdef FEAT_TEXT_PROP
4743 // Text properties overrule syntax highlighting.
4744 if (text_prop_attr == 0)
4745#endif
4746 {
4747 if (!attr_pri)
4748 char_attr = syntax_attr;
4749 else
4750 char_attr = hl_combine_attr(syntax_attr, char_attr);
4751 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004752# ifdef FEAT_CONCEAL
4753 /* no concealing past the end of the line, it interferes
4754 * with line highlighting */
4755 if (c == NUL)
4756 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004757 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004758 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004761#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004762
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004763#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004764 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004765 * Only do this when there is no syntax highlighting, the
4766 * @Spell cluster is not used or the current syntax item
4767 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004768 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004769 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004770 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004771 if (c != 0 && (
4772# ifdef FEAT_SYN_HL
4773 !has_syntax ||
4774# endif
4775 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004776 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004777 char_u *prev_ptr, *p;
4778 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004779 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004780# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004781 if (has_mbyte)
4782 {
4783 prev_ptr = ptr - mb_l;
4784 v -= mb_l - 1;
4785 }
4786 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004787# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004788 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004789
4790 /* Use nextline[] if possible, it has the start of the
4791 * next line concatenated. */
4792 if ((prev_ptr - line) - nextlinecol >= 0)
4793 p = nextline + (prev_ptr - line) - nextlinecol;
4794 else
4795 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004796 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004797 len = spell_check(wp, p, &spell_hlf, &cap_col,
4798 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004799 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004800
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004801 /* In Insert mode only highlight a word that
4802 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004803 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004804 && (State & INSERT) != 0
4805 && wp->w_cursor.lnum == lnum
4806 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004807 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004808 && wp->w_cursor.col < (colnr_T)word_end)
4809 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004810 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004811 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004812 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004813
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004814 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004815 && (p - nextline) + len > nextline_idx)
4816 {
4817 /* Remember that the good word continues at the
4818 * start of the next line. */
4819 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004820 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004821 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004822
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004823 /* Turn index into actual attributes. */
4824 if (spell_hlf != HLF_COUNT)
4825 spell_attr = highlight_attr[spell_hlf];
4826
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004827 if (cap_col > 0)
4828 {
4829 if (p != prev_ptr
4830 && (p - nextline) + cap_col >= nextline_idx)
4831 {
4832 /* Remember that the word in the next line
4833 * must start with a capital. */
4834 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004835 cap_col = (int)((p - nextline) + cap_col
4836 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004837 }
4838 else
4839 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004840 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004841 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004842 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004843 }
4844 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004845 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004846 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004847 char_attr = hl_combine_attr(char_attr, spell_attr);
4848 else
4849 char_attr = hl_combine_attr(spell_attr, char_attr);
4850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851#endif
4852#ifdef FEAT_LINEBREAK
4853 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004854 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004856 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004857 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004859# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004860 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004861# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004862 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004864 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004866 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004867
Bram Moolenaar597a4222014-06-25 14:39:50 +02004868 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004869 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004870 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004871 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004872# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004873 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004874 wp->w_buffer->b_p_vts_array) - 1;
4875# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004876 n_extra = (int)wp->w_buffer->b_p_ts
4877 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004878# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004879
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004880# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004881 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004882# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004884# endif
Bram Moolenaar83a52172019-01-16 22:41:54 +01004885 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004886 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004887 {
4888#ifdef FEAT_CONCEAL
4889 if (c == TAB)
4890 /* See "Tab alignment" below. */
4891 FIX_FOR_BOGUSCOLS;
4892#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004893 if (!wp->w_p_list)
4894 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897#endif
4898
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004899 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4900 */
4901 if (wp->w_p_list
4902 && (((c == 160
4903#ifdef FEAT_MBYTE
4904 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4905#endif
4906 ) && lcs_nbsp)
4907 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4908 {
4909 c = (c == ' ') ? lcs_space : lcs_nbsp;
4910 if (area_attr == 0 && search_attr == 0)
4911 {
4912 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004913 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004914 saved_attr2 = char_attr; /* save current attr */
4915 }
4916#ifdef FEAT_MBYTE
4917 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004918 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004919 {
4920 mb_utf8 = TRUE;
4921 u8cc[0] = 0;
4922 c = 0xc0;
4923 }
4924 else
4925 mb_utf8 = FALSE;
4926#endif
4927 }
4928
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4930 {
4931 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004932 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 {
4934 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004935 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 saved_attr2 = char_attr; /* save current attr */
4937 }
4938#ifdef FEAT_MBYTE
4939 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004940 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
4942 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004943 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004944 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 else
4947 mb_utf8 = FALSE;
4948#endif
4949 }
4950 }
4951
4952 /*
4953 * Handling of non-printable characters.
4954 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004955 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
4957 /*
4958 * when getting a character from the file, we may have to
4959 * turn it into something else on the way to putting it
4960 * into "ScreenLines".
4961 */
4962 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4963 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004964 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004965 long vcol_adjusted = vcol; /* removed showbreak length */
4966#ifdef FEAT_LINEBREAK
4967 /* only adjust the tab_len, when at the first column
4968 * after the showbreak value was drawn */
4969 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4970 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004973#ifdef FEAT_VARTABS
4974 tab_len = tabstop_padding(vcol_adjusted,
4975 wp->w_buffer->b_p_ts,
4976 wp->w_buffer->b_p_vts_array) - 1;
4977#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004978 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004979 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4980#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004981
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004982#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004983 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004984#endif
4985 /* tab amount depends on current column */
4986 n_extra = tab_len;
4987#ifdef FEAT_LINEBREAK
4988 else
4989 {
4990 char_u *p;
4991 int len = n_extra;
4992 int i;
4993 int saved_nextra = n_extra;
4994
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004995#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004996 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004997 /* there are characters to conceal */
4998 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004999 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5000 */
5001 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5002 && n_extra > tab_len)
5003 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005004#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005005
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005006 /* if n_extra > 0, it gives the number of chars, to
5007 * use for a tab, else we need to calculate the width
5008 * for a tab */
5009#ifdef FEAT_MBYTE
5010 len = (tab_len * mb_char2len(lcs_tab2));
5011 if (n_extra > 0)
5012 len += n_extra - tab_len;
5013#endif
5014 c = lcs_tab1;
5015 p = alloc((unsigned)(len + 1));
5016 vim_memset(p, ' ', len);
5017 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005018 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005019 p_extra_free = p;
5020 for (i = 0; i < tab_len; i++)
5021 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005022 if (*p == NUL)
5023 {
5024 tab_len = i;
5025 break;
5026 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005027#ifdef FEAT_MBYTE
5028 mb_char2bytes(lcs_tab2, p);
5029 p += mb_char2len(lcs_tab2);
5030 n_extra += mb_char2len(lcs_tab2)
5031 - (saved_nextra > 0 ? 1 : 0);
5032#else
5033 p[i] = lcs_tab2;
5034#endif
5035 }
5036 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005037#ifdef FEAT_CONCEAL
5038 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5039 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005040 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005041 n_extra -= vcol_off;
5042#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005043 }
5044#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005045#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005046 {
5047 int vc_saved = vcol_off;
5048
5049 /* Tab alignment should be identical regardless of
5050 * 'conceallevel' value. So tab compensates of all
5051 * previous concealed characters, and thus resets
5052 * vcol_off and boguscols accumulated so far in the
5053 * line. Note that the tab can be longer than
5054 * 'tabstop' when there are concealed characters. */
5055 FIX_FOR_BOGUSCOLS;
5056
5057 /* Make sure, the highlighting for the tab char will be
5058 * correctly set further below (effectively reverts the
5059 * FIX_FOR_BOGSUCOLS macro */
5060 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005061 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005062 tab_len += vc_saved;
5063 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065#ifdef FEAT_MBYTE
5066 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5067#endif
5068 if (wp->w_p_list)
5069 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005070 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005071#ifdef FEAT_LINEBREAK
5072 if (wp->w_p_lbr)
5073 c_extra = NUL; /* using p_extra from above */
5074 else
5075#endif
5076 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005077 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005078 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005079 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 saved_attr2 = char_attr; /* save current attr */
5081#ifdef FEAT_MBYTE
5082 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005083 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005086 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005087 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
5089#endif
5090 }
5091 else
5092 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005093 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 c_extra = ' ';
5095 c = ' ';
5096 }
5097 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005098 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005099 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005100 || ((fromcol >= 0 || fromcol_prev >= 0)
5101 && tocol > vcol
5102 && VIsual_mode != Ctrl_V
5103 && (
5104# ifdef FEAT_RIGHTLEFT
5105 wp->w_p_rl ? (col >= 0) :
5106# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005107 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005108 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005109 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005110 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005111 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005113 /* Display a '$' after the line or highlight an extra
5114 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5116 /* For a diff line the highlighting continues after the
5117 * "$". */
5118 if (
5119# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005120 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121# ifdef LINE_ATTR
5122 &&
5123# endif
5124# endif
5125# ifdef LINE_ATTR
5126 line_attr == 0
5127# endif
5128 )
5129#endif
5130 {
5131#ifdef FEAT_VIRTUALEDIT
5132 /* In virtualedit, visual selections may extend
5133 * beyond end of line. */
5134 if (area_highlighting && virtual_active()
5135 && tocol != MAXCOL && vcol < tocol)
5136 n_extra = 0;
5137 else
5138#endif
5139 {
5140 p_extra = at_end_str;
5141 n_extra = 1;
5142 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005143 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005146 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005147 c = lcs_eol;
5148 else
5149 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 lcs_eol_one = -1;
5151 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005152 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005154 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 n_attr = 1;
5156 }
5157#ifdef FEAT_MBYTE
5158 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005159 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
5161 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005162 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005163 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 else
5166 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5167#endif
5168 }
5169 else if (c != NUL)
5170 {
5171 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005172 if (n_extra == 0)
5173 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174#ifdef FEAT_RIGHTLEFT
5175 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5176 rl_mirror(p_extra); /* reverse "<12>" */
5177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005179 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005180#ifdef FEAT_LINEBREAK
5181 if (wp->w_p_lbr)
5182 {
5183 char_u *p;
5184
5185 c = *p_extra;
5186 p = alloc((unsigned)n_extra + 1);
5187 vim_memset(p, ' ', n_extra);
5188 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5189 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005190 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005191 p_extra_free = p_extra = p;
5192 }
5193 else
5194#endif
5195 {
5196 n_extra = byte2cells(c) - 1;
5197 c = *p_extra++;
5198 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005199 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
5201 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005202 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 saved_attr2 = char_attr; /* save current attr */
5204 }
5205#ifdef FEAT_MBYTE
5206 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5207#endif
5208 }
5209#ifdef FEAT_VIRTUALEDIT
5210 else if (VIsual_active
5211 && (VIsual_mode == Ctrl_V
5212 || VIsual_mode == 'v')
5213 && virtual_active()
5214 && tocol != MAXCOL
5215 && vcol < tocol
5216 && (
5217# ifdef FEAT_RIGHTLEFT
5218 wp->w_p_rl ? (col >= 0) :
5219# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005220 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 c = ' ';
5223 --ptr; /* put it back at the NUL */
5224 }
5225#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005226#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 else if ((
5228# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005229 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005231# ifdef FEAT_TERMINAL
5232 term_attr != 0 ||
5233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 ) && (
5236# ifdef FEAT_RIGHTLEFT
5237 wp->w_p_rl ? (col >= 0) :
5238# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005239 (col
5240# ifdef FEAT_CONCEAL
5241 - boguscols
5242# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005243 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 {
5245 /* Highlight until the right side of the window */
5246 c = ' ';
5247 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005248
5249 /* Remember we do the char for line highlighting. */
5250 ++did_line_attr;
5251
5252 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005253 if (line_attr != 0 && char_attr == search_attr
5254 && (did_line_attr > 1
5255 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005256 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257# ifdef FEAT_DIFF
5258 if (diff_hlf == HLF_TXD)
5259 {
5260 diff_hlf = HLF_CHD;
5261 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005262 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005263 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005264 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5265 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005266 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
5269# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005270# ifdef FEAT_TERMINAL
5271 if (term_attr != 0)
5272 {
5273 char_attr = term_attr;
5274 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5275 char_attr = hl_combine_attr(char_attr,
5276 HL_ATTR(HLF_CUL));
5277 }
5278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280#endif
5281 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005282
5283#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005284 if ( wp->w_p_cole > 0
5285 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005286 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005287 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005288 && !(lnum_in_visual_area
5289 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005290 {
5291 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005292 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005293 && (syn_get_sub_char() != NUL || match_conc
5294 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005295 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005296 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005297 /* First time at this concealed item: display one
5298 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005299 if (match_conc)
5300 c = match_conc;
5301 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005302 c = syn_get_sub_char();
5303 else if (lcs_conceal != NUL)
5304 c = lcs_conceal;
5305 else
5306 c = ' ';
5307
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005308 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005309
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005310 if (n_extra > 0)
5311 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005312 vcol += n_extra;
5313 if (wp->w_p_wrap && n_extra > 0)
5314 {
5315# ifdef FEAT_RIGHTLEFT
5316 if (wp->w_p_rl)
5317 {
5318 col -= n_extra;
5319 boguscols -= n_extra;
5320 }
5321 else
5322# endif
5323 {
5324 boguscols += n_extra;
5325 col += n_extra;
5326 }
5327 }
5328 n_extra = 0;
5329 n_attr = 0;
5330 }
5331 else if (n_skip == 0)
5332 {
5333 is_concealing = TRUE;
5334 n_skip = 1;
5335 }
5336# ifdef FEAT_MBYTE
5337 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005338 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005339 {
5340 mb_utf8 = TRUE;
5341 u8cc[0] = 0;
5342 c = 0xc0;
5343 }
5344 else
5345 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5346# endif
5347 }
5348 else
5349 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005350 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005351 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005352 }
5353#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005356#ifdef FEAT_CONCEAL
5357 /* In the cursor line and we may be concealing characters: correct
5358 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005359 if (!did_wcol && draw_state == WL_LINE
5360 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005361 && conceal_cursor_line(wp)
5362 && (int)wp->w_virtcol <= vcol + n_skip)
5363 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005364# ifdef FEAT_RIGHTLEFT
5365 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005366 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005367 else
5368# endif
5369 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005370 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005371 did_wcol = TRUE;
5372 }
5373#endif
5374
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 /* Don't override visual selection highlighting. */
5376 if (n_attr > 0
5377 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005378 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 char_attr = extra_attr;
5380
Bram Moolenaar81695252004-12-29 20:58:21 +00005381#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 /* XIM don't send preedit_start and preedit_end, but they send
5383 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5384 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005385 if (p_imst == IM_ON_THE_SPOT
5386 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005387 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 && (State & INSERT)
5389 && !p_imdisable
5390 && im_is_preediting()
5391 && draw_state == WL_LINE)
5392 {
5393 colnr_T tcol;
5394
5395 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005396 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 else
5398 tcol = preedit_end_col;
5399 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5400 {
5401 if (feedback_old_attr < 0)
5402 {
5403 feedback_col = 0;
5404 feedback_old_attr = char_attr;
5405 }
5406 char_attr = im_get_feedback_attr(feedback_col);
5407 if (char_attr < 0)
5408 char_attr = feedback_old_attr;
5409 feedback_col++;
5410 }
5411 else if (feedback_old_attr >= 0)
5412 {
5413 char_attr = feedback_old_attr;
5414 feedback_old_attr = -1;
5415 feedback_col = 0;
5416 }
5417 }
5418#endif
5419 /*
5420 * Handle the case where we are in column 0 but not on the first
5421 * character of the line and the user wants us to show us a
5422 * special character (via 'listchars' option "precedes:<char>".
5423 */
5424 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005425 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5427#ifdef FEAT_DIFF
5428 && filler_todo <= 0
5429#endif
5430 && draw_state > WL_NR
5431 && c != NUL)
5432 {
5433 c = lcs_prec;
5434 lcs_prec_todo = NUL;
5435#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005436 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5437 {
5438 /* Double-width character being overwritten by the "precedes"
5439 * character, need to fill up half the character. */
5440 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005441 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005442 n_extra = 1;
5443 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005444 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005447 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
5449 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005450 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005451 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453 else
5454 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5455#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005456 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005459 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 n_attr3 = 1;
5461 }
5462 }
5463
5464 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005465 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005467 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005468#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005469 || did_line_attr == 1
5470#endif
5471 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005473#ifdef FEAT_SEARCH_EXTRA
5474 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005475
5476 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005477 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005478 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005479#endif
5480
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005481 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 * highlight match at end of line. If it's beyond the last
5483 * char on the screen, just overwrite that one (tricky!) Not
5484 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005485#ifdef FEAT_SEARCH_EXTRA
5486 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005487 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005488 prevcol_hl_flag = TRUE;
5489 else
5490 {
5491 cur = wp->w_match_head;
5492 while (cur != NULL)
5493 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005494 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005495 {
5496 prevcol_hl_flag = TRUE;
5497 break;
5498 }
5499 cur = cur->next;
5500 }
5501 }
5502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005504 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005505 && (VIsual_mode != Ctrl_V
5506 || lnum == VIsual.lnum
5507 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005508 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509#ifdef FEAT_SEARCH_EXTRA
5510 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005511 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005512# ifdef FEAT_SYN_HL
5513 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5514 && !(wp == curwin && VIsual_active))
5515# endif
5516# ifdef FEAT_DIFF
5517 && diff_hlf == (hlf_T)0
5518# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005519# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005520 && did_line_attr <= 1
5521# endif
5522 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523#endif
5524 ))
5525 {
5526 int n = 0;
5527
5528#ifdef FEAT_RIGHTLEFT
5529 if (wp->w_p_rl)
5530 {
5531 if (col < 0)
5532 n = 1;
5533 }
5534 else
5535#endif
5536 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005537 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 n = -1;
5539 }
5540 if (n != 0)
5541 {
5542 /* At the window boundary, highlight the last character
5543 * instead (better than nothing). */
5544 off += n;
5545 col += n;
5546 }
5547 else
5548 {
5549 /* Add a blank character to highlight. */
5550 ScreenLines[off] = ' ';
5551#ifdef FEAT_MBYTE
5552 if (enc_utf8)
5553 ScreenLinesUC[off] = 0;
5554#endif
5555 }
5556#ifdef FEAT_SEARCH_EXTRA
5557 if (area_attr == 0)
5558 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005559 /* Use attributes from match with highest priority among
5560 * 'search_hl' and the match list. */
5561 char_attr = search_hl.attr;
5562 cur = wp->w_match_head;
5563 shl_flag = FALSE;
5564 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005565 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005566 if (shl_flag == FALSE
5567 && ((cur != NULL
5568 && cur->priority > SEARCH_HL_PRIORITY)
5569 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005570 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005571 shl = &search_hl;
5572 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005573 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005574 else
5575 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005576 if ((ptr - line) - 1 == (long)shl->startcol
5577 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005578 char_attr = shl->attr;
5579 if (shl != &search_hl && cur != NULL)
5580 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583#endif
5584 ScreenAttrs[off] = char_attr;
5585#ifdef FEAT_RIGHTLEFT
5586 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005589 --off;
5590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 else
5592#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005595 ++off;
5596 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005597 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005598#ifdef FEAT_SYN_HL
5599 eol_hl_off = 1;
5600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
Bram Moolenaar91170f82006-05-05 21:15:17 +00005604 /*
5605 * At end of the text line.
5606 */
5607 if (c == NUL)
5608 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005609#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005610 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005611 if (wp->w_p_wrap)
5612 v = wp->w_skipcol;
5613 else
5614 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005615
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005616 /* check if line ends before left margin */
5617 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005618 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005619#ifdef FEAT_CONCEAL
5620 /* Get rid of the boguscols now, we want to draw until the right
5621 * edge for 'cursorcolumn'. */
5622 col -= boguscols;
5623 boguscols = 0;
5624#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005625
5626 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005627 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005628
5629 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005630 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5631 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005632 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005633 && lnum != wp->w_cursor.lnum)
5634 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005635# ifdef FEAT_RIGHTLEFT
5636 && !wp->w_p_rl
5637# endif
5638 )
5639 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005640 int rightmost_vcol = 0;
5641 int i;
5642
5643 if (wp->w_p_cuc)
5644 rightmost_vcol = wp->w_virtcol;
5645 if (draw_color_col)
5646 /* determine rightmost colorcolumn to possibly draw */
5647 for (i = 0; color_cols[i] >= 0; ++i)
5648 if (rightmost_vcol < color_cols[i])
5649 rightmost_vcol = color_cols[i];
5650
Bram Moolenaar02631462017-09-22 15:20:32 +02005651 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005652 {
5653 ScreenLines[off] = ' ';
5654#ifdef FEAT_MBYTE
5655 if (enc_utf8)
5656 ScreenLinesUC[off] = 0;
5657#endif
5658 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005659 if (draw_color_col)
5660 draw_color_col = advance_color_col(VCOL_HLC,
5661 &color_cols);
5662
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005663 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005664 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005665 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005666 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005667 else
5668 ScreenAttrs[off++] = 0;
5669
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005670 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005671 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005672
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005673 ++vcol;
5674 }
5675 }
5676#endif
5677
Bram Moolenaar53f81742017-09-22 14:35:51 +02005678 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005679 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 row++;
5681
5682 /*
5683 * Update w_cline_height and w_cline_folded if the cursor line was
5684 * updated (saves a call to plines() later).
5685 */
5686 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5687 {
5688 curwin->w_cline_row = startrow;
5689 curwin->w_cline_height = row - startrow;
5690#ifdef FEAT_FOLDING
5691 curwin->w_cline_folded = FALSE;
5692#endif
5693 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5694 }
5695
5696 break;
5697 }
5698
5699 /* line continues beyond line end */
5700 if (lcs_ext
5701 && !wp->w_p_wrap
5702#ifdef FEAT_DIFF
5703 && filler_todo <= 0
5704#endif
5705 && (
5706#ifdef FEAT_RIGHTLEFT
5707 wp->w_p_rl ? col == 0 :
5708#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005709 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005711 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5713 {
5714 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005715 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716#ifdef FEAT_MBYTE
5717 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005718 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
5720 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005721 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005722 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724 else
5725 mb_utf8 = FALSE;
5726#endif
5727 }
5728
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005729#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005730 /* advance to the next 'colorcolumn' */
5731 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005732 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005733
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005734 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005735 * highlight the cursor position itself.
5736 * Also highlight the 'colorcolumn' if it is different than
5737 * 'cursorcolumn' */
5738 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005739 if (draw_state == WL_LINE && !lnum_in_visual_area
5740 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005741 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005742 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005743 && lnum != wp->w_cursor.lnum)
5744 {
5745 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005746 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005747 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005748 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005749 {
5750 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005751 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005752 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005753 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005754#endif
5755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 /*
5757 * Store character to be displayed.
5758 * Skip characters that are left of the screen for 'nowrap'.
5759 */
5760 vcol_prev = vcol;
5761 if (draw_state < WL_LINE || n_skip <= 0)
5762 {
5763 /*
5764 * Store the character.
5765 */
5766#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5767 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5768 {
5769 /* A double-wide character is: put first halve in left cell. */
5770 --off;
5771 --col;
5772 }
5773#endif
5774 ScreenLines[off] = c;
5775#ifdef FEAT_MBYTE
5776 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005777 {
5778 if ((mb_c & 0xff00) == 0x8e00)
5779 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 else if (enc_utf8)
5783 {
5784 if (mb_utf8)
5785 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005786 int i;
5787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005789 if ((c & 0xff) == 0)
5790 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005791 for (i = 0; i < Screen_mco; ++i)
5792 {
5793 ScreenLinesC[i][off] = u8cc[i];
5794 if (u8cc[i] == 0)
5795 break;
5796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
5798 else
5799 ScreenLinesUC[off] = 0;
5800 }
5801 if (multi_attr)
5802 {
5803 ScreenAttrs[off] = multi_attr;
5804 multi_attr = 0;
5805 }
5806 else
5807#endif
5808 ScreenAttrs[off] = char_attr;
5809
5810#ifdef FEAT_MBYTE
5811 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5812 {
5813 /* Need to fill two screen columns. */
5814 ++off;
5815 ++col;
5816 if (enc_utf8)
5817 /* UTF-8: Put a 0 in the second screen char. */
5818 ScreenLines[off] = 0;
5819 else
5820 /* DBCS: Put second byte in the second screen char. */
5821 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005822 if (draw_state > WL_NR
5823#ifdef FEAT_DIFF
5824 && filler_todo <= 0
5825#endif
5826 )
5827 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 /* When "tocol" is halfway a character, set it to the end of
5829 * the character, otherwise highlighting won't stop. */
5830 if (tocol == vcol)
5831 ++tocol;
5832#ifdef FEAT_RIGHTLEFT
5833 if (wp->w_p_rl)
5834 {
5835 /* now it's time to backup one cell */
5836 --off;
5837 --col;
5838 }
5839#endif
5840 }
5841#endif
5842#ifdef FEAT_RIGHTLEFT
5843 if (wp->w_p_rl)
5844 {
5845 --off;
5846 --col;
5847 }
5848 else
5849#endif
5850 {
5851 ++off;
5852 ++col;
5853 }
5854 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005855#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005856 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005857 {
5858 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005859 ++vcol_off;
5860 if (n_extra > 0)
5861 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005862 if (wp->w_p_wrap)
5863 {
5864 /*
5865 * Special voodoo required if 'wrap' is on.
5866 *
5867 * Advance the column indicator to force the line
5868 * drawing to wrap early. This will make the line
5869 * take up the same screen space when parts are concealed,
5870 * so that cursor line computations aren't messed up.
5871 *
5872 * To avoid the fictitious advance of 'col' causing
5873 * trailing junk to be written out of the screen line
5874 * we are building, 'boguscols' keeps track of the number
5875 * of bad columns we have advanced.
5876 */
5877 if (n_extra > 0)
5878 {
5879 vcol += n_extra;
5880# ifdef FEAT_RIGHTLEFT
5881 if (wp->w_p_rl)
5882 {
5883 col -= n_extra;
5884 boguscols -= n_extra;
5885 }
5886 else
5887# endif
5888 {
5889 col += n_extra;
5890 boguscols += n_extra;
5891 }
5892 n_extra = 0;
5893 n_attr = 0;
5894 }
5895
5896
5897# ifdef FEAT_MBYTE
5898 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5899 {
5900 /* Need to fill two screen columns. */
5901# ifdef FEAT_RIGHTLEFT
5902 if (wp->w_p_rl)
5903 {
5904 --boguscols;
5905 --col;
5906 }
5907 else
5908# endif
5909 {
5910 ++boguscols;
5911 ++col;
5912 }
5913 }
5914# endif
5915
5916# ifdef FEAT_RIGHTLEFT
5917 if (wp->w_p_rl)
5918 {
5919 --boguscols;
5920 --col;
5921 }
5922 else
5923# endif
5924 {
5925 ++boguscols;
5926 ++col;
5927 }
5928 }
5929 else
5930 {
5931 if (n_extra > 0)
5932 {
5933 vcol += n_extra;
5934 n_extra = 0;
5935 n_attr = 0;
5936 }
5937 }
5938
5939 }
5940#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 else
5942 --n_skip;
5943
Bram Moolenaar64486672010-05-16 15:46:46 +02005944 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5945 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005946 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947#ifdef FEAT_DIFF
5948 && filler_todo <= 0
5949#endif
5950 )
5951 ++vcol;
5952
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005953#ifdef FEAT_SYN_HL
5954 if (vcol_save_attr >= 0)
5955 char_attr = vcol_save_attr;
5956#endif
5957
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 /* restore attributes after "predeces" in 'listchars' */
5959 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5960 char_attr = saved_attr3;
5961
5962 /* restore attributes after last 'listchars' or 'number' char */
5963 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5964 char_attr = saved_attr2;
5965
5966 /*
5967 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005968 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 */
5970 if ((
5971#ifdef FEAT_RIGHTLEFT
5972 wp->w_p_rl ? (col < 0) :
5973#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005974 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 && (*ptr != NUL
5976#ifdef FEAT_DIFF
5977 || filler_todo > 0
5978#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005979 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5981 )
5982 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005983#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005984 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005985 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005986 boguscols = 0;
5987#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005988 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005989 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 ++row;
5992 ++screen_row;
5993
5994 /* When not wrapping and finished diff lines, or when displayed
5995 * '$' and highlighting until last column, break here. */
5996 if ((!wp->w_p_wrap
5997#ifdef FEAT_DIFF
5998 && filler_todo <= 0
5999#endif
6000 ) || lcs_eol_one == -1)
6001 break;
6002
6003 /* When the window is too narrow draw all "@" lines. */
6004 if (draw_state != WL_LINE
6005#ifdef FEAT_DIFF
6006 && filler_todo <= 0
6007#endif
6008 )
6009 {
6010 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 row = endrow;
6013 }
6014
6015 /* When line got too long for screen break here. */
6016 if (row == endrow)
6017 {
6018 ++row;
6019 break;
6020 }
6021
6022 if (screen_cur_row == screen_row - 1
6023#ifdef FEAT_DIFF
6024 && filler_todo <= 0
6025#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006026 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 {
6028 /* Remember that the line wraps, used for modeless copy. */
6029 LineWraps[screen_row - 1] = TRUE;
6030
6031 /*
6032 * Special trick to make copy/paste of wrapped lines work with
6033 * xterm/screen: write an extra character beyond the end of
6034 * the line. This will work with all terminal types
6035 * (regardless of the xn,am settings).
6036 * Only do this on a fast tty.
6037 * Only do this if the cursor is on the current line
6038 * (something has been written in it).
6039 * Don't do this for the GUI.
6040 * Don't do this for double-width characters.
6041 * Don't do this for a window not at the right screen border.
6042 */
6043 if (p_tf
6044#ifdef FEAT_GUI
6045 && !gui.in_use
6046#endif
6047#ifdef FEAT_MBYTE
6048 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006049 && ((*mb_off2cells)(LineOffset[screen_row],
6050 LineOffset[screen_row] + screen_Columns)
6051 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006053 + (int)Columns - 2,
6054 LineOffset[screen_row] + screen_Columns)
6055 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056#endif
6057 )
6058 {
6059 /* First make sure we are at the end of the screen line,
6060 * then output the same character again to let the
6061 * terminal know about the wrap. If the terminal doesn't
6062 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006063 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 screen_char(LineOffset[screen_row - 1]
6065 + (unsigned)Columns - 1,
6066 screen_row - 1, (int)(Columns - 1));
6067
6068#ifdef FEAT_MBYTE
6069 /* When there is a multi-byte character, just output a
6070 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006071 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6072 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 out_char(' ');
6074 else
6075#endif
6076 out_char(ScreenLines[LineOffset[screen_row - 1]
6077 + (Columns - 1)]);
6078 /* force a redraw of the first char on the next line */
6079 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6080 screen_start(); /* don't know where cursor is now */
6081 }
6082 }
6083
6084 col = 0;
6085 off = (unsigned)(current_ScreenLine - ScreenLines);
6086#ifdef FEAT_RIGHTLEFT
6087 if (wp->w_p_rl)
6088 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006089 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 off += col;
6091 }
6092#endif
6093
6094 /* reset the drawing state for the start of a wrapped line */
6095 draw_state = WL_START;
6096 saved_n_extra = n_extra;
6097 saved_p_extra = p_extra;
6098 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006099 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 saved_char_attr = char_attr;
6101 n_extra = 0;
6102 lcs_prec_todo = lcs_prec;
6103#ifdef FEAT_LINEBREAK
6104# ifdef FEAT_DIFF
6105 if (filler_todo <= 0)
6106# endif
6107 need_showbreak = TRUE;
6108#endif
6109#ifdef FEAT_DIFF
6110 --filler_todo;
6111 /* When the filler lines are actually below the last line of the
6112 * file, don't draw the line itself, break here. */
6113 if (filler_todo == 0 && wp->w_botfill)
6114 break;
6115#endif
6116 }
6117
6118 } /* for every character in the line */
6119
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006120#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006121 /* After an empty line check first word for capital. */
6122 if (*skipwhite(line) == NUL)
6123 {
6124 capcol_lnum = lnum + 1;
6125 cap_col = 0;
6126 }
6127#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006128#ifdef FEAT_TEXT_PROP
6129 vim_free(text_props);
6130 vim_free(text_prop_idxs);
6131#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006132
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006133 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 return row;
6135}
6136
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006137#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006138/*
6139 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006140 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006141 */
6142 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006143comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006144{
6145 int i;
6146
6147 for (i = 0; i < Screen_mco; ++i)
6148 {
6149 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6150 return TRUE;
6151 if (ScreenLinesC[i][off_from] == 0)
6152 break;
6153 }
6154 return FALSE;
6155}
6156#endif
6157
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158/*
6159 * Check whether the given character needs redrawing:
6160 * - the (first byte of the) character is different
6161 * - the attributes are different
6162 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006163 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 */
6165 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006166char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
6168 if (cols > 0
6169 && ((ScreenLines[off_from] != ScreenLines[off_to]
6170 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6171
6172#ifdef FEAT_MBYTE
6173 || (enc_dbcs != 0
6174 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6175 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6176 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6177 : (cols > 1 && ScreenLines[off_from + 1]
6178 != ScreenLines[off_to + 1])))
6179 || (enc_utf8
6180 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6181 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006182 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006183 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6184 && ScreenLines[off_from + 1]
6185 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186#endif
6187 ))
6188 return TRUE;
6189 return FALSE;
6190}
6191
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006192#if defined(FEAT_TERMINAL) || defined(PROTO)
6193/*
6194 * Return the index in ScreenLines[] for the current screen line.
6195 */
6196 int
6197screen_get_current_line_off()
6198{
6199 return (int)(current_ScreenLine - ScreenLines);
6200}
6201#endif
6202
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203/*
6204 * Move one "cooked" screen line to the screen, but only the characters that
6205 * have actually changed. Handle insert/delete character.
6206 * "coloff" gives the first column on the screen for this line.
6207 * "endcol" gives the columns where valid characters are.
6208 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6209 * needs to be cleared, negative otherwise.
6210 * "rlflag" is TRUE in a rightleft window:
6211 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6212 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6213 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006214 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006215screen_line(
6216 int row,
6217 int coloff,
6218 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006219 int clear_width,
6220 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
6222 unsigned off_from;
6223 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006224#ifdef FEAT_MBYTE
6225 unsigned max_off_from;
6226 unsigned max_off_to;
6227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 int force = FALSE; /* force update rest of the line */
6231 int redraw_this /* bool: does character need redraw? */
6232#ifdef FEAT_GUI
6233 = TRUE /* For GUI when while-loop empty */
6234#endif
6235 ;
6236 int redraw_next; /* redraw_this for next character */
6237#ifdef FEAT_MBYTE
6238 int clear_next = FALSE;
6239 int char_cells; /* 1: normal char */
6240 /* 2: occupies two display cells */
6241# define CHAR_CELLS char_cells
6242#else
6243# define CHAR_CELLS 1
6244#endif
6245
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006246 /* Check for illegal row and col, just in case. */
6247 if (row >= Rows)
6248 row = Rows - 1;
6249 if (endcol > Columns)
6250 endcol = Columns;
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252# ifdef FEAT_CLIPBOARD
6253 clip_may_clear_selection(row, row);
6254# endif
6255
6256 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6257 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006258#ifdef FEAT_MBYTE
6259 max_off_from = off_from + screen_Columns;
6260 max_off_to = LineOffset[row] + screen_Columns;
6261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
6263#ifdef FEAT_RIGHTLEFT
6264 if (rlflag)
6265 {
6266 /* Clear rest first, because it's left of the text. */
6267 if (clear_width > 0)
6268 {
6269 while (col <= endcol && ScreenLines[off_to] == ' '
6270 && ScreenAttrs[off_to] == 0
6271# ifdef FEAT_MBYTE
6272 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6273# endif
6274 )
6275 {
6276 ++off_to;
6277 ++col;
6278 }
6279 if (col <= endcol)
6280 screen_fill(row, row + 1, col + coloff,
6281 endcol + coloff + 1, ' ', ' ', 0);
6282 }
6283 col = endcol + 1;
6284 off_to = LineOffset[row] + col + coloff;
6285 off_from += col;
6286 endcol = (clear_width > 0 ? clear_width : -clear_width);
6287 }
6288#endif /* FEAT_RIGHTLEFT */
6289
6290 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6291
6292 while (col < endcol)
6293 {
6294#ifdef FEAT_MBYTE
6295 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006296 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 else
6298 char_cells = 1;
6299#endif
6300
6301 redraw_this = redraw_next;
6302 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6303 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6304
6305#ifdef FEAT_GUI
6306 /* If the next character was bold, then redraw the current character to
6307 * remove any pixels that might have spilt over into us. This only
6308 * happens in the GUI.
6309 */
6310 if (redraw_next && gui.in_use)
6311 {
6312 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006313 if (hl > HL_ALL)
6314 hl = syn_attr2attr(hl);
6315 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 redraw_this = TRUE;
6317 }
6318#endif
6319
6320 if (redraw_this)
6321 {
6322 /*
6323 * Special handling when 'xs' termcap flag set (hpterm):
6324 * Attributes for characters are stored at the position where the
6325 * cursor is when writing the highlighting code. The
6326 * start-highlighting code must be written with the cursor on the
6327 * first highlighted character. The stop-highlighting code must
6328 * be written with the cursor just after the last highlighted
6329 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006330 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 * to clear the rest of the line, and force redrawing it
6332 * completely.
6333 */
6334 if ( p_wiv
6335 && !force
6336#ifdef FEAT_GUI
6337 && !gui.in_use
6338#endif
6339 && ScreenAttrs[off_to] != 0
6340 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6341 {
6342 /*
6343 * Need to remove highlighting attributes here.
6344 */
6345 windgoto(row, col + coloff);
6346 out_str(T_CE); /* clear rest of this screen line */
6347 screen_start(); /* don't know where cursor is now */
6348 force = TRUE; /* force redraw of rest of the line */
6349 redraw_next = TRUE; /* or else next char would miss out */
6350
6351 /*
6352 * If the previous character was highlighted, need to stop
6353 * highlighting at this character.
6354 */
6355 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6356 {
6357 screen_attr = ScreenAttrs[off_to - 1];
6358 term_windgoto(row, col + coloff);
6359 screen_stop_highlight();
6360 }
6361 else
6362 screen_attr = 0; /* highlighting has stopped */
6363 }
6364#ifdef FEAT_MBYTE
6365 if (enc_dbcs != 0)
6366 {
6367 /* Check if overwriting a double-byte with a single-byte or
6368 * the other way around requires another character to be
6369 * redrawn. For UTF-8 this isn't needed, because comparing
6370 * ScreenLinesUC[] is sufficient. */
6371 if (char_cells == 1
6372 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006373 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
6375 /* Writing a single-cell character over a double-cell
6376 * character: need to redraw the next cell. */
6377 ScreenLines[off_to + 1] = 0;
6378 redraw_next = TRUE;
6379 }
6380 else if (char_cells == 2
6381 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006382 && (*mb_off2cells)(off_to, max_off_to) == 1
6383 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 {
6385 /* Writing the second half of a double-cell character over
6386 * a double-cell character: need to redraw the second
6387 * cell. */
6388 ScreenLines[off_to + 2] = 0;
6389 redraw_next = TRUE;
6390 }
6391
6392 if (enc_dbcs == DBCS_JPNU)
6393 ScreenLines2[off_to] = ScreenLines2[off_from];
6394 }
6395 /* When writing a single-width character over a double-width
6396 * character and at the end of the redrawn text, need to clear out
6397 * the right halve of the old character.
6398 * Also required when writing the right halve of a double-width
6399 * char over the left halve of an existing one. */
6400 if (has_mbyte && col + char_cells == endcol
6401 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006402 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006404 && (*mb_off2cells)(off_to, max_off_to) == 1
6405 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 clear_next = TRUE;
6407#endif
6408
6409 ScreenLines[off_to] = ScreenLines[off_from];
6410#ifdef FEAT_MBYTE
6411 if (enc_utf8)
6412 {
6413 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6414 if (ScreenLinesUC[off_from] != 0)
6415 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006416 int i;
6417
6418 for (i = 0; i < Screen_mco; ++i)
6419 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 }
6421 }
6422 if (char_cells == 2)
6423 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6424#endif
6425
6426#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006427 /* The bold trick makes a single column of pixels appear in the
6428 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 * character should be redrawn too. This happens for our own GUI
6430 * and for some xterms. */
6431 if (
6432# ifdef FEAT_GUI
6433 gui.in_use
6434# endif
6435# if defined(FEAT_GUI) && defined(UNIX)
6436 ||
6437# endif
6438# ifdef UNIX
6439 term_is_xterm
6440# endif
6441 )
6442 {
6443 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006444 if (hl > HL_ALL)
6445 hl = syn_attr2attr(hl);
6446 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 redraw_next = TRUE;
6448 }
6449#endif
6450 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6451#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006452 /* For simplicity set the attributes of second half of a
6453 * double-wide character equal to the first half. */
6454 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006456
6457 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 else
6460#endif
6461 screen_char(off_to, row, col + coloff);
6462 }
6463 else if ( p_wiv
6464#ifdef FEAT_GUI
6465 && !gui.in_use
6466#endif
6467 && col + coloff > 0)
6468 {
6469 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6470 {
6471 /*
6472 * Don't output stop-highlight when moving the cursor, it will
6473 * stop the highlighting when it should continue.
6474 */
6475 screen_attr = 0;
6476 }
6477 else if (screen_attr != 0)
6478 screen_stop_highlight();
6479 }
6480
6481 off_to += CHAR_CELLS;
6482 off_from += CHAR_CELLS;
6483 col += CHAR_CELLS;
6484 }
6485
6486#ifdef FEAT_MBYTE
6487 if (clear_next)
6488 {
6489 /* Clear the second half of a double-wide character of which the left
6490 * half was overwritten with a single-wide character. */
6491 ScreenLines[off_to] = ' ';
6492 if (enc_utf8)
6493 ScreenLinesUC[off_to] = 0;
6494 screen_char(off_to, row, col + coloff);
6495 }
6496#endif
6497
6498 if (clear_width > 0
6499#ifdef FEAT_RIGHTLEFT
6500 && !rlflag
6501#endif
6502 )
6503 {
6504#ifdef FEAT_GUI
6505 int startCol = col;
6506#endif
6507
6508 /* blank out the rest of the line */
6509 while (col < clear_width && ScreenLines[off_to] == ' '
6510 && ScreenAttrs[off_to] == 0
6511#ifdef FEAT_MBYTE
6512 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6513#endif
6514 )
6515 {
6516 ++off_to;
6517 ++col;
6518 }
6519 if (col < clear_width)
6520 {
6521#ifdef FEAT_GUI
6522 /*
6523 * In the GUI, clearing the rest of the line may leave pixels
6524 * behind if the first character cleared was bold. Some bold
6525 * fonts spill over the left. In this case we redraw the previous
6526 * character too. If we didn't skip any blanks above, then we
6527 * only redraw if the character wasn't already redrawn anyway.
6528 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006529 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 {
6531 hl = ScreenAttrs[off_to];
6532 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006533 {
6534 int prev_cells = 1;
6535# ifdef FEAT_MBYTE
6536 if (enc_utf8)
6537 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6538 * that its width is 2. */
6539 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6540 else if (enc_dbcs != 0)
6541 {
6542 /* find previous character by counting from first
6543 * column and get its width. */
6544 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006545 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006546
6547 while (off < off_to)
6548 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006549 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006550 off += prev_cells;
6551 }
6552 }
6553
6554 if (enc_dbcs != 0 && prev_cells > 1)
6555 screen_char_2(off_to - prev_cells, row,
6556 col + coloff - prev_cells);
6557 else
6558# endif
6559 screen_char(off_to - prev_cells, row,
6560 col + coloff - prev_cells);
6561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
6563#endif
6564 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6565 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 off_to += clear_width - col;
6567 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
6569 }
6570
6571 if (clear_width > 0)
6572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 /* For a window that's left of another, draw the separator char. */
6574 if (col + coloff < Columns)
6575 {
6576 int c;
6577
6578 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006579 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006580#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006581 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6582 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 || ScreenAttrs[off_to] != hl)
6585 {
6586 ScreenLines[off_to] = c;
6587 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006588#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 if (enc_utf8)
6590 {
6591 if (c >= 0x80)
6592 {
6593 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006594 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
6596 else
6597 ScreenLinesUC[off_to] = 0;
6598 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 screen_char(off_to, row, col + coloff);
6601 }
6602 }
6603 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 LineWraps[row] = FALSE;
6605 }
6606}
6607
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006608#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006610 * Mirror text "str" for right-left displaying.
6611 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006613 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006614rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 char_u *p1, *p2;
6617 int t;
6618
6619 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6620 {
6621 t = *p1;
6622 *p1 = *p2;
6623 *p2 = t;
6624 }
6625}
6626#endif
6627
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628/*
6629 * mark all status lines for redraw; used after first :cd
6630 */
6631 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006632status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633{
6634 win_T *wp;
6635
Bram Moolenaar29323592016-07-24 22:04:11 +02006636 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 if (wp->w_status_height)
6638 {
6639 wp->w_redr_status = TRUE;
6640 redraw_later(VALID);
6641 }
6642}
6643
6644/*
6645 * mark all status lines of the current buffer for redraw
6646 */
6647 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006648status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649{
6650 win_T *wp;
6651
Bram Moolenaar29323592016-07-24 22:04:11 +02006652 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6654 {
6655 wp->w_redr_status = TRUE;
6656 redraw_later(VALID);
6657 }
6658}
6659
6660/*
6661 * Redraw all status lines that need to be redrawn.
6662 */
6663 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006664redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 win_T *wp;
6667
Bram Moolenaar29323592016-07-24 22:04:11 +02006668 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006670 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006671 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006672 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
Bram Moolenaar4033c552017-09-16 20:54:51 +02006675#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676/*
6677 * Redraw all status lines at the bottom of frame "frp".
6678 */
6679 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006680win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
6682 if (frp->fr_layout == FR_LEAF)
6683 frp->fr_win->w_redr_status = TRUE;
6684 else if (frp->fr_layout == FR_ROW)
6685 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006686 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 win_redraw_last_status(frp);
6688 }
6689 else /* frp->fr_layout == FR_COL */
6690 {
6691 frp = frp->fr_child;
6692 while (frp->fr_next != NULL)
6693 frp = frp->fr_next;
6694 win_redraw_last_status(frp);
6695 }
6696}
6697#endif
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699/*
6700 * Draw the verticap separator right of window "wp" starting with line "row".
6701 */
6702 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006703draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
6705 int hl;
6706 int c;
6707
6708 if (wp->w_vsep_width)
6709 {
6710 /* draw the vertical separator right of this window */
6711 c = fillchar_vsep(&hl);
6712 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6713 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6714 c, ' ', hl);
6715 }
6716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
6718#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006719static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006722 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 */
6724 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006725status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 int len = 0;
6728
6729#ifdef FEAT_MENU
6730 int emenu = (xp->xp_context == EXPAND_MENUS
6731 || xp->xp_context == EXPAND_MENUNAMES);
6732
6733 /* Check for menu separators - replace with '|'. */
6734 if (emenu && menu_is_separator(s))
6735 return 1;
6736#endif
6737
6738 while (*s != NUL)
6739 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006740 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006741 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006742 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744
6745 return len;
6746}
6747
6748/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006749 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006750 * These are backslashes used for escaping. Do show backslashes in help tags.
6751 */
6752 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006753skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006754{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006755 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006756#ifdef FEAT_MENU
6757 || ((xp->xp_context == EXPAND_MENUS
6758 || xp->xp_context == EXPAND_MENUNAMES)
6759 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6760#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006761 )
6762 {
6763#ifndef BACKSLASH_IN_FILENAME
6764 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6765 return 2;
6766#endif
6767 return 1;
6768 }
6769 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006770}
6771
6772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 * Show wildchar matches in the status line.
6774 * Show at least the "match" item.
6775 * We start at item 'first_match' in the list and show all matches that fit.
6776 *
6777 * If inversion is possible we use it. Else '=' characters are used.
6778 */
6779 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006780win_redr_status_matches(
6781 expand_T *xp,
6782 int num_matches,
6783 char_u **matches, /* list of matches */
6784 int match,
6785 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786{
6787#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6788 int row;
6789 char_u *buf;
6790 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006791 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 int fillchar;
6793 int attr;
6794 int i;
6795 int highlight = TRUE;
6796 char_u *selstart = NULL;
6797 int selstart_col = 0;
6798 char_u *selend = NULL;
6799 static int first_match = 0;
6800 int add_left = FALSE;
6801 char_u *s;
6802#ifdef FEAT_MENU
6803 int emenu;
6804#endif
6805#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6806 int l;
6807#endif
6808
6809 if (matches == NULL) /* interrupted completion? */
6810 return;
6811
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006812#ifdef FEAT_MBYTE
6813 if (has_mbyte)
6814 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6815 else
6816#endif
6817 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (buf == NULL)
6819 return;
6820
6821 if (match == -1) /* don't show match but original text */
6822 {
6823 match = 0;
6824 highlight = FALSE;
6825 }
6826 /* count 1 for the ending ">" */
6827 clen = status_match_len(xp, L_MATCH(match)) + 3;
6828 if (match == 0)
6829 first_match = 0;
6830 else if (match < first_match)
6831 {
6832 /* jumping left, as far as we can go */
6833 first_match = match;
6834 add_left = TRUE;
6835 }
6836 else
6837 {
6838 /* check if match fits on the screen */
6839 for (i = first_match; i < match; ++i)
6840 clen += status_match_len(xp, L_MATCH(i)) + 2;
6841 if (first_match > 0)
6842 clen += 2;
6843 /* jumping right, put match at the left */
6844 if ((long)clen > Columns)
6845 {
6846 first_match = match;
6847 /* if showing the last match, we can add some on the left */
6848 clen = 2;
6849 for (i = match; i < num_matches; ++i)
6850 {
6851 clen += status_match_len(xp, L_MATCH(i)) + 2;
6852 if ((long)clen >= Columns)
6853 break;
6854 }
6855 if (i == num_matches)
6856 add_left = TRUE;
6857 }
6858 }
6859 if (add_left)
6860 while (first_match > 0)
6861 {
6862 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6863 if ((long)clen >= Columns)
6864 break;
6865 --first_match;
6866 }
6867
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006868 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869
6870 if (first_match == 0)
6871 {
6872 *buf = NUL;
6873 len = 0;
6874 }
6875 else
6876 {
6877 STRCPY(buf, "< ");
6878 len = 2;
6879 }
6880 clen = len;
6881
6882 i = first_match;
6883 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6884 {
6885 if (i == match)
6886 {
6887 selstart = buf + len;
6888 selstart_col = clen;
6889 }
6890
6891 s = L_MATCH(i);
6892 /* Check for menu separators - replace with '|' */
6893#ifdef FEAT_MENU
6894 emenu = (xp->xp_context == EXPAND_MENUS
6895 || xp->xp_context == EXPAND_MENUNAMES);
6896 if (emenu && menu_is_separator(s))
6897 {
6898 STRCPY(buf + len, transchar('|'));
6899 l = (int)STRLEN(buf + len);
6900 len += l;
6901 clen += l;
6902 }
6903 else
6904#endif
6905 for ( ; *s != NUL; ++s)
6906 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006907 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 clen += ptr2cells(s);
6909#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006910 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {
6912 STRNCPY(buf + len, s, l);
6913 s += l - 1;
6914 len += l;
6915 }
6916 else
6917#endif
6918 {
6919 STRCPY(buf + len, transchar_byte(*s));
6920 len += (int)STRLEN(buf + len);
6921 }
6922 }
6923 if (i == match)
6924 selend = buf + len;
6925
6926 *(buf + len++) = ' ';
6927 *(buf + len++) = ' ';
6928 clen += 2;
6929 if (++i == num_matches)
6930 break;
6931 }
6932
6933 if (i != num_matches)
6934 {
6935 *(buf + len++) = '>';
6936 ++clen;
6937 }
6938
6939 buf[len] = NUL;
6940
6941 row = cmdline_row - 1;
6942 if (row >= 0)
6943 {
6944 if (wild_menu_showing == 0)
6945 {
6946 if (msg_scrolled > 0)
6947 {
6948 /* Put the wildmenu just above the command line. If there is
6949 * no room, scroll the screen one line up. */
6950 if (cmdline_row == Rows - 1)
6951 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006952 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 ++msg_scrolled;
6954 }
6955 else
6956 {
6957 ++cmdline_row;
6958 ++row;
6959 }
6960 wild_menu_showing = WM_SCROLLED;
6961 }
6962 else
6963 {
6964 /* Create status line if needed by setting 'laststatus' to 2.
6965 * Set 'winminheight' to zero to avoid that the window is
6966 * resized. */
6967 if (lastwin->w_status_height == 0)
6968 {
6969 save_p_ls = p_ls;
6970 save_p_wmh = p_wmh;
6971 p_ls = 2;
6972 p_wmh = 0;
6973 last_status(FALSE);
6974 }
6975 wild_menu_showing = WM_SHOWN;
6976 }
6977 }
6978
6979 screen_puts(buf, row, 0, attr);
6980 if (selstart != NULL && highlight)
6981 {
6982 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006983 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
6985
6986 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6987 }
6988
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 vim_free(buf);
6991}
6992#endif
6993
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994/*
6995 * Redraw the status line of window wp.
6996 *
6997 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006998 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6999 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007001 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007002win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003{
7004 int row;
7005 char_u *p;
7006 int len;
7007 int fillchar;
7008 int attr;
7009 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007010 static int busy = FALSE;
7011
7012 /* It's possible to get here recursively when 'statusline' (indirectly)
7013 * invokes ":redrawstatus". Simply ignore the call then. */
7014 if (busy)
7015 return;
7016 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
7018 wp->w_redr_status = FALSE;
7019 if (wp->w_status_height == 0)
7020 {
7021 /* no status line, can only be last window */
7022 redraw_cmdline = TRUE;
7023 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007024 else if (!redrawing()
7025#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007026 // don't update status line when popup menu is visible and may be
7027 // drawn over it, unless it will be redrawn later
7028 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007029#endif
7030 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {
7032 /* Don't redraw right now, do it later. */
7033 wp->w_redr_status = TRUE;
7034 }
7035#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007036 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
7038 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007039 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
7041#endif
7042 else
7043 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007044 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007046 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 p = NameBuff;
7048 len = (int)STRLEN(p);
7049
Bram Moolenaard85f2712017-07-28 21:51:57 +02007050 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051#ifdef FEAT_QUICKFIX
7052 || wp->w_p_pvw
7053#endif
7054 || bufIsChanged(wp->w_buffer)
7055 || wp->w_buffer->b_p_ro)
7056 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007057 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007059 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 len += (int)STRLEN(p + len);
7061 }
7062#ifdef FEAT_QUICKFIX
7063 if (wp->w_p_pvw)
7064 {
7065 STRCPY(p + len, _("[Preview]"));
7066 len += (int)STRLEN(p + len);
7067 }
7068#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007069 if (bufIsChanged(wp->w_buffer)
7070#ifdef FEAT_TERMINAL
7071 && !bt_terminal(wp->w_buffer)
7072#endif
7073 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {
7075 STRCPY(p + len, "[+]");
7076 len += 3;
7077 }
7078 if (wp->w_buffer->b_p_ro)
7079 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007080 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007081 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 }
7083
Bram Moolenaar02631462017-09-22 15:20:32 +02007084 this_ru_col = ru_col - (Columns - wp->w_width);
7085 if (this_ru_col < (wp->w_width + 1) / 2)
7086 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 if (this_ru_col <= 1)
7088 {
7089 p = (char_u *)"<"; /* No room for file name! */
7090 len = 1;
7091 }
7092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093#ifdef FEAT_MBYTE
7094 if (has_mbyte)
7095 {
7096 int clen = 0, i;
7097
7098 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007099 clen = mb_string2cells(p, -1);
7100
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 /* Find first character that will fit.
7102 * Going from start to end is much faster for DBCS. */
7103 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007104 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 clen -= (*mb_ptr2cells)(p + i);
7106 len = clen;
7107 if (i > 0)
7108 {
7109 p = p + i - 1;
7110 *p = '<';
7111 ++len;
7112 }
7113
7114 }
7115 else
7116#endif
7117 if (len > this_ru_col - 1)
7118 {
7119 p += len - (this_ru_col - 1);
7120 *p = '<';
7121 len = this_ru_col - 1;
7122 }
7123
7124 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007125 screen_puts(p, row, wp->w_wincol, attr);
7126 screen_fill(row, row + 1, len + wp->w_wincol,
7127 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007129 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7131 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007132 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133
7134#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007135 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136#endif
7137 }
7138
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 /*
7140 * May need to draw the character below the vertical separator.
7141 */
7142 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7143 {
7144 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007145 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 else
7147 fillchar = fillchar_vsep(&attr);
7148 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7149 attr);
7150 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007151 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152}
7153
Bram Moolenaar238a5642006-02-21 22:12:05 +00007154#ifdef FEAT_STL_OPT
7155/*
7156 * Redraw the status line according to 'statusline' and take care of any
7157 * errors encountered.
7158 */
7159 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007160redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007161{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007162 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007163 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007164
7165 /* When called recursively return. This can happen when the statusline
7166 * contains an expression that triggers a redraw. */
7167 if (entered)
7168 return;
7169 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007170
Bram Moolenaara742e082016-04-05 21:10:38 +02007171 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007172 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007173 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007174 {
7175 /* When there is an error disable the statusline, otherwise the
7176 * display is messed up with errors and a redraw triggers the problem
7177 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007178 set_string_option_direct((char_u *)"statusline", -1,
7179 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007180 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007181 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007182 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007183 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007184}
7185#endif
7186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187/*
7188 * Return TRUE if the status line of window "wp" is connected to the status
7189 * line of the window right of it. If not, then it's a vertical separator.
7190 * Only call if (wp->w_vsep_width != 0).
7191 */
7192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007193stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 frame_T *fr;
7196
7197 fr = wp->w_frame;
7198 while (fr->fr_parent != NULL)
7199 {
7200 if (fr->fr_parent->fr_layout == FR_COL)
7201 {
7202 if (fr->fr_next != NULL)
7203 break;
7204 }
7205 else
7206 {
7207 if (fr->fr_next != NULL)
7208 return TRUE;
7209 }
7210 fr = fr->fr_parent;
7211 }
7212 return FALSE;
7213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216/*
7217 * Get the value to show for the language mappings, active 'keymap'.
7218 */
7219 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007220get_keymap_str(
7221 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007222 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007223 char_u *buf, /* buffer for the result */
7224 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
7226 char_u *p;
7227
7228 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7229 return FALSE;
7230
7231 {
7232#ifdef FEAT_EVAL
7233 buf_T *old_curbuf = curbuf;
7234 win_T *old_curwin = curwin;
7235 char_u *s;
7236
7237 curbuf = wp->w_buffer;
7238 curwin = wp;
7239 STRCPY(buf, "b:keymap_name"); /* must be writable */
7240 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007241 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 --emsg_skip;
7243 curbuf = old_curbuf;
7244 curwin = old_curwin;
7245 if (p == NULL || *p == NUL)
7246#endif
7247 {
7248#ifdef FEAT_KEYMAP
7249 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7250 p = wp->w_buffer->b_p_keymap;
7251 else
7252#endif
7253 p = (char_u *)"lang";
7254 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007255 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 buf[0] = NUL;
7257#ifdef FEAT_EVAL
7258 vim_free(s);
7259#endif
7260 }
7261 return buf[0] != NUL;
7262}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264#if defined(FEAT_STL_OPT) || defined(PROTO)
7265/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007266 * Redraw the status line or ruler of window "wp".
7267 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 */
7269 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007270win_redr_custom(
7271 win_T *wp,
7272 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007274 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 int attr;
7276 int curattr;
7277 int row;
7278 int col = 0;
7279 int maxwidth;
7280 int width;
7281 int n;
7282 int len;
7283 int fillchar;
7284 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007285 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007287 struct stl_hlrec hltab[STL_MAX_ITEM];
7288 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007289 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007290 win_T *ewp;
7291 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
Bram Moolenaar1d633412013-12-11 15:52:01 +01007293 /* There is a tiny chance that this gets called recursively: When
7294 * redrawing a status line triggers redrawing the ruler or tabline.
7295 * Avoid trouble by not allowing recursion. */
7296 if (entered)
7297 return;
7298 entered = TRUE;
7299
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007301 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007303 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007304 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007305 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007306 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007307 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007308 maxwidth = Columns;
7309# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007310 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007313 else
7314 {
7315 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007316 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007317 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007318
7319 if (draw_ruler)
7320 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007321 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007322 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007323 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007324 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007325 if (*++stl == '-')
7326 stl++;
7327 if (atoi((char *)stl))
7328 while (VIM_ISDIGIT(*stl))
7329 stl++;
7330 if (*stl++ != '(')
7331 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007332 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007333 col = ru_col - (Columns - wp->w_width);
7334 if (col < (wp->w_width + 1) / 2)
7335 col = (wp->w_width + 1) / 2;
7336 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007337 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007338 {
7339 row = Rows - 1;
7340 --maxwidth; /* writing in last column may cause scrolling */
7341 fillchar = ' ';
7342 attr = 0;
7343 }
7344
7345# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007346 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007347# endif
7348 }
7349 else
7350 {
7351 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007352 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007353 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007354 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007355# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007356 use_sandbox = was_set_insecurely((char_u *)"statusline",
7357 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007358# endif
7359 }
7360
Bram Moolenaar53f81742017-09-22 14:35:51 +02007361 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007362 }
7363
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007365 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
Bram Moolenaar61452852011-02-01 18:01:11 +01007367 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7368 * the cursor away and back. */
7369 ewp = wp == NULL ? curwin : wp;
7370 p_crb_save = ewp->w_p_crb;
7371 ewp->w_p_crb = FALSE;
7372
Bram Moolenaar362f3562009-11-03 16:20:34 +00007373 /* Make a copy, because the statusline may include a function call that
7374 * might change the option value and free the memory. */
7375 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007376 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007377 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007378 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007379 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007380 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007382 /* Make all characters printable. */
7383 p = transstr(buf);
7384 if (p != NULL)
7385 {
7386 vim_strncpy(buf, p, sizeof(buf) - 1);
7387 vim_free(p);
7388 }
7389
7390 /* fill up with "fillchar" */
7391 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007392 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
7394#ifdef FEAT_MBYTE
7395 len += (*mb_char2bytes)(fillchar, buf + len);
7396#else
7397 buf[len++] = fillchar;
7398#endif
7399 ++width;
7400 }
7401 buf[len] = NUL;
7402
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007403 /*
7404 * Draw each snippet with the specified highlighting.
7405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 curattr = attr;
7407 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007408 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007410 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 screen_puts_len(p, len, row, col, curattr);
7412 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007413 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007415 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007417 else if (hltab[n].userhl < 0)
7418 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007419#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007420 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7421 && wp->w_status_height != 0)
7422 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007423 else if (wp != NULL && bt_terminal(wp->w_buffer)
7424 && wp->w_status_height != 0)
7425 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007426#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007427 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007428 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007430 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007433
7434 if (wp == NULL)
7435 {
7436 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7437 col = 0;
7438 len = 0;
7439 p = buf;
7440 fillchar = 0;
7441 for (n = 0; tabtab[n].start != NULL; n++)
7442 {
7443 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7444 while (col < len)
7445 TabPageIdxs[col++] = fillchar;
7446 p = tabtab[n].start;
7447 fillchar = tabtab[n].userhl;
7448 }
7449 while (col < Columns)
7450 TabPageIdxs[col++] = fillchar;
7451 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007452
7453theend:
7454 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455}
7456
7457#endif /* FEAT_STL_OPT */
7458
7459/*
7460 * Output a single character directly to the screen and update ScreenLines.
7461 */
7462 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007463screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 char_u buf[MB_MAXBYTES + 1];
7466
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007467#ifdef FEAT_MBYTE
7468 if (has_mbyte)
7469 buf[(*mb_char2bytes)(c, buf)] = NUL;
7470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007472 {
7473 buf[0] = c;
7474 buf[1] = NUL;
7475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 screen_puts(buf, row, col, attr);
7477}
7478
7479/*
7480 * Get a single character directly from ScreenLines into "bytes[]".
7481 * Also return its attribute in *attrp;
7482 */
7483 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007484screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 unsigned off;
7487
7488 /* safety check */
7489 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7490 {
7491 off = LineOffset[row] + col;
7492 *attrp = ScreenAttrs[off];
7493 bytes[0] = ScreenLines[off];
7494 bytes[1] = NUL;
7495
7496#ifdef FEAT_MBYTE
7497 if (enc_utf8 && ScreenLinesUC[off] != 0)
7498 bytes[utfc_char2bytes(off, bytes)] = NUL;
7499 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7500 {
7501 bytes[0] = ScreenLines[off];
7502 bytes[1] = ScreenLines2[off];
7503 bytes[2] = NUL;
7504 }
7505 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7506 {
7507 bytes[1] = ScreenLines[off + 1];
7508 bytes[2] = NUL;
7509 }
7510#endif
7511 }
7512}
7513
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007514#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007515/*
7516 * Return TRUE if composing characters for screen posn "off" differs from
7517 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007518 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007519 */
7520 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007521screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007522{
7523 int i;
7524
7525 for (i = 0; i < Screen_mco; ++i)
7526 {
7527 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7528 return TRUE;
7529 if (u8cc[i] == 0)
7530 break;
7531 }
7532 return FALSE;
7533}
7534#endif
7535
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536/*
7537 * Put string '*text' on the screen at position 'row' and 'col', with
7538 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7539 * Note: only outputs within one row, message is truncated at screen boundary!
7540 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7541 */
7542 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007543screen_puts(
7544 char_u *text,
7545 int row,
7546 int col,
7547 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
7549 screen_puts_len(text, -1, row, col, attr);
7550}
7551
7552/*
7553 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7554 * a NUL.
7555 */
7556 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007557screen_puts_len(
7558 char_u *text,
7559 int textlen,
7560 int row,
7561 int col,
7562 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563{
7564 unsigned off;
7565 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007566 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 int c;
7568#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007569 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 int mbyte_blen = 1;
7571 int mbyte_cells = 1;
7572 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007573 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 int clear_next_cell = FALSE;
7575# ifdef FEAT_ARABIC
7576 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007577 int pc, nc, nc1;
7578 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579# endif
7580#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007581#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7582 int force_redraw_this;
7583 int force_redraw_next = FALSE;
7584#endif
7585 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586
7587 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7588 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007589 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590
Bram Moolenaarc236c162008-07-13 17:41:49 +00007591#ifdef FEAT_MBYTE
7592 /* When drawing over the right halve of a double-wide char clear out the
7593 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007594 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007595# ifdef FEAT_GUI
7596 && !gui.in_use
7597# endif
7598 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007599 {
7600 ScreenLines[off - 1] = ' ';
7601 ScreenAttrs[off - 1] = 0;
7602 if (enc_utf8)
7603 {
7604 ScreenLinesUC[off - 1] = 0;
7605 ScreenLinesC[0][off - 1] = 0;
7606 }
7607 /* redraw the previous cell, make it empty */
7608 screen_char(off - 1, row, col - 1);
7609 /* force the cell at "col" to be redrawn */
7610 force_redraw_next = TRUE;
7611 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007612#endif
7613
Bram Moolenaar367329b2007-08-30 11:53:22 +00007614#ifdef FEAT_MBYTE
7615 max_off = LineOffset[row] + screen_Columns;
7616#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007617 while (col < screen_Columns
7618 && (len < 0 || (int)(ptr - text) < len)
7619 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 {
7621 c = *ptr;
7622#ifdef FEAT_MBYTE
7623 /* check if this is the first byte of a multibyte */
7624 if (has_mbyte)
7625 {
7626 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007627 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007629 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7631 mbyte_cells = 1;
7632 else if (enc_dbcs != 0)
7633 mbyte_cells = mbyte_blen;
7634 else /* enc_utf8 */
7635 {
7636 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007637 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 (int)((text + len) - ptr));
7639 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007640 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007642# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 /* Non-BMP character: display as ? or fullwidth ?. */
7644 if (u8c >= 0x10000)
7645 {
7646 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7647 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007648 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651# ifdef FEAT_ARABIC
7652 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7653 {
7654 /* Do Arabic shaping. */
7655 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7656 {
7657 /* Past end of string to be displayed. */
7658 nc = NUL;
7659 nc1 = NUL;
7660 }
7661 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007662 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007663 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7664 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007665 nc1 = pcc[0];
7666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 pc = prev_c;
7668 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007669 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
7671 else
7672 prev_c = u8c;
7673# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007674 if (col + mbyte_cells > screen_Columns)
7675 {
7676 /* Only 1 cell left, but character requires 2 cells:
7677 * display a '>' in the last column to avoid wrapping. */
7678 c = '>';
7679 mbyte_cells = 1;
7680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
7682 }
7683#endif
7684
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007685#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7686 force_redraw_this = force_redraw_next;
7687 force_redraw_next = FALSE;
7688#endif
7689
7690 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691#ifdef FEAT_MBYTE
7692 || (mbyte_cells == 2
7693 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7694 || (enc_dbcs == DBCS_JPNU
7695 && c == 0x8e
7696 && ScreenLines2[off] != ptr[1])
7697 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007698 && (ScreenLinesUC[off] !=
7699 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7700 || (ScreenLinesUC[off] != 0
7701 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702#endif
7703 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007704 || exmode_active;
7705
7706 if (need_redraw
7707#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7708 || force_redraw_this
7709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 )
7711 {
7712#if defined(FEAT_GUI) || defined(UNIX)
7713 /* The bold trick makes a single row of pixels appear in the next
7714 * character. When a bold character is removed, the next
7715 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007716 * and for some xterms. */
7717 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718# ifdef FEAT_GUI
7719 gui.in_use
7720# endif
7721# if defined(FEAT_GUI) && defined(UNIX)
7722 ||
7723# endif
7724# ifdef UNIX
7725 term_is_xterm
7726# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007727 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007729 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007731 if (n > HL_ALL)
7732 n = syn_attr2attr(n);
7733 if (n & HL_BOLD)
7734 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 }
7736#endif
7737#ifdef FEAT_MBYTE
7738 /* When at the end of the text and overwriting a two-cell
7739 * character with a one-cell character, need to clear the next
7740 * cell. Also when overwriting the left halve of a two-cell char
7741 * with the right halve of a two-cell char. Do this only once
7742 * (mb_off2cells() may return 2 on the right halve). */
7743 if (clear_next_cell)
7744 clear_next_cell = FALSE;
7745 else if (has_mbyte
7746 && (len < 0 ? ptr[mbyte_blen] == NUL
7747 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007748 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007750 && (*mb_off2cells)(off, max_off) == 1
7751 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 clear_next_cell = TRUE;
7753
7754 /* Make sure we never leave a second byte of a double-byte behind,
7755 * it confuses mb_off2cells(). */
7756 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007757 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007759 && (*mb_off2cells)(off, max_off) == 1
7760 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 ScreenLines[off + mbyte_blen] = 0;
7762#endif
7763 ScreenLines[off] = c;
7764 ScreenAttrs[off] = attr;
7765#ifdef FEAT_MBYTE
7766 if (enc_utf8)
7767 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007768 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 ScreenLinesUC[off] = 0;
7770 else
7771 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007772 int i;
7773
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007775 for (i = 0; i < Screen_mco; ++i)
7776 {
7777 ScreenLinesC[i][off] = u8cc[i];
7778 if (u8cc[i] == 0)
7779 break;
7780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 }
7782 if (mbyte_cells == 2)
7783 {
7784 ScreenLines[off + 1] = 0;
7785 ScreenAttrs[off + 1] = attr;
7786 }
7787 screen_char(off, row, col);
7788 }
7789 else if (mbyte_cells == 2)
7790 {
7791 ScreenLines[off + 1] = ptr[1];
7792 ScreenAttrs[off + 1] = attr;
7793 screen_char_2(off, row, col);
7794 }
7795 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7796 {
7797 ScreenLines2[off] = ptr[1];
7798 screen_char(off, row, col);
7799 }
7800 else
7801#endif
7802 screen_char(off, row, col);
7803 }
7804#ifdef FEAT_MBYTE
7805 if (has_mbyte)
7806 {
7807 off += mbyte_cells;
7808 col += mbyte_cells;
7809 ptr += mbyte_blen;
7810 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007811 {
7812 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007814 len = -1;
7815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 }
7817 else
7818#endif
7819 {
7820 ++off;
7821 ++col;
7822 ++ptr;
7823 }
7824 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007825
7826#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7827 /* If we detected the next character needs to be redrawn, but the text
7828 * doesn't extend up to there, update the character here. */
7829 if (force_redraw_next && col < screen_Columns)
7830 {
7831# ifdef FEAT_MBYTE
7832 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7833 screen_char_2(off, row, col);
7834 else
7835# endif
7836 screen_char(off, row, col);
7837 }
7838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839}
7840
7841#ifdef FEAT_SEARCH_EXTRA
7842/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007843 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 */
7845 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007846start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 if (p_hls && !no_hlsearch)
7849 {
7850 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007851 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007852# ifdef FEAT_RELTIME
7853 /* Set the time limit to 'redrawtime'. */
7854 profile_setlimit(p_rdt, &search_hl.tm);
7855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
7857}
7858
7859/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007860 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 */
7862 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007863end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 if (search_hl.rm.regprog != NULL)
7866 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007867 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 search_hl.rm.regprog = NULL;
7869 }
7870}
7871
7872/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007873 * Init for calling prepare_search_hl().
7874 */
7875 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007876init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007877{
7878 matchitem_T *cur;
7879
7880 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7881 * match */
7882 cur = wp->w_match_head;
7883 while (cur != NULL)
7884 {
7885 cur->hl.rm = cur->match;
7886 if (cur->hlg_id == 0)
7887 cur->hl.attr = 0;
7888 else
7889 cur->hl.attr = syn_id2attr(cur->hlg_id);
7890 cur->hl.buf = wp->w_buffer;
7891 cur->hl.lnum = 0;
7892 cur->hl.first_lnum = 0;
7893# ifdef FEAT_RELTIME
7894 /* Set the time limit to 'redrawtime'. */
7895 profile_setlimit(p_rdt, &(cur->hl.tm));
7896# endif
7897 cur = cur->next;
7898 }
7899 search_hl.buf = wp->w_buffer;
7900 search_hl.lnum = 0;
7901 search_hl.first_lnum = 0;
7902 /* time limit is set at the toplevel, for all windows */
7903}
7904
7905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 * Advance to the match in window "wp" line "lnum" or past it.
7907 */
7908 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007909prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007911 matchitem_T *cur; /* points to the match list */
7912 match_T *shl; /* points to search_hl or a match */
7913 int shl_flag; /* flag to indicate whether search_hl
7914 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007915 int pos_inprogress; /* marks that position match search is
7916 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 int n;
7918
7919 /*
7920 * When using a multi-line pattern, start searching at the top
7921 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007922 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007924 cur = wp->w_match_head;
7925 shl_flag = FALSE;
7926 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007928 if (shl_flag == FALSE)
7929 {
7930 shl = &search_hl;
7931 shl_flag = TRUE;
7932 }
7933 else
7934 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 if (shl->rm.regprog != NULL
7936 && shl->lnum == 0
7937 && re_multiline(shl->rm.regprog))
7938 {
7939 if (shl->first_lnum == 0)
7940 {
7941# ifdef FEAT_FOLDING
7942 for (shl->first_lnum = lnum;
7943 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7944 if (hasFoldingWin(wp, shl->first_lnum - 1,
7945 NULL, NULL, TRUE, NULL))
7946 break;
7947# else
7948 shl->first_lnum = wp->w_topline;
7949# endif
7950 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007951 if (cur != NULL)
7952 cur->pos.cur = 0;
7953 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7956 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007958 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7959 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007960 pos_inprogress = cur == NULL || cur->pos.cur == 0
7961 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (shl->lnum != 0)
7963 {
7964 shl->first_lnum = shl->lnum
7965 + shl->rm.endpos[0].lnum
7966 - shl->rm.startpos[0].lnum;
7967 n = shl->rm.endpos[0].col;
7968 }
7969 else
7970 {
7971 ++shl->first_lnum;
7972 n = 0;
7973 }
7974 }
7975 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007976 if (shl != &search_hl && cur != NULL)
7977 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 }
7979}
7980
7981/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007982 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 * Uses shl->buf.
7984 * Sets shl->lnum and shl->rm contents.
7985 * Note: Assumes a previous match is always before "lnum", unless
7986 * shl->lnum is zero.
7987 * Careful: Any pointers for buffer lines will become invalid.
7988 */
7989 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007990next_search_hl(
7991 win_T *win,
7992 match_T *shl, /* points to search_hl or a match */
7993 linenr_T lnum,
7994 colnr_T mincol, /* minimal column for a match */
7995 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996{
7997 linenr_T l;
7998 colnr_T matchcol;
7999 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008000 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008002 // for :{range}s/pat only highlight inside the range
8003 if (lnum < search_first_line || lnum > search_last_line)
8004 {
8005 shl->lnum = 0;
8006 return;
8007 }
8008
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (shl->lnum != 0)
8010 {
8011 /* Check for three situations:
8012 * 1. If the "lnum" is below a previous match, start a new search.
8013 * 2. If the previous match includes "mincol", use it.
8014 * 3. Continue after the previous match.
8015 */
8016 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8017 if (lnum > l)
8018 shl->lnum = 0;
8019 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8020 return;
8021 }
8022
8023 /*
8024 * Repeat searching for a match until one is found that includes "mincol"
8025 * or none is found in this line.
8026 */
8027 called_emsg = FALSE;
8028 for (;;)
8029 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008030#ifdef FEAT_RELTIME
8031 /* Stop searching after passing the time limit. */
8032 if (profile_passed_limit(&(shl->tm)))
8033 {
8034 shl->lnum = 0; /* no match found in time */
8035 break;
8036 }
8037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 /* Three situations:
8039 * 1. No useful previous match: search from start of line.
8040 * 2. Not Vi compatible or empty match: continue at next character.
8041 * Break the loop if this is beyond the end of the line.
8042 * 3. Vi compatible searching: continue at end of previous match.
8043 */
8044 if (shl->lnum == 0)
8045 matchcol = 0;
8046 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8047 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008048 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008050 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008051
8052 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008053 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008054 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008056 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 shl->lnum = 0;
8058 break;
8059 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008060#ifdef FEAT_MBYTE
8061 if (has_mbyte)
8062 matchcol += mb_ptr2len(ml);
8063 else
8064#endif
8065 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 }
8067 else
8068 matchcol = shl->rm.endpos[0].col;
8069
8070 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008071 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008073 /* Remember whether shl->rm is using a copy of the regprog in
8074 * cur->match. */
8075 int regprog_is_copy = (shl != &search_hl && cur != NULL
8076 && shl == &cur->hl
8077 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008078 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008079
Bram Moolenaarb3414592014-06-17 17:48:32 +02008080 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8081 matchcol,
8082#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008083 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008085 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086#endif
8087 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008088 /* Copy the regprog, in case it got freed and recompiled. */
8089 if (regprog_is_copy)
8090 cur->match.regprog = cur->hl.rm.regprog;
8091
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008092 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008093 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008094 /* Error while handling regexp: stop using this regexp. */
8095 if (shl == &search_hl)
8096 {
8097 /* don't free regprog in the match list, it's a copy */
8098 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008099 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008100 }
8101 shl->rm.regprog = NULL;
8102 shl->lnum = 0;
8103 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8104 message */
8105 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008106 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008107 }
8108 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008109 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008110 else
8111 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (nmatched == 0)
8113 {
8114 shl->lnum = 0; /* no match found */
8115 break;
8116 }
8117 if (shl->rm.startpos[0].lnum > 0
8118 || shl->rm.startpos[0].col >= mincol
8119 || nmatched > 1
8120 || shl->rm.endpos[0].col > mincol)
8121 {
8122 shl->lnum += shl->rm.startpos[0].lnum;
8123 break; /* useful match found */
8124 }
8125 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008126
8127 // Restore called_emsg for assert_fails().
8128 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130
Bram Moolenaar85077472016-10-16 14:35:48 +02008131/*
8132 * If there is a match fill "shl" and return one.
8133 * Return zero otherwise.
8134 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008135 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008136next_search_hl_pos(
8137 match_T *shl, /* points to a match */
8138 linenr_T lnum,
8139 posmatch_T *posmatch, /* match positions */
8140 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008141{
8142 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008143 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008144
Bram Moolenaarb3414592014-06-17 17:48:32 +02008145 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8146 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008147 llpos_T *pos = &posmatch->pos[i];
8148
8149 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008150 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008151 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008152 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008153 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008154 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008155 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008156 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008157 /* if this match comes before the one at "found" then swap
8158 * them */
8159 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008160 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008161 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008162
Bram Moolenaar85077472016-10-16 14:35:48 +02008163 *pos = posmatch->pos[found];
8164 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008165 }
8166 }
8167 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008168 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008169 }
8170 }
8171 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008172 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008173 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008174 colnr_T start = posmatch->pos[found].col == 0
8175 ? 0 : posmatch->pos[found].col - 1;
8176 colnr_T end = posmatch->pos[found].col == 0
8177 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008178
Bram Moolenaar85077472016-10-16 14:35:48 +02008179 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008180 shl->rm.startpos[0].lnum = 0;
8181 shl->rm.startpos[0].col = start;
8182 shl->rm.endpos[0].lnum = 0;
8183 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008184 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008185 posmatch->cur = found + 1;
8186 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008187 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008188 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008189}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008190#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008191
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008193screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194{
8195 attrentry_T *aep = NULL;
8196
8197 screen_attr = attr;
8198 if (full_screen
8199#ifdef WIN3264
8200 && termcap_active
8201#endif
8202 )
8203 {
8204#ifdef FEAT_GUI
8205 if (gui.in_use)
8206 {
8207 char buf[20];
8208
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008209 /* The GUI handles this internally. */
8210 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 OUT_STR(buf);
8212 }
8213 else
8214#endif
8215 {
8216 if (attr > HL_ALL) /* special HL attr. */
8217 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008218 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 aep = syn_cterm_attr2entry(attr);
8220 else
8221 aep = syn_term_attr2entry(attr);
8222 if (aep == NULL) /* did ":syntax clear" */
8223 attr = 0;
8224 else
8225 attr = aep->ae_attr;
8226 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008227 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008229 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008230#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008231 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8232 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8233 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008234#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008235 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008236 /* If the Normal FG color has BOLD attribute and the new HL
8237 * has a FG color defined, clear BOLD. */
8238 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008239 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008241 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008242 out_str(T_UCS);
8243 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008244 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8245 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008247 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008249 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008251 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008252 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
8254 /*
8255 * Output the color or start string after bold etc., in case the
8256 * bold etc. override the color setting.
8257 */
8258 if (aep != NULL)
8259 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008260#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008261 /* When 'termguicolors' is set but fg or bg is unset,
8262 * fall back to the cterm colors. This helps for SpellBad,
8263 * where the GUI uses a red undercurl. */
8264 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008266 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008267 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008268 }
8269 else
8270#endif
8271 if (t_colors > 1)
8272 {
8273 if (aep->ae_u.cterm.fg_color)
8274 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8275 }
8276#ifdef FEAT_TERMGUICOLORS
8277 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8278 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008279 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008280 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 }
8282 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008283#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008284 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008286 if (aep->ae_u.cterm.bg_color)
8287 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8288 }
8289
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008290 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008291 {
8292 if (aep->ae_u.term.start != NULL)
8293 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 }
8295 }
8296 }
8297 }
8298}
8299
8300 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008301screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 int do_ME = FALSE; /* output T_ME code */
8304
8305 if (screen_attr != 0
8306#ifdef WIN3264
8307 && termcap_active
8308#endif
8309 )
8310 {
8311#ifdef FEAT_GUI
8312 if (gui.in_use)
8313 {
8314 char buf[20];
8315
8316 /* use internal GUI code */
8317 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8318 OUT_STR(buf);
8319 }
8320 else
8321#endif
8322 {
8323 if (screen_attr > HL_ALL) /* special HL attr. */
8324 {
8325 attrentry_T *aep;
8326
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008327 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {
8329 /*
8330 * Assume that t_me restores the original colors!
8331 */
8332 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008333 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008334#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008335 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8336 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8337 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008338#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008339 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008340#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008341 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8342 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8343 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008344#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008345 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 do_ME = TRUE;
8347 }
8348 else
8349 {
8350 aep = syn_term_attr2entry(screen_attr);
8351 if (aep != NULL && aep->ae_u.term.stop != NULL)
8352 {
8353 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8354 do_ME = TRUE;
8355 else
8356 out_str(aep->ae_u.term.stop);
8357 }
8358 }
8359 if (aep == NULL) /* did ":syntax clear" */
8360 screen_attr = 0;
8361 else
8362 screen_attr = aep->ae_attr;
8363 }
8364
8365 /*
8366 * Often all ending-codes are equal to T_ME. Avoid outputting the
8367 * same sequence several times.
8368 */
8369 if (screen_attr & HL_STANDOUT)
8370 {
8371 if (STRCMP(T_SE, T_ME) == 0)
8372 do_ME = TRUE;
8373 else
8374 out_str(T_SE);
8375 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008376 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008377 {
8378 if (STRCMP(T_UCE, T_ME) == 0)
8379 do_ME = TRUE;
8380 else
8381 out_str(T_UCE);
8382 }
8383 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008384 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {
8386 if (STRCMP(T_UE, T_ME) == 0)
8387 do_ME = TRUE;
8388 else
8389 out_str(T_UE);
8390 }
8391 if (screen_attr & HL_ITALIC)
8392 {
8393 if (STRCMP(T_CZR, T_ME) == 0)
8394 do_ME = TRUE;
8395 else
8396 out_str(T_CZR);
8397 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008398 if (screen_attr & HL_STRIKETHROUGH)
8399 {
8400 if (STRCMP(T_STE, T_ME) == 0)
8401 do_ME = TRUE;
8402 else
8403 out_str(T_STE);
8404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8406 out_str(T_ME);
8407
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008408#ifdef FEAT_TERMGUICOLORS
8409 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008411 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008412 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008413 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008414 term_bg_rgb_color(cterm_normal_bg_gui_color);
8415 }
8416 else
8417#endif
8418 {
8419 if (t_colors > 1)
8420 {
8421 /* set Normal cterm colors */
8422 if (cterm_normal_fg_color != 0)
8423 term_fg_color(cterm_normal_fg_color - 1);
8424 if (cterm_normal_bg_color != 0)
8425 term_bg_color(cterm_normal_bg_color - 1);
8426 if (cterm_normal_fg_bold)
8427 out_str(T_MD);
8428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 }
8430 }
8431 }
8432 screen_attr = 0;
8433}
8434
8435/*
8436 * Reset the colors for a cterm. Used when leaving Vim.
8437 * The machine specific code may override this again.
8438 */
8439 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008440reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008442 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
8444 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008445#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008446 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8447 || cterm_normal_bg_gui_color != INVALCOLOR)
8448 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008449#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {
8453 out_str(T_OP);
8454 screen_attr = -1;
8455 }
8456 if (cterm_normal_fg_bold)
8457 {
8458 out_str(T_ME);
8459 screen_attr = -1;
8460 }
8461 }
8462}
8463
8464/*
8465 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8466 * using the attributes from ScreenAttrs["off"].
8467 */
8468 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008469screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 int attr;
8472
8473 /* Check for illegal values, just in case (could happen just after
8474 * resizing). */
8475 if (row >= screen_Rows || col >= screen_Columns)
8476 return;
8477
Bram Moolenaar494838a2015-02-10 19:20:37 +01008478 /* Outputting a character in the last cell on the screen may scroll the
8479 * screen up. Only do it when the "xn" termcap property is set, otherwise
8480 * mark the character invalid (update it when scrolled up). */
8481 if (*T_XN == NUL
8482 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483#ifdef FEAT_RIGHTLEFT
8484 /* account for first command-line character in rightleft mode */
8485 && !cmdmsg_rl
8486#endif
8487 )
8488 {
8489 ScreenAttrs[off] = (sattr_T)-1;
8490 return;
8491 }
8492
8493 /*
8494 * Stop highlighting first, so it's easier to move the cursor.
8495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 if (screen_char_attr != 0)
8497 attr = screen_char_attr;
8498 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 attr = ScreenAttrs[off];
8500 if (screen_attr != attr)
8501 screen_stop_highlight();
8502
8503 windgoto(row, col);
8504
8505 if (screen_attr != attr)
8506 screen_start_highlight(attr);
8507
8508#ifdef FEAT_MBYTE
8509 if (enc_utf8 && ScreenLinesUC[off] != 0)
8510 {
8511 char_u buf[MB_MAXBYTES + 1];
8512
Bram Moolenaarcb070082016-04-02 22:14:51 +02008513 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008514 {
8515 if (*p_ambw == 'd'
8516# ifdef FEAT_GUI
8517 && !gui.in_use
8518# endif
8519 )
8520 {
8521 /* Clear the two screen cells. If the character is actually
8522 * single width it won't change the second cell. */
8523 out_str((char_u *)" ");
8524 term_windgoto(row, col);
8525 }
8526 /* not sure where the cursor is after drawing the ambiguous width
8527 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008528 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008529 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008530 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008532
8533 /* Convert the UTF-8 character to bytes and write it. */
8534 buf[utfc_char2bytes(off, buf)] = NUL;
8535 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 }
8537 else
8538#endif
8539 {
8540#ifdef FEAT_MBYTE
8541 out_flush_check();
8542#endif
8543 out_char(ScreenLines[off]);
8544#ifdef FEAT_MBYTE
8545 /* double-byte character in single-width cell */
8546 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8547 out_char(ScreenLines2[off]);
8548#endif
8549 }
8550
8551 screen_cur_col++;
8552}
8553
8554#ifdef FEAT_MBYTE
8555
8556/*
8557 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8558 * on the screen at position 'row' and 'col'.
8559 * The attributes of the first byte is used for all. This is required to
8560 * output the two bytes of a double-byte character with nothing in between.
8561 */
8562 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008563screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 /* Check for illegal values (could be wrong when screen was resized). */
8566 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8567 return;
8568
8569 /* Outputting the last character on the screen may scrollup the screen.
8570 * Don't to it! Mark the character invalid (update it when scrolled up) */
8571 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8572 {
8573 ScreenAttrs[off] = (sattr_T)-1;
8574 return;
8575 }
8576
8577 /* Output the first byte normally (positions the cursor), then write the
8578 * second byte directly. */
8579 screen_char(off, row, col);
8580 out_char(ScreenLines[off + 1]);
8581 ++screen_cur_col;
8582}
8583#endif
8584
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585/*
8586 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8587 * This uses the contents of ScreenLines[] and doesn't change it.
8588 */
8589 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008590screen_draw_rectangle(
8591 int row,
8592 int col,
8593 int height,
8594 int width,
8595 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 int r, c;
8598 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008599#ifdef FEAT_MBYTE
8600 int max_off;
8601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008603 /* Can't use ScreenLines unless initialized */
8604 if (ScreenLines == NULL)
8605 return;
8606
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 if (invert)
8608 screen_char_attr = HL_INVERSE;
8609 for (r = row; r < row + height; ++r)
8610 {
8611 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008612#ifdef FEAT_MBYTE
8613 max_off = off + screen_Columns;
8614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 for (c = col; c < col + width; ++c)
8616 {
8617#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008618 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 {
8620 screen_char_2(off + c, r, c);
8621 ++c;
8622 }
8623 else
8624#endif
8625 {
8626 screen_char(off + c, r, c);
8627#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008628 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 ++c;
8630#endif
8631 }
8632 }
8633 }
8634 screen_char_attr = 0;
8635}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637/*
8638 * Redraw the characters for a vertically split window.
8639 */
8640 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008641redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
8643 int col;
8644 int width;
8645
8646# ifdef FEAT_CLIPBOARD
8647 clip_may_clear_selection(row, end - 1);
8648# endif
8649
8650 if (wp == NULL)
8651 {
8652 col = 0;
8653 width = Columns;
8654 }
8655 else
8656 {
8657 col = wp->w_wincol;
8658 width = wp->w_width;
8659 }
8660 screen_draw_rectangle(row, col, end - row, width, FALSE);
8661}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008663 static void
8664space_to_screenline(int off, int attr)
8665{
8666 ScreenLines[off] = ' ';
8667 ScreenAttrs[off] = attr;
8668# ifdef FEAT_MBYTE
8669 if (enc_utf8)
8670 ScreenLinesUC[off] = 0;
8671# endif
8672}
8673
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674/*
8675 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8676 * with character 'c1' in first column followed by 'c2' in the other columns.
8677 * Use attributes 'attr'.
8678 */
8679 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008680screen_fill(
8681 int start_row,
8682 int end_row,
8683 int start_col,
8684 int end_col,
8685 int c1,
8686 int c2,
8687 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688{
8689 int row;
8690 int col;
8691 int off;
8692 int end_off;
8693 int did_delete;
8694 int c;
8695 int norm_term;
8696#if defined(FEAT_GUI) || defined(UNIX)
8697 int force_next = FALSE;
8698#endif
8699
8700 if (end_row > screen_Rows) /* safety check */
8701 end_row = screen_Rows;
8702 if (end_col > screen_Columns) /* safety check */
8703 end_col = screen_Columns;
8704 if (ScreenLines == NULL
8705 || start_row >= end_row
8706 || start_col >= end_col) /* nothing to do */
8707 return;
8708
8709 /* it's a "normal" terminal when not in a GUI or cterm */
8710 norm_term = (
8711#ifdef FEAT_GUI
8712 !gui.in_use &&
8713#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008714 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 for (row = start_row; row < end_row; ++row)
8716 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008717#ifdef FEAT_MBYTE
8718 if (has_mbyte
8719# ifdef FEAT_GUI
8720 && !gui.in_use
8721# endif
8722 )
8723 {
8724 /* When drawing over the right halve of a double-wide char clear
8725 * out the left halve. When drawing over the left halve of a
8726 * double wide-char clear out the right halve. Only needed in a
8727 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008728 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008729 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008730 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008731 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008732 }
8733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 /*
8735 * Try to use delete-line termcap code, when no attributes or in a
8736 * "normal" terminal, where a bold/italic space is just a
8737 * space.
8738 */
8739 did_delete = FALSE;
8740 if (c2 == ' '
8741 && end_col == Columns
8742 && can_clear(T_CE)
8743 && (attr == 0
8744 || (norm_term
8745 && attr <= HL_ALL
8746 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8747 {
8748 /*
8749 * check if we really need to clear something
8750 */
8751 col = start_col;
8752 if (c1 != ' ') /* don't clear first char */
8753 ++col;
8754
8755 off = LineOffset[row] + col;
8756 end_off = LineOffset[row] + end_col;
8757
8758 /* skip blanks (used often, keep it fast!) */
8759#ifdef FEAT_MBYTE
8760 if (enc_utf8)
8761 while (off < end_off && ScreenLines[off] == ' '
8762 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8763 ++off;
8764 else
8765#endif
8766 while (off < end_off && ScreenLines[off] == ' '
8767 && ScreenAttrs[off] == 0)
8768 ++off;
8769 if (off < end_off) /* something to be cleared */
8770 {
8771 col = off - LineOffset[row];
8772 screen_stop_highlight();
8773 term_windgoto(row, col);/* clear rest of this screen line */
8774 out_str(T_CE);
8775 screen_start(); /* don't know where cursor is now */
8776 col = end_col - col;
8777 while (col--) /* clear chars in ScreenLines */
8778 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008779 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 ++off;
8781 }
8782 }
8783 did_delete = TRUE; /* the chars are cleared now */
8784 }
8785
8786 off = LineOffset[row] + start_col;
8787 c = c1;
8788 for (col = start_col; col < end_col; ++col)
8789 {
8790 if (ScreenLines[off] != c
8791#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008792 || (enc_utf8 && (int)ScreenLinesUC[off]
8793 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794#endif
8795 || ScreenAttrs[off] != attr
8796#if defined(FEAT_GUI) || defined(UNIX)
8797 || force_next
8798#endif
8799 )
8800 {
8801#if defined(FEAT_GUI) || defined(UNIX)
8802 /* The bold trick may make a single row of pixels appear in
8803 * the next character. When a bold character is removed, the
8804 * next character should be redrawn too. This happens for our
8805 * own GUI and for some xterms. */
8806 if (
8807# ifdef FEAT_GUI
8808 gui.in_use
8809# endif
8810# if defined(FEAT_GUI) && defined(UNIX)
8811 ||
8812# endif
8813# ifdef UNIX
8814 term_is_xterm
8815# endif
8816 )
8817 {
8818 if (ScreenLines[off] != ' '
8819 && (ScreenAttrs[off] > HL_ALL
8820 || ScreenAttrs[off] & HL_BOLD))
8821 force_next = TRUE;
8822 else
8823 force_next = FALSE;
8824 }
8825#endif
8826 ScreenLines[off] = c;
8827#ifdef FEAT_MBYTE
8828 if (enc_utf8)
8829 {
8830 if (c >= 0x80)
8831 {
8832 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008833 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
8835 else
8836 ScreenLinesUC[off] = 0;
8837 }
8838#endif
8839 ScreenAttrs[off] = attr;
8840 if (!did_delete || c != ' ')
8841 screen_char(off, row, col);
8842 }
8843 ++off;
8844 if (col == start_col)
8845 {
8846 if (did_delete)
8847 break;
8848 c = c2;
8849 }
8850 }
8851 if (end_col == Columns)
8852 LineWraps[row] = FALSE;
8853 if (row == Rows - 1) /* overwritten the command line */
8854 {
8855 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008856 if (start_col == 0 && end_col == Columns
8857 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008859 if (start_col == 0)
8860 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 }
8862 }
8863}
8864
8865/*
8866 * Check if there should be a delay. Used before clearing or redrawing the
8867 * screen or the command line.
8868 */
8869 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008870check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8873 && !did_wait_return
8874 && emsg_silent == 0)
8875 {
8876 out_flush();
8877 ui_delay(1000L, TRUE);
8878 emsg_on_display = FALSE;
8879 if (check_msg_scroll)
8880 msg_scroll = FALSE;
8881 }
8882}
8883
8884/*
8885 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008886 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 * Returns TRUE if there is a valid screen to write to.
8888 * Returns FALSE when starting up and screen not initialized yet.
8889 */
8890 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008891screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008893 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 return (ScreenLines != NULL);
8895}
8896
8897/*
8898 * Resize the shell to Rows and Columns.
8899 * Allocate ScreenLines[] and associated items.
8900 *
8901 * There may be some time between setting Rows and Columns and (re)allocating
8902 * ScreenLines[]. This happens when starting up and when (manually) changing
8903 * the shell size. Always use screen_Rows and screen_Columns to access items
8904 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8905 * final size of the shell is needed.
8906 */
8907 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008908screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
8910 int new_row, old_row;
8911#ifdef FEAT_GUI
8912 int old_Rows;
8913#endif
8914 win_T *wp;
8915 int outofmem = FALSE;
8916 int len;
8917 schar_T *new_ScreenLines;
8918#ifdef FEAT_MBYTE
8919 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008920 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008922 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923#endif
8924 sattr_T *new_ScreenAttrs;
8925 unsigned *new_LineOffset;
8926 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008927 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008928 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008930 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008931 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008933retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 /*
8935 * Allocation of the screen buffers is done only when the size changes and
8936 * when Rows and Columns have been set and we have started doing full
8937 * screen stuff.
8938 */
8939 if ((ScreenLines != NULL
8940 && Rows == screen_Rows
8941 && Columns == screen_Columns
8942#ifdef FEAT_MBYTE
8943 && enc_utf8 == (ScreenLinesUC != NULL)
8944 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008945 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946#endif
8947 )
8948 || Rows == 0
8949 || Columns == 0
8950 || (!full_screen && ScreenLines == NULL))
8951 return;
8952
8953 /*
8954 * It's possible that we produce an out-of-memory message below, which
8955 * will cause this function to be called again. To break the loop, just
8956 * return here.
8957 */
8958 if (entered)
8959 return;
8960 entered = TRUE;
8961
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008962 /*
8963 * Note that the window sizes are updated before reallocating the arrays,
8964 * thus we must not redraw here!
8965 */
8966 ++RedrawingDisabled;
8967
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 win_new_shellsize(); /* fit the windows in the new sized shell */
8969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 comp_col(); /* recompute columns for shown command and ruler */
8971
8972 /*
8973 * We're changing the size of the screen.
8974 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8975 * - Move lines from the old arrays into the new arrays, clear extra
8976 * lines (unless the screen is going to be cleared).
8977 * - Free the old arrays.
8978 *
8979 * If anything fails, make ScreenLines NULL, so we don't do anything!
8980 * Continuing with the old ScreenLines may result in a crash, because the
8981 * size is wrong.
8982 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008983 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008985 if (aucmd_win != NULL)
8986 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987
8988 new_ScreenLines = (schar_T *)lalloc((long_u)(
8989 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8990#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008991 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 if (enc_utf8)
8993 {
8994 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8995 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008996 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008997 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8999 }
9000 if (enc_dbcs == DBCS_JPNU)
9001 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9002 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9003#endif
9004 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9005 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9006 new_LineOffset = (unsigned *)lalloc((long_u)(
9007 Rows * sizeof(unsigned)), FALSE);
9008 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009009 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009011 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 {
9013 if (win_alloc_lines(wp) == FAIL)
9014 {
9015 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009016 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
9018 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009019 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9020 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009021 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009022give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009024#ifdef FEAT_MBYTE
9025 for (i = 0; i < p_mco; ++i)
9026 if (new_ScreenLinesC[i] == NULL)
9027 break;
9028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 if (new_ScreenLines == NULL
9030#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009031 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9033#endif
9034 || new_ScreenAttrs == NULL
9035 || new_LineOffset == NULL
9036 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009037 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 || outofmem)
9039 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009040 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009041 {
9042 /* guess the size */
9043 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9044
9045 /* Remember we did this to avoid getting outofmem messages over
9046 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009047 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009048 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009049 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009051 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009052 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009053 VIM_CLEAR(new_ScreenLinesC[i]);
9054 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009056 VIM_CLEAR(new_ScreenAttrs);
9057 VIM_CLEAR(new_LineOffset);
9058 VIM_CLEAR(new_LineWraps);
9059 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
9061 else
9062 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009063 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009064
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 for (new_row = 0; new_row < Rows; ++new_row)
9066 {
9067 new_LineOffset[new_row] = new_row * Columns;
9068 new_LineWraps[new_row] = FALSE;
9069
9070 /*
9071 * If the screen is not going to be cleared, copy as much as
9072 * possible from the old screen to the new one and clear the rest
9073 * (used when resizing the window at the "--more--" prompt or when
9074 * executing an external command, for the GUI).
9075 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009076 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 {
9078 (void)vim_memset(new_ScreenLines + new_row * Columns,
9079 ' ', (size_t)Columns * sizeof(schar_T));
9080#ifdef FEAT_MBYTE
9081 if (enc_utf8)
9082 {
9083 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9084 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009085 for (i = 0; i < p_mco; ++i)
9086 (void)vim_memset(new_ScreenLinesC[i]
9087 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 0, (size_t)Columns * sizeof(u8char_T));
9089 }
9090 if (enc_dbcs == DBCS_JPNU)
9091 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9092 0, (size_t)Columns * sizeof(schar_T));
9093#endif
9094 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9095 0, (size_t)Columns * sizeof(sattr_T));
9096 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009097 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 {
9099 if (screen_Columns < Columns)
9100 len = screen_Columns;
9101 else
9102 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009103#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009104 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009105 * may be invalid now. Also when p_mco changes. */
9106 if (!(enc_utf8 && ScreenLinesUC == NULL)
9107 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009108#endif
9109 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9110 ScreenLines + LineOffset[old_row],
9111 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009113 if (enc_utf8 && ScreenLinesUC != NULL
9114 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 {
9116 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9117 ScreenLinesUC + LineOffset[old_row],
9118 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009119 for (i = 0; i < p_mco; ++i)
9120 mch_memmove(new_ScreenLinesC[i]
9121 + new_LineOffset[new_row],
9122 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 (size_t)len * sizeof(u8char_T));
9124 }
9125 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9126 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9127 ScreenLines2 + LineOffset[old_row],
9128 (size_t)len * sizeof(schar_T));
9129#endif
9130 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9131 ScreenAttrs + LineOffset[old_row],
9132 (size_t)len * sizeof(sattr_T));
9133 }
9134 }
9135 }
9136 /* Use the last line of the screen for the current line. */
9137 current_ScreenLine = new_ScreenLines + Rows * Columns;
9138 }
9139
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009140 free_screenlines();
9141
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 ScreenLines = new_ScreenLines;
9143#ifdef FEAT_MBYTE
9144 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009145 for (i = 0; i < p_mco; ++i)
9146 ScreenLinesC[i] = new_ScreenLinesC[i];
9147 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 ScreenLines2 = new_ScreenLines2;
9149#endif
9150 ScreenAttrs = new_ScreenAttrs;
9151 LineOffset = new_LineOffset;
9152 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009153 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154
9155 /* It's important that screen_Rows and screen_Columns reflect the actual
9156 * size of ScreenLines[]. Set them before calling anything. */
9157#ifdef FEAT_GUI
9158 old_Rows = screen_Rows;
9159#endif
9160 screen_Rows = Rows;
9161 screen_Columns = Columns;
9162
9163 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009164 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 screenclear2();
9166
9167#ifdef FEAT_GUI
9168 else if (gui.in_use
9169 && !gui.starting
9170 && ScreenLines != NULL
9171 && old_Rows != Rows)
9172 {
9173 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9174 /*
9175 * Adjust the position of the cursor, for when executing an external
9176 * command.
9177 */
9178 if (msg_row >= Rows) /* Rows got smaller */
9179 msg_row = Rows - 1; /* put cursor at last row */
9180 else if (Rows > old_Rows) /* Rows got bigger */
9181 msg_row += Rows - old_Rows; /* put cursor in same place */
9182 if (msg_col >= Columns) /* Columns got smaller */
9183 msg_col = Columns - 1; /* put cursor at last column */
9184 }
9185#endif
9186
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009188 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009189
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009190 /*
9191 * Do not apply autocommands more than 3 times to avoid an endless loop
9192 * in case applying autocommands always changes Rows or Columns.
9193 */
9194 if (starting == 0 && ++retry_count <= 3)
9195 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009196 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009197 /* In rare cases, autocommands may have altered Rows or Columns,
9198 * jump back to check if we need to allocate the screen again. */
9199 goto retry;
9200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
9203 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009204free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009205{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009206#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009207 int i;
9208
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009209 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009210 for (i = 0; i < Screen_mco; ++i)
9211 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009212 vim_free(ScreenLines2);
9213#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009214 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009215 vim_free(ScreenAttrs);
9216 vim_free(LineOffset);
9217 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009218 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009219}
9220
9221 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009222screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 check_for_delay(FALSE);
9225 screenalloc(FALSE); /* allocate screen buffers if size changed */
9226 screenclear2(); /* clear the screen */
9227}
9228
9229 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009230screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
9232 int i;
9233
9234 if (starting == NO_SCREEN || ScreenLines == NULL
9235#ifdef FEAT_GUI
9236 || (gui.in_use && gui.starting)
9237#endif
9238 )
9239 return;
9240
9241#ifdef FEAT_GUI
9242 if (!gui.in_use)
9243#endif
9244 screen_attr = -1; /* force setting the Normal colors */
9245 screen_stop_highlight(); /* don't want highlighting here */
9246
9247#ifdef FEAT_CLIPBOARD
9248 /* disable selection without redrawing it */
9249 clip_scroll_selection(9999);
9250#endif
9251
9252 /* blank out ScreenLines */
9253 for (i = 0; i < Rows; ++i)
9254 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009255 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 LineWraps[i] = FALSE;
9257 }
9258
9259 if (can_clear(T_CL))
9260 {
9261 out_str(T_CL); /* clear the display */
9262 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009263 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 }
9265 else
9266 {
9267 /* can't clear the screen, mark all chars with invalid attributes */
9268 for (i = 0; i < Rows; ++i)
9269 lineinvalid(LineOffset[i], (int)Columns);
9270 clear_cmdline = TRUE;
9271 }
9272
9273 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9274
9275 win_rest_invalid(firstwin);
9276 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009277 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 if (must_redraw == CLEAR) /* no need to clear again */
9279 must_redraw = NOT_VALID;
9280 compute_cmdrow();
9281 msg_row = cmdline_row; /* put cursor on last line for messages */
9282 msg_col = 0;
9283 screen_start(); /* don't know where cursor is now */
9284 msg_scrolled = 0; /* can't scroll back */
9285 msg_didany = FALSE;
9286 msg_didout = FALSE;
9287}
9288
9289/*
9290 * Clear one line in ScreenLines.
9291 */
9292 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009293lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294{
9295 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9296#ifdef FEAT_MBYTE
9297 if (enc_utf8)
9298 (void)vim_memset(ScreenLinesUC + off, 0,
9299 (size_t)width * sizeof(u8char_T));
9300#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009301 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302}
9303
9304/*
9305 * Mark one line in ScreenLines invalid by setting the attributes to an
9306 * invalid value.
9307 */
9308 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009309lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9312}
9313
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314/*
9315 * Copy part of a Screenline for vertically split window "wp".
9316 */
9317 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009318linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
9320 unsigned off_to = LineOffset[to] + wp->w_wincol;
9321 unsigned off_from = LineOffset[from] + wp->w_wincol;
9322
9323 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9324 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009325#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 if (enc_utf8)
9327 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009328 int i;
9329
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9331 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009332 for (i = 0; i < p_mco; ++i)
9333 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9334 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 }
9336 if (enc_dbcs == DBCS_JPNU)
9337 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9338 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9341 wp->w_width * sizeof(sattr_T));
9342}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343
9344/*
9345 * Return TRUE if clearing with term string "p" would work.
9346 * It can't work when the string is empty or it won't set the right background.
9347 */
9348 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009349can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350{
9351 return (*p != NUL && (t_colors <= 1
9352#ifdef FEAT_GUI
9353 || gui.in_use
9354#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009355#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009356 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009357 || (!p_tgc && cterm_normal_bg_color == 0)
9358#else
9359 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009360#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009361 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362}
9363
9364/*
9365 * Reset cursor position. Use whenever cursor was moved because of outputting
9366 * something directly to the screen (shell commands) or a terminal control
9367 * code.
9368 */
9369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009370screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
9372 screen_cur_row = screen_cur_col = 9999;
9373}
9374
9375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 * Move the cursor to position "row","col" in the screen.
9377 * This tries to find the most efficient way to move, minimizing the number of
9378 * characters sent to the terminal.
9379 */
9380 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009381windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009383 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 int i;
9385 int plan;
9386 int cost;
9387 int wouldbe_col;
9388 int noinvcurs;
9389 char_u *bs;
9390 int goto_cost;
9391 int attr;
9392
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009393#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9395
9396#define PLAN_LE 1
9397#define PLAN_CR 2
9398#define PLAN_NL 3
9399#define PLAN_WRITE 4
9400 /* Can't use ScreenLines unless initialized */
9401 if (ScreenLines == NULL)
9402 return;
9403
9404 if (col != screen_cur_col || row != screen_cur_row)
9405 {
9406 /* Check for valid position. */
9407 if (row < 0) /* window without text lines? */
9408 row = 0;
9409 if (row >= screen_Rows)
9410 row = screen_Rows - 1;
9411 if (col >= screen_Columns)
9412 col = screen_Columns - 1;
9413
9414 /* check if no cursor movement is allowed in highlight mode */
9415 if (screen_attr && *T_MS == NUL)
9416 noinvcurs = HIGHL_COST;
9417 else
9418 noinvcurs = 0;
9419 goto_cost = GOTO_COST + noinvcurs;
9420
9421 /*
9422 * Plan how to do the positioning:
9423 * 1. Use CR to move it to column 0, same row.
9424 * 2. Use T_LE to move it a few columns to the left.
9425 * 3. Use NL to move a few lines down, column 0.
9426 * 4. Move a few columns to the right with T_ND or by writing chars.
9427 *
9428 * Don't do this if the cursor went beyond the last column, the cursor
9429 * position is unknown then (some terminals wrap, some don't )
9430 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009431 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 * characters to move the cursor to the right.
9433 */
9434 if (row >= screen_cur_row && screen_cur_col < Columns)
9435 {
9436 /*
9437 * If the cursor is in the same row, bigger col, we can use CR
9438 * or T_LE.
9439 */
9440 bs = NULL; /* init for GCC */
9441 attr = screen_attr;
9442 if (row == screen_cur_row && col < screen_cur_col)
9443 {
9444 /* "le" is preferred over "bc", because "bc" is obsolete */
9445 if (*T_LE)
9446 bs = T_LE; /* "cursor left" */
9447 else
9448 bs = T_BC; /* "backspace character (old) */
9449 if (*bs)
9450 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9451 else
9452 cost = 999;
9453 if (col + 1 < cost) /* using CR is less characters */
9454 {
9455 plan = PLAN_CR;
9456 wouldbe_col = 0;
9457 cost = 1; /* CR is just one character */
9458 }
9459 else
9460 {
9461 plan = PLAN_LE;
9462 wouldbe_col = col;
9463 }
9464 if (noinvcurs) /* will stop highlighting */
9465 {
9466 cost += noinvcurs;
9467 attr = 0;
9468 }
9469 }
9470
9471 /*
9472 * If the cursor is above where we want to be, we can use CR LF.
9473 */
9474 else if (row > screen_cur_row)
9475 {
9476 plan = PLAN_NL;
9477 wouldbe_col = 0;
9478 cost = (row - screen_cur_row) * 2; /* CR LF */
9479 if (noinvcurs) /* will stop highlighting */
9480 {
9481 cost += noinvcurs;
9482 attr = 0;
9483 }
9484 }
9485
9486 /*
9487 * If the cursor is in the same row, smaller col, just use write.
9488 */
9489 else
9490 {
9491 plan = PLAN_WRITE;
9492 wouldbe_col = screen_cur_col;
9493 cost = 0;
9494 }
9495
9496 /*
9497 * Check if any characters that need to be written have the
9498 * correct attributes. Also avoid UTF-8 characters.
9499 */
9500 i = col - wouldbe_col;
9501 if (i > 0)
9502 cost += i;
9503 if (cost < goto_cost && i > 0)
9504 {
9505 /*
9506 * Check if the attributes are correct without additionally
9507 * stopping highlighting.
9508 */
9509 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9510 while (i && *p++ == attr)
9511 --i;
9512 if (i != 0)
9513 {
9514 /*
9515 * Try if it works when highlighting is stopped here.
9516 */
9517 if (*--p == 0)
9518 {
9519 cost += noinvcurs;
9520 while (i && *p++ == 0)
9521 --i;
9522 }
9523 if (i != 0)
9524 cost = 999; /* different attributes, don't do it */
9525 }
9526#ifdef FEAT_MBYTE
9527 if (enc_utf8)
9528 {
9529 /* Don't use an UTF-8 char for positioning, it's slow. */
9530 for (i = wouldbe_col; i < col; ++i)
9531 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9532 {
9533 cost = 999;
9534 break;
9535 }
9536 }
9537#endif
9538 }
9539
9540 /*
9541 * We can do it without term_windgoto()!
9542 */
9543 if (cost < goto_cost)
9544 {
9545 if (plan == PLAN_LE)
9546 {
9547 if (noinvcurs)
9548 screen_stop_highlight();
9549 while (screen_cur_col > col)
9550 {
9551 out_str(bs);
9552 --screen_cur_col;
9553 }
9554 }
9555 else if (plan == PLAN_CR)
9556 {
9557 if (noinvcurs)
9558 screen_stop_highlight();
9559 out_char('\r');
9560 screen_cur_col = 0;
9561 }
9562 else if (plan == PLAN_NL)
9563 {
9564 if (noinvcurs)
9565 screen_stop_highlight();
9566 while (screen_cur_row < row)
9567 {
9568 out_char('\n');
9569 ++screen_cur_row;
9570 }
9571 screen_cur_col = 0;
9572 }
9573
9574 i = col - screen_cur_col;
9575 if (i > 0)
9576 {
9577 /*
9578 * Use cursor-right if it's one character only. Avoids
9579 * removing a line of pixels from the last bold char, when
9580 * using the bold trick in the GUI.
9581 */
9582 if (T_ND[0] != NUL && T_ND[1] == NUL)
9583 {
9584 while (i-- > 0)
9585 out_char(*T_ND);
9586 }
9587 else
9588 {
9589 int off;
9590
9591 off = LineOffset[row] + screen_cur_col;
9592 while (i-- > 0)
9593 {
9594 if (ScreenAttrs[off] != screen_attr)
9595 screen_stop_highlight();
9596#ifdef FEAT_MBYTE
9597 out_flush_check();
9598#endif
9599 out_char(ScreenLines[off]);
9600#ifdef FEAT_MBYTE
9601 if (enc_dbcs == DBCS_JPNU
9602 && ScreenLines[off] == 0x8e)
9603 out_char(ScreenLines2[off]);
9604#endif
9605 ++off;
9606 }
9607 }
9608 }
9609 }
9610 }
9611 else
9612 cost = 999;
9613
9614 if (cost >= goto_cost)
9615 {
9616 if (noinvcurs)
9617 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009618 if (row == screen_cur_row && (col > screen_cur_col)
9619 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 term_cursor_right(col - screen_cur_col);
9621 else
9622 term_windgoto(row, col);
9623 }
9624 screen_cur_row = row;
9625 screen_cur_col = col;
9626 }
9627}
9628
9629/*
9630 * Set cursor to its position in the current window.
9631 */
9632 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009633setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009635 setcursor_mayforce(FALSE);
9636}
9637
9638/*
9639 * Set cursor to its position in the current window.
9640 * When "force" is TRUE also when not redrawing.
9641 */
9642 void
9643setcursor_mayforce(int force)
9644{
9645 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 {
9647 validate_cursor();
9648 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009649 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009651 /* With 'rightleft' set and the cursor on a double-wide
9652 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009653 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009655 (has_mbyte
9656 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9657 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658# endif
9659 1)) :
9660#endif
9661 curwin->w_wcol));
9662 }
9663}
9664
9665
9666/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009667 * Insert 'line_count' lines at 'row' in window 'wp'.
9668 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9669 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 * scrolling.
9671 * Returns FAIL if the lines are not inserted, OK for success.
9672 */
9673 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009674win_ins_lines(
9675 win_T *wp,
9676 int row,
9677 int line_count,
9678 int invalid,
9679 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 int did_delete;
9682 int nextrow;
9683 int lastrow;
9684 int retval;
9685
9686 if (invalid)
9687 wp->w_lines_valid = 0;
9688
9689 if (wp->w_height < 5)
9690 return FAIL;
9691
9692 if (line_count > wp->w_height - row)
9693 line_count = wp->w_height - row;
9694
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009695 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 if (retval != MAYBE)
9697 return retval;
9698
9699 /*
9700 * If there is a next window or a status line, we first try to delete the
9701 * lines at the bottom to avoid messing what is after the window.
9702 * If this fails and there are following windows, don't do anything to avoid
9703 * messing up those windows, better just redraw.
9704 */
9705 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (wp->w_next != NULL || wp->w_status_height)
9707 {
9708 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009709 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 did_delete = TRUE;
9711 else if (wp->w_next)
9712 return FAIL;
9713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 /*
9715 * if no lines deleted, blank the lines that will end up below the window
9716 */
9717 if (!did_delete)
9718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009721 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 lastrow = nextrow + line_count;
9723 if (lastrow > Rows)
9724 lastrow = Rows;
9725 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009726 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 ' ', ' ', 0);
9728 }
9729
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009730 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 == FAIL)
9732 {
9733 /* deletion will have messed up other windows */
9734 if (did_delete)
9735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 win_rest_invalid(W_NEXT(wp));
9738 }
9739 return FAIL;
9740 }
9741
9742 return OK;
9743}
9744
9745/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009746 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9748 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9749 * scrolling
9750 * Return OK for success, FAIL if the lines are not deleted.
9751 */
9752 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009753win_del_lines(
9754 win_T *wp,
9755 int row,
9756 int line_count,
9757 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009758 int mayclear,
9759 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
9761 int retval;
9762
9763 if (invalid)
9764 wp->w_lines_valid = 0;
9765
9766 if (line_count > wp->w_height - row)
9767 line_count = wp->w_height - row;
9768
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009769 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 if (retval != MAYBE)
9771 return retval;
9772
9773 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009774 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 return FAIL;
9776
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 /*
9778 * If there are windows or status lines below, try to put them at the
9779 * correct place. If we can't do that, they have to be redrawn.
9780 */
9781 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9782 {
9783 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009784 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 {
9786 wp->w_redr_status = TRUE;
9787 win_rest_invalid(wp->w_next);
9788 }
9789 }
9790 /*
9791 * If this is the last window and there is no status line, redraw the
9792 * command line later.
9793 */
9794 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 redraw_cmdline = TRUE;
9796 return OK;
9797}
9798
9799/*
9800 * Common code for win_ins_lines() and win_del_lines().
9801 * Returns OK or FAIL when the work has been done.
9802 * Returns MAYBE when not finished yet.
9803 */
9804 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009805win_do_lines(
9806 win_T *wp,
9807 int row,
9808 int line_count,
9809 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009810 int del,
9811 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813 int retval;
9814
9815 if (!redrawing() || line_count <= 0)
9816 return FAIL;
9817
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009818 /* When inserting lines would result in loss of command output, just redraw
9819 * the lines. */
9820 if (no_win_do_lines_ins && !del)
9821 return FAIL;
9822
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009824 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009826 if (!no_win_do_lines_ins)
9827 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 return FAIL;
9829 }
9830
9831 /*
9832 * Delete all remaining lines
9833 */
9834 if (row + line_count >= wp->w_height)
9835 {
9836 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009837 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 ' ', ' ', 0);
9839 return OK;
9840 }
9841
9842 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009843 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009845 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009847 if (!no_win_do_lines_ins)
9848 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849
9850 /*
9851 * If the terminal can set a scroll region, use that.
9852 * Always do this in a vertically split window. This will redraw from
9853 * ScreenLines[] when t_CV isn't defined. That's faster than using
9854 * win_line().
9855 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009856 * a character in the lower right corner of the scroll region may cause a
9857 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009859 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 scroll_region_set(wp, row);
9863 if (del)
9864 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009865 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 else
9867 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009868 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 scroll_region_reset();
9871 return retval;
9872 }
9873
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9875 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876
9877 return MAYBE;
9878}
9879
9880/*
9881 * window 'wp' and everything after it is messed up, mark it for redraw
9882 */
9883 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009884win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 {
9888 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 wp->w_redr_status = TRUE;
9890 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 }
9892 redraw_cmdline = TRUE;
9893}
9894
9895/*
9896 * The rest of the routines in this file perform screen manipulations. The
9897 * given operation is performed physically on the screen. The corresponding
9898 * change is also made to the internal screen image. In this way, the editor
9899 * anticipates the effect of editing changes on the appearance of the screen.
9900 * That way, when we call screenupdate a complete redraw isn't usually
9901 * necessary. Another advantage is that we can keep adding code to anticipate
9902 * screen changes, and in the meantime, everything still works.
9903 */
9904
9905/*
9906 * types for inserting or deleting lines
9907 */
9908#define USE_T_CAL 1
9909#define USE_T_CDL 2
9910#define USE_T_AL 3
9911#define USE_T_CE 4
9912#define USE_T_DL 5
9913#define USE_T_SR 6
9914#define USE_NL 7
9915#define USE_T_CD 8
9916#define USE_REDRAW 9
9917
9918/*
9919 * insert lines on the screen and update ScreenLines[]
9920 * 'end' is the line after the scrolled part. Normally it is Rows.
9921 * When scrolling region used 'off' is the offset from the top for the region.
9922 * 'row' and 'end' are relative to the start of the region.
9923 *
9924 * return FAIL for failure, OK for success.
9925 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009926 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009927screen_ins_lines(
9928 int off,
9929 int row,
9930 int line_count,
9931 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009932 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009933 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934{
9935 int i;
9936 int j;
9937 unsigned temp;
9938 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009939 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 int type;
9941 int result_empty;
9942 int can_ce = can_clear(T_CE);
9943
9944 /*
9945 * FAIL if
9946 * - there is no valid screen
9947 * - the screen has to be redrawn completely
9948 * - the line count is less than one
9949 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009950 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009952 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9953#ifdef FEAT_CLIPBOARD
9954 || (clip_star.state != SELECT_CLEARED
9955 && redrawing_for_callback > 0)
9956#endif
9957 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 return FAIL;
9959
9960 /*
9961 * There are seven ways to insert lines:
9962 * 0. When in a vertically split window and t_CV isn't set, redraw the
9963 * characters from ScreenLines[].
9964 * 1. Use T_CD (clear to end of display) if it exists and the result of
9965 * the insert is just empty lines
9966 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9967 * present or line_count > 1. It looks better if we do all the inserts
9968 * at once.
9969 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9970 * insert is just empty lines and T_CE is not present or line_count >
9971 * 1.
9972 * 4. Use T_AL (insert line) if it exists.
9973 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9974 * just empty lines.
9975 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9976 * just empty lines.
9977 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9978 * the 'da' flag is not set or we have clear line capability.
9979 * 8. redraw the characters from ScreenLines[].
9980 *
9981 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9982 * the scrollbar for the window. It does have insert line, use that if it
9983 * exists.
9984 */
9985 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9987 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009988 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 type = USE_T_CD;
9990 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9991 type = USE_T_CAL;
9992 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9993 type = USE_T_CDL;
9994 else if (*T_AL != NUL)
9995 type = USE_T_AL;
9996 else if (can_ce && result_empty)
9997 type = USE_T_CE;
9998 else if (*T_DL != NUL && result_empty)
9999 type = USE_T_DL;
10000 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10001 type = USE_T_SR;
10002 else
10003 return FAIL;
10004
10005 /*
10006 * For clearing the lines screen_del_lines() is used. This will also take
10007 * care of t_db if necessary.
10008 */
10009 if (type == USE_T_CD || type == USE_T_CDL ||
10010 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010011 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012
10013 /*
10014 * If text is retained below the screen, first clear or delete as many
10015 * lines at the bottom of the window as are about to be inserted so that
10016 * the deleted lines won't later surface during a screen_del_lines.
10017 */
10018 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010019 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020
10021#ifdef FEAT_CLIPBOARD
10022 /* Remove a modeless selection when inserting lines halfway the screen
10023 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010024 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010025 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 else
10027 clip_scroll_selection(-line_count);
10028#endif
10029
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030#ifdef FEAT_GUI
10031 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10032 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010033 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034#endif
10035
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010036 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10037 cursor_col = wp->w_wincol;
10038
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 if (*T_CCS != NUL) /* cursor relative to region */
10040 cursor_row = row;
10041 else
10042 cursor_row = row + off;
10043
10044 /*
10045 * Shift LineOffset[] line_count down to reflect the inserted lines.
10046 * Clear the inserted lines in ScreenLines[].
10047 */
10048 row += off;
10049 end += off;
10050 for (i = 0; i < line_count; ++i)
10051 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 if (wp != NULL && wp->w_width != Columns)
10053 {
10054 /* need to copy part of a line */
10055 j = end - 1 - i;
10056 while ((j -= line_count) >= row)
10057 linecopy(j + line_count, j, wp);
10058 j += line_count;
10059 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010060 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10061 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 else
10063 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10064 LineWraps[j] = FALSE;
10065 }
10066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 {
10068 j = end - 1 - i;
10069 temp = LineOffset[j];
10070 while ((j -= line_count) >= row)
10071 {
10072 LineOffset[j + line_count] = LineOffset[j];
10073 LineWraps[j + line_count] = LineWraps[j];
10074 }
10075 LineOffset[j + line_count] = temp;
10076 LineWraps[j + line_count] = FALSE;
10077 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010078 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 else
10080 lineinvalid(temp, (int)Columns);
10081 }
10082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
10084 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010085 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010086 if (clear_attr != 0)
10087 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 /* redraw the characters */
10090 if (type == USE_REDRAW)
10091 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010092 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 {
10094 term_append_lines(line_count);
10095 screen_start(); /* don't know where cursor is now */
10096 }
10097 else
10098 {
10099 for (i = 0; i < line_count; i++)
10100 {
10101 if (type == USE_T_AL)
10102 {
10103 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010104 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 out_str(T_AL);
10106 }
10107 else /* type == USE_T_SR */
10108 out_str(T_SR);
10109 screen_start(); /* don't know where cursor is now */
10110 }
10111 }
10112
10113 /*
10114 * With scroll-reverse and 'da' flag set we need to clear the lines that
10115 * have been scrolled down into the region.
10116 */
10117 if (type == USE_T_SR && *T_DA)
10118 {
10119 for (i = 0; i < line_count; ++i)
10120 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010121 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 out_str(T_CE);
10123 screen_start(); /* don't know where cursor is now */
10124 }
10125 }
10126
10127#ifdef FEAT_GUI
10128 gui_can_update_cursor();
10129 if (gui.in_use)
10130 out_flush(); /* always flush after a scroll */
10131#endif
10132 return OK;
10133}
10134
10135/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010136 * Delete lines on the screen and update ScreenLines[].
10137 * "end" is the line after the scrolled part. Normally it is Rows.
10138 * When scrolling region used "off" is the offset from the top for the region.
10139 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 *
10141 * Return OK for success, FAIL if the lines are not deleted.
10142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010144screen_del_lines(
10145 int off,
10146 int row,
10147 int line_count,
10148 int end,
10149 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010150 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010151 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152{
10153 int j;
10154 int i;
10155 unsigned temp;
10156 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010157 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 int cursor_end;
10159 int result_empty; /* result is empty until end of region */
10160 int can_delete; /* deleting line codes can be used */
10161 int type;
10162
10163 /*
10164 * FAIL if
10165 * - there is no valid screen
10166 * - the screen has to be redrawn completely
10167 * - the line count is less than one
10168 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010169 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010171 if (!screen_valid(TRUE) || line_count <= 0
10172 || (!force && line_count > p_ttyscroll)
10173#ifdef FEAT_CLIPBOARD
10174 || (clip_star.state != SELECT_CLEARED
10175 && redrawing_for_callback > 0)
10176#endif
10177 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 return FAIL;
10179
10180 /*
10181 * Check if the rest of the current region will become empty.
10182 */
10183 result_empty = row + line_count >= end;
10184
10185 /*
10186 * We can delete lines only when 'db' flag not set or when 'ce' option
10187 * available.
10188 */
10189 can_delete = (*T_DB == NUL || can_clear(T_CE));
10190
10191 /*
10192 * There are six ways to delete lines:
10193 * 0. When in a vertically split window and t_CV isn't set, redraw the
10194 * characters from ScreenLines[].
10195 * 1. Use T_CD if it exists and the result is empty.
10196 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10197 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10198 * none of the other ways work.
10199 * 4. Use T_CE (erase line) if the result is empty.
10200 * 5. Use T_DL (delete line) if it exists.
10201 * 6. redraw the characters from ScreenLines[].
10202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10204 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010205 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 type = USE_T_CD;
10207#if defined(__BEOS__) && defined(BEOS_DR8)
10208 /*
10209 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10210 * its internal termcap... this works okay for tests which test *T_DB !=
10211 * NUL. It has the disadvantage that the user cannot use any :set t_*
10212 * command to get T_DB (back) to empty_option, only :set term=... will do
10213 * the trick...
10214 * Anyway, this hack will hopefully go away with the next OS release.
10215 * (Olaf Seibert)
10216 */
10217 else if (row == 0 && T_DB == empty_option
10218 && (line_count == 1 || *T_CDL == NUL))
10219#else
10220 else if (row == 0 && (
10221#ifndef AMIGA
10222 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10223 * up, so use delete-line command */
10224 line_count == 1 ||
10225#endif
10226 *T_CDL == NUL))
10227#endif
10228 type = USE_NL;
10229 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10230 type = USE_T_CDL;
10231 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010232 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 type = USE_T_CE;
10234 else if (*T_DL != NUL && can_delete)
10235 type = USE_T_DL;
10236 else if (*T_CDL != NUL && can_delete)
10237 type = USE_T_CDL;
10238 else
10239 return FAIL;
10240
10241#ifdef FEAT_CLIPBOARD
10242 /* Remove a modeless selection when deleting lines halfway the screen or
10243 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010244 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010245 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 else
10247 clip_scroll_selection(line_count);
10248#endif
10249
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250#ifdef FEAT_GUI
10251 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10252 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010253 gui_dont_update_cursor(gui.cursor_row >= row + off
10254 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255#endif
10256
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010257 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10258 cursor_col = wp->w_wincol;
10259
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 if (*T_CCS != NUL) /* cursor relative to region */
10261 {
10262 cursor_row = row;
10263 cursor_end = end;
10264 }
10265 else
10266 {
10267 cursor_row = row + off;
10268 cursor_end = end + off;
10269 }
10270
10271 /*
10272 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10273 * Clear the inserted lines in ScreenLines[].
10274 */
10275 row += off;
10276 end += off;
10277 for (i = 0; i < line_count; ++i)
10278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 if (wp != NULL && wp->w_width != Columns)
10280 {
10281 /* need to copy part of a line */
10282 j = row + i;
10283 while ((j += line_count) <= end - 1)
10284 linecopy(j - line_count, j, wp);
10285 j -= line_count;
10286 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010287 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10288 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 else
10290 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10291 LineWraps[j] = FALSE;
10292 }
10293 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 {
10295 /* whole width, moving the line pointers is faster */
10296 j = row + i;
10297 temp = LineOffset[j];
10298 while ((j += line_count) <= end - 1)
10299 {
10300 LineOffset[j - line_count] = LineOffset[j];
10301 LineWraps[j - line_count] = LineWraps[j];
10302 }
10303 LineOffset[j - line_count] = temp;
10304 LineWraps[j - line_count] = FALSE;
10305 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010306 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 else
10308 lineinvalid(temp, (int)Columns);
10309 }
10310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010312 if (screen_attr != clear_attr)
10313 screen_stop_highlight();
10314 if (clear_attr != 0)
10315 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 /* redraw the characters */
10318 if (type == USE_REDRAW)
10319 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010320 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010322 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 out_str(T_CD);
10324 screen_start(); /* don't know where cursor is now */
10325 }
10326 else if (type == USE_T_CDL)
10327 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010328 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 term_delete_lines(line_count);
10330 screen_start(); /* don't know where cursor is now */
10331 }
10332 /*
10333 * Deleting lines at top of the screen or scroll region: Just scroll
10334 * the whole screen (scroll region) up by outputting newlines on the
10335 * last line.
10336 */
10337 else if (type == USE_NL)
10338 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010339 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 for (i = line_count; --i >= 0; )
10341 out_char('\n'); /* cursor will remain on same line */
10342 }
10343 else
10344 {
10345 for (i = line_count; --i >= 0; )
10346 {
10347 if (type == USE_T_DL)
10348 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010349 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 out_str(T_DL); /* delete a line */
10351 }
10352 else /* type == USE_T_CE */
10353 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010354 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 out_str(T_CE); /* erase a line */
10356 }
10357 screen_start(); /* don't know where cursor is now */
10358 }
10359 }
10360
10361 /*
10362 * If the 'db' flag is set, we need to clear the lines that have been
10363 * scrolled up at the bottom of the region.
10364 */
10365 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10366 {
10367 for (i = line_count; i > 0; --i)
10368 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010369 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 out_str(T_CE); /* erase a line */
10371 screen_start(); /* don't know where cursor is now */
10372 }
10373 }
10374
10375#ifdef FEAT_GUI
10376 gui_can_update_cursor();
10377 if (gui.in_use)
10378 out_flush(); /* always flush after a scroll */
10379#endif
10380
10381 return OK;
10382}
10383
10384/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010385 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 *
10387 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10388 * If clear_cmdline is FALSE there may be a message there that needs to be
10389 * cleared only if a mode is shown.
10390 * Return the length of the message (0 if no message).
10391 */
10392 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010393showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394{
10395 int need_clear;
10396 int length = 0;
10397 int do_mode;
10398 int attr;
10399 int nwr_save;
10400#ifdef FEAT_INS_EXPAND
10401 int sub_attr;
10402#endif
10403
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010404 do_mode = ((p_smd && msg_silent == 0)
10405 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010406 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010407 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010408 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 {
10410 /*
10411 * Don't show mode right now, when not redrawing or inside a mapping.
10412 * Call char_avail() only when we are going to show something, because
10413 * it takes a bit of time.
10414 */
10415 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10416 {
10417 redraw_cmdline = TRUE; /* show mode later */
10418 return 0;
10419 }
10420
10421 nwr_save = need_wait_return;
10422
10423 /* wait a bit before overwriting an important message */
10424 check_for_delay(FALSE);
10425
10426 /* if the cmdline is more than one line high, erase top lines */
10427 need_clear = clear_cmdline;
10428 if (clear_cmdline && cmdline_row < Rows - 1)
10429 msg_clr_cmdline(); /* will reset clear_cmdline */
10430
10431 /* Position on the last line in the window, column 0 */
10432 msg_pos_mode();
10433 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010434 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 if (do_mode)
10436 {
10437 MSG_PUTS_ATTR("--", attr);
10438#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010439 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010440# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010441 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010442# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010443 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010444# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010445 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010446# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 MSG_PUTS_ATTR(" IM", attr);
10448# else
10449 MSG_PUTS_ATTR(" XIM", attr);
10450# endif
10451#endif
10452#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10453 if (gui.in_use)
10454 {
10455 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010456 {
10457 /* HANGUL */
10458 if (enc_utf8)
10459 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10460 else
10461 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 }
10464#endif
10465#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010466 /* CTRL-X in Insert mode */
10467 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 {
10469 /* These messages can get long, avoid a wrap in a narrow
10470 * window. Prefer showing edit_submode_extra. */
10471 length = (Rows - msg_row) * Columns - 3;
10472 if (edit_submode_extra != NULL)
10473 length -= vim_strsize(edit_submode_extra);
10474 if (length > 0)
10475 {
10476 if (edit_submode_pre != NULL)
10477 length -= vim_strsize(edit_submode_pre);
10478 if (length - vim_strsize(edit_submode) > 0)
10479 {
10480 if (edit_submode_pre != NULL)
10481 msg_puts_attr(edit_submode_pre, attr);
10482 msg_puts_attr(edit_submode, attr);
10483 }
10484 if (edit_submode_extra != NULL)
10485 {
10486 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10487 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010488 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 else
10490 sub_attr = attr;
10491 msg_puts_attr(edit_submode_extra, sub_attr);
10492 }
10493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 }
10495 else
10496#endif
10497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 if (State & VREPLACE_FLAG)
10499 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010500 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10502 else if (State & INSERT)
10503 {
10504#ifdef FEAT_RIGHTLEFT
10505 if (p_ri)
10506 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10507#endif
10508 MSG_PUTS_ATTR(_(" INSERT"), attr);
10509 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010510 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 MSG_PUTS_ATTR(_(" (insert)"), attr);
10512 else if (restart_edit == 'R')
10513 MSG_PUTS_ATTR(_(" (replace)"), attr);
10514 else if (restart_edit == 'V')
10515 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10516#ifdef FEAT_RIGHTLEFT
10517 if (p_hkmap)
10518 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10519# ifdef FEAT_FKMAP
10520 if (p_fkmap)
10521 MSG_PUTS_ATTR(farsi_text_5, attr);
10522# endif
10523#endif
10524#ifdef FEAT_KEYMAP
10525 if (State & LANGMAP)
10526 {
10527# ifdef FEAT_ARABIC
10528 if (curwin->w_p_arab)
10529 MSG_PUTS_ATTR(_(" Arabic"), attr);
10530 else
10531# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010532 if (get_keymap_str(curwin, (char_u *)" (%s)",
10533 NameBuff, MAXPATHL))
10534 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 }
10536#endif
10537 if ((State & INSERT) && p_paste)
10538 MSG_PUTS_ATTR(_(" (paste)"), attr);
10539
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 if (VIsual_active)
10541 {
10542 char *p;
10543
10544 /* Don't concatenate separate words to avoid translation
10545 * problems. */
10546 switch ((VIsual_select ? 4 : 0)
10547 + (VIsual_mode == Ctrl_V) * 2
10548 + (VIsual_mode == 'V'))
10549 {
10550 case 0: p = N_(" VISUAL"); break;
10551 case 1: p = N_(" VISUAL LINE"); break;
10552 case 2: p = N_(" VISUAL BLOCK"); break;
10553 case 4: p = N_(" SELECT"); break;
10554 case 5: p = N_(" SELECT LINE"); break;
10555 default: p = N_(" SELECT BLOCK"); break;
10556 }
10557 MSG_PUTS_ATTR(_(p), attr);
10558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 MSG_PUTS_ATTR(" --", attr);
10560 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010561
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 need_clear = TRUE;
10563 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010564 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565#ifdef FEAT_INS_EXPAND
10566 && edit_submode == NULL /* otherwise it gets too long */
10567#endif
10568 )
10569 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010570 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 need_clear = TRUE;
10572 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010573
10574 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 if (need_clear || clear_cmdline)
10576 msg_clr_eos();
10577 msg_didout = FALSE; /* overwrite this message */
10578 length = msg_col;
10579 msg_col = 0;
10580 need_wait_return = nwr_save; /* never ask for hit-return for this */
10581 }
10582 else if (clear_cmdline && msg_silent == 0)
10583 /* Clear the whole command line. Will reset "clear_cmdline". */
10584 msg_clr_cmdline();
10585
10586#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 /* In Visual mode the size of the selected area must be redrawn. */
10588 if (VIsual_active)
10589 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590
10591 /* If the last window has no status line, the ruler is after the mode
10592 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010593 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010594 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#endif
10596 redraw_cmdline = FALSE;
10597 clear_cmdline = FALSE;
10598
10599 return length;
10600}
10601
10602/*
10603 * Position for a mode message.
10604 */
10605 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010606msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607{
10608 msg_col = 0;
10609 msg_row = Rows - 1;
10610}
10611
10612/*
10613 * Delete mode message. Used when ESC is typed which is expected to end
10614 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010615 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 */
10617 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010618unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619{
10620 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010621 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 */
10623 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10624 redraw_cmdline = TRUE; /* delete mode later */
10625 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010626 clearmode();
10627}
10628
10629/*
10630 * Clear the mode message.
10631 */
10632 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010633clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010634{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010635 int save_msg_row = msg_row;
10636 int save_msg_col = msg_col;
10637
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010638 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010639 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010640 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010641 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010642
10643 msg_col = save_msg_col;
10644 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645}
10646
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010647 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010648recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010649{
10650 MSG_PUTS_ATTR(_("recording"), attr);
10651 if (!shortmess(SHM_RECORDING))
10652 {
10653 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010654 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010655 MSG_PUTS_ATTR(s, attr);
10656 }
10657}
10658
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010659/*
10660 * Draw the tab pages line at the top of the Vim window.
10661 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010662 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010663draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010664{
10665 int tabcount = 0;
10666 tabpage_T *tp;
10667 int tabwidth;
10668 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010669 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010670 int attr;
10671 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010672 win_T *cwp;
10673 int wincount;
10674 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010675 int c;
10676 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010677 int attr_sel = HL_ATTR(HLF_TPS);
10678 int attr_nosel = HL_ATTR(HLF_TP);
10679 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010680 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010681 int room;
10682 int use_sep_chars = (t_colors < 8
10683#ifdef FEAT_GUI
10684 && !gui.in_use
10685#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010686#ifdef FEAT_TERMGUICOLORS
10687 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010688#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010689 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010690
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010691 if (ScreenLines == NULL)
10692 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010693 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010694
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010695#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010696 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010697 if (gui_use_tabline())
10698 {
10699 gui_update_tabline();
10700 return;
10701 }
10702#endif
10703
10704 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010705 return;
10706
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010707#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010708
10709 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10710 for (scol = 0; scol < Columns; ++scol)
10711 TabPageIdxs[scol] = 0;
10712
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010713 /* Use the 'tabline' option if it's set. */
10714 if (*p_tal != NUL)
10715 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010716 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010717
10718 /* Check for an error. If there is one we would loop in redrawing the
10719 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010720 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010721 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010722 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010723 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010724 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010725 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010726 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010727 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010728#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010729 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010730 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010731 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010732
Bram Moolenaar238a5642006-02-21 22:12:05 +000010733 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10734 if (tabwidth < 6)
10735 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010736
Bram Moolenaar238a5642006-02-21 22:12:05 +000010737 attr = attr_nosel;
10738 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010739 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010740 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10741 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010742 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010743 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010744
Bram Moolenaar238a5642006-02-21 22:12:05 +000010745 if (tp->tp_topframe == topframe)
10746 attr = attr_sel;
10747 if (use_sep_chars && col > 0)
10748 screen_putchar('|', 0, col++, attr);
10749
10750 if (tp->tp_topframe != topframe)
10751 attr = attr_nosel;
10752
10753 screen_putchar(' ', 0, col++, attr);
10754
10755 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010756 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010757 cwp = curwin;
10758 wp = firstwin;
10759 }
10760 else
10761 {
10762 cwp = tp->tp_curwin;
10763 wp = tp->tp_firstwin;
10764 }
10765
10766 modified = FALSE;
10767 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10768 if (bufIsChanged(wp->w_buffer))
10769 modified = TRUE;
10770 if (modified || wincount > 1)
10771 {
10772 if (wincount > 1)
10773 {
10774 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010775 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010776 if (col + len >= Columns - 3)
10777 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010778 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010779#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010780 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010781#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010782 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010783#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010784 );
10785 col += len;
10786 }
10787 if (modified)
10788 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10789 screen_putchar(' ', 0, col++, attr);
10790 }
10791
10792 room = scol - col + tabwidth - 1;
10793 if (room > 0)
10794 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010795 /* Get buffer name in NameBuff[] */
10796 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010797 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010798 len = vim_strsize(NameBuff);
10799 p = NameBuff;
10800#ifdef FEAT_MBYTE
10801 if (has_mbyte)
10802 while (len > room)
10803 {
10804 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010805 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010806 }
10807 else
10808#endif
10809 if (len > room)
10810 {
10811 p += len - room;
10812 len = room;
10813 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010814 if (len > Columns - col - 1)
10815 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010816
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010817 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010818 col += len;
10819 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010820 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010821
10822 /* Store the tab page number in TabPageIdxs[], so that
10823 * jump_to_mouse() knows where each one is. */
10824 ++tabcount;
10825 while (scol < col)
10826 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010827 }
10828
Bram Moolenaar238a5642006-02-21 22:12:05 +000010829 if (use_sep_chars)
10830 c = '_';
10831 else
10832 c = ' ';
10833 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010834
10835 /* Put an "X" for closing the current tab if there are several. */
10836 if (first_tabpage->tp_next != NULL)
10837 {
10838 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10839 TabPageIdxs[Columns - 1] = -999;
10840 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010841 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010842
10843 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10844 * set. */
10845 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010846}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010847
10848/*
10849 * Get buffer name for "buf" into NameBuff[].
10850 * Takes care of special buffer names and translates special characters.
10851 */
10852 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010853get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010854{
10855 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010856 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010857 else
10858 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10859 trans_characters(NameBuff, MAXPATHL);
10860}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010861
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862/*
10863 * Get the character to use in a status line. Get its attributes in "*attr".
10864 */
10865 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010866fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867{
10868 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010869
10870#ifdef FEAT_TERMINAL
10871 if (bt_terminal(wp->w_buffer))
10872 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010873 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010874 {
10875 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010876 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010877 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010878 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010879 {
10880 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010881 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010882 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010883 }
10884 else
10885#endif
10886 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010888 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 fill = fill_stl;
10890 }
10891 else
10892 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010893 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 fill = fill_stlnc;
10895 }
10896 /* Use fill when there is highlighting, and highlighting of current
10897 * window differs, or the fillchars differ, or this is not the
10898 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010899 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010900 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 || (fill_stl != fill_stlnc)))
10902 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010903 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 return '^';
10905 return '=';
10906}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908/*
10909 * Get the character to use in a separator between vertically split windows.
10910 * Get its attributes in "*attr".
10911 */
10912 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010913fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010915 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 if (*attr == 0 && fill_vert == ' ')
10917 return '|';
10918 else
10919 return fill_vert;
10920}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921
10922/*
10923 * Return TRUE if redrawing should currently be done.
10924 */
10925 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010926redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010928#ifdef FEAT_EVAL
10929 if (disable_redraw_for_testing)
10930 return 0;
10931 else
10932#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010933 return ((!RedrawingDisabled
10934#ifdef FEAT_EVAL
10935 || ignore_redraw_flag_for_testing
10936#endif
10937 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938}
10939
10940/*
10941 * Return TRUE if printing messages should currently be done.
10942 */
10943 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010944messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
10946 return (!(p_lz && char_avail() && !KeyTyped));
10947}
10948
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010949#ifdef FEAT_MENU
10950/*
10951 * Draw the window toolbar.
10952 */
10953 static void
10954redraw_win_toolbar(win_T *wp)
10955{
10956 vimmenu_T *menu;
10957 int item_idx = 0;
10958 int item_count = 0;
10959 int col = 0;
10960 int next_col;
10961 int off = (int)(current_ScreenLine - ScreenLines);
10962 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10963 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10964
10965 vim_free(wp->w_winbar_items);
10966 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10967 ++item_count;
10968 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10969 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10970
10971 /* TODO: use fewer spaces if there is not enough room */
10972 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010973 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010974 {
10975 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010976 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010977 break;
10978 if (col > 1)
10979 {
10980 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010981 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010982 break;
10983 }
10984
10985 wp->w_winbar_items[item_idx].wb_startcol = col;
10986 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010987 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010988 break;
10989
10990 next_col = text_to_screenline(wp, menu->name, col);
10991 while (col < next_col)
10992 {
10993 ScreenAttrs[off + col] = button_attr;
10994 ++col;
10995 }
10996 wp->w_winbar_items[item_idx].wb_endcol = col;
10997 wp->w_winbar_items[item_idx].wb_menu = menu;
10998 ++item_idx;
10999
Bram Moolenaar02631462017-09-22 15:20:32 +020011000 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011001 break;
11002 space_to_screenline(off + col, button_attr);
11003 ++col;
11004 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011005 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011006 {
11007 space_to_screenline(off + col, fill_attr);
11008 ++col;
11009 }
11010 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11011
Bram Moolenaar02631462017-09-22 15:20:32 +020011012 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11013 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011014}
11015#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011016
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017/*
11018 * Show current status info in ruler and various other places
11019 * If always is FALSE, only show ruler if position has changed.
11020 */
11021 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011022showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023{
11024 if (!always && !redrawing())
11025 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011026#ifdef FEAT_INS_EXPAND
11027 if (pum_visible())
11028 {
11029 /* Don't redraw right now, do it later. */
11030 curwin->w_redr_status = TRUE;
11031 return;
11032 }
11033#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011034#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011035 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011036 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011037 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 else
11040#endif
11041#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011042 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043#endif
11044
11045#ifdef FEAT_TITLE
11046 if (need_maketitle
11047# ifdef FEAT_STL_OPT
11048 || (p_icon && (stl_syntax & STL_IN_ICON))
11049 || (p_title && (stl_syntax & STL_IN_TITLE))
11050# endif
11051 )
11052 maketitle();
11053#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011054 /* Redraw the tab pages line if needed. */
11055 if (redraw_tabline)
11056 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057}
11058
11059#ifdef FEAT_CMDL_INFO
11060 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011061win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011063#define RULER_BUF_LEN 70
11064 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 int row;
11066 int fillchar;
11067 int attr;
11068 int empty_line = FALSE;
11069 colnr_T virtcol;
11070 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011071 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 int this_ru_col;
11074 int off = 0;
11075 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076
11077 /* If 'ruler' off or redrawing disabled, don't do anything */
11078 if (!p_ru)
11079 return;
11080
11081 /*
11082 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11083 * after deleting lines, before cursor.lnum is corrected.
11084 */
11085 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11086 return;
11087
11088#ifdef FEAT_INS_EXPAND
11089 /* Don't draw the ruler while doing insert-completion, it might overwrite
11090 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 if (edit_submode != NULL)
11093 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011094 // Don't draw the ruler when the popup menu is visible, it may overlap.
11095 // Except when the popup menu will be redrawn anyway.
11096 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011097 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098#endif
11099
11100#ifdef FEAT_STL_OPT
11101 if (*p_ruf)
11102 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011103 int save_called_emsg = called_emsg;
11104
11105 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011107 if (called_emsg)
11108 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011109 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011110 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 return;
11112 }
11113#endif
11114
11115 /*
11116 * Check if not in Insert mode and the line is empty (will show "0-1").
11117 */
11118 if (!(State & INSERT)
11119 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11120 empty_line = TRUE;
11121
11122 /*
11123 * Only draw the ruler when something changed.
11124 */
11125 validate_virtcol_win(wp);
11126 if ( redraw_cmdline
11127 || always
11128 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11129 || wp->w_cursor.col != wp->w_ru_cursor.col
11130 || wp->w_virtcol != wp->w_ru_virtcol
11131#ifdef FEAT_VIRTUALEDIT
11132 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11133#endif
11134 || wp->w_topline != wp->w_ru_topline
11135 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11136#ifdef FEAT_DIFF
11137 || wp->w_topfill != wp->w_ru_topfill
11138#endif
11139 || empty_line != wp->w_ru_empty)
11140 {
11141 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 if (wp->w_status_height)
11143 {
11144 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011145 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011146 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011147 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 }
11149 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150 {
11151 row = Rows - 1;
11152 fillchar = ' ';
11153 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 width = Columns;
11155 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 }
11157
11158 /* In list mode virtcol needs to be recomputed */
11159 virtcol = wp->w_virtcol;
11160 if (wp->w_p_list && lcs_tab1 == NUL)
11161 {
11162 wp->w_p_list = FALSE;
11163 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11164 wp->w_p_list = TRUE;
11165 }
11166
11167 /*
11168 * Some sprintfs return the length, some return a pointer.
11169 * To avoid portability problems we use strlen() here.
11170 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011171 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11173 ? 0L
11174 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011175 len = STRLEN(buffer);
11176 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11178 (int)virtcol + 1);
11179
11180 /*
11181 * Add a "50%" if there is room for it.
11182 * On the last line, don't print in the last column (scrolls the
11183 * screen up on some terminals).
11184 */
11185 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011186 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 this_ru_col = ru_col - (Columns - width);
11191 if (this_ru_col < 0)
11192 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 /* Never use more than half the window/screen width, leave the other
11194 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011195 if (this_ru_col < (width + 1) / 2)
11196 this_ru_col = (width + 1) / 2;
11197 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011199 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011200 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 {
11202#ifdef FEAT_MBYTE
11203 if (has_mbyte)
11204 i += (*mb_char2bytes)(fillchar, buffer + i);
11205 else
11206#endif
11207 buffer[i++] = fillchar;
11208 ++o;
11209 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011210 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 }
11212 /* Truncate at window boundary. */
11213#ifdef FEAT_MBYTE
11214 if (has_mbyte)
11215 {
11216 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011217 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 {
11219 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011220 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 {
11222 buffer[i] = NUL;
11223 break;
11224 }
11225 }
11226 }
11227 else
11228#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011229 if (this_ru_col + (int)STRLEN(buffer) > width)
11230 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231
Bram Moolenaar4033c552017-09-16 20:54:51 +020011232 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 i = redraw_cmdline;
11234 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011235 this_ru_col + off + (int)STRLEN(buffer),
11236 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 fillchar, fillchar, attr);
11238 /* don't redraw the cmdline because of showing the ruler */
11239 redraw_cmdline = i;
11240 wp->w_ru_cursor = wp->w_cursor;
11241 wp->w_ru_virtcol = wp->w_virtcol;
11242 wp->w_ru_empty = empty_line;
11243 wp->w_ru_topline = wp->w_topline;
11244 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11245#ifdef FEAT_DIFF
11246 wp->w_ru_topfill = wp->w_topfill;
11247#endif
11248 }
11249}
11250#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011251
11252#if defined(FEAT_LINEBREAK) || defined(PROTO)
11253/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011254 * Return the width of the 'number' and 'relativenumber' column.
11255 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011256 * Otherwise it depends on 'numberwidth' and the line count.
11257 */
11258 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011259number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011260{
11261 int n;
11262 linenr_T lnum;
11263
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011264 if (wp->w_p_rnu && !wp->w_p_nu)
11265 /* cursor line shows "0" */
11266 lnum = wp->w_height;
11267 else
11268 /* cursor line shows absolute line number */
11269 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011270
Bram Moolenaar6b314672015-03-20 15:42:10 +010011271 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011272 return wp->w_nrwidth_width;
11273 wp->w_nrwidth_line_count = lnum;
11274
11275 n = 0;
11276 do
11277 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011278 lnum /= 10;
11279 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011280 } while (lnum > 0);
11281
11282 /* 'numberwidth' gives the minimal width plus one */
11283 if (n < wp->w_p_nuw - 1)
11284 n = wp->w_p_nuw - 1;
11285
11286 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011287 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011288 return n;
11289}
11290#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011291
11292/*
11293 * Return the current cursor column. This is the actual position on the
11294 * screen. First column is 0.
11295 */
11296 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011297screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011298{
11299 return screen_cur_col;
11300}
11301
11302/*
11303 * Return the current cursor row. This is the actual position on the screen.
11304 * First row is 0.
11305 */
11306 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011307screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011308{
11309 return screen_cur_row;
11310}