blob: f262cf4c0272e78f11e45536ba5b63ff7e39351f [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.
zeertzjq99941602023-08-19 13:08:50 +020020 * ScreenCols[off] Contains the virtual columns in the line. -1 means not
zeertzjqdeb22042024-03-17 19:44:30 +010021 * available or before buffer text.
Bram Moolenaarb9081882022-07-09 04:56:24 +010022 *
23 * LineOffset[row] Contains the offset into ScreenLines*[], ScreenAttrs[]
24 * and ScreenCols[] for each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025 * LineWraps[row] Flag for each line whether it wraps to the next line.
26 *
27 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
28 * one character which occupies two display cells.
29 * For UTF-8 a multi-byte character is converted to Unicode and stored in
30 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010031 * character without composing chars ScreenLinesUC[] will be 0 and
32 * ScreenLinesC[][] is not used. When the character occupies two display
33 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000034 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010035 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000036 * ScreenLines2[] is only used for euc-jp to store the second byte if the
37 * first byte is 0x8e (single-width character).
38 *
39 * The screen_*() functions write to the screen and handle updating
40 * ScreenLines[].
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 */
42
43#include "vim.h"
44
45/*
46 * The attributes that are actually active for writing to the screen.
47 */
48static int screen_attr = 0;
49
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar838b7462022-09-26 15:19:56 +010051static int screenclear2(int doclear);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020052static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010053static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020054static 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 +010055static void win_rest_invalid(win_T *wp);
56static void msg_pos_mode(void);
57static void recording_mode(int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaar63d9e732019-12-05 21:10:38 +010059// Ugly global: overrule attribute used by screen_char()
Bram Moolenaar071d4272004-06-13 20:20:40 +000060static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaar860cae12010-06-05 23:22:07 +020062#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020063/*
64 * Return TRUE if the cursor line in window "wp" may be concealed, according
65 * to the 'concealcursor' option.
66 */
67 int
Bram Moolenaar05540972016-01-30 20:31:25 +010068conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020069{
70 int c;
71
72 if (*wp->w_p_cocu == NUL)
73 return FALSE;
Bram Moolenaar24959102022-05-07 20:01:16 +010074 if (get_real_state() & MODE_VISUAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020075 c = 'v';
Bram Moolenaar24959102022-05-07 20:01:16 +010076 else if (State & MODE_INSERT)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020077 c = 'i';
Bram Moolenaar24959102022-05-07 20:01:16 +010078 else if (State & MODE_NORMAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020079 c = 'n';
Bram Moolenaar24959102022-05-07 20:01:16 +010080 else if (State & MODE_CMDLINE)
Bram Moolenaarca8c9862010-07-24 15:00:38 +020081 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +020082 else
83 return FALSE;
84 return vim_strchr(wp->w_p_cocu, c) != NULL;
85}
86
87/*
88 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
Bram Moolenaarea042672021-06-29 20:22:32 +020089 * To be called after changing the state, "was_concealed" is the value of
90 * "conceal_cursor_line()" before the change.
91 * "
Bram Moolenaarf5963f72010-07-23 22:10:27 +020092 */
93 void
Bram Moolenaarea042672021-06-29 20:22:32 +020094conceal_check_cursor_line(int was_concealed)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020095{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000096 if (curwin->w_p_cole <= 0 || conceal_cursor_line(curwin) == was_concealed)
97 return;
Bram Moolenaarea042672021-06-29 20:22:32 +020098
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000099 int wcol = curwin->w_wcol;
Bram Moolenaarea042672021-06-29 20:22:32 +0200100
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000101 need_cursor_line_redraw = TRUE;
102 // Need to recompute cursor column, e.g., when starting Visual mode
103 // without concealing.
104 curs_columns(TRUE);
105
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#endif
112
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200113/*
114 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
115 * window then get the "Pmenu" highlight attribute.
116 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200117 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200118get_wcr_attr(win_T *wp)
119{
120 int wcr_attr = 0;
121
122 if (*wp->w_p_wcr != NUL)
123 wcr_attr = syn_name2attr(wp->w_p_wcr);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100124#ifdef FEAT_PROP_POPUP
Bram Moolenaarc363fe12019-08-04 18:13:46 +0200125 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200126 {
127 if (wp->w_popup_flags & POPF_INFO)
128 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
129 else
130 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
131 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200132#endif
133 return wcr_attr;
134}
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100137 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
138 * Return the new offset.
139 */
140 static int
141screen_fill_end(
142 win_T *wp,
143 int c1,
144 int c2,
145 int off,
146 int width,
147 int row,
148 int endrow,
149 int attr)
150{
151 int nn = off + width;
152
153 if (nn > wp->w_width)
154 nn = wp->w_width;
155#ifdef FEAT_RIGHTLEFT
156 if (wp->w_p_rl)
157 {
158 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200159 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100160 c1, c2, attr);
161 }
162 else
163#endif
164 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200165 wp->w_wincol + off, (int)wp->w_wincol + nn,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100166 c1, c2, attr);
167 return nn;
168}
169
170/*
171 * Clear lines near the end the window and mark the unused lines with "c1".
172 * use "c2" as the filler character.
173 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200175 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100176win_draw_end(
177 win_T *wp,
178 int c1,
179 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100180 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +0100181 int row,
182 int endrow,
183 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200186 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200187 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200188
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200189 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100190
191 if (draw_margin)
192 {
Bram Moolenaar1c934292015-01-27 16:39:29 +0100193#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100194 int fdc = compute_foldcolumn(wp, 0);
195
196 if (fdc > 0)
197 // draw the fold column
198 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200199 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +0100200#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100201#ifdef FEAT_SIGNS
202 if (signcolumn_on(wp))
203 // draw the sign column
204 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200205 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100206#endif
207 if ((wp->w_p_nu || wp->w_p_rnu)
208 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
209 // draw the number column
210 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200211 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214#ifdef FEAT_RIGHTLEFT
215 if (wp->w_p_rl)
216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200218 wp->w_wincol, W_ENDCOL(wp) - 1 - n, c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200220 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n, c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 }
222 else
223#endif
224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200226 wp->w_wincol + n, (int)W_ENDCOL(wp), c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 set_empty_rows(wp, row);
230}
231
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200232#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233/*
Bram Moolenaar1c934292015-01-27 16:39:29 +0100234 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
235 * space is available for window "wp", minus "col".
236 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200237 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100238compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +0100239{
Bram Moolenaar1c934292015-01-27 16:39:29 +0100240 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
John Marriottc15de972025-01-17 13:54:49 +0100241 int n = wp->w_width - (col + wmw);
Bram Moolenaar1c934292015-01-27 16:39:29 +0100242
John Marriottc15de972025-01-17 13:54:49 +0100243 return MIN(wp->w_p_fdc, n);
Bram Moolenaar1c934292015-01-27 16:39:29 +0100244}
245
246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000248 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100249 * Returns the number of bytes stored in 'p'. When non-multibyte characters are
250 * used for the fold column markers, this is equal to 'fdc' setting. Otherwise,
251 * this will be greater than 'fdc'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 */
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100253 size_t
Bram Moolenaar05540972016-01-30 20:31:25 +0100254fill_foldcolumn(
255 char_u *p,
256 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100257 int closed, // TRUE of FALSE
258 linenr_T lnum) // current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 int i = 0;
261 int level;
262 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000263 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100264 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100265 size_t byte_counter = 0;
266 int symbol = 0;
267 int len = 0;
John Marriottc15de972025-01-17 13:54:49 +0100268 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100270 // Init to all spaces.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100271 vim_memset(p, ' ', MAX_MCO * fdc + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
273 level = win_foldinfo.fi_level;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100274 empty = (fdc == 1) ? 0 : 1;
275
276 // If the column is too narrow, we start at the lowest level that
Bram Moolenaar008bff92021-03-04 21:55:58 +0100277 // fits and use numbers to indicate the depth.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100278 first_level = level - fdc - closed + 1 + empty;
279 if (first_level < 1)
280 first_level = 1;
281
John Marriottc15de972025-01-17 13:54:49 +0100282 n = MIN(fdc, level); // evaluate this once
283 for (i = 0; i < n; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100285 if (win_foldinfo.fi_lnum == lnum
286 && first_level + i >= win_foldinfo.fi_low_level)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100287 symbol = wp->w_fill_chars.foldopen;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100288 else if (first_level == 1)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100289 symbol = wp->w_fill_chars.foldsep;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100290 else if (first_level + i <= 9)
291 symbol = '0' + first_level + i;
292 else
293 symbol = '>';
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000294
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100295 len = utf_char2bytes(symbol, &p[byte_counter]);
296 byte_counter += len;
297 if (first_level + i >= level)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100299 i++;
300 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 }
302 }
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100303
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 if (closed)
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100305 {
306 if (symbol != 0)
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100307 {
308 // rollback length and the character
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100309 byte_counter -= len;
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100310 if (len > 1)
311 // for a multibyte character, erase all the bytes
312 vim_memset(p + byte_counter, ' ', len);
313 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100314 symbol = wp->w_fill_chars.foldclosed;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100315 len = utf_char2bytes(symbol, &p[byte_counter]);
316 byte_counter += len;
317 }
318
319 return MAX(byte_counter + (fdc - i), (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320}
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100321#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000323/*
324 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +0100325 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000326 */
327 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100328comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000329{
330 int i;
331
332 for (i = 0; i < Screen_mco; ++i)
333 {
334 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
335 return TRUE;
336 if (ScreenLinesC[i][off_from] == 0)
337 break;
338 }
339 return FALSE;
340}
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000341
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342/*
343 * Check whether the given character needs redrawing:
344 * - the (first byte of the) character is different
345 * - the attributes are different
346 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000347 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 */
349 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100350char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351{
352 if (cols > 0
353 && ((ScreenLines[off_from] != ScreenLines[off_to]
354 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 || (enc_dbcs != 0
356 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
357 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
358 ? ScreenLines2[off_from] != ScreenLines2[off_to]
359 : (cols > 1 && ScreenLines[off_from + 1]
360 != ScreenLines[off_to + 1])))
361 || (enc_utf8
362 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
363 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000364 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +0200365 || ((*mb_off2cells)(off_from, off_from + cols) > 1
366 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +0100367 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 return TRUE;
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200369 // TODO: This is a temporary solution until the root cause is fixed.
370 if (firstwin->w_wincol > 0)
371 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 return FALSE;
373}
374
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200375#if defined(FEAT_TERMINAL) || defined(PROTO)
376/*
377 * Return the index in ScreenLines[] for the current screen line.
378 */
379 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000380screen_get_current_line_off(void)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200381{
382 return (int)(current_ScreenLine - ScreenLines);
383}
384#endif
385
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100386#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200387/*
388 * Return TRUE if this position has a higher level popup or this cell is
389 * transparent in the current popup.
390 */
391 static int
392blocked_by_popup(int row, int col)
393{
394 int off;
395
396 if (!popup_visible)
397 return FALSE;
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200398 off = row * screen_Columns + col;
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200399 return popup_mask[off] > screen_zindex || popup_transparent[off];
400}
401#endif
402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200404 * Reset the highlighting. Used before clearing the screen.
405 */
406 void
407reset_screen_attr(void)
408{
409#ifdef FEAT_GUI
410 if (gui.in_use)
411 // Use a code that will reset gui.highlight_mask in
412 // gui_stop_highlight().
413 screen_attr = HL_ALL + 1;
414 else
415#endif
416 // Use attributes that is very unlikely to appear in text.
417 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
418}
419
420/*
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100421 * Return TRUE if the character at "row" / "col" is under the popup menu and it
422 * will be redrawn soon or it is under another popup.
423 */
424 static int
425skip_for_popup(int row, int col)
426{
427 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
428 if (pum_under_menu(row, col, TRUE)
429#ifdef FEAT_PROP_POPUP
430 && screen_zindex <= POPUPMENU_ZINDEX
431#endif
432 )
433 return TRUE;
434#ifdef FEAT_PROP_POPUP
435 if (blocked_by_popup(row, col))
436 return TRUE;
437#endif
438 return FALSE;
439}
440
441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 * Move one "cooked" screen line to the screen, but only the characters that
443 * have actually changed. Handle insert/delete character.
444 * "coloff" gives the first column on the screen for this line.
445 * "endcol" gives the columns where valid characters are.
446 * "clear_width" is the width of the window. It's > 0 if the rest of the line
447 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200448 * "flags" can have bits:
449 * SLF_POPUP popup window
450 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
452 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
zeertzjqd0c1b772024-03-16 15:03:33 +0100453 * SLF_INC_VCOL:
454 * When FALSE, use "last_vcol" for ScreenCols[] of the columns to clear.
455 * When TRUE, use an increasing sequence starting from "last_vcol + 1" for
456 * ScreenCols[] of the columns to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200458 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100459screen_line(
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100460 win_T *wp,
461 int row,
462 int coloff,
463 int endcol,
464 int clear_width,
zeertzjqd0c1b772024-03-16 15:03:33 +0100465 colnr_T last_vcol,
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100466 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
468 unsigned off_from;
469 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000470 unsigned max_off_from;
471 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100474 int force = FALSE; // force update rest of the line
475 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100477 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478#endif
479 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100480 int redraw_next; // redraw_this for next character
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100481#ifdef FEAT_GUI_MSWIN
482 int changed_this; // TRUE if character changed
483 int changed_next; // TRUE if next character changed
484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100486 int char_cells; // 1: normal char
487 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100489 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100490 if (row >= Rows)
491 row = Rows - 1;
492 if (endcol > Columns)
493 endcol = Columns;
494
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495# ifdef FEAT_CLIPBOARD
496 clip_may_clear_selection(row, row);
497# endif
498
499 off_from = (unsigned)(current_ScreenLine - ScreenLines);
500 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000501 max_off_from = off_from + screen_Columns;
502 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
504#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200505 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100507 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 if (clear_width > 0)
509 {
zeertzjqdeb22042024-03-17 19:44:30 +0100510 int clear_start = col;
511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 while (col <= endcol && ScreenLines[off_to] == ' '
513 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100514 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
516 ++off_to;
517 ++col;
518 }
519 if (col <= endcol)
520 screen_fill(row, row + 1, col + coloff,
521 endcol + coloff + 1, ' ', ' ', 0);
zeertzjqdeb22042024-03-17 19:44:30 +0100522
523 for (int i = endcol; i >= clear_start; i--)
524 ScreenCols[off_to + (i - col)] =
525 (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
527 col = endcol + 1;
528 off_to = LineOffset[row] + col + coloff;
529 off_from += col;
530 endcol = (clear_width > 0 ? clear_width : -clear_width);
531 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100532#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100534#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100535 // First char of a popup window may go on top of the right half of a
536 // double-wide character. Clear the left half to avoid it getting the popup
537 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100538 if (coloff > 0 && enc_utf8
539 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200540 && ScreenLinesUC[off_to - 1] != 0
541 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100542 {
543 ScreenLines[off_to - 1] = ' ';
544 ScreenLinesUC[off_to - 1] = 0;
545 screen_char(off_to - 1, row, col + coloff - 1);
546 }
547#endif
548
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100550#ifdef FEAT_GUI_MSWIN
551 changed_next = redraw_next;
552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554 while (col < endcol)
555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000557 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 else
559 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561 redraw_this = redraw_next;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100562 redraw_next = force || char_needs_redraw(off_from + char_cells,
563 off_to + char_cells, endcol - col - char_cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
565#ifdef FEAT_GUI
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100566# ifdef FEAT_GUI_MSWIN
567 changed_this = changed_next;
568 changed_next = redraw_next;
569# endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100570 // If the next character was bold, then redraw the current character to
571 // remove any pixels that might have spilt over into us. This only
572 // happens in the GUI.
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100573 // With MS-Windows antialiasing may also cause pixels to spill over
574 // from a previous character, no matter attributes, always redraw if a
575 // character changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (redraw_next && gui.in_use)
577 {
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100578# ifndef FEAT_GUI_MSWIN
Bram Moolenaarb9081882022-07-09 04:56:24 +0100579 hl = ScreenAttrs[off_to + char_cells];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000580 if (hl > HL_ALL)
581 hl = syn_attr2attr(hl);
582 if (hl & HL_BOLD)
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100583# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 redraw_this = TRUE;
585 }
586#endif
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100587 // Do not redraw if under the popup menu.
588 if (redraw_this && skip_for_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200589 redraw_this = FALSE;
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 if (redraw_this)
592 {
593 /*
594 * Special handling when 'xs' termcap flag set (hpterm):
595 * Attributes for characters are stored at the position where the
596 * cursor is when writing the highlighting code. The
597 * start-highlighting code must be written with the cursor on the
598 * first highlighted character. The stop-highlighting code must
599 * be written with the cursor just after the last highlighted
600 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100601 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 * to clear the rest of the line, and force redrawing it
603 * completely.
604 */
605 if ( p_wiv
606 && !force
607#ifdef FEAT_GUI
608 && !gui.in_use
609#endif
610 && ScreenAttrs[off_to] != 0
611 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
612 {
613 /*
614 * Need to remove highlighting attributes here.
615 */
616 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100617 out_str(T_CE); // clear rest of this screen line
618 screen_start(); // don't know where cursor is now
619 force = TRUE; // force redraw of rest of the line
620 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 /*
623 * If the previous character was highlighted, need to stop
624 * highlighting at this character.
625 */
626 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
627 {
628 screen_attr = ScreenAttrs[off_to - 1];
629 term_windgoto(row, col + coloff);
630 screen_stop_highlight();
631 }
632 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100633 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (enc_dbcs != 0)
636 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100637 // Check if overwriting a double-byte with a single-byte or
638 // the other way around requires another character to be
639 // redrawn. For UTF-8 this isn't needed, because comparing
640 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (char_cells == 1
642 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000643 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100645 // Writing a single-cell character over a double-cell
646 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 ScreenLines[off_to + 1] = 0;
648 redraw_next = TRUE;
649 }
650 else if (char_cells == 2
651 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000652 && (*mb_off2cells)(off_to, max_off_to) == 1
653 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100655 // Writing the second half of a double-cell character over
656 // a double-cell character: need to redraw the second
657 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 ScreenLines[off_to + 2] = 0;
659 redraw_next = TRUE;
660 }
661
662 if (enc_dbcs == DBCS_JPNU)
663 ScreenLines2[off_to] = ScreenLines2[off_from];
664 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100665 // When writing a single-width character over a double-width
666 // character and at the end of the redrawn text, need to clear out
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000667 // the right half of the old character.
668 // Also required when writing the right half of a double-width
669 // char over the left half of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (has_mbyte && col + char_cells == endcol
671 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000672 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000674 && (*mb_off2cells)(off_to, max_off_to) == 1
675 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (enc_utf8)
680 {
681 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
682 if (ScreenLinesUC[off_from] != 0)
683 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000684 int i;
685
686 for (i = 0; i < Screen_mco; ++i)
687 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689 }
690 if (char_cells == 2)
691 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
693#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100694 // The bold trick makes a single column of pixels appear in the
695 // next character. When a bold character is removed, the next
696 // character should be redrawn too. This happens for our own GUI
697 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (
699# ifdef FEAT_GUI
700 gui.in_use
701# endif
702# if defined(FEAT_GUI) && defined(UNIX)
703 ||
704# endif
705# ifdef UNIX
706 term_is_xterm
707# endif
708 )
709 {
710 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000711 if (hl > HL_ALL)
712 hl = syn_attr2attr(hl);
713 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 redraw_next = TRUE;
715 }
716#endif
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100717#ifdef FEAT_GUI_MSWIN
718 // MS-Windows antialiasing may spill over to the next character,
719 // redraw that one if this one changed, no matter attributes.
720 if (gui.in_use && changed_this)
721 redraw_next = TRUE;
722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100724
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100725 // For simplicity set the attributes of second half of a
726 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000727 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000729
730 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 screen_char(off_to, row, col + coloff);
734 }
735 else if ( p_wiv
736#ifdef FEAT_GUI
737 && !gui.in_use
738#endif
739 && col + coloff > 0)
740 {
741 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
742 {
743 /*
744 * Don't output stop-highlight when moving the cursor, it will
745 * stop the highlighting when it should continue.
746 */
747 screen_attr = 0;
748 }
749 else if (screen_attr != 0)
750 screen_stop_highlight();
751 }
752
Bram Moolenaarb9081882022-07-09 04:56:24 +0100753 ScreenCols[off_to] = ScreenCols[off_from];
754 if (char_cells == 2)
zeertzjq99941602023-08-19 13:08:50 +0200755 ScreenCols[off_to + 1] = ScreenCols[off_from + 1];
Bram Moolenaarb9081882022-07-09 04:56:24 +0100756
757 off_to += char_cells;
758 off_from += char_cells;
759 col += char_cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100762 if (clear_next && !skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100764 // Clear the second half of a double-wide character of which the left
765 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 ScreenLines[off_to] = ' ';
767 if (enc_utf8)
768 ScreenLinesUC[off_to] = 0;
769 screen_char(off_to, row, col + coloff);
770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 if (clear_width > 0
773#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200774 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776 )
777 {
778#ifdef FEAT_GUI
779 int startCol = col;
780#endif
781
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100782 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 while (col < clear_width && ScreenLines[off_to] == ' '
784 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100785 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
zeertzjqd0c1b772024-03-16 15:03:33 +0100787 ScreenCols[off_to] =
788 (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 ++off_to;
790 ++col;
791 }
792 if (col < clear_width)
793 {
794#ifdef FEAT_GUI
795 /*
796 * In the GUI, clearing the rest of the line may leave pixels
797 * behind if the first character cleared was bold. Some bold
798 * fonts spill over the left. In this case we redraw the previous
799 * character too. If we didn't skip any blanks above, then we
800 * only redraw if the character wasn't already redrawn anyway.
801 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000802 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 {
804 hl = ScreenAttrs[off_to];
805 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000806 {
807 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100808
Bram Moolenaar9c697322006-10-09 20:11:17 +0000809 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100810 // for utf-8, ScreenLines[char_offset + 1] == 0 means
811 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000812 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
813 else if (enc_dbcs != 0)
814 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100815 // find previous character by counting from first
816 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000817 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000818 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000819
820 while (off < off_to)
821 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000822 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000823 off += prev_cells;
824 }
825 }
826
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100827 if (!skip_for_popup(row, col + coloff - prev_cells))
828 {
829 if (enc_dbcs != 0 && prev_cells > 1)
830 screen_char_2(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000831 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100832 else
833 screen_char(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000834 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100835 }
Bram Moolenaar9c697322006-10-09 20:11:17 +0000836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838#endif
839 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
840 ' ', ' ', 0);
Bram Moolenaarb9081882022-07-09 04:56:24 +0100841 while (col < clear_width)
842 {
zeertzjqd0c1b772024-03-16 15:03:33 +0100843 ScreenCols[off_to++]
844 = (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100845 ++col;
846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 }
849
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200850 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100851#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200852 && !(flags & SLF_POPUP) // no separator for popup window
853#endif
854 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200856 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200857 // right of the window contents. But not on top of a popup window.
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200858 if (coloff + col < firstwin->w_wincol + topframe->fr_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100860 if (!skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200862 int c;
863
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100864 c = fillchar_vsep(&hl, wp);
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200865 if (ScreenLines[off_to] != (schar_T)c
866 || (enc_utf8 && (int)ScreenLinesUC[off_to]
867 != (c >= 0x80 ? c : 0))
868 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200870 ScreenLines[off_to] = c;
871 ScreenAttrs[off_to] = hl;
872 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200874 if (c >= 0x80)
875 {
876 ScreenLinesUC[off_to] = c;
877 ScreenLinesC[0][off_to] = 0;
878 }
879 else
880 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200882 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
885 }
886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 LineWraps[row] = FALSE;
888 }
889}
890
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000891#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000893 * Mirror text "str" for right-left displaying.
894 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000896 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100897rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
899 char_u *p1, *p2;
900 int t;
901
902 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
903 {
904 t = *p1;
905 *p1 = *p2;
906 *p2 = t;
907 }
908}
909#endif
910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 * Draw the verticap separator right of window "wp" starting with line "row".
913 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200914 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100915draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
917 int hl;
918 int c;
919
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000920 if (!wp->w_vsep_width)
921 return;
922
923 // draw the vertical separator right of this window
924 c = fillchar_vsep(&hl, wp);
925 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200926 W_ENDCOL(wp), W_ENDCOL(wp) + 1, c, ' ', hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 * Return TRUE if the status line of window "wp" is connected to the status
931 * line of the window right of it. If not, then it's a vertical separator.
932 * Only call if (wp->w_vsep_width != 0).
933 */
934 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100935stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937 frame_T *fr;
938
939 fr = wp->w_frame;
940 while (fr->fr_parent != NULL)
941 {
942 if (fr->fr_parent->fr_layout == FR_COL)
943 {
944 if (fr->fr_next != NULL)
945 break;
946 }
947 else
948 {
949 if (fr->fr_next != NULL)
950 return TRUE;
951 }
952 fr = fr->fr_parent;
953 }
954 return FALSE;
955}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958/*
959 * Get the value to show for the language mappings, active 'keymap'.
960 */
961 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100962get_keymap_str(
963 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100964 char_u *fmt, // format string containing one %s item
965 char_u *buf, // buffer for the result
966 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967{
968 char_u *p;
John Marriotta21240b2025-01-08 20:10:59 +0100969 int plen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
John Marriotta21240b2025-01-08 20:10:59 +0100972 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#ifdef FEAT_EVAL
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000975 buf_T *old_curbuf = curbuf;
976 win_T *old_curwin = curwin;
John Marriotta21240b2025-01-08 20:10:59 +0100977 char_u to_evaluate[] = "b:keymap_name";
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000978 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000980 curbuf = wp->w_buffer;
981 curwin = wp;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000982 ++emsg_skip;
John Marriotta21240b2025-01-08 20:10:59 +0100983 s = p = eval_to_string(to_evaluate, FALSE, FALSE);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000984 --emsg_skip;
985 curbuf = old_curbuf;
986 curwin = old_curwin;
987 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#ifdef FEAT_KEYMAP
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000991 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
992 p = wp->w_buffer->b_p_keymap;
993 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000995 p = (char_u *)"lang";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
John Marriotta21240b2025-01-08 20:10:59 +0100997 plen = vim_snprintf((char *)buf, len, (char *)fmt, p);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000998#ifdef FEAT_EVAL
999 vim_free(s);
1000#endif
John Marriotta21240b2025-01-08 20:10:59 +01001001 if (plen < 0 || plen > len - 1)
1002 {
1003 buf[0] = NUL;
1004 plen = 0;
1005 }
1006
1007 return plen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
1010#if defined(FEAT_STL_OPT) || defined(PROTO)
1011/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001012 * Redraw the status line or ruler of window "wp".
1013 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001015 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001016win_redr_custom(
1017 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001018 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001020 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 int attr;
1022 int curattr;
1023 int row;
1024 int col = 0;
1025 int maxwidth;
1026 int width;
1027 int n;
1028 int len;
1029 int fillchar;
1030 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001031 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 char_u *p;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001033 char_u *opt_name;
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001034 int opt_scope = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001035 stl_hlrec_T *hltab;
1036 stl_hlrec_T *tabtab;
Bram Moolenaar61452852011-02-01 18:01:11 +01001037 win_T *ewp;
1038 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001040 // There is a tiny chance that this gets called recursively: When
1041 // redrawing a status line triggers redrawing the ruler or tabline.
1042 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001043 if (entered)
1044 return;
1045 entered = TRUE;
1046
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001047 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001048 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001050 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001051 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001052 row = 0;
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001053 col = firstwin->w_wincol;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001054 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001055 attr = HL_ATTR(HLF_TPF);
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001056 maxwidth = topframe->fr_width;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001057 opt_name = (char_u *)"tabline";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001059 else
1060 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001061 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001062 fillchar = fillchar_status(&attr, wp);
Sean Dewarfc8a6012023-04-17 16:41:20 +01001063 int in_status_line = wp->w_status_height != 0;
1064 maxwidth = in_status_line ? wp->w_width : Columns;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001065
1066 if (draw_ruler)
1067 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001068 stl = p_ruf;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001069 opt_name = (char_u *)"rulerformat";
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001070 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001071 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001072 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001073 if (*++stl == '-')
1074 stl++;
1075 if (atoi((char *)stl))
1076 while (VIM_ISDIGIT(*stl))
1077 stl++;
1078 if (*stl++ != '(')
1079 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001080 }
Sean Dewarfc8a6012023-04-17 16:41:20 +01001081 col = ru_col - (Columns - maxwidth);
1082 if (col < (maxwidth + 1) / 2)
1083 col = (maxwidth + 1) / 2;
1084 maxwidth -= col;
1085 if (!in_status_line)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001086 {
1087 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001088 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001089 fillchar = ' ';
1090 attr = 0;
1091 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001092 }
1093 else
1094 {
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001095 opt_name = (char_u *)"statusline";
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001096 if (*wp->w_p_stl != NUL)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001097 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001098 stl = wp->w_p_stl;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001099 opt_scope = OPT_LOCAL;
1100 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001101 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001102 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001103 }
1104
Sean Dewarfc8a6012023-04-17 16:41:20 +01001105 if (in_status_line)
1106 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001107 }
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001110 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001112 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1113 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001114 ewp = wp == NULL ? curwin : wp;
1115 p_crb_save = ewp->w_p_crb;
1116 ewp->w_p_crb = FALSE;
1117
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001118 // Make a copy, because the statusline may include a function call that
1119 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001120 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001121 width = build_stl_str_hl(ewp, buf, sizeof(buf),
John Marriottc15de972025-01-17 13:54:49 +01001122 (stl == NULL) ? (char_u *)"" : stl, opt_name, opt_scope,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001123 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001124 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001125 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001127 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001128 p = transstr(buf);
1129 if (p != NULL)
1130 {
John Marriottc15de972025-01-17 13:54:49 +01001131 len = vim_snprintf((char *)buf, sizeof(buf), "%s", p);
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001132 vim_free(p);
1133 }
John Marriottc15de972025-01-17 13:54:49 +01001134 else
1135 len = (int)STRLEN(buf);
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001136
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001137 // fill up with "fillchar"
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001138 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 ++width;
1142 }
1143 buf[len] = NUL;
1144
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001145 /*
1146 * Draw each snippet with the specified highlighting.
1147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 curattr = attr;
1149 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001150 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001152 len = (int)(hltab[n].start - p);
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001153 screen_puts_len(p, len, row, col, curattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001155 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001157 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001159 else if (hltab[n].userhl < 0)
1160 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001161#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001162 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1163 && wp->w_status_height != 0)
1164 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001165 else if (wp != NULL && bt_terminal(wp->w_buffer)
1166 && wp->w_status_height != 0)
1167 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001168#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001169 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001170 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001172 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001174 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001175
1176 if (wp == NULL)
1177 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001178 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001179 col = firstwin->w_wincol;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001180 len = 0;
1181 p = buf;
1182 fillchar = 0;
1183 for (n = 0; tabtab[n].start != NULL; n++)
1184 {
1185 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1186 while (col < len)
1187 TabPageIdxs[col++] = fillchar;
1188 p = tabtab[n].start;
1189 fillchar = tabtab[n].userhl;
1190 }
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001191 while (col < Columns)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001192 TabPageIdxs[col++] = fillchar;
1193 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001194
1195theend:
1196 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197}
1198
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001199#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201/*
1202 * Output a single character directly to the screen and update ScreenLines.
1203 */
1204 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001205screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 char_u buf[MB_MAXBYTES + 1];
1208
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001209 if (has_mbyte)
1210 buf[(*mb_char2bytes)(c, buf)] = NUL;
1211 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001212 {
1213 buf[0] = c;
1214 buf[1] = NUL;
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 screen_puts(buf, row, col, attr);
1217}
1218
1219/*
zeertzjq47eec672023-06-01 20:26:55 +01001220 * Get a single character directly from ScreenLines into "bytes", which must
1221 * have a size of "MB_MAXBYTES + 1".
1222 * If "attrp" is not NULL, return the character's attribute in "*attrp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
1224 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001225screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226{
1227 unsigned off;
1228
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001229 // safety check
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001230 if (ScreenLines == NULL || row >= screen_Rows || col >= screen_Columns)
1231 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001233 off = LineOffset[row] + col;
zeertzjq47eec672023-06-01 20:26:55 +01001234 if (attrp != NULL)
1235 *attrp = ScreenAttrs[off];
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001236 bytes[0] = ScreenLines[off];
1237 bytes[1] = NUL;
1238
1239 if (enc_utf8 && ScreenLinesUC[off] != 0)
1240 bytes[utfc_char2bytes(off, bytes)] = NUL;
1241 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1242 {
1243 bytes[0] = ScreenLines[off];
1244 bytes[1] = ScreenLines2[off];
1245 bytes[2] = NUL;
1246 }
1247 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1248 {
1249 bytes[1] = ScreenLines[off + 1];
1250 bytes[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
1252}
1253
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001254/*
1255 * Return TRUE if composing characters for screen posn "off" differs from
1256 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001257 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001258 */
1259 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001260screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001261{
1262 int i;
1263
1264 for (i = 0; i < Screen_mco; ++i)
1265 {
1266 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1267 return TRUE;
1268 if (u8cc[i] == 0)
1269 break;
1270 }
1271 return FALSE;
1272}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274/*
1275 * Put string '*text' on the screen at position 'row' and 'col', with
1276 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1277 * Note: only outputs within one row, message is truncated at screen boundary!
1278 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1279 */
1280 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001281screen_puts(
1282 char_u *text,
1283 int row,
1284 int col,
1285 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 screen_puts_len(text, -1, row, col, attr);
1288}
1289
1290/*
1291 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1292 * a NUL.
1293 */
1294 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001295screen_puts_len(
1296 char_u *text,
1297 int textlen,
1298 int row,
1299 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001300 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001302 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 unsigned off;
1304 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001305 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001307 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 int mbyte_blen = 1;
1309 int mbyte_cells = 1;
1310 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001311 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001313#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001314 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001315 int pc, nc, nc1;
1316 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001318 int force_redraw_this;
1319 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001320 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001322 // Safety check. The check for negative row and column is to fix issue
1323 // #4102. TODO: find out why row/col could be negative.
1324 if (ScreenLines == NULL
1325 || row >= screen_Rows || row < 0
1326 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001328 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001330 // When drawing over the right half of a double-wide char clear out the
1331 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001332 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001333#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001334 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001335#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001336 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001337 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001338 if (!skip_for_popup(row, col - 1))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001339 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001340 ScreenLines[off - 1] = ' ';
1341 ScreenAttrs[off - 1] = 0;
1342 if (enc_utf8)
1343 {
1344 ScreenLinesUC[off - 1] = 0;
1345 ScreenLinesC[0][off - 1] = 0;
1346 }
1347 // redraw the previous cell, make it empty
1348 screen_char(off - 1, row, col - 1);
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001349 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001350 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001351 force_redraw_next = TRUE;
1352 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001353
Bram Moolenaar367329b2007-08-30 11:53:22 +00001354 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001355 while (col < screen_Columns
1356 && (len < 0 || (int)(ptr - text) < len)
1357 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001360 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 if (has_mbyte)
1362 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001363 mbyte_blen = enc_utf8 && len > 0
1364 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1365 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1367 mbyte_cells = 1;
1368 else if (enc_dbcs != 0)
1369 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001370 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001372 u8c = len >= 0
1373 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1374 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001376#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1378 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001379 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1381 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001382 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 nc = NUL;
1384 nc1 = NUL;
1385 }
1386 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001387 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001388 nc = len >= 0
1389 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1390 (int)((text + len) - ptr - mbyte_blen))
1391 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001392 nc1 = pcc[0];
1393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 pc = prev_c;
1395 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 else
1399 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001400#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001401 if (col + mbyte_cells > screen_Columns)
1402 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001403 // Only 1 cell left, but character requires 2 cells:
1404 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001405 c = '>';
1406 mbyte_cells = 1;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001411 force_redraw_this = force_redraw_next;
1412 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001413
1414 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 || (mbyte_cells == 2
1416 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1417 || (enc_dbcs == DBCS_JPNU
1418 && c == 0x8e
1419 && ScreenLines2[off] != ptr[1])
1420 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001421 && (ScreenLinesUC[off] !=
1422 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1423 || (ScreenLinesUC[off] != 0
1424 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001426 || exmode_active;
1427
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001428 if ((need_redraw || force_redraw_this) && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
1430#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001431 // The bold trick makes a single row of pixels appear in the next
1432 // character. When a bold character is removed, the next
1433 // character should be redrawn too. This happens for our own GUI
1434 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001435 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436# ifdef FEAT_GUI
1437 gui.in_use
1438# endif
1439# if defined(FEAT_GUI) && defined(UNIX)
1440 ||
1441# endif
1442# ifdef UNIX
1443 term_is_xterm
1444# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001445 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001447 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001449 if (n > HL_ALL)
1450 n = syn_attr2attr(n);
1451 if (n & HL_BOLD)
1452 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001455 // When at the end of the text and overwriting a two-cell
1456 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001457 // cell. Also when overwriting the left half of a two-cell char
1458 // with the right half of a two-cell char. Do this only once
1459 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (clear_next_cell)
1461 clear_next_cell = FALSE;
1462 else if (has_mbyte
1463 && (len < 0 ? ptr[mbyte_blen] == NUL
1464 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001465 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001467 && (*mb_off2cells)(off, max_off) == 1
1468 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 clear_next_cell = TRUE;
1470
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001471 // Make sure we never leave a second byte of a double-byte behind,
1472 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001474 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001476 && (*mb_off2cells)(off, max_off) == 1
1477 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 ScreenLines[off] = c;
1480 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001481 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (enc_utf8)
1483 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001484 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 ScreenLinesUC[off] = 0;
1486 else
1487 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001488 int i;
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001491 for (i = 0; i < Screen_mco; ++i)
1492 {
1493 ScreenLinesC[i][off] = u8cc[i];
1494 if (u8cc[i] == 0)
1495 break;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 if (mbyte_cells == 2)
1499 {
1500 ScreenLines[off + 1] = 0;
1501 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001502 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 screen_char(off, row, col);
1505 }
1506 else if (mbyte_cells == 2)
1507 {
1508 ScreenLines[off + 1] = ptr[1];
1509 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001510 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 screen_char_2(off, row, col);
1512 }
1513 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1514 {
1515 ScreenLines2[off] = ptr[1];
1516 screen_char(off, row, col);
1517 }
1518 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 screen_char(off, row, col);
1520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (has_mbyte)
1522 {
1523 off += mbyte_cells;
1524 col += mbyte_cells;
1525 ptr += mbyte_blen;
1526 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001527 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001528 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001529 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001531 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001532 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 ++off;
1538 ++col;
1539 ++ptr;
1540 }
1541 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001542
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001543 // If we detected the next character needs to be redrawn, but the text
1544 // doesn't extend up to there, update the character here.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001545 if (force_redraw_next && col < screen_Columns && !skip_for_popup(row, col))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001546 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001547 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1548 screen_char_2(off, row, col);
1549 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001550 screen_char(off, row, col);
1551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552}
1553
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001554#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001556 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001558 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001559start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001561 if (!p_hls || no_hlsearch)
1562 return;
1563
1564 end_search_hl(); // just in case it wasn't called before
1565 last_pat_prog(&screen_search_hl.rm);
1566 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567}
1568
1569/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001570 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001572 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001573end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001575 if (screen_search_hl.rm.regprog == NULL)
1576 return;
1577
1578 vim_regfree(screen_search_hl.rm.regprog);
1579 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001581#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001582
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02001583 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001584screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 attrentry_T *aep = NULL;
1587
1588 screen_attr = attr;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001589 if (!full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001590#ifdef MSWIN
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001591 || !termcap_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001593 )
1594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001596#ifdef FEAT_GUI
1597 if (gui.in_use)
1598 {
1599 char buf[20];
1600
1601 // The GUI handles this internally.
1602 sprintf(buf, "\033|%dh", attr);
1603 OUT_STR(buf);
1604 return;
1605 }
1606#endif
1607
1608 if (attr > HL_ALL) // special HL attr.
1609 {
1610 if (IS_CTERM)
1611 aep = syn_cterm_attr2entry(attr);
1612 else
1613 aep = syn_term_attr2entry(attr);
1614 if (aep == NULL) // did ":syntax clear"
1615 attr = 0;
1616 else
1617 attr = aep->ae_attr;
1618 }
1619#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1620 if (use_vtp())
1621 {
1622 guicolor_T defguifg, defguibg;
1623 int defctermfg, defctermbg;
1624
1625 // If FG and BG are unset, the color is undefined when
1626 // BOLD+INVERSE. Use Normal as the default value.
1627 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1628 &defguibg);
1629
1630 if (p_tgc)
1631 {
1632 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1633 term_fg_rgb_color(defguifg);
1634 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1635 term_bg_rgb_color(defguibg);
1636 }
1637 else if (t_colors >= 256)
1638 {
1639 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1640 term_fg_color(defctermfg);
1641 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1642 term_bg_color(defctermbg);
1643 }
1644 }
1645#endif
1646 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
1647 out_str(T_MD);
1648 else if (aep != NULL && cterm_normal_fg_bold && (
1649#ifdef FEAT_TERMGUICOLORS
1650 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1651 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1652 :
1653#endif
1654 t_colors > 1 && aep->ae_u.cterm.fg_color))
1655 // If the Normal FG color has BOLD attribute and the new HL
1656 // has a FG color defined, clear BOLD.
1657 out_str(T_ME);
1658 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
1659 out_str(T_SO);
1660 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
1661 out_str(T_UCS);
1662 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1663 out_str(T_USS);
1664 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1665 out_str(T_DS);
1666 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1667 out_str(T_CDS);
1668 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1669 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1670 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1671 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1672 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
1673 && *T_US != NUL)
1674 out_str(T_US);
1675 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
1676 out_str(T_CZH);
1677 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
1678 out_str(T_MR);
1679 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
1680 out_str(T_STS);
1681
1682 /*
1683 * Output the color or start string after bold etc., in case the
1684 * bold etc. override the color setting.
1685 */
1686 if (aep != NULL)
1687 {
PMuncha606f3a2023-11-15 15:35:49 +01001688 if (aep->ae_u.cterm.font > 0 && aep->ae_u.cterm.font < 12)
1689 term_font(aep->ae_u.cterm.font);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001690#ifdef FEAT_TERMGUICOLORS
1691 // When 'termguicolors' is set but fg or bg is unset,
1692 // fall back to the cterm colors. This helps for SpellBad,
1693 // where the GUI uses a red undercurl.
1694 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
1695 {
1696 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
1697 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699 else
1700#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001701 if (t_colors > 1)
1702 {
1703 if (aep->ae_u.cterm.fg_color)
1704 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1705 }
1706#ifdef FEAT_TERMGUICOLORS
1707 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001709 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
1710 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
1711 }
1712 else
1713#endif
1714 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001716 if (aep->ae_u.cterm.bg_color)
1717 term_bg_color(aep->ae_u.cterm.bg_color - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001719#ifdef FEAT_TERMGUICOLORS
1720 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1721 {
1722 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1723 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1724 }
1725 else
1726#endif
1727 if (t_colors > 1)
Bram Moolenaara050b942019-12-02 21:35:31 +01001728 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001729 if (aep->ae_u.cterm.ul_color)
1730 term_ul_color(aep->ae_u.cterm.ul_color - 1);
Bram Moolenaara050b942019-12-02 21:35:31 +01001731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001733 if (!IS_CTERM)
1734 {
1735 if (aep->ae_u.term.start != NULL)
1736 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
1738 }
1739}
1740
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02001741 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001742screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001744 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001745#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001746 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001750#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 && termcap_active
1752#endif
1753 )
1754 {
1755#ifdef FEAT_GUI
1756 if (gui.in_use)
1757 {
1758 char buf[20];
1759
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001760 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001761 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 OUT_STR(buf);
1763 }
1764 else
1765#endif
1766 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001767 int is_under;
1768
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001769 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
1771 attrentry_T *aep;
1772
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001773 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 {
1775 /*
1776 * Assume that t_me restores the original colors!
1777 */
1778 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001779 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001780#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001781 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1782 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001783# ifdef FEAT_VTP
1784 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1785# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001786 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001787#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001788 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001789#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001790 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1791 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001792# ifdef FEAT_VTP
1793 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1794# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001795 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001796#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001797 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001799#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1800 if (use_vtp())
1801 {
1802 if (do_ME_fg && do_ME_bg)
1803 do_ME = TRUE;
1804
1805 // FG and BG cannot be separated in T_ME, which is not
1806 // efficient.
1807 if (!do_ME && do_ME_fg)
1808 out_str((char_u *)"\033|39m"); // restore FG
1809 if (!do_ME && do_ME_bg)
1810 out_str((char_u *)"\033|49m"); // restore BG
1811 }
1812 else
1813 {
1814 // Process FG and BG at once.
1815 if (!do_ME)
1816 do_ME = do_ME_fg | do_ME_bg;
1817 }
1818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820 else
1821 {
1822 aep = syn_term_attr2entry(screen_attr);
1823 if (aep != NULL && aep->ae_u.term.stop != NULL)
1824 {
1825 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1826 do_ME = TRUE;
1827 else
1828 out_str(aep->ae_u.term.stop);
1829 }
1830 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001831 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 screen_attr = 0;
1833 else
1834 screen_attr = aep->ae_attr;
1835 }
1836
1837 /*
1838 * Often all ending-codes are equal to T_ME. Avoid outputting the
1839 * same sequence several times.
1840 */
1841 if (screen_attr & HL_STANDOUT)
1842 {
1843 if (STRCMP(T_SE, T_ME) == 0)
1844 do_ME = TRUE;
1845 else
1846 out_str(T_SE);
1847 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001848 is_under = (screen_attr & (HL_UNDERCURL
1849 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
1850 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001851 {
1852 if (STRCMP(T_UCE, T_ME) == 0)
1853 do_ME = TRUE;
1854 else
1855 out_str(T_UCE);
1856 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001857 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
1859 if (STRCMP(T_UE, T_ME) == 0)
1860 do_ME = TRUE;
1861 else
1862 out_str(T_UE);
1863 }
1864 if (screen_attr & HL_ITALIC)
1865 {
1866 if (STRCMP(T_CZR, T_ME) == 0)
1867 do_ME = TRUE;
1868 else
1869 out_str(T_CZR);
1870 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001871 if (screen_attr & HL_STRIKETHROUGH)
1872 {
1873 if (STRCMP(T_STE, T_ME) == 0)
1874 do_ME = TRUE;
1875 else
1876 out_str(T_STE);
1877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
1879 out_str(T_ME);
1880
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001881#ifdef FEAT_TERMGUICOLORS
1882 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001884 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001885 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001886 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001887 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02001888 if (cterm_normal_ul_gui_color != INVALCOLOR)
1889 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001890 }
1891 else
1892#endif
1893 {
1894 if (t_colors > 1)
1895 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001896 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001897 if (cterm_normal_fg_color != 0)
1898 term_fg_color(cterm_normal_fg_color - 1);
1899 if (cterm_normal_bg_color != 0)
1900 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02001901 if (cterm_normal_ul_color != 0)
1902 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001903 if (cterm_normal_fg_bold)
1904 out_str(T_MD);
1905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 }
1908 }
1909 screen_attr = 0;
1910}
1911
1912/*
1913 * Reset the colors for a cterm. Used when leaving Vim.
1914 * The machine specific code may override this again.
1915 */
1916 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001917reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001919 if (!IS_CTERM)
1920 return;
1921
1922 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001923#ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001924 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
1925 || cterm_normal_bg_gui_color != INVALCOLOR)
1926 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001927#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
1931 out_str(T_OP);
1932 screen_attr = -1;
1933 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001934 if (cterm_normal_fg_bold)
1935 {
1936 out_str(T_ME);
1937 screen_attr = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939}
1940
1941/*
1942 * Put character ScreenLines["off"] on the screen at position "row" and "col",
1943 * using the attributes from ScreenAttrs["off"].
1944 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001945 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001946screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
1948 int attr;
1949
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001950 // Check for illegal values, just in case (could happen just after
1951 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (row >= screen_Rows || col >= screen_Columns)
1953 return;
1954
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001955 // Outputting a character in the last cell on the screen may scroll the
1956 // screen up. Only do it when the "xn" termcap property is set, otherwise
1957 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01001958 if (*T_XN == NUL
1959 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001961 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 && !cmdmsg_rl
1963#endif
1964 )
1965 {
1966 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001967 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 return;
1969 }
1970
1971 /*
1972 * Stop highlighting first, so it's easier to move the cursor.
1973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (screen_char_attr != 0)
1975 attr = screen_char_attr;
1976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 attr = ScreenAttrs[off];
1978 if (screen_attr != attr)
1979 screen_stop_highlight();
1980
1981 windgoto(row, col);
1982
1983 if (screen_attr != attr)
1984 screen_start_highlight(attr);
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (enc_utf8 && ScreenLinesUC[off] != 0)
1987 {
1988 char_u buf[MB_MAXBYTES + 1];
1989
h-east8927c9b2024-04-20 17:57:19 +02001990 if (
1991#ifdef FEAT_GUI
1992 !gui.in_use &&
1993#endif
1994 get_cellwidth(ScreenLinesUC[off]) > 1
1995 )
mikoto2000e20fa592024-04-17 22:06:54 +02001996 {
h-east8927c9b2024-04-20 17:57:19 +02001997 // If the width is set to 2 with setcellwidths()
1998 // clear the two screen cells. If the character is actually
1999 // single width it won't change the second cell.
2000 out_str((char_u *)" ");
2001 term_windgoto(row, col);
2002 screen_cur_col = 9999;
mikoto2000e20fa592024-04-17 22:06:54 +02002003 }
2004 else if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002005 {
2006 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002007#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002008 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002009#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002010 )
2011 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002012 // Clear the two screen cells. If the character is actually
2013 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002014 out_str((char_u *)" ");
2015 term_windgoto(row, col);
2016 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002017 // not sure where the cursor is after drawing the ambiguous width
2018 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002019 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002020 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002021 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002023
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002024 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002025 buf[utfc_char2bytes(off, buf)] = NUL;
2026 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002032 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2034 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036
2037 screen_cur_col++;
2038}
2039
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040/*
2041 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2042 * on the screen at position 'row' and 'col'.
2043 * The attributes of the first byte is used for all. This is required to
2044 * output the two bytes of a double-byte character with nothing in between.
2045 */
2046 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002047screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002049 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2051 return;
2052
dundargocc57b5bc2022-11-02 13:30:51 +00002053 // Outputting the last character on the screen may scroll the screen up.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002054 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2056 {
2057 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002058 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 return;
2060 }
2061
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002062 // Output the first byte normally (positions the cursor), then write the
2063 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 screen_char(off, row, col);
2065 out_char(ScreenLines[off + 1]);
2066 ++screen_cur_col;
2067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069/*
2070 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2071 * This uses the contents of ScreenLines[] and doesn't change it.
2072 */
2073 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002074screen_draw_rectangle(
2075 int row,
2076 int col,
2077 int height,
2078 int width,
2079 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 int r, c;
2082 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002083 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002085 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002086 if (ScreenLines == NULL)
2087 return;
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 if (invert)
2090 screen_char_attr = HL_INVERSE;
2091 for (r = row; r < row + height; ++r)
2092 {
2093 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002094 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 for (c = col; c < col + width; ++c)
2096 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002097 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002099 if (!skip_for_popup(r, c))
2100 screen_char_2(off + c, r, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 ++c;
2102 }
2103 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002105 if (!skip_for_popup(r, c))
2106 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002107 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110 }
2111 }
2112 screen_char_attr = 0;
2113}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115/*
2116 * Redraw the characters for a vertically split window.
2117 */
2118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002119redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121 int col;
2122 int width;
2123
2124# ifdef FEAT_CLIPBOARD
2125 clip_may_clear_selection(row, end - 1);
2126# endif
2127
2128 if (wp == NULL)
2129 {
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02002130 col = firstwin->w_wincol;
2131 width = topframe->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133 else
2134 {
2135 col = wp->w_wincol;
2136 width = wp->w_width;
2137 }
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02002138 screen_draw_rectangle(row, col, end - row, width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002141 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002142space_to_screenline(int off, int attr)
2143{
2144 ScreenLines[off] = ' ';
2145 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002146 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002147 if (enc_utf8)
2148 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002149}
2150
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002152 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2153 * to "end_col" (exclusive) with character "c1" in first column followed by
2154 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 */
2156 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002157screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002158 int start_row,
2159 int end_row,
2160 int start_col,
2161 int end_col,
2162 int c1,
2163 int c2,
2164 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002166 int row;
2167 int col;
2168 int off;
2169 int end_off;
2170 int did_delete;
2171 int c;
2172 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002174 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#endif
2176
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002177 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002179 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 end_col = screen_Columns;
2181 if (ScreenLines == NULL
2182 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002183 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 return;
2185
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002186 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 norm_term = (
2188#ifdef FEAT_GUI
2189 !gui.in_use &&
2190#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002191 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 for (row = start_row; row < end_row; ++row)
2193 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002194 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002195#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002196 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002197#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002198 )
2199 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002200 // When drawing over the right half of a double-wide char clear
2201 // out the left half. When drawing over the left half of a
2202 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002203 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002204 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002205 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002206 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002207 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 /*
2210 * Try to use delete-line termcap code, when no attributes or in a
2211 * "normal" terminal, where a bold/italic space is just a
2212 * space.
2213 */
2214 did_delete = FALSE;
2215 if (c2 == ' '
2216 && end_col == Columns
2217 && can_clear(T_CE)
2218 && (attr == 0
2219 || (norm_term
2220 && attr <= HL_ALL
2221 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2222 {
2223 /*
2224 * check if we really need to clear something
2225 */
2226 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002227 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 ++col;
2229
2230 off = LineOffset[row] + col;
2231 end_off = LineOffset[row] + end_col;
2232
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002233 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (enc_utf8)
2235 while (off < end_off && ScreenLines[off] == ' '
2236 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2237 ++off;
2238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 while (off < end_off && ScreenLines[off] == ' '
2240 && ScreenAttrs[off] == 0)
2241 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002242 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 col = off - LineOffset[row];
2245 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002246 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002248 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002250 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002252 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 ++off;
2254 }
2255 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002256 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258
2259 off = LineOffset[row] + start_col;
2260 c = c1;
2261 for (col = start_col; col < end_col; ++col)
2262 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002263 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002264 || (enc_utf8 && (int)ScreenLinesUC[off]
2265 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002267 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#if defined(FEAT_GUI) || defined(UNIX)
2269 || force_next
2270#endif
2271 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002272 // Skip if under a(nother) popup.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002273 && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002276 // The bold trick may make a single row of pixels appear in
2277 // the next character. When a bold character is removed, the
2278 // next character should be redrawn too. This happens for our
2279 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (
2281# ifdef FEAT_GUI
2282 gui.in_use
2283# endif
2284# if defined(FEAT_GUI) && defined(UNIX)
2285 ||
2286# endif
2287# ifdef UNIX
2288 term_is_xterm
2289# endif
2290 )
2291 {
2292 if (ScreenLines[off] != ' '
2293 && (ScreenAttrs[off] > HL_ALL
2294 || ScreenAttrs[off] & HL_BOLD))
2295 force_next = TRUE;
2296 else
2297 force_next = FALSE;
2298 }
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002299#endif // FEAT_GUI || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (enc_utf8)
2302 {
2303 if (c >= 0x80)
2304 {
2305 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002306 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308 else
2309 ScreenLinesUC[off] = 0;
2310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 ScreenAttrs[off] = attr;
2312 if (!did_delete || c != ' ')
2313 screen_char(off, row, col);
2314 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002315 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 ++off;
2317 if (col == start_col)
2318 {
2319 if (did_delete)
2320 break;
2321 c = c2;
2322 }
2323 }
2324 if (end_col == Columns)
2325 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002326 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002329 if (start_col == 0 && end_col == Columns
glepnirff159252025-03-01 16:17:00 +01002330 && c1 == ' ' && c2 == ' ' && attr == 0
2331#ifdef FEAT_PROP_POPUP
2332 && !popup_overlaps_cmdline()
2333#endif
2334 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002335 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002336 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002337 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339 }
2340}
2341
2342/*
2343 * Check if there should be a delay. Used before clearing or redrawing the
2344 * screen or the command line.
2345 */
2346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002347check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2350 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002351 && emsg_silent == 0
2352 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002355 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 emsg_on_display = FALSE;
2357 if (check_msg_scroll)
2358 msg_scroll = FALSE;
2359 }
2360}
2361
2362/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002363 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2364 */
2365 static void
2366clear_TabPageIdxs(void)
2367{
2368 int scol;
2369
2370 for (scol = 0; scol < Columns; ++scol)
2371 TabPageIdxs[scol] = 0;
2372}
2373
2374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002376 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 * Returns TRUE if there is a valid screen to write to.
2378 * Returns FALSE when starting up and screen not initialized yet.
2379 */
2380 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002381screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002383 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 return (ScreenLines != NULL);
2385}
2386
2387/*
2388 * Resize the shell to Rows and Columns.
2389 * Allocate ScreenLines[] and associated items.
2390 *
2391 * There may be some time between setting Rows and Columns and (re)allocating
2392 * ScreenLines[]. This happens when starting up and when (manually) changing
2393 * the shell size. Always use screen_Rows and screen_Columns to access items
2394 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2395 * final size of the shell is needed.
2396 */
2397 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002398screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400 int new_row, old_row;
2401#ifdef FEAT_GUI
2402 int old_Rows;
2403#endif
2404 win_T *wp;
2405 int outofmem = FALSE;
2406 int len;
2407 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002409 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002412 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 unsigned *new_LineOffset;
2414 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002415 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002416#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002417 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002418 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002419 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002420#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002421 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002422 static int entered = FALSE; // avoid recursiveness
2423 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002424 int retry_count = 0;
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002425 int found_null;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002427retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 /*
2429 * Allocation of the screen buffers is done only when the size changes and
2430 * when Rows and Columns have been set and we have started doing full
2431 * screen stuff.
2432 */
2433 if ((ScreenLines != NULL
2434 && Rows == screen_Rows
2435 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 && enc_utf8 == (ScreenLinesUC != NULL)
2437 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002438 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 || Rows == 0
2440 || Columns == 0
2441 || (!full_screen && ScreenLines == NULL))
2442 return;
2443
2444 /*
2445 * It's possible that we produce an out-of-memory message below, which
2446 * will cause this function to be called again. To break the loop, just
2447 * return here.
2448 */
2449 if (entered)
2450 return;
2451 entered = TRUE;
2452
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002453 /*
2454 * Note that the window sizes are updated before reallocating the arrays,
2455 * thus we must not redraw here!
2456 */
2457 ++RedrawingDisabled;
2458
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002459 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002461#ifdef FEAT_GUI_HAIKU
2462 vim_lock_screen(); // be safe, put it here
2463#endif
2464
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002465 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 /*
2468 * We're changing the size of the screen.
2469 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2470 * - Move lines from the old arrays into the new arrays, clear extra
2471 * lines (unless the screen is going to be cleared).
2472 * - Free the old arrays.
2473 *
2474 * If anything fails, make ScreenLines NULL, so we don't do anything!
2475 * Continuing with the old ScreenLines may result in a crash, because the
2476 * size is wrong.
2477 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002478 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 win_free_lsize(wp);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002480 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002481 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002482 win_free_lsize(aucmd_win[i].auc_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002483#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002484 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002485 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002486 win_free_lsize(wp);
2487 // tab-local popup windows
2488 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002489 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002490 win_free_lsize(wp);
2491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002493 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002494 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (enc_utf8)
2496 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002497 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002498 for (int i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002499 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2500 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
2502 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002503 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2504 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
dundargocc57b5bc2022-11-02 13:30:51 +00002505 // Clear ScreenCols to avoid a warning for uninitialized memory in
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002506 // jump_to_mouse().
2507 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002508 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2509 new_LineWraps = LALLOC_MULT(char_u, Rows);
2510 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002511#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002512 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002513 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002514 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002517 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
2519 if (win_alloc_lines(wp) == FAIL)
2520 {
2521 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002522 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
2524 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00002525 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002526 if (aucmd_win[i].auc_win != NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002527 && aucmd_win[i].auc_win->w_lines == NULL
2528 && win_alloc_lines(aucmd_win[i].auc_win) == FAIL)
2529 {
2530 outofmem = TRUE;
2531 break;
2532 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002533#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002534 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002535 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002536 if (win_alloc_lines(wp) == FAIL)
2537 {
2538 outofmem = TRUE;
2539 goto give_up;
2540 }
2541 // tab-local popup windows
2542 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002543 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002544 if (win_alloc_lines(wp) == FAIL)
2545 {
2546 outofmem = TRUE;
2547 goto give_up;
2548 }
2549#endif
2550
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002551give_up:
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002552 found_null = FALSE;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002553 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002554 if (new_ScreenLinesC[i] == NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002555 {
2556 found_null = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002557 break;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (new_ScreenLines == NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002560 || (enc_utf8 && (new_ScreenLinesUC == NULL || found_null))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002563 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 || new_LineOffset == NULL
2565 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002566 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002567#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002568 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002569 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002570 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 || outofmem)
2573 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002574 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002575 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002576 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002577 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2578
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002579 // Remember we did this to avoid getting outofmem messages over
2580 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002581 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002582 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002583 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002584 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002585 for (int i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002586 VIM_CLEAR(new_ScreenLinesC[i]);
2587 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002588 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002589 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002590 VIM_CLEAR(new_LineOffset);
2591 VIM_CLEAR(new_LineWraps);
2592 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002593#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002594 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002595 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002596 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599 else
2600 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002601 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002602
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 for (new_row = 0; new_row < Rows; ++new_row)
2604 {
2605 new_LineOffset[new_row] = new_row * Columns;
2606 new_LineWraps[new_row] = FALSE;
2607
Olaf Seibertfd472652024-02-01 21:11:16 +01002608 (void)vim_memset(new_ScreenLines + new_row * Columns,
2609 ' ', (size_t)Columns * sizeof(schar_T));
2610 if (enc_utf8)
2611 {
2612 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2613 0, (size_t)Columns * sizeof(u8char_T));
2614 for (int i = 0; i < p_mco; ++i)
2615 (void)vim_memset(new_ScreenLinesC[i]
2616 + new_row * Columns,
2617 0, (size_t)Columns * sizeof(u8char_T));
2618 }
2619 if (enc_dbcs == DBCS_JPNU)
2620 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2621 0, (size_t)Columns * sizeof(schar_T));
2622 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2623 0, (size_t)Columns * sizeof(sattr_T));
2624 (void)vim_memset(new_ScreenCols + new_row * Columns,
2625 0, (size_t)Columns * sizeof(colnr_T));
2626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /*
2628 * If the screen is not going to be cleared, copy as much as
2629 * possible from the old screen to the new one and clear the rest
2630 * (used when resizing the window at the "--more--" prompt or when
2631 * executing an external command, for the GUI).
2632 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002633 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002636 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
2638 if (screen_Columns < Columns)
2639 len = screen_Columns;
2640 else
2641 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002642 // When switching to utf-8 don't copy characters, they
2643 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002644 if (!(enc_utf8 && ScreenLinesUC == NULL)
2645 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002646 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2647 ScreenLines + LineOffset[old_row],
2648 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002649 if (enc_utf8 && ScreenLinesUC != NULL
2650 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
2652 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2653 ScreenLinesUC + LineOffset[old_row],
2654 (size_t)len * sizeof(u8char_T));
Bram Moolenaare76062c2022-11-28 18:51:43 +00002655 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002656 mch_memmove(new_ScreenLinesC[i]
2657 + new_LineOffset[new_row],
2658 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 (size_t)len * sizeof(u8char_T));
2660 }
2661 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2662 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2663 ScreenLines2 + LineOffset[old_row],
2664 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2666 ScreenAttrs + LineOffset[old_row],
2667 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002668 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2669 ScreenAttrs + LineOffset[old_row],
2670 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
2672 }
2673 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002674 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002676
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002677#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002678 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2679 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002683 free_screenlines();
2684
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002685 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002688 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002689 ScreenLinesC[i] = new_ScreenLinesC[i];
2690 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002693 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 LineOffset = new_LineOffset;
2695 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002696 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002697#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002698 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002699 popup_mask_next = new_popup_mask_next;
2700 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002701 popup_mask_refresh = TRUE;
2702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002704 // It's important that screen_Rows and screen_Columns reflect the actual
2705 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706#ifdef FEAT_GUI
2707 old_Rows = screen_Rows;
2708#endif
2709 screen_Rows = Rows;
2710 screen_Columns = Columns;
2711
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002712 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002713 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002714 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#ifdef FEAT_GUI
2716 else if (gui.in_use
2717 && !gui.starting
2718 && ScreenLines != NULL
2719 && old_Rows != Rows)
2720 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002721 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2722
2723 // Adjust the position of the cursor, for when executing an external
2724 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002725 if (msg_row >= Rows) // Rows got smaller
2726 msg_row = Rows - 1; // put cursor at last row
2727 else if (Rows > old_Rows) // Rows got bigger
2728 msg_row += Rows - old_Rows; // put cursor in same place
2729 if (msg_col >= Columns) // Columns got smaller
2730 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002733 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002735#ifdef FEAT_GUI_HAIKU
2736 vim_unlock_screen();
2737#endif
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 entered = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002740 if (RedrawingDisabled > 0)
2741 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002742
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002743 /*
2744 * Do not apply autocommands more than 3 times to avoid an endless loop
2745 * in case applying autocommands always changes Rows or Columns.
2746 */
2747 if (starting == 0 && ++retry_count <= 3)
2748 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002749 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002750 // In rare cases, autocommands may have altered Rows or Columns,
2751 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002752 goto retry;
2753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754}
2755
2756 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002757free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002758{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002759 int i;
2760
Bram Moolenaar33796b32019-06-08 16:01:13 +02002761 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002762 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002763 VIM_CLEAR(ScreenLinesC[i]);
2764 VIM_CLEAR(ScreenLines2);
2765 VIM_CLEAR(ScreenLines);
2766 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002767 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002768 VIM_CLEAR(LineOffset);
2769 VIM_CLEAR(LineWraps);
2770 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002771#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002772 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002773 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002774 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002775#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002776}
2777
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002778/*
2779 * Clear the screen.
2780 * May delay if there is something the user should read.
2781 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002782 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002783 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002784 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01002785 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002786screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002789 screenalloc(FALSE); // allocate screen buffers if size changed
2790 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002791}
2792
2793/*
2794 * Do not clear the screen but mark everything for redraw.
2795 */
2796 void
2797redraw_as_cleared(void)
2798{
2799 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800}
2801
Bram Moolenaar838b7462022-09-26 15:19:56 +01002802 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002803screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
2805 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002806 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808 if (starting == NO_SCREEN || ScreenLines == NULL
2809#ifdef FEAT_GUI
2810 || (gui.in_use && gui.starting)
2811#endif
2812 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01002813 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815#ifdef FEAT_GUI
2816 if (!gui.in_use)
2817#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002818 screen_attr = -1; // force setting the Normal colors
2819 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
2821#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002822 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 clip_scroll_selection(9999);
2824#endif
2825
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002826 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 for (i = 0; i < Rows; ++i)
2828 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002829 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 LineWraps[i] = FALSE;
2831 }
2832
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002833 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002835 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002836 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002838 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840 else
2841 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002842 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 for (i = 0; i < Rows; ++i)
2844 lineinvalid(LineOffset[i], (int)Columns);
2845 clear_cmdline = TRUE;
2846 }
2847
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002848 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002850 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002852 redraw_tabline = TRUE;
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02002853#if defined(FEAT_TABPANEL)
2854 redraw_tabpanel = TRUE;
2855#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002856 if (must_redraw == UPD_CLEAR) // no need to clear again
2857 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002858 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002860#ifdef FEAT_PROP_POPUP
2861 popup_redraw_all(); // redraw all popup windows
2862#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002863 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002865 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 msg_didany = FALSE;
2867 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002868
2869 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870}
2871
2872/*
2873 * Clear one line in ScreenLines.
2874 */
2875 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002876lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877{
2878 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (enc_utf8)
2880 (void)vim_memset(ScreenLinesUC + off, 0,
2881 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002882 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002883 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884}
2885
2886/*
2887 * Mark one line in ScreenLines invalid by setting the attributes to an
2888 * invalid value.
2889 */
2890 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002891lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002894 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895}
2896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02002898 * To be called when characters were sent to the terminal directly, outputting
2899 * test on "screen_lnum".
2900 */
2901 void
2902line_was_clobbered(int screen_lnum)
2903{
2904 lineinvalid(LineOffset[screen_lnum], (int)Columns);
2905}
2906
2907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 * Copy part of a Screenline for vertically split window "wp".
2909 */
2910 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002911linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912{
2913 unsigned off_to = LineOffset[to] + wp->w_wincol;
2914 unsigned off_from = LineOffset[from] + wp->w_wincol;
2915
2916 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
2917 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 if (enc_utf8)
2919 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002920 int i;
2921
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
2923 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002924 for (i = 0; i < p_mco; ++i)
2925 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
2926 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 }
2928 if (enc_dbcs == DBCS_JPNU)
2929 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
2930 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
2932 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002933 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
2934 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
2937/*
2938 * Return TRUE if clearing with term string "p" would work.
2939 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02002940 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
2942 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002943can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
2945 return (*p != NUL && (t_colors <= 1
2946#ifdef FEAT_GUI
2947 || gui.in_use
2948#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002949#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002950 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02002951 || (!p_tgc && cterm_normal_bg_color == 0)
2952#else
2953 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002954#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002955 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002956#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002957 && !(p == T_CE && popup_visible)
2958#endif
2959 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960}
2961
2962/*
2963 * Reset cursor position. Use whenever cursor was moved because of outputting
2964 * something directly to the screen (shell commands) or a terminal control
2965 * code.
2966 */
2967 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002968screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 screen_cur_row = screen_cur_col = 9999;
2971}
2972
2973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 * Move the cursor to position "row","col" in the screen.
2975 * This tries to find the most efficient way to move, minimizing the number of
2976 * characters sent to the terminal.
2977 */
2978 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002979windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002981 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 int i;
2983 int plan;
2984 int cost;
2985 int wouldbe_col;
2986 int noinvcurs;
2987 char_u *bs;
2988 int goto_cost;
2989 int attr;
2990
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002991#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
2992#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994#define PLAN_LE 1
2995#define PLAN_CR 2
2996#define PLAN_NL 3
2997#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002998 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 if (ScreenLines == NULL)
3000 return;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003001 if (col == screen_cur_col && row == screen_cur_row)
3002 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003004 // Check for valid position.
3005 if (row < 0) // window without text lines?
3006 row = 0;
3007 if (row >= screen_Rows)
3008 row = screen_Rows - 1;
3009 if (col >= screen_Columns)
3010 col = screen_Columns - 1;
3011
3012 // check if no cursor movement is allowed in highlight mode
3013 if (screen_attr && *T_MS == NUL)
3014 noinvcurs = HIGHL_COST;
3015 else
3016 noinvcurs = 0;
3017 goto_cost = GOTO_COST + noinvcurs;
3018
3019 /*
3020 * Plan how to do the positioning:
3021 * 1. Use CR to move it to column 0, same row.
3022 * 2. Use T_LE to move it a few columns to the left.
3023 * 3. Use NL to move a few lines down, column 0.
3024 * 4. Move a few columns to the right with T_ND or by writing chars.
3025 *
3026 * Don't do this if the cursor went beyond the last column, the cursor
3027 * position is unknown then (some terminals wrap, some don't )
3028 *
3029 * First check if the highlighting attributes allow us to write
3030 * characters to move the cursor to the right.
3031 */
3032 if (row >= screen_cur_row && screen_cur_col < Columns)
3033 {
3034 /*
3035 * If the cursor is in the same row, bigger col, we can use CR
3036 * or T_LE.
3037 */
3038 bs = NULL; // init for GCC
3039 attr = screen_attr;
3040 if (row == screen_cur_row && col < screen_cur_col)
3041 {
3042 // "le" is preferred over "bc", because "bc" is obsolete
3043 if (*T_LE)
3044 bs = T_LE; // "cursor left"
3045 else
3046 bs = T_BC; // "backspace character (old)
3047 if (*bs)
3048 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3049 else
3050 cost = 999;
3051 if (col + 1 < cost) // using CR is less characters
3052 {
3053 plan = PLAN_CR;
3054 wouldbe_col = 0;
3055 cost = 1; // CR is just one character
3056 }
3057 else
3058 {
3059 plan = PLAN_LE;
3060 wouldbe_col = col;
3061 }
3062 if (noinvcurs) // will stop highlighting
3063 {
3064 cost += noinvcurs;
3065 attr = 0;
3066 }
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003070 * If the cursor is above where we want to be, we can use CR LF.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003072 else if (row > screen_cur_row)
3073 {
3074 plan = PLAN_NL;
3075 wouldbe_col = 0;
3076 cost = (row - screen_cur_row) * 2; // CR LF
3077 if (noinvcurs) // will stop highlighting
3078 {
3079 cost += noinvcurs;
3080 attr = 0;
3081 }
3082 }
3083
3084 /*
3085 * If the cursor is in the same row, smaller col, just use write.
3086 */
3087 else
3088 {
3089 plan = PLAN_WRITE;
3090 wouldbe_col = screen_cur_col;
3091 cost = 0;
3092 }
3093
3094 /*
3095 * Check if any characters that need to be written have the
3096 * correct attributes. Also avoid UTF-8 characters.
3097 */
3098 i = col - wouldbe_col;
3099 if (i > 0)
3100 cost += i;
3101 if (cost < goto_cost && i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
3103 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003104 * Check if the attributes are correct without additionally
3105 * stopping highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003107 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3108 while (i && *p++ == attr)
3109 --i;
3110 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003113 * Try if it works when highlighting is stopped here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003115 if (*--p == 0)
3116 {
3117 cost += noinvcurs;
3118 while (i && *p++ == 0)
3119 --i;
3120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 if (i != 0)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003122 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003124 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003126 // Don't use an UTF-8 char for positioning, it's slow.
3127 for (i = wouldbe_col; i < col; ++i)
3128 if (ScreenLinesUC[LineOffset[row] + i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003130 cost = 999;
3131 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003133 }
3134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003136 /*
3137 * We can do it without term_windgoto()!
3138 */
3139 if (cost < goto_cost)
3140 {
3141 if (plan == PLAN_LE)
3142 {
3143 if (noinvcurs)
3144 screen_stop_highlight();
3145 while (screen_cur_col > col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003147 out_str(bs);
3148 --screen_cur_col;
3149 }
3150 }
3151 else if (plan == PLAN_CR)
3152 {
3153 if (noinvcurs)
3154 screen_stop_highlight();
3155 out_char('\r');
3156 screen_cur_col = 0;
3157 }
3158 else if (plan == PLAN_NL)
3159 {
3160 if (noinvcurs)
3161 screen_stop_highlight();
3162 while (screen_cur_row < row)
3163 {
3164 out_char('\n');
3165 ++screen_cur_row;
3166 }
3167 screen_cur_col = 0;
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003170 i = col - screen_cur_col;
3171 if (i > 0)
3172 {
3173 /*
3174 * Use cursor-right if it's one character only. Avoids
3175 * removing a line of pixels from the last bold char, when
3176 * using the bold trick in the GUI.
3177 */
3178 if (T_ND[0] != NUL && T_ND[1] == NUL)
3179 {
3180 while (i-- > 0)
3181 out_char(*T_ND);
3182 }
3183 else
3184 {
3185 int off;
3186
3187 off = LineOffset[row] + screen_cur_col;
3188 while (i-- > 0)
3189 {
3190 if (ScreenAttrs[off] != screen_attr)
3191 screen_stop_highlight();
3192 out_flush_check();
3193 out_char(ScreenLines[off]);
3194 if (enc_dbcs == DBCS_JPNU
3195 && ScreenLines[off] == 0x8e)
3196 out_char(ScreenLines2[off]);
3197 ++off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199 }
3200 }
3201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003203 else
3204 cost = 999;
3205
3206 if (cost >= goto_cost)
3207 {
3208 if (noinvcurs)
3209 screen_stop_highlight();
3210 if (row == screen_cur_row && (col > screen_cur_col)
3211 && *T_CRI != NUL)
3212 term_cursor_right(col - screen_cur_col);
3213 else
3214 term_windgoto(row, col);
3215 }
3216 screen_cur_row = row;
3217 screen_cur_col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218}
3219
3220/*
3221 * Set cursor to its position in the current window.
3222 */
3223 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003224setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003226 setcursor_mayforce(FALSE);
3227}
3228
3229/*
3230 * Set cursor to its position in the current window.
3231 * When "force" is TRUE also when not redrawing.
3232 */
3233 void
3234setcursor_mayforce(int force)
3235{
3236 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
3238 validate_cursor();
3239 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003240 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003242 // With 'rightleft' set and the cursor on a double-wide
3243 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003244 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3245 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003246 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003247 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248#endif
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003249 curwin->w_wcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251}
3252
3253
3254/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003255 * Insert 'line_count' lines at 'row' in window 'wp'.
3256 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3257 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 * scrolling.
3259 * Returns FAIL if the lines are not inserted, OK for success.
3260 */
3261 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003262win_ins_lines(
3263 win_T *wp,
3264 int row,
3265 int line_count,
3266 int invalid,
3267 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 int did_delete;
3270 int nextrow;
3271 int lastrow;
3272 int retval;
3273
3274 if (invalid)
3275 wp->w_lines_valid = 0;
3276
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003277 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (wp->w_height < 5)
3279 return FAIL;
3280
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003281 // with the popup menu visible this might not work correctly
3282 if (pum_visible())
3283 return FAIL;
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (line_count > wp->w_height - row)
3286 line_count = wp->w_height - row;
3287
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003288 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (retval != MAYBE)
3290 return retval;
3291
3292 /*
3293 * If there is a next window or a status line, we first try to delete the
3294 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003295 * If this fails and there are following windows, don't do anything to
3296 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 */
3298 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 if (wp->w_next != NULL || wp->w_status_height)
3300 {
3301 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003302 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 did_delete = TRUE;
3304 else if (wp->w_next)
3305 return FAIL;
3306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 /*
3308 * if no lines deleted, blank the lines that will end up below the window
3309 */
3310 if (!did_delete)
3311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003314 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 lastrow = nextrow + line_count;
3316 if (lastrow > Rows)
3317 lastrow = Rows;
3318 screen_fill(nextrow - line_count, lastrow - line_count,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003319 wp->w_wincol, (int)W_ENDCOL(wp), ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
3321
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003322 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 == FAIL)
3324 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003325 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (did_delete)
3327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 win_rest_invalid(W_NEXT(wp));
3330 }
3331 return FAIL;
3332 }
3333
3334 return OK;
3335}
3336
3337/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003338 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3340 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3341 * scrolling
3342 * Return OK for success, FAIL if the lines are not deleted.
3343 */
3344 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003345win_del_lines(
3346 win_T *wp,
3347 int row,
3348 int line_count,
3349 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003350 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003351 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 int retval;
3354
3355 if (invalid)
3356 wp->w_lines_valid = 0;
3357
3358 if (line_count > wp->w_height - row)
3359 line_count = wp->w_height - row;
3360
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003361 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (retval != MAYBE)
3363 return retval;
3364
3365 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003366 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 return FAIL;
3368
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 /*
3370 * If there are windows or status lines below, try to put them at the
3371 * correct place. If we can't do that, they have to be redrawn.
3372 */
3373 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3374 {
3375 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003376 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 wp->w_redr_status = TRUE;
3379 win_rest_invalid(wp->w_next);
3380 }
3381 }
3382 /*
3383 * If this is the last window and there is no status line, redraw the
3384 * command line later.
3385 */
3386 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 redraw_cmdline = TRUE;
3388 return OK;
3389}
3390
3391/*
3392 * Common code for win_ins_lines() and win_del_lines().
3393 * Returns OK or FAIL when the work has been done.
3394 * Returns MAYBE when not finished yet.
3395 */
3396 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003397win_do_lines(
3398 win_T *wp,
3399 int row,
3400 int line_count,
3401 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003402 int del,
3403 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404{
3405 int retval;
3406
3407 if (!redrawing() || line_count <= 0)
3408 return FAIL;
3409
Bram Moolenaar33796b32019-06-08 16:01:13 +02003410 // When inserting lines would result in loss of command output, just redraw
3411 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003412 if (no_win_do_lines_ins && !del)
3413 return FAIL;
3414
Bram Moolenaar33796b32019-06-08 16:01:13 +02003415 // only a few lines left: redraw is faster
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003416 if (mayclear && Rows - line_count < 5
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003417 && wp->w_width == topframe->fr_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003419 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003420 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 return FAIL;
3422 }
3423
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003424#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003425 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003426 if (popup_visible)
3427 return FAIL;
3428#endif
3429
3430 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 if (row + line_count >= wp->w_height)
3432 {
3433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003434 wp->w_wincol, (int)W_ENDCOL(wp), ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 return OK;
3436 }
3437
3438 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003439 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003441 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003443 if (!no_win_do_lines_ins)
3444 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
3446 /*
3447 * If the terminal can set a scroll region, use that.
3448 * Always do this in a vertically split window. This will redraw from
3449 * ScreenLines[] when t_CV isn't defined. That's faster than using
3450 * win_line().
3451 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003452 * a character in the lower right corner of the scroll region may cause a
3453 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003455 if (scroll_region || wp->w_width != topframe->fr_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003457 if (scroll_region && (wp->w_width == topframe->fr_width
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003458 || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 scroll_region_set(wp, row);
3460 if (del)
3461 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003462 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 else
3464 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003465 wp->w_height - row, clear_attr, wp);
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003466 if (scroll_region && (wp->w_width == topframe->fr_width
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003467 || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 scroll_region_reset();
3469 return retval;
3470 }
3471
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003472 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 return MAYBE;
3476}
3477
3478/*
3479 * window 'wp' and everything after it is messed up, mark it for redraw
3480 */
3481 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003482win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003486 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 wp->w_redr_status = TRUE;
3488 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
3490 redraw_cmdline = TRUE;
3491}
3492
3493/*
3494 * The rest of the routines in this file perform screen manipulations. The
3495 * given operation is performed physically on the screen. The corresponding
3496 * change is also made to the internal screen image. In this way, the editor
3497 * anticipates the effect of editing changes on the appearance of the screen.
3498 * That way, when we call screenupdate a complete redraw isn't usually
3499 * necessary. Another advantage is that we can keep adding code to anticipate
3500 * screen changes, and in the meantime, everything still works.
3501 */
3502
3503/*
3504 * types for inserting or deleting lines
3505 */
3506#define USE_T_CAL 1
3507#define USE_T_CDL 2
3508#define USE_T_AL 3
3509#define USE_T_CE 4
3510#define USE_T_DL 5
3511#define USE_T_SR 6
3512#define USE_NL 7
3513#define USE_T_CD 8
3514#define USE_REDRAW 9
3515
3516/*
3517 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003518 * "end" is the line after the scrolled part. Normally it is Rows.
3519 * When scrolling region used "off" is the offset from the top for the region.
3520 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 *
3522 * return FAIL for failure, OK for success.
3523 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003524 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003525screen_ins_lines(
3526 int off,
3527 int row,
3528 int line_count,
3529 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003530 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003531 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
3533 int i;
3534 int j;
3535 unsigned temp;
3536 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003537 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 int type;
3539 int result_empty;
3540 int can_ce = can_clear(T_CE);
3541
3542 /*
3543 * FAIL if
3544 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 * - the line count is less than one
3546 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003547 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003548 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003549 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 */
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02003551 if (!screen_valid(TRUE)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003552 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003553 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003554#ifdef FEAT_CLIPBOARD
3555 || (clip_star.state != SELECT_CLEARED
3556 && redrawing_for_callback > 0)
3557#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003558#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003559 || popup_visible
3560#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003561 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 return FAIL;
3563
3564 /*
3565 * There are seven ways to insert lines:
3566 * 0. When in a vertically split window and t_CV isn't set, redraw the
3567 * characters from ScreenLines[].
3568 * 1. Use T_CD (clear to end of display) if it exists and the result of
3569 * the insert is just empty lines
3570 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3571 * present or line_count > 1. It looks better if we do all the inserts
3572 * at once.
3573 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3574 * insert is just empty lines and T_CE is not present or line_count >
3575 * 1.
3576 * 4. Use T_AL (insert line) if it exists.
3577 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3578 * just empty lines.
3579 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3580 * just empty lines.
3581 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3582 * the 'da' flag is not set or we have clear line capability.
3583 * 8. redraw the characters from ScreenLines[].
3584 *
3585 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3586 * the scrollbar for the window. It does have insert line, use that if it
3587 * exists.
3588 */
3589 result_empty = (row + line_count >= end);
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003590 if (wp != NULL && wp->w_width != topframe->fr_width && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003591 {
3592 // Avoid that lines are first cleared here and then redrawn, which
3593 // results in many characters updated twice. This happens with CTRL-F
3594 // in a vertically split window. With line-by-line scrolling
3595 // USE_REDRAW should be faster.
3596 if (line_count > 3)
3597 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003599 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003600 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 type = USE_T_CD;
3602 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3603 type = USE_T_CAL;
3604 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3605 type = USE_T_CDL;
3606 else if (*T_AL != NUL)
3607 type = USE_T_AL;
3608 else if (can_ce && result_empty)
3609 type = USE_T_CE;
3610 else if (*T_DL != NUL && result_empty)
3611 type = USE_T_DL;
3612 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3613 type = USE_T_SR;
3614 else
3615 return FAIL;
3616
3617 /*
3618 * For clearing the lines screen_del_lines() is used. This will also take
3619 * care of t_db if necessary.
3620 */
3621 if (type == USE_T_CD || type == USE_T_CDL ||
3622 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003623 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
3625 /*
3626 * If text is retained below the screen, first clear or delete as many
3627 * lines at the bottom of the window as are about to be inserted so that
3628 * the deleted lines won't later surface during a screen_del_lines.
3629 */
3630 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003631 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003634 // Remove a modeless selection when inserting lines halfway the screen
3635 // or not the full width of the screen.
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003636 if (off + row > 0 || (wp != NULL && wp->w_width != topframe->fr_width))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003637 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 else
3639 clip_scroll_selection(-line_count);
3640#endif
3641
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003642#ifdef FEAT_GUI_HAIKU
3643 vim_lock_screen();
3644#endif
3645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003647 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3648 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003649 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650#endif
3651
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003652 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3653 cursor_col = wp->w_wincol;
3654
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003655 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 cursor_row = row;
3657 else
3658 cursor_row = row + off;
3659
3660 /*
3661 * Shift LineOffset[] line_count down to reflect the inserted lines.
3662 * Clear the inserted lines in ScreenLines[].
3663 */
3664 row += off;
3665 end += off;
3666 for (i = 0; i < line_count; ++i)
3667 {
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003668 if (wp != NULL && wp->w_width != topframe->fr_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003670 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 j = end - 1 - i;
3672 while ((j -= line_count) >= row)
3673 linecopy(j + line_count, j, wp);
3674 j += line_count;
3675 if (can_clear((char_u *)" "))
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003676 lineclear(LineOffset[j] + wp->w_wincol,
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003677 wp->w_width, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 else
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003679 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 LineWraps[j] = FALSE;
3681 }
3682 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
3684 j = end - 1 - i;
3685 temp = LineOffset[j];
3686 while ((j -= line_count) >= row)
3687 {
3688 LineOffset[j + line_count] = LineOffset[j];
3689 LineWraps[j + line_count] = LineWraps[j];
3690 }
3691 LineOffset[j + line_count] = temp;
3692 LineWraps[j + line_count] = FALSE;
3693 if (can_clear((char_u *)" "))
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003694 lineclear(temp, topframe->fr_width, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 else
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003696 lineinvalid(temp, topframe->fr_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003700#ifdef FEAT_GUI_HAIKU
3701 vim_unlock_screen();
3702#endif
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003705 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003706 if (clear_attr != 0)
3707 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003709 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (type == USE_REDRAW)
3711 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003712 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
3714 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003715 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 else
3718 {
3719 for (i = 0; i < line_count; i++)
3720 {
3721 if (type == USE_T_AL)
3722 {
3723 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003724 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 out_str(T_AL);
3726 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003727 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003729 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731 }
3732
3733 /*
3734 * With scroll-reverse and 'da' flag set we need to clear the lines that
3735 * have been scrolled down into the region.
3736 */
3737 if (type == USE_T_SR && *T_DA)
3738 {
3739 for (i = 0; i < line_count; ++i)
3740 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003741 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003743 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 }
3746
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003747#if defined(FEAT_TABPANEL)
3748 redraw_tabpanel = TRUE;
3749#endif
3750
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751#ifdef FEAT_GUI
3752 gui_can_update_cursor();
3753 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003754 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755#endif
3756 return OK;
3757}
3758
3759/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003760 * Delete lines on the screen and update ScreenLines[].
3761 * "end" is the line after the scrolled part. Normally it is Rows.
3762 * When scrolling region used "off" is the offset from the top for the region.
3763 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 *
3765 * Return OK for success, FAIL if the lines are not deleted.
3766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003768screen_del_lines(
3769 int off,
3770 int row,
3771 int line_count,
3772 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003773 int force, // even when line_count > p_ttyscroll
3774 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003775 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
3777 int j;
3778 int i;
3779 unsigned temp;
3780 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003781 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003783 int result_empty; // result is empty until end of region
3784 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 int type;
3786
3787 /*
3788 * FAIL if
3789 * - there is no valid screen
3790 * - the screen has to be redrawn completely
3791 * - the line count is less than one
3792 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003793 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003794 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003796 if (!screen_valid(TRUE)
3797 || line_count <= 0
3798 || (!force && line_count > p_ttyscroll)
3799 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003800#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003801 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003802#endif
3803 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return FAIL;
3805
3806 /*
3807 * Check if the rest of the current region will become empty.
3808 */
3809 result_empty = row + line_count >= end;
3810
3811 /*
3812 * We can delete lines only when 'db' flag not set or when 'ce' option
3813 * available.
3814 */
3815 can_delete = (*T_DB == NUL || can_clear(T_CE));
3816
3817 /*
3818 * There are six ways to delete lines:
3819 * 0. When in a vertically split window and t_CV isn't set, redraw the
3820 * characters from ScreenLines[].
3821 * 1. Use T_CD if it exists and the result is empty.
3822 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3823 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3824 * none of the other ways work.
3825 * 4. Use T_CE (erase line) if the result is empty.
3826 * 5. Use T_DL (delete line) if it exists.
3827 * 6. redraw the characters from ScreenLines[].
3828 */
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003829 if (wp != NULL && wp->w_width != topframe->fr_width && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003830 {
3831 // Avoid that lines are first cleared here and then redrawn, which
3832 // results in many characters updated twice. This happens with CTRL-F
3833 // in a vertically split window. With line-by-line scrolling
3834 // USE_REDRAW should be faster.
3835 if (line_count > 3)
3836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003838 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003839 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 else if (row == 0 && (
3842#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003843 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3844 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 line_count == 1 ||
3846#endif
3847 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 type = USE_NL;
3849 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3850 type = USE_T_CDL;
3851 else if (can_clear(T_CE) && result_empty
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003852 && (wp == NULL || wp->w_width == topframe->fr_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 type = USE_T_CE;
3854 else if (*T_DL != NUL && can_delete)
3855 type = USE_T_DL;
3856 else if (*T_CDL != NUL && can_delete)
3857 type = USE_T_CDL;
3858 else
3859 return FAIL;
3860
3861#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003862 // Remove a modeless selection when deleting lines halfway the screen or
3863 // not the full width of the screen.
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003864 if (off + row > 0 || (wp != NULL && wp->w_width != topframe->fr_width))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003865 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 else
3867 clip_scroll_selection(line_count);
3868#endif
3869
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003870#ifdef FEAT_GUI_HAIKU
3871 vim_lock_screen();
3872#endif
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003875 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3876 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003877 gui_dont_update_cursor(gui.cursor_row >= row + off
3878 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879#endif
3880
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003881 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3882 cursor_col = wp->w_wincol;
3883
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003884 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
3886 cursor_row = row;
3887 cursor_end = end;
3888 }
3889 else
3890 {
3891 cursor_row = row + off;
3892 cursor_end = end + off;
3893 }
3894
3895 /*
3896 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3897 * Clear the inserted lines in ScreenLines[].
3898 */
3899 row += off;
3900 end += off;
3901 for (i = 0; i < line_count; ++i)
3902 {
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003903 if (wp != NULL && wp->w_width != topframe->fr_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003905 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 j = row + i;
3907 while ((j += line_count) <= end - 1)
3908 linecopy(j - line_count, j, wp);
3909 j -= line_count;
3910 if (can_clear((char_u *)" "))
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003911 lineclear(LineOffset[j] + wp->w_wincol,
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003912 wp->w_width, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 else
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003914 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 LineWraps[j] = FALSE;
3916 }
3917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003919 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 j = row + i;
3921 temp = LineOffset[j];
3922 while ((j += line_count) <= end - 1)
3923 {
3924 LineOffset[j - line_count] = LineOffset[j];
3925 LineWraps[j - line_count] = LineWraps[j];
3926 }
3927 LineOffset[j - line_count] = temp;
3928 LineWraps[j - line_count] = FALSE;
3929 if (can_clear((char_u *)" "))
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003930 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 else
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02003932 lineinvalid(temp, (int)Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003936#ifdef FEAT_GUI_HAIKU
3937 vim_unlock_screen();
3938#endif
3939
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003940 if (screen_attr != clear_attr)
3941 screen_stop_highlight();
3942 if (clear_attr != 0)
3943 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003945 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (type == USE_REDRAW)
3947 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003948 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003950 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003952 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954 else if (type == USE_T_CDL)
3955 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003956 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003958 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960 /*
3961 * Deleting lines at top of the screen or scroll region: Just scroll
3962 * the whole screen (scroll region) up by outputting newlines on the
3963 * last line.
3964 */
3965 else if (type == USE_NL)
3966 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003967 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003969 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 else
3972 {
3973 for (i = line_count; --i >= 0; )
3974 {
3975 if (type == USE_T_DL)
3976 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003977 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003978 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003980 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003982 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003983 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003985 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 }
3988
3989 /*
3990 * If the 'db' flag is set, we need to clear the lines that have been
3991 * scrolled up at the bottom of the region.
3992 */
3993 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
3994 {
3995 for (i = line_count; i > 0; --i)
3996 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003997 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003998 out_str(T_CE); // erase a line
3999 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 }
4002
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02004003#if defined(FEAT_TABPANEL)
4004 redraw_tabpanel = TRUE;
4005#endif
4006
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007#ifdef FEAT_GUI
4008 gui_can_update_cursor();
4009 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004010 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#endif
4012
4013 return OK;
4014}
4015
4016/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004017 * Return TRUE when postponing displaying the mode message: when not redrawing
4018 * or inside a mapping.
4019 */
4020 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004021skip_showmode(void)
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004022{
4023 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004024 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004025 if (global_busy
4026 || msg_silent != 0
4027 || !redrawing()
4028 || (char_avail() && !KeyTyped))
4029 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004030 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004031 return TRUE;
4032 }
4033 return FALSE;
4034}
4035
4036/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004037 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 *
4039 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4040 * If clear_cmdline is FALSE there may be a message there that needs to be
4041 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004042 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 * Return the length of the message (0 if no message).
4044 */
4045 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004046showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047{
4048 int need_clear;
4049 int length = 0;
4050 int do_mode;
4051 int attr;
4052 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
Bram Moolenaara2a89732022-08-31 14:46:18 +01004055 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004056 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004057 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004058 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004059 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004061 if (skip_showmode())
4062 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064 nwr_save = need_wait_return;
4065
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004066 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 check_for_delay(FALSE);
4068
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004069 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 need_clear = clear_cmdline;
4071 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004072 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004074 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 msg_pos_mode();
4076 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004077 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (do_mode)
4079 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004080 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004082 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004083# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004084 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004085# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004086 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004087# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004088 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004089# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004090 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004092 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093# endif
4094#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004095 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004096 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004098 // These messages can get long, avoid a wrap in a narrow
4099 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 length = (Rows - msg_row) * Columns - 3;
4101 if (edit_submode_extra != NULL)
4102 length -= vim_strsize(edit_submode_extra);
4103 if (length > 0)
4104 {
4105 if (edit_submode_pre != NULL)
4106 length -= vim_strsize(edit_submode_pre);
4107 if (length - vim_strsize(edit_submode) > 0)
4108 {
4109 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004110 msg_puts_attr((char *)edit_submode_pre, attr);
4111 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113 if (edit_submode_extra != NULL)
4114 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004115 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004117 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 else
4119 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004120 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004127 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004128 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004129 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004130 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
4132#ifdef FEAT_RIGHTLEFT
4133 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004134 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004136 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004138 else if (restart_edit == 'I' || restart_edit == 'i' ||
4139 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004140 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004142 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004144 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145#ifdef FEAT_RIGHTLEFT
4146 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004147 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148#endif
4149#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004150 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
4152# ifdef FEAT_ARABIC
4153 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004154 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 else
4156# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004157 if (get_keymap_str(curwin, (char_u *)" (%s)",
John Marriotta21240b2025-01-08 20:10:59 +01004158 NameBuff, MAXPATHL) > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004159 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
4161#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004162 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004163 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (VIsual_active)
4166 {
4167 char *p;
4168
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004169 // Don't concatenate separate words to avoid translation
4170 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 switch ((VIsual_select ? 4 : 0)
4172 + (VIsual_mode == Ctrl_V) * 2
4173 + (VIsual_mode == 'V'))
4174 {
4175 case 0: p = N_(" VISUAL"); break;
4176 case 1: p = N_(" VISUAL LINE"); break;
4177 case 2: p = N_(" VISUAL BLOCK"); break;
4178 case 4: p = N_(" SELECT"); break;
4179 case 5: p = N_(" SELECT LINE"); break;
4180 default: p = N_(" SELECT BLOCK"); break;
4181 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004182 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004184 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 need_clear = TRUE;
4188 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004189 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004190 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004192 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 need_clear = TRUE;
4194 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004195
4196 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004197 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004199 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 length = msg_col;
4201 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004202 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004205 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004207 else if (redraw_mode)
4208 {
4209 msg_pos_mode();
4210 msg_clr_eos();
4211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004213 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (VIsual_active)
4215 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004217 // If the last window has no status line, the ruler is after the mode
4218 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004219 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004220 win_redr_ruler(lastwin, TRUE, FALSE);
Martin Tournoijba43e762022-10-13 22:12:15 +01004221
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004223 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 clear_cmdline = FALSE;
4225
4226 return length;
4227}
4228
4229/*
4230 * Position for a mode message.
4231 */
4232 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004233msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
4235 msg_col = 0;
4236 msg_row = Rows - 1;
4237}
4238
4239/*
4240 * Delete mode message. Used when ESC is typed which is expected to end
4241 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004242 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 */
4244 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004245unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
4247 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004248 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 */
4250 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004251 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004253 clearmode();
4254}
4255
4256/*
4257 * Clear the mode message.
4258 */
4259 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004260clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004261{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004262 int save_msg_row = msg_row;
4263 int save_msg_col = msg_col;
4264
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004265 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004266 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004267 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004268 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004269
4270 msg_col = save_msg_col;
4271 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272}
4273
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004274 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004275recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004276{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004277 msg_puts_attr(_("recording"), attr);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004278 if (shortmess(SHM_RECORDING))
4279 return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004280
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004281 char s[4];
4282
4283 sprintf(s, " @%c", reg_recording);
4284 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004285}
4286
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004287/*
4288 * Draw the tab pages line at the top of the Vim window.
4289 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004290 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004291draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004292{
4293 int tabcount = 0;
4294 tabpage_T *tp;
4295 int tabwidth;
4296 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004297 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004298 int attr;
4299 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004300 win_T *cwp;
4301 int wincount;
4302 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004303 int c;
4304 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004305 int attr_sel = HL_ATTR(HLF_TPS);
4306 int attr_nosel = HL_ATTR(HLF_TP);
4307 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004308 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004309 int room;
4310 int use_sep_chars = (t_colors < 8
4311#ifdef FEAT_GUI
4312 && !gui.in_use
4313#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004314#ifdef FEAT_TERMGUICOLORS
4315 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004316#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004317 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004318
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02004319#if defined(FEAT_TABPANEL)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004320 col = firstwin->w_wincol;
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02004321#endif
4322
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004323 if (ScreenLines == NULL)
4324 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004325 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004326
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004327#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004328 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004329 if (gui_use_tabline())
4330 {
4331 gui_update_tabline();
4332 return;
4333 }
4334#endif
4335
4336 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004337 return;
4338
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004339#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004340 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004341
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004342 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004343 if (*p_tal != NUL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004344 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004345 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004346#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004347 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004348 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004349 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004350
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02004351 tabwidth = (topframe->fr_width - 1 + tabcount / 2) / tabcount;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004352 if (tabwidth < 6)
4353 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004354
Bram Moolenaar238a5642006-02-21 22:12:05 +00004355 attr = attr_nosel;
4356 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004357 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4358 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004359 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004360 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004361
Bram Moolenaar238a5642006-02-21 22:12:05 +00004362 if (tp->tp_topframe == topframe)
4363 attr = attr_sel;
4364 if (use_sep_chars && col > 0)
4365 screen_putchar('|', 0, col++, attr);
4366
4367 if (tp->tp_topframe != topframe)
4368 attr = attr_nosel;
4369
4370 screen_putchar(' ', 0, col++, attr);
4371
4372 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004373 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004374 cwp = curwin;
4375 wp = firstwin;
4376 }
4377 else
4378 {
4379 cwp = tp->tp_curwin;
4380 wp = tp->tp_firstwin;
4381 }
4382
4383 modified = FALSE;
4384 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4385 if (bufIsChanged(wp->w_buffer))
4386 modified = TRUE;
4387 if (modified || wincount > 1)
4388 {
4389 if (wincount > 1)
4390 {
John Marriottc15de972025-01-17 13:54:49 +01004391 len = vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004392 if (col + len >= Columns - 3)
4393 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004394 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004395#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004396 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004397#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004398 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004399#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004400 );
4401 col += len;
4402 }
4403 if (modified)
4404 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4405 screen_putchar(' ', 0, col++, attr);
4406 }
4407
4408 room = scol - col + tabwidth - 1;
4409 if (room > 0)
4410 {
John Marriottc15de972025-01-17 13:54:49 +01004411 int n;
4412
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004413 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004414 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004415 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004416 len = vim_strsize(NameBuff);
4417 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004418 if (has_mbyte)
4419 while (len > room)
4420 {
4421 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004422 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004423 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004424 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004425 {
4426 p += len - room;
4427 len = room;
4428 }
John Marriottc15de972025-01-17 13:54:49 +01004429 n = Columns - col - 1;
4430 if (len > n)
4431 len = n;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004432
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004433 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004434 col += len;
4435 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004436 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004437
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004438 // Store the tab page number in TabPageIdxs[], so that
4439 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004440 ++tabcount;
4441 while (scol < col)
4442 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004443 }
4444
Bram Moolenaar238a5642006-02-21 22:12:05 +00004445 if (use_sep_chars)
4446 c = '_';
4447 else
4448 c = ' ';
4449 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004450
Luuk van Baalba936f62022-12-15 13:15:39 +00004451 // Draw the 'showcmd' information if 'showcmdloc' == "tabline".
4452 if (p_sc && *p_sloc == 't')
4453 {
John Marriottc15de972025-01-17 13:54:49 +01004454 int n = (int)Columns - col - (tabcount > 1) * 3;
4455 int width = MIN(10, n);
Luuk van Baalba936f62022-12-15 13:15:39 +00004456
4457 if (width > 0)
4458 screen_puts_len(showcmd_buf, width, 0, (int)Columns
4459 - width - (tabcount > 1) * 2, attr_nosel);
4460 }
4461
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004462 // Put an "X" for closing the current tab if there are several.
Luuk van Baalba936f62022-12-15 13:15:39 +00004463 if (tabcount > 1)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004464 {
4465 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4466 TabPageIdxs[Columns - 1] = -999;
4467 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004468 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004469
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004470 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4471 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004472 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004473}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004474
4475/*
4476 * Get buffer name for "buf" into NameBuff[].
4477 * Takes care of special buffer names and translates special characters.
4478 */
4479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004480get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004481{
4482 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004483 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004484 else
4485 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4486 trans_characters(NameBuff, MAXPATHL);
4487}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004488
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489/*
4490 * Get the character to use in a status line. Get its attributes in "*attr".
4491 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004492 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004493fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494{
4495 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004496
4497#ifdef FEAT_TERMINAL
4498 if (bt_terminal(wp->w_buffer))
4499 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004500 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004501 {
4502 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004503 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004504 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004505 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004506 {
4507 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004508 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004509 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004510 }
4511 else
4512#endif
4513 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004515 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004516 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
4518 else
4519 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004520 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004521 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
Christian Brabandt6a650bf2023-11-08 21:23:29 +01004523 return fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526/*
4527 * Get the character to use in a separator between vertically split windows.
4528 * Get its attributes in "*attr".
4529 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004530 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004531fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004533 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004534 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 return '|';
4536 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004537 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540/*
4541 * Return TRUE if redrawing should currently be done.
4542 */
4543 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004544redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004546#ifdef FEAT_EVAL
4547 if (disable_redraw_for_testing)
4548 return 0;
4549 else
4550#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004551 return ((RedrawingDisabled == 0
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004552#ifdef FEAT_EVAL
4553 || ignore_redraw_flag_for_testing
4554#endif
4555 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556}
4557
4558/*
4559 * Return TRUE if printing messages should currently be done.
4560 */
4561 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004562messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004564 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565}
4566
Bram Moolenaare677df82019-09-02 22:31:11 +02004567/*
4568 * Compute columns for ruler and shown command. 'sc_col' is also used to
4569 * decide what the maximum length of a message on the status line can be.
4570 * If there is a status line for the last window, 'sc_col' is independent
4571 * of 'ru_col'.
4572 */
4573
4574#define COL_RULER 17 // columns needed by standard ruler
4575
4576 void
4577comp_col(void)
4578{
Sean Dewar876f5fb2023-08-17 22:40:05 +02004579 int last_has_status = last_stl_height(FALSE) > 0;
Bram Moolenaare677df82019-09-02 22:31:11 +02004580
4581 sc_col = 0;
4582 ru_col = 0;
4583 if (p_ru)
4584 {
Martin Tournoijba43e762022-10-13 22:12:15 +01004585#ifdef FEAT_STL_OPT
Bram Moolenaare677df82019-09-02 22:31:11 +02004586 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004587#else
Bram Moolenaare677df82019-09-02 22:31:11 +02004588 ru_col = COL_RULER + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004589#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004590 // no last status line, adjust sc_col
4591 if (!last_has_status)
4592 sc_col = ru_col;
4593 }
Sam-programs062141b2024-02-29 17:40:29 +01004594 if (p_sc && *p_sloc == 'l')
Bram Moolenaare677df82019-09-02 22:31:11 +02004595 {
4596 sc_col += SHOWCMD_COLS;
4597 if (!p_ru || last_has_status) // no need for separating space
4598 ++sc_col;
4599 }
4600 sc_col = Columns - sc_col;
4601 ru_col = Columns - ru_col;
4602 if (sc_col <= 0) // screen too narrow, will become a mess
4603 sc_col = 1;
4604 if (ru_col <= 0)
4605 ru_col = 1;
Bram Moolenaare677df82019-09-02 22:31:11 +02004606#ifdef FEAT_EVAL
4607 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4608#endif
4609}
4610
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004611#if defined(FEAT_LINEBREAK) || defined(PROTO)
4612/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004613 * Return the width of the 'number' and 'relativenumber' column.
4614 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004615 * Otherwise it depends on 'numberwidth' and the line count.
4616 */
4617 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004618number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004619{
4620 int n;
4621 linenr_T lnum;
4622
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004623 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004624 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004625 lnum = wp->w_height;
4626 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004627 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004628 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004629
Bram Moolenaar6b314672015-03-20 15:42:10 +01004630 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004631 return wp->w_nrwidth_width;
4632 wp->w_nrwidth_line_count = lnum;
4633
4634 n = 0;
4635 do
4636 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004637 lnum /= 10;
4638 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004639 } while (lnum > 0);
4640
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004641 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004642 if (n < wp->w_p_nuw - 1)
4643 n = wp->w_p_nuw - 1;
4644
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004645# ifdef FEAT_SIGNS
4646 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4647 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004648 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004649 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4650 n = 2;
4651# endif
4652
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004653 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004654 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004655 return n;
4656}
4657#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004658
Bram Moolenaar113e1072019-01-20 15:30:40 +01004659#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004660/*
4661 * Return the current cursor column. This is the actual position on the
4662 * screen. First column is 0.
4663 */
4664 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004665screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004666{
4667 return screen_cur_col;
4668}
4669
4670/*
4671 * Return the current cursor row. This is the actual position on the screen.
4672 * First row is 0.
4673 */
4674 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004675screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004676{
4677 return screen_cur_row;
4678}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004679#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004680
4681/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004682 * Calls mb_ptr2char_adv(p) and returns the character.
4683 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4684 */
4685 static int
4686get_encoded_char_adv(char_u **p)
4687{
4688 char_u *s = *p;
4689
4690 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4691 {
4692 varnumber_T num = 0;
4693 int bytes;
4694 int n;
4695
4696 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4697 {
4698 *p += 2;
4699 n = hexhex2nr(*p);
4700 if (n < 0)
4701 return 0;
4702 num = num * 256 + n;
4703 }
4704 *p += 2;
4705 return num;
4706 }
4707 return mb_ptr2char_adv(p);
4708}
4709
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004710struct charstab
4711{
4712 int *cp;
John Marriottc15de972025-01-17 13:54:49 +01004713 string_T name;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004714};
John Marriottc15de972025-01-17 13:54:49 +01004715
4716#define CHARSTAB_ENTRY(cp, name) \
4717 {(cp), {(char_u *)(name), STRLEN_LITERAL(name)}}
4718
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004719static fill_chars_T fill_chars;
4720static struct charstab filltab[] =
4721{
John Marriottc15de972025-01-17 13:54:49 +01004722 CHARSTAB_ENTRY(&fill_chars.stl, "stl"),
4723 CHARSTAB_ENTRY(&fill_chars.stlnc, "stlnc"),
4724 CHARSTAB_ENTRY(&fill_chars.vert, "vert"),
4725 CHARSTAB_ENTRY(&fill_chars.fold, "fold"),
4726 CHARSTAB_ENTRY(&fill_chars.foldopen, "foldopen"),
4727 CHARSTAB_ENTRY(&fill_chars.foldclosed, "foldclose"),
4728 CHARSTAB_ENTRY(&fill_chars.foldsep, "foldsep"),
4729 CHARSTAB_ENTRY(&fill_chars.diff, "diff"),
4730 CHARSTAB_ENTRY(&fill_chars.eob, "eob"),
glepnirb8762042025-04-07 20:57:14 +02004731 CHARSTAB_ENTRY(&fill_chars.lastline, "lastline"),
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02004732#if defined(FEAT_TABPANEL)
4733 CHARSTAB_ENTRY(&fill_chars.tpl_vert, "tpl_vert"),
4734#endif
glepnirb8762042025-04-07 20:57:14 +02004735 CHARSTAB_ENTRY(&fill_chars.trunc, "trunc"),
glepnird4dbf822025-04-12 18:35:34 +02004736 CHARSTAB_ENTRY(&fill_chars.truncrl, "truncrl"),
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004737};
4738static lcs_chars_T lcs_chars;
4739static struct charstab lcstab[] =
4740{
John Marriottc15de972025-01-17 13:54:49 +01004741 CHARSTAB_ENTRY(&lcs_chars.eol, "eol"),
4742 CHARSTAB_ENTRY(&lcs_chars.ext, "extends"),
4743 CHARSTAB_ENTRY(&lcs_chars.nbsp, "nbsp"),
4744 CHARSTAB_ENTRY(&lcs_chars.prec, "precedes"),
4745 CHARSTAB_ENTRY(&lcs_chars.space, "space"),
4746 CHARSTAB_ENTRY(&lcs_chars.tab2, "tab"),
4747 CHARSTAB_ENTRY(&lcs_chars.trail, "trail"),
4748 CHARSTAB_ENTRY(&lcs_chars.lead, "lead"),
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004749#ifdef FEAT_CONCEAL
John Marriottc15de972025-01-17 13:54:49 +01004750 CHARSTAB_ENTRY(&lcs_chars.conceal, "conceal"),
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004751#else
John Marriottc15de972025-01-17 13:54:49 +01004752 CHARSTAB_ENTRY(NULL, "conceal"),
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004753#endif
John Marriottc15de972025-01-17 13:54:49 +01004754 CHARSTAB_ENTRY(NULL, "multispace"),
4755 CHARSTAB_ENTRY(NULL, "leadmultispace")
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004756};
4757
zeertzjq6a8d2e12024-01-17 20:54:49 +01004758 static char *
John Marriottc15de972025-01-17 13:54:49 +01004759field_value_err(char *errbuf, size_t errbuflen, char *fmt, char_u *field)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004760{
4761 if (errbuf == NULL)
4762 return "";
4763 vim_snprintf(errbuf, errbuflen, _(fmt), field);
4764 return errbuf;
4765}
4766
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004767/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004768 * Handle setting 'listchars' or 'fillchars'.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004769 * "value" points to either the global or the window-local value.
zeertzjqd619d6a2023-05-08 15:56:21 +01004770 * "is_listchars" is TRUE for "listchars" and FALSE for "fillchars".
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004771 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004772 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004773 * Returns error message, NULL if it's OK.
4774 */
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004775 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004776set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
4777 char *errbuf, size_t errbuflen)
Bram Moolenaare677df82019-09-02 22:31:11 +02004778{
John Marriottc15de972025-01-17 13:54:49 +01004779 int round, i, entries;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004780 char_u *p, *s;
4781 int c1 = 0, c2 = 0, c3 = 0;
4782 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4783 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4784 int multispace_len = 0; // Length of lcs-multispace string
4785 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004786
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004787 struct charstab *tab;
4788
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004789 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004790 {
4791 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004792 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004793 entries = ARRAY_LENGTH(lcstab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004794 if (wp->w_p_lcs[0] == NUL)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004795 value = p_lcs; // local value is empty, use the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004796 }
4797 else
4798 {
4799 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004800 entries = ARRAY_LENGTH(filltab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004801 if (wp->w_p_fcs[0] == NUL)
John Marriottc15de972025-01-17 13:54:49 +01004802 value = p_fcs; // local value is empty, use the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004803 }
4804
4805 // first round: check for valid value, second round: assign values
zeertzjq00624a22023-11-23 20:47:16 +01004806 for (round = 0; round <= (apply ? 1 : 0); ++round)
Bram Moolenaare677df82019-09-02 22:31:11 +02004807 {
4808 if (round > 0)
4809 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004810 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004811 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004812 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004813 for (i = 0; i < entries; ++i)
4814 if (tab[i].cp != NULL)
4815 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004816 lcs_chars.tab1 = NUL;
4817 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004818
Mike Williamsf5785cf2021-09-13 22:17:38 +02004819 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004820 {
4821 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004822 if (lcs_chars.multispace != NULL)
4823 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004824 }
4825 else
4826 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004827
4828 if (lead_multispace_len > 0)
4829 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004830 lcs_chars.leadmultispace =
4831 ALLOC_MULT(int, lead_multispace_len + 1);
John Marriottc15de972025-01-17 13:54:49 +01004832 if (lcs_chars.leadmultispace != NULL)
4833 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004834 }
4835 else
4836 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004837 }
4838 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004839 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004840 fill_chars.stl = ' ';
4841 fill_chars.stlnc = ' ';
4842 fill_chars.vert = ' ';
4843 fill_chars.fold = '-';
4844 fill_chars.foldopen = '-';
4845 fill_chars.foldclosed = '+';
4846 fill_chars.foldsep = '|';
4847 fill_chars.diff = '-';
4848 fill_chars.eob = '~';
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01004849 fill_chars.lastline = '@';
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02004850#if defined(FEAT_TABPANEL)
4851 fill_chars.tpl_vert = '|';
4852#endif
glepnirb8762042025-04-07 20:57:14 +02004853 fill_chars.trunc = '>';
glepnird4dbf822025-04-12 18:35:34 +02004854 fill_chars.truncrl = '<';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004855 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004856 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004857 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02004858 while (*p)
4859 {
4860 for (i = 0; i < entries; ++i)
4861 {
John Marriottc15de972025-01-17 13:54:49 +01004862 if (!(STRNCMP(p, tab[i].name.string, tab[i].name.length) == 0 && p[tab[i].name.length] == ':'))
zeertzjq1f025b02023-09-30 12:43:07 +02004863 continue;
Bram Moolenaare677df82019-09-02 22:31:11 +02004864
John Marriottc15de972025-01-17 13:54:49 +01004865 s = p + tab[i].name.length + 1;
glepnirb8762042025-04-07 20:57:14 +02004866
John Marriottc15de972025-01-17 13:54:49 +01004867 if (is_listchars && STRCMP(tab[i].name.string, "multispace") == 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004868 {
zeertzjqf14b8ba2021-09-10 16:58:30 +02004869 if (round == 0)
4870 {
4871 // Get length of lcs-multispace string in first round
4872 last_multispace = p;
4873 multispace_len = 0;
4874 while (*s != NUL && *s != ',')
4875 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004876 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004877 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004878 return field_value_err(errbuf, errbuflen,
4879 e_wrong_character_width_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004880 tab[i].name.string);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004881 ++multispace_len;
4882 }
4883 if (multispace_len == 0)
4884 // lcs-multispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004885 return field_value_err(errbuf, errbuflen,
4886 e_wrong_number_of_characters_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004887 tab[i].name.string);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004888 }
4889 else
4890 {
4891 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02004892
zeertzjqf14b8ba2021-09-10 16:58:30 +02004893 while (*s != NUL && *s != ',')
4894 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004895 c1 = get_encoded_char_adv(&s);
John Marriottc15de972025-01-17 13:54:49 +01004896 if (p == last_multispace && lcs_chars.multispace != NULL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004897 lcs_chars.multispace[multispace_pos++] = c1;
4898 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02004899 }
glepnirb8762042025-04-07 20:57:14 +02004900 p = s;
zeertzjq1f025b02023-09-30 12:43:07 +02004901 break;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004902 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004903
John Marriottc15de972025-01-17 13:54:49 +01004904 if (is_listchars && STRCMP(tab[i].name.string, "leadmultispace") == 0)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004905 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004906 if (round == 0)
4907 {
glepnirb8762042025-04-07 20:57:14 +02004908 // Get length of lcs-leadmultispace string in first
zeertzjqb5f08012022-06-09 13:55:28 +01004909 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004910 last_lmultispace = p;
4911 lead_multispace_len = 0;
4912 while (*s != NUL && *s != ',')
4913 {
4914 c1 = get_encoded_char_adv(&s);
4915 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004916 return field_value_err(errbuf, errbuflen,
4917 e_wrong_character_width_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004918 tab[i].name.string);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004919 ++lead_multispace_len;
4920 }
4921 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01004922 // lcs-leadmultispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004923 return field_value_err(errbuf, errbuflen,
4924 e_wrong_number_of_characters_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004925 tab[i].name.string);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004926 }
4927 else
4928 {
4929 int multispace_pos = 0;
4930
4931 while (*s != NUL && *s != ',')
4932 {
4933 c1 = get_encoded_char_adv(&s);
John Marriottc15de972025-01-17 13:54:49 +01004934 if (p == last_lmultispace && lcs_chars.leadmultispace != NULL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004935 lcs_chars.leadmultispace[multispace_pos++] = c1;
4936 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004937 }
glepnirb8762042025-04-07 20:57:14 +02004938 p = s;
zeertzjq1f025b02023-09-30 12:43:07 +02004939 break;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004940 }
zeertzjq1f025b02023-09-30 12:43:07 +02004941
4942 c2 = c3 = 0;
zeertzjq6a8d2e12024-01-17 20:54:49 +01004943 if (*s == NUL)
4944 return field_value_err(errbuf, errbuflen,
4945 e_wrong_number_of_characters_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004946 tab[i].name.string);
zeertzjq1f025b02023-09-30 12:43:07 +02004947 c1 = get_encoded_char_adv(&s);
4948 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004949 return field_value_err(errbuf, errbuflen,
4950 e_wrong_character_width_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004951 tab[i].name.string);
zeertzjq1f025b02023-09-30 12:43:07 +02004952 if (tab[i].cp == &lcs_chars.tab2)
4953 {
4954 if (*s == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004955 return field_value_err(errbuf, errbuflen,
4956 e_wrong_number_of_characters_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004957 tab[i].name.string);
zeertzjq1f025b02023-09-30 12:43:07 +02004958 c2 = get_encoded_char_adv(&s);
4959 if (char2cells(c2) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004960 return field_value_err(errbuf, errbuflen,
4961 e_wrong_character_width_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004962 tab[i].name.string);
zeertzjq1f025b02023-09-30 12:43:07 +02004963 if (!(*s == ',' || *s == NUL))
4964 {
4965 c3 = get_encoded_char_adv(&s);
4966 if (char2cells(c3) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004967 return field_value_err(errbuf, errbuflen,
4968 e_wrong_character_width_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004969 tab[i].name.string);
zeertzjq1f025b02023-09-30 12:43:07 +02004970 }
4971 }
4972
4973 if (*s == ',' || *s == NUL)
4974 {
4975 if (round > 0)
4976 {
4977 if (tab[i].cp == &lcs_chars.tab2)
4978 {
4979 lcs_chars.tab1 = c1;
4980 lcs_chars.tab2 = c2;
4981 lcs_chars.tab3 = c3;
4982 }
4983 else if (tab[i].cp != NULL)
4984 *(tab[i].cp) = c1;
4985
4986 }
4987 p = s;
4988 break;
4989 }
zeertzjq6a8d2e12024-01-17 20:54:49 +01004990 else
4991 return field_value_err(errbuf, errbuflen,
4992 e_wrong_number_of_characters_for_field_str,
John Marriottc15de972025-01-17 13:54:49 +01004993 tab[i].name.string);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004994 }
4995
zeertzjq1f025b02023-09-30 12:43:07 +02004996 if (i == entries)
4997 return e_invalid_argument;
4998
Bram Moolenaare677df82019-09-02 22:31:11 +02004999 if (*p == ',')
5000 ++p;
5001 }
5002 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005003
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005004 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005005 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005006 if (is_listchars)
5007 {
5008 vim_free(wp->w_lcs_chars.multispace);
5009 vim_free(wp->w_lcs_chars.leadmultispace);
5010 wp->w_lcs_chars = lcs_chars;
5011 }
5012 else
5013 {
5014 wp->w_fill_chars = fill_chars;
5015 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005016 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005017
5018 return NULL; // no error
5019}
zeertzjq8ca29b62022-08-09 12:53:14 +01005020
5021/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005022 * Handle the new value of 'fillchars'.
5023 */
5024 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01005025set_fillchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
5026 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005027{
zeertzjq6a8d2e12024-01-17 20:54:49 +01005028 return set_chars_option(wp, val, FALSE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005029}
5030
5031/*
5032 * Handle the new value of 'listchars'.
5033 */
5034 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01005035set_listchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
5036 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005037{
zeertzjq6a8d2e12024-01-17 20:54:49 +01005038 return set_chars_option(wp, val, TRUE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005039}
5040
5041/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02005042 * Function given to ExpandGeneric() to obtain possible arguments of the
5043 * 'fillchars' option.
5044 */
5045 char_u *
5046get_fillchars_name(expand_T *xp UNUSED, int idx)
5047{
John Marriottc15de972025-01-17 13:54:49 +01005048 if (idx < 0 || idx >= (int)ARRAY_LENGTH(filltab))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02005049 return NULL;
5050
John Marriottc15de972025-01-17 13:54:49 +01005051 return filltab[idx].name.string;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02005052}
5053
5054/*
5055 * Function given to ExpandGeneric() to obtain possible arguments of the
5056 * 'listchars' option.
5057 */
5058 char_u *
5059get_listchars_name(expand_T *xp UNUSED, int idx)
5060{
John Marriottc15de972025-01-17 13:54:49 +01005061 if (idx < 0 || idx >= (int)ARRAY_LENGTH(lcstab))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02005062 return NULL;
5063
John Marriottc15de972025-01-17 13:54:49 +01005064 return lcstab[idx].name.string;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02005065}
5066
5067/*
zeertzjq8ca29b62022-08-09 12:53:14 +01005068 * Check all global and local values of 'listchars' and 'fillchars'.
5069 * Return an untranslated error messages if any of them is invalid, NULL
5070 * otherwise.
5071 */
5072 char *
5073check_chars_options(void)
5074{
5075 tabpage_T *tp;
5076 win_T *wp;
5077
zeertzjq6a8d2e12024-01-17 20:54:49 +01005078 if (set_listchars_option(curwin, p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005079 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005080 if (set_fillchars_option(curwin, p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005081 return e_conflicts_with_value_of_fillchars;
5082 FOR_ALL_TAB_WINDOWS(tp, wp)
5083 {
zeertzjq6a8d2e12024-01-17 20:54:49 +01005084 if (set_listchars_option(wp, wp->w_p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005085 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005086 if (set_fillchars_option(wp, wp->w_p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005087 return e_conflicts_with_value_of_fillchars;
5088 }
5089 return NULL;
5090}