blob: 01518882f29b45b62b3589d4b28289e11b84a55f [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/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020011 * screen.c: Lower level code for displaying on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012 *
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[].
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 */
39
40#include "vim.h"
41
42/*
43 * The attributes that are actually active for writing to the screen.
44 */
45static int screen_attr = 0;
46
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010048static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020049static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020051static 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 +010052static void win_rest_invalid(win_T *wp);
53static void msg_pos_mode(void);
54static void recording_mode(int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaar63d9e732019-12-05 21:10:38 +010056// Ugly global: overrule attribute used by screen_char()
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaar860cae12010-06-05 23:22:07 +020059#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020060/*
61 * Return TRUE if the cursor line in window "wp" may be concealed, according
62 * to the 'concealcursor' option.
63 */
64 int
Bram Moolenaar05540972016-01-30 20:31:25 +010065conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020066{
67 int c;
68
69 if (*wp->w_p_cocu == NUL)
70 return FALSE;
Bram Moolenaar24959102022-05-07 20:01:16 +010071 if (get_real_state() & MODE_VISUAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020072 c = 'v';
Bram Moolenaar24959102022-05-07 20:01:16 +010073 else if (State & MODE_INSERT)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020074 c = 'i';
Bram Moolenaar24959102022-05-07 20:01:16 +010075 else if (State & MODE_NORMAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020076 c = 'n';
Bram Moolenaar24959102022-05-07 20:01:16 +010077 else if (State & MODE_CMDLINE)
Bram Moolenaarca8c9862010-07-24 15:00:38 +020078 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +020079 else
80 return FALSE;
81 return vim_strchr(wp->w_p_cocu, c) != NULL;
82}
83
84/*
85 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
Bram Moolenaarea042672021-06-29 20:22:32 +020086 * To be called after changing the state, "was_concealed" is the value of
87 * "conceal_cursor_line()" before the change.
88 * "
Bram Moolenaarf5963f72010-07-23 22:10:27 +020089 */
90 void
Bram Moolenaarea042672021-06-29 20:22:32 +020091conceal_check_cursor_line(int was_concealed)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020092{
Bram Moolenaarea042672021-06-29 20:22:32 +020093 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin) != was_concealed)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020094 {
Bram Moolenaarea042672021-06-29 20:22:32 +020095 int wcol = curwin->w_wcol;
96
Bram Moolenaarf5963f72010-07-23 22:10:27 +020097 need_cursor_line_redraw = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +010098 // Need to recompute cursor column, e.g., when starting Visual mode
99 // without concealing.
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200100 curs_columns(TRUE);
Bram Moolenaarea042672021-06-29 20:22:32 +0200101
102 // When concealing now w_wcol will be computed wrong, keep the previous
103 // value, it will be updated in win_line().
104 if (!was_concealed)
105 curwin->w_wcol = wcol;
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200106 }
107}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200110/*
111 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
112 * window then get the "Pmenu" highlight attribute.
113 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200114 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200115get_wcr_attr(win_T *wp)
116{
117 int wcr_attr = 0;
118
119 if (*wp->w_p_wcr != NUL)
120 wcr_attr = syn_name2attr(wp->w_p_wcr);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100121#ifdef FEAT_PROP_POPUP
Bram Moolenaarc363fe12019-08-04 18:13:46 +0200122 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200123 {
124 if (wp->w_popup_flags & POPF_INFO)
125 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
126 else
127 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
128 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200129#endif
130 return wcr_attr;
131}
132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100134 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
135 * Return the new offset.
136 */
137 static int
138screen_fill_end(
139 win_T *wp,
140 int c1,
141 int c2,
142 int off,
143 int width,
144 int row,
145 int endrow,
146 int attr)
147{
148 int nn = off + width;
149
150 if (nn > wp->w_width)
151 nn = wp->w_width;
152#ifdef FEAT_RIGHTLEFT
153 if (wp->w_p_rl)
154 {
155 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
156 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
157 c1, c2, attr);
158 }
159 else
160#endif
161 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
162 wp->w_wincol + off, (int)wp->w_wincol + nn,
163 c1, c2, attr);
164 return nn;
165}
166
167/*
168 * Clear lines near the end the window and mark the unused lines with "c1".
169 * use "c2" as the filler character.
170 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200172 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100173win_draw_end(
174 win_T *wp,
175 int c1,
176 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100177 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +0100178 int row,
179 int endrow,
180 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200183 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200184 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200185
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200186 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100187
188 if (draw_margin)
189 {
Bram Moolenaar1c934292015-01-27 16:39:29 +0100190#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100191 int fdc = compute_foldcolumn(wp, 0);
192
193 if (fdc > 0)
194 // draw the fold column
195 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200196 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +0100197#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100198#ifdef FEAT_SIGNS
199 if (signcolumn_on(wp))
200 // draw the sign column
201 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200202 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100203#endif
204 if ((wp->w_p_nu || wp->w_p_rnu)
205 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
206 // draw the number column
207 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200208 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
211#ifdef FEAT_RIGHTLEFT
212 if (wp->w_p_rl)
213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100215 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200216 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100218 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200219 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 }
221 else
222#endif
223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100225 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200226 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 set_empty_rows(wp, row);
230}
231
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200232#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233/*
Bram Moolenaar1c934292015-01-27 16:39:29 +0100234 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
235 * space is available for window "wp", minus "col".
236 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200237 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100238compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +0100239{
240 int fdc = wp->w_p_fdc;
241 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +0200242 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100243
244 if (fdc > wwidth - (col + wmw))
245 fdc = wwidth - (col + wmw);
246 return fdc;
247}
248
249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000251 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100252 * Returns the number of bytes stored in 'p'. When non-multibyte characters are
253 * used for the fold column markers, this is equal to 'fdc' setting. Otherwise,
254 * this will be greater than 'fdc'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 */
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100256 size_t
Bram Moolenaar05540972016-01-30 20:31:25 +0100257fill_foldcolumn(
258 char_u *p,
259 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100260 int closed, // TRUE of FALSE
261 linenr_T lnum) // current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262{
263 int i = 0;
264 int level;
265 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000266 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100267 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100268 size_t byte_counter = 0;
269 int symbol = 0;
270 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100272 // Init to all spaces.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100273 vim_memset(p, ' ', MAX_MCO * fdc + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275 level = win_foldinfo.fi_level;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100276 empty = (fdc == 1) ? 0 : 1;
277
278 // If the column is too narrow, we start at the lowest level that
Bram Moolenaar008bff92021-03-04 21:55:58 +0100279 // fits and use numbers to indicate the depth.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100280 first_level = level - fdc - closed + 1 + empty;
281 if (first_level < 1)
282 first_level = 1;
283
284 for (i = 0; i < MIN(fdc, level); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100286 if (win_foldinfo.fi_lnum == lnum
287 && first_level + i >= win_foldinfo.fi_low_level)
288 symbol = fill_foldopen;
289 else if (first_level == 1)
290 symbol = fill_foldsep;
291 else if (first_level + i <= 9)
292 symbol = '0' + first_level + i;
293 else
294 symbol = '>';
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000295
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100296 len = utf_char2bytes(symbol, &p[byte_counter]);
297 byte_counter += len;
298 if (first_level + i >= level)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100300 i++;
301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 }
303 }
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100304
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 if (closed)
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100306 {
307 if (symbol != 0)
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100308 {
309 // rollback length and the character
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100310 byte_counter -= len;
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100311 if (len > 1)
312 // for a multibyte character, erase all the bytes
313 vim_memset(p + byte_counter, ' ', len);
314 }
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100315 symbol = fill_foldclosed;
316 len = utf_char2bytes(symbol, &p[byte_counter]);
317 byte_counter += len;
318 }
319
320 return MAX(byte_counter + (fdc - i), (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321}
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100322#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000324/*
325 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +0100326 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000327 */
328 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100329comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000330{
331 int i;
332
333 for (i = 0; i < Screen_mco; ++i)
334 {
335 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
336 return TRUE;
337 if (ScreenLinesC[i][off_from] == 0)
338 break;
339 }
340 return FALSE;
341}
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000342
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343/*
344 * Check whether the given character needs redrawing:
345 * - the (first byte of the) character is different
346 * - the attributes are different
347 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000348 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 */
350 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100351char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
353 if (cols > 0
354 && ((ScreenLines[off_from] != ScreenLines[off_to]
355 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 || (enc_dbcs != 0
357 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
358 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
359 ? ScreenLines2[off_from] != ScreenLines2[off_to]
360 : (cols > 1 && ScreenLines[off_from + 1]
361 != ScreenLines[off_to + 1])))
362 || (enc_utf8
363 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
364 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000365 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +0200366 || ((*mb_off2cells)(off_from, off_from + cols) > 1
367 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +0100368 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 return TRUE;
370 return FALSE;
371}
372
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200373#if defined(FEAT_TERMINAL) || defined(PROTO)
374/*
375 * Return the index in ScreenLines[] for the current screen line.
376 */
377 int
378screen_get_current_line_off()
379{
380 return (int)(current_ScreenLine - ScreenLines);
381}
382#endif
383
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100384#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200385/*
386 * Return TRUE if this position has a higher level popup or this cell is
387 * transparent in the current popup.
388 */
389 static int
390blocked_by_popup(int row, int col)
391{
392 int off;
393
394 if (!popup_visible)
395 return FALSE;
396 off = row * screen_Columns + col;
397 return popup_mask[off] > screen_zindex || popup_transparent[off];
398}
399#endif
400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200402 * Reset the highlighting. Used before clearing the screen.
403 */
404 void
405reset_screen_attr(void)
406{
407#ifdef FEAT_GUI
408 if (gui.in_use)
409 // Use a code that will reset gui.highlight_mask in
410 // gui_stop_highlight().
411 screen_attr = HL_ALL + 1;
412 else
413#endif
414 // Use attributes that is very unlikely to appear in text.
415 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
416}
417
418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 * Move one "cooked" screen line to the screen, but only the characters that
420 * have actually changed. Handle insert/delete character.
421 * "coloff" gives the first column on the screen for this line.
422 * "endcol" gives the columns where valid characters are.
423 * "clear_width" is the width of the window. It's > 0 if the rest of the line
424 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200425 * "flags" can have bits:
426 * SLF_POPUP popup window
427 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
429 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
430 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200431 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100432screen_line(
433 int row,
434 int coloff,
435 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200436 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200437 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 unsigned off_from;
440 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000441 unsigned max_off_from;
442 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100445 int force = FALSE; // force update rest of the line
446 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100448 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449#endif
450 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100451 int redraw_next; // redraw_this for next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100453 int char_cells; // 1: normal char
454 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100457 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100458 if (row >= Rows)
459 row = Rows - 1;
460 if (endcol > Columns)
461 endcol = Columns;
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463# ifdef FEAT_CLIPBOARD
464 clip_may_clear_selection(row, row);
465# endif
466
467 off_from = (unsigned)(current_ScreenLine - ScreenLines);
468 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000469 max_off_from = off_from + screen_Columns;
470 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471
472#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200473 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100475 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (clear_width > 0)
477 {
478 while (col <= endcol && ScreenLines[off_to] == ' '
479 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100480 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {
482 ++off_to;
483 ++col;
484 }
485 if (col <= endcol)
486 screen_fill(row, row + 1, col + coloff,
487 endcol + coloff + 1, ' ', ' ', 0);
488 }
489 col = endcol + 1;
490 off_to = LineOffset[row] + col + coloff;
491 off_from += col;
492 endcol = (clear_width > 0 ? clear_width : -clear_width);
493 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100494#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100496#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100497 // First char of a popup window may go on top of the right half of a
498 // double-wide character. Clear the left half to avoid it getting the popup
499 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100500 if (coloff > 0 && enc_utf8
501 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200502 && ScreenLinesUC[off_to - 1] != 0
503 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100504 {
505 ScreenLines[off_to - 1] = ' ';
506 ScreenLinesUC[off_to - 1] = 0;
507 screen_char(off_to - 1, row, col + coloff - 1);
508 }
509#endif
510
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
512
513 while (col < endcol)
514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000516 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 else
518 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520 redraw_this = redraw_next;
521 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
522 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
523
524#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100525 // If the next character was bold, then redraw the current character to
526 // remove any pixels that might have spilt over into us. This only
527 // happens in the GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 if (redraw_next && gui.in_use)
529 {
530 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000531 if (hl > HL_ALL)
532 hl = syn_attr2attr(hl);
533 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 redraw_this = TRUE;
535 }
536#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100537#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200538 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200539 redraw_this = FALSE;
540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (redraw_this)
542 {
543 /*
544 * Special handling when 'xs' termcap flag set (hpterm):
545 * Attributes for characters are stored at the position where the
546 * cursor is when writing the highlighting code. The
547 * start-highlighting code must be written with the cursor on the
548 * first highlighted character. The stop-highlighting code must
549 * be written with the cursor just after the last highlighted
550 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100551 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 * to clear the rest of the line, and force redrawing it
553 * completely.
554 */
555 if ( p_wiv
556 && !force
557#ifdef FEAT_GUI
558 && !gui.in_use
559#endif
560 && ScreenAttrs[off_to] != 0
561 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
562 {
563 /*
564 * Need to remove highlighting attributes here.
565 */
566 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100567 out_str(T_CE); // clear rest of this screen line
568 screen_start(); // don't know where cursor is now
569 force = TRUE; // force redraw of rest of the line
570 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572 /*
573 * If the previous character was highlighted, need to stop
574 * highlighting at this character.
575 */
576 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
577 {
578 screen_attr = ScreenAttrs[off_to - 1];
579 term_windgoto(row, col + coloff);
580 screen_stop_highlight();
581 }
582 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100583 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (enc_dbcs != 0)
586 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100587 // Check if overwriting a double-byte with a single-byte or
588 // the other way around requires another character to be
589 // redrawn. For UTF-8 this isn't needed, because comparing
590 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 if (char_cells == 1
592 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000593 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100595 // Writing a single-cell character over a double-cell
596 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 ScreenLines[off_to + 1] = 0;
598 redraw_next = TRUE;
599 }
600 else if (char_cells == 2
601 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000602 && (*mb_off2cells)(off_to, max_off_to) == 1
603 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100605 // Writing the second half of a double-cell character over
606 // a double-cell character: need to redraw the second
607 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 ScreenLines[off_to + 2] = 0;
609 redraw_next = TRUE;
610 }
611
612 if (enc_dbcs == DBCS_JPNU)
613 ScreenLines2[off_to] = ScreenLines2[off_from];
614 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100615 // When writing a single-width character over a double-width
616 // character and at the end of the redrawn text, need to clear out
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000617 // the right half of the old character.
618 // Also required when writing the right half of a double-width
619 // char over the left half of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (has_mbyte && col + char_cells == endcol
621 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000622 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000624 && (*mb_off2cells)(off_to, max_off_to) == 1
625 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 if (enc_utf8)
630 {
631 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
632 if (ScreenLinesUC[off_from] != 0)
633 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000634 int i;
635
636 for (i = 0; i < Screen_mco; ++i)
637 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
639 }
640 if (char_cells == 2)
641 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
643#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100644 // The bold trick makes a single column of pixels appear in the
645 // next character. When a bold character is removed, the next
646 // character should be redrawn too. This happens for our own GUI
647 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (
649# ifdef FEAT_GUI
650 gui.in_use
651# endif
652# if defined(FEAT_GUI) && defined(UNIX)
653 ||
654# endif
655# ifdef UNIX
656 term_is_xterm
657# endif
658 )
659 {
660 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000661 if (hl > HL_ALL)
662 hl = syn_attr2attr(hl);
663 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 redraw_next = TRUE;
665 }
666#endif
667 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100668
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100669 // For simplicity set the attributes of second half of a
670 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000671 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000673
674 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 screen_char(off_to, row, col + coloff);
678 }
679 else if ( p_wiv
680#ifdef FEAT_GUI
681 && !gui.in_use
682#endif
683 && col + coloff > 0)
684 {
685 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
686 {
687 /*
688 * Don't output stop-highlight when moving the cursor, it will
689 * stop the highlighting when it should continue.
690 */
691 screen_attr = 0;
692 }
693 else if (screen_attr != 0)
694 screen_stop_highlight();
695 }
696
697 off_to += CHAR_CELLS;
698 off_from += CHAR_CELLS;
699 col += CHAR_CELLS;
700 }
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 if (clear_next)
703 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100704 // Clear the second half of a double-wide character of which the left
705 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 ScreenLines[off_to] = ' ';
707 if (enc_utf8)
708 ScreenLinesUC[off_to] = 0;
709 screen_char(off_to, row, col + coloff);
710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712 if (clear_width > 0
713#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200714 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715#endif
716 )
717 {
718#ifdef FEAT_GUI
719 int startCol = col;
720#endif
721
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100722 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 while (col < clear_width && ScreenLines[off_to] == ' '
724 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100725 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
727 ++off_to;
728 ++col;
729 }
730 if (col < clear_width)
731 {
732#ifdef FEAT_GUI
733 /*
734 * In the GUI, clearing the rest of the line may leave pixels
735 * behind if the first character cleared was bold. Some bold
736 * fonts spill over the left. In this case we redraw the previous
737 * character too. If we didn't skip any blanks above, then we
738 * only redraw if the character wasn't already redrawn anyway.
739 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000740 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
742 hl = ScreenAttrs[off_to];
743 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000744 {
745 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100746
Bram Moolenaar9c697322006-10-09 20:11:17 +0000747 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100748 // for utf-8, ScreenLines[char_offset + 1] == 0 means
749 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000750 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
751 else if (enc_dbcs != 0)
752 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100753 // find previous character by counting from first
754 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000755 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000756 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000757
758 while (off < off_to)
759 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000760 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000761 off += prev_cells;
762 }
763 }
764
765 if (enc_dbcs != 0 && prev_cells > 1)
766 screen_char_2(off_to - prev_cells, row,
767 col + coloff - prev_cells);
768 else
Bram Moolenaar9c697322006-10-09 20:11:17 +0000769 screen_char(off_to - prev_cells, row,
770 col + coloff - prev_cells);
771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 }
773#endif
774 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
775 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 off_to += clear_width - col;
777 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
779 }
780
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200781 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100782#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200783 && !(flags & SLF_POPUP) // no separator for popup window
784#endif
785 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200787 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200788 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200789 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100791#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200792 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200795 int c;
796
797 c = fillchar_vsep(&hl);
798 if (ScreenLines[off_to] != (schar_T)c
799 || (enc_utf8 && (int)ScreenLinesUC[off_to]
800 != (c >= 0x80 ? c : 0))
801 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200803 ScreenLines[off_to] = c;
804 ScreenAttrs[off_to] = hl;
805 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200807 if (c >= 0x80)
808 {
809 ScreenLinesUC[off_to] = c;
810 ScreenLinesC[0][off_to] = 0;
811 }
812 else
813 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200815 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
818 }
819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 LineWraps[row] = FALSE;
821 }
822}
823
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000824#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000826 * Mirror text "str" for right-left displaying.
827 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000829 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100830rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
832 char_u *p1, *p2;
833 int t;
834
835 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
836 {
837 t = *p1;
838 *p1 = *p2;
839 *p2 = t;
840 }
841}
842#endif
843
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 * Draw the verticap separator right of window "wp" starting with line "row".
846 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200847 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100848draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849{
850 int hl;
851 int c;
852
853 if (wp->w_vsep_width)
854 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100855 // draw the vertical separator right of this window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 c = fillchar_vsep(&hl);
857 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
858 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
859 c, ' ', hl);
860 }
861}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100864static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866/*
Bram Moolenaar367329b2007-08-30 11:53:22 +0000867 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 */
869 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100870status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871{
872 int len = 0;
873
874#ifdef FEAT_MENU
875 int emenu = (xp->xp_context == EXPAND_MENUS
876 || xp->xp_context == EXPAND_MENUNAMES);
877
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100878 // Check for menu separators - replace with '|'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (emenu && menu_is_separator(s))
880 return 1;
881#endif
882
883 while (*s != NUL)
884 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000885 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +0000886 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100887 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889
890 return len;
891}
892
893/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000894 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000895 * These are backslashes used for escaping. Do show backslashes in help tags.
896 */
897 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100898skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000899{
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000900 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000901#ifdef FEAT_MENU
902 || ((xp->xp_context == EXPAND_MENUS
903 || xp->xp_context == EXPAND_MENUNAMES)
904 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
905#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000906 )
907 {
908#ifndef BACKSLASH_IN_FILENAME
909 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
910 return 2;
911#endif
912 return 1;
913 }
914 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000915}
916
917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 * Show wildchar matches in the status line.
919 * Show at least the "match" item.
920 * We start at item 'first_match' in the list and show all matches that fit.
921 *
922 * If inversion is possible we use it. Else '=' characters are used.
923 */
924 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100925win_redr_status_matches(
926 expand_T *xp,
927 int num_matches,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100928 char_u **matches, // list of matches
Bram Moolenaar05540972016-01-30 20:31:25 +0100929 int match,
930 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
933 int row;
934 char_u *buf;
935 int len;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100936 int clen; // length in screen cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 int fillchar;
938 int attr;
939 int i;
940 int highlight = TRUE;
941 char_u *selstart = NULL;
942 int selstart_col = 0;
943 char_u *selend = NULL;
944 static int first_match = 0;
945 int add_left = FALSE;
946 char_u *s;
947#ifdef FEAT_MENU
948 int emenu;
949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100952 if (matches == NULL) // interrupted completion?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 return;
954
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000955 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200956 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000957 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200958 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (buf == NULL)
960 return;
961
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100962 if (match == -1) // don't show match but original text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 {
964 match = 0;
965 highlight = FALSE;
966 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100967 // count 1 for the ending ">"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 clen = status_match_len(xp, L_MATCH(match)) + 3;
969 if (match == 0)
970 first_match = 0;
971 else if (match < first_match)
972 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100973 // jumping left, as far as we can go
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 first_match = match;
975 add_left = TRUE;
976 }
977 else
978 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100979 // check if match fits on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 for (i = first_match; i < match; ++i)
981 clen += status_match_len(xp, L_MATCH(i)) + 2;
982 if (first_match > 0)
983 clen += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100984 // jumping right, put match at the left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if ((long)clen > Columns)
986 {
987 first_match = match;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100988 // if showing the last match, we can add some on the left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 clen = 2;
990 for (i = match; i < num_matches; ++i)
991 {
992 clen += status_match_len(xp, L_MATCH(i)) + 2;
993 if ((long)clen >= Columns)
994 break;
995 }
996 if (i == num_matches)
997 add_left = TRUE;
998 }
999 }
1000 if (add_left)
1001 while (first_match > 0)
1002 {
1003 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
1004 if ((long)clen >= Columns)
1005 break;
1006 --first_match;
1007 }
1008
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001009 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011 if (first_match == 0)
1012 {
1013 *buf = NUL;
1014 len = 0;
1015 }
1016 else
1017 {
1018 STRCPY(buf, "< ");
1019 len = 2;
1020 }
1021 clen = len;
1022
1023 i = first_match;
1024 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
1025 {
1026 if (i == match)
1027 {
1028 selstart = buf + len;
1029 selstart_col = clen;
1030 }
1031
1032 s = L_MATCH(i);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001033 // Check for menu separators - replace with '|'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034#ifdef FEAT_MENU
1035 emenu = (xp->xp_context == EXPAND_MENUS
1036 || xp->xp_context == EXPAND_MENUNAMES);
1037 if (emenu && menu_is_separator(s))
1038 {
1039 STRCPY(buf + len, transchar('|'));
1040 l = (int)STRLEN(buf + len);
1041 len += l;
1042 clen += l;
1043 }
1044 else
1045#endif
1046 for ( ; *s != NUL; ++s)
1047 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001048 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001050 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
1052 STRNCPY(buf + len, s, l);
1053 s += l - 1;
1054 len += l;
1055 }
1056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 {
1058 STRCPY(buf + len, transchar_byte(*s));
1059 len += (int)STRLEN(buf + len);
1060 }
1061 }
1062 if (i == match)
1063 selend = buf + len;
1064
1065 *(buf + len++) = ' ';
1066 *(buf + len++) = ' ';
1067 clen += 2;
1068 if (++i == num_matches)
1069 break;
1070 }
1071
1072 if (i != num_matches)
1073 {
1074 *(buf + len++) = '>';
1075 ++clen;
1076 }
1077
1078 buf[len] = NUL;
1079
1080 row = cmdline_row - 1;
1081 if (row >= 0)
1082 {
1083 if (wild_menu_showing == 0)
1084 {
1085 if (msg_scrolled > 0)
1086 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001087 // Put the wildmenu just above the command line. If there is
1088 // no room, scroll the screen one line up.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (cmdline_row == Rows - 1)
1090 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001091 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 ++msg_scrolled;
1093 }
1094 else
1095 {
1096 ++cmdline_row;
1097 ++row;
1098 }
1099 wild_menu_showing = WM_SCROLLED;
1100 }
1101 else
1102 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001103 // Create status line if needed by setting 'laststatus' to 2.
1104 // Set 'winminheight' to zero to avoid that the window is
1105 // resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (lastwin->w_status_height == 0)
1107 {
1108 save_p_ls = p_ls;
1109 save_p_wmh = p_wmh;
1110 p_ls = 2;
1111 p_wmh = 0;
1112 last_status(FALSE);
1113 }
1114 wild_menu_showing = WM_SHOWN;
1115 }
1116 }
1117
1118 screen_puts(buf, row, 0, attr);
1119 if (selstart != NULL && highlight)
1120 {
1121 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001122 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
1124
1125 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
1126 }
1127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 vim_free(buf);
1130}
1131#endif
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * Return TRUE if the status line of window "wp" is connected to the status
1135 * line of the window right of it. If not, then it's a vertical separator.
1136 * Only call if (wp->w_vsep_width != 0).
1137 */
1138 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001139stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140{
1141 frame_T *fr;
1142
1143 fr = wp->w_frame;
1144 while (fr->fr_parent != NULL)
1145 {
1146 if (fr->fr_parent->fr_layout == FR_COL)
1147 {
1148 if (fr->fr_next != NULL)
1149 break;
1150 }
1151 else
1152 {
1153 if (fr->fr_next != NULL)
1154 return TRUE;
1155 }
1156 fr = fr->fr_parent;
1157 }
1158 return FALSE;
1159}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162/*
1163 * Get the value to show for the language mappings, active 'keymap'.
1164 */
1165 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001166get_keymap_str(
1167 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001168 char_u *fmt, // format string containing one %s item
1169 char_u *buf, // buffer for the result
1170 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 char_u *p;
1173
1174 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
1175 return FALSE;
1176
1177 {
1178#ifdef FEAT_EVAL
1179 buf_T *old_curbuf = curbuf;
1180 win_T *old_curwin = curwin;
1181 char_u *s;
1182
1183 curbuf = wp->w_buffer;
1184 curwin = wp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001185 STRCPY(buf, "b:keymap_name"); // must be writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001187 s = p = eval_to_string(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 --emsg_skip;
1189 curbuf = old_curbuf;
1190 curwin = old_curwin;
1191 if (p == NULL || *p == NUL)
1192#endif
1193 {
1194#ifdef FEAT_KEYMAP
1195 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
1196 p = wp->w_buffer->b_p_keymap;
1197 else
1198#endif
1199 p = (char_u *)"lang";
1200 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001201 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 buf[0] = NUL;
1203#ifdef FEAT_EVAL
1204 vim_free(s);
1205#endif
1206 }
1207 return buf[0] != NUL;
1208}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
1210#if defined(FEAT_STL_OPT) || defined(PROTO)
1211/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001212 * Redraw the status line or ruler of window "wp".
1213 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001215 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001216win_redr_custom(
1217 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001218 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001220 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 int attr;
1222 int curattr;
1223 int row;
1224 int col = 0;
1225 int maxwidth;
1226 int width;
1227 int n;
1228 int len;
1229 int fillchar;
1230 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001231 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 char_u *p;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001233 stl_hlrec_T *hltab;
1234 stl_hlrec_T *tabtab;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001235 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01001236 win_T *ewp;
1237 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001239 // There is a tiny chance that this gets called recursively: When
1240 // redrawing a status line triggers redrawing the ruler or tabline.
1241 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001242 if (entered)
1243 return;
1244 entered = TRUE;
1245
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001246 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001247 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001249 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001250 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001251 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001252 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001253 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001254 maxwidth = Columns;
1255# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001256 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001259 else
1260 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001261 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001262 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001263 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001264
1265 if (draw_ruler)
1266 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001267 stl = p_ruf;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001268 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001269 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001270 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001271 if (*++stl == '-')
1272 stl++;
1273 if (atoi((char *)stl))
1274 while (VIM_ISDIGIT(*stl))
1275 stl++;
1276 if (*stl++ != '(')
1277 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001278 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001279 col = ru_col - (Columns - wp->w_width);
1280 if (col < (wp->w_width + 1) / 2)
1281 col = (wp->w_width + 1) / 2;
1282 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001283 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001284 {
1285 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001286 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001287 fillchar = ' ';
1288 attr = 0;
1289 }
1290
1291# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001292 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001293# endif
1294 }
1295 else
1296 {
1297 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00001298 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001299 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001300 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001301# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001302 use_sandbox = was_set_insecurely((char_u *)"statusline",
1303 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001304# endif
1305 }
1306
Bram Moolenaar53f81742017-09-22 14:35:51 +02001307 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001308 }
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001311 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001313 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1314 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001315 ewp = wp == NULL ? curwin : wp;
1316 p_crb_save = ewp->w_p_crb;
1317 ewp->w_p_crb = FALSE;
1318
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001319 // Make a copy, because the statusline may include a function call that
1320 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001321 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001322 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00001323 stl, use_sandbox,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001324 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001325 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001326 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001328 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001329 p = transstr(buf);
1330 if (p != NULL)
1331 {
1332 vim_strncpy(buf, p, sizeof(buf) - 1);
1333 vim_free(p);
1334 }
1335
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001336 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001337 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001338 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ++width;
1342 }
1343 buf[len] = NUL;
1344
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001345 /*
1346 * Draw each snippet with the specified highlighting.
1347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 curattr = attr;
1349 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001350 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001352 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 screen_puts_len(p, len, row, col, curattr);
1354 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001355 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001357 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001359 else if (hltab[n].userhl < 0)
1360 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001361#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001362 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1363 && wp->w_status_height != 0)
1364 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001365 else if (wp != NULL && bt_terminal(wp->w_buffer)
1366 && wp->w_status_height != 0)
1367 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001368#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001369 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001370 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001372 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001375
1376 if (wp == NULL)
1377 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001378 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001379 col = 0;
1380 len = 0;
1381 p = buf;
1382 fillchar = 0;
1383 for (n = 0; tabtab[n].start != NULL; n++)
1384 {
1385 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1386 while (col < len)
1387 TabPageIdxs[col++] = fillchar;
1388 p = tabtab[n].start;
1389 fillchar = tabtab[n].userhl;
1390 }
1391 while (col < Columns)
1392 TabPageIdxs[col++] = fillchar;
1393 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001394
1395theend:
1396 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397}
1398
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001399#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
1401/*
1402 * Output a single character directly to the screen and update ScreenLines.
1403 */
1404 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001405screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 char_u buf[MB_MAXBYTES + 1];
1408
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001409 if (has_mbyte)
1410 buf[(*mb_char2bytes)(c, buf)] = NUL;
1411 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001412 {
1413 buf[0] = c;
1414 buf[1] = NUL;
1415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 screen_puts(buf, row, col, attr);
1417}
1418
1419/*
1420 * Get a single character directly from ScreenLines into "bytes[]".
1421 * Also return its attribute in *attrp;
1422 */
1423 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001424screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 unsigned off;
1427
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001428 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1430 {
1431 off = LineOffset[row] + col;
1432 *attrp = ScreenAttrs[off];
1433 bytes[0] = ScreenLines[off];
1434 bytes[1] = NUL;
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (enc_utf8 && ScreenLinesUC[off] != 0)
1437 bytes[utfc_char2bytes(off, bytes)] = NUL;
1438 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1439 {
1440 bytes[0] = ScreenLines[off];
1441 bytes[1] = ScreenLines2[off];
1442 bytes[2] = NUL;
1443 }
1444 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1445 {
1446 bytes[1] = ScreenLines[off + 1];
1447 bytes[2] = NUL;
1448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450}
1451
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001452/*
1453 * Return TRUE if composing characters for screen posn "off" differs from
1454 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001455 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001456 */
1457 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001458screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001459{
1460 int i;
1461
1462 for (i = 0; i < Screen_mco; ++i)
1463 {
1464 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1465 return TRUE;
1466 if (u8cc[i] == 0)
1467 break;
1468 }
1469 return FALSE;
1470}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472/*
1473 * Put string '*text' on the screen at position 'row' and 'col', with
1474 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1475 * Note: only outputs within one row, message is truncated at screen boundary!
1476 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1477 */
1478 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001479screen_puts(
1480 char_u *text,
1481 int row,
1482 int col,
1483 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 screen_puts_len(text, -1, row, col, attr);
1486}
1487
1488/*
1489 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1490 * a NUL.
1491 */
1492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001493screen_puts_len(
1494 char_u *text,
1495 int textlen,
1496 int row,
1497 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001498 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001500 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 unsigned off;
1502 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001503 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001505 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 int mbyte_blen = 1;
1507 int mbyte_cells = 1;
1508 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001509 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001511#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001512 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001513 int pc, nc, nc1;
1514 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001516 int force_redraw_this;
1517 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001518 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001520 // Safety check. The check for negative row and column is to fix issue
1521 // #4102. TODO: find out why row/col could be negative.
1522 if (ScreenLines == NULL
1523 || row >= screen_Rows || row < 0
1524 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001526 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001528 // When drawing over the right half of a double-wide char clear out the
1529 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001530 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001531#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001532 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001533#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001534 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001535 {
1536 ScreenLines[off - 1] = ' ';
1537 ScreenAttrs[off - 1] = 0;
1538 if (enc_utf8)
1539 {
1540 ScreenLinesUC[off - 1] = 0;
1541 ScreenLinesC[0][off - 1] = 0;
1542 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001543 // redraw the previous cell, make it empty
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001544 screen_char(off - 1, row, col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001545 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001546 force_redraw_next = TRUE;
1547 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001548
Bram Moolenaar367329b2007-08-30 11:53:22 +00001549 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001550 while (col < screen_Columns
1551 && (len < 0 || (int)(ptr - text) < len)
1552 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
1554 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001555 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (has_mbyte)
1557 {
1558 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001559 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001561 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1563 mbyte_cells = 1;
1564 else if (enc_dbcs != 0)
1565 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001566 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
1568 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001569 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 (int)((text + len) - ptr));
1571 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001572 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001574#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1576 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001577 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1579 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001580 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 nc = NUL;
1582 nc1 = NUL;
1583 }
1584 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001585 {
Bram Moolenaar54620182009-11-11 16:07:20 +00001586 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1587 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001588 nc1 = pcc[0];
1589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 pc = prev_c;
1591 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001592 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
1594 else
1595 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001596#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001597 if (col + mbyte_cells > screen_Columns)
1598 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001599 // Only 1 cell left, but character requires 2 cells:
1600 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001601 c = '>';
1602 mbyte_cells = 1;
1603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001607 force_redraw_this = force_redraw_next;
1608 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001609
1610 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 || (mbyte_cells == 2
1612 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1613 || (enc_dbcs == DBCS_JPNU
1614 && c == 0x8e
1615 && ScreenLines2[off] != ptr[1])
1616 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001617 && (ScreenLinesUC[off] !=
1618 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1619 || (ScreenLinesUC[off] != 0
1620 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001622 || exmode_active;
1623
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001624 if ((need_redraw || force_redraw_this)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001625#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001626 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001627#endif
1628 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001631 // The bold trick makes a single row of pixels appear in the next
1632 // character. When a bold character is removed, the next
1633 // character should be redrawn too. This happens for our own GUI
1634 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001635 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636# ifdef FEAT_GUI
1637 gui.in_use
1638# endif
1639# if defined(FEAT_GUI) && defined(UNIX)
1640 ||
1641# endif
1642# ifdef UNIX
1643 term_is_xterm
1644# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001645 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001647 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001649 if (n > HL_ALL)
1650 n = syn_attr2attr(n);
1651 if (n & HL_BOLD)
1652 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001655 // When at the end of the text and overwriting a two-cell
1656 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001657 // cell. Also when overwriting the left half of a two-cell char
1658 // with the right half of a two-cell char. Do this only once
1659 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (clear_next_cell)
1661 clear_next_cell = FALSE;
1662 else if (has_mbyte
1663 && (len < 0 ? ptr[mbyte_blen] == NUL
1664 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001665 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001667 && (*mb_off2cells)(off, max_off) == 1
1668 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 clear_next_cell = TRUE;
1670
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001671 // Make sure we never leave a second byte of a double-byte behind,
1672 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001674 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001676 && (*mb_off2cells)(off, max_off) == 1
1677 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 ScreenLines[off] = c;
1680 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (enc_utf8)
1682 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001683 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 ScreenLinesUC[off] = 0;
1685 else
1686 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001687 int i;
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001690 for (i = 0; i < Screen_mco; ++i)
1691 {
1692 ScreenLinesC[i][off] = u8cc[i];
1693 if (u8cc[i] == 0)
1694 break;
1695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 }
1697 if (mbyte_cells == 2)
1698 {
1699 ScreenLines[off + 1] = 0;
1700 ScreenAttrs[off + 1] = attr;
1701 }
1702 screen_char(off, row, col);
1703 }
1704 else if (mbyte_cells == 2)
1705 {
1706 ScreenLines[off + 1] = ptr[1];
1707 ScreenAttrs[off + 1] = attr;
1708 screen_char_2(off, row, col);
1709 }
1710 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1711 {
1712 ScreenLines2[off] = ptr[1];
1713 screen_char(off, row, col);
1714 }
1715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 screen_char(off, row, col);
1717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (has_mbyte)
1719 {
1720 off += mbyte_cells;
1721 col += mbyte_cells;
1722 ptr += mbyte_blen;
1723 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001724 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001725 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001726 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001728 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001729 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
1732 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
1734 ++off;
1735 ++col;
1736 ++ptr;
1737 }
1738 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001739
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001740 // If we detected the next character needs to be redrawn, but the text
1741 // doesn't extend up to there, update the character here.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001742 if (force_redraw_next && col < screen_Columns)
1743 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001744 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1745 screen_char_2(off, row, col);
1746 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001747 screen_char(off, row, col);
1748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749}
1750
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001751#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001753 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001755 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001756start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 if (p_hls && !no_hlsearch)
1759 {
Bram Moolenaar0b6849e2020-05-02 18:33:25 +02001760 end_search_hl(); // just in case it wasn't called before
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001761 last_pat_prog(&screen_search_hl.rm);
1762 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 }
1764}
1765
1766/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001767 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001769 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001770end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001772 if (screen_search_hl.rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001774 vim_regfree(screen_search_hl.rm.regprog);
1775 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
1777}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001778#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001781screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 attrentry_T *aep = NULL;
1784
1785 screen_attr = attr;
1786 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001787#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 && termcap_active
1789#endif
1790 )
1791 {
1792#ifdef FEAT_GUI
1793 if (gui.in_use)
1794 {
1795 char buf[20];
1796
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001797 // The GUI handles this internally.
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001798 sprintf(buf, "\033|%dh", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 OUT_STR(buf);
1800 }
1801 else
1802#endif
1803 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001804 if (attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001806 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 aep = syn_cterm_attr2entry(attr);
1808 else
1809 aep = syn_term_attr2entry(attr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001810 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 attr = 0;
1812 else
1813 attr = aep->ae_attr;
1814 }
Bram Moolenaara050b942019-12-02 21:35:31 +01001815#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1816 if (use_vtp())
1817 {
1818 guicolor_T defguifg, defguibg;
1819 int defctermfg, defctermbg;
1820
1821 // If FG and BG are unset, the color is undefined when
1822 // BOLD+INVERSE. Use Normal as the default value.
1823 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1824 &defguibg);
1825
1826 if (p_tgc)
1827 {
1828 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1829 term_fg_rgb_color(defguifg);
1830 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1831 term_bg_rgb_color(defguibg);
1832 }
1833 else if (t_colors >= 256)
1834 {
1835 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1836 term_fg_color(defctermfg);
1837 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1838 term_bg_color(defctermbg);
1839 }
1840 }
1841#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001842 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001844 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001845#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001846 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1847 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1848 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001849#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001850 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001851 // If the Normal FG color has BOLD attribute and the new HL
1852 // has a FG color defined, clear BOLD.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001853 out_str(T_ME);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001854 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 out_str(T_SO);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001856 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001857 out_str(T_UCS);
Bram Moolenaar84f54632022-06-29 18:39:11 +01001858 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1859 out_str(T_USS);
1860 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1861 out_str(T_DS);
1862 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1863 out_str(T_CDS);
1864 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1865 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1866 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1867 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1868 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
Bram Moolenaar45a00002017-12-22 21:12:34 +01001869 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 out_str(T_US);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001871 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 out_str(T_CZH);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001873 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 out_str(T_MR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001875 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001876 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 /*
1879 * Output the color or start string after bold etc., in case the
1880 * bold etc. override the color setting.
1881 */
1882 if (aep != NULL)
1883 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001884#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001885 // When 'termguicolors' is set but fg or bg is unset,
1886 // fall back to the cterm colors. This helps for SpellBad,
1887 // where the GUI uses a red undercurl.
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001888 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001890 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001891 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001892 }
1893 else
1894#endif
1895 if (t_colors > 1)
1896 {
1897 if (aep->ae_u.cterm.fg_color)
1898 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1899 }
1900#ifdef FEAT_TERMGUICOLORS
1901 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1902 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001903 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001904 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001907#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001908 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001910 if (aep->ae_u.cterm.bg_color)
1911 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1912 }
Bram Moolenaare023e882020-05-31 16:42:30 +02001913#ifdef FEAT_TERMGUICOLORS
1914 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1915 {
1916 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1917 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1918 }
1919 else
1920#endif
1921 if (t_colors > 1)
1922 {
1923 if (aep->ae_u.cterm.ul_color)
1924 term_ul_color(aep->ae_u.cterm.ul_color - 1);
1925 }
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001926
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001927 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001928 {
1929 if (aep->ae_u.term.start != NULL)
1930 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 }
1932 }
1933 }
1934 }
1935}
1936
1937 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001938screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001940 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001941#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001942 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001946#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 && termcap_active
1948#endif
1949 )
1950 {
1951#ifdef FEAT_GUI
1952 if (gui.in_use)
1953 {
1954 char buf[20];
1955
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001956 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001957 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 OUT_STR(buf);
1959 }
1960 else
1961#endif
1962 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001963 int is_under;
1964
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001965 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 attrentry_T *aep;
1968
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001969 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 /*
1972 * Assume that t_me restores the original colors!
1973 */
1974 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001975 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001976#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001977 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1978 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001979# ifdef FEAT_VTP
1980 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1981# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001982 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001983#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001984 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001985#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001986 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1987 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001988# ifdef FEAT_VTP
1989 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1990# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001991 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001992#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001993 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001995#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1996 if (use_vtp())
1997 {
1998 if (do_ME_fg && do_ME_bg)
1999 do_ME = TRUE;
2000
2001 // FG and BG cannot be separated in T_ME, which is not
2002 // efficient.
2003 if (!do_ME && do_ME_fg)
2004 out_str((char_u *)"\033|39m"); // restore FG
2005 if (!do_ME && do_ME_bg)
2006 out_str((char_u *)"\033|49m"); // restore BG
2007 }
2008 else
2009 {
2010 // Process FG and BG at once.
2011 if (!do_ME)
2012 do_ME = do_ME_fg | do_ME_bg;
2013 }
2014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 else
2017 {
2018 aep = syn_term_attr2entry(screen_attr);
2019 if (aep != NULL && aep->ae_u.term.stop != NULL)
2020 {
2021 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
2022 do_ME = TRUE;
2023 else
2024 out_str(aep->ae_u.term.stop);
2025 }
2026 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002027 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 screen_attr = 0;
2029 else
2030 screen_attr = aep->ae_attr;
2031 }
2032
2033 /*
2034 * Often all ending-codes are equal to T_ME. Avoid outputting the
2035 * same sequence several times.
2036 */
2037 if (screen_attr & HL_STANDOUT)
2038 {
2039 if (STRCMP(T_SE, T_ME) == 0)
2040 do_ME = TRUE;
2041 else
2042 out_str(T_SE);
2043 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002044 is_under = (screen_attr & (HL_UNDERCURL
2045 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
2046 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01002047 {
2048 if (STRCMP(T_UCE, T_ME) == 0)
2049 do_ME = TRUE;
2050 else
2051 out_str(T_UCE);
2052 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002053 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
2055 if (STRCMP(T_UE, T_ME) == 0)
2056 do_ME = TRUE;
2057 else
2058 out_str(T_UE);
2059 }
2060 if (screen_attr & HL_ITALIC)
2061 {
2062 if (STRCMP(T_CZR, T_ME) == 0)
2063 do_ME = TRUE;
2064 else
2065 out_str(T_CZR);
2066 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002067 if (screen_attr & HL_STRIKETHROUGH)
2068 {
2069 if (STRCMP(T_STE, T_ME) == 0)
2070 do_ME = TRUE;
2071 else
2072 out_str(T_STE);
2073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
2075 out_str(T_ME);
2076
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002077#ifdef FEAT_TERMGUICOLORS
2078 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002080 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002081 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002082 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002083 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02002084 if (cterm_normal_ul_gui_color != INVALCOLOR)
2085 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002086 }
2087 else
2088#endif
2089 {
2090 if (t_colors > 1)
2091 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002092 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002093 if (cterm_normal_fg_color != 0)
2094 term_fg_color(cterm_normal_fg_color - 1);
2095 if (cterm_normal_bg_color != 0)
2096 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02002097 if (cterm_normal_ul_color != 0)
2098 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002099 if (cterm_normal_fg_bold)
2100 out_str(T_MD);
2101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
2103 }
2104 }
2105 screen_attr = 0;
2106}
2107
2108/*
2109 * Reset the colors for a cterm. Used when leaving Vim.
2110 * The machine specific code may override this again.
2111 */
2112 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002113reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002115 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002117 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002118#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002119 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2120 || cterm_normal_bg_gui_color != INVALCOLOR)
2121 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002122#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
2126 out_str(T_OP);
2127 screen_attr = -1;
2128 }
2129 if (cterm_normal_fg_bold)
2130 {
2131 out_str(T_ME);
2132 screen_attr = -1;
2133 }
2134 }
2135}
2136
2137/*
2138 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2139 * using the attributes from ScreenAttrs["off"].
2140 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002142screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144 int attr;
2145
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002146 // Check for illegal values, just in case (could happen just after
2147 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (row >= screen_Rows || col >= screen_Columns)
2149 return;
2150
Bram Moolenaar33796b32019-06-08 16:01:13 +02002151 // Skip if under the popup menu.
2152 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
Bakudankun65555002021-11-17 20:40:16 +00002153 if (pum_under_menu(row, col, TRUE)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002154#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002155 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002156#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002157 )
Bram Moolenaarae654382019-01-17 21:09:05 +01002158 return;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002159#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002160 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02002161 return;
2162#endif
2163
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002164 // Outputting a character in the last cell on the screen may scroll the
2165 // screen up. Only do it when the "xn" termcap property is set, otherwise
2166 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01002167 if (*T_XN == NUL
2168 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002170 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 && !cmdmsg_rl
2172#endif
2173 )
2174 {
2175 ScreenAttrs[off] = (sattr_T)-1;
2176 return;
2177 }
2178
2179 /*
2180 * Stop highlighting first, so it's easier to move the cursor.
2181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (screen_char_attr != 0)
2183 attr = screen_char_attr;
2184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 attr = ScreenAttrs[off];
2186 if (screen_attr != attr)
2187 screen_stop_highlight();
2188
2189 windgoto(row, col);
2190
2191 if (screen_attr != attr)
2192 screen_start_highlight(attr);
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 if (enc_utf8 && ScreenLinesUC[off] != 0)
2195 {
2196 char_u buf[MB_MAXBYTES + 1];
2197
Bram Moolenaarcb070082016-04-02 22:14:51 +02002198 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002199 {
2200 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002201#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002202 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002203#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002204 )
2205 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002206 // Clear the two screen cells. If the character is actually
2207 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002208 out_str((char_u *)" ");
2209 term_windgoto(row, col);
2210 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002211 // not sure where the cursor is after drawing the ambiguous width
2212 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002213 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002214 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002215 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002217
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002218 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002219 buf[utfc_char2bytes(off, buf)] = NUL;
2220 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
2222 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002226 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2228 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230
2231 screen_cur_col++;
2232}
2233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234/*
2235 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2236 * on the screen at position 'row' and 'col'.
2237 * The attributes of the first byte is used for all. This is required to
2238 * output the two bytes of a double-byte character with nothing in between.
2239 */
2240 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002241screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002243 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2245 return;
2246
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002247 // Outputting the last character on the screen may scrollup the screen.
2248 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2250 {
2251 ScreenAttrs[off] = (sattr_T)-1;
2252 return;
2253 }
2254
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002255 // Output the first byte normally (positions the cursor), then write the
2256 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 screen_char(off, row, col);
2258 out_char(ScreenLines[off + 1]);
2259 ++screen_cur_col;
2260}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262/*
2263 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2264 * This uses the contents of ScreenLines[] and doesn't change it.
2265 */
2266 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002267screen_draw_rectangle(
2268 int row,
2269 int col,
2270 int height,
2271 int width,
2272 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274 int r, c;
2275 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002276 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002278 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002279 if (ScreenLines == NULL)
2280 return;
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (invert)
2283 screen_char_attr = HL_INVERSE;
2284 for (r = row; r < row + height; ++r)
2285 {
2286 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002287 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 for (c = col; c < col + width; ++c)
2289 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002290 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 screen_char_2(off + c, r, c);
2293 ++c;
2294 }
2295 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002298 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 }
2302 }
2303 screen_char_attr = 0;
2304}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
2307 * Redraw the characters for a vertically split window.
2308 */
2309 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002310redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 int col;
2313 int width;
2314
2315# ifdef FEAT_CLIPBOARD
2316 clip_may_clear_selection(row, end - 1);
2317# endif
2318
2319 if (wp == NULL)
2320 {
2321 col = 0;
2322 width = Columns;
2323 }
2324 else
2325 {
2326 col = wp->w_wincol;
2327 width = wp->w_width;
2328 }
2329 screen_draw_rectangle(row, col, end - row, width, FALSE);
2330}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002332 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002333space_to_screenline(int off, int attr)
2334{
2335 ScreenLines[off] = ' ';
2336 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002337 if (enc_utf8)
2338 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002339}
2340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002342 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2343 * to "end_col" (exclusive) with character "c1" in first column followed by
2344 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 */
2346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002347screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002348 int start_row,
2349 int end_row,
2350 int start_col,
2351 int end_col,
2352 int c1,
2353 int c2,
2354 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002356 int row;
2357 int col;
2358 int off;
2359 int end_off;
2360 int did_delete;
2361 int c;
2362 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002364 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002367 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002369 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 end_col = screen_Columns;
2371 if (ScreenLines == NULL
2372 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002373 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return;
2375
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002376 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 norm_term = (
2378#ifdef FEAT_GUI
2379 !gui.in_use &&
2380#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002381 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 for (row = start_row; row < end_row; ++row)
2383 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002384 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002385#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002386 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002387#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002388 )
2389 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002390 // When drawing over the right half of a double-wide char clear
2391 // out the left half. When drawing over the left half of a
2392 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002393 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002394 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002395 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002396 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002397 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 /*
2400 * Try to use delete-line termcap code, when no attributes or in a
2401 * "normal" terminal, where a bold/italic space is just a
2402 * space.
2403 */
2404 did_delete = FALSE;
2405 if (c2 == ' '
2406 && end_col == Columns
2407 && can_clear(T_CE)
2408 && (attr == 0
2409 || (norm_term
2410 && attr <= HL_ALL
2411 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2412 {
2413 /*
2414 * check if we really need to clear something
2415 */
2416 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002417 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 ++col;
2419
2420 off = LineOffset[row] + col;
2421 end_off = LineOffset[row] + end_col;
2422
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002423 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (enc_utf8)
2425 while (off < end_off && ScreenLines[off] == ' '
2426 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2427 ++off;
2428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 while (off < end_off && ScreenLines[off] == ' '
2430 && ScreenAttrs[off] == 0)
2431 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002432 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 col = off - LineOffset[row];
2435 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002436 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002438 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002440 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002442 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 ++off;
2444 }
2445 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002446 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448
2449 off = LineOffset[row] + start_col;
2450 c = c1;
2451 for (col = start_col; col < end_col; ++col)
2452 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002453 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002454 || (enc_utf8 && (int)ScreenLinesUC[off]
2455 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 || ScreenAttrs[off] != attr
2457#if defined(FEAT_GUI) || defined(UNIX)
2458 || force_next
2459#endif
2460 )
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002461#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002462 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002463 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002464#endif
2465 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
2467#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002468 // The bold trick may make a single row of pixels appear in
2469 // the next character. When a bold character is removed, the
2470 // next character should be redrawn too. This happens for our
2471 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (
2473# ifdef FEAT_GUI
2474 gui.in_use
2475# endif
2476# if defined(FEAT_GUI) && defined(UNIX)
2477 ||
2478# endif
2479# ifdef UNIX
2480 term_is_xterm
2481# endif
2482 )
2483 {
2484 if (ScreenLines[off] != ' '
2485 && (ScreenAttrs[off] > HL_ALL
2486 || ScreenAttrs[off] & HL_BOLD))
2487 force_next = TRUE;
2488 else
2489 force_next = FALSE;
2490 }
2491#endif
2492 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 if (enc_utf8)
2494 {
2495 if (c >= 0x80)
2496 {
2497 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002498 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 else
2501 ScreenLinesUC[off] = 0;
2502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 ScreenAttrs[off] = attr;
2504 if (!did_delete || c != ' ')
2505 screen_char(off, row, col);
2506 }
2507 ++off;
2508 if (col == start_col)
2509 {
2510 if (did_delete)
2511 break;
2512 c = c2;
2513 }
2514 }
2515 if (end_col == Columns)
2516 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002517 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
2519 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002520 if (start_col == 0 && end_col == Columns
2521 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002522 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002523 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002524 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
2526 }
2527}
2528
2529/*
2530 * Check if there should be a delay. Used before clearing or redrawing the
2531 * screen or the command line.
2532 */
2533 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002534check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2537 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002538 && emsg_silent == 0
2539 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
2541 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002542 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 emsg_on_display = FALSE;
2544 if (check_msg_scroll)
2545 msg_scroll = FALSE;
2546 }
2547}
2548
2549/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002550 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2551 */
2552 static void
2553clear_TabPageIdxs(void)
2554{
2555 int scol;
2556
2557 for (scol = 0; scol < Columns; ++scol)
2558 TabPageIdxs[scol] = 0;
2559}
2560
2561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002563 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 * Returns TRUE if there is a valid screen to write to.
2565 * Returns FALSE when starting up and screen not initialized yet.
2566 */
2567 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002568screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002570 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 return (ScreenLines != NULL);
2572}
2573
2574/*
2575 * Resize the shell to Rows and Columns.
2576 * Allocate ScreenLines[] and associated items.
2577 *
2578 * There may be some time between setting Rows and Columns and (re)allocating
2579 * ScreenLines[]. This happens when starting up and when (manually) changing
2580 * the shell size. Always use screen_Rows and screen_Columns to access items
2581 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2582 * final size of the shell is needed.
2583 */
2584 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002585screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 int new_row, old_row;
2588#ifdef FEAT_GUI
2589 int old_Rows;
2590#endif
2591 win_T *wp;
2592 int outofmem = FALSE;
2593 int len;
2594 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002596 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002598 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 sattr_T *new_ScreenAttrs;
2600 unsigned *new_LineOffset;
2601 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002602 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002603#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002604 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002605 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002606 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002607#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002608 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002609 static int entered = FALSE; // avoid recursiveness
2610 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002611 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002613retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 /*
2615 * Allocation of the screen buffers is done only when the size changes and
2616 * when Rows and Columns have been set and we have started doing full
2617 * screen stuff.
2618 */
2619 if ((ScreenLines != NULL
2620 && Rows == screen_Rows
2621 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 && enc_utf8 == (ScreenLinesUC != NULL)
2623 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002624 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 || Rows == 0
2626 || Columns == 0
2627 || (!full_screen && ScreenLines == NULL))
2628 return;
2629
2630 /*
2631 * It's possible that we produce an out-of-memory message below, which
2632 * will cause this function to be called again. To break the loop, just
2633 * return here.
2634 */
2635 if (entered)
2636 return;
2637 entered = TRUE;
2638
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002639 /*
2640 * Note that the window sizes are updated before reallocating the arrays,
2641 * thus we must not redraw here!
2642 */
2643 ++RedrawingDisabled;
2644
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002645 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002647#ifdef FEAT_GUI_HAIKU
2648 vim_lock_screen(); // be safe, put it here
2649#endif
2650
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002651 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 /*
2654 * We're changing the size of the screen.
2655 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2656 * - Move lines from the old arrays into the new arrays, clear extra
2657 * lines (unless the screen is going to be cleared).
2658 * - Free the old arrays.
2659 *
2660 * If anything fails, make ScreenLines NULL, so we don't do anything!
2661 * Continuing with the old ScreenLines may result in a crash, because the
2662 * size is wrong.
2663 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002664 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002666 if (aucmd_win != NULL)
2667 win_free_lsize(aucmd_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002668#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002669 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002670 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002671 win_free_lsize(wp);
2672 // tab-local popup windows
2673 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002674 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002675 win_free_lsize(wp);
2676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002678 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002679 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (enc_utf8)
2681 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002682 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002683 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002684 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2685 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002688 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2689 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
2690 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2691 new_LineWraps = LALLOC_MULT(char_u, Rows);
2692 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002693#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002694 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002695 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002696 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002699 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 if (win_alloc_lines(wp) == FAIL)
2702 {
2703 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002704 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
2706 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002707 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
2708 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002709 outofmem = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002710#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002711 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002712 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002713 if (win_alloc_lines(wp) == FAIL)
2714 {
2715 outofmem = TRUE;
2716 goto give_up;
2717 }
2718 // tab-local popup windows
2719 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002720 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002721 if (win_alloc_lines(wp) == FAIL)
2722 {
2723 outofmem = TRUE;
2724 goto give_up;
2725 }
2726#endif
2727
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002728give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730 for (i = 0; i < p_mco; ++i)
2731 if (new_ScreenLinesC[i] == NULL)
2732 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002734 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 || new_ScreenAttrs == NULL
2737 || new_LineOffset == NULL
2738 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002739 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002740#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002741 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002742 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002743 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 || outofmem)
2746 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002747 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002748 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002749 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002750 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2751
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002752 // Remember we did this to avoid getting outofmem messages over
2753 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002754 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002755 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002756 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002757 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002758 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002759 VIM_CLEAR(new_ScreenLinesC[i]);
2760 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002761 VIM_CLEAR(new_ScreenAttrs);
2762 VIM_CLEAR(new_LineOffset);
2763 VIM_CLEAR(new_LineWraps);
2764 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002765#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002766 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002767 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002768 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 else
2772 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002773 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002774
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 for (new_row = 0; new_row < Rows; ++new_row)
2776 {
2777 new_LineOffset[new_row] = new_row * Columns;
2778 new_LineWraps[new_row] = FALSE;
2779
2780 /*
2781 * If the screen is not going to be cleared, copy as much as
2782 * possible from the old screen to the new one and clear the rest
2783 * (used when resizing the window at the "--more--" prompt or when
2784 * executing an external command, for the GUI).
2785 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002786 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
2788 (void)vim_memset(new_ScreenLines + new_row * Columns,
2789 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if (enc_utf8)
2791 {
2792 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2793 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002794 for (i = 0; i < p_mco; ++i)
2795 (void)vim_memset(new_ScreenLinesC[i]
2796 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 0, (size_t)Columns * sizeof(u8char_T));
2798 }
2799 if (enc_dbcs == DBCS_JPNU)
2800 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2801 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2803 0, (size_t)Columns * sizeof(sattr_T));
2804 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002805 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
2807 if (screen_Columns < Columns)
2808 len = screen_Columns;
2809 else
2810 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002811 // When switching to utf-8 don't copy characters, they
2812 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002813 if (!(enc_utf8 && ScreenLinesUC == NULL)
2814 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002815 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2816 ScreenLines + LineOffset[old_row],
2817 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002818 if (enc_utf8 && ScreenLinesUC != NULL
2819 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
2821 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2822 ScreenLinesUC + LineOffset[old_row],
2823 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002824 for (i = 0; i < p_mco; ++i)
2825 mch_memmove(new_ScreenLinesC[i]
2826 + new_LineOffset[new_row],
2827 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 (size_t)len * sizeof(u8char_T));
2829 }
2830 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2831 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2832 ScreenLines2 + LineOffset[old_row],
2833 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2835 ScreenAttrs + LineOffset[old_row],
2836 (size_t)len * sizeof(sattr_T));
2837 }
2838 }
2839 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002840 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002842
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002843#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002844 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2845 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002849 free_screenlines();
2850
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002851 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002854 for (i = 0; i < p_mco; ++i)
2855 ScreenLinesC[i] = new_ScreenLinesC[i];
2856 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 ScreenAttrs = new_ScreenAttrs;
2859 LineOffset = new_LineOffset;
2860 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002861 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002862#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002863 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002864 popup_mask_next = new_popup_mask_next;
2865 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002866 popup_mask_refresh = TRUE;
2867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002869 // It's important that screen_Rows and screen_Columns reflect the actual
2870 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871#ifdef FEAT_GUI
2872 old_Rows = screen_Rows;
2873#endif
2874 screen_Rows = Rows;
2875 screen_Columns = Columns;
2876
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002877 must_redraw = CLEAR; // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002878 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880#ifdef FEAT_GUI
2881 else if (gui.in_use
2882 && !gui.starting
2883 && ScreenLines != NULL
2884 && old_Rows != Rows)
2885 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002886 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2887
2888 // Adjust the position of the cursor, for when executing an external
2889 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002890 if (msg_row >= Rows) // Rows got smaller
2891 msg_row = Rows - 1; // put cursor at last row
2892 else if (Rows > old_Rows) // Rows got bigger
2893 msg_row += Rows - old_Rows; // put cursor in same place
2894 if (msg_col >= Columns) // Columns got smaller
2895 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002898 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002900#ifdef FEAT_GUI_HAIKU
2901 vim_unlock_screen();
2902#endif
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002905 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002906
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002907 /*
2908 * Do not apply autocommands more than 3 times to avoid an endless loop
2909 * in case applying autocommands always changes Rows or Columns.
2910 */
2911 if (starting == 0 && ++retry_count <= 3)
2912 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002913 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002914 // In rare cases, autocommands may have altered Rows or Columns,
2915 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002916 goto retry;
2917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
2920 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002921free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002922{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002923 int i;
2924
Bram Moolenaar33796b32019-06-08 16:01:13 +02002925 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002926 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002927 VIM_CLEAR(ScreenLinesC[i]);
2928 VIM_CLEAR(ScreenLines2);
2929 VIM_CLEAR(ScreenLines);
2930 VIM_CLEAR(ScreenAttrs);
2931 VIM_CLEAR(LineOffset);
2932 VIM_CLEAR(LineWraps);
2933 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002934#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002935 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002936 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002937 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002938#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002939}
2940
2941 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002942screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 check_for_delay(FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002945 screenalloc(FALSE); // allocate screen buffers if size changed
2946 screenclear2(); // clear the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947}
2948
2949 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002950screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
2952 int i;
2953
2954 if (starting == NO_SCREEN || ScreenLines == NULL
2955#ifdef FEAT_GUI
2956 || (gui.in_use && gui.starting)
2957#endif
2958 )
2959 return;
2960
2961#ifdef FEAT_GUI
2962 if (!gui.in_use)
2963#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002964 screen_attr = -1; // force setting the Normal colors
2965 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002968 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 clip_scroll_selection(9999);
2970#endif
2971
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002972 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 for (i = 0; i < Rows; ++i)
2974 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002975 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 LineWraps[i] = FALSE;
2977 }
2978
2979 if (can_clear(T_CL))
2980 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002981 out_str(T_CL); // clear the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002983 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
2985 else
2986 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002987 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 for (i = 0; i < Rows; ++i)
2989 lineinvalid(LineOffset[i], (int)Columns);
2990 clear_cmdline = TRUE;
2991 }
2992
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002993 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 win_rest_invalid(firstwin);
2996 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002997 redraw_tabline = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002998 if (must_redraw == CLEAR) // no need to clear again
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 must_redraw = NOT_VALID;
3000 compute_cmdrow();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003001 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003003 screen_start(); // don't know where cursor is now
3004 msg_scrolled = 0; // can't scroll back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 msg_didany = FALSE;
3006 msg_didout = FALSE;
3007}
3008
3009/*
3010 * Clear one line in ScreenLines.
3011 */
3012 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003013lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 if (enc_utf8)
3017 (void)vim_memset(ScreenLinesUC + off, 0,
3018 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003019 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020}
3021
3022/*
3023 * Mark one line in ScreenLines invalid by setting the attributes to an
3024 * invalid value.
3025 */
3026 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003027lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
3030}
3031
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003033 * To be called when characters were sent to the terminal directly, outputting
3034 * test on "screen_lnum".
3035 */
3036 void
3037line_was_clobbered(int screen_lnum)
3038{
3039 lineinvalid(LineOffset[screen_lnum], (int)Columns);
3040}
3041
3042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 * Copy part of a Screenline for vertically split window "wp".
3044 */
3045 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003046linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
3048 unsigned off_to = LineOffset[to] + wp->w_wincol;
3049 unsigned off_from = LineOffset[from] + wp->w_wincol;
3050
3051 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
3052 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 if (enc_utf8)
3054 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003055 int i;
3056
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
3058 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003059 for (i = 0; i < p_mco; ++i)
3060 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
3061 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 }
3063 if (enc_dbcs == DBCS_JPNU)
3064 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
3065 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
3067 wp->w_width * sizeof(sattr_T));
3068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
3070/*
3071 * Return TRUE if clearing with term string "p" would work.
3072 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02003073 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 */
3075 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003076can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 return (*p != NUL && (t_colors <= 1
3079#ifdef FEAT_GUI
3080 || gui.in_use
3081#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003082#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003083 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02003084 || (!p_tgc && cterm_normal_bg_color == 0)
3085#else
3086 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003087#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003088 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003089#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003090 && !(p == T_CE && popup_visible)
3091#endif
3092 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093}
3094
3095/*
3096 * Reset cursor position. Use whenever cursor was moved because of outputting
3097 * something directly to the screen (shell commands) or a terminal control
3098 * code.
3099 */
3100 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003101screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102{
3103 screen_cur_row = screen_cur_col = 9999;
3104}
3105
3106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * Move the cursor to position "row","col" in the screen.
3108 * This tries to find the most efficient way to move, minimizing the number of
3109 * characters sent to the terminal.
3110 */
3111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003112windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003114 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 int i;
3116 int plan;
3117 int cost;
3118 int wouldbe_col;
3119 int noinvcurs;
3120 char_u *bs;
3121 int goto_cost;
3122 int attr;
3123
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003124#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
3125#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127#define PLAN_LE 1
3128#define PLAN_CR 2
3129#define PLAN_NL 3
3130#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003131 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 if (ScreenLines == NULL)
3133 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 if (col != screen_cur_col || row != screen_cur_row)
3135 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003136 // Check for valid position.
3137 if (row < 0) // window without text lines?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 row = 0;
3139 if (row >= screen_Rows)
3140 row = screen_Rows - 1;
3141 if (col >= screen_Columns)
3142 col = screen_Columns - 1;
3143
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003144 // check if no cursor movement is allowed in highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (screen_attr && *T_MS == NUL)
3146 noinvcurs = HIGHL_COST;
3147 else
3148 noinvcurs = 0;
3149 goto_cost = GOTO_COST + noinvcurs;
3150
3151 /*
3152 * Plan how to do the positioning:
3153 * 1. Use CR to move it to column 0, same row.
3154 * 2. Use T_LE to move it a few columns to the left.
3155 * 3. Use NL to move a few lines down, column 0.
3156 * 4. Move a few columns to the right with T_ND or by writing chars.
3157 *
3158 * Don't do this if the cursor went beyond the last column, the cursor
3159 * position is unknown then (some terminals wrap, some don't )
3160 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003161 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 * characters to move the cursor to the right.
3163 */
3164 if (row >= screen_cur_row && screen_cur_col < Columns)
3165 {
3166 /*
3167 * If the cursor is in the same row, bigger col, we can use CR
3168 * or T_LE.
3169 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003170 bs = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 attr = screen_attr;
3172 if (row == screen_cur_row && col < screen_cur_col)
3173 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003174 // "le" is preferred over "bc", because "bc" is obsolete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 if (*T_LE)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003176 bs = T_LE; // "cursor left"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003178 bs = T_BC; // "backspace character (old)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 if (*bs)
3180 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3181 else
3182 cost = 999;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003183 if (col + 1 < cost) // using CR is less characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
3185 plan = PLAN_CR;
3186 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003187 cost = 1; // CR is just one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
3189 else
3190 {
3191 plan = PLAN_LE;
3192 wouldbe_col = col;
3193 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003194 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
3196 cost += noinvcurs;
3197 attr = 0;
3198 }
3199 }
3200
3201 /*
3202 * If the cursor is above where we want to be, we can use CR LF.
3203 */
3204 else if (row > screen_cur_row)
3205 {
3206 plan = PLAN_NL;
3207 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003208 cost = (row - screen_cur_row) * 2; // CR LF
3209 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
3211 cost += noinvcurs;
3212 attr = 0;
3213 }
3214 }
3215
3216 /*
3217 * If the cursor is in the same row, smaller col, just use write.
3218 */
3219 else
3220 {
3221 plan = PLAN_WRITE;
3222 wouldbe_col = screen_cur_col;
3223 cost = 0;
3224 }
3225
3226 /*
3227 * Check if any characters that need to be written have the
3228 * correct attributes. Also avoid UTF-8 characters.
3229 */
3230 i = col - wouldbe_col;
3231 if (i > 0)
3232 cost += i;
3233 if (cost < goto_cost && i > 0)
3234 {
3235 /*
3236 * Check if the attributes are correct without additionally
3237 * stopping highlighting.
3238 */
3239 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3240 while (i && *p++ == attr)
3241 --i;
3242 if (i != 0)
3243 {
3244 /*
3245 * Try if it works when highlighting is stopped here.
3246 */
3247 if (*--p == 0)
3248 {
3249 cost += noinvcurs;
3250 while (i && *p++ == 0)
3251 --i;
3252 }
3253 if (i != 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003254 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (enc_utf8)
3257 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003258 // Don't use an UTF-8 char for positioning, it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 for (i = wouldbe_col; i < col; ++i)
3260 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3261 {
3262 cost = 999;
3263 break;
3264 }
3265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267
3268 /*
3269 * We can do it without term_windgoto()!
3270 */
3271 if (cost < goto_cost)
3272 {
3273 if (plan == PLAN_LE)
3274 {
3275 if (noinvcurs)
3276 screen_stop_highlight();
3277 while (screen_cur_col > col)
3278 {
3279 out_str(bs);
3280 --screen_cur_col;
3281 }
3282 }
3283 else if (plan == PLAN_CR)
3284 {
3285 if (noinvcurs)
3286 screen_stop_highlight();
3287 out_char('\r');
3288 screen_cur_col = 0;
3289 }
3290 else if (plan == PLAN_NL)
3291 {
3292 if (noinvcurs)
3293 screen_stop_highlight();
3294 while (screen_cur_row < row)
3295 {
3296 out_char('\n');
3297 ++screen_cur_row;
3298 }
3299 screen_cur_col = 0;
3300 }
3301
3302 i = col - screen_cur_col;
3303 if (i > 0)
3304 {
3305 /*
3306 * Use cursor-right if it's one character only. Avoids
3307 * removing a line of pixels from the last bold char, when
3308 * using the bold trick in the GUI.
3309 */
3310 if (T_ND[0] != NUL && T_ND[1] == NUL)
3311 {
3312 while (i-- > 0)
3313 out_char(*T_ND);
3314 }
3315 else
3316 {
3317 int off;
3318
3319 off = LineOffset[row] + screen_cur_col;
3320 while (i-- > 0)
3321 {
3322 if (ScreenAttrs[off] != screen_attr)
3323 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (enc_dbcs == DBCS_JPNU
3327 && ScreenLines[off] == 0x8e)
3328 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 ++off;
3330 }
3331 }
3332 }
3333 }
3334 }
3335 else
3336 cost = 999;
3337
3338 if (cost >= goto_cost)
3339 {
3340 if (noinvcurs)
3341 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003342 if (row == screen_cur_row && (col > screen_cur_col)
3343 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 term_cursor_right(col - screen_cur_col);
3345 else
3346 term_windgoto(row, col);
3347 }
3348 screen_cur_row = row;
3349 screen_cur_col = col;
3350 }
3351}
3352
3353/*
3354 * Set cursor to its position in the current window.
3355 */
3356 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003357setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003359 setcursor_mayforce(FALSE);
3360}
3361
3362/*
3363 * Set cursor to its position in the current window.
3364 * When "force" is TRUE also when not redrawing.
3365 */
3366 void
3367setcursor_mayforce(int force)
3368{
3369 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
3371 validate_cursor();
3372 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003373 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003375 // With 'rightleft' set and the cursor on a double-wide
3376 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003377 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3378 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003379 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003380 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#endif
3382 curwin->w_wcol));
3383 }
3384}
3385
3386
3387/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003388 * Insert 'line_count' lines at 'row' in window 'wp'.
3389 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3390 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 * scrolling.
3392 * Returns FAIL if the lines are not inserted, OK for success.
3393 */
3394 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003395win_ins_lines(
3396 win_T *wp,
3397 int row,
3398 int line_count,
3399 int invalid,
3400 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 int did_delete;
3403 int nextrow;
3404 int lastrow;
3405 int retval;
3406
3407 if (invalid)
3408 wp->w_lines_valid = 0;
3409
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003410 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (wp->w_height < 5)
3412 return FAIL;
3413
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003414 // with the popup menu visible this might not work correctly
3415 if (pum_visible())
3416 return FAIL;
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (line_count > wp->w_height - row)
3419 line_count = wp->w_height - row;
3420
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003421 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (retval != MAYBE)
3423 return retval;
3424
3425 /*
3426 * If there is a next window or a status line, we first try to delete the
3427 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003428 * If this fails and there are following windows, don't do anything to
3429 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
3431 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (wp->w_next != NULL || wp->w_status_height)
3433 {
3434 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003435 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 did_delete = TRUE;
3437 else if (wp->w_next)
3438 return FAIL;
3439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 /*
3441 * if no lines deleted, blank the lines that will end up below the window
3442 */
3443 if (!did_delete)
3444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003447 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 lastrow = nextrow + line_count;
3449 if (lastrow > Rows)
3450 lastrow = Rows;
3451 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003452 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 ' ', ' ', 0);
3454 }
3455
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003456 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 == FAIL)
3458 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003459 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (did_delete)
3461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 win_rest_invalid(W_NEXT(wp));
3464 }
3465 return FAIL;
3466 }
3467
3468 return OK;
3469}
3470
3471/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003472 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3474 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3475 * scrolling
3476 * Return OK for success, FAIL if the lines are not deleted.
3477 */
3478 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003479win_del_lines(
3480 win_T *wp,
3481 int row,
3482 int line_count,
3483 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003484 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003485 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487 int retval;
3488
3489 if (invalid)
3490 wp->w_lines_valid = 0;
3491
3492 if (line_count > wp->w_height - row)
3493 line_count = wp->w_height - row;
3494
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003495 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (retval != MAYBE)
3497 return retval;
3498
3499 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003500 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 return FAIL;
3502
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 /*
3504 * If there are windows or status lines below, try to put them at the
3505 * correct place. If we can't do that, they have to be redrawn.
3506 */
3507 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3508 {
3509 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003510 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 {
3512 wp->w_redr_status = TRUE;
3513 win_rest_invalid(wp->w_next);
3514 }
3515 }
3516 /*
3517 * If this is the last window and there is no status line, redraw the
3518 * command line later.
3519 */
3520 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 redraw_cmdline = TRUE;
3522 return OK;
3523}
3524
3525/*
3526 * Common code for win_ins_lines() and win_del_lines().
3527 * Returns OK or FAIL when the work has been done.
3528 * Returns MAYBE when not finished yet.
3529 */
3530 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003531win_do_lines(
3532 win_T *wp,
3533 int row,
3534 int line_count,
3535 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003536 int del,
3537 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
3539 int retval;
3540
3541 if (!redrawing() || line_count <= 0)
3542 return FAIL;
3543
Bram Moolenaar33796b32019-06-08 16:01:13 +02003544 // When inserting lines would result in loss of command output, just redraw
3545 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003546 if (no_win_do_lines_ins && !del)
3547 return FAIL;
3548
Bram Moolenaar33796b32019-06-08 16:01:13 +02003549 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003550 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003552 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003553 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 return FAIL;
3555 }
3556
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003557#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003558 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003559 if (popup_visible)
3560 return FAIL;
3561#endif
3562
3563 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (row + line_count >= wp->w_height)
3565 {
3566 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003567 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 ' ', ' ', 0);
3569 return OK;
3570 }
3571
3572 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003573 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003575 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003577 if (!no_win_do_lines_ins)
3578 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
3580 /*
3581 * If the terminal can set a scroll region, use that.
3582 * Always do this in a vertically split window. This will redraw from
3583 * ScreenLines[] when t_CV isn't defined. That's faster than using
3584 * win_line().
3585 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003586 * a character in the lower right corner of the scroll region may cause a
3587 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003589 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 scroll_region_set(wp, row);
3593 if (del)
3594 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003595 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 else
3597 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003598 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 scroll_region_reset();
3601 return retval;
3602 }
3603
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003604 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
3607 return MAYBE;
3608}
3609
3610/*
3611 * window 'wp' and everything after it is messed up, mark it for redraw
3612 */
3613 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003614win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 wp->w_redr_status = TRUE;
3620 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 redraw_cmdline = TRUE;
3623}
3624
3625/*
3626 * The rest of the routines in this file perform screen manipulations. The
3627 * given operation is performed physically on the screen. The corresponding
3628 * change is also made to the internal screen image. In this way, the editor
3629 * anticipates the effect of editing changes on the appearance of the screen.
3630 * That way, when we call screenupdate a complete redraw isn't usually
3631 * necessary. Another advantage is that we can keep adding code to anticipate
3632 * screen changes, and in the meantime, everything still works.
3633 */
3634
3635/*
3636 * types for inserting or deleting lines
3637 */
3638#define USE_T_CAL 1
3639#define USE_T_CDL 2
3640#define USE_T_AL 3
3641#define USE_T_CE 4
3642#define USE_T_DL 5
3643#define USE_T_SR 6
3644#define USE_NL 7
3645#define USE_T_CD 8
3646#define USE_REDRAW 9
3647
3648/*
3649 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003650 * "end" is the line after the scrolled part. Normally it is Rows.
3651 * When scrolling region used "off" is the offset from the top for the region.
3652 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 *
3654 * return FAIL for failure, OK for success.
3655 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003656 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003657screen_ins_lines(
3658 int off,
3659 int row,
3660 int line_count,
3661 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003662 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003663 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
3665 int i;
3666 int j;
3667 unsigned temp;
3668 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003669 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 int type;
3671 int result_empty;
3672 int can_ce = can_clear(T_CE);
3673
3674 /*
3675 * FAIL if
3676 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 * - the line count is less than one
3678 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003679 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003680 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003681 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003683 if (!screen_valid(TRUE)
3684 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003685 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003686#ifdef FEAT_CLIPBOARD
3687 || (clip_star.state != SELECT_CLEARED
3688 && redrawing_for_callback > 0)
3689#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003690#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003691 || popup_visible
3692#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003693 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return FAIL;
3695
3696 /*
3697 * There are seven ways to insert lines:
3698 * 0. When in a vertically split window and t_CV isn't set, redraw the
3699 * characters from ScreenLines[].
3700 * 1. Use T_CD (clear to end of display) if it exists and the result of
3701 * the insert is just empty lines
3702 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3703 * present or line_count > 1. It looks better if we do all the inserts
3704 * at once.
3705 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3706 * insert is just empty lines and T_CE is not present or line_count >
3707 * 1.
3708 * 4. Use T_AL (insert line) if it exists.
3709 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3710 * just empty lines.
3711 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3712 * just empty lines.
3713 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3714 * the 'da' flag is not set or we have clear line capability.
3715 * 8. redraw the characters from ScreenLines[].
3716 *
3717 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3718 * the scrollbar for the window. It does have insert line, use that if it
3719 * exists.
3720 */
3721 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003723 {
3724 // Avoid that lines are first cleared here and then redrawn, which
3725 // results in many characters updated twice. This happens with CTRL-F
3726 // in a vertically split window. With line-by-line scrolling
3727 // USE_REDRAW should be faster.
3728 if (line_count > 3)
3729 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003731 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003732 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 type = USE_T_CD;
3734 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3735 type = USE_T_CAL;
3736 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3737 type = USE_T_CDL;
3738 else if (*T_AL != NUL)
3739 type = USE_T_AL;
3740 else if (can_ce && result_empty)
3741 type = USE_T_CE;
3742 else if (*T_DL != NUL && result_empty)
3743 type = USE_T_DL;
3744 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3745 type = USE_T_SR;
3746 else
3747 return FAIL;
3748
3749 /*
3750 * For clearing the lines screen_del_lines() is used. This will also take
3751 * care of t_db if necessary.
3752 */
3753 if (type == USE_T_CD || type == USE_T_CDL ||
3754 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003755 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
3757 /*
3758 * If text is retained below the screen, first clear or delete as many
3759 * lines at the bottom of the window as are about to be inserted so that
3760 * the deleted lines won't later surface during a screen_del_lines.
3761 */
3762 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003763 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003766 // Remove a modeless selection when inserting lines halfway the screen
3767 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003768 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003769 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 else
3771 clip_scroll_selection(-line_count);
3772#endif
3773
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003774#ifdef FEAT_GUI_HAIKU
3775 vim_lock_screen();
3776#endif
3777
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003779 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3780 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003781 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782#endif
3783
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003784 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3785 cursor_col = wp->w_wincol;
3786
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003787 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 cursor_row = row;
3789 else
3790 cursor_row = row + off;
3791
3792 /*
3793 * Shift LineOffset[] line_count down to reflect the inserted lines.
3794 * Clear the inserted lines in ScreenLines[].
3795 */
3796 row += off;
3797 end += off;
3798 for (i = 0; i < line_count; ++i)
3799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 if (wp != NULL && wp->w_width != Columns)
3801 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003802 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 j = end - 1 - i;
3804 while ((j -= line_count) >= row)
3805 linecopy(j + line_count, j, wp);
3806 j += line_count;
3807 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003808 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3809 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 else
3811 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3812 LineWraps[j] = FALSE;
3813 }
3814 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
3816 j = end - 1 - i;
3817 temp = LineOffset[j];
3818 while ((j -= line_count) >= row)
3819 {
3820 LineOffset[j + line_count] = LineOffset[j];
3821 LineWraps[j + line_count] = LineWraps[j];
3822 }
3823 LineOffset[j + line_count] = temp;
3824 LineWraps[j + line_count] = FALSE;
3825 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003826 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 else
3828 lineinvalid(temp, (int)Columns);
3829 }
3830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003832#ifdef FEAT_GUI_HAIKU
3833 vim_unlock_screen();
3834#endif
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003837 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003838 if (clear_attr != 0)
3839 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003841 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if (type == USE_REDRAW)
3843 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003844 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003847 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 else
3850 {
3851 for (i = 0; i < line_count; i++)
3852 {
3853 if (type == USE_T_AL)
3854 {
3855 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003856 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 out_str(T_AL);
3858 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003859 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003861 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
3863 }
3864
3865 /*
3866 * With scroll-reverse and 'da' flag set we need to clear the lines that
3867 * have been scrolled down into the region.
3868 */
3869 if (type == USE_T_SR && *T_DA)
3870 {
3871 for (i = 0; i < line_count; ++i)
3872 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003873 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003875 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
3877 }
3878
3879#ifdef FEAT_GUI
3880 gui_can_update_cursor();
3881 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003882 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883#endif
3884 return OK;
3885}
3886
3887/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003888 * Delete lines on the screen and update ScreenLines[].
3889 * "end" is the line after the scrolled part. Normally it is Rows.
3890 * When scrolling region used "off" is the offset from the top for the region.
3891 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 *
3893 * Return OK for success, FAIL if the lines are not deleted.
3894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003896screen_del_lines(
3897 int off,
3898 int row,
3899 int line_count,
3900 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003901 int force, // even when line_count > p_ttyscroll
3902 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003903 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
3905 int j;
3906 int i;
3907 unsigned temp;
3908 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003909 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003911 int result_empty; // result is empty until end of region
3912 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 int type;
3914
3915 /*
3916 * FAIL if
3917 * - there is no valid screen
3918 * - the screen has to be redrawn completely
3919 * - the line count is less than one
3920 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003921 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003922 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003924 if (!screen_valid(TRUE)
3925 || line_count <= 0
3926 || (!force && line_count > p_ttyscroll)
3927 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003928#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003929 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003930#endif
3931 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 return FAIL;
3933
3934 /*
3935 * Check if the rest of the current region will become empty.
3936 */
3937 result_empty = row + line_count >= end;
3938
3939 /*
3940 * We can delete lines only when 'db' flag not set or when 'ce' option
3941 * available.
3942 */
3943 can_delete = (*T_DB == NUL || can_clear(T_CE));
3944
3945 /*
3946 * There are six ways to delete lines:
3947 * 0. When in a vertically split window and t_CV isn't set, redraw the
3948 * characters from ScreenLines[].
3949 * 1. Use T_CD if it exists and the result is empty.
3950 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3951 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3952 * none of the other ways work.
3953 * 4. Use T_CE (erase line) if the result is empty.
3954 * 5. Use T_DL (delete line) if it exists.
3955 * 6. redraw the characters from ScreenLines[].
3956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003958 {
3959 // Avoid that lines are first cleared here and then redrawn, which
3960 // results in many characters updated twice. This happens with CTRL-F
3961 // in a vertically split window. With line-by-line scrolling
3962 // USE_REDRAW should be faster.
3963 if (line_count > 3)
3964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003966 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003967 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 else if (row == 0 && (
3970#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003971 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3972 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 line_count == 1 ||
3974#endif
3975 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 type = USE_NL;
3977 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3978 type = USE_T_CDL;
3979 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003980 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 type = USE_T_CE;
3982 else if (*T_DL != NUL && can_delete)
3983 type = USE_T_DL;
3984 else if (*T_CDL != NUL && can_delete)
3985 type = USE_T_CDL;
3986 else
3987 return FAIL;
3988
3989#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003990 // Remove a modeless selection when deleting lines halfway the screen or
3991 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003992 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003993 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 else
3995 clip_scroll_selection(line_count);
3996#endif
3997
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003998#ifdef FEAT_GUI_HAIKU
3999 vim_lock_screen();
4000#endif
4001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004003 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
4004 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02004005 gui_dont_update_cursor(gui.cursor_row >= row + off
4006 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007#endif
4008
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004009 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
4010 cursor_col = wp->w_wincol;
4011
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004012 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 {
4014 cursor_row = row;
4015 cursor_end = end;
4016 }
4017 else
4018 {
4019 cursor_row = row + off;
4020 cursor_end = end + off;
4021 }
4022
4023 /*
4024 * Now shift LineOffset[] line_count up to reflect the deleted lines.
4025 * Clear the inserted lines in ScreenLines[].
4026 */
4027 row += off;
4028 end += off;
4029 for (i = 0; i < line_count; ++i)
4030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 if (wp != NULL && wp->w_width != Columns)
4032 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004033 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 j = row + i;
4035 while ((j += line_count) <= end - 1)
4036 linecopy(j - line_count, j, wp);
4037 j -= line_count;
4038 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004039 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
4040 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 else
4042 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
4043 LineWraps[j] = FALSE;
4044 }
4045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004047 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 j = row + i;
4049 temp = LineOffset[j];
4050 while ((j += line_count) <= end - 1)
4051 {
4052 LineOffset[j - line_count] = LineOffset[j];
4053 LineWraps[j - line_count] = LineWraps[j];
4054 }
4055 LineOffset[j - line_count] = temp;
4056 LineWraps[j - line_count] = FALSE;
4057 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004058 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 else
4060 lineinvalid(temp, (int)Columns);
4061 }
4062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004064#ifdef FEAT_GUI_HAIKU
4065 vim_unlock_screen();
4066#endif
4067
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004068 if (screen_attr != clear_attr)
4069 screen_stop_highlight();
4070 if (clear_attr != 0)
4071 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004073 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 if (type == USE_REDRAW)
4075 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004076 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004078 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004080 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082 else if (type == USE_T_CDL)
4083 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004084 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004086 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 /*
4089 * Deleting lines at top of the screen or scroll region: Just scroll
4090 * the whole screen (scroll region) up by outputting newlines on the
4091 * last line.
4092 */
4093 else if (type == USE_NL)
4094 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004095 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004097 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
4099 else
4100 {
4101 for (i = line_count; --i >= 0; )
4102 {
4103 if (type == USE_T_DL)
4104 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004105 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004106 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004108 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004110 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004111 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004113 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 }
4116
4117 /*
4118 * If the 'db' flag is set, we need to clear the lines that have been
4119 * scrolled up at the bottom of the region.
4120 */
4121 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
4122 {
4123 for (i = line_count; i > 0; --i)
4124 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004125 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004126 out_str(T_CE); // erase a line
4127 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 }
4130
4131#ifdef FEAT_GUI
4132 gui_can_update_cursor();
4133 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004134 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135#endif
4136
4137 return OK;
4138}
4139
4140/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004141 * Return TRUE when postponing displaying the mode message: when not redrawing
4142 * or inside a mapping.
4143 */
4144 int
4145skip_showmode()
4146{
4147 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004148 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004149 if (global_busy
4150 || msg_silent != 0
4151 || !redrawing()
4152 || (char_avail() && !KeyTyped))
4153 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004154 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004155 return TRUE;
4156 }
4157 return FALSE;
4158}
4159
4160/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004161 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 *
4163 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4164 * If clear_cmdline is FALSE there may be a message there that needs to be
4165 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004166 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * Return the length of the message (0 if no message).
4168 */
4169 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004170showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171{
4172 int need_clear;
4173 int length = 0;
4174 int do_mode;
4175 int attr;
4176 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
Bram Moolenaar7df351e2006-01-23 22:30:28 +00004179 do_mode = ((p_smd && msg_silent == 0)
Bram Moolenaar24959102022-05-07 20:01:16 +01004180 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004181 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004182 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004183 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004185 if (skip_showmode())
4186 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 nwr_save = need_wait_return;
4189
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004190 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 check_for_delay(FALSE);
4192
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004193 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 need_clear = clear_cmdline;
4195 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004196 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004198 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 msg_pos_mode();
4200 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004201 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (do_mode)
4203 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004204 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004206 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004207# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004208 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004209# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004210 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004211# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004212 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004213# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004214 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004216 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217# endif
4218#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004219 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004220 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004222 // These messages can get long, avoid a wrap in a narrow
4223 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 length = (Rows - msg_row) * Columns - 3;
4225 if (edit_submode_extra != NULL)
4226 length -= vim_strsize(edit_submode_extra);
4227 if (length > 0)
4228 {
4229 if (edit_submode_pre != NULL)
4230 length -= vim_strsize(edit_submode_pre);
4231 if (length - vim_strsize(edit_submode) > 0)
4232 {
4233 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004234 msg_puts_attr((char *)edit_submode_pre, attr);
4235 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237 if (edit_submode_extra != NULL)
4238 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004239 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004241 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 else
4243 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004244 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004251 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004252 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004253 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004254 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
4256#ifdef FEAT_RIGHTLEFT
4257 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004258 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004260 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004262 else if (restart_edit == 'I' || restart_edit == 'i' ||
4263 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004264 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004266 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004268 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269#ifdef FEAT_RIGHTLEFT
4270 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004271 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272#endif
4273#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004274 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
4276# ifdef FEAT_ARABIC
4277 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004278 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 else
4280# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004281 if (get_keymap_str(curwin, (char_u *)" (%s)",
4282 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004283 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004286 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004287 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (VIsual_active)
4290 {
4291 char *p;
4292
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004293 // Don't concatenate separate words to avoid translation
4294 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 switch ((VIsual_select ? 4 : 0)
4296 + (VIsual_mode == Ctrl_V) * 2
4297 + (VIsual_mode == 'V'))
4298 {
4299 case 0: p = N_(" VISUAL"); break;
4300 case 1: p = N_(" VISUAL LINE"); break;
4301 case 2: p = N_(" VISUAL BLOCK"); break;
4302 case 4: p = N_(" SELECT"); break;
4303 case 5: p = N_(" SELECT LINE"); break;
4304 default: p = N_(" SELECT BLOCK"); break;
4305 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004306 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004308 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004310
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 need_clear = TRUE;
4312 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004313 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004314 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004316 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 need_clear = TRUE;
4318 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004319
4320 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004321 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004323 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 length = msg_col;
4325 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004326 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004329 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004331 else if (redraw_mode)
4332 {
4333 msg_pos_mode();
4334 msg_clr_eos();
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
4337#ifdef FEAT_CMDL_INFO
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004338 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 if (VIsual_active)
4340 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004342 // If the last window has no status line, the ruler is after the mode
4343 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004344 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004345 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346#endif
4347 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004348 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 clear_cmdline = FALSE;
4350
4351 return length;
4352}
4353
4354/*
4355 * Position for a mode message.
4356 */
4357 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004358msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359{
4360 msg_col = 0;
4361 msg_row = Rows - 1;
4362}
4363
4364/*
4365 * Delete mode message. Used when ESC is typed which is expected to end
4366 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004367 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 */
4369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004370unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
4372 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004373 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 */
4375 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004376 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004378 clearmode();
4379}
4380
4381/*
4382 * Clear the mode message.
4383 */
4384 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004385clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004386{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004387 int save_msg_row = msg_row;
4388 int save_msg_col = msg_col;
4389
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004390 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004391 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004392 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004393 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004394
4395 msg_col = save_msg_col;
4396 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397}
4398
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004399 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004400recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004401{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004402 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004403 if (!shortmess(SHM_RECORDING))
4404 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004405 char s[4];
4406
4407 sprintf(s, " @%c", reg_recording);
4408 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004409 }
4410}
4411
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004412/*
4413 * Draw the tab pages line at the top of the Vim window.
4414 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004415 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004416draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004417{
4418 int tabcount = 0;
4419 tabpage_T *tp;
4420 int tabwidth;
4421 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004422 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004423 int attr;
4424 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004425 win_T *cwp;
4426 int wincount;
4427 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004428 int c;
4429 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004430 int attr_sel = HL_ATTR(HLF_TPS);
4431 int attr_nosel = HL_ATTR(HLF_TP);
4432 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004433 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004434 int room;
4435 int use_sep_chars = (t_colors < 8
4436#ifdef FEAT_GUI
4437 && !gui.in_use
4438#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004439#ifdef FEAT_TERMGUICOLORS
4440 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004441#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004442 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004443
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004444 if (ScreenLines == NULL)
4445 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004446 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004447
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004448#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004449 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004450 if (gui_use_tabline())
4451 {
4452 gui_update_tabline();
4453 return;
4454 }
4455#endif
4456
4457 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004458 return;
4459
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004460#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004461 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004462
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004463 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004464 if (*p_tal != NUL)
4465 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004466 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004467
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004468 // Check for an error. If there is one we would loop in redrawing the
4469 // screen. Avoid that by making 'tabline' empty.
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004470 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004471 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004472 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004473 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004474 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004475 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004476 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00004477 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004478#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004479 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004480 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004481 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004482
Bram Moolenaar238a5642006-02-21 22:12:05 +00004483 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4484 if (tabwidth < 6)
4485 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004486
Bram Moolenaar238a5642006-02-21 22:12:05 +00004487 attr = attr_nosel;
4488 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004489 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4490 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004491 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004492 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004493
Bram Moolenaar238a5642006-02-21 22:12:05 +00004494 if (tp->tp_topframe == topframe)
4495 attr = attr_sel;
4496 if (use_sep_chars && col > 0)
4497 screen_putchar('|', 0, col++, attr);
4498
4499 if (tp->tp_topframe != topframe)
4500 attr = attr_nosel;
4501
4502 screen_putchar(' ', 0, col++, attr);
4503
4504 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004505 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004506 cwp = curwin;
4507 wp = firstwin;
4508 }
4509 else
4510 {
4511 cwp = tp->tp_curwin;
4512 wp = tp->tp_firstwin;
4513 }
4514
4515 modified = FALSE;
4516 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4517 if (bufIsChanged(wp->w_buffer))
4518 modified = TRUE;
4519 if (modified || wincount > 1)
4520 {
4521 if (wincount > 1)
4522 {
4523 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004524 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004525 if (col + len >= Columns - 3)
4526 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004527 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004528#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004529 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004530#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004531 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004532#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004533 );
4534 col += len;
4535 }
4536 if (modified)
4537 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4538 screen_putchar(' ', 0, col++, attr);
4539 }
4540
4541 room = scol - col + tabwidth - 1;
4542 if (room > 0)
4543 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004544 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004545 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004546 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004547 len = vim_strsize(NameBuff);
4548 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004549 if (has_mbyte)
4550 while (len > room)
4551 {
4552 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004553 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004554 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004555 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004556 {
4557 p += len - room;
4558 len = room;
4559 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004560 if (len > Columns - col - 1)
4561 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004562
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004563 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004564 col += len;
4565 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004566 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004567
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004568 // Store the tab page number in TabPageIdxs[], so that
4569 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004570 ++tabcount;
4571 while (scol < col)
4572 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004573 }
4574
Bram Moolenaar238a5642006-02-21 22:12:05 +00004575 if (use_sep_chars)
4576 c = '_';
4577 else
4578 c = ' ';
4579 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004580
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004581 // Put an "X" for closing the current tab if there are several.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004582 if (first_tabpage->tp_next != NULL)
4583 {
4584 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4585 TabPageIdxs[Columns - 1] = -999;
4586 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004587 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004588
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004589 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4590 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004591 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004592}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004593
4594/*
4595 * Get buffer name for "buf" into NameBuff[].
4596 * Takes care of special buffer names and translates special characters.
4597 */
4598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004599get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004600{
4601 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004602 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004603 else
4604 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4605 trans_characters(NameBuff, MAXPATHL);
4606}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608/*
4609 * Get the character to use in a status line. Get its attributes in "*attr".
4610 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004611 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004612fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613{
4614 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004615
4616#ifdef FEAT_TERMINAL
4617 if (bt_terminal(wp->w_buffer))
4618 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004619 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004620 {
4621 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004622 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004623 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004624 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004625 {
4626 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004627 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004628 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004629 }
4630 else
4631#endif
4632 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004634 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 fill = fill_stl;
4636 }
4637 else
4638 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004639 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 fill = fill_stlnc;
4641 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004642 // Use fill when there is highlighting, and highlighting of current
4643 // window differs, or the fillchars differ, or this is not the
4644 // current window
Bram Moolenaar8820b482017-03-16 17:23:31 +01004645 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004646 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 || (fill_stl != fill_stlnc)))
4648 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004649 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 return '^';
4651 return '=';
4652}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654/*
4655 * Get the character to use in a separator between vertically split windows.
4656 * Get its attributes in "*attr".
4657 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004658 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004659fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004661 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (*attr == 0 && fill_vert == ' ')
4663 return '|';
4664 else
4665 return fill_vert;
4666}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
4668/*
4669 * Return TRUE if redrawing should currently be done.
4670 */
4671 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004672redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004674#ifdef FEAT_EVAL
4675 if (disable_redraw_for_testing)
4676 return 0;
4677 else
4678#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004679 return ((!RedrawingDisabled
4680#ifdef FEAT_EVAL
4681 || ignore_redraw_flag_for_testing
4682#endif
4683 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684}
4685
4686/*
4687 * Return TRUE if printing messages should currently be done.
4688 */
4689 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004690messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692 return (!(p_lz && char_avail() && !KeyTyped));
4693}
4694
Bram Moolenaare677df82019-09-02 22:31:11 +02004695/*
4696 * Compute columns for ruler and shown command. 'sc_col' is also used to
4697 * decide what the maximum length of a message on the status line can be.
4698 * If there is a status line for the last window, 'sc_col' is independent
4699 * of 'ru_col'.
4700 */
4701
4702#define COL_RULER 17 // columns needed by standard ruler
4703
4704 void
4705comp_col(void)
4706{
4707#if defined(FEAT_CMDL_INFO)
4708 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4709
4710 sc_col = 0;
4711 ru_col = 0;
4712 if (p_ru)
4713 {
4714# ifdef FEAT_STL_OPT
4715 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
4716# else
4717 ru_col = COL_RULER + 1;
4718# endif
4719 // no last status line, adjust sc_col
4720 if (!last_has_status)
4721 sc_col = ru_col;
4722 }
4723 if (p_sc)
4724 {
4725 sc_col += SHOWCMD_COLS;
4726 if (!p_ru || last_has_status) // no need for separating space
4727 ++sc_col;
4728 }
4729 sc_col = Columns - sc_col;
4730 ru_col = Columns - ru_col;
4731 if (sc_col <= 0) // screen too narrow, will become a mess
4732 sc_col = 1;
4733 if (ru_col <= 0)
4734 ru_col = 1;
4735#else
4736 sc_col = Columns;
4737 ru_col = Columns;
4738#endif
4739#ifdef FEAT_EVAL
4740 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4741#endif
4742}
4743
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004744#if defined(FEAT_LINEBREAK) || defined(PROTO)
4745/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004746 * Return the width of the 'number' and 'relativenumber' column.
4747 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004748 * Otherwise it depends on 'numberwidth' and the line count.
4749 */
4750 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004751number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004752{
4753 int n;
4754 linenr_T lnum;
4755
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004756 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004757 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004758 lnum = wp->w_height;
4759 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004760 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004761 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004762
Bram Moolenaar6b314672015-03-20 15:42:10 +01004763 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004764 return wp->w_nrwidth_width;
4765 wp->w_nrwidth_line_count = lnum;
4766
4767 n = 0;
4768 do
4769 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004770 lnum /= 10;
4771 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004772 } while (lnum > 0);
4773
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004774 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004775 if (n < wp->w_p_nuw - 1)
4776 n = wp->w_p_nuw - 1;
4777
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004778# ifdef FEAT_SIGNS
4779 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4780 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004781 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004782 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4783 n = 2;
4784# endif
4785
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004786 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004787 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004788 return n;
4789}
4790#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004791
Bram Moolenaar113e1072019-01-20 15:30:40 +01004792#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004793/*
4794 * Return the current cursor column. This is the actual position on the
4795 * screen. First column is 0.
4796 */
4797 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004798screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004799{
4800 return screen_cur_col;
4801}
4802
4803/*
4804 * Return the current cursor row. This is the actual position on the screen.
4805 * First row is 0.
4806 */
4807 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004808screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004809{
4810 return screen_cur_row;
4811}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004812#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004813
4814/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004815 * Calls mb_ptr2char_adv(p) and returns the character.
4816 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4817 */
4818 static int
4819get_encoded_char_adv(char_u **p)
4820{
4821 char_u *s = *p;
4822
4823 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4824 {
4825 varnumber_T num = 0;
4826 int bytes;
4827 int n;
4828
4829 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4830 {
4831 *p += 2;
4832 n = hexhex2nr(*p);
4833 if (n < 0)
4834 return 0;
4835 num = num * 256 + n;
4836 }
4837 *p += 2;
4838 return num;
4839 }
4840 return mb_ptr2char_adv(p);
4841}
4842
4843/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004844 * Handle setting 'listchars' or 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004845 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004846 * Returns error message, NULL if it's OK.
4847 */
4848 char *
Bram Moolenaareed9d462021-02-15 20:38:25 +01004849set_chars_option(win_T *wp, char_u **varp)
Bram Moolenaare677df82019-09-02 22:31:11 +02004850{
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004851 int round, i, len, len2, entries;
Bram Moolenaare677df82019-09-02 22:31:11 +02004852 char_u *p, *s;
4853 int c1 = 0, c2 = 0, c3 = 0;
Bram Moolenaar56e14692021-09-11 12:15:09 +02004854 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004855 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
Bram Moolenaar56e14692021-09-11 12:15:09 +02004856 int multispace_len = 0; // Length of lcs-multispace string
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004857 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaare677df82019-09-02 22:31:11 +02004858 struct charstab
4859 {
4860 int *cp;
4861 char *name;
4862 };
4863 static struct charstab filltab[] =
4864 {
Bram Moolenaar3aca5a62021-02-17 13:14:07 +01004865 {&fill_stl, "stl"},
4866 {&fill_stlnc, "stlnc"},
4867 {&fill_vert, "vert"},
4868 {&fill_fold, "fold"},
4869 {&fill_foldopen, "foldopen"},
4870 {&fill_foldclosed, "foldclose"},
4871 {&fill_foldsep, "foldsep"},
4872 {&fill_diff, "diff"},
4873 {&fill_eob, "eob"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004874 };
Bram Moolenaar333bd562021-02-16 22:22:13 +01004875 static lcs_chars_T lcs_chars;
Bram Moolenaareed9d462021-02-15 20:38:25 +01004876 struct charstab lcstab[] =
Bram Moolenaare677df82019-09-02 22:31:11 +02004877 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004878 {&lcs_chars.eol, "eol"},
4879 {&lcs_chars.ext, "extends"},
4880 {&lcs_chars.nbsp, "nbsp"},
4881 {&lcs_chars.prec, "precedes"},
4882 {&lcs_chars.space, "space"},
4883 {&lcs_chars.tab2, "tab"},
4884 {&lcs_chars.trail, "trail"},
4885 {&lcs_chars.lead, "lead"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004886#ifdef FEAT_CONCEAL
Bram Moolenaar333bd562021-02-16 22:22:13 +01004887 {&lcs_chars.conceal, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004888#else
Bram Moolenaar333bd562021-02-16 22:22:13 +01004889 {NULL, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004890#endif
4891 };
4892 struct charstab *tab;
4893
Bram Moolenaareed9d462021-02-15 20:38:25 +01004894 if (varp == &p_lcs || varp == &wp->w_p_lcs)
Bram Moolenaare677df82019-09-02 22:31:11 +02004895 {
4896 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004897 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004898 entries = ARRAY_LENGTH(lcstab);
Bram Moolenaareed9d462021-02-15 20:38:25 +01004899 if (varp == &wp->w_p_lcs && wp->w_p_lcs[0] == NUL)
4900 varp = &p_lcs;
Bram Moolenaare677df82019-09-02 22:31:11 +02004901 }
4902 else
4903 {
4904 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004905 entries = ARRAY_LENGTH(filltab);
Bram Moolenaare677df82019-09-02 22:31:11 +02004906 }
4907
4908 // first round: check for valid value, second round: assign values
4909 for (round = 0; round <= 1; ++round)
4910 {
4911 if (round > 0)
4912 {
4913 // After checking that the value is valid: set defaults: space for
4914 // 'fillchars', NUL for 'listchars'
4915 for (i = 0; i < entries; ++i)
4916 if (tab[i].cp != NULL)
Bram Moolenaareed9d462021-02-15 20:38:25 +01004917 *(tab[i].cp) =
4918 ((varp == &p_lcs || varp == &wp->w_p_lcs) ? NUL : ' ');
Bram Moolenaare677df82019-09-02 22:31:11 +02004919
Bram Moolenaareed9d462021-02-15 20:38:25 +01004920 if (varp == &p_lcs || varp == &wp->w_p_lcs)
Bram Moolenaare677df82019-09-02 22:31:11 +02004921 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004922 lcs_chars.tab1 = NUL;
4923 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004924
Mike Williamsf5785cf2021-09-13 22:17:38 +02004925 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004926 {
4927 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
4928 lcs_chars.multispace[multispace_len] = NUL;
4929 }
4930 else
4931 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004932
4933 if (lead_multispace_len > 0)
4934 {
4935 lcs_chars.leadmultispace = ALLOC_MULT(int, lead_multispace_len + 1);
4936 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
4937 }
4938 else
4939 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004940 }
4941 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004942 {
Bram Moolenaare677df82019-09-02 22:31:11 +02004943 fill_diff = '-';
Bram Moolenaar3aca5a62021-02-17 13:14:07 +01004944 fill_foldopen = '-';
4945 fill_foldclosed = '+';
4946 fill_foldsep = '|';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004947 fill_eob = '~';
4948 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004949 }
4950 p = *varp;
4951 while (*p)
4952 {
4953 for (i = 0; i < entries; ++i)
4954 {
4955 len = (int)STRLEN(tab[i].name);
4956 if (STRNCMP(p, tab[i].name, len) == 0
4957 && p[len] == ':'
4958 && p[len + 1] != NUL)
4959 {
4960 c2 = c3 = 0;
4961 s = p + len + 1;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004962 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004963 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004964 return e_invalid_argument;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004965 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02004966 {
4967 if (*s == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004968 return e_invalid_argument;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004969 c2 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004970 if (char2cells(c2) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004971 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02004972 if (!(*s == ',' || *s == NUL))
4973 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004974 c3 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004975 if (char2cells(c3) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004976 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02004977 }
4978 }
4979
4980 if (*s == ',' || *s == NUL)
4981 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02004982 if (round > 0)
Bram Moolenaare677df82019-09-02 22:31:11 +02004983 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004984 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02004985 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004986 lcs_chars.tab1 = c1;
4987 lcs_chars.tab2 = c2;
4988 lcs_chars.tab3 = c3;
Bram Moolenaare677df82019-09-02 22:31:11 +02004989 }
4990 else if (tab[i].cp != NULL)
4991 *(tab[i].cp) = c1;
4992
4993 }
4994 p = s;
4995 break;
4996 }
4997 }
4998 }
4999
5000 if (i == entries)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005001 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005002 len = (int)STRLEN("multispace");
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005003 len2 = (int)STRLEN("leadmultispace");
zeertzjqf14b8ba2021-09-10 16:58:30 +02005004 if ((varp == &p_lcs || varp == &wp->w_p_lcs)
5005 && STRNCMP(p, "multispace", len) == 0
5006 && p[len] == ':'
5007 && p[len + 1] != NUL)
5008 {
5009 s = p + len + 1;
5010 if (round == 0)
5011 {
5012 // Get length of lcs-multispace string in first round
5013 last_multispace = p;
5014 multispace_len = 0;
5015 while (*s != NUL && *s != ',')
5016 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005017 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005018 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005019 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005020 ++multispace_len;
5021 }
5022 if (multispace_len == 0)
5023 // lcs-multispace cannot be an empty string
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005024 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005025 p = s;
5026 }
5027 else
5028 {
5029 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02005030
zeertzjqf14b8ba2021-09-10 16:58:30 +02005031 while (*s != NUL && *s != ',')
5032 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005033 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02005034 if (p == last_multispace)
5035 lcs_chars.multispace[multispace_pos++] = c1;
5036 }
5037 p = s;
5038 }
5039 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005040
5041 else if ((varp == &p_lcs || varp == &wp->w_p_lcs)
5042 && STRNCMP(p, "leadmultispace", len2) == 0
5043 && p[len2] == ':'
5044 && p[len2 + 1] != NUL)
5045 {
5046 s = p + len2 + 1;
5047 if (round == 0)
5048 {
zeertzjqb5f08012022-06-09 13:55:28 +01005049 // get length of lcs-leadmultispace string in first
5050 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005051 last_lmultispace = p;
5052 lead_multispace_len = 0;
5053 while (*s != NUL && *s != ',')
5054 {
5055 c1 = get_encoded_char_adv(&s);
5056 if (char2cells(c1) > 1)
5057 return e_invalid_argument;
5058 ++lead_multispace_len;
5059 }
5060 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01005061 // lcs-leadmultispace cannot be an empty string
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005062 return e_invalid_argument;
5063 p = s;
5064 }
5065 else
5066 {
5067 int multispace_pos = 0;
5068
5069 while (*s != NUL && *s != ',')
5070 {
5071 c1 = get_encoded_char_adv(&s);
5072 if (p == last_lmultispace)
5073 lcs_chars.leadmultispace[multispace_pos++] = c1;
5074 }
5075 p = s;
5076 }
5077 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005078 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005079 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005080 }
5081
Bram Moolenaare677df82019-09-02 22:31:11 +02005082 if (*p == ',')
5083 ++p;
5084 }
5085 }
Bram Moolenaar333bd562021-02-16 22:22:13 +01005086 if (tab == lcstab)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005087 {
zeertzjqb5f08012022-06-09 13:55:28 +01005088 vim_free(wp->w_lcs_chars.multispace);
5089 vim_free(wp->w_lcs_chars.leadmultispace);
Bram Moolenaar333bd562021-02-16 22:22:13 +01005090 wp->w_lcs_chars = lcs_chars;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005091 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005092
5093 return NULL; // no error
5094}