blob: a8b4559b118329771d3ed986ba0821a18067155d [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,
159 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
160 c1, c2, attr);
161 }
162 else
163#endif
164 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
165 wp->w_wincol + off, (int)wp->w_wincol + nn,
166 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,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100218 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200219 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100221 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200222 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224 else
225#endif
226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100228 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200229 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 set_empty_rows(wp, row);
233}
234
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200235#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236/*
Bram Moolenaar1c934292015-01-27 16:39:29 +0100237 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
238 * space is available for window "wp", minus "col".
239 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200240 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100241compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +0100242{
243 int fdc = wp->w_p_fdc;
244 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +0200245 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100246
247 if (fdc > wwidth - (col + wmw))
248 fdc = wwidth - (col + wmw);
249 return fdc;
250}
251
252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000254 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100255 * Returns the number of bytes stored in 'p'. When non-multibyte characters are
256 * used for the fold column markers, this is equal to 'fdc' setting. Otherwise,
257 * this will be greater than 'fdc'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 */
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100259 size_t
Bram Moolenaar05540972016-01-30 20:31:25 +0100260fill_foldcolumn(
261 char_u *p,
262 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100263 int closed, // TRUE of FALSE
264 linenr_T lnum) // current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265{
266 int i = 0;
267 int level;
268 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000269 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100270 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100271 size_t byte_counter = 0;
272 int symbol = 0;
273 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100275 // Init to all spaces.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100276 vim_memset(p, ' ', MAX_MCO * fdc + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278 level = win_foldinfo.fi_level;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100279 empty = (fdc == 1) ? 0 : 1;
280
281 // If the column is too narrow, we start at the lowest level that
Bram Moolenaar008bff92021-03-04 21:55:58 +0100282 // fits and use numbers to indicate the depth.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100283 first_level = level - fdc - closed + 1 + empty;
284 if (first_level < 1)
285 first_level = 1;
286
287 for (i = 0; i < MIN(fdc, level); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100289 if (win_foldinfo.fi_lnum == lnum
290 && first_level + i >= win_foldinfo.fi_low_level)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100291 symbol = wp->w_fill_chars.foldopen;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100292 else if (first_level == 1)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100293 symbol = wp->w_fill_chars.foldsep;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100294 else if (first_level + i <= 9)
295 symbol = '0' + first_level + i;
296 else
297 symbol = '>';
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000298
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100299 len = utf_char2bytes(symbol, &p[byte_counter]);
300 byte_counter += len;
301 if (first_level + i >= level)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100303 i++;
304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 }
306 }
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 if (closed)
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100309 {
310 if (symbol != 0)
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100311 {
312 // rollback length and the character
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100313 byte_counter -= len;
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100314 if (len > 1)
315 // for a multibyte character, erase all the bytes
316 vim_memset(p + byte_counter, ' ', len);
317 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100318 symbol = wp->w_fill_chars.foldclosed;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100319 len = utf_char2bytes(symbol, &p[byte_counter]);
320 byte_counter += len;
321 }
322
323 return MAX(byte_counter + (fdc - i), (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324}
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100325#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000327/*
328 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +0100329 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000330 */
331 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100332comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000333{
334 int i;
335
336 for (i = 0; i < Screen_mco; ++i)
337 {
338 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
339 return TRUE;
340 if (ScreenLinesC[i][off_from] == 0)
341 break;
342 }
343 return FALSE;
344}
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000345
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346/*
347 * Check whether the given character needs redrawing:
348 * - the (first byte of the) character is different
349 * - the attributes are different
350 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000351 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 */
353 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100354char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355{
356 if (cols > 0
357 && ((ScreenLines[off_from] != ScreenLines[off_to]
358 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 || (enc_dbcs != 0
360 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
361 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
362 ? ScreenLines2[off_from] != ScreenLines2[off_to]
363 : (cols > 1 && ScreenLines[off_from + 1]
364 != ScreenLines[off_to + 1])))
365 || (enc_utf8
366 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
367 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000368 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +0200369 || ((*mb_off2cells)(off_from, off_from + cols) > 1
370 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +0100371 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 return TRUE;
373 return FALSE;
374}
375
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200376#if defined(FEAT_TERMINAL) || defined(PROTO)
377/*
378 * Return the index in ScreenLines[] for the current screen line.
379 */
380 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000381screen_get_current_line_off(void)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200382{
383 return (int)(current_ScreenLine - ScreenLines);
384}
385#endif
386
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100387#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200388/*
389 * Return TRUE if this position has a higher level popup or this cell is
390 * transparent in the current popup.
391 */
392 static int
393blocked_by_popup(int row, int col)
394{
395 int off;
396
397 if (!popup_visible)
398 return FALSE;
399 off = row * screen_Columns + col;
400 return popup_mask[off] > screen_zindex || popup_transparent[off];
401}
402#endif
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200405 * Reset the highlighting. Used before clearing the screen.
406 */
407 void
408reset_screen_attr(void)
409{
410#ifdef FEAT_GUI
411 if (gui.in_use)
412 // Use a code that will reset gui.highlight_mask in
413 // gui_stop_highlight().
414 screen_attr = HL_ALL + 1;
415 else
416#endif
417 // Use attributes that is very unlikely to appear in text.
418 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
419}
420
421/*
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100422 * Return TRUE if the character at "row" / "col" is under the popup menu and it
423 * will be redrawn soon or it is under another popup.
424 */
425 static int
426skip_for_popup(int row, int col)
427{
428 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
429 if (pum_under_menu(row, col, TRUE)
430#ifdef FEAT_PROP_POPUP
431 && screen_zindex <= POPUPMENU_ZINDEX
432#endif
433 )
434 return TRUE;
435#ifdef FEAT_PROP_POPUP
436 if (blocked_by_popup(row, col))
437 return TRUE;
438#endif
439 return FALSE;
440}
441
442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 * Move one "cooked" screen line to the screen, but only the characters that
444 * have actually changed. Handle insert/delete character.
445 * "coloff" gives the first column on the screen for this line.
446 * "endcol" gives the columns where valid characters are.
447 * "clear_width" is the width of the window. It's > 0 if the rest of the line
448 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200449 * "flags" can have bits:
450 * SLF_POPUP popup window
451 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
453 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
zeertzjqd0c1b772024-03-16 15:03:33 +0100454 * SLF_INC_VCOL:
455 * When FALSE, use "last_vcol" for ScreenCols[] of the columns to clear.
456 * When TRUE, use an increasing sequence starting from "last_vcol + 1" for
457 * ScreenCols[] of the columns to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200459 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100460screen_line(
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100461 win_T *wp,
462 int row,
463 int coloff,
464 int endcol,
465 int clear_width,
zeertzjqd0c1b772024-03-16 15:03:33 +0100466 colnr_T last_vcol,
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100467 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
469 unsigned off_from;
470 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000471 unsigned max_off_from;
472 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100475 int force = FALSE; // force update rest of the line
476 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100478 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479#endif
480 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100481 int redraw_next; // redraw_this for next character
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100482#ifdef FEAT_GUI_MSWIN
483 int changed_this; // TRUE if character changed
484 int changed_next; // TRUE if next character changed
485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100487 int char_cells; // 1: normal char
488 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100490 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100491 if (row >= Rows)
492 row = Rows - 1;
493 if (endcol > Columns)
494 endcol = Columns;
495
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496# ifdef FEAT_CLIPBOARD
497 clip_may_clear_selection(row, row);
498# endif
499
500 off_from = (unsigned)(current_ScreenLine - ScreenLines);
501 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000502 max_off_from = off_from + screen_Columns;
503 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
505#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200506 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100508 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (clear_width > 0)
510 {
zeertzjqdeb22042024-03-17 19:44:30 +0100511 int clear_start = col;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 while (col <= endcol && ScreenLines[off_to] == ' '
514 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100515 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 {
517 ++off_to;
518 ++col;
519 }
520 if (col <= endcol)
521 screen_fill(row, row + 1, col + coloff,
522 endcol + coloff + 1, ' ', ' ', 0);
zeertzjqdeb22042024-03-17 19:44:30 +0100523
524 for (int i = endcol; i >= clear_start; i--)
525 ScreenCols[off_to + (i - col)] =
526 (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
528 col = endcol + 1;
529 off_to = LineOffset[row] + col + coloff;
530 off_from += col;
531 endcol = (clear_width > 0 ? clear_width : -clear_width);
532 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100533#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100535#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100536 // First char of a popup window may go on top of the right half of a
537 // double-wide character. Clear the left half to avoid it getting the popup
538 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100539 if (coloff > 0 && enc_utf8
540 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200541 && ScreenLinesUC[off_to - 1] != 0
542 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100543 {
544 ScreenLines[off_to - 1] = ' ';
545 ScreenLinesUC[off_to - 1] = 0;
546 screen_char(off_to - 1, row, col + coloff - 1);
547 }
548#endif
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100551#ifdef FEAT_GUI_MSWIN
552 changed_next = redraw_next;
553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 while (col < endcol)
556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000558 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 else
560 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
562 redraw_this = redraw_next;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100563 redraw_next = force || char_needs_redraw(off_from + char_cells,
564 off_to + char_cells, endcol - col - char_cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
566#ifdef FEAT_GUI
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100567# ifdef FEAT_GUI_MSWIN
568 changed_this = changed_next;
569 changed_next = redraw_next;
570# endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100571 // If the next character was bold, then redraw the current character to
572 // remove any pixels that might have spilt over into us. This only
573 // happens in the GUI.
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100574 // With MS-Windows antialiasing may also cause pixels to spill over
575 // from a previous character, no matter attributes, always redraw if a
576 // character changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (redraw_next && gui.in_use)
578 {
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100579# ifndef FEAT_GUI_MSWIN
Bram Moolenaarb9081882022-07-09 04:56:24 +0100580 hl = ScreenAttrs[off_to + char_cells];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000581 if (hl > HL_ALL)
582 hl = syn_attr2attr(hl);
583 if (hl & HL_BOLD)
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 redraw_this = TRUE;
586 }
587#endif
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100588 // Do not redraw if under the popup menu.
589 if (redraw_this && skip_for_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200590 redraw_this = FALSE;
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100591
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 if (redraw_this)
593 {
594 /*
595 * Special handling when 'xs' termcap flag set (hpterm):
596 * Attributes for characters are stored at the position where the
597 * cursor is when writing the highlighting code. The
598 * start-highlighting code must be written with the cursor on the
599 * first highlighted character. The stop-highlighting code must
600 * be written with the cursor just after the last highlighted
601 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100602 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * to clear the rest of the line, and force redrawing it
604 * completely.
605 */
606 if ( p_wiv
607 && !force
608#ifdef FEAT_GUI
609 && !gui.in_use
610#endif
611 && ScreenAttrs[off_to] != 0
612 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
613 {
614 /*
615 * Need to remove highlighting attributes here.
616 */
617 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100618 out_str(T_CE); // clear rest of this screen line
619 screen_start(); // don't know where cursor is now
620 force = TRUE; // force redraw of rest of the line
621 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623 /*
624 * If the previous character was highlighted, need to stop
625 * highlighting at this character.
626 */
627 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
628 {
629 screen_attr = ScreenAttrs[off_to - 1];
630 term_windgoto(row, col + coloff);
631 screen_stop_highlight();
632 }
633 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100634 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (enc_dbcs != 0)
637 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100638 // Check if overwriting a double-byte with a single-byte or
639 // the other way around requires another character to be
640 // redrawn. For UTF-8 this isn't needed, because comparing
641 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (char_cells == 1
643 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000644 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100646 // Writing a single-cell character over a double-cell
647 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 ScreenLines[off_to + 1] = 0;
649 redraw_next = TRUE;
650 }
651 else if (char_cells == 2
652 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000653 && (*mb_off2cells)(off_to, max_off_to) == 1
654 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100656 // Writing the second half of a double-cell character over
657 // a double-cell character: need to redraw the second
658 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 ScreenLines[off_to + 2] = 0;
660 redraw_next = TRUE;
661 }
662
663 if (enc_dbcs == DBCS_JPNU)
664 ScreenLines2[off_to] = ScreenLines2[off_from];
665 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100666 // When writing a single-width character over a double-width
667 // character and at the end of the redrawn text, need to clear out
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000668 // the right half of the old character.
669 // Also required when writing the right half of a double-width
670 // char over the left half of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if (has_mbyte && col + char_cells == endcol
672 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000673 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000675 && (*mb_off2cells)(off_to, max_off_to) == 1
676 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (enc_utf8)
681 {
682 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
683 if (ScreenLinesUC[off_from] != 0)
684 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000685 int i;
686
687 for (i = 0; i < Screen_mco; ++i)
688 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690 }
691 if (char_cells == 2)
692 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100695 // The bold trick makes a single column of pixels appear in the
696 // next character. When a bold character is removed, the next
697 // character should be redrawn too. This happens for our own GUI
698 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (
700# ifdef FEAT_GUI
701 gui.in_use
702# endif
703# if defined(FEAT_GUI) && defined(UNIX)
704 ||
705# endif
706# ifdef UNIX
707 term_is_xterm
708# endif
709 )
710 {
711 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000712 if (hl > HL_ALL)
713 hl = syn_attr2attr(hl);
714 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 redraw_next = TRUE;
716 }
717#endif
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100718#ifdef FEAT_GUI_MSWIN
719 // MS-Windows antialiasing may spill over to the next character,
720 // redraw that one if this one changed, no matter attributes.
721 if (gui.in_use && changed_this)
722 redraw_next = TRUE;
723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100725
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100726 // For simplicity set the attributes of second half of a
727 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000728 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000730
731 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 screen_char(off_to, row, col + coloff);
735 }
736 else if ( p_wiv
737#ifdef FEAT_GUI
738 && !gui.in_use
739#endif
740 && col + coloff > 0)
741 {
742 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
743 {
744 /*
745 * Don't output stop-highlight when moving the cursor, it will
746 * stop the highlighting when it should continue.
747 */
748 screen_attr = 0;
749 }
750 else if (screen_attr != 0)
751 screen_stop_highlight();
752 }
753
Bram Moolenaarb9081882022-07-09 04:56:24 +0100754 ScreenCols[off_to] = ScreenCols[off_from];
755 if (char_cells == 2)
zeertzjq99941602023-08-19 13:08:50 +0200756 ScreenCols[off_to + 1] = ScreenCols[off_from + 1];
Bram Moolenaarb9081882022-07-09 04:56:24 +0100757
758 off_to += char_cells;
759 off_from += char_cells;
760 col += char_cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100763 if (clear_next && !skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100765 // Clear the second half of a double-wide character of which the left
766 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 ScreenLines[off_to] = ' ';
768 if (enc_utf8)
769 ScreenLinesUC[off_to] = 0;
770 screen_char(off_to, row, col + coloff);
771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
773 if (clear_width > 0
774#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200775 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#endif
777 )
778 {
779#ifdef FEAT_GUI
780 int startCol = col;
781#endif
782
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100783 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 while (col < clear_width && ScreenLines[off_to] == ' '
785 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100786 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
zeertzjqd0c1b772024-03-16 15:03:33 +0100788 ScreenCols[off_to] =
789 (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 ++off_to;
791 ++col;
792 }
793 if (col < clear_width)
794 {
795#ifdef FEAT_GUI
796 /*
797 * In the GUI, clearing the rest of the line may leave pixels
798 * behind if the first character cleared was bold. Some bold
799 * fonts spill over the left. In this case we redraw the previous
800 * character too. If we didn't skip any blanks above, then we
801 * only redraw if the character wasn't already redrawn anyway.
802 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000803 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
805 hl = ScreenAttrs[off_to];
806 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000807 {
808 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100809
Bram Moolenaar9c697322006-10-09 20:11:17 +0000810 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100811 // for utf-8, ScreenLines[char_offset + 1] == 0 means
812 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000813 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
814 else if (enc_dbcs != 0)
815 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100816 // find previous character by counting from first
817 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000818 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000819 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000820
821 while (off < off_to)
822 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000823 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000824 off += prev_cells;
825 }
826 }
827
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100828 if (!skip_for_popup(row, col + coloff - prev_cells))
829 {
830 if (enc_dbcs != 0 && prev_cells > 1)
831 screen_char_2(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000832 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100833 else
834 screen_char(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000835 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100836 }
Bram Moolenaar9c697322006-10-09 20:11:17 +0000837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 }
839#endif
840 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
841 ' ', ' ', 0);
Bram Moolenaarb9081882022-07-09 04:56:24 +0100842 while (col < clear_width)
843 {
zeertzjqd0c1b772024-03-16 15:03:33 +0100844 ScreenCols[off_to++]
845 = (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100846 ++col;
847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
849 }
850
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200851 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100852#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200853 && !(flags & SLF_POPUP) // no separator for popup window
854#endif
855 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200857 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200858 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200859 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100861 if (!skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200863 int c;
864
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100865 c = fillchar_vsep(&hl, wp);
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200866 if (ScreenLines[off_to] != (schar_T)c
867 || (enc_utf8 && (int)ScreenLinesUC[off_to]
868 != (c >= 0x80 ? c : 0))
869 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200871 ScreenLines[off_to] = c;
872 ScreenAttrs[off_to] = hl;
873 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200875 if (c >= 0x80)
876 {
877 ScreenLinesUC[off_to] = c;
878 ScreenLinesC[0][off_to] = 0;
879 }
880 else
881 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200883 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886 }
887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 LineWraps[row] = FALSE;
889 }
890}
891
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000892#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000894 * Mirror text "str" for right-left displaying.
895 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000897 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100898rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900 char_u *p1, *p2;
901 int t;
902
903 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
904 {
905 t = *p1;
906 *p1 = *p2;
907 *p2 = t;
908 }
909}
910#endif
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * Draw the verticap separator right of window "wp" starting with line "row".
914 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200915 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100916draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
918 int hl;
919 int c;
920
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000921 if (!wp->w_vsep_width)
922 return;
923
924 // draw the vertical separator right of this window
925 c = fillchar_vsep(&hl, wp);
926 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
927 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
928 c, ' ', hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 * Return TRUE if the status line of window "wp" is connected to the status
933 * line of the window right of it. If not, then it's a vertical separator.
934 * Only call if (wp->w_vsep_width != 0).
935 */
936 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100937stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
939 frame_T *fr;
940
941 fr = wp->w_frame;
942 while (fr->fr_parent != NULL)
943 {
944 if (fr->fr_parent->fr_layout == FR_COL)
945 {
946 if (fr->fr_next != NULL)
947 break;
948 }
949 else
950 {
951 if (fr->fr_next != NULL)
952 return TRUE;
953 }
954 fr = fr->fr_parent;
955 }
956 return FALSE;
957}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960/*
961 * Get the value to show for the language mappings, active 'keymap'.
962 */
963 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100964get_keymap_str(
965 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100966 char_u *fmt, // format string containing one %s item
967 char_u *buf, // buffer for the result
968 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
970 char_u *p;
971
972 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
973 return FALSE;
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975#ifdef FEAT_EVAL
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000976 buf_T *old_curbuf = curbuf;
977 win_T *old_curwin = curwin;
978 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;
982 STRCPY(buf, "b:keymap_name"); // must be writable
983 ++emsg_skip;
984 s = p = eval_to_string(buf, FALSE, FALSE);
985 --emsg_skip;
986 curbuf = old_curbuf;
987 curwin = old_curwin;
988 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_KEYMAP
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000992 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
993 p = wp->w_buffer->b_p_keymap;
994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000996 p = (char_u *)"lang";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000998 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
999 buf[0] = NUL;
1000#ifdef FEAT_EVAL
1001 vim_free(s);
1002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 return buf[0] != NUL;
1004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
1006#if defined(FEAT_STL_OPT) || defined(PROTO)
1007/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001008 * Redraw the status line or ruler of window "wp".
1009 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001011 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001012win_redr_custom(
1013 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001014 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001016 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 int attr;
1018 int curattr;
1019 int row;
1020 int col = 0;
1021 int maxwidth;
1022 int width;
1023 int n;
1024 int len;
1025 int fillchar;
1026 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001027 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 char_u *p;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001029 char_u *opt_name;
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001030 int opt_scope = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001031 stl_hlrec_T *hltab;
1032 stl_hlrec_T *tabtab;
Bram Moolenaar61452852011-02-01 18:01:11 +01001033 win_T *ewp;
1034 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001036 // There is a tiny chance that this gets called recursively: When
1037 // redrawing a status line triggers redrawing the ruler or tabline.
1038 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001039 if (entered)
1040 return;
1041 entered = TRUE;
1042
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001043 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001044 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001046 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001047 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001048 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001049 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001050 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001051 maxwidth = Columns;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001052 opt_name = (char_u *)"tabline";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001054 else
1055 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001056 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001057 fillchar = fillchar_status(&attr, wp);
Sean Dewarfc8a6012023-04-17 16:41:20 +01001058 int in_status_line = wp->w_status_height != 0;
1059 maxwidth = in_status_line ? wp->w_width : Columns;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001060
1061 if (draw_ruler)
1062 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001063 stl = p_ruf;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001064 opt_name = (char_u *)"rulerformat";
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001065 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001066 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001067 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001068 if (*++stl == '-')
1069 stl++;
1070 if (atoi((char *)stl))
1071 while (VIM_ISDIGIT(*stl))
1072 stl++;
1073 if (*stl++ != '(')
1074 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001075 }
Sean Dewarfc8a6012023-04-17 16:41:20 +01001076 col = ru_col - (Columns - maxwidth);
1077 if (col < (maxwidth + 1) / 2)
1078 col = (maxwidth + 1) / 2;
1079 maxwidth -= col;
1080 if (!in_status_line)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001081 {
1082 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001083 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001084 fillchar = ' ';
1085 attr = 0;
1086 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001087 }
1088 else
1089 {
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001090 opt_name = (char_u *)"statusline";
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001091 if (*wp->w_p_stl != NUL)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001092 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001093 stl = wp->w_p_stl;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001094 opt_scope = OPT_LOCAL;
1095 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001096 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001097 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001098 }
1099
Sean Dewarfc8a6012023-04-17 16:41:20 +01001100 if (in_status_line)
1101 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001102 }
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001105 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001107 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1108 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001109 ewp = wp == NULL ? curwin : wp;
1110 p_crb_save = ewp->w_p_crb;
1111 ewp->w_p_crb = FALSE;
1112
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001113 // Make a copy, because the statusline may include a function call that
1114 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001115 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001116 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001117 stl, opt_name, opt_scope,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001118 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001119 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001120 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001122 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001123 p = transstr(buf);
1124 if (p != NULL)
1125 {
1126 vim_strncpy(buf, p, sizeof(buf) - 1);
1127 vim_free(p);
1128 }
1129
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001130 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001131 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001132 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 ++width;
1136 }
1137 buf[len] = NUL;
1138
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001139 /*
1140 * Draw each snippet with the specified highlighting.
1141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 curattr = attr;
1143 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001144 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001146 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 screen_puts_len(p, len, row, col, curattr);
1148 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001149 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001151 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001153 else if (hltab[n].userhl < 0)
1154 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001155#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001156 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1157 && wp->w_status_height != 0)
1158 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001159 else if (wp != NULL && bt_terminal(wp->w_buffer)
1160 && wp->w_status_height != 0)
1161 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001162#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001163 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001164 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001166 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 }
1168 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001169
1170 if (wp == NULL)
1171 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001172 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001173 col = 0;
1174 len = 0;
1175 p = buf;
1176 fillchar = 0;
1177 for (n = 0; tabtab[n].start != NULL; n++)
1178 {
1179 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1180 while (col < len)
1181 TabPageIdxs[col++] = fillchar;
1182 p = tabtab[n].start;
1183 fillchar = tabtab[n].userhl;
1184 }
1185 while (col < Columns)
1186 TabPageIdxs[col++] = fillchar;
1187 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001188
1189theend:
1190 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191}
1192
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001193#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195/*
1196 * Output a single character directly to the screen and update ScreenLines.
1197 */
1198 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001199screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 char_u buf[MB_MAXBYTES + 1];
1202
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001203 if (has_mbyte)
1204 buf[(*mb_char2bytes)(c, buf)] = NUL;
1205 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001206 {
1207 buf[0] = c;
1208 buf[1] = NUL;
1209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 screen_puts(buf, row, col, attr);
1211}
1212
1213/*
zeertzjq47eec672023-06-01 20:26:55 +01001214 * Get a single character directly from ScreenLines into "bytes", which must
1215 * have a size of "MB_MAXBYTES + 1".
1216 * If "attrp" is not NULL, return the character's attribute in "*attrp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
1218 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001219screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
1221 unsigned off;
1222
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001223 // safety check
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001224 if (ScreenLines == NULL || row >= screen_Rows || col >= screen_Columns)
1225 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001227 off = LineOffset[row] + col;
zeertzjq47eec672023-06-01 20:26:55 +01001228 if (attrp != NULL)
1229 *attrp = ScreenAttrs[off];
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001230 bytes[0] = ScreenLines[off];
1231 bytes[1] = NUL;
1232
1233 if (enc_utf8 && ScreenLinesUC[off] != 0)
1234 bytes[utfc_char2bytes(off, bytes)] = NUL;
1235 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1236 {
1237 bytes[0] = ScreenLines[off];
1238 bytes[1] = ScreenLines2[off];
1239 bytes[2] = NUL;
1240 }
1241 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1242 {
1243 bytes[1] = ScreenLines[off + 1];
1244 bytes[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246}
1247
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001248/*
1249 * Return TRUE if composing characters for screen posn "off" differs from
1250 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001251 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001252 */
1253 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001254screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001255{
1256 int i;
1257
1258 for (i = 0; i < Screen_mco; ++i)
1259 {
1260 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1261 return TRUE;
1262 if (u8cc[i] == 0)
1263 break;
1264 }
1265 return FALSE;
1266}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268/*
1269 * Put string '*text' on the screen at position 'row' and 'col', with
1270 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1271 * Note: only outputs within one row, message is truncated at screen boundary!
1272 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1273 */
1274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001275screen_puts(
1276 char_u *text,
1277 int row,
1278 int col,
1279 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280{
1281 screen_puts_len(text, -1, row, col, attr);
1282}
1283
1284/*
1285 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1286 * a NUL.
1287 */
1288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001289screen_puts_len(
1290 char_u *text,
1291 int textlen,
1292 int row,
1293 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001294 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001296 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 unsigned off;
1298 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001299 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001301 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 int mbyte_blen = 1;
1303 int mbyte_cells = 1;
1304 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001305 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001307#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001308 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001309 int pc, nc, nc1;
1310 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001312 int force_redraw_this;
1313 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001314 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001316 // Safety check. The check for negative row and column is to fix issue
1317 // #4102. TODO: find out why row/col could be negative.
1318 if (ScreenLines == NULL
1319 || row >= screen_Rows || row < 0
1320 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001322 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001324 // When drawing over the right half of a double-wide char clear out the
1325 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001326 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001327#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001328 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001329#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001330 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001331 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001332 if (!skip_for_popup(row, col - 1))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001333 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001334 ScreenLines[off - 1] = ' ';
1335 ScreenAttrs[off - 1] = 0;
1336 if (enc_utf8)
1337 {
1338 ScreenLinesUC[off - 1] = 0;
1339 ScreenLinesC[0][off - 1] = 0;
1340 }
1341 // redraw the previous cell, make it empty
1342 screen_char(off - 1, row, col - 1);
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001343 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001344 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001345 force_redraw_next = TRUE;
1346 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001347
Bram Moolenaar367329b2007-08-30 11:53:22 +00001348 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001349 while (col < screen_Columns
1350 && (len < 0 || (int)(ptr - text) < len)
1351 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 {
1353 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001354 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (has_mbyte)
1356 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001357 mbyte_blen = enc_utf8 && len > 0
1358 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1359 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1361 mbyte_cells = 1;
1362 else if (enc_dbcs != 0)
1363 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001364 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001366 u8c = len >= 0
1367 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1368 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001370#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1372 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001373 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1375 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001376 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 nc = NUL;
1378 nc1 = NUL;
1379 }
1380 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001382 nc = len >= 0
1383 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1384 (int)((text + len) - ptr - mbyte_blen))
1385 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001386 nc1 = pcc[0];
1387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 pc = prev_c;
1389 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001390 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 else
1393 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001394#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001395 if (col + mbyte_cells > screen_Columns)
1396 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001397 // Only 1 cell left, but character requires 2 cells:
1398 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001399 c = '>';
1400 mbyte_cells = 1;
1401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001405 force_redraw_this = force_redraw_next;
1406 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001407
1408 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 || (mbyte_cells == 2
1410 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1411 || (enc_dbcs == DBCS_JPNU
1412 && c == 0x8e
1413 && ScreenLines2[off] != ptr[1])
1414 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001415 && (ScreenLinesUC[off] !=
1416 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1417 || (ScreenLinesUC[off] != 0
1418 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001420 || exmode_active;
1421
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001422 if ((need_redraw || force_redraw_this) && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
1424#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001425 // The bold trick makes a single row of pixels appear in the next
1426 // character. When a bold character is removed, the next
1427 // character should be redrawn too. This happens for our own GUI
1428 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001429 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430# ifdef FEAT_GUI
1431 gui.in_use
1432# endif
1433# if defined(FEAT_GUI) && defined(UNIX)
1434 ||
1435# endif
1436# ifdef UNIX
1437 term_is_xterm
1438# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001439 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001441 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001443 if (n > HL_ALL)
1444 n = syn_attr2attr(n);
1445 if (n & HL_BOLD)
1446 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001449 // When at the end of the text and overwriting a two-cell
1450 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001451 // cell. Also when overwriting the left half of a two-cell char
1452 // with the right half of a two-cell char. Do this only once
1453 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if (clear_next_cell)
1455 clear_next_cell = FALSE;
1456 else if (has_mbyte
1457 && (len < 0 ? ptr[mbyte_blen] == NUL
1458 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001459 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001461 && (*mb_off2cells)(off, max_off) == 1
1462 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 clear_next_cell = TRUE;
1464
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001465 // Make sure we never leave a second byte of a double-byte behind,
1466 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001468 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001470 && (*mb_off2cells)(off, max_off) == 1
1471 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ScreenLines[off] = c;
1474 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001475 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (enc_utf8)
1477 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001478 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 ScreenLinesUC[off] = 0;
1480 else
1481 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001482 int i;
1483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001485 for (i = 0; i < Screen_mco; ++i)
1486 {
1487 ScreenLinesC[i][off] = u8cc[i];
1488 if (u8cc[i] == 0)
1489 break;
1490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492 if (mbyte_cells == 2)
1493 {
1494 ScreenLines[off + 1] = 0;
1495 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001496 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 screen_char(off, row, col);
1499 }
1500 else if (mbyte_cells == 2)
1501 {
1502 ScreenLines[off + 1] = ptr[1];
1503 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001504 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 screen_char_2(off, row, col);
1506 }
1507 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1508 {
1509 ScreenLines2[off] = ptr[1];
1510 screen_char(off, row, col);
1511 }
1512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 screen_char(off, row, col);
1514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 if (has_mbyte)
1516 {
1517 off += mbyte_cells;
1518 col += mbyte_cells;
1519 ptr += mbyte_blen;
1520 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001521 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001522 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001523 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001525 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001526 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
1529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
1531 ++off;
1532 ++col;
1533 ++ptr;
1534 }
1535 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001536
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001537 // If we detected the next character needs to be redrawn, but the text
1538 // doesn't extend up to there, update the character here.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001539 if (force_redraw_next && col < screen_Columns && !skip_for_popup(row, col))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001540 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001541 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1542 screen_char_2(off, row, col);
1543 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001544 screen_char(off, row, col);
1545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546}
1547
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001548#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001550 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001552 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001553start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001555 if (!p_hls || no_hlsearch)
1556 return;
1557
1558 end_search_hl(); // just in case it wasn't called before
1559 last_pat_prog(&screen_search_hl.rm);
1560 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561}
1562
1563/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001564 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001566 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001567end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001569 if (screen_search_hl.rm.regprog == NULL)
1570 return;
1571
1572 vim_regfree(screen_search_hl.rm.regprog);
1573 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001575#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001578screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 attrentry_T *aep = NULL;
1581
1582 screen_attr = attr;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001583 if (!full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001584#ifdef MSWIN
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001585 || !termcap_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001587 )
1588 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001590#ifdef FEAT_GUI
1591 if (gui.in_use)
1592 {
1593 char buf[20];
1594
1595 // The GUI handles this internally.
1596 sprintf(buf, "\033|%dh", attr);
1597 OUT_STR(buf);
1598 return;
1599 }
1600#endif
1601
1602 if (attr > HL_ALL) // special HL attr.
1603 {
1604 if (IS_CTERM)
1605 aep = syn_cterm_attr2entry(attr);
1606 else
1607 aep = syn_term_attr2entry(attr);
1608 if (aep == NULL) // did ":syntax clear"
1609 attr = 0;
1610 else
1611 attr = aep->ae_attr;
1612 }
1613#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1614 if (use_vtp())
1615 {
1616 guicolor_T defguifg, defguibg;
1617 int defctermfg, defctermbg;
1618
1619 // If FG and BG are unset, the color is undefined when
1620 // BOLD+INVERSE. Use Normal as the default value.
1621 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1622 &defguibg);
1623
1624 if (p_tgc)
1625 {
1626 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1627 term_fg_rgb_color(defguifg);
1628 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1629 term_bg_rgb_color(defguibg);
1630 }
1631 else if (t_colors >= 256)
1632 {
1633 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1634 term_fg_color(defctermfg);
1635 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1636 term_bg_color(defctermbg);
1637 }
1638 }
1639#endif
1640 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
1641 out_str(T_MD);
1642 else if (aep != NULL && cterm_normal_fg_bold && (
1643#ifdef FEAT_TERMGUICOLORS
1644 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1645 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1646 :
1647#endif
1648 t_colors > 1 && aep->ae_u.cterm.fg_color))
1649 // If the Normal FG color has BOLD attribute and the new HL
1650 // has a FG color defined, clear BOLD.
1651 out_str(T_ME);
1652 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
1653 out_str(T_SO);
1654 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
1655 out_str(T_UCS);
1656 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1657 out_str(T_USS);
1658 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1659 out_str(T_DS);
1660 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1661 out_str(T_CDS);
1662 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1663 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1664 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1665 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1666 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
1667 && *T_US != NUL)
1668 out_str(T_US);
1669 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
1670 out_str(T_CZH);
1671 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
1672 out_str(T_MR);
1673 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
1674 out_str(T_STS);
1675
1676 /*
1677 * Output the color or start string after bold etc., in case the
1678 * bold etc. override the color setting.
1679 */
1680 if (aep != NULL)
1681 {
PMuncha606f3a2023-11-15 15:35:49 +01001682 if (aep->ae_u.cterm.font > 0 && aep->ae_u.cterm.font < 12)
1683 term_font(aep->ae_u.cterm.font);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001684#ifdef FEAT_TERMGUICOLORS
1685 // When 'termguicolors' is set but fg or bg is unset,
1686 // fall back to the cterm colors. This helps for SpellBad,
1687 // where the GUI uses a red undercurl.
1688 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
1689 {
1690 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
1691 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
1693 else
1694#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001695 if (t_colors > 1)
1696 {
1697 if (aep->ae_u.cterm.fg_color)
1698 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1699 }
1700#ifdef FEAT_TERMGUICOLORS
1701 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001703 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
1704 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
1705 }
1706 else
1707#endif
1708 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001710 if (aep->ae_u.cterm.bg_color)
1711 term_bg_color(aep->ae_u.cterm.bg_color - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001713#ifdef FEAT_TERMGUICOLORS
1714 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1715 {
1716 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1717 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1718 }
1719 else
1720#endif
1721 if (t_colors > 1)
Bram Moolenaara050b942019-12-02 21:35:31 +01001722 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001723 if (aep->ae_u.cterm.ul_color)
1724 term_ul_color(aep->ae_u.cterm.ul_color - 1);
Bram Moolenaara050b942019-12-02 21:35:31 +01001725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001727 if (!IS_CTERM)
1728 {
1729 if (aep->ae_u.term.start != NULL)
1730 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
1732 }
1733}
1734
1735 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001736screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001738 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001739#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001740 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
1743 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001744#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 && termcap_active
1746#endif
1747 )
1748 {
1749#ifdef FEAT_GUI
1750 if (gui.in_use)
1751 {
1752 char buf[20];
1753
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001754 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001755 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 OUT_STR(buf);
1757 }
1758 else
1759#endif
1760 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001761 int is_under;
1762
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001763 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
1765 attrentry_T *aep;
1766
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001767 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 {
1769 /*
1770 * Assume that t_me restores the original colors!
1771 */
1772 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001773 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001774#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001775 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1776 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001777# ifdef FEAT_VTP
1778 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1779# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001780 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001781#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001782 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001783#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001784 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1785 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001786# ifdef FEAT_VTP
1787 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1788# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001789 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001790#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001791 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001793#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1794 if (use_vtp())
1795 {
1796 if (do_ME_fg && do_ME_bg)
1797 do_ME = TRUE;
1798
1799 // FG and BG cannot be separated in T_ME, which is not
1800 // efficient.
1801 if (!do_ME && do_ME_fg)
1802 out_str((char_u *)"\033|39m"); // restore FG
1803 if (!do_ME && do_ME_bg)
1804 out_str((char_u *)"\033|49m"); // restore BG
1805 }
1806 else
1807 {
1808 // Process FG and BG at once.
1809 if (!do_ME)
1810 do_ME = do_ME_fg | do_ME_bg;
1811 }
1812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 else
1815 {
1816 aep = syn_term_attr2entry(screen_attr);
1817 if (aep != NULL && aep->ae_u.term.stop != NULL)
1818 {
1819 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1820 do_ME = TRUE;
1821 else
1822 out_str(aep->ae_u.term.stop);
1823 }
1824 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001825 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 screen_attr = 0;
1827 else
1828 screen_attr = aep->ae_attr;
1829 }
1830
1831 /*
1832 * Often all ending-codes are equal to T_ME. Avoid outputting the
1833 * same sequence several times.
1834 */
1835 if (screen_attr & HL_STANDOUT)
1836 {
1837 if (STRCMP(T_SE, T_ME) == 0)
1838 do_ME = TRUE;
1839 else
1840 out_str(T_SE);
1841 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001842 is_under = (screen_attr & (HL_UNDERCURL
1843 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
1844 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001845 {
1846 if (STRCMP(T_UCE, T_ME) == 0)
1847 do_ME = TRUE;
1848 else
1849 out_str(T_UCE);
1850 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001851 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
1853 if (STRCMP(T_UE, T_ME) == 0)
1854 do_ME = TRUE;
1855 else
1856 out_str(T_UE);
1857 }
1858 if (screen_attr & HL_ITALIC)
1859 {
1860 if (STRCMP(T_CZR, T_ME) == 0)
1861 do_ME = TRUE;
1862 else
1863 out_str(T_CZR);
1864 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001865 if (screen_attr & HL_STRIKETHROUGH)
1866 {
1867 if (STRCMP(T_STE, T_ME) == 0)
1868 do_ME = TRUE;
1869 else
1870 out_str(T_STE);
1871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
1873 out_str(T_ME);
1874
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001875#ifdef FEAT_TERMGUICOLORS
1876 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001878 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001879 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001880 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001881 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02001882 if (cterm_normal_ul_gui_color != INVALCOLOR)
1883 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001884 }
1885 else
1886#endif
1887 {
1888 if (t_colors > 1)
1889 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001890 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001891 if (cterm_normal_fg_color != 0)
1892 term_fg_color(cterm_normal_fg_color - 1);
1893 if (cterm_normal_bg_color != 0)
1894 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02001895 if (cterm_normal_ul_color != 0)
1896 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001897 if (cterm_normal_fg_bold)
1898 out_str(T_MD);
1899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901 }
1902 }
1903 screen_attr = 0;
1904}
1905
1906/*
1907 * Reset the colors for a cterm. Used when leaving Vim.
1908 * The machine specific code may override this again.
1909 */
1910 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001911reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001913 if (!IS_CTERM)
1914 return;
1915
1916 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001917#ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001918 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
1919 || cterm_normal_bg_gui_color != INVALCOLOR)
1920 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001921#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 out_str(T_OP);
1926 screen_attr = -1;
1927 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001928 if (cterm_normal_fg_bold)
1929 {
1930 out_str(T_ME);
1931 screen_attr = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
1933}
1934
1935/*
1936 * Put character ScreenLines["off"] on the screen at position "row" and "col",
1937 * using the attributes from ScreenAttrs["off"].
1938 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001939 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001940screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
1942 int attr;
1943
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001944 // Check for illegal values, just in case (could happen just after
1945 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (row >= screen_Rows || col >= screen_Columns)
1947 return;
1948
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001949 // Outputting a character in the last cell on the screen may scroll the
1950 // screen up. Only do it when the "xn" termcap property is set, otherwise
1951 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01001952 if (*T_XN == NUL
1953 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001955 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 && !cmdmsg_rl
1957#endif
1958 )
1959 {
1960 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001961 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 return;
1963 }
1964
1965 /*
1966 * Stop highlighting first, so it's easier to move the cursor.
1967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (screen_char_attr != 0)
1969 attr = screen_char_attr;
1970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 attr = ScreenAttrs[off];
1972 if (screen_attr != attr)
1973 screen_stop_highlight();
1974
1975 windgoto(row, col);
1976
1977 if (screen_attr != attr)
1978 screen_start_highlight(attr);
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (enc_utf8 && ScreenLinesUC[off] != 0)
1981 {
1982 char_u buf[MB_MAXBYTES + 1];
1983
mikoto2000e20fa592024-04-17 22:06:54 +02001984 if (get_cellwidth(ScreenLinesUC[off]) > 1)
1985 {
1986 // If the width is set to 2 with `setcellwidths`
1987
1988#ifdef FEAT_GUI
1989 if (!gui.in_use)
1990 {
1991#endif
1992 // Clear the two screen cells. If the character is actually
1993 // single width it won't change the second cell.
1994 out_str((char_u *)" ");
1995 term_windgoto(row, col);
1996 screen_cur_col = 9999;
1997#ifdef FEAT_GUI
1998 }
1999#endif
2000 }
2001 else if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002002 {
2003 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002004#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002005 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002006#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002007 )
2008 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002009 // Clear the two screen cells. If the character is actually
2010 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002011 out_str((char_u *)" ");
2012 term_windgoto(row, col);
2013 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002014 // not sure where the cursor is after drawing the ambiguous width
2015 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002016 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002017 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002018 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002020
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002021 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002022 buf[utfc_char2bytes(off, buf)] = NUL;
2023 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002029 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2031 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
2033
2034 screen_cur_col++;
2035}
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037/*
2038 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2039 * on the screen at position 'row' and 'col'.
2040 * The attributes of the first byte is used for all. This is required to
2041 * output the two bytes of a double-byte character with nothing in between.
2042 */
2043 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002044screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002046 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2048 return;
2049
dundargocc57b5bc2022-11-02 13:30:51 +00002050 // Outputting the last character on the screen may scroll the screen up.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002051 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2053 {
2054 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002055 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 return;
2057 }
2058
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002059 // Output the first byte normally (positions the cursor), then write the
2060 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 screen_char(off, row, col);
2062 out_char(ScreenLines[off + 1]);
2063 ++screen_cur_col;
2064}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066/*
2067 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2068 * This uses the contents of ScreenLines[] and doesn't change it.
2069 */
2070 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002071screen_draw_rectangle(
2072 int row,
2073 int col,
2074 int height,
2075 int width,
2076 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
2078 int r, c;
2079 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002080 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002082 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002083 if (ScreenLines == NULL)
2084 return;
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (invert)
2087 screen_char_attr = HL_INVERSE;
2088 for (r = row; r < row + height; ++r)
2089 {
2090 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002091 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 for (c = col; c < col + width; ++c)
2093 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002094 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002096 if (!skip_for_popup(r, c))
2097 screen_char_2(off + c, r, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 ++c;
2099 }
2100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002102 if (!skip_for_popup(r, c))
2103 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002104 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107 }
2108 }
2109 screen_char_attr = 0;
2110}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112/*
2113 * Redraw the characters for a vertically split window.
2114 */
2115 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002116redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117{
2118 int col;
2119 int width;
2120
2121# ifdef FEAT_CLIPBOARD
2122 clip_may_clear_selection(row, end - 1);
2123# endif
2124
2125 if (wp == NULL)
2126 {
2127 col = 0;
2128 width = Columns;
2129 }
2130 else
2131 {
2132 col = wp->w_wincol;
2133 width = wp->w_width;
2134 }
2135 screen_draw_rectangle(row, col, end - row, width, FALSE);
2136}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002138 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002139space_to_screenline(int off, int attr)
2140{
2141 ScreenLines[off] = ' ';
2142 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002143 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002144 if (enc_utf8)
2145 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002146}
2147
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002149 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2150 * to "end_col" (exclusive) with character "c1" in first column followed by
2151 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 */
2153 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002154screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002155 int start_row,
2156 int end_row,
2157 int start_col,
2158 int end_col,
2159 int c1,
2160 int c2,
2161 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002163 int row;
2164 int col;
2165 int off;
2166 int end_off;
2167 int did_delete;
2168 int c;
2169 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002171 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172#endif
2173
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002174 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002176 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 end_col = screen_Columns;
2178 if (ScreenLines == NULL
2179 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002180 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 return;
2182
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002183 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 norm_term = (
2185#ifdef FEAT_GUI
2186 !gui.in_use &&
2187#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002188 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 for (row = start_row; row < end_row; ++row)
2190 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002191 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002192#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002193 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002194#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002195 )
2196 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002197 // When drawing over the right half of a double-wide char clear
2198 // out the left half. When drawing over the left half of a
2199 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002200 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002201 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002202 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002203 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002204 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /*
2207 * Try to use delete-line termcap code, when no attributes or in a
2208 * "normal" terminal, where a bold/italic space is just a
2209 * space.
2210 */
2211 did_delete = FALSE;
2212 if (c2 == ' '
2213 && end_col == Columns
2214 && can_clear(T_CE)
2215 && (attr == 0
2216 || (norm_term
2217 && attr <= HL_ALL
2218 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2219 {
2220 /*
2221 * check if we really need to clear something
2222 */
2223 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002224 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 ++col;
2226
2227 off = LineOffset[row] + col;
2228 end_off = LineOffset[row] + end_col;
2229
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002230 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 if (enc_utf8)
2232 while (off < end_off && ScreenLines[off] == ' '
2233 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2234 ++off;
2235 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 while (off < end_off && ScreenLines[off] == ' '
2237 && ScreenAttrs[off] == 0)
2238 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002239 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
2241 col = off - LineOffset[row];
2242 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002243 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002245 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002247 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002249 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 ++off;
2251 }
2252 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002253 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
2255
2256 off = LineOffset[row] + start_col;
2257 c = c1;
2258 for (col = start_col; col < end_col; ++col)
2259 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002260 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002261 || (enc_utf8 && (int)ScreenLinesUC[off]
2262 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002264 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265#if defined(FEAT_GUI) || defined(UNIX)
2266 || force_next
2267#endif
2268 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002269 // Skip if under a(nother) popup.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002270 && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002273 // The bold trick may make a single row of pixels appear in
2274 // the next character. When a bold character is removed, the
2275 // next character should be redrawn too. This happens for our
2276 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (
2278# ifdef FEAT_GUI
2279 gui.in_use
2280# endif
2281# if defined(FEAT_GUI) && defined(UNIX)
2282 ||
2283# endif
2284# ifdef UNIX
2285 term_is_xterm
2286# endif
2287 )
2288 {
2289 if (ScreenLines[off] != ' '
2290 && (ScreenAttrs[off] > HL_ALL
2291 || ScreenAttrs[off] & HL_BOLD))
2292 force_next = TRUE;
2293 else
2294 force_next = FALSE;
2295 }
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002296#endif // FEAT_GUI || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (enc_utf8)
2299 {
2300 if (c >= 0x80)
2301 {
2302 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002303 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 else
2306 ScreenLinesUC[off] = 0;
2307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 ScreenAttrs[off] = attr;
2309 if (!did_delete || c != ' ')
2310 screen_char(off, row, col);
2311 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002312 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ++off;
2314 if (col == start_col)
2315 {
2316 if (did_delete)
2317 break;
2318 c = c2;
2319 }
2320 }
2321 if (end_col == Columns)
2322 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002323 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002326 if (start_col == 0 && end_col == Columns
2327 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002328 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002330 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332 }
2333}
2334
2335/*
2336 * Check if there should be a delay. Used before clearing or redrawing the
2337 * screen or the command line.
2338 */
2339 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002340check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
2342 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2343 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002344 && emsg_silent == 0
2345 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
2347 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002348 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 emsg_on_display = FALSE;
2350 if (check_msg_scroll)
2351 msg_scroll = FALSE;
2352 }
2353}
2354
2355/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002356 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2357 */
2358 static void
2359clear_TabPageIdxs(void)
2360{
2361 int scol;
2362
2363 for (scol = 0; scol < Columns; ++scol)
2364 TabPageIdxs[scol] = 0;
2365}
2366
2367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002369 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 * Returns TRUE if there is a valid screen to write to.
2371 * Returns FALSE when starting up and screen not initialized yet.
2372 */
2373 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002374screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002376 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return (ScreenLines != NULL);
2378}
2379
2380/*
2381 * Resize the shell to Rows and Columns.
2382 * Allocate ScreenLines[] and associated items.
2383 *
2384 * There may be some time between setting Rows and Columns and (re)allocating
2385 * ScreenLines[]. This happens when starting up and when (manually) changing
2386 * the shell size. Always use screen_Rows and screen_Columns to access items
2387 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2388 * final size of the shell is needed.
2389 */
2390 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002391screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
2393 int new_row, old_row;
2394#ifdef FEAT_GUI
2395 int old_Rows;
2396#endif
2397 win_T *wp;
2398 int outofmem = FALSE;
2399 int len;
2400 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002402 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002405 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 unsigned *new_LineOffset;
2407 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002408 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002409#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002410 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002411 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002412 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002413#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002414 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002415 static int entered = FALSE; // avoid recursiveness
2416 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002417 int retry_count = 0;
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002418 int found_null;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002420retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 /*
2422 * Allocation of the screen buffers is done only when the size changes and
2423 * when Rows and Columns have been set and we have started doing full
2424 * screen stuff.
2425 */
2426 if ((ScreenLines != NULL
2427 && Rows == screen_Rows
2428 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 && enc_utf8 == (ScreenLinesUC != NULL)
2430 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002431 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 || Rows == 0
2433 || Columns == 0
2434 || (!full_screen && ScreenLines == NULL))
2435 return;
2436
2437 /*
2438 * It's possible that we produce an out-of-memory message below, which
2439 * will cause this function to be called again. To break the loop, just
2440 * return here.
2441 */
2442 if (entered)
2443 return;
2444 entered = TRUE;
2445
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002446 /*
2447 * Note that the window sizes are updated before reallocating the arrays,
2448 * thus we must not redraw here!
2449 */
2450 ++RedrawingDisabled;
2451
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002452 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002454#ifdef FEAT_GUI_HAIKU
2455 vim_lock_screen(); // be safe, put it here
2456#endif
2457
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002458 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 /*
2461 * We're changing the size of the screen.
2462 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2463 * - Move lines from the old arrays into the new arrays, clear extra
2464 * lines (unless the screen is going to be cleared).
2465 * - Free the old arrays.
2466 *
2467 * If anything fails, make ScreenLines NULL, so we don't do anything!
2468 * Continuing with the old ScreenLines may result in a crash, because the
2469 * size is wrong.
2470 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002471 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 win_free_lsize(wp);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002473 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002474 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002475 win_free_lsize(aucmd_win[i].auc_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002476#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002477 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002478 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002479 win_free_lsize(wp);
2480 // tab-local popup windows
2481 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002482 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002483 win_free_lsize(wp);
2484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002486 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002487 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (enc_utf8)
2489 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002490 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002491 for (int i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002492 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2493 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002496 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2497 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
dundargocc57b5bc2022-11-02 13:30:51 +00002498 // Clear ScreenCols to avoid a warning for uninitialized memory in
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002499 // jump_to_mouse().
2500 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002501 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2502 new_LineWraps = LALLOC_MULT(char_u, Rows);
2503 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002504#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002505 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002506 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002507 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002510 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 if (win_alloc_lines(wp) == FAIL)
2513 {
2514 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002515 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00002518 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002519 if (aucmd_win[i].auc_win != NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002520 && aucmd_win[i].auc_win->w_lines == NULL
2521 && win_alloc_lines(aucmd_win[i].auc_win) == FAIL)
2522 {
2523 outofmem = TRUE;
2524 break;
2525 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002526#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002527 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002528 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002529 if (win_alloc_lines(wp) == FAIL)
2530 {
2531 outofmem = TRUE;
2532 goto give_up;
2533 }
2534 // tab-local popup windows
2535 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002536 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002537 if (win_alloc_lines(wp) == FAIL)
2538 {
2539 outofmem = TRUE;
2540 goto give_up;
2541 }
2542#endif
2543
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002544give_up:
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002545 found_null = FALSE;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002546 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002547 if (new_ScreenLinesC[i] == NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002548 {
2549 found_null = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002550 break;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (new_ScreenLines == NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002553 || (enc_utf8 && (new_ScreenLinesUC == NULL || found_null))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002556 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 || new_LineOffset == NULL
2558 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002559 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002560#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002561 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002562 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002563 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 || outofmem)
2566 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002567 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002568 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002569 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002570 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2571
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002572 // Remember we did this to avoid getting outofmem messages over
2573 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002574 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002575 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002576 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002577 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002578 for (int i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002579 VIM_CLEAR(new_ScreenLinesC[i]);
2580 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002581 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002582 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002583 VIM_CLEAR(new_LineOffset);
2584 VIM_CLEAR(new_LineWraps);
2585 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002586#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002587 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002588 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002589 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592 else
2593 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002594 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 for (new_row = 0; new_row < Rows; ++new_row)
2597 {
2598 new_LineOffset[new_row] = new_row * Columns;
2599 new_LineWraps[new_row] = FALSE;
2600
Olaf Seibertfd472652024-02-01 21:11:16 +01002601 (void)vim_memset(new_ScreenLines + new_row * Columns,
2602 ' ', (size_t)Columns * sizeof(schar_T));
2603 if (enc_utf8)
2604 {
2605 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2606 0, (size_t)Columns * sizeof(u8char_T));
2607 for (int i = 0; i < p_mco; ++i)
2608 (void)vim_memset(new_ScreenLinesC[i]
2609 + new_row * Columns,
2610 0, (size_t)Columns * sizeof(u8char_T));
2611 }
2612 if (enc_dbcs == DBCS_JPNU)
2613 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2614 0, (size_t)Columns * sizeof(schar_T));
2615 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2616 0, (size_t)Columns * sizeof(sattr_T));
2617 (void)vim_memset(new_ScreenCols + new_row * Columns,
2618 0, (size_t)Columns * sizeof(colnr_T));
2619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 /*
2621 * If the screen is not going to be cleared, copy as much as
2622 * possible from the old screen to the new one and clear the rest
2623 * (used when resizing the window at the "--more--" prompt or when
2624 * executing an external command, for the GUI).
2625 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002626 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002629 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 if (screen_Columns < Columns)
2632 len = screen_Columns;
2633 else
2634 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002635 // When switching to utf-8 don't copy characters, they
2636 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002637 if (!(enc_utf8 && ScreenLinesUC == NULL)
2638 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002639 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2640 ScreenLines + LineOffset[old_row],
2641 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002642 if (enc_utf8 && ScreenLinesUC != NULL
2643 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {
2645 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2646 ScreenLinesUC + LineOffset[old_row],
2647 (size_t)len * sizeof(u8char_T));
Bram Moolenaare76062c2022-11-28 18:51:43 +00002648 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002649 mch_memmove(new_ScreenLinesC[i]
2650 + new_LineOffset[new_row],
2651 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 (size_t)len * sizeof(u8char_T));
2653 }
2654 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2655 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2656 ScreenLines2 + LineOffset[old_row],
2657 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2659 ScreenAttrs + LineOffset[old_row],
2660 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002661 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2662 ScreenAttrs + LineOffset[old_row],
2663 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665 }
2666 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002667 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002669
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002670#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002671 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2672 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002676 free_screenlines();
2677
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002678 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002681 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002682 ScreenLinesC[i] = new_ScreenLinesC[i];
2683 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002686 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 LineOffset = new_LineOffset;
2688 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002689 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002690#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002691 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002692 popup_mask_next = new_popup_mask_next;
2693 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002694 popup_mask_refresh = TRUE;
2695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002697 // It's important that screen_Rows and screen_Columns reflect the actual
2698 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699#ifdef FEAT_GUI
2700 old_Rows = screen_Rows;
2701#endif
2702 screen_Rows = Rows;
2703 screen_Columns = Columns;
2704
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002705 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002706 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002707 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708#ifdef FEAT_GUI
2709 else if (gui.in_use
2710 && !gui.starting
2711 && ScreenLines != NULL
2712 && old_Rows != Rows)
2713 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002714 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2715
2716 // Adjust the position of the cursor, for when executing an external
2717 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002718 if (msg_row >= Rows) // Rows got smaller
2719 msg_row = Rows - 1; // put cursor at last row
2720 else if (Rows > old_Rows) // Rows got bigger
2721 msg_row += Rows - old_Rows; // put cursor in same place
2722 if (msg_col >= Columns) // Columns got smaller
2723 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002726 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002728#ifdef FEAT_GUI_HAIKU
2729 vim_unlock_screen();
2730#endif
2731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 entered = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002733 if (RedrawingDisabled > 0)
2734 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002735
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002736 /*
2737 * Do not apply autocommands more than 3 times to avoid an endless loop
2738 * in case applying autocommands always changes Rows or Columns.
2739 */
2740 if (starting == 0 && ++retry_count <= 3)
2741 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002742 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002743 // In rare cases, autocommands may have altered Rows or Columns,
2744 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002745 goto retry;
2746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747}
2748
2749 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002750free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002751{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002752 int i;
2753
Bram Moolenaar33796b32019-06-08 16:01:13 +02002754 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002755 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002756 VIM_CLEAR(ScreenLinesC[i]);
2757 VIM_CLEAR(ScreenLines2);
2758 VIM_CLEAR(ScreenLines);
2759 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002760 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002761 VIM_CLEAR(LineOffset);
2762 VIM_CLEAR(LineWraps);
2763 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002764#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002765 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002766 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002767 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002768#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002769}
2770
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002771/*
2772 * Clear the screen.
2773 * May delay if there is something the user should read.
2774 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002775 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002776 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002777 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01002778 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002779screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002782 screenalloc(FALSE); // allocate screen buffers if size changed
2783 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002784}
2785
2786/*
2787 * Do not clear the screen but mark everything for redraw.
2788 */
2789 void
2790redraw_as_cleared(void)
2791{
2792 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793}
2794
Bram Moolenaar838b7462022-09-26 15:19:56 +01002795 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002796screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002799 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
2801 if (starting == NO_SCREEN || ScreenLines == NULL
2802#ifdef FEAT_GUI
2803 || (gui.in_use && gui.starting)
2804#endif
2805 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01002806 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808#ifdef FEAT_GUI
2809 if (!gui.in_use)
2810#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002811 screen_attr = -1; // force setting the Normal colors
2812 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
2814#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002815 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 clip_scroll_selection(9999);
2817#endif
2818
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002819 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 for (i = 0; i < Rows; ++i)
2821 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002822 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 LineWraps[i] = FALSE;
2824 }
2825
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002826 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002828 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002829 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002831 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
2833 else
2834 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002835 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 for (i = 0; i < Rows; ++i)
2837 lineinvalid(LineOffset[i], (int)Columns);
2838 clear_cmdline = TRUE;
2839 }
2840
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002841 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002843 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002845 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002846 if (must_redraw == UPD_CLEAR) // no need to clear again
2847 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002848 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002850#ifdef FEAT_PROP_POPUP
2851 popup_redraw_all(); // redraw all popup windows
2852#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002853 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002855 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 msg_didany = FALSE;
2857 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002858
2859 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860}
2861
2862/*
2863 * Clear one line in ScreenLines.
2864 */
2865 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002866lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
2868 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 if (enc_utf8)
2870 (void)vim_memset(ScreenLinesUC + off, 0,
2871 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002872 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002873 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874}
2875
2876/*
2877 * Mark one line in ScreenLines invalid by setting the attributes to an
2878 * invalid value.
2879 */
2880 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002881lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002884 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885}
2886
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02002888 * To be called when characters were sent to the terminal directly, outputting
2889 * test on "screen_lnum".
2890 */
2891 void
2892line_was_clobbered(int screen_lnum)
2893{
2894 lineinvalid(LineOffset[screen_lnum], (int)Columns);
2895}
2896
2897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 * Copy part of a Screenline for vertically split window "wp".
2899 */
2900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002901linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
2903 unsigned off_to = LineOffset[to] + wp->w_wincol;
2904 unsigned off_from = LineOffset[from] + wp->w_wincol;
2905
2906 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
2907 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 if (enc_utf8)
2909 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002910 int i;
2911
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
2913 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002914 for (i = 0; i < p_mco; ++i)
2915 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
2916 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 if (enc_dbcs == DBCS_JPNU)
2919 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
2920 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
2922 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002923 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
2924 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
2927/*
2928 * Return TRUE if clearing with term string "p" would work.
2929 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02002930 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 */
2932 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002933can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
2935 return (*p != NUL && (t_colors <= 1
2936#ifdef FEAT_GUI
2937 || gui.in_use
2938#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002939#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002940 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02002941 || (!p_tgc && cterm_normal_bg_color == 0)
2942#else
2943 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002944#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002945 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002946#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002947 && !(p == T_CE && popup_visible)
2948#endif
2949 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
2953 * Reset cursor position. Use whenever cursor was moved because of outputting
2954 * something directly to the screen (shell commands) or a terminal control
2955 * code.
2956 */
2957 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002958screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
2960 screen_cur_row = screen_cur_col = 9999;
2961}
2962
2963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 * Move the cursor to position "row","col" in the screen.
2965 * This tries to find the most efficient way to move, minimizing the number of
2966 * characters sent to the terminal.
2967 */
2968 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002969windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002971 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 int i;
2973 int plan;
2974 int cost;
2975 int wouldbe_col;
2976 int noinvcurs;
2977 char_u *bs;
2978 int goto_cost;
2979 int attr;
2980
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002981#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
2982#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
2984#define PLAN_LE 1
2985#define PLAN_CR 2
2986#define PLAN_NL 3
2987#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002988 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 if (ScreenLines == NULL)
2990 return;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002991 if (col == screen_cur_col && row == screen_cur_row)
2992 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002994 // Check for valid position.
2995 if (row < 0) // window without text lines?
2996 row = 0;
2997 if (row >= screen_Rows)
2998 row = screen_Rows - 1;
2999 if (col >= screen_Columns)
3000 col = screen_Columns - 1;
3001
3002 // check if no cursor movement is allowed in highlight mode
3003 if (screen_attr && *T_MS == NUL)
3004 noinvcurs = HIGHL_COST;
3005 else
3006 noinvcurs = 0;
3007 goto_cost = GOTO_COST + noinvcurs;
3008
3009 /*
3010 * Plan how to do the positioning:
3011 * 1. Use CR to move it to column 0, same row.
3012 * 2. Use T_LE to move it a few columns to the left.
3013 * 3. Use NL to move a few lines down, column 0.
3014 * 4. Move a few columns to the right with T_ND or by writing chars.
3015 *
3016 * Don't do this if the cursor went beyond the last column, the cursor
3017 * position is unknown then (some terminals wrap, some don't )
3018 *
3019 * First check if the highlighting attributes allow us to write
3020 * characters to move the cursor to the right.
3021 */
3022 if (row >= screen_cur_row && screen_cur_col < Columns)
3023 {
3024 /*
3025 * If the cursor is in the same row, bigger col, we can use CR
3026 * or T_LE.
3027 */
3028 bs = NULL; // init for GCC
3029 attr = screen_attr;
3030 if (row == screen_cur_row && col < screen_cur_col)
3031 {
3032 // "le" is preferred over "bc", because "bc" is obsolete
3033 if (*T_LE)
3034 bs = T_LE; // "cursor left"
3035 else
3036 bs = T_BC; // "backspace character (old)
3037 if (*bs)
3038 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3039 else
3040 cost = 999;
3041 if (col + 1 < cost) // using CR is less characters
3042 {
3043 plan = PLAN_CR;
3044 wouldbe_col = 0;
3045 cost = 1; // CR is just one character
3046 }
3047 else
3048 {
3049 plan = PLAN_LE;
3050 wouldbe_col = col;
3051 }
3052 if (noinvcurs) // will stop highlighting
3053 {
3054 cost += noinvcurs;
3055 attr = 0;
3056 }
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
3059 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003060 * If the cursor is above where we want to be, we can use CR LF.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003062 else if (row > screen_cur_row)
3063 {
3064 plan = PLAN_NL;
3065 wouldbe_col = 0;
3066 cost = (row - screen_cur_row) * 2; // CR LF
3067 if (noinvcurs) // will stop highlighting
3068 {
3069 cost += noinvcurs;
3070 attr = 0;
3071 }
3072 }
3073
3074 /*
3075 * If the cursor is in the same row, smaller col, just use write.
3076 */
3077 else
3078 {
3079 plan = PLAN_WRITE;
3080 wouldbe_col = screen_cur_col;
3081 cost = 0;
3082 }
3083
3084 /*
3085 * Check if any characters that need to be written have the
3086 * correct attributes. Also avoid UTF-8 characters.
3087 */
3088 i = col - wouldbe_col;
3089 if (i > 0)
3090 cost += i;
3091 if (cost < goto_cost && i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
3093 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003094 * Check if the attributes are correct without additionally
3095 * stopping highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003097 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3098 while (i && *p++ == attr)
3099 --i;
3100 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
3102 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003103 * Try if it works when highlighting is stopped here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003105 if (*--p == 0)
3106 {
3107 cost += noinvcurs;
3108 while (i && *p++ == 0)
3109 --i;
3110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 if (i != 0)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003112 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003114 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003116 // Don't use an UTF-8 char for positioning, it's slow.
3117 for (i = wouldbe_col; i < col; ++i)
3118 if (ScreenLinesUC[LineOffset[row] + i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003120 cost = 999;
3121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003123 }
3124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003126 /*
3127 * We can do it without term_windgoto()!
3128 */
3129 if (cost < goto_cost)
3130 {
3131 if (plan == PLAN_LE)
3132 {
3133 if (noinvcurs)
3134 screen_stop_highlight();
3135 while (screen_cur_col > col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003137 out_str(bs);
3138 --screen_cur_col;
3139 }
3140 }
3141 else if (plan == PLAN_CR)
3142 {
3143 if (noinvcurs)
3144 screen_stop_highlight();
3145 out_char('\r');
3146 screen_cur_col = 0;
3147 }
3148 else if (plan == PLAN_NL)
3149 {
3150 if (noinvcurs)
3151 screen_stop_highlight();
3152 while (screen_cur_row < row)
3153 {
3154 out_char('\n');
3155 ++screen_cur_row;
3156 }
3157 screen_cur_col = 0;
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003160 i = col - screen_cur_col;
3161 if (i > 0)
3162 {
3163 /*
3164 * Use cursor-right if it's one character only. Avoids
3165 * removing a line of pixels from the last bold char, when
3166 * using the bold trick in the GUI.
3167 */
3168 if (T_ND[0] != NUL && T_ND[1] == NUL)
3169 {
3170 while (i-- > 0)
3171 out_char(*T_ND);
3172 }
3173 else
3174 {
3175 int off;
3176
3177 off = LineOffset[row] + screen_cur_col;
3178 while (i-- > 0)
3179 {
3180 if (ScreenAttrs[off] != screen_attr)
3181 screen_stop_highlight();
3182 out_flush_check();
3183 out_char(ScreenLines[off]);
3184 if (enc_dbcs == DBCS_JPNU
3185 && ScreenLines[off] == 0x8e)
3186 out_char(ScreenLines2[off]);
3187 ++off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
3189 }
3190 }
3191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003193 else
3194 cost = 999;
3195
3196 if (cost >= goto_cost)
3197 {
3198 if (noinvcurs)
3199 screen_stop_highlight();
3200 if (row == screen_cur_row && (col > screen_cur_col)
3201 && *T_CRI != NUL)
3202 term_cursor_right(col - screen_cur_col);
3203 else
3204 term_windgoto(row, col);
3205 }
3206 screen_cur_row = row;
3207 screen_cur_col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208}
3209
3210/*
3211 * Set cursor to its position in the current window.
3212 */
3213 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003214setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003216 setcursor_mayforce(FALSE);
3217}
3218
3219/*
3220 * Set cursor to its position in the current window.
3221 * When "force" is TRUE also when not redrawing.
3222 */
3223 void
3224setcursor_mayforce(int force)
3225{
3226 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
3228 validate_cursor();
3229 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003230 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003232 // With 'rightleft' set and the cursor on a double-wide
3233 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003234 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3235 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003236 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003237 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238#endif
3239 curwin->w_wcol));
3240 }
3241}
3242
3243
3244/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003245 * Insert 'line_count' lines at 'row' in window 'wp'.
3246 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3247 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 * scrolling.
3249 * Returns FAIL if the lines are not inserted, OK for success.
3250 */
3251 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003252win_ins_lines(
3253 win_T *wp,
3254 int row,
3255 int line_count,
3256 int invalid,
3257 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
3259 int did_delete;
3260 int nextrow;
3261 int lastrow;
3262 int retval;
3263
3264 if (invalid)
3265 wp->w_lines_valid = 0;
3266
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003267 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 if (wp->w_height < 5)
3269 return FAIL;
3270
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003271 // with the popup menu visible this might not work correctly
3272 if (pum_visible())
3273 return FAIL;
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 if (line_count > wp->w_height - row)
3276 line_count = wp->w_height - row;
3277
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003278 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (retval != MAYBE)
3280 return retval;
3281
3282 /*
3283 * If there is a next window or a status line, we first try to delete the
3284 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003285 * If this fails and there are following windows, don't do anything to
3286 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 */
3288 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (wp->w_next != NULL || wp->w_status_height)
3290 {
3291 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003292 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 did_delete = TRUE;
3294 else if (wp->w_next)
3295 return FAIL;
3296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 /*
3298 * if no lines deleted, blank the lines that will end up below the window
3299 */
3300 if (!did_delete)
3301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003304 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 lastrow = nextrow + line_count;
3306 if (lastrow > Rows)
3307 lastrow = Rows;
3308 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003309 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 ' ', ' ', 0);
3311 }
3312
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003313 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 == FAIL)
3315 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003316 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (did_delete)
3318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 win_rest_invalid(W_NEXT(wp));
3321 }
3322 return FAIL;
3323 }
3324
3325 return OK;
3326}
3327
3328/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003329 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3331 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3332 * scrolling
3333 * Return OK for success, FAIL if the lines are not deleted.
3334 */
3335 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003336win_del_lines(
3337 win_T *wp,
3338 int row,
3339 int line_count,
3340 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003341 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003342 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 int retval;
3345
3346 if (invalid)
3347 wp->w_lines_valid = 0;
3348
3349 if (line_count > wp->w_height - row)
3350 line_count = wp->w_height - row;
3351
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003352 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (retval != MAYBE)
3354 return retval;
3355
3356 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003357 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 return FAIL;
3359
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 /*
3361 * If there are windows or status lines below, try to put them at the
3362 * correct place. If we can't do that, they have to be redrawn.
3363 */
3364 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3365 {
3366 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003367 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
3369 wp->w_redr_status = TRUE;
3370 win_rest_invalid(wp->w_next);
3371 }
3372 }
3373 /*
3374 * If this is the last window and there is no status line, redraw the
3375 * command line later.
3376 */
3377 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 redraw_cmdline = TRUE;
3379 return OK;
3380}
3381
3382/*
3383 * Common code for win_ins_lines() and win_del_lines().
3384 * Returns OK or FAIL when the work has been done.
3385 * Returns MAYBE when not finished yet.
3386 */
3387 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003388win_do_lines(
3389 win_T *wp,
3390 int row,
3391 int line_count,
3392 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003393 int del,
3394 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
3396 int retval;
3397
3398 if (!redrawing() || line_count <= 0)
3399 return FAIL;
3400
Bram Moolenaar33796b32019-06-08 16:01:13 +02003401 // When inserting lines would result in loss of command output, just redraw
3402 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003403 if (no_win_do_lines_ins && !del)
3404 return FAIL;
3405
Bram Moolenaar33796b32019-06-08 16:01:13 +02003406 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003407 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003409 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003410 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 return FAIL;
3412 }
3413
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003414#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003415 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003416 if (popup_visible)
3417 return FAIL;
3418#endif
3419
3420 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (row + line_count >= wp->w_height)
3422 {
3423 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003424 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 ' ', ' ', 0);
3426 return OK;
3427 }
3428
3429 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003430 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003432 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003434 if (!no_win_do_lines_ins)
3435 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437 /*
3438 * If the terminal can set a scroll region, use that.
3439 * Always do this in a vertically split window. This will redraw from
3440 * ScreenLines[] when t_CV isn't defined. That's faster than using
3441 * win_line().
3442 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003443 * a character in the lower right corner of the scroll region may cause a
3444 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003446 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 scroll_region_set(wp, row);
3450 if (del)
3451 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003452 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 else
3454 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003455 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 scroll_region_reset();
3458 return retval;
3459 }
3460
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003461 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 return MAYBE;
3465}
3466
3467/*
3468 * window 'wp' and everything after it is messed up, mark it for redraw
3469 */
3470 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003471win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003475 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 wp->w_redr_status = TRUE;
3477 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479 redraw_cmdline = TRUE;
3480}
3481
3482/*
3483 * The rest of the routines in this file perform screen manipulations. The
3484 * given operation is performed physically on the screen. The corresponding
3485 * change is also made to the internal screen image. In this way, the editor
3486 * anticipates the effect of editing changes on the appearance of the screen.
3487 * That way, when we call screenupdate a complete redraw isn't usually
3488 * necessary. Another advantage is that we can keep adding code to anticipate
3489 * screen changes, and in the meantime, everything still works.
3490 */
3491
3492/*
3493 * types for inserting or deleting lines
3494 */
3495#define USE_T_CAL 1
3496#define USE_T_CDL 2
3497#define USE_T_AL 3
3498#define USE_T_CE 4
3499#define USE_T_DL 5
3500#define USE_T_SR 6
3501#define USE_NL 7
3502#define USE_T_CD 8
3503#define USE_REDRAW 9
3504
3505/*
3506 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003507 * "end" is the line after the scrolled part. Normally it is Rows.
3508 * When scrolling region used "off" is the offset from the top for the region.
3509 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 *
3511 * return FAIL for failure, OK for success.
3512 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003513 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003514screen_ins_lines(
3515 int off,
3516 int row,
3517 int line_count,
3518 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003519 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003520 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 int i;
3523 int j;
3524 unsigned temp;
3525 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003526 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 int type;
3528 int result_empty;
3529 int can_ce = can_clear(T_CE);
3530
3531 /*
3532 * FAIL if
3533 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 * - the line count is less than one
3535 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003536 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003537 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003538 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003540 if (!screen_valid(TRUE)
3541 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003542 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003543#ifdef FEAT_CLIPBOARD
3544 || (clip_star.state != SELECT_CLEARED
3545 && redrawing_for_callback > 0)
3546#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003547#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003548 || popup_visible
3549#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003550 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 return FAIL;
3552
3553 /*
3554 * There are seven ways to insert lines:
3555 * 0. When in a vertically split window and t_CV isn't set, redraw the
3556 * characters from ScreenLines[].
3557 * 1. Use T_CD (clear to end of display) if it exists and the result of
3558 * the insert is just empty lines
3559 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3560 * present or line_count > 1. It looks better if we do all the inserts
3561 * at once.
3562 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3563 * insert is just empty lines and T_CE is not present or line_count >
3564 * 1.
3565 * 4. Use T_AL (insert line) if it exists.
3566 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3567 * just empty lines.
3568 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3569 * just empty lines.
3570 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3571 * the 'da' flag is not set or we have clear line capability.
3572 * 8. redraw the characters from ScreenLines[].
3573 *
3574 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3575 * the scrollbar for the window. It does have insert line, use that if it
3576 * exists.
3577 */
3578 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003580 {
3581 // Avoid that lines are first cleared here and then redrawn, which
3582 // results in many characters updated twice. This happens with CTRL-F
3583 // in a vertically split window. With line-by-line scrolling
3584 // USE_REDRAW should be faster.
3585 if (line_count > 3)
3586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003588 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003589 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 type = USE_T_CD;
3591 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3592 type = USE_T_CAL;
3593 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3594 type = USE_T_CDL;
3595 else if (*T_AL != NUL)
3596 type = USE_T_AL;
3597 else if (can_ce && result_empty)
3598 type = USE_T_CE;
3599 else if (*T_DL != NUL && result_empty)
3600 type = USE_T_DL;
3601 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3602 type = USE_T_SR;
3603 else
3604 return FAIL;
3605
3606 /*
3607 * For clearing the lines screen_del_lines() is used. This will also take
3608 * care of t_db if necessary.
3609 */
3610 if (type == USE_T_CD || type == USE_T_CDL ||
3611 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003612 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 /*
3615 * If text is retained below the screen, first clear or delete as many
3616 * lines at the bottom of the window as are about to be inserted so that
3617 * the deleted lines won't later surface during a screen_del_lines.
3618 */
3619 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003620 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003623 // Remove a modeless selection when inserting lines halfway the screen
3624 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003625 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003626 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 else
3628 clip_scroll_selection(-line_count);
3629#endif
3630
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003631#ifdef FEAT_GUI_HAIKU
3632 vim_lock_screen();
3633#endif
3634
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003636 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3637 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003638 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#endif
3640
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003641 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3642 cursor_col = wp->w_wincol;
3643
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003644 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 cursor_row = row;
3646 else
3647 cursor_row = row + off;
3648
3649 /*
3650 * Shift LineOffset[] line_count down to reflect the inserted lines.
3651 * Clear the inserted lines in ScreenLines[].
3652 */
3653 row += off;
3654 end += off;
3655 for (i = 0; i < line_count; ++i)
3656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (wp != NULL && wp->w_width != Columns)
3658 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003659 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 j = end - 1 - i;
3661 while ((j -= line_count) >= row)
3662 linecopy(j + line_count, j, wp);
3663 j += line_count;
3664 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003665 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3666 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 else
3668 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3669 LineWraps[j] = FALSE;
3670 }
3671 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
3673 j = end - 1 - i;
3674 temp = LineOffset[j];
3675 while ((j -= line_count) >= row)
3676 {
3677 LineOffset[j + line_count] = LineOffset[j];
3678 LineWraps[j + line_count] = LineWraps[j];
3679 }
3680 LineOffset[j + line_count] = temp;
3681 LineWraps[j + line_count] = FALSE;
3682 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003683 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 else
3685 lineinvalid(temp, (int)Columns);
3686 }
3687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003689#ifdef FEAT_GUI_HAIKU
3690 vim_unlock_screen();
3691#endif
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003694 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003695 if (clear_attr != 0)
3696 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003698 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 if (type == USE_REDRAW)
3700 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003701 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
3703 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003704 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706 else
3707 {
3708 for (i = 0; i < line_count; i++)
3709 {
3710 if (type == USE_T_AL)
3711 {
3712 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003713 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 out_str(T_AL);
3715 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003716 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003718 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 }
3721
3722 /*
3723 * With scroll-reverse and 'da' flag set we need to clear the lines that
3724 * have been scrolled down into the region.
3725 */
3726 if (type == USE_T_SR && *T_DA)
3727 {
3728 for (i = 0; i < line_count; ++i)
3729 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003730 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003732 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
3734 }
3735
3736#ifdef FEAT_GUI
3737 gui_can_update_cursor();
3738 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003739 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740#endif
3741 return OK;
3742}
3743
3744/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003745 * Delete lines on the screen and update ScreenLines[].
3746 * "end" is the line after the scrolled part. Normally it is Rows.
3747 * When scrolling region used "off" is the offset from the top for the region.
3748 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 *
3750 * Return OK for success, FAIL if the lines are not deleted.
3751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003753screen_del_lines(
3754 int off,
3755 int row,
3756 int line_count,
3757 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003758 int force, // even when line_count > p_ttyscroll
3759 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003760 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
3762 int j;
3763 int i;
3764 unsigned temp;
3765 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003766 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003768 int result_empty; // result is empty until end of region
3769 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 int type;
3771
3772 /*
3773 * FAIL if
3774 * - there is no valid screen
3775 * - the screen has to be redrawn completely
3776 * - the line count is less than one
3777 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003778 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003779 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003781 if (!screen_valid(TRUE)
3782 || line_count <= 0
3783 || (!force && line_count > p_ttyscroll)
3784 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003785#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003786 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003787#endif
3788 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 return FAIL;
3790
3791 /*
3792 * Check if the rest of the current region will become empty.
3793 */
3794 result_empty = row + line_count >= end;
3795
3796 /*
3797 * We can delete lines only when 'db' flag not set or when 'ce' option
3798 * available.
3799 */
3800 can_delete = (*T_DB == NUL || can_clear(T_CE));
3801
3802 /*
3803 * There are six ways to delete lines:
3804 * 0. When in a vertically split window and t_CV isn't set, redraw the
3805 * characters from ScreenLines[].
3806 * 1. Use T_CD if it exists and the result is empty.
3807 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3808 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3809 * none of the other ways work.
3810 * 4. Use T_CE (erase line) if the result is empty.
3811 * 5. Use T_DL (delete line) if it exists.
3812 * 6. redraw the characters from ScreenLines[].
3813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003815 {
3816 // Avoid that lines are first cleared here and then redrawn, which
3817 // results in many characters updated twice. This happens with CTRL-F
3818 // in a vertically split window. With line-by-line scrolling
3819 // USE_REDRAW should be faster.
3820 if (line_count > 3)
3821 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003823 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003824 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 else if (row == 0 && (
3827#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003828 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3829 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 line_count == 1 ||
3831#endif
3832 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 type = USE_NL;
3834 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3835 type = USE_T_CDL;
3836 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003837 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 type = USE_T_CE;
3839 else if (*T_DL != NUL && can_delete)
3840 type = USE_T_DL;
3841 else if (*T_CDL != NUL && can_delete)
3842 type = USE_T_CDL;
3843 else
3844 return FAIL;
3845
3846#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003847 // Remove a modeless selection when deleting lines halfway the screen or
3848 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003849 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003850 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 else
3852 clip_scroll_selection(line_count);
3853#endif
3854
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003855#ifdef FEAT_GUI_HAIKU
3856 vim_lock_screen();
3857#endif
3858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003860 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3861 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003862 gui_dont_update_cursor(gui.cursor_row >= row + off
3863 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864#endif
3865
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003866 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3867 cursor_col = wp->w_wincol;
3868
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003869 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
3871 cursor_row = row;
3872 cursor_end = end;
3873 }
3874 else
3875 {
3876 cursor_row = row + off;
3877 cursor_end = end + off;
3878 }
3879
3880 /*
3881 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3882 * Clear the inserted lines in ScreenLines[].
3883 */
3884 row += off;
3885 end += off;
3886 for (i = 0; i < line_count; ++i)
3887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (wp != NULL && wp->w_width != Columns)
3889 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003890 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 j = row + i;
3892 while ((j += line_count) <= end - 1)
3893 linecopy(j - line_count, j, wp);
3894 j -= line_count;
3895 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003896 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3897 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
3899 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3900 LineWraps[j] = FALSE;
3901 }
3902 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003904 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 j = row + i;
3906 temp = LineOffset[j];
3907 while ((j += line_count) <= end - 1)
3908 {
3909 LineOffset[j - line_count] = LineOffset[j];
3910 LineWraps[j - line_count] = LineWraps[j];
3911 }
3912 LineOffset[j - line_count] = temp;
3913 LineWraps[j - line_count] = FALSE;
3914 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003915 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 else
3917 lineinvalid(temp, (int)Columns);
3918 }
3919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003921#ifdef FEAT_GUI_HAIKU
3922 vim_unlock_screen();
3923#endif
3924
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003925 if (screen_attr != clear_attr)
3926 screen_stop_highlight();
3927 if (clear_attr != 0)
3928 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003930 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (type == USE_REDRAW)
3932 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003933 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003935 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003937 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
3939 else if (type == USE_T_CDL)
3940 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003941 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003943 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945 /*
3946 * Deleting lines at top of the screen or scroll region: Just scroll
3947 * the whole screen (scroll region) up by outputting newlines on the
3948 * last line.
3949 */
3950 else if (type == USE_NL)
3951 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003952 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003954 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956 else
3957 {
3958 for (i = line_count; --i >= 0; )
3959 {
3960 if (type == USE_T_DL)
3961 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003962 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003963 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003965 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003967 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003968 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003970 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972 }
3973
3974 /*
3975 * If the 'db' flag is set, we need to clear the lines that have been
3976 * scrolled up at the bottom of the region.
3977 */
3978 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
3979 {
3980 for (i = line_count; i > 0; --i)
3981 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003982 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003983 out_str(T_CE); // erase a line
3984 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986 }
3987
3988#ifdef FEAT_GUI
3989 gui_can_update_cursor();
3990 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003991 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#endif
3993
3994 return OK;
3995}
3996
3997/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003998 * Return TRUE when postponing displaying the mode message: when not redrawing
3999 * or inside a mapping.
4000 */
4001 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004002skip_showmode(void)
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004003{
4004 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004005 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004006 if (global_busy
4007 || msg_silent != 0
4008 || !redrawing()
4009 || (char_avail() && !KeyTyped))
4010 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004011 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004012 return TRUE;
4013 }
4014 return FALSE;
4015}
4016
4017/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004018 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 *
4020 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4021 * If clear_cmdline is FALSE there may be a message there that needs to be
4022 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004023 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 * Return the length of the message (0 if no message).
4025 */
4026 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004027showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
4029 int need_clear;
4030 int length = 0;
4031 int do_mode;
4032 int attr;
4033 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
Bram Moolenaara2a89732022-08-31 14:46:18 +01004036 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004037 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004038 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004039 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004040 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004042 if (skip_showmode())
4043 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 nwr_save = need_wait_return;
4046
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004047 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 check_for_delay(FALSE);
4049
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004050 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 need_clear = clear_cmdline;
4052 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004053 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004055 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 msg_pos_mode();
4057 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004058 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (do_mode)
4060 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004061 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004063 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004064# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004065 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004066# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004067 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004068# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004069 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004070# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004071 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004073 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074# endif
4075#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004076 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004077 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004079 // These messages can get long, avoid a wrap in a narrow
4080 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 length = (Rows - msg_row) * Columns - 3;
4082 if (edit_submode_extra != NULL)
4083 length -= vim_strsize(edit_submode_extra);
4084 if (length > 0)
4085 {
4086 if (edit_submode_pre != NULL)
4087 length -= vim_strsize(edit_submode_pre);
4088 if (length - vim_strsize(edit_submode) > 0)
4089 {
4090 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004091 msg_puts_attr((char *)edit_submode_pre, attr);
4092 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
4094 if (edit_submode_extra != NULL)
4095 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004096 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004098 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 else
4100 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004101 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
4105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004108 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004109 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004110 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004111 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
4113#ifdef FEAT_RIGHTLEFT
4114 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004115 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004117 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004119 else if (restart_edit == 'I' || restart_edit == 'i' ||
4120 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004121 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004123 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004125 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126#ifdef FEAT_RIGHTLEFT
4127 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004128 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129#endif
4130#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004131 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
4133# ifdef FEAT_ARABIC
4134 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004135 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 else
4137# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004138 if (get_keymap_str(curwin, (char_u *)" (%s)",
4139 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004140 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004143 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004144 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 if (VIsual_active)
4147 {
4148 char *p;
4149
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004150 // Don't concatenate separate words to avoid translation
4151 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 switch ((VIsual_select ? 4 : 0)
4153 + (VIsual_mode == Ctrl_V) * 2
4154 + (VIsual_mode == 'V'))
4155 {
4156 case 0: p = N_(" VISUAL"); break;
4157 case 1: p = N_(" VISUAL LINE"); break;
4158 case 2: p = N_(" VISUAL BLOCK"); break;
4159 case 4: p = N_(" SELECT"); break;
4160 case 5: p = N_(" SELECT LINE"); break;
4161 default: p = N_(" SELECT BLOCK"); break;
4162 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004163 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004165 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 need_clear = TRUE;
4169 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004170 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004171 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004173 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 need_clear = TRUE;
4175 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004176
4177 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004178 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004180 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 length = msg_col;
4182 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004183 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004186 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004188 else if (redraw_mode)
4189 {
4190 msg_pos_mode();
4191 msg_clr_eos();
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004194 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (VIsual_active)
4196 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004198 // If the last window has no status line, the ruler is after the mode
4199 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004200 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004201 win_redr_ruler(lastwin, TRUE, FALSE);
Martin Tournoijba43e762022-10-13 22:12:15 +01004202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004204 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 clear_cmdline = FALSE;
4206
4207 return length;
4208}
4209
4210/*
4211 * Position for a mode message.
4212 */
4213 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004214msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
4216 msg_col = 0;
4217 msg_row = Rows - 1;
4218}
4219
4220/*
4221 * Delete mode message. Used when ESC is typed which is expected to end
4222 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004223 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 */
4225 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004226unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
4228 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004229 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 */
4231 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004232 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004234 clearmode();
4235}
4236
4237/*
4238 * Clear the mode message.
4239 */
4240 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004241clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004242{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004243 int save_msg_row = msg_row;
4244 int save_msg_col = msg_col;
4245
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004246 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004247 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004248 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004249 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004250
4251 msg_col = save_msg_col;
4252 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253}
4254
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004255 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004256recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004257{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004258 msg_puts_attr(_("recording"), attr);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004259 if (shortmess(SHM_RECORDING))
4260 return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004261
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004262 char s[4];
4263
4264 sprintf(s, " @%c", reg_recording);
4265 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004266}
4267
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004268/*
4269 * Draw the tab pages line at the top of the Vim window.
4270 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004271 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004272draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004273{
4274 int tabcount = 0;
4275 tabpage_T *tp;
4276 int tabwidth;
4277 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004278 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004279 int attr;
4280 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004281 win_T *cwp;
4282 int wincount;
4283 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004284 int c;
4285 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004286 int attr_sel = HL_ATTR(HLF_TPS);
4287 int attr_nosel = HL_ATTR(HLF_TP);
4288 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004289 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004290 int room;
4291 int use_sep_chars = (t_colors < 8
4292#ifdef FEAT_GUI
4293 && !gui.in_use
4294#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004295#ifdef FEAT_TERMGUICOLORS
4296 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004297#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004298 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004299
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004300 if (ScreenLines == NULL)
4301 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004302 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004303
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004304#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004305 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004306 if (gui_use_tabline())
4307 {
4308 gui_update_tabline();
4309 return;
4310 }
4311#endif
4312
4313 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004314 return;
4315
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004316#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004317 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004318
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004319 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004320 if (*p_tal != NUL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004321 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004322 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004323#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004324 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004325 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004326 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004327
Bram Moolenaar238a5642006-02-21 22:12:05 +00004328 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4329 if (tabwidth < 6)
4330 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004331
Bram Moolenaar238a5642006-02-21 22:12:05 +00004332 attr = attr_nosel;
4333 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004334 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4335 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004336 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004337 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004338
Bram Moolenaar238a5642006-02-21 22:12:05 +00004339 if (tp->tp_topframe == topframe)
4340 attr = attr_sel;
4341 if (use_sep_chars && col > 0)
4342 screen_putchar('|', 0, col++, attr);
4343
4344 if (tp->tp_topframe != topframe)
4345 attr = attr_nosel;
4346
4347 screen_putchar(' ', 0, col++, attr);
4348
4349 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004350 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004351 cwp = curwin;
4352 wp = firstwin;
4353 }
4354 else
4355 {
4356 cwp = tp->tp_curwin;
4357 wp = tp->tp_firstwin;
4358 }
4359
4360 modified = FALSE;
4361 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4362 if (bufIsChanged(wp->w_buffer))
4363 modified = TRUE;
4364 if (modified || wincount > 1)
4365 {
4366 if (wincount > 1)
4367 {
4368 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004369 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004370 if (col + len >= Columns - 3)
4371 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004372 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004373#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004374 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004375#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004376 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004377#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004378 );
4379 col += len;
4380 }
4381 if (modified)
4382 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4383 screen_putchar(' ', 0, col++, attr);
4384 }
4385
4386 room = scol - col + tabwidth - 1;
4387 if (room > 0)
4388 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004389 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004390 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004391 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004392 len = vim_strsize(NameBuff);
4393 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004394 if (has_mbyte)
4395 while (len > room)
4396 {
4397 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004398 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004399 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004400 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004401 {
4402 p += len - room;
4403 len = room;
4404 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004405 if (len > Columns - col - 1)
4406 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004407
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004408 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004409 col += len;
4410 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004411 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004412
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004413 // Store the tab page number in TabPageIdxs[], so that
4414 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004415 ++tabcount;
4416 while (scol < col)
4417 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004418 }
4419
Bram Moolenaar238a5642006-02-21 22:12:05 +00004420 if (use_sep_chars)
4421 c = '_';
4422 else
4423 c = ' ';
4424 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004425
Luuk van Baalba936f62022-12-15 13:15:39 +00004426 // Draw the 'showcmd' information if 'showcmdloc' == "tabline".
4427 if (p_sc && *p_sloc == 't')
4428 {
4429 int width = MIN(10, (int)Columns - col - (tabcount > 1) * 3);
4430
4431 if (width > 0)
4432 screen_puts_len(showcmd_buf, width, 0, (int)Columns
4433 - width - (tabcount > 1) * 2, attr_nosel);
4434 }
4435
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004436 // Put an "X" for closing the current tab if there are several.
Luuk van Baalba936f62022-12-15 13:15:39 +00004437 if (tabcount > 1)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004438 {
4439 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4440 TabPageIdxs[Columns - 1] = -999;
4441 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004442 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004443
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004444 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4445 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004446 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004447}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004448
4449/*
4450 * Get buffer name for "buf" into NameBuff[].
4451 * Takes care of special buffer names and translates special characters.
4452 */
4453 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004454get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004455{
4456 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004457 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004458 else
4459 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4460 trans_characters(NameBuff, MAXPATHL);
4461}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004462
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463/*
4464 * Get the character to use in a status line. Get its attributes in "*attr".
4465 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004466 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004467fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
4469 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004470
4471#ifdef FEAT_TERMINAL
4472 if (bt_terminal(wp->w_buffer))
4473 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004474 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004475 {
4476 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004477 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004478 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004479 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004480 {
4481 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004482 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004483 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004484 }
4485 else
4486#endif
4487 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004489 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004490 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492 else
4493 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004494 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004495 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
Christian Brabandt6a650bf2023-11-08 21:23:29 +01004497 return fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500/*
4501 * Get the character to use in a separator between vertically split windows.
4502 * Get its attributes in "*attr".
4503 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004504 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004505fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004507 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004508 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 return '|';
4510 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004511 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
4514/*
4515 * Return TRUE if redrawing should currently be done.
4516 */
4517 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004518redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004520#ifdef FEAT_EVAL
4521 if (disable_redraw_for_testing)
4522 return 0;
4523 else
4524#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004525 return ((RedrawingDisabled == 0
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004526#ifdef FEAT_EVAL
4527 || ignore_redraw_flag_for_testing
4528#endif
4529 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530}
4531
4532/*
4533 * Return TRUE if printing messages should currently be done.
4534 */
4535 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004536messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004538 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539}
4540
Bram Moolenaare677df82019-09-02 22:31:11 +02004541/*
4542 * Compute columns for ruler and shown command. 'sc_col' is also used to
4543 * decide what the maximum length of a message on the status line can be.
4544 * If there is a status line for the last window, 'sc_col' is independent
4545 * of 'ru_col'.
4546 */
4547
4548#define COL_RULER 17 // columns needed by standard ruler
4549
4550 void
4551comp_col(void)
4552{
Sean Dewar876f5fb2023-08-17 22:40:05 +02004553 int last_has_status = last_stl_height(FALSE) > 0;
Bram Moolenaare677df82019-09-02 22:31:11 +02004554
4555 sc_col = 0;
4556 ru_col = 0;
4557 if (p_ru)
4558 {
Martin Tournoijba43e762022-10-13 22:12:15 +01004559#ifdef FEAT_STL_OPT
Bram Moolenaare677df82019-09-02 22:31:11 +02004560 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004561#else
Bram Moolenaare677df82019-09-02 22:31:11 +02004562 ru_col = COL_RULER + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004563#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004564 // no last status line, adjust sc_col
4565 if (!last_has_status)
4566 sc_col = ru_col;
4567 }
Sam-programs062141b2024-02-29 17:40:29 +01004568 if (p_sc && *p_sloc == 'l')
Bram Moolenaare677df82019-09-02 22:31:11 +02004569 {
4570 sc_col += SHOWCMD_COLS;
4571 if (!p_ru || last_has_status) // no need for separating space
4572 ++sc_col;
4573 }
4574 sc_col = Columns - sc_col;
4575 ru_col = Columns - ru_col;
4576 if (sc_col <= 0) // screen too narrow, will become a mess
4577 sc_col = 1;
4578 if (ru_col <= 0)
4579 ru_col = 1;
Bram Moolenaare677df82019-09-02 22:31:11 +02004580#ifdef FEAT_EVAL
4581 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4582#endif
4583}
4584
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004585#if defined(FEAT_LINEBREAK) || defined(PROTO)
4586/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004587 * Return the width of the 'number' and 'relativenumber' column.
4588 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004589 * Otherwise it depends on 'numberwidth' and the line count.
4590 */
4591 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004592number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004593{
4594 int n;
4595 linenr_T lnum;
4596
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004597 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004598 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004599 lnum = wp->w_height;
4600 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004601 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004602 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004603
Bram Moolenaar6b314672015-03-20 15:42:10 +01004604 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004605 return wp->w_nrwidth_width;
4606 wp->w_nrwidth_line_count = lnum;
4607
4608 n = 0;
4609 do
4610 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004611 lnum /= 10;
4612 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004613 } while (lnum > 0);
4614
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004615 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004616 if (n < wp->w_p_nuw - 1)
4617 n = wp->w_p_nuw - 1;
4618
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004619# ifdef FEAT_SIGNS
4620 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4621 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004622 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004623 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4624 n = 2;
4625# endif
4626
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004627 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004628 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004629 return n;
4630}
4631#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004632
Bram Moolenaar113e1072019-01-20 15:30:40 +01004633#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004634/*
4635 * Return the current cursor column. This is the actual position on the
4636 * screen. First column is 0.
4637 */
4638 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004639screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004640{
4641 return screen_cur_col;
4642}
4643
4644/*
4645 * Return the current cursor row. This is the actual position on the screen.
4646 * First row is 0.
4647 */
4648 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004649screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004650{
4651 return screen_cur_row;
4652}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004653#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004654
4655/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004656 * Calls mb_ptr2char_adv(p) and returns the character.
4657 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4658 */
4659 static int
4660get_encoded_char_adv(char_u **p)
4661{
4662 char_u *s = *p;
4663
4664 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4665 {
4666 varnumber_T num = 0;
4667 int bytes;
4668 int n;
4669
4670 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4671 {
4672 *p += 2;
4673 n = hexhex2nr(*p);
4674 if (n < 0)
4675 return 0;
4676 num = num * 256 + n;
4677 }
4678 *p += 2;
4679 return num;
4680 }
4681 return mb_ptr2char_adv(p);
4682}
4683
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004684struct charstab
4685{
4686 int *cp;
4687 char *name;
4688};
4689static fill_chars_T fill_chars;
4690static struct charstab filltab[] =
4691{
4692 {&fill_chars.stl, "stl"},
4693 {&fill_chars.stlnc, "stlnc"},
4694 {&fill_chars.vert, "vert"},
4695 {&fill_chars.fold, "fold"},
4696 {&fill_chars.foldopen, "foldopen"},
4697 {&fill_chars.foldclosed, "foldclose"},
4698 {&fill_chars.foldsep, "foldsep"},
4699 {&fill_chars.diff, "diff"},
4700 {&fill_chars.eob, "eob"},
4701 {&fill_chars.lastline, "lastline"},
4702};
4703static lcs_chars_T lcs_chars;
4704static struct charstab lcstab[] =
4705{
4706 {&lcs_chars.eol, "eol"},
4707 {&lcs_chars.ext, "extends"},
4708 {&lcs_chars.nbsp, "nbsp"},
4709 {&lcs_chars.prec, "precedes"},
4710 {&lcs_chars.space, "space"},
4711 {&lcs_chars.tab2, "tab"},
4712 {&lcs_chars.trail, "trail"},
4713 {&lcs_chars.lead, "lead"},
4714#ifdef FEAT_CONCEAL
4715 {&lcs_chars.conceal, "conceal"},
4716#else
4717 {NULL, "conceal"},
4718#endif
zeertzjq1f025b02023-09-30 12:43:07 +02004719 {NULL, "multispace"},
4720 {NULL, "leadmultispace"},
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004721};
4722
zeertzjq6a8d2e12024-01-17 20:54:49 +01004723 static char *
4724field_value_err(char *errbuf, size_t errbuflen, char *fmt, char *field)
4725{
4726 if (errbuf == NULL)
4727 return "";
4728 vim_snprintf(errbuf, errbuflen, _(fmt), field);
4729 return errbuf;
4730}
4731
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004732/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004733 * Handle setting 'listchars' or 'fillchars'.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004734 * "value" points to either the global or the window-local value.
zeertzjqd619d6a2023-05-08 15:56:21 +01004735 * "is_listchars" is TRUE for "listchars" and FALSE for "fillchars".
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004736 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004737 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004738 * Returns error message, NULL if it's OK.
4739 */
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004740 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004741set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
4742 char *errbuf, size_t errbuflen)
Bram Moolenaare677df82019-09-02 22:31:11 +02004743{
zeertzjq1f025b02023-09-30 12:43:07 +02004744 int round, i, len, entries;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004745 char_u *p, *s;
4746 int c1 = 0, c2 = 0, c3 = 0;
4747 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4748 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4749 int multispace_len = 0; // Length of lcs-multispace string
4750 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004751
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004752 struct charstab *tab;
4753
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004754 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004755 {
4756 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004757 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004758 entries = ARRAY_LENGTH(lcstab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004759 if (wp->w_p_lcs[0] == NUL)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004760 value = p_lcs; // local value is empty, use the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004761 }
4762 else
4763 {
4764 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004765 entries = ARRAY_LENGTH(filltab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004766 if (wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004767 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004768 }
4769
4770 // first round: check for valid value, second round: assign values
zeertzjq00624a22023-11-23 20:47:16 +01004771 for (round = 0; round <= (apply ? 1 : 0); ++round)
Bram Moolenaare677df82019-09-02 22:31:11 +02004772 {
4773 if (round > 0)
4774 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004775 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004776 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004777 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004778 for (i = 0; i < entries; ++i)
4779 if (tab[i].cp != NULL)
4780 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004781 lcs_chars.tab1 = NUL;
4782 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004783
Mike Williamsf5785cf2021-09-13 22:17:38 +02004784 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004785 {
4786 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004787 if (lcs_chars.multispace != NULL)
4788 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004789 }
4790 else
4791 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004792
4793 if (lead_multispace_len > 0)
4794 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004795 lcs_chars.leadmultispace =
4796 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004797 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
4798 }
4799 else
4800 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004801 }
4802 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004803 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004804 fill_chars.stl = ' ';
4805 fill_chars.stlnc = ' ';
4806 fill_chars.vert = ' ';
4807 fill_chars.fold = '-';
4808 fill_chars.foldopen = '-';
4809 fill_chars.foldclosed = '+';
4810 fill_chars.foldsep = '|';
4811 fill_chars.diff = '-';
4812 fill_chars.eob = '~';
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01004813 fill_chars.lastline = '@';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004814 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004815 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004816 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02004817 while (*p)
4818 {
4819 for (i = 0; i < entries; ++i)
4820 {
4821 len = (int)STRLEN(tab[i].name);
zeertzjq6a8d2e12024-01-17 20:54:49 +01004822 if (!(STRNCMP(p, tab[i].name, len) == 0 && p[len] == ':'))
zeertzjq1f025b02023-09-30 12:43:07 +02004823 continue;
Bram Moolenaare677df82019-09-02 22:31:11 +02004824
zeertzjq1f025b02023-09-30 12:43:07 +02004825 if (is_listchars && strcmp(tab[i].name, "multispace") == 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004826 {
4827 s = p + len + 1;
4828 if (round == 0)
4829 {
4830 // Get length of lcs-multispace string in first round
4831 last_multispace = p;
4832 multispace_len = 0;
4833 while (*s != NUL && *s != ',')
4834 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004835 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004836 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004837 return field_value_err(errbuf, errbuflen,
4838 e_wrong_character_width_for_field_str,
4839 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004840 ++multispace_len;
4841 }
4842 if (multispace_len == 0)
4843 // lcs-multispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004844 return field_value_err(errbuf, errbuflen,
4845 e_wrong_number_of_characters_for_field_str,
4846 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004847 p = s;
4848 }
4849 else
4850 {
4851 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02004852
zeertzjqf14b8ba2021-09-10 16:58:30 +02004853 while (*s != NUL && *s != ',')
4854 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004855 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004856 if (p == last_multispace)
4857 lcs_chars.multispace[multispace_pos++] = c1;
4858 }
4859 p = s;
4860 }
zeertzjq1f025b02023-09-30 12:43:07 +02004861 break;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004862 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004863
zeertzjq1f025b02023-09-30 12:43:07 +02004864 if (is_listchars && strcmp(tab[i].name, "leadmultispace") == 0)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004865 {
zeertzjq1f025b02023-09-30 12:43:07 +02004866 s = p + len + 1;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004867 if (round == 0)
4868 {
zeertzjqb5f08012022-06-09 13:55:28 +01004869 // get length of lcs-leadmultispace string in first
4870 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004871 last_lmultispace = p;
4872 lead_multispace_len = 0;
4873 while (*s != NUL && *s != ',')
4874 {
4875 c1 = get_encoded_char_adv(&s);
4876 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004877 return field_value_err(errbuf, errbuflen,
4878 e_wrong_character_width_for_field_str,
4879 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004880 ++lead_multispace_len;
4881 }
4882 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01004883 // lcs-leadmultispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004884 return field_value_err(errbuf, errbuflen,
4885 e_wrong_number_of_characters_for_field_str,
4886 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004887 p = s;
4888 }
4889 else
4890 {
4891 int multispace_pos = 0;
4892
4893 while (*s != NUL && *s != ',')
4894 {
4895 c1 = get_encoded_char_adv(&s);
4896 if (p == last_lmultispace)
4897 lcs_chars.leadmultispace[multispace_pos++] = c1;
4898 }
4899 p = s;
4900 }
zeertzjq1f025b02023-09-30 12:43:07 +02004901 break;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004902 }
zeertzjq1f025b02023-09-30 12:43:07 +02004903
4904 c2 = c3 = 0;
4905 s = p + len + 1;
zeertzjq6a8d2e12024-01-17 20:54:49 +01004906 if (*s == NUL)
4907 return field_value_err(errbuf, errbuflen,
4908 e_wrong_number_of_characters_for_field_str,
4909 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004910 c1 = get_encoded_char_adv(&s);
4911 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004912 return field_value_err(errbuf, errbuflen,
4913 e_wrong_character_width_for_field_str,
4914 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004915 if (tab[i].cp == &lcs_chars.tab2)
4916 {
4917 if (*s == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004918 return field_value_err(errbuf, errbuflen,
4919 e_wrong_number_of_characters_for_field_str,
4920 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004921 c2 = get_encoded_char_adv(&s);
4922 if (char2cells(c2) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004923 return field_value_err(errbuf, errbuflen,
4924 e_wrong_character_width_for_field_str,
4925 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004926 if (!(*s == ',' || *s == NUL))
4927 {
4928 c3 = get_encoded_char_adv(&s);
4929 if (char2cells(c3) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004930 return field_value_err(errbuf, errbuflen,
4931 e_wrong_character_width_for_field_str,
4932 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004933 }
4934 }
4935
4936 if (*s == ',' || *s == NUL)
4937 {
4938 if (round > 0)
4939 {
4940 if (tab[i].cp == &lcs_chars.tab2)
4941 {
4942 lcs_chars.tab1 = c1;
4943 lcs_chars.tab2 = c2;
4944 lcs_chars.tab3 = c3;
4945 }
4946 else if (tab[i].cp != NULL)
4947 *(tab[i].cp) = c1;
4948
4949 }
4950 p = s;
4951 break;
4952 }
zeertzjq6a8d2e12024-01-17 20:54:49 +01004953 else
4954 return field_value_err(errbuf, errbuflen,
4955 e_wrong_number_of_characters_for_field_str,
4956 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004957 }
4958
zeertzjq1f025b02023-09-30 12:43:07 +02004959 if (i == entries)
4960 return e_invalid_argument;
4961
Bram Moolenaare677df82019-09-02 22:31:11 +02004962 if (*p == ',')
4963 ++p;
4964 }
4965 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004966
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004967 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004968 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004969 if (is_listchars)
4970 {
4971 vim_free(wp->w_lcs_chars.multispace);
4972 vim_free(wp->w_lcs_chars.leadmultispace);
4973 wp->w_lcs_chars = lcs_chars;
4974 }
4975 else
4976 {
4977 wp->w_fill_chars = fill_chars;
4978 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02004979 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004980
4981 return NULL; // no error
4982}
zeertzjq8ca29b62022-08-09 12:53:14 +01004983
4984/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004985 * Handle the new value of 'fillchars'.
4986 */
4987 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004988set_fillchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4989 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004990{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004991 return set_chars_option(wp, val, FALSE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004992}
4993
4994/*
4995 * Handle the new value of 'listchars'.
4996 */
4997 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004998set_listchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4999 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005000{
zeertzjq6a8d2e12024-01-17 20:54:49 +01005001 return set_chars_option(wp, val, TRUE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005002}
5003
5004/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02005005 * Function given to ExpandGeneric() to obtain possible arguments of the
5006 * 'fillchars' option.
5007 */
5008 char_u *
5009get_fillchars_name(expand_T *xp UNUSED, int idx)
5010{
5011 if (idx >= (int)(sizeof(filltab) / sizeof(filltab[0])))
5012 return NULL;
5013
5014 return (char_u*)filltab[idx].name;
5015}
5016
5017/*
5018 * Function given to ExpandGeneric() to obtain possible arguments of the
5019 * 'listchars' option.
5020 */
5021 char_u *
5022get_listchars_name(expand_T *xp UNUSED, int idx)
5023{
5024 if (idx >= (int)(sizeof(lcstab) / sizeof(lcstab[0])))
5025 return NULL;
5026
5027 return (char_u*)lcstab[idx].name;
5028}
5029
5030/*
zeertzjq8ca29b62022-08-09 12:53:14 +01005031 * Check all global and local values of 'listchars' and 'fillchars'.
5032 * Return an untranslated error messages if any of them is invalid, NULL
5033 * otherwise.
5034 */
5035 char *
5036check_chars_options(void)
5037{
5038 tabpage_T *tp;
5039 win_T *wp;
5040
zeertzjq6a8d2e12024-01-17 20:54:49 +01005041 if (set_listchars_option(curwin, p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005042 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005043 if (set_fillchars_option(curwin, p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005044 return e_conflicts_with_value_of_fillchars;
5045 FOR_ALL_TAB_WINDOWS(tp, wp)
5046 {
zeertzjq6a8d2e12024-01-17 20:54:49 +01005047 if (set_listchars_option(wp, wp->w_p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005048 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005049 if (set_fillchars_option(wp, wp->w_p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005050 return e_conflicts_with_value_of_fillchars;
5051 }
5052 return NULL;
5053}