blob: ffea593724b9e25ce6601c01688f38342aadb23b [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.
Bram Moolenaarb9081882022-07-09 04:56:24 +010020 * ScreenCols[off] Contains the byte offset in the line. -1 means not
21 * available (below last line), MAXCOL means after the end
22 * of the line.
23 *
24 * LineOffset[row] Contains the offset into ScreenLines*[], ScreenAttrs[]
25 * and ScreenCols[] for each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * LineWraps[row] Flag for each line whether it wraps to the next line.
27 *
28 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
29 * one character which occupies two display cells.
30 * For UTF-8 a multi-byte character is converted to Unicode and stored in
31 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * character without composing chars ScreenLinesUC[] will be 0 and
33 * ScreenLinesC[][] is not used. When the character occupies two display
34 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000035 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010036 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000037 * ScreenLines2[] is only used for euc-jp to store the second byte if the
38 * first byte is 0x8e (single-width character).
39 *
40 * The screen_*() functions write to the screen and handle updating
41 * ScreenLines[].
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 */
43
44#include "vim.h"
45
46/*
47 * The attributes that are actually active for writing to the screen.
48 */
49static int screen_attr = 0;
50
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010051static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar838b7462022-09-26 15:19:56 +010052static int screenclear2(int doclear);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020053static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020055static 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 +010056static void win_rest_invalid(win_T *wp);
57static void msg_pos_mode(void);
58static void recording_mode(int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaar63d9e732019-12-05 21:10:38 +010060// Ugly global: overrule attribute used by screen_char()
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaar860cae12010-06-05 23:22:07 +020063#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020064/*
65 * Return TRUE if the cursor line in window "wp" may be concealed, according
66 * to the 'concealcursor' option.
67 */
68 int
Bram Moolenaar05540972016-01-30 20:31:25 +010069conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020070{
71 int c;
72
73 if (*wp->w_p_cocu == NUL)
74 return FALSE;
Bram Moolenaar24959102022-05-07 20:01:16 +010075 if (get_real_state() & MODE_VISUAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020076 c = 'v';
Bram Moolenaar24959102022-05-07 20:01:16 +010077 else if (State & MODE_INSERT)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020078 c = 'i';
Bram Moolenaar24959102022-05-07 20:01:16 +010079 else if (State & MODE_NORMAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020080 c = 'n';
Bram Moolenaar24959102022-05-07 20:01:16 +010081 else if (State & MODE_CMDLINE)
Bram Moolenaarca8c9862010-07-24 15:00:38 +020082 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +020083 else
84 return FALSE;
85 return vim_strchr(wp->w_p_cocu, c) != NULL;
86}
87
88/*
89 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
Bram Moolenaarea042672021-06-29 20:22:32 +020090 * To be called after changing the state, "was_concealed" is the value of
91 * "conceal_cursor_line()" before the change.
92 * "
Bram Moolenaarf5963f72010-07-23 22:10:27 +020093 */
94 void
Bram Moolenaarea042672021-06-29 20:22:32 +020095conceal_check_cursor_line(int was_concealed)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020096{
Bram Moolenaarea042672021-06-29 20:22:32 +020097 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin) != was_concealed)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020098 {
Bram Moolenaarea042672021-06-29 20:22:32 +020099 int wcol = curwin->w_wcol;
100
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200101 need_cursor_line_redraw = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100102 // Need to recompute cursor column, e.g., when starting Visual mode
103 // without concealing.
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200104 curs_columns(TRUE);
Bram Moolenaarea042672021-06-29 20:22:32 +0200105
106 // When concealing now w_wcol will be computed wrong, keep the previous
107 // value, it will be updated in win_line().
108 if (!was_concealed)
109 curwin->w_wcol = wcol;
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200110 }
111}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#endif
113
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200114/*
115 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
116 * window then get the "Pmenu" highlight attribute.
117 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200118 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200119get_wcr_attr(win_T *wp)
120{
121 int wcr_attr = 0;
122
123 if (*wp->w_p_wcr != NUL)
124 wcr_attr = syn_name2attr(wp->w_p_wcr);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100125#ifdef FEAT_PROP_POPUP
Bram Moolenaarc363fe12019-08-04 18:13:46 +0200126 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200127 {
128 if (wp->w_popup_flags & POPF_INFO)
129 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
130 else
131 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
132 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200133#endif
134 return wcr_attr;
135}
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100138 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
139 * Return the new offset.
140 */
141 static int
142screen_fill_end(
143 win_T *wp,
144 int c1,
145 int c2,
146 int off,
147 int width,
148 int row,
149 int endrow,
150 int attr)
151{
152 int nn = off + width;
153
154 if (nn > wp->w_width)
155 nn = wp->w_width;
156#ifdef FEAT_RIGHTLEFT
157 if (wp->w_p_rl)
158 {
159 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
160 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
161 c1, c2, attr);
162 }
163 else
164#endif
165 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
166 wp->w_wincol + off, (int)wp->w_wincol + nn,
167 c1, c2, attr);
168 return nn;
169}
170
171/*
172 * Clear lines near the end the window and mark the unused lines with "c1".
173 * use "c2" as the filler character.
174 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200176 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100177win_draw_end(
178 win_T *wp,
179 int c1,
180 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100181 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +0100182 int row,
183 int endrow,
184 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200187 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200188 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200189
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200190 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100191
192 if (draw_margin)
193 {
Bram Moolenaar1c934292015-01-27 16:39:29 +0100194#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100195 int fdc = compute_foldcolumn(wp, 0);
196
197 if (fdc > 0)
198 // draw the fold column
199 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200200 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +0100201#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100202#ifdef FEAT_SIGNS
203 if (signcolumn_on(wp))
204 // draw the sign column
205 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200206 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100207#endif
208 if ((wp->w_p_nu || wp->w_p_rnu)
209 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
210 // draw the number column
211 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200212 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215#ifdef FEAT_RIGHTLEFT
216 if (wp->w_p_rl)
217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100219 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200220 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100222 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200223 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 }
225 else
226#endif
227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100229 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200230 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 set_empty_rows(wp, row);
234}
235
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200236#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237/*
Bram Moolenaar1c934292015-01-27 16:39:29 +0100238 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
239 * space is available for window "wp", minus "col".
240 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200241 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100242compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +0100243{
244 int fdc = wp->w_p_fdc;
245 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +0200246 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100247
248 if (fdc > wwidth - (col + wmw))
249 fdc = wwidth - (col + wmw);
250 return fdc;
251}
252
253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000255 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100256 * Returns the number of bytes stored in 'p'. When non-multibyte characters are
257 * used for the fold column markers, this is equal to 'fdc' setting. Otherwise,
258 * this will be greater than 'fdc'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 */
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100260 size_t
Bram Moolenaar05540972016-01-30 20:31:25 +0100261fill_foldcolumn(
262 char_u *p,
263 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100264 int closed, // TRUE of FALSE
265 linenr_T lnum) // current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
267 int i = 0;
268 int level;
269 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000270 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100271 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100272 size_t byte_counter = 0;
273 int symbol = 0;
274 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100276 // Init to all spaces.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100277 vim_memset(p, ' ', MAX_MCO * fdc + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
279 level = win_foldinfo.fi_level;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100280 empty = (fdc == 1) ? 0 : 1;
281
282 // If the column is too narrow, we start at the lowest level that
Bram Moolenaar008bff92021-03-04 21:55:58 +0100283 // fits and use numbers to indicate the depth.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100284 first_level = level - fdc - closed + 1 + empty;
285 if (first_level < 1)
286 first_level = 1;
287
288 for (i = 0; i < MIN(fdc, level); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100290 if (win_foldinfo.fi_lnum == lnum
291 && first_level + i >= win_foldinfo.fi_low_level)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100292 symbol = wp->w_fill_chars.foldopen;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100293 else if (first_level == 1)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100294 symbol = wp->w_fill_chars.foldsep;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100295 else if (first_level + i <= 9)
296 symbol = '0' + first_level + i;
297 else
298 symbol = '>';
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000299
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100300 len = utf_char2bytes(symbol, &p[byte_counter]);
301 byte_counter += len;
302 if (first_level + i >= level)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100304 i++;
305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307 }
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100308
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 if (closed)
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100310 {
311 if (symbol != 0)
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100312 {
313 // rollback length and the character
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100314 byte_counter -= len;
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100315 if (len > 1)
316 // for a multibyte character, erase all the bytes
317 vim_memset(p + byte_counter, ' ', len);
318 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100319 symbol = wp->w_fill_chars.foldclosed;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100320 len = utf_char2bytes(symbol, &p[byte_counter]);
321 byte_counter += len;
322 }
323
324 return MAX(byte_counter + (fdc - i), (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325}
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100326#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000328/*
329 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +0100330 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000331 */
332 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100333comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000334{
335 int i;
336
337 for (i = 0; i < Screen_mco; ++i)
338 {
339 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
340 return TRUE;
341 if (ScreenLinesC[i][off_from] == 0)
342 break;
343 }
344 return FALSE;
345}
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Check whether the given character needs redrawing:
349 * - the (first byte of the) character is different
350 * - the attributes are different
351 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000352 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 */
354 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100355char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356{
357 if (cols > 0
358 && ((ScreenLines[off_from] != ScreenLines[off_to]
359 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 || (enc_dbcs != 0
361 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
362 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
363 ? ScreenLines2[off_from] != ScreenLines2[off_to]
364 : (cols > 1 && ScreenLines[off_from + 1]
365 != ScreenLines[off_to + 1])))
366 || (enc_utf8
367 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
368 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000369 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +0200370 || ((*mb_off2cells)(off_from, off_from + cols) > 1
371 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +0100372 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 return TRUE;
374 return FALSE;
375}
376
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200377#if defined(FEAT_TERMINAL) || defined(PROTO)
378/*
379 * Return the index in ScreenLines[] for the current screen line.
380 */
381 int
382screen_get_current_line_off()
383{
384 return (int)(current_ScreenLine - ScreenLines);
385}
386#endif
387
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100388#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200389/*
390 * Return TRUE if this position has a higher level popup or this cell is
391 * transparent in the current popup.
392 */
393 static int
394blocked_by_popup(int row, int col)
395{
396 int off;
397
398 if (!popup_visible)
399 return FALSE;
400 off = row * screen_Columns + col;
401 return popup_mask[off] > screen_zindex || popup_transparent[off];
402}
403#endif
404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200406 * Reset the highlighting. Used before clearing the screen.
407 */
408 void
409reset_screen_attr(void)
410{
411#ifdef FEAT_GUI
412 if (gui.in_use)
413 // Use a code that will reset gui.highlight_mask in
414 // gui_stop_highlight().
415 screen_attr = HL_ALL + 1;
416 else
417#endif
418 // Use attributes that is very unlikely to appear in text.
419 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
420}
421
422/*
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100423 * Return TRUE if the character at "row" / "col" is under the popup menu and it
424 * will be redrawn soon or it is under another popup.
425 */
426 static int
427skip_for_popup(int row, int col)
428{
429 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
430 if (pum_under_menu(row, col, TRUE)
431#ifdef FEAT_PROP_POPUP
432 && screen_zindex <= POPUPMENU_ZINDEX
433#endif
434 )
435 return TRUE;
436#ifdef FEAT_PROP_POPUP
437 if (blocked_by_popup(row, col))
438 return TRUE;
439#endif
440 return FALSE;
441}
442
443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 * Move one "cooked" screen line to the screen, but only the characters that
445 * have actually changed. Handle insert/delete character.
446 * "coloff" gives the first column on the screen for this line.
447 * "endcol" gives the columns where valid characters are.
448 * "clear_width" is the width of the window. It's > 0 if the rest of the line
449 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200450 * "flags" can have bits:
451 * SLF_POPUP popup window
452 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
454 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
455 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200456 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100457screen_line(
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100458 win_T *wp,
459 int row,
460 int coloff,
461 int endcol,
462 int clear_width,
463 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464{
465 unsigned off_from;
466 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000467 unsigned max_off_from;
468 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100471 int force = FALSE; // force update rest of the line
472 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100474 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475#endif
476 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100477 int redraw_next; // redraw_this for next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100479 int char_cells; // 1: normal char
480 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100482 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100483 if (row >= Rows)
484 row = Rows - 1;
485 if (endcol > Columns)
486 endcol = Columns;
487
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488# ifdef FEAT_CLIPBOARD
489 clip_may_clear_selection(row, row);
490# endif
491
492 off_from = (unsigned)(current_ScreenLine - ScreenLines);
493 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000494 max_off_from = off_from + screen_Columns;
495 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496
497#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200498 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100500 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 if (clear_width > 0)
502 {
503 while (col <= endcol && ScreenLines[off_to] == ' '
504 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100505 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 {
507 ++off_to;
508 ++col;
509 }
510 if (col <= endcol)
511 screen_fill(row, row + 1, col + coloff,
512 endcol + coloff + 1, ' ', ' ', 0);
513 }
514 col = endcol + 1;
515 off_to = LineOffset[row] + col + coloff;
516 off_from += col;
517 endcol = (clear_width > 0 ? clear_width : -clear_width);
518 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100519#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100521#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100522 // First char of a popup window may go on top of the right half of a
523 // double-wide character. Clear the left half to avoid it getting the popup
524 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100525 if (coloff > 0 && enc_utf8
526 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200527 && ScreenLinesUC[off_to - 1] != 0
528 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100529 {
530 ScreenLines[off_to - 1] = ' ';
531 ScreenLinesUC[off_to - 1] = 0;
532 screen_char(off_to - 1, row, col + coloff - 1);
533 }
534#endif
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
537
538 while (col < endcol)
539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000541 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 else
543 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
545 redraw_this = redraw_next;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100546 redraw_next = force || char_needs_redraw(off_from + char_cells,
547 off_to + char_cells, endcol - col - char_cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
549#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100550 // If the next character was bold, then redraw the current character to
551 // remove any pixels that might have spilt over into us. This only
552 // happens in the GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (redraw_next && gui.in_use)
554 {
Bram Moolenaarb9081882022-07-09 04:56:24 +0100555 hl = ScreenAttrs[off_to + char_cells];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000556 if (hl > HL_ALL)
557 hl = syn_attr2attr(hl);
558 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 redraw_this = TRUE;
560 }
561#endif
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100562 // Do not redraw if under the popup menu.
563 if (redraw_this && skip_for_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200564 redraw_this = FALSE;
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 if (redraw_this)
567 {
568 /*
569 * Special handling when 'xs' termcap flag set (hpterm):
570 * Attributes for characters are stored at the position where the
571 * cursor is when writing the highlighting code. The
572 * start-highlighting code must be written with the cursor on the
573 * first highlighted character. The stop-highlighting code must
574 * be written with the cursor just after the last highlighted
575 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100576 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 * to clear the rest of the line, and force redrawing it
578 * completely.
579 */
580 if ( p_wiv
581 && !force
582#ifdef FEAT_GUI
583 && !gui.in_use
584#endif
585 && ScreenAttrs[off_to] != 0
586 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
587 {
588 /*
589 * Need to remove highlighting attributes here.
590 */
591 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100592 out_str(T_CE); // clear rest of this screen line
593 screen_start(); // don't know where cursor is now
594 force = TRUE; // force redraw of rest of the line
595 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
597 /*
598 * If the previous character was highlighted, need to stop
599 * highlighting at this character.
600 */
601 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
602 {
603 screen_attr = ScreenAttrs[off_to - 1];
604 term_windgoto(row, col + coloff);
605 screen_stop_highlight();
606 }
607 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100608 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 if (enc_dbcs != 0)
611 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100612 // Check if overwriting a double-byte with a single-byte or
613 // the other way around requires another character to be
614 // redrawn. For UTF-8 this isn't needed, because comparing
615 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 if (char_cells == 1
617 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000618 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100620 // Writing a single-cell character over a double-cell
621 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 ScreenLines[off_to + 1] = 0;
623 redraw_next = TRUE;
624 }
625 else if (char_cells == 2
626 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000627 && (*mb_off2cells)(off_to, max_off_to) == 1
628 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100630 // Writing the second half of a double-cell character over
631 // a double-cell character: need to redraw the second
632 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 ScreenLines[off_to + 2] = 0;
634 redraw_next = TRUE;
635 }
636
637 if (enc_dbcs == DBCS_JPNU)
638 ScreenLines2[off_to] = ScreenLines2[off_from];
639 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100640 // When writing a single-width character over a double-width
641 // character and at the end of the redrawn text, need to clear out
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000642 // the right half of the old character.
643 // Also required when writing the right half of a double-width
644 // char over the left half of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if (has_mbyte && col + char_cells == endcol
646 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000647 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000649 && (*mb_off2cells)(off_to, max_off_to) == 1
650 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
653 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 if (enc_utf8)
655 {
656 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
657 if (ScreenLinesUC[off_from] != 0)
658 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000659 int i;
660
661 for (i = 0; i < Screen_mco; ++i)
662 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 }
665 if (char_cells == 2)
666 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100669 // The bold trick makes a single column of pixels appear in the
670 // next character. When a bold character is removed, the next
671 // character should be redrawn too. This happens for our own GUI
672 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 if (
674# ifdef FEAT_GUI
675 gui.in_use
676# endif
677# if defined(FEAT_GUI) && defined(UNIX)
678 ||
679# endif
680# ifdef UNIX
681 term_is_xterm
682# endif
683 )
684 {
685 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000686 if (hl > HL_ALL)
687 hl = syn_attr2attr(hl);
688 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 redraw_next = TRUE;
690 }
691#endif
692 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100693
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100694 // For simplicity set the attributes of second half of a
695 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000696 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000698
699 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 screen_char(off_to, row, col + coloff);
703 }
704 else if ( p_wiv
705#ifdef FEAT_GUI
706 && !gui.in_use
707#endif
708 && col + coloff > 0)
709 {
710 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
711 {
712 /*
713 * Don't output stop-highlight when moving the cursor, it will
714 * stop the highlighting when it should continue.
715 */
716 screen_attr = 0;
717 }
718 else if (screen_attr != 0)
719 screen_stop_highlight();
720 }
721
Bram Moolenaarb9081882022-07-09 04:56:24 +0100722 ScreenCols[off_to] = ScreenCols[off_from];
723 if (char_cells == 2)
724 ScreenCols[off_to + 1] = ScreenCols[off_from];
725
726 off_to += char_cells;
727 off_from += char_cells;
728 col += char_cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 }
730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (clear_next)
732 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100733 // Clear the second half of a double-wide character of which the left
734 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 ScreenLines[off_to] = ' ';
736 if (enc_utf8)
737 ScreenLinesUC[off_to] = 0;
738 screen_char(off_to, row, col + coloff);
739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741 if (clear_width > 0
742#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200743 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#endif
745 )
746 {
747#ifdef FEAT_GUI
748 int startCol = col;
749#endif
750
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100751 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 while (col < clear_width && ScreenLines[off_to] == ' '
753 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100754 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
Bram Moolenaarb9081882022-07-09 04:56:24 +0100756 ScreenCols[off_to] = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 ++off_to;
758 ++col;
759 }
760 if (col < clear_width)
761 {
762#ifdef FEAT_GUI
763 /*
764 * In the GUI, clearing the rest of the line may leave pixels
765 * behind if the first character cleared was bold. Some bold
766 * fonts spill over the left. In this case we redraw the previous
767 * character too. If we didn't skip any blanks above, then we
768 * only redraw if the character wasn't already redrawn anyway.
769 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000770 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
772 hl = ScreenAttrs[off_to];
773 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000774 {
775 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100776
Bram Moolenaar9c697322006-10-09 20:11:17 +0000777 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100778 // for utf-8, ScreenLines[char_offset + 1] == 0 means
779 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000780 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
781 else if (enc_dbcs != 0)
782 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100783 // find previous character by counting from first
784 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000785 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000786 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000787
788 while (off < off_to)
789 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000790 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000791 off += prev_cells;
792 }
793 }
794
795 if (enc_dbcs != 0 && prev_cells > 1)
796 screen_char_2(off_to - prev_cells, row,
797 col + coloff - prev_cells);
798 else
Bram Moolenaar9c697322006-10-09 20:11:17 +0000799 screen_char(off_to - prev_cells, row,
800 col + coloff - prev_cells);
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
803#endif
804 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
805 ' ', ' ', 0);
Bram Moolenaarb9081882022-07-09 04:56:24 +0100806 while (col < clear_width)
807 {
808 ScreenCols[off_to++] = MAXCOL;
809 ++col;
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 }
813
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200814 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100815#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200816 && !(flags & SLF_POPUP) // no separator for popup window
817#endif
818 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200820 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200821 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200822 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100824#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200825 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200828 int c;
829
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100830 c = fillchar_vsep(&hl, wp);
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200831 if (ScreenLines[off_to] != (schar_T)c
832 || (enc_utf8 && (int)ScreenLinesUC[off_to]
833 != (c >= 0x80 ? c : 0))
834 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200836 ScreenLines[off_to] = c;
837 ScreenAttrs[off_to] = hl;
838 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200840 if (c >= 0x80)
841 {
842 ScreenLinesUC[off_to] = c;
843 ScreenLinesC[0][off_to] = 0;
844 }
845 else
846 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200848 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 }
852 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 LineWraps[row] = FALSE;
854 }
855}
856
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000857#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000859 * Mirror text "str" for right-left displaying.
860 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000862 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100863rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 char_u *p1, *p2;
866 int t;
867
868 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
869 {
870 t = *p1;
871 *p1 = *p2;
872 *p2 = t;
873 }
874}
875#endif
876
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * Draw the verticap separator right of window "wp" starting with line "row".
879 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200880 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100881draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882{
883 int hl;
884 int c;
885
886 if (wp->w_vsep_width)
887 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100888 // draw the vertical separator right of this window
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100889 c = fillchar_vsep(&hl, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
891 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
892 c, ' ', hl);
893 }
894}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100896static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
898/*
Bram Moolenaar367329b2007-08-30 11:53:22 +0000899 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 */
901 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100902status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903{
904 int len = 0;
905
906#ifdef FEAT_MENU
907 int emenu = (xp->xp_context == EXPAND_MENUS
908 || xp->xp_context == EXPAND_MENUNAMES);
909
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100910 // Check for menu separators - replace with '|'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (emenu && menu_is_separator(s))
912 return 1;
913#endif
914
915 while (*s != NUL)
916 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000917 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +0000918 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100919 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921
922 return len;
923}
924
925/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000926 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000927 * These are backslashes used for escaping. Do show backslashes in help tags.
928 */
929 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100930skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000931{
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000932 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000933#ifdef FEAT_MENU
934 || ((xp->xp_context == EXPAND_MENUS
935 || xp->xp_context == EXPAND_MENUNAMES)
936 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
937#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000938 )
939 {
940#ifndef BACKSLASH_IN_FILENAME
941 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
942 return 2;
943#endif
944 return 1;
945 }
946 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000947}
948
949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 * Show wildchar matches in the status line.
951 * Show at least the "match" item.
952 * We start at item 'first_match' in the list and show all matches that fit.
953 *
954 * If inversion is possible we use it. Else '=' characters are used.
955 */
956 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100957win_redr_status_matches(
958 expand_T *xp,
959 int num_matches,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100960 char_u **matches, // list of matches
Bram Moolenaar05540972016-01-30 20:31:25 +0100961 int match,
962 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
965 int row;
966 char_u *buf;
967 int len;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100968 int clen; // length in screen cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 int fillchar;
970 int attr;
971 int i;
972 int highlight = TRUE;
973 char_u *selstart = NULL;
974 int selstart_col = 0;
975 char_u *selend = NULL;
976 static int first_match = 0;
977 int add_left = FALSE;
978 char_u *s;
979#ifdef FEAT_MENU
980 int emenu;
981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100984 if (matches == NULL) // interrupted completion?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 return;
986
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000987 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200988 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000989 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200990 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (buf == NULL)
992 return;
993
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100994 if (match == -1) // don't show match but original text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 {
996 match = 0;
997 highlight = FALSE;
998 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100999 // count 1 for the ending ">"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 clen = status_match_len(xp, L_MATCH(match)) + 3;
1001 if (match == 0)
1002 first_match = 0;
1003 else if (match < first_match)
1004 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001005 // jumping left, as far as we can go
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 first_match = match;
1007 add_left = TRUE;
1008 }
1009 else
1010 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001011 // check if match fits on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 for (i = first_match; i < match; ++i)
1013 clen += status_match_len(xp, L_MATCH(i)) + 2;
1014 if (first_match > 0)
1015 clen += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001016 // jumping right, put match at the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if ((long)clen > Columns)
1018 {
1019 first_match = match;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001020 // if showing the last match, we can add some on the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 clen = 2;
1022 for (i = match; i < num_matches; ++i)
1023 {
1024 clen += status_match_len(xp, L_MATCH(i)) + 2;
1025 if ((long)clen >= Columns)
1026 break;
1027 }
1028 if (i == num_matches)
1029 add_left = TRUE;
1030 }
1031 }
1032 if (add_left)
1033 while (first_match > 0)
1034 {
1035 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
1036 if ((long)clen >= Columns)
1037 break;
1038 --first_match;
1039 }
1040
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001041 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 if (first_match == 0)
1044 {
1045 *buf = NUL;
1046 len = 0;
1047 }
1048 else
1049 {
1050 STRCPY(buf, "< ");
1051 len = 2;
1052 }
1053 clen = len;
1054
1055 i = first_match;
1056 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
1057 {
1058 if (i == match)
1059 {
1060 selstart = buf + len;
1061 selstart_col = clen;
1062 }
1063
1064 s = L_MATCH(i);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001065 // Check for menu separators - replace with '|'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#ifdef FEAT_MENU
1067 emenu = (xp->xp_context == EXPAND_MENUS
1068 || xp->xp_context == EXPAND_MENUNAMES);
1069 if (emenu && menu_is_separator(s))
1070 {
1071 STRCPY(buf + len, transchar('|'));
1072 l = (int)STRLEN(buf + len);
1073 len += l;
1074 clen += l;
1075 }
1076 else
1077#endif
1078 for ( ; *s != NUL; ++s)
1079 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001080 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001082 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 STRNCPY(buf + len, s, l);
1085 s += l - 1;
1086 len += l;
1087 }
1088 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
1090 STRCPY(buf + len, transchar_byte(*s));
1091 len += (int)STRLEN(buf + len);
1092 }
1093 }
1094 if (i == match)
1095 selend = buf + len;
1096
1097 *(buf + len++) = ' ';
1098 *(buf + len++) = ' ';
1099 clen += 2;
1100 if (++i == num_matches)
1101 break;
1102 }
1103
1104 if (i != num_matches)
1105 {
1106 *(buf + len++) = '>';
1107 ++clen;
1108 }
1109
1110 buf[len] = NUL;
1111
1112 row = cmdline_row - 1;
1113 if (row >= 0)
1114 {
1115 if (wild_menu_showing == 0)
1116 {
1117 if (msg_scrolled > 0)
1118 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001119 // Put the wildmenu just above the command line. If there is
1120 // no room, scroll the screen one line up.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (cmdline_row == Rows - 1)
1122 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001123 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 ++msg_scrolled;
1125 }
1126 else
1127 {
1128 ++cmdline_row;
1129 ++row;
1130 }
1131 wild_menu_showing = WM_SCROLLED;
1132 }
1133 else
1134 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001135 // Create status line if needed by setting 'laststatus' to 2.
1136 // Set 'winminheight' to zero to avoid that the window is
1137 // resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (lastwin->w_status_height == 0)
1139 {
1140 save_p_ls = p_ls;
1141 save_p_wmh = p_wmh;
1142 p_ls = 2;
1143 p_wmh = 0;
1144 last_status(FALSE);
1145 }
1146 wild_menu_showing = WM_SHOWN;
1147 }
1148 }
1149
1150 screen_puts(buf, row, 0, attr);
1151 if (selstart != NULL && highlight)
1152 {
1153 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001154 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156
1157 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
1158 }
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 vim_free(buf);
1162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 * Return TRUE if the status line of window "wp" is connected to the status
1166 * line of the window right of it. If not, then it's a vertical separator.
1167 * Only call if (wp->w_vsep_width != 0).
1168 */
1169 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001170stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 frame_T *fr;
1173
1174 fr = wp->w_frame;
1175 while (fr->fr_parent != NULL)
1176 {
1177 if (fr->fr_parent->fr_layout == FR_COL)
1178 {
1179 if (fr->fr_next != NULL)
1180 break;
1181 }
1182 else
1183 {
1184 if (fr->fr_next != NULL)
1185 return TRUE;
1186 }
1187 fr = fr->fr_parent;
1188 }
1189 return FALSE;
1190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193/*
1194 * Get the value to show for the language mappings, active 'keymap'.
1195 */
1196 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001197get_keymap_str(
1198 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001199 char_u *fmt, // format string containing one %s item
1200 char_u *buf, // buffer for the result
1201 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
1203 char_u *p;
1204
1205 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
1206 return FALSE;
1207
1208 {
1209#ifdef FEAT_EVAL
1210 buf_T *old_curbuf = curbuf;
1211 win_T *old_curwin = curwin;
1212 char_u *s;
1213
1214 curbuf = wp->w_buffer;
1215 curwin = wp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001216 STRCPY(buf, "b:keymap_name"); // must be writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01001218 s = p = eval_to_string(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 --emsg_skip;
1220 curbuf = old_curbuf;
1221 curwin = old_curwin;
1222 if (p == NULL || *p == NUL)
1223#endif
1224 {
1225#ifdef FEAT_KEYMAP
1226 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
1227 p = wp->w_buffer->b_p_keymap;
1228 else
1229#endif
1230 p = (char_u *)"lang";
1231 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001232 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 buf[0] = NUL;
1234#ifdef FEAT_EVAL
1235 vim_free(s);
1236#endif
1237 }
1238 return buf[0] != NUL;
1239}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241#if defined(FEAT_STL_OPT) || defined(PROTO)
1242/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001243 * Redraw the status line or ruler of window "wp".
1244 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001246 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001247win_redr_custom(
1248 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001249 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001251 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 int attr;
1253 int curattr;
1254 int row;
1255 int col = 0;
1256 int maxwidth;
1257 int width;
1258 int n;
1259 int len;
1260 int fillchar;
1261 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001262 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 char_u *p;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001264 stl_hlrec_T *hltab;
1265 stl_hlrec_T *tabtab;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001266 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01001267 win_T *ewp;
1268 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001270 // There is a tiny chance that this gets called recursively: When
1271 // redrawing a status line triggers redrawing the ruler or tabline.
1272 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001273 if (entered)
1274 return;
1275 entered = TRUE;
1276
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001277 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001278 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001280 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001281 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001282 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001283 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001284 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001285 maxwidth = Columns;
1286# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001287 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001290 else
1291 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001292 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001293 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001294 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001295
1296 if (draw_ruler)
1297 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001298 stl = p_ruf;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001299 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001300 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001301 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001302 if (*++stl == '-')
1303 stl++;
1304 if (atoi((char *)stl))
1305 while (VIM_ISDIGIT(*stl))
1306 stl++;
1307 if (*stl++ != '(')
1308 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001309 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001310 col = ru_col - (Columns - wp->w_width);
1311 if (col < (wp->w_width + 1) / 2)
1312 col = (wp->w_width + 1) / 2;
1313 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001314 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001315 {
1316 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001317 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001318 fillchar = ' ';
1319 attr = 0;
1320 }
1321
1322# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001323 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001324# endif
1325 }
1326 else
1327 {
1328 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00001329 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001330 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001331 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001332# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001333 use_sandbox = was_set_insecurely((char_u *)"statusline",
1334 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001335# endif
1336 }
1337
Bram Moolenaar53f81742017-09-22 14:35:51 +02001338 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001339 }
1340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001342 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001344 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1345 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001346 ewp = wp == NULL ? curwin : wp;
1347 p_crb_save = ewp->w_p_crb;
1348 ewp->w_p_crb = FALSE;
1349
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001350 // Make a copy, because the statusline may include a function call that
1351 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001352 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001353 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00001354 stl, use_sandbox,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001355 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001356 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001357 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001359 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001360 p = transstr(buf);
1361 if (p != NULL)
1362 {
1363 vim_strncpy(buf, p, sizeof(buf) - 1);
1364 vim_free(p);
1365 }
1366
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001367 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001368 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001369 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 ++width;
1373 }
1374 buf[len] = NUL;
1375
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001376 /*
1377 * Draw each snippet with the specified highlighting.
1378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 curattr = attr;
1380 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001381 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001383 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 screen_puts_len(p, len, row, col, curattr);
1385 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001386 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001388 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001390 else if (hltab[n].userhl < 0)
1391 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001392#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001393 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1394 && wp->w_status_height != 0)
1395 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001396 else if (wp != NULL && bt_terminal(wp->w_buffer)
1397 && wp->w_status_height != 0)
1398 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001399#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001400 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001401 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001403 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001406
1407 if (wp == NULL)
1408 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001409 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001410 col = 0;
1411 len = 0;
1412 p = buf;
1413 fillchar = 0;
1414 for (n = 0; tabtab[n].start != NULL; n++)
1415 {
1416 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1417 while (col < len)
1418 TabPageIdxs[col++] = fillchar;
1419 p = tabtab[n].start;
1420 fillchar = tabtab[n].userhl;
1421 }
1422 while (col < Columns)
1423 TabPageIdxs[col++] = fillchar;
1424 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001425
1426theend:
1427 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428}
1429
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001430#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431
1432/*
1433 * Output a single character directly to the screen and update ScreenLines.
1434 */
1435 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001436screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 char_u buf[MB_MAXBYTES + 1];
1439
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001440 if (has_mbyte)
1441 buf[(*mb_char2bytes)(c, buf)] = NUL;
1442 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001443 {
1444 buf[0] = c;
1445 buf[1] = NUL;
1446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 screen_puts(buf, row, col, attr);
1448}
1449
1450/*
1451 * Get a single character directly from ScreenLines into "bytes[]".
1452 * Also return its attribute in *attrp;
1453 */
1454 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001455screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 unsigned off;
1458
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001459 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1461 {
1462 off = LineOffset[row] + col;
1463 *attrp = ScreenAttrs[off];
1464 bytes[0] = ScreenLines[off];
1465 bytes[1] = NUL;
1466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (enc_utf8 && ScreenLinesUC[off] != 0)
1468 bytes[utfc_char2bytes(off, bytes)] = NUL;
1469 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1470 {
1471 bytes[0] = ScreenLines[off];
1472 bytes[1] = ScreenLines2[off];
1473 bytes[2] = NUL;
1474 }
1475 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1476 {
1477 bytes[1] = ScreenLines[off + 1];
1478 bytes[2] = NUL;
1479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481}
1482
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001483/*
1484 * Return TRUE if composing characters for screen posn "off" differs from
1485 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001486 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001487 */
1488 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001489screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001490{
1491 int i;
1492
1493 for (i = 0; i < Screen_mco; ++i)
1494 {
1495 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1496 return TRUE;
1497 if (u8cc[i] == 0)
1498 break;
1499 }
1500 return FALSE;
1501}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503/*
1504 * Put string '*text' on the screen at position 'row' and 'col', with
1505 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1506 * Note: only outputs within one row, message is truncated at screen boundary!
1507 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1508 */
1509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001510screen_puts(
1511 char_u *text,
1512 int row,
1513 int col,
1514 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 screen_puts_len(text, -1, row, col, attr);
1517}
1518
1519/*
1520 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1521 * a NUL.
1522 */
1523 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001524screen_puts_len(
1525 char_u *text,
1526 int textlen,
1527 int row,
1528 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001529 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001531 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 unsigned off;
1533 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001534 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001536 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 int mbyte_blen = 1;
1538 int mbyte_cells = 1;
1539 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001540 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001542#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001543 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001544 int pc, nc, nc1;
1545 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001547 int force_redraw_this;
1548 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001549 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001551 // Safety check. The check for negative row and column is to fix issue
1552 // #4102. TODO: find out why row/col could be negative.
1553 if (ScreenLines == NULL
1554 || row >= screen_Rows || row < 0
1555 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001557 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001559 // When drawing over the right half of a double-wide char clear out the
1560 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001561 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001562#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001563 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001564#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001565 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001566 {
1567 ScreenLines[off - 1] = ' ';
1568 ScreenAttrs[off - 1] = 0;
1569 if (enc_utf8)
1570 {
1571 ScreenLinesUC[off - 1] = 0;
1572 ScreenLinesC[0][off - 1] = 0;
1573 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001574 // redraw the previous cell, make it empty
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001575 screen_char(off - 1, row, col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001576 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001577 force_redraw_next = TRUE;
1578 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001579
Bram Moolenaar367329b2007-08-30 11:53:22 +00001580 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001581 while (col < screen_Columns
1582 && (len < 0 || (int)(ptr - text) < len)
1583 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001586 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (has_mbyte)
1588 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001589 mbyte_blen = enc_utf8 && len > 0
1590 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1591 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1593 mbyte_cells = 1;
1594 else if (enc_dbcs != 0)
1595 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001596 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001598 u8c = len >= 0
1599 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1600 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001602#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1604 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001605 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1607 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001608 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 nc = NUL;
1610 nc1 = NUL;
1611 }
1612 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001613 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001614 nc = len >= 0
1615 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1616 (int)((text + len) - ptr - mbyte_blen))
1617 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001618 nc1 = pcc[0];
1619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 pc = prev_c;
1621 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001622 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
1624 else
1625 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001626#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001627 if (col + mbyte_cells > screen_Columns)
1628 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001629 // Only 1 cell left, but character requires 2 cells:
1630 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001631 c = '>';
1632 mbyte_cells = 1;
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001637 force_redraw_this = force_redraw_next;
1638 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001639
1640 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 || (mbyte_cells == 2
1642 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1643 || (enc_dbcs == DBCS_JPNU
1644 && c == 0x8e
1645 && ScreenLines2[off] != ptr[1])
1646 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001647 && (ScreenLinesUC[off] !=
1648 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1649 || (ScreenLinesUC[off] != 0
1650 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001652 || exmode_active;
1653
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001654 if ((need_redraw || force_redraw_this)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001655#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001656 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001657#endif
1658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 {
1660#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001661 // The bold trick makes a single row of pixels appear in the next
1662 // character. When a bold character is removed, the next
1663 // character should be redrawn too. This happens for our own GUI
1664 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001665 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666# ifdef FEAT_GUI
1667 gui.in_use
1668# endif
1669# if defined(FEAT_GUI) && defined(UNIX)
1670 ||
1671# endif
1672# ifdef UNIX
1673 term_is_xterm
1674# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001675 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001677 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001679 if (n > HL_ALL)
1680 n = syn_attr2attr(n);
1681 if (n & HL_BOLD)
1682 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001685 // When at the end of the text and overwriting a two-cell
1686 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001687 // cell. Also when overwriting the left half of a two-cell char
1688 // with the right half of a two-cell char. Do this only once
1689 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if (clear_next_cell)
1691 clear_next_cell = FALSE;
1692 else if (has_mbyte
1693 && (len < 0 ? ptr[mbyte_blen] == NUL
1694 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001695 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001697 && (*mb_off2cells)(off, max_off) == 1
1698 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 clear_next_cell = TRUE;
1700
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001701 // Make sure we never leave a second byte of a double-byte behind,
1702 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001704 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001706 && (*mb_off2cells)(off, max_off) == 1
1707 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 ScreenLines[off] = c;
1710 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001711 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 if (enc_utf8)
1713 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001714 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 ScreenLinesUC[off] = 0;
1716 else
1717 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001718 int i;
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001721 for (i = 0; i < Screen_mco; ++i)
1722 {
1723 ScreenLinesC[i][off] = u8cc[i];
1724 if (u8cc[i] == 0)
1725 break;
1726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
1728 if (mbyte_cells == 2)
1729 {
1730 ScreenLines[off + 1] = 0;
1731 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001732 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
1734 screen_char(off, row, col);
1735 }
1736 else if (mbyte_cells == 2)
1737 {
1738 ScreenLines[off + 1] = ptr[1];
1739 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001740 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 screen_char_2(off, row, col);
1742 }
1743 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1744 {
1745 ScreenLines2[off] = ptr[1];
1746 screen_char(off, row, col);
1747 }
1748 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 screen_char(off, row, col);
1750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 if (has_mbyte)
1752 {
1753 off += mbyte_cells;
1754 col += mbyte_cells;
1755 ptr += mbyte_blen;
1756 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001757 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001758 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001759 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001761 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001762 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
1765 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 {
1767 ++off;
1768 ++col;
1769 ++ptr;
1770 }
1771 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001772
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001773 // If we detected the next character needs to be redrawn, but the text
1774 // doesn't extend up to there, update the character here.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001775 if (force_redraw_next && col < screen_Columns)
1776 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001777 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1778 screen_char_2(off, row, col);
1779 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001780 screen_char(off, row, col);
1781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782}
1783
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001784#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001786 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001788 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001789start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790{
1791 if (p_hls && !no_hlsearch)
1792 {
Bram Moolenaar0b6849e2020-05-02 18:33:25 +02001793 end_search_hl(); // just in case it wasn't called before
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001794 last_pat_prog(&screen_search_hl.rm);
1795 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
1797}
1798
1799/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001800 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001802 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001803end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001805 if (screen_search_hl.rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001807 vim_regfree(screen_search_hl.rm.regprog);
1808 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
1810}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001811#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001814screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
1816 attrentry_T *aep = NULL;
1817
1818 screen_attr = attr;
1819 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001820#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 && termcap_active
1822#endif
1823 )
1824 {
1825#ifdef FEAT_GUI
1826 if (gui.in_use)
1827 {
1828 char buf[20];
1829
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001830 // The GUI handles this internally.
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001831 sprintf(buf, "\033|%dh", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 OUT_STR(buf);
1833 }
1834 else
1835#endif
1836 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001837 if (attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001839 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 aep = syn_cterm_attr2entry(attr);
1841 else
1842 aep = syn_term_attr2entry(attr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001843 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 attr = 0;
1845 else
1846 attr = aep->ae_attr;
1847 }
Bram Moolenaara050b942019-12-02 21:35:31 +01001848#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1849 if (use_vtp())
1850 {
1851 guicolor_T defguifg, defguibg;
1852 int defctermfg, defctermbg;
1853
1854 // If FG and BG are unset, the color is undefined when
1855 // BOLD+INVERSE. Use Normal as the default value.
1856 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1857 &defguibg);
1858
1859 if (p_tgc)
1860 {
1861 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1862 term_fg_rgb_color(defguifg);
1863 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1864 term_bg_rgb_color(defguibg);
1865 }
1866 else if (t_colors >= 256)
1867 {
1868 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1869 term_fg_color(defctermfg);
1870 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1871 term_bg_color(defctermbg);
1872 }
1873 }
1874#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001875 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001877 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001878#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001879 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1880 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1881 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001882#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001883 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001884 // If the Normal FG color has BOLD attribute and the new HL
1885 // has a FG color defined, clear BOLD.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001886 out_str(T_ME);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001887 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 out_str(T_SO);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001889 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001890 out_str(T_UCS);
Bram Moolenaar84f54632022-06-29 18:39:11 +01001891 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1892 out_str(T_USS);
1893 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1894 out_str(T_DS);
1895 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1896 out_str(T_CDS);
1897 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1898 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1899 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1900 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1901 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
Bram Moolenaar45a00002017-12-22 21:12:34 +01001902 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 out_str(T_US);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001904 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 out_str(T_CZH);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001906 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 out_str(T_MR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001908 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001909 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
1911 /*
1912 * Output the color or start string after bold etc., in case the
1913 * bold etc. override the color setting.
1914 */
1915 if (aep != NULL)
1916 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001917#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001918 // When 'termguicolors' is set but fg or bg is unset,
1919 // fall back to the cterm colors. This helps for SpellBad,
1920 // where the GUI uses a red undercurl.
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001921 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001923 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001924 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001925 }
1926 else
1927#endif
1928 if (t_colors > 1)
1929 {
1930 if (aep->ae_u.cterm.fg_color)
1931 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1932 }
1933#ifdef FEAT_TERMGUICOLORS
1934 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1935 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001936 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001937 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001940#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001941 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001943 if (aep->ae_u.cterm.bg_color)
1944 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1945 }
Bram Moolenaare023e882020-05-31 16:42:30 +02001946#ifdef FEAT_TERMGUICOLORS
1947 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1948 {
1949 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1950 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1951 }
1952 else
1953#endif
1954 if (t_colors > 1)
1955 {
1956 if (aep->ae_u.cterm.ul_color)
1957 term_ul_color(aep->ae_u.cterm.ul_color - 1);
1958 }
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001959
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001960 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001961 {
1962 if (aep->ae_u.term.start != NULL)
1963 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 }
1966 }
1967 }
1968}
1969
1970 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001971screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001973 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001974#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001975 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001979#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 && termcap_active
1981#endif
1982 )
1983 {
1984#ifdef FEAT_GUI
1985 if (gui.in_use)
1986 {
1987 char buf[20];
1988
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001989 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001990 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 OUT_STR(buf);
1992 }
1993 else
1994#endif
1995 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001996 int is_under;
1997
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001998 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
2000 attrentry_T *aep;
2001
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002002 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
2004 /*
2005 * Assume that t_me restores the original colors!
2006 */
2007 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002008 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002009#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002010 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
2011 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002012# ifdef FEAT_VTP
2013 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
2014# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002015 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002016#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002017 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002018#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002019 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
2020 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002021# ifdef FEAT_VTP
2022 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
2023# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002024 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002025#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002026 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002028#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2029 if (use_vtp())
2030 {
2031 if (do_ME_fg && do_ME_bg)
2032 do_ME = TRUE;
2033
2034 // FG and BG cannot be separated in T_ME, which is not
2035 // efficient.
2036 if (!do_ME && do_ME_fg)
2037 out_str((char_u *)"\033|39m"); // restore FG
2038 if (!do_ME && do_ME_bg)
2039 out_str((char_u *)"\033|49m"); // restore BG
2040 }
2041 else
2042 {
2043 // Process FG and BG at once.
2044 if (!do_ME)
2045 do_ME = do_ME_fg | do_ME_bg;
2046 }
2047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049 else
2050 {
2051 aep = syn_term_attr2entry(screen_attr);
2052 if (aep != NULL && aep->ae_u.term.stop != NULL)
2053 {
2054 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
2055 do_ME = TRUE;
2056 else
2057 out_str(aep->ae_u.term.stop);
2058 }
2059 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002060 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 screen_attr = 0;
2062 else
2063 screen_attr = aep->ae_attr;
2064 }
2065
2066 /*
2067 * Often all ending-codes are equal to T_ME. Avoid outputting the
2068 * same sequence several times.
2069 */
2070 if (screen_attr & HL_STANDOUT)
2071 {
2072 if (STRCMP(T_SE, T_ME) == 0)
2073 do_ME = TRUE;
2074 else
2075 out_str(T_SE);
2076 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002077 is_under = (screen_attr & (HL_UNDERCURL
2078 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
2079 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01002080 {
2081 if (STRCMP(T_UCE, T_ME) == 0)
2082 do_ME = TRUE;
2083 else
2084 out_str(T_UCE);
2085 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002086 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
2088 if (STRCMP(T_UE, T_ME) == 0)
2089 do_ME = TRUE;
2090 else
2091 out_str(T_UE);
2092 }
2093 if (screen_attr & HL_ITALIC)
2094 {
2095 if (STRCMP(T_CZR, T_ME) == 0)
2096 do_ME = TRUE;
2097 else
2098 out_str(T_CZR);
2099 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002100 if (screen_attr & HL_STRIKETHROUGH)
2101 {
2102 if (STRCMP(T_STE, T_ME) == 0)
2103 do_ME = TRUE;
2104 else
2105 out_str(T_STE);
2106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
2108 out_str(T_ME);
2109
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002110#ifdef FEAT_TERMGUICOLORS
2111 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002113 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002114 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002115 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002116 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02002117 if (cterm_normal_ul_gui_color != INVALCOLOR)
2118 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002119 }
2120 else
2121#endif
2122 {
2123 if (t_colors > 1)
2124 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002125 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002126 if (cterm_normal_fg_color != 0)
2127 term_fg_color(cterm_normal_fg_color - 1);
2128 if (cterm_normal_bg_color != 0)
2129 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02002130 if (cterm_normal_ul_color != 0)
2131 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002132 if (cterm_normal_fg_bold)
2133 out_str(T_MD);
2134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
2136 }
2137 }
2138 screen_attr = 0;
2139}
2140
2141/*
2142 * Reset the colors for a cterm. Used when leaving Vim.
2143 * The machine specific code may override this again.
2144 */
2145 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002146reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002148 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002150 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002151#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002152 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2153 || cterm_normal_bg_gui_color != INVALCOLOR)
2154 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002155#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
2159 out_str(T_OP);
2160 screen_attr = -1;
2161 }
2162 if (cterm_normal_fg_bold)
2163 {
2164 out_str(T_ME);
2165 screen_attr = -1;
2166 }
2167 }
2168}
2169
2170/*
2171 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2172 * using the attributes from ScreenAttrs["off"].
2173 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002174 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002175screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 int attr;
2178
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002179 // Check for illegal values, just in case (could happen just after
2180 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (row >= screen_Rows || col >= screen_Columns)
2182 return;
2183
Bram Moolenaar33796b32019-06-08 16:01:13 +02002184 // Skip if under the popup menu.
Bram Moolenaar393f8d62022-10-02 14:28:30 +01002185 if (skip_for_popup(row, col))
Bram Moolenaarae654382019-01-17 21:09:05 +01002186 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002187
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002188 // Outputting a character in the last cell on the screen may scroll the
2189 // screen up. Only do it when the "xn" termcap property is set, otherwise
2190 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01002191 if (*T_XN == NUL
2192 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002194 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 && !cmdmsg_rl
2196#endif
2197 )
2198 {
2199 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002200 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 return;
2202 }
2203
2204 /*
2205 * Stop highlighting first, so it's easier to move the cursor.
2206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 if (screen_char_attr != 0)
2208 attr = screen_char_attr;
2209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 attr = ScreenAttrs[off];
2211 if (screen_attr != attr)
2212 screen_stop_highlight();
2213
2214 windgoto(row, col);
2215
2216 if (screen_attr != attr)
2217 screen_start_highlight(attr);
2218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (enc_utf8 && ScreenLinesUC[off] != 0)
2220 {
2221 char_u buf[MB_MAXBYTES + 1];
2222
Bram Moolenaarcb070082016-04-02 22:14:51 +02002223 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002224 {
2225 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002226#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002227 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002228#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002229 )
2230 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002231 // Clear the two screen cells. If the character is actually
2232 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002233 out_str((char_u *)" ");
2234 term_windgoto(row, col);
2235 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002236 // not sure where the cursor is after drawing the ambiguous width
2237 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002238 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002239 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002240 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002242
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002243 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002244 buf[utfc_char2bytes(off, buf)] = NUL;
2245 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002251 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2253 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
2255
2256 screen_cur_col++;
2257}
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259/*
2260 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2261 * on the screen at position 'row' and 'col'.
2262 * The attributes of the first byte is used for all. This is required to
2263 * output the two bytes of a double-byte character with nothing in between.
2264 */
2265 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002266screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002268 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2270 return;
2271
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002272 // Outputting the last character on the screen may scrollup the screen.
2273 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2275 {
2276 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002277 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 return;
2279 }
2280
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002281 // Output the first byte normally (positions the cursor), then write the
2282 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 screen_char(off, row, col);
2284 out_char(ScreenLines[off + 1]);
2285 ++screen_cur_col;
2286}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288/*
2289 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2290 * This uses the contents of ScreenLines[] and doesn't change it.
2291 */
2292 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002293screen_draw_rectangle(
2294 int row,
2295 int col,
2296 int height,
2297 int width,
2298 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299{
2300 int r, c;
2301 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002302 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002304 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002305 if (ScreenLines == NULL)
2306 return;
2307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (invert)
2309 screen_char_attr = HL_INVERSE;
2310 for (r = row; r < row + height; ++r)
2311 {
2312 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002313 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 for (c = col; c < col + width; ++c)
2315 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002316 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
2318 screen_char_2(off + c, r, c);
2319 ++c;
2320 }
2321 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
2323 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002324 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327 }
2328 }
2329 screen_char_attr = 0;
2330}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332/*
2333 * Redraw the characters for a vertically split window.
2334 */
2335 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002336redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
2338 int col;
2339 int width;
2340
2341# ifdef FEAT_CLIPBOARD
2342 clip_may_clear_selection(row, end - 1);
2343# endif
2344
2345 if (wp == NULL)
2346 {
2347 col = 0;
2348 width = Columns;
2349 }
2350 else
2351 {
2352 col = wp->w_wincol;
2353 width = wp->w_width;
2354 }
2355 screen_draw_rectangle(row, col, end - row, width, FALSE);
2356}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002358 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002359space_to_screenline(int off, int attr)
2360{
2361 ScreenLines[off] = ' ';
2362 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002363 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002364 if (enc_utf8)
2365 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002366}
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002369 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2370 * to "end_col" (exclusive) with character "c1" in first column followed by
2371 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 */
2373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002374screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002375 int start_row,
2376 int end_row,
2377 int start_col,
2378 int end_col,
2379 int c1,
2380 int c2,
2381 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002383 int row;
2384 int col;
2385 int off;
2386 int end_off;
2387 int did_delete;
2388 int c;
2389 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002391 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#endif
2393
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002394 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002396 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 end_col = screen_Columns;
2398 if (ScreenLines == NULL
2399 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002400 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 return;
2402
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002403 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 norm_term = (
2405#ifdef FEAT_GUI
2406 !gui.in_use &&
2407#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002408 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 for (row = start_row; row < end_row; ++row)
2410 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002411 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002412#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002413 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002414#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002415 )
2416 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002417 // When drawing over the right half of a double-wide char clear
2418 // out the left half. When drawing over the left half of a
2419 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002420 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002421 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002422 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002423 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002424 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /*
2427 * Try to use delete-line termcap code, when no attributes or in a
2428 * "normal" terminal, where a bold/italic space is just a
2429 * space.
2430 */
2431 did_delete = FALSE;
2432 if (c2 == ' '
2433 && end_col == Columns
2434 && can_clear(T_CE)
2435 && (attr == 0
2436 || (norm_term
2437 && attr <= HL_ALL
2438 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2439 {
2440 /*
2441 * check if we really need to clear something
2442 */
2443 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002444 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 ++col;
2446
2447 off = LineOffset[row] + col;
2448 end_off = LineOffset[row] + end_col;
2449
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002450 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 if (enc_utf8)
2452 while (off < end_off && ScreenLines[off] == ' '
2453 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2454 ++off;
2455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 while (off < end_off && ScreenLines[off] == ' '
2457 && ScreenAttrs[off] == 0)
2458 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002459 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
2461 col = off - LineOffset[row];
2462 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002463 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002465 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002467 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002469 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 ++off;
2471 }
2472 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002473 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
2475
2476 off = LineOffset[row] + start_col;
2477 c = c1;
2478 for (col = start_col; col < end_col; ++col)
2479 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002480 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002481 || (enc_utf8 && (int)ScreenLinesUC[off]
2482 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002484 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485#if defined(FEAT_GUI) || defined(UNIX)
2486 || force_next
2487#endif
2488 )
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002489#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002490 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002491 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002492#endif
2493 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002496 // The bold trick may make a single row of pixels appear in
2497 // the next character. When a bold character is removed, the
2498 // next character should be redrawn too. This happens for our
2499 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (
2501# ifdef FEAT_GUI
2502 gui.in_use
2503# endif
2504# if defined(FEAT_GUI) && defined(UNIX)
2505 ||
2506# endif
2507# ifdef UNIX
2508 term_is_xterm
2509# endif
2510 )
2511 {
2512 if (ScreenLines[off] != ' '
2513 && (ScreenAttrs[off] > HL_ALL
2514 || ScreenAttrs[off] & HL_BOLD))
2515 force_next = TRUE;
2516 else
2517 force_next = FALSE;
2518 }
2519#endif
2520 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (enc_utf8)
2522 {
2523 if (c >= 0x80)
2524 {
2525 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002526 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528 else
2529 ScreenLinesUC[off] = 0;
2530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 ScreenAttrs[off] = attr;
2532 if (!did_delete || c != ' ')
2533 screen_char(off, row, col);
2534 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002535 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 ++off;
2537 if (col == start_col)
2538 {
2539 if (did_delete)
2540 break;
2541 c = c2;
2542 }
2543 }
2544 if (end_col == Columns)
2545 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002546 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002549 if (start_col == 0 && end_col == Columns
2550 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002551 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002552 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002553 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555 }
2556}
2557
2558/*
2559 * Check if there should be a delay. Used before clearing or redrawing the
2560 * screen or the command line.
2561 */
2562 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002563check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2566 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002567 && emsg_silent == 0
2568 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002571 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 emsg_on_display = FALSE;
2573 if (check_msg_scroll)
2574 msg_scroll = FALSE;
2575 }
2576}
2577
2578/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002579 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2580 */
2581 static void
2582clear_TabPageIdxs(void)
2583{
2584 int scol;
2585
2586 for (scol = 0; scol < Columns; ++scol)
2587 TabPageIdxs[scol] = 0;
2588}
2589
2590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002592 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 * Returns TRUE if there is a valid screen to write to.
2594 * Returns FALSE when starting up and screen not initialized yet.
2595 */
2596 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002597screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002599 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 return (ScreenLines != NULL);
2601}
2602
2603/*
2604 * Resize the shell to Rows and Columns.
2605 * Allocate ScreenLines[] and associated items.
2606 *
2607 * There may be some time between setting Rows and Columns and (re)allocating
2608 * ScreenLines[]. This happens when starting up and when (manually) changing
2609 * the shell size. Always use screen_Rows and screen_Columns to access items
2610 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2611 * final size of the shell is needed.
2612 */
2613 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002614screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 int new_row, old_row;
2617#ifdef FEAT_GUI
2618 int old_Rows;
2619#endif
2620 win_T *wp;
2621 int outofmem = FALSE;
2622 int len;
2623 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002625 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002627 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002629 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 unsigned *new_LineOffset;
2631 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002632 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002633#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002634 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002635 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002636 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002637#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002638 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002639 static int entered = FALSE; // avoid recursiveness
2640 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002641 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002643retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 /*
2645 * Allocation of the screen buffers is done only when the size changes and
2646 * when Rows and Columns have been set and we have started doing full
2647 * screen stuff.
2648 */
2649 if ((ScreenLines != NULL
2650 && Rows == screen_Rows
2651 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 && enc_utf8 == (ScreenLinesUC != NULL)
2653 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002654 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 || Rows == 0
2656 || Columns == 0
2657 || (!full_screen && ScreenLines == NULL))
2658 return;
2659
2660 /*
2661 * It's possible that we produce an out-of-memory message below, which
2662 * will cause this function to be called again. To break the loop, just
2663 * return here.
2664 */
2665 if (entered)
2666 return;
2667 entered = TRUE;
2668
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002669 /*
2670 * Note that the window sizes are updated before reallocating the arrays,
2671 * thus we must not redraw here!
2672 */
2673 ++RedrawingDisabled;
2674
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002675 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002677#ifdef FEAT_GUI_HAIKU
2678 vim_lock_screen(); // be safe, put it here
2679#endif
2680
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002681 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683 /*
2684 * We're changing the size of the screen.
2685 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2686 * - Move lines from the old arrays into the new arrays, clear extra
2687 * lines (unless the screen is going to be cleared).
2688 * - Free the old arrays.
2689 *
2690 * If anything fails, make ScreenLines NULL, so we don't do anything!
2691 * Continuing with the old ScreenLines may result in a crash, because the
2692 * size is wrong.
2693 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002694 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002696 if (aucmd_win != NULL)
2697 win_free_lsize(aucmd_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002698#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002699 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002700 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002701 win_free_lsize(wp);
2702 // tab-local popup windows
2703 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002704 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002705 win_free_lsize(wp);
2706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002708 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002709 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (enc_utf8)
2711 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002712 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002713 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002714 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2715 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002718 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2719 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002720 // Clear ScreenCols to avoid a warning for unitialized memory in
2721 // jump_to_mouse().
2722 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002723 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2724 new_LineWraps = LALLOC_MULT(char_u, Rows);
2725 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002726#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002727 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002728 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002729 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002732 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
2734 if (win_alloc_lines(wp) == FAIL)
2735 {
2736 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002737 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 }
2739 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002740 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
2741 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002742 outofmem = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002743#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002744 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002745 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002746 if (win_alloc_lines(wp) == FAIL)
2747 {
2748 outofmem = TRUE;
2749 goto give_up;
2750 }
2751 // tab-local popup windows
2752 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002753 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002754 if (win_alloc_lines(wp) == FAIL)
2755 {
2756 outofmem = TRUE;
2757 goto give_up;
2758 }
2759#endif
2760
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002761give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002763 for (i = 0; i < p_mco; ++i)
2764 if (new_ScreenLinesC[i] == NULL)
2765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002767 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002770 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 || new_LineOffset == NULL
2772 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002773 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002774#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002775 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002776 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002777 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 || outofmem)
2780 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002781 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002782 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002783 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002784 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2785
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002786 // Remember we did this to avoid getting outofmem messages over
2787 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002788 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002789 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002790 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002791 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002792 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002793 VIM_CLEAR(new_ScreenLinesC[i]);
2794 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002795 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002796 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002797 VIM_CLEAR(new_LineOffset);
2798 VIM_CLEAR(new_LineWraps);
2799 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002800#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002801 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002802 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002803 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806 else
2807 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002808 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002809
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 for (new_row = 0; new_row < Rows; ++new_row)
2811 {
2812 new_LineOffset[new_row] = new_row * Columns;
2813 new_LineWraps[new_row] = FALSE;
2814
2815 /*
2816 * If the screen is not going to be cleared, copy as much as
2817 * possible from the old screen to the new one and clear the rest
2818 * (used when resizing the window at the "--more--" prompt or when
2819 * executing an external command, for the GUI).
2820 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002821 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {
2823 (void)vim_memset(new_ScreenLines + new_row * Columns,
2824 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 if (enc_utf8)
2826 {
2827 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2828 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002829 for (i = 0; i < p_mco; ++i)
2830 (void)vim_memset(new_ScreenLinesC[i]
2831 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 0, (size_t)Columns * sizeof(u8char_T));
2833 }
2834 if (enc_dbcs == DBCS_JPNU)
2835 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2836 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2838 0, (size_t)Columns * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002839 (void)vim_memset(new_ScreenCols + new_row * Columns,
2840 0, (size_t)Columns * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002842 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
2844 if (screen_Columns < Columns)
2845 len = screen_Columns;
2846 else
2847 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002848 // When switching to utf-8 don't copy characters, they
2849 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002850 if (!(enc_utf8 && ScreenLinesUC == NULL)
2851 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002852 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2853 ScreenLines + LineOffset[old_row],
2854 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002855 if (enc_utf8 && ScreenLinesUC != NULL
2856 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
2858 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2859 ScreenLinesUC + LineOffset[old_row],
2860 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002861 for (i = 0; i < p_mco; ++i)
2862 mch_memmove(new_ScreenLinesC[i]
2863 + new_LineOffset[new_row],
2864 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 (size_t)len * sizeof(u8char_T));
2866 }
2867 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2868 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2869 ScreenLines2 + LineOffset[old_row],
2870 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2872 ScreenAttrs + LineOffset[old_row],
2873 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002874 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2875 ScreenAttrs + LineOffset[old_row],
2876 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
2878 }
2879 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002880 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002882
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002883#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002884 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2885 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002889 free_screenlines();
2890
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002891 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002894 for (i = 0; i < p_mco; ++i)
2895 ScreenLinesC[i] = new_ScreenLinesC[i];
2896 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002899 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 LineOffset = new_LineOffset;
2901 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002902 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002903#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002904 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002905 popup_mask_next = new_popup_mask_next;
2906 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002907 popup_mask_refresh = TRUE;
2908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002910 // It's important that screen_Rows and screen_Columns reflect the actual
2911 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#ifdef FEAT_GUI
2913 old_Rows = screen_Rows;
2914#endif
2915 screen_Rows = Rows;
2916 screen_Columns = Columns;
2917
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002918 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002919 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002920 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#ifdef FEAT_GUI
2922 else if (gui.in_use
2923 && !gui.starting
2924 && ScreenLines != NULL
2925 && old_Rows != Rows)
2926 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002927 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2928
2929 // Adjust the position of the cursor, for when executing an external
2930 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002931 if (msg_row >= Rows) // Rows got smaller
2932 msg_row = Rows - 1; // put cursor at last row
2933 else if (Rows > old_Rows) // Rows got bigger
2934 msg_row += Rows - old_Rows; // put cursor in same place
2935 if (msg_col >= Columns) // Columns got smaller
2936 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002939 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002941#ifdef FEAT_GUI_HAIKU
2942 vim_unlock_screen();
2943#endif
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002946 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002947
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002948 /*
2949 * Do not apply autocommands more than 3 times to avoid an endless loop
2950 * in case applying autocommands always changes Rows or Columns.
2951 */
2952 if (starting == 0 && ++retry_count <= 3)
2953 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002954 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002955 // In rare cases, autocommands may have altered Rows or Columns,
2956 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002957 goto retry;
2958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959}
2960
2961 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002962free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002963{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002964 int i;
2965
Bram Moolenaar33796b32019-06-08 16:01:13 +02002966 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002967 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002968 VIM_CLEAR(ScreenLinesC[i]);
2969 VIM_CLEAR(ScreenLines2);
2970 VIM_CLEAR(ScreenLines);
2971 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002972 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002973 VIM_CLEAR(LineOffset);
2974 VIM_CLEAR(LineWraps);
2975 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002976#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002977 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002978 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002979 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002980#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002981}
2982
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002983/*
2984 * Clear the screen.
2985 * May delay if there is something the user should read.
2986 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002987 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002988 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002989 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01002990 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002991screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002994 screenalloc(FALSE); // allocate screen buffers if size changed
2995 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002996}
2997
2998/*
2999 * Do not clear the screen but mark everything for redraw.
3000 */
3001 void
3002redraw_as_cleared(void)
3003{
3004 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005}
3006
Bram Moolenaar838b7462022-09-26 15:19:56 +01003007 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003008screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01003011 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 if (starting == NO_SCREEN || ScreenLines == NULL
3014#ifdef FEAT_GUI
3015 || (gui.in_use && gui.starting)
3016#endif
3017 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01003018 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
3020#ifdef FEAT_GUI
3021 if (!gui.in_use)
3022#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003023 screen_attr = -1; // force setting the Normal colors
3024 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003027 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 clip_scroll_selection(9999);
3029#endif
3030
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003031 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 for (i = 0; i < Rows; ++i)
3033 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003034 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 LineWraps[i] = FALSE;
3036 }
3037
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003038 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003040 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01003041 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003043 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 }
3045 else
3046 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003047 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 for (i = 0; i < Rows; ++i)
3049 lineinvalid(LineOffset[i], (int)Columns);
3050 clear_cmdline = TRUE;
3051 }
3052
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003053 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003055 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003057 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003058 if (must_redraw == UPD_CLEAR) // no need to clear again
3059 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01003060 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01003062#ifdef FEAT_PROP_POPUP
3063 popup_redraw_all(); // redraw all popup windows
3064#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003065 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003067 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 msg_didany = FALSE;
3069 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01003070
3071 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072}
3073
3074/*
3075 * Clear one line in ScreenLines.
3076 */
3077 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003078lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 if (enc_utf8)
3082 (void)vim_memset(ScreenLinesUC + off, 0,
3083 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003084 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003085 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086}
3087
3088/*
3089 * Mark one line in ScreenLines invalid by setting the attributes to an
3090 * invalid value.
3091 */
3092 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003093lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
3095 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003096 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097}
3098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003100 * To be called when characters were sent to the terminal directly, outputting
3101 * test on "screen_lnum".
3102 */
3103 void
3104line_was_clobbered(int screen_lnum)
3105{
3106 lineinvalid(LineOffset[screen_lnum], (int)Columns);
3107}
3108
3109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 * Copy part of a Screenline for vertically split window "wp".
3111 */
3112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003113linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 unsigned off_to = LineOffset[to] + wp->w_wincol;
3116 unsigned off_from = LineOffset[from] + wp->w_wincol;
3117
3118 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
3119 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 if (enc_utf8)
3121 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003122 int i;
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
3125 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003126 for (i = 0; i < p_mco; ++i)
3127 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
3128 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
3130 if (enc_dbcs == DBCS_JPNU)
3131 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
3132 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
3134 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003135 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
3136 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
3139/*
3140 * Return TRUE if clearing with term string "p" would work.
3141 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02003142 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 */
3144 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003145can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
3147 return (*p != NUL && (t_colors <= 1
3148#ifdef FEAT_GUI
3149 || gui.in_use
3150#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003151#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003152 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02003153 || (!p_tgc && cterm_normal_bg_color == 0)
3154#else
3155 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003156#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003157 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003158#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003159 && !(p == T_CE && popup_visible)
3160#endif
3161 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162}
3163
3164/*
3165 * Reset cursor position. Use whenever cursor was moved because of outputting
3166 * something directly to the screen (shell commands) or a terminal control
3167 * code.
3168 */
3169 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003170screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 screen_cur_row = screen_cur_col = 9999;
3173}
3174
3175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 * Move the cursor to position "row","col" in the screen.
3177 * This tries to find the most efficient way to move, minimizing the number of
3178 * characters sent to the terminal.
3179 */
3180 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003181windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003183 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 int i;
3185 int plan;
3186 int cost;
3187 int wouldbe_col;
3188 int noinvcurs;
3189 char_u *bs;
3190 int goto_cost;
3191 int attr;
3192
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003193#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
3194#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196#define PLAN_LE 1
3197#define PLAN_CR 2
3198#define PLAN_NL 3
3199#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003200 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (ScreenLines == NULL)
3202 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (col != screen_cur_col || row != screen_cur_row)
3204 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003205 // Check for valid position.
3206 if (row < 0) // window without text lines?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 row = 0;
3208 if (row >= screen_Rows)
3209 row = screen_Rows - 1;
3210 if (col >= screen_Columns)
3211 col = screen_Columns - 1;
3212
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003213 // check if no cursor movement is allowed in highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 if (screen_attr && *T_MS == NUL)
3215 noinvcurs = HIGHL_COST;
3216 else
3217 noinvcurs = 0;
3218 goto_cost = GOTO_COST + noinvcurs;
3219
3220 /*
3221 * Plan how to do the positioning:
3222 * 1. Use CR to move it to column 0, same row.
3223 * 2. Use T_LE to move it a few columns to the left.
3224 * 3. Use NL to move a few lines down, column 0.
3225 * 4. Move a few columns to the right with T_ND or by writing chars.
3226 *
3227 * Don't do this if the cursor went beyond the last column, the cursor
3228 * position is unknown then (some terminals wrap, some don't )
3229 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003230 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 * characters to move the cursor to the right.
3232 */
3233 if (row >= screen_cur_row && screen_cur_col < Columns)
3234 {
3235 /*
3236 * If the cursor is in the same row, bigger col, we can use CR
3237 * or T_LE.
3238 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003239 bs = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 attr = screen_attr;
3241 if (row == screen_cur_row && col < screen_cur_col)
3242 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003243 // "le" is preferred over "bc", because "bc" is obsolete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (*T_LE)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003245 bs = T_LE; // "cursor left"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003247 bs = T_BC; // "backspace character (old)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (*bs)
3249 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3250 else
3251 cost = 999;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003252 if (col + 1 < cost) // using CR is less characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
3254 plan = PLAN_CR;
3255 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003256 cost = 1; // CR is just one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258 else
3259 {
3260 plan = PLAN_LE;
3261 wouldbe_col = col;
3262 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003263 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 cost += noinvcurs;
3266 attr = 0;
3267 }
3268 }
3269
3270 /*
3271 * If the cursor is above where we want to be, we can use CR LF.
3272 */
3273 else if (row > screen_cur_row)
3274 {
3275 plan = PLAN_NL;
3276 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003277 cost = (row - screen_cur_row) * 2; // CR LF
3278 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 cost += noinvcurs;
3281 attr = 0;
3282 }
3283 }
3284
3285 /*
3286 * If the cursor is in the same row, smaller col, just use write.
3287 */
3288 else
3289 {
3290 plan = PLAN_WRITE;
3291 wouldbe_col = screen_cur_col;
3292 cost = 0;
3293 }
3294
3295 /*
3296 * Check if any characters that need to be written have the
3297 * correct attributes. Also avoid UTF-8 characters.
3298 */
3299 i = col - wouldbe_col;
3300 if (i > 0)
3301 cost += i;
3302 if (cost < goto_cost && i > 0)
3303 {
3304 /*
3305 * Check if the attributes are correct without additionally
3306 * stopping highlighting.
3307 */
3308 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3309 while (i && *p++ == attr)
3310 --i;
3311 if (i != 0)
3312 {
3313 /*
3314 * Try if it works when highlighting is stopped here.
3315 */
3316 if (*--p == 0)
3317 {
3318 cost += noinvcurs;
3319 while (i && *p++ == 0)
3320 --i;
3321 }
3322 if (i != 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003323 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (enc_utf8)
3326 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003327 // Don't use an UTF-8 char for positioning, it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 for (i = wouldbe_col; i < col; ++i)
3329 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3330 {
3331 cost = 999;
3332 break;
3333 }
3334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336
3337 /*
3338 * We can do it without term_windgoto()!
3339 */
3340 if (cost < goto_cost)
3341 {
3342 if (plan == PLAN_LE)
3343 {
3344 if (noinvcurs)
3345 screen_stop_highlight();
3346 while (screen_cur_col > col)
3347 {
3348 out_str(bs);
3349 --screen_cur_col;
3350 }
3351 }
3352 else if (plan == PLAN_CR)
3353 {
3354 if (noinvcurs)
3355 screen_stop_highlight();
3356 out_char('\r');
3357 screen_cur_col = 0;
3358 }
3359 else if (plan == PLAN_NL)
3360 {
3361 if (noinvcurs)
3362 screen_stop_highlight();
3363 while (screen_cur_row < row)
3364 {
3365 out_char('\n');
3366 ++screen_cur_row;
3367 }
3368 screen_cur_col = 0;
3369 }
3370
3371 i = col - screen_cur_col;
3372 if (i > 0)
3373 {
3374 /*
3375 * Use cursor-right if it's one character only. Avoids
3376 * removing a line of pixels from the last bold char, when
3377 * using the bold trick in the GUI.
3378 */
3379 if (T_ND[0] != NUL && T_ND[1] == NUL)
3380 {
3381 while (i-- > 0)
3382 out_char(*T_ND);
3383 }
3384 else
3385 {
3386 int off;
3387
3388 off = LineOffset[row] + screen_cur_col;
3389 while (i-- > 0)
3390 {
3391 if (ScreenAttrs[off] != screen_attr)
3392 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 if (enc_dbcs == DBCS_JPNU
3396 && ScreenLines[off] == 0x8e)
3397 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 ++off;
3399 }
3400 }
3401 }
3402 }
3403 }
3404 else
3405 cost = 999;
3406
3407 if (cost >= goto_cost)
3408 {
3409 if (noinvcurs)
3410 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003411 if (row == screen_cur_row && (col > screen_cur_col)
3412 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 term_cursor_right(col - screen_cur_col);
3414 else
3415 term_windgoto(row, col);
3416 }
3417 screen_cur_row = row;
3418 screen_cur_col = col;
3419 }
3420}
3421
3422/*
3423 * Set cursor to its position in the current window.
3424 */
3425 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003426setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003428 setcursor_mayforce(FALSE);
3429}
3430
3431/*
3432 * Set cursor to its position in the current window.
3433 * When "force" is TRUE also when not redrawing.
3434 */
3435 void
3436setcursor_mayforce(int force)
3437{
3438 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 validate_cursor();
3441 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003442 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003444 // With 'rightleft' set and the cursor on a double-wide
3445 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003446 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3447 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003448 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003449 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450#endif
3451 curwin->w_wcol));
3452 }
3453}
3454
3455
3456/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003457 * Insert 'line_count' lines at 'row' in window 'wp'.
3458 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3459 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 * scrolling.
3461 * Returns FAIL if the lines are not inserted, OK for success.
3462 */
3463 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003464win_ins_lines(
3465 win_T *wp,
3466 int row,
3467 int line_count,
3468 int invalid,
3469 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
3471 int did_delete;
3472 int nextrow;
3473 int lastrow;
3474 int retval;
3475
3476 if (invalid)
3477 wp->w_lines_valid = 0;
3478
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003479 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (wp->w_height < 5)
3481 return FAIL;
3482
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003483 // with the popup menu visible this might not work correctly
3484 if (pum_visible())
3485 return FAIL;
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (line_count > wp->w_height - row)
3488 line_count = wp->w_height - row;
3489
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003490 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (retval != MAYBE)
3492 return retval;
3493
3494 /*
3495 * If there is a next window or a status line, we first try to delete the
3496 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003497 * If this fails and there are following windows, don't do anything to
3498 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 */
3500 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (wp->w_next != NULL || wp->w_status_height)
3502 {
3503 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003504 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 did_delete = TRUE;
3506 else if (wp->w_next)
3507 return FAIL;
3508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 /*
3510 * if no lines deleted, blank the lines that will end up below the window
3511 */
3512 if (!did_delete)
3513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003516 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 lastrow = nextrow + line_count;
3518 if (lastrow > Rows)
3519 lastrow = Rows;
3520 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003521 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 ' ', ' ', 0);
3523 }
3524
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003525 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 == FAIL)
3527 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003528 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (did_delete)
3530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 win_rest_invalid(W_NEXT(wp));
3533 }
3534 return FAIL;
3535 }
3536
3537 return OK;
3538}
3539
3540/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003541 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3543 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3544 * scrolling
3545 * Return OK for success, FAIL if the lines are not deleted.
3546 */
3547 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003548win_del_lines(
3549 win_T *wp,
3550 int row,
3551 int line_count,
3552 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003553 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003554 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
3556 int retval;
3557
3558 if (invalid)
3559 wp->w_lines_valid = 0;
3560
3561 if (line_count > wp->w_height - row)
3562 line_count = wp->w_height - row;
3563
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003564 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (retval != MAYBE)
3566 return retval;
3567
3568 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003569 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return FAIL;
3571
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 /*
3573 * If there are windows or status lines below, try to put them at the
3574 * correct place. If we can't do that, they have to be redrawn.
3575 */
3576 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3577 {
3578 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003579 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 wp->w_redr_status = TRUE;
3582 win_rest_invalid(wp->w_next);
3583 }
3584 }
3585 /*
3586 * If this is the last window and there is no status line, redraw the
3587 * command line later.
3588 */
3589 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 redraw_cmdline = TRUE;
3591 return OK;
3592}
3593
3594/*
3595 * Common code for win_ins_lines() and win_del_lines().
3596 * Returns OK or FAIL when the work has been done.
3597 * Returns MAYBE when not finished yet.
3598 */
3599 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003600win_do_lines(
3601 win_T *wp,
3602 int row,
3603 int line_count,
3604 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003605 int del,
3606 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
3608 int retval;
3609
3610 if (!redrawing() || line_count <= 0)
3611 return FAIL;
3612
Bram Moolenaar33796b32019-06-08 16:01:13 +02003613 // When inserting lines would result in loss of command output, just redraw
3614 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003615 if (no_win_do_lines_ins && !del)
3616 return FAIL;
3617
Bram Moolenaar33796b32019-06-08 16:01:13 +02003618 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003619 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003621 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003622 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 return FAIL;
3624 }
3625
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003626#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003627 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003628 if (popup_visible)
3629 return FAIL;
3630#endif
3631
3632 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (row + line_count >= wp->w_height)
3634 {
3635 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003636 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 ' ', ' ', 0);
3638 return OK;
3639 }
3640
3641 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003642 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003644 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003646 if (!no_win_do_lines_ins)
3647 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
3649 /*
3650 * If the terminal can set a scroll region, use that.
3651 * Always do this in a vertically split window. This will redraw from
3652 * ScreenLines[] when t_CV isn't defined. That's faster than using
3653 * win_line().
3654 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003655 * a character in the lower right corner of the scroll region may cause a
3656 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003658 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 scroll_region_set(wp, row);
3662 if (del)
3663 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003664 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 else
3666 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003667 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 scroll_region_reset();
3670 return retval;
3671 }
3672
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003673 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
3676 return MAYBE;
3677}
3678
3679/*
3680 * window 'wp' and everything after it is messed up, mark it for redraw
3681 */
3682 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003683win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003687 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 wp->w_redr_status = TRUE;
3689 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 redraw_cmdline = TRUE;
3692}
3693
3694/*
3695 * The rest of the routines in this file perform screen manipulations. The
3696 * given operation is performed physically on the screen. The corresponding
3697 * change is also made to the internal screen image. In this way, the editor
3698 * anticipates the effect of editing changes on the appearance of the screen.
3699 * That way, when we call screenupdate a complete redraw isn't usually
3700 * necessary. Another advantage is that we can keep adding code to anticipate
3701 * screen changes, and in the meantime, everything still works.
3702 */
3703
3704/*
3705 * types for inserting or deleting lines
3706 */
3707#define USE_T_CAL 1
3708#define USE_T_CDL 2
3709#define USE_T_AL 3
3710#define USE_T_CE 4
3711#define USE_T_DL 5
3712#define USE_T_SR 6
3713#define USE_NL 7
3714#define USE_T_CD 8
3715#define USE_REDRAW 9
3716
3717/*
3718 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003719 * "end" is the line after the scrolled part. Normally it is Rows.
3720 * When scrolling region used "off" is the offset from the top for the region.
3721 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 *
3723 * return FAIL for failure, OK for success.
3724 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003725 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003726screen_ins_lines(
3727 int off,
3728 int row,
3729 int line_count,
3730 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003731 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003732 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733{
3734 int i;
3735 int j;
3736 unsigned temp;
3737 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003738 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 int type;
3740 int result_empty;
3741 int can_ce = can_clear(T_CE);
3742
3743 /*
3744 * FAIL if
3745 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 * - the line count is less than one
3747 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003748 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003749 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003750 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003752 if (!screen_valid(TRUE)
3753 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003754 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003755#ifdef FEAT_CLIPBOARD
3756 || (clip_star.state != SELECT_CLEARED
3757 && redrawing_for_callback > 0)
3758#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003759#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003760 || popup_visible
3761#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003762 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 return FAIL;
3764
3765 /*
3766 * There are seven ways to insert lines:
3767 * 0. When in a vertically split window and t_CV isn't set, redraw the
3768 * characters from ScreenLines[].
3769 * 1. Use T_CD (clear to end of display) if it exists and the result of
3770 * the insert is just empty lines
3771 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3772 * present or line_count > 1. It looks better if we do all the inserts
3773 * at once.
3774 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3775 * insert is just empty lines and T_CE is not present or line_count >
3776 * 1.
3777 * 4. Use T_AL (insert line) if it exists.
3778 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3779 * just empty lines.
3780 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3781 * just empty lines.
3782 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3783 * the 'da' flag is not set or we have clear line capability.
3784 * 8. redraw the characters from ScreenLines[].
3785 *
3786 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3787 * the scrollbar for the window. It does have insert line, use that if it
3788 * exists.
3789 */
3790 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003792 {
3793 // Avoid that lines are first cleared here and then redrawn, which
3794 // results in many characters updated twice. This happens with CTRL-F
3795 // in a vertically split window. With line-by-line scrolling
3796 // USE_REDRAW should be faster.
3797 if (line_count > 3)
3798 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003800 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003801 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 type = USE_T_CD;
3803 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3804 type = USE_T_CAL;
3805 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3806 type = USE_T_CDL;
3807 else if (*T_AL != NUL)
3808 type = USE_T_AL;
3809 else if (can_ce && result_empty)
3810 type = USE_T_CE;
3811 else if (*T_DL != NUL && result_empty)
3812 type = USE_T_DL;
3813 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3814 type = USE_T_SR;
3815 else
3816 return FAIL;
3817
3818 /*
3819 * For clearing the lines screen_del_lines() is used. This will also take
3820 * care of t_db if necessary.
3821 */
3822 if (type == USE_T_CD || type == USE_T_CDL ||
3823 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003824 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 /*
3827 * If text is retained below the screen, first clear or delete as many
3828 * lines at the bottom of the window as are about to be inserted so that
3829 * the deleted lines won't later surface during a screen_del_lines.
3830 */
3831 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003832 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003835 // Remove a modeless selection when inserting lines halfway the screen
3836 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003837 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003838 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 else
3840 clip_scroll_selection(-line_count);
3841#endif
3842
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003843#ifdef FEAT_GUI_HAIKU
3844 vim_lock_screen();
3845#endif
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003848 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3849 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003850 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851#endif
3852
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003853 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3854 cursor_col = wp->w_wincol;
3855
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003856 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 cursor_row = row;
3858 else
3859 cursor_row = row + off;
3860
3861 /*
3862 * Shift LineOffset[] line_count down to reflect the inserted lines.
3863 * Clear the inserted lines in ScreenLines[].
3864 */
3865 row += off;
3866 end += off;
3867 for (i = 0; i < line_count; ++i)
3868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (wp != NULL && wp->w_width != Columns)
3870 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003871 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 j = end - 1 - i;
3873 while ((j -= line_count) >= row)
3874 linecopy(j + line_count, j, wp);
3875 j += line_count;
3876 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003877 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3878 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 else
3880 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3881 LineWraps[j] = FALSE;
3882 }
3883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
3885 j = end - 1 - i;
3886 temp = LineOffset[j];
3887 while ((j -= line_count) >= row)
3888 {
3889 LineOffset[j + line_count] = LineOffset[j];
3890 LineWraps[j + line_count] = LineWraps[j];
3891 }
3892 LineOffset[j + line_count] = temp;
3893 LineWraps[j + line_count] = FALSE;
3894 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003895 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 else
3897 lineinvalid(temp, (int)Columns);
3898 }
3899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003901#ifdef FEAT_GUI_HAIKU
3902 vim_unlock_screen();
3903#endif
3904
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003906 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003907 if (clear_attr != 0)
3908 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003910 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (type == USE_REDRAW)
3912 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003913 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 {
3915 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003916 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918 else
3919 {
3920 for (i = 0; i < line_count; i++)
3921 {
3922 if (type == USE_T_AL)
3923 {
3924 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003925 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 out_str(T_AL);
3927 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003928 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003930 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932 }
3933
3934 /*
3935 * With scroll-reverse and 'da' flag set we need to clear the lines that
3936 * have been scrolled down into the region.
3937 */
3938 if (type == USE_T_SR && *T_DA)
3939 {
3940 for (i = 0; i < line_count; ++i)
3941 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003942 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003944 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 }
3947
3948#ifdef FEAT_GUI
3949 gui_can_update_cursor();
3950 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003951 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952#endif
3953 return OK;
3954}
3955
3956/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003957 * Delete lines on the screen and update ScreenLines[].
3958 * "end" is the line after the scrolled part. Normally it is Rows.
3959 * When scrolling region used "off" is the offset from the top for the region.
3960 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 *
3962 * Return OK for success, FAIL if the lines are not deleted.
3963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003965screen_del_lines(
3966 int off,
3967 int row,
3968 int line_count,
3969 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003970 int force, // even when line_count > p_ttyscroll
3971 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003972 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
3974 int j;
3975 int i;
3976 unsigned temp;
3977 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003978 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003980 int result_empty; // result is empty until end of region
3981 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 int type;
3983
3984 /*
3985 * FAIL if
3986 * - there is no valid screen
3987 * - the screen has to be redrawn completely
3988 * - the line count is less than one
3989 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003990 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003991 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003993 if (!screen_valid(TRUE)
3994 || line_count <= 0
3995 || (!force && line_count > p_ttyscroll)
3996 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003997#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003998 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003999#endif
4000 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 return FAIL;
4002
4003 /*
4004 * Check if the rest of the current region will become empty.
4005 */
4006 result_empty = row + line_count >= end;
4007
4008 /*
4009 * We can delete lines only when 'db' flag not set or when 'ce' option
4010 * available.
4011 */
4012 can_delete = (*T_DB == NUL || can_clear(T_CE));
4013
4014 /*
4015 * There are six ways to delete lines:
4016 * 0. When in a vertically split window and t_CV isn't set, redraw the
4017 * characters from ScreenLines[].
4018 * 1. Use T_CD if it exists and the result is empty.
4019 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
4020 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
4021 * none of the other ways work.
4022 * 4. Use T_CE (erase line) if the result is empty.
4023 * 5. Use T_DL (delete line) if it exists.
4024 * 6. redraw the characters from ScreenLines[].
4025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01004027 {
4028 // Avoid that lines are first cleared here and then redrawn, which
4029 // results in many characters updated twice. This happens with CTRL-F
4030 // in a vertically split window. With line-by-line scrolling
4031 // USE_REDRAW should be faster.
4032 if (line_count > 3)
4033 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01004035 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004036 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 else if (row == 0 && (
4039#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004040 // On the Amiga, somehow '\n' on the last line doesn't always scroll
4041 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 line_count == 1 ||
4043#endif
4044 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 type = USE_NL;
4046 else if (*T_CDL != NUL && line_count > 1 && can_delete)
4047 type = USE_T_CDL;
4048 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02004049 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 type = USE_T_CE;
4051 else if (*T_DL != NUL && can_delete)
4052 type = USE_T_DL;
4053 else if (*T_CDL != NUL && can_delete)
4054 type = USE_T_CDL;
4055 else
4056 return FAIL;
4057
4058#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004059 // Remove a modeless selection when deleting lines halfway the screen or
4060 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02004061 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02004062 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 else
4064 clip_scroll_selection(line_count);
4065#endif
4066
Bram Moolenaar92c461e2020-04-24 22:19:00 +02004067#ifdef FEAT_GUI_HAIKU
4068 vim_lock_screen();
4069#endif
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004072 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
4073 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02004074 gui_dont_update_cursor(gui.cursor_row >= row + off
4075 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076#endif
4077
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004078 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
4079 cursor_col = wp->w_wincol;
4080
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004081 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
4083 cursor_row = row;
4084 cursor_end = end;
4085 }
4086 else
4087 {
4088 cursor_row = row + off;
4089 cursor_end = end + off;
4090 }
4091
4092 /*
4093 * Now shift LineOffset[] line_count up to reflect the deleted lines.
4094 * Clear the inserted lines in ScreenLines[].
4095 */
4096 row += off;
4097 end += off;
4098 for (i = 0; i < line_count; ++i)
4099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (wp != NULL && wp->w_width != Columns)
4101 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004102 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 j = row + i;
4104 while ((j += line_count) <= end - 1)
4105 linecopy(j - line_count, j, wp);
4106 j -= line_count;
4107 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004108 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
4109 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 else
4111 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
4112 LineWraps[j] = FALSE;
4113 }
4114 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004116 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 j = row + i;
4118 temp = LineOffset[j];
4119 while ((j += line_count) <= end - 1)
4120 {
4121 LineOffset[j - line_count] = LineOffset[j];
4122 LineWraps[j - line_count] = LineWraps[j];
4123 }
4124 LineOffset[j - line_count] = temp;
4125 LineWraps[j - line_count] = FALSE;
4126 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004127 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 else
4129 lineinvalid(temp, (int)Columns);
4130 }
4131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004133#ifdef FEAT_GUI_HAIKU
4134 vim_unlock_screen();
4135#endif
4136
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004137 if (screen_attr != clear_attr)
4138 screen_stop_highlight();
4139 if (clear_attr != 0)
4140 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004142 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (type == USE_REDRAW)
4144 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004145 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004147 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004149 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 }
4151 else if (type == USE_T_CDL)
4152 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004153 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004155 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157 /*
4158 * Deleting lines at top of the screen or scroll region: Just scroll
4159 * the whole screen (scroll region) up by outputting newlines on the
4160 * last line.
4161 */
4162 else if (type == USE_NL)
4163 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004164 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004166 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 else
4169 {
4170 for (i = line_count; --i >= 0; )
4171 {
4172 if (type == USE_T_DL)
4173 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004174 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004175 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004177 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004179 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004180 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004182 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184 }
4185
4186 /*
4187 * If the 'db' flag is set, we need to clear the lines that have been
4188 * scrolled up at the bottom of the region.
4189 */
4190 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
4191 {
4192 for (i = line_count; i > 0; --i)
4193 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004194 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004195 out_str(T_CE); // erase a line
4196 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 }
4199
4200#ifdef FEAT_GUI
4201 gui_can_update_cursor();
4202 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004203 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204#endif
4205
4206 return OK;
4207}
4208
4209/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004210 * Return TRUE when postponing displaying the mode message: when not redrawing
4211 * or inside a mapping.
4212 */
4213 int
4214skip_showmode()
4215{
4216 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004217 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004218 if (global_busy
4219 || msg_silent != 0
4220 || !redrawing()
4221 || (char_avail() && !KeyTyped))
4222 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004223 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004224 return TRUE;
4225 }
4226 return FALSE;
4227}
4228
4229/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004230 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 *
4232 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4233 * If clear_cmdline is FALSE there may be a message there that needs to be
4234 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004235 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 * Return the length of the message (0 if no message).
4237 */
4238 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004239showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240{
4241 int need_clear;
4242 int length = 0;
4243 int do_mode;
4244 int attr;
4245 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
Bram Moolenaara2a89732022-08-31 14:46:18 +01004248 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004249 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004250 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004251 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004252 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004254 if (skip_showmode())
4255 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 nwr_save = need_wait_return;
4258
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004259 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 check_for_delay(FALSE);
4261
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004262 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 need_clear = clear_cmdline;
4264 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004265 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004267 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 msg_pos_mode();
4269 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004270 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (do_mode)
4272 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004273 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004275 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004276# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004277 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004278# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004279 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004280# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004281 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004282# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004283 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004285 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286# endif
4287#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004288 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004289 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004291 // These messages can get long, avoid a wrap in a narrow
4292 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 length = (Rows - msg_row) * Columns - 3;
4294 if (edit_submode_extra != NULL)
4295 length -= vim_strsize(edit_submode_extra);
4296 if (length > 0)
4297 {
4298 if (edit_submode_pre != NULL)
4299 length -= vim_strsize(edit_submode_pre);
4300 if (length - vim_strsize(edit_submode) > 0)
4301 {
4302 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004303 msg_puts_attr((char *)edit_submode_pre, attr);
4304 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 if (edit_submode_extra != NULL)
4307 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004308 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004310 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 else
4312 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004313 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004320 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004321 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004322 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004323 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
4325#ifdef FEAT_RIGHTLEFT
4326 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004327 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004329 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004331 else if (restart_edit == 'I' || restart_edit == 'i' ||
4332 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004333 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004335 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004337 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338#ifdef FEAT_RIGHTLEFT
4339 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004340 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#endif
4342#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004343 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
4345# ifdef FEAT_ARABIC
4346 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004347 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 else
4349# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004350 if (get_keymap_str(curwin, (char_u *)" (%s)",
4351 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004352 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004355 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004356 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (VIsual_active)
4359 {
4360 char *p;
4361
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004362 // Don't concatenate separate words to avoid translation
4363 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 switch ((VIsual_select ? 4 : 0)
4365 + (VIsual_mode == Ctrl_V) * 2
4366 + (VIsual_mode == 'V'))
4367 {
4368 case 0: p = N_(" VISUAL"); break;
4369 case 1: p = N_(" VISUAL LINE"); break;
4370 case 2: p = N_(" VISUAL BLOCK"); break;
4371 case 4: p = N_(" SELECT"); break;
4372 case 5: p = N_(" SELECT LINE"); break;
4373 default: p = N_(" SELECT BLOCK"); break;
4374 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004375 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004377 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004379
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 need_clear = TRUE;
4381 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004382 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004383 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004385 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 need_clear = TRUE;
4387 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004388
4389 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004390 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004392 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 length = msg_col;
4394 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004395 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004398 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004400 else if (redraw_mode)
4401 {
4402 msg_pos_mode();
4403 msg_clr_eos();
4404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406#ifdef FEAT_CMDL_INFO
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004407 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (VIsual_active)
4409 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004411 // If the last window has no status line, the ruler is after the mode
4412 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004413 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004414 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415#endif
4416 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004417 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 clear_cmdline = FALSE;
4419
4420 return length;
4421}
4422
4423/*
4424 * Position for a mode message.
4425 */
4426 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004427msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
4429 msg_col = 0;
4430 msg_row = Rows - 1;
4431}
4432
4433/*
4434 * Delete mode message. Used when ESC is typed which is expected to end
4435 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004436 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
4438 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004439unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
4441 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004442 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 */
4444 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004445 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004447 clearmode();
4448}
4449
4450/*
4451 * Clear the mode message.
4452 */
4453 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004454clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004455{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004456 int save_msg_row = msg_row;
4457 int save_msg_col = msg_col;
4458
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004459 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004460 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004461 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004462 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004463
4464 msg_col = save_msg_col;
4465 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466}
4467
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004468 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004469recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004470{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004471 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004472 if (!shortmess(SHM_RECORDING))
4473 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004474 char s[4];
4475
4476 sprintf(s, " @%c", reg_recording);
4477 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004478 }
4479}
4480
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004481/*
4482 * Draw the tab pages line at the top of the Vim window.
4483 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004484 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004485draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004486{
4487 int tabcount = 0;
4488 tabpage_T *tp;
4489 int tabwidth;
4490 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004491 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004492 int attr;
4493 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004494 win_T *cwp;
4495 int wincount;
4496 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004497 int c;
4498 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004499 int attr_sel = HL_ATTR(HLF_TPS);
4500 int attr_nosel = HL_ATTR(HLF_TP);
4501 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004502 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004503 int room;
4504 int use_sep_chars = (t_colors < 8
4505#ifdef FEAT_GUI
4506 && !gui.in_use
4507#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004508#ifdef FEAT_TERMGUICOLORS
4509 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004510#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004511 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004512
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004513 if (ScreenLines == NULL)
4514 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004515 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004516
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004517#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004518 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004519 if (gui_use_tabline())
4520 {
4521 gui_update_tabline();
4522 return;
4523 }
4524#endif
4525
4526 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004527 return;
4528
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004529#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004530 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004531
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004532 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004533 if (*p_tal != NUL)
4534 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004535 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004536
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004537 // Check for an error. If there is one we would loop in redrawing the
4538 // screen. Avoid that by making 'tabline' empty.
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004539 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004540 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004541 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004542 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004543 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004544 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004545 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00004546 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004547#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004548 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004549 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004550 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004551
Bram Moolenaar238a5642006-02-21 22:12:05 +00004552 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4553 if (tabwidth < 6)
4554 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004555
Bram Moolenaar238a5642006-02-21 22:12:05 +00004556 attr = attr_nosel;
4557 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004558 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4559 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004560 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004561 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004562
Bram Moolenaar238a5642006-02-21 22:12:05 +00004563 if (tp->tp_topframe == topframe)
4564 attr = attr_sel;
4565 if (use_sep_chars && col > 0)
4566 screen_putchar('|', 0, col++, attr);
4567
4568 if (tp->tp_topframe != topframe)
4569 attr = attr_nosel;
4570
4571 screen_putchar(' ', 0, col++, attr);
4572
4573 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004574 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004575 cwp = curwin;
4576 wp = firstwin;
4577 }
4578 else
4579 {
4580 cwp = tp->tp_curwin;
4581 wp = tp->tp_firstwin;
4582 }
4583
4584 modified = FALSE;
4585 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4586 if (bufIsChanged(wp->w_buffer))
4587 modified = TRUE;
4588 if (modified || wincount > 1)
4589 {
4590 if (wincount > 1)
4591 {
4592 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004593 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004594 if (col + len >= Columns - 3)
4595 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004596 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004597#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004598 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004599#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004600 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004601#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004602 );
4603 col += len;
4604 }
4605 if (modified)
4606 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4607 screen_putchar(' ', 0, col++, attr);
4608 }
4609
4610 room = scol - col + tabwidth - 1;
4611 if (room > 0)
4612 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004613 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004614 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004615 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004616 len = vim_strsize(NameBuff);
4617 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004618 if (has_mbyte)
4619 while (len > room)
4620 {
4621 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004622 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004623 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004624 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004625 {
4626 p += len - room;
4627 len = room;
4628 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004629 if (len > Columns - col - 1)
4630 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004632 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004633 col += len;
4634 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004635 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004636
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004637 // Store the tab page number in TabPageIdxs[], so that
4638 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004639 ++tabcount;
4640 while (scol < col)
4641 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004642 }
4643
Bram Moolenaar238a5642006-02-21 22:12:05 +00004644 if (use_sep_chars)
4645 c = '_';
4646 else
4647 c = ' ';
4648 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004649
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004650 // Put an "X" for closing the current tab if there are several.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004651 if (first_tabpage->tp_next != NULL)
4652 {
4653 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4654 TabPageIdxs[Columns - 1] = -999;
4655 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004656 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004657
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004658 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4659 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004660 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004661}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004662
4663/*
4664 * Get buffer name for "buf" into NameBuff[].
4665 * Takes care of special buffer names and translates special characters.
4666 */
4667 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004668get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004669{
4670 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004671 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004672 else
4673 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4674 trans_characters(NameBuff, MAXPATHL);
4675}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004676
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677/*
4678 * Get the character to use in a status line. Get its attributes in "*attr".
4679 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004680 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004681fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682{
4683 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004684
4685#ifdef FEAT_TERMINAL
4686 if (bt_terminal(wp->w_buffer))
4687 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004688 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004689 {
4690 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004691 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004692 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004693 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004694 {
4695 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004696 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004697 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004698 }
4699 else
4700#endif
4701 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004703 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004704 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 }
4706 else
4707 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004708 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004709 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004711 // Use fill when there is highlighting, and highlighting of current
4712 // window differs, or the fillchars differ, or this is not the
4713 // current window
Bram Moolenaar8820b482017-03-16 17:23:31 +01004714 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004715 || wp != curwin || ONE_WINDOW)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004716 || (wp->w_fill_chars.stl != wp->w_fill_chars.stlnc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004718 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 return '^';
4720 return '=';
4721}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723/*
4724 * Get the character to use in a separator between vertically split windows.
4725 * Get its attributes in "*attr".
4726 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004727 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004728fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004730 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004731 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return '|';
4733 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004734 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737/*
4738 * Return TRUE if redrawing should currently be done.
4739 */
4740 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004741redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004743#ifdef FEAT_EVAL
4744 if (disable_redraw_for_testing)
4745 return 0;
4746 else
4747#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004748 return ((!RedrawingDisabled
4749#ifdef FEAT_EVAL
4750 || ignore_redraw_flag_for_testing
4751#endif
4752 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753}
4754
4755/*
4756 * Return TRUE if printing messages should currently be done.
4757 */
4758 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004759messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004761 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762}
4763
Bram Moolenaare677df82019-09-02 22:31:11 +02004764/*
4765 * Compute columns for ruler and shown command. 'sc_col' is also used to
4766 * decide what the maximum length of a message on the status line can be.
4767 * If there is a status line for the last window, 'sc_col' is independent
4768 * of 'ru_col'.
4769 */
4770
4771#define COL_RULER 17 // columns needed by standard ruler
4772
4773 void
4774comp_col(void)
4775{
4776#if defined(FEAT_CMDL_INFO)
4777 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4778
4779 sc_col = 0;
4780 ru_col = 0;
4781 if (p_ru)
4782 {
4783# ifdef FEAT_STL_OPT
4784 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
4785# else
4786 ru_col = COL_RULER + 1;
4787# endif
4788 // no last status line, adjust sc_col
4789 if (!last_has_status)
4790 sc_col = ru_col;
4791 }
4792 if (p_sc)
4793 {
4794 sc_col += SHOWCMD_COLS;
4795 if (!p_ru || last_has_status) // no need for separating space
4796 ++sc_col;
4797 }
4798 sc_col = Columns - sc_col;
4799 ru_col = Columns - ru_col;
4800 if (sc_col <= 0) // screen too narrow, will become a mess
4801 sc_col = 1;
4802 if (ru_col <= 0)
4803 ru_col = 1;
4804#else
4805 sc_col = Columns;
4806 ru_col = Columns;
4807#endif
4808#ifdef FEAT_EVAL
4809 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4810#endif
4811}
4812
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004813#if defined(FEAT_LINEBREAK) || defined(PROTO)
4814/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004815 * Return the width of the 'number' and 'relativenumber' column.
4816 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004817 * Otherwise it depends on 'numberwidth' and the line count.
4818 */
4819 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004820number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004821{
4822 int n;
4823 linenr_T lnum;
4824
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004825 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004826 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004827 lnum = wp->w_height;
4828 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004829 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004830 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004831
Bram Moolenaar6b314672015-03-20 15:42:10 +01004832 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004833 return wp->w_nrwidth_width;
4834 wp->w_nrwidth_line_count = lnum;
4835
4836 n = 0;
4837 do
4838 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004839 lnum /= 10;
4840 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004841 } while (lnum > 0);
4842
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004843 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004844 if (n < wp->w_p_nuw - 1)
4845 n = wp->w_p_nuw - 1;
4846
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004847# ifdef FEAT_SIGNS
4848 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4849 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004850 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004851 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4852 n = 2;
4853# endif
4854
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004855 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004856 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004857 return n;
4858}
4859#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004860
Bram Moolenaar113e1072019-01-20 15:30:40 +01004861#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004862/*
4863 * Return the current cursor column. This is the actual position on the
4864 * screen. First column is 0.
4865 */
4866 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004867screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004868{
4869 return screen_cur_col;
4870}
4871
4872/*
4873 * Return the current cursor row. This is the actual position on the screen.
4874 * First row is 0.
4875 */
4876 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004877screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004878{
4879 return screen_cur_row;
4880}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004881#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004882
4883/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004884 * Calls mb_ptr2char_adv(p) and returns the character.
4885 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4886 */
4887 static int
4888get_encoded_char_adv(char_u **p)
4889{
4890 char_u *s = *p;
4891
4892 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4893 {
4894 varnumber_T num = 0;
4895 int bytes;
4896 int n;
4897
4898 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4899 {
4900 *p += 2;
4901 n = hexhex2nr(*p);
4902 if (n < 0)
4903 return 0;
4904 num = num * 256 + n;
4905 }
4906 *p += 2;
4907 return num;
4908 }
4909 return mb_ptr2char_adv(p);
4910}
4911
4912/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004913 * Handle setting 'listchars' or 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004914 * "varp" points to either the global or the window-local value.
4915 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004916 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004917 * Returns error message, NULL if it's OK.
4918 */
4919 char *
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004920set_chars_option(win_T *wp, char_u **varp, int apply)
Bram Moolenaare677df82019-09-02 22:31:11 +02004921{
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004922 int round, i, len, len2, entries;
4923 char_u *p, *s;
4924 int c1 = 0, c2 = 0, c3 = 0;
4925 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4926 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4927 int multispace_len = 0; // Length of lcs-multispace string
4928 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004929 int is_listchars = (varp == &p_lcs || varp == &wp->w_p_lcs);
4930 char_u *value = *varp;
4931
Bram Moolenaare677df82019-09-02 22:31:11 +02004932 struct charstab
4933 {
4934 int *cp;
4935 char *name;
4936 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004937 struct charstab *tab;
4938
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004939 static fill_chars_T fill_chars;
Bram Moolenaare677df82019-09-02 22:31:11 +02004940 static struct charstab filltab[] =
4941 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004942 {&fill_chars.stl, "stl"},
4943 {&fill_chars.stlnc, "stlnc"},
4944 {&fill_chars.vert, "vert"},
4945 {&fill_chars.fold, "fold"},
4946 {&fill_chars.foldopen, "foldopen"},
4947 {&fill_chars.foldclosed, "foldclose"},
4948 {&fill_chars.foldsep, "foldsep"},
4949 {&fill_chars.diff, "diff"},
4950 {&fill_chars.eob, "eob"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004951 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004952
Bram Moolenaar333bd562021-02-16 22:22:13 +01004953 static lcs_chars_T lcs_chars;
Bram Moolenaareed9d462021-02-15 20:38:25 +01004954 struct charstab lcstab[] =
Bram Moolenaare677df82019-09-02 22:31:11 +02004955 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004956 {&lcs_chars.eol, "eol"},
4957 {&lcs_chars.ext, "extends"},
4958 {&lcs_chars.nbsp, "nbsp"},
4959 {&lcs_chars.prec, "precedes"},
4960 {&lcs_chars.space, "space"},
4961 {&lcs_chars.tab2, "tab"},
4962 {&lcs_chars.trail, "trail"},
4963 {&lcs_chars.lead, "lead"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004964#ifdef FEAT_CONCEAL
Bram Moolenaar333bd562021-02-16 22:22:13 +01004965 {&lcs_chars.conceal, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004966#else
Bram Moolenaar333bd562021-02-16 22:22:13 +01004967 {NULL, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004968#endif
4969 };
Bram Moolenaare677df82019-09-02 22:31:11 +02004970
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004971 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004972 {
4973 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004974 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004975 entries = ARRAY_LENGTH(lcstab);
Bram Moolenaareed9d462021-02-15 20:38:25 +01004976 if (varp == &wp->w_p_lcs && wp->w_p_lcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004977 value = p_lcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004978 }
4979 else
4980 {
4981 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004982 entries = ARRAY_LENGTH(filltab);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004983 if (varp == &wp->w_p_fcs && wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004984 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004985 }
4986
4987 // first round: check for valid value, second round: assign values
4988 for (round = 0; round <= 1; ++round)
4989 {
4990 if (round > 0)
4991 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004992 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004993 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004994 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004995 for (i = 0; i < entries; ++i)
4996 if (tab[i].cp != NULL)
4997 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004998 lcs_chars.tab1 = NUL;
4999 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01005000
Mike Williamsf5785cf2021-09-13 22:17:38 +02005001 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005002 {
5003 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005004 if (lcs_chars.multispace != NULL)
5005 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005006 }
5007 else
5008 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005009
5010 if (lead_multispace_len > 0)
5011 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005012 lcs_chars.leadmultispace =
5013 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005014 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
5015 }
5016 else
5017 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02005018 }
5019 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01005020 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005021 fill_chars.stl = ' ';
5022 fill_chars.stlnc = ' ';
5023 fill_chars.vert = ' ';
5024 fill_chars.fold = '-';
5025 fill_chars.foldopen = '-';
5026 fill_chars.foldclosed = '+';
5027 fill_chars.foldsep = '|';
5028 fill_chars.diff = '-';
5029 fill_chars.eob = '~';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01005030 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005031 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005032 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02005033 while (*p)
5034 {
5035 for (i = 0; i < entries; ++i)
5036 {
5037 len = (int)STRLEN(tab[i].name);
5038 if (STRNCMP(p, tab[i].name, len) == 0
5039 && p[len] == ':'
5040 && p[len + 1] != NUL)
5041 {
5042 c2 = c3 = 0;
5043 s = p + len + 1;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005044 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005045 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005046 return e_invalid_argument;
Bram Moolenaar333bd562021-02-16 22:22:13 +01005047 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005048 {
5049 if (*s == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005050 return e_invalid_argument;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005051 c2 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005052 if (char2cells(c2) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005053 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005054 if (!(*s == ',' || *s == NUL))
5055 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005056 c3 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005057 if (char2cells(c3) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005058 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005059 }
5060 }
5061
5062 if (*s == ',' || *s == NUL)
5063 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005064 if (round > 0)
Bram Moolenaare677df82019-09-02 22:31:11 +02005065 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005066 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005067 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005068 lcs_chars.tab1 = c1;
5069 lcs_chars.tab2 = c2;
5070 lcs_chars.tab3 = c3;
Bram Moolenaare677df82019-09-02 22:31:11 +02005071 }
5072 else if (tab[i].cp != NULL)
5073 *(tab[i].cp) = c1;
5074
5075 }
5076 p = s;
5077 break;
5078 }
5079 }
5080 }
5081
5082 if (i == entries)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005083 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005084 len = (int)STRLEN("multispace");
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005085 len2 = (int)STRLEN("leadmultispace");
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005086 if (is_listchars
zeertzjqf14b8ba2021-09-10 16:58:30 +02005087 && STRNCMP(p, "multispace", len) == 0
5088 && p[len] == ':'
5089 && p[len + 1] != NUL)
5090 {
5091 s = p + len + 1;
5092 if (round == 0)
5093 {
5094 // Get length of lcs-multispace string in first round
5095 last_multispace = p;
5096 multispace_len = 0;
5097 while (*s != NUL && *s != ',')
5098 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005099 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005100 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005101 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005102 ++multispace_len;
5103 }
5104 if (multispace_len == 0)
5105 // lcs-multispace cannot be an empty string
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005106 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005107 p = s;
5108 }
5109 else
5110 {
5111 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02005112
zeertzjqf14b8ba2021-09-10 16:58:30 +02005113 while (*s != NUL && *s != ',')
5114 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005115 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02005116 if (p == last_multispace)
5117 lcs_chars.multispace[multispace_pos++] = c1;
5118 }
5119 p = s;
5120 }
5121 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005122
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005123 else if (is_listchars
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005124 && STRNCMP(p, "leadmultispace", len2) == 0
5125 && p[len2] == ':'
5126 && p[len2 + 1] != NUL)
5127 {
5128 s = p + len2 + 1;
5129 if (round == 0)
5130 {
zeertzjqb5f08012022-06-09 13:55:28 +01005131 // get length of lcs-leadmultispace string in first
5132 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005133 last_lmultispace = p;
5134 lead_multispace_len = 0;
5135 while (*s != NUL && *s != ',')
5136 {
5137 c1 = get_encoded_char_adv(&s);
5138 if (char2cells(c1) > 1)
5139 return e_invalid_argument;
5140 ++lead_multispace_len;
5141 }
5142 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01005143 // lcs-leadmultispace cannot be an empty string
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005144 return e_invalid_argument;
5145 p = s;
5146 }
5147 else
5148 {
5149 int multispace_pos = 0;
5150
5151 while (*s != NUL && *s != ',')
5152 {
5153 c1 = get_encoded_char_adv(&s);
5154 if (p == last_lmultispace)
5155 lcs_chars.leadmultispace[multispace_pos++] = c1;
5156 }
5157 p = s;
5158 }
5159 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005160 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005161 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005162 }
5163
Bram Moolenaare677df82019-09-02 22:31:11 +02005164 if (*p == ',')
5165 ++p;
5166 }
5167 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005168
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005169 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005170 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005171 if (is_listchars)
5172 {
5173 vim_free(wp->w_lcs_chars.multispace);
5174 vim_free(wp->w_lcs_chars.leadmultispace);
5175 wp->w_lcs_chars = lcs_chars;
5176 }
5177 else
5178 {
5179 wp->w_fill_chars = fill_chars;
5180 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005181 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005182 else if (is_listchars)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005183 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005184 vim_free(lcs_chars.multispace);
5185 vim_free(lcs_chars.leadmultispace);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005186 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005187
5188 return NULL; // no error
5189}
zeertzjq8ca29b62022-08-09 12:53:14 +01005190
5191/*
5192 * Check all global and local values of 'listchars' and 'fillchars'.
5193 * Return an untranslated error messages if any of them is invalid, NULL
5194 * otherwise.
5195 */
5196 char *
5197check_chars_options(void)
5198{
5199 tabpage_T *tp;
5200 win_T *wp;
5201
5202 if (set_chars_option(curwin, &p_lcs, FALSE) != NULL)
5203 return e_conflicts_with_value_of_listchars;
5204 if (set_chars_option(curwin, &p_fcs, FALSE) != NULL)
5205 return e_conflicts_with_value_of_fillchars;
5206 FOR_ALL_TAB_WINDOWS(tp, wp)
5207 {
5208 if (set_chars_option(wp, &wp->w_p_lcs, FALSE) != NULL)
5209 return e_conflicts_with_value_of_listchars;
5210 if (set_chars_option(wp, &wp->w_p_fcs, FALSE) != NULL)
5211 return e_conflicts_with_value_of_fillchars;
5212 }
5213 return NULL;
5214}