blob: 35d300213b6162a4e30f2ca666731ef2b1d1bac5 [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
h-east8927c9b2024-04-20 17:57:19 +02001984 if (
1985#ifdef FEAT_GUI
1986 !gui.in_use &&
1987#endif
1988 get_cellwidth(ScreenLinesUC[off]) > 1
1989 )
mikoto2000e20fa592024-04-17 22:06:54 +02001990 {
h-east8927c9b2024-04-20 17:57:19 +02001991 // If the width is set to 2 with setcellwidths()
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;
mikoto2000e20fa592024-04-17 22:06:54 +02001997 }
1998 else if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001999 {
2000 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002001#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002002 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002003#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002004 )
2005 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002006 // Clear the two screen cells. If the character is actually
2007 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002008 out_str((char_u *)" ");
2009 term_windgoto(row, col);
2010 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002011 // not sure where the cursor is after drawing the ambiguous width
2012 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002013 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002014 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002015 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002017
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002018 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002019 buf[utfc_char2bytes(off, buf)] = NUL;
2020 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002026 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2028 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030
2031 screen_cur_col++;
2032}
2033
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034/*
2035 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2036 * on the screen at position 'row' and 'col'.
2037 * The attributes of the first byte is used for all. This is required to
2038 * output the two bytes of a double-byte character with nothing in between.
2039 */
2040 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002041screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002043 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2045 return;
2046
dundargocc57b5bc2022-11-02 13:30:51 +00002047 // Outputting the last character on the screen may scroll the screen up.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002048 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2050 {
2051 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002052 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 return;
2054 }
2055
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002056 // Output the first byte normally (positions the cursor), then write the
2057 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 screen_char(off, row, col);
2059 out_char(ScreenLines[off + 1]);
2060 ++screen_cur_col;
2061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063/*
2064 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2065 * This uses the contents of ScreenLines[] and doesn't change it.
2066 */
2067 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002068screen_draw_rectangle(
2069 int row,
2070 int col,
2071 int height,
2072 int width,
2073 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075 int r, c;
2076 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002077 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002079 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002080 if (ScreenLines == NULL)
2081 return;
2082
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if (invert)
2084 screen_char_attr = HL_INVERSE;
2085 for (r = row; r < row + height; ++r)
2086 {
2087 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002088 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 for (c = col; c < col + width; ++c)
2090 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002091 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002093 if (!skip_for_popup(r, c))
2094 screen_char_2(off + c, r, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 ++c;
2096 }
2097 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002099 if (!skip_for_popup(r, c))
2100 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002101 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 }
2104 }
2105 }
2106 screen_char_attr = 0;
2107}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109/*
2110 * Redraw the characters for a vertically split window.
2111 */
2112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002113redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
2115 int col;
2116 int width;
2117
2118# ifdef FEAT_CLIPBOARD
2119 clip_may_clear_selection(row, end - 1);
2120# endif
2121
2122 if (wp == NULL)
2123 {
2124 col = 0;
2125 width = Columns;
2126 }
2127 else
2128 {
2129 col = wp->w_wincol;
2130 width = wp->w_width;
2131 }
2132 screen_draw_rectangle(row, col, end - row, width, FALSE);
2133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002135 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002136space_to_screenline(int off, int attr)
2137{
2138 ScreenLines[off] = ' ';
2139 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002140 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002141 if (enc_utf8)
2142 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002143}
2144
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002146 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2147 * to "end_col" (exclusive) with character "c1" in first column followed by
2148 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 */
2150 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002151screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002152 int start_row,
2153 int end_row,
2154 int start_col,
2155 int end_col,
2156 int c1,
2157 int c2,
2158 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002160 int row;
2161 int col;
2162 int off;
2163 int end_off;
2164 int did_delete;
2165 int c;
2166 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002168 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#endif
2170
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002171 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002173 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 end_col = screen_Columns;
2175 if (ScreenLines == NULL
2176 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002177 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 return;
2179
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002180 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 norm_term = (
2182#ifdef FEAT_GUI
2183 !gui.in_use &&
2184#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002185 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 for (row = start_row; row < end_row; ++row)
2187 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002188 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002189#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002190 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002191#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002192 )
2193 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002194 // When drawing over the right half of a double-wide char clear
2195 // out the left half. When drawing over the left half of a
2196 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002197 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002198 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002199 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002200 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002201 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 /*
2204 * Try to use delete-line termcap code, when no attributes or in a
2205 * "normal" terminal, where a bold/italic space is just a
2206 * space.
2207 */
2208 did_delete = FALSE;
2209 if (c2 == ' '
2210 && end_col == Columns
2211 && can_clear(T_CE)
2212 && (attr == 0
2213 || (norm_term
2214 && attr <= HL_ALL
2215 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2216 {
2217 /*
2218 * check if we really need to clear something
2219 */
2220 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002221 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ++col;
2223
2224 off = LineOffset[row] + col;
2225 end_off = LineOffset[row] + end_col;
2226
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002227 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (enc_utf8)
2229 while (off < end_off && ScreenLines[off] == ' '
2230 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2231 ++off;
2232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 while (off < end_off && ScreenLines[off] == ' '
2234 && ScreenAttrs[off] == 0)
2235 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002236 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
2238 col = off - LineOffset[row];
2239 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002240 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002242 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002244 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002246 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 ++off;
2248 }
2249 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002250 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
2252
2253 off = LineOffset[row] + start_col;
2254 c = c1;
2255 for (col = start_col; col < end_col; ++col)
2256 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002257 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002258 || (enc_utf8 && (int)ScreenLinesUC[off]
2259 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002261 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#if defined(FEAT_GUI) || defined(UNIX)
2263 || force_next
2264#endif
2265 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002266 // Skip if under a(nother) popup.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002267 && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
2269#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002270 // The bold trick may make a single row of pixels appear in
2271 // the next character. When a bold character is removed, the
2272 // next character should be redrawn too. This happens for our
2273 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (
2275# ifdef FEAT_GUI
2276 gui.in_use
2277# endif
2278# if defined(FEAT_GUI) && defined(UNIX)
2279 ||
2280# endif
2281# ifdef UNIX
2282 term_is_xterm
2283# endif
2284 )
2285 {
2286 if (ScreenLines[off] != ' '
2287 && (ScreenAttrs[off] > HL_ALL
2288 || ScreenAttrs[off] & HL_BOLD))
2289 force_next = TRUE;
2290 else
2291 force_next = FALSE;
2292 }
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002293#endif // FEAT_GUI || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (enc_utf8)
2296 {
2297 if (c >= 0x80)
2298 {
2299 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002300 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302 else
2303 ScreenLinesUC[off] = 0;
2304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 ScreenAttrs[off] = attr;
2306 if (!did_delete || c != ' ')
2307 screen_char(off, row, col);
2308 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002309 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 ++off;
2311 if (col == start_col)
2312 {
2313 if (did_delete)
2314 break;
2315 c = c2;
2316 }
2317 }
2318 if (end_col == Columns)
2319 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002320 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002323 if (start_col == 0 && end_col == Columns
2324 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002325 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002326 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002327 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329 }
2330}
2331
2332/*
2333 * Check if there should be a delay. Used before clearing or redrawing the
2334 * screen or the command line.
2335 */
2336 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002337check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2340 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002341 && emsg_silent == 0
2342 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002345 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 emsg_on_display = FALSE;
2347 if (check_msg_scroll)
2348 msg_scroll = FALSE;
2349 }
2350}
2351
2352/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002353 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2354 */
2355 static void
2356clear_TabPageIdxs(void)
2357{
2358 int scol;
2359
2360 for (scol = 0; scol < Columns; ++scol)
2361 TabPageIdxs[scol] = 0;
2362}
2363
2364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002366 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 * Returns TRUE if there is a valid screen to write to.
2368 * Returns FALSE when starting up and screen not initialized yet.
2369 */
2370 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002371screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002373 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return (ScreenLines != NULL);
2375}
2376
2377/*
2378 * Resize the shell to Rows and Columns.
2379 * Allocate ScreenLines[] and associated items.
2380 *
2381 * There may be some time between setting Rows and Columns and (re)allocating
2382 * ScreenLines[]. This happens when starting up and when (manually) changing
2383 * the shell size. Always use screen_Rows and screen_Columns to access items
2384 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2385 * final size of the shell is needed.
2386 */
2387 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002388screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389{
2390 int new_row, old_row;
2391#ifdef FEAT_GUI
2392 int old_Rows;
2393#endif
2394 win_T *wp;
2395 int outofmem = FALSE;
2396 int len;
2397 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002399 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002402 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 unsigned *new_LineOffset;
2404 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002405 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002406#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002407 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002408 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002409 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002410#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002411 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002412 static int entered = FALSE; // avoid recursiveness
2413 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002414 int retry_count = 0;
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002415 int found_null;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002417retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 /*
2419 * Allocation of the screen buffers is done only when the size changes and
2420 * when Rows and Columns have been set and we have started doing full
2421 * screen stuff.
2422 */
2423 if ((ScreenLines != NULL
2424 && Rows == screen_Rows
2425 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 && enc_utf8 == (ScreenLinesUC != NULL)
2427 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002428 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 || Rows == 0
2430 || Columns == 0
2431 || (!full_screen && ScreenLines == NULL))
2432 return;
2433
2434 /*
2435 * It's possible that we produce an out-of-memory message below, which
2436 * will cause this function to be called again. To break the loop, just
2437 * return here.
2438 */
2439 if (entered)
2440 return;
2441 entered = TRUE;
2442
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002443 /*
2444 * Note that the window sizes are updated before reallocating the arrays,
2445 * thus we must not redraw here!
2446 */
2447 ++RedrawingDisabled;
2448
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002449 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002451#ifdef FEAT_GUI_HAIKU
2452 vim_lock_screen(); // be safe, put it here
2453#endif
2454
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002455 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 /*
2458 * We're changing the size of the screen.
2459 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2460 * - Move lines from the old arrays into the new arrays, clear extra
2461 * lines (unless the screen is going to be cleared).
2462 * - Free the old arrays.
2463 *
2464 * If anything fails, make ScreenLines NULL, so we don't do anything!
2465 * Continuing with the old ScreenLines may result in a crash, because the
2466 * size is wrong.
2467 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002468 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 win_free_lsize(wp);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002470 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002471 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002472 win_free_lsize(aucmd_win[i].auc_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002473#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002474 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002475 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002476 win_free_lsize(wp);
2477 // tab-local popup windows
2478 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002479 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002480 win_free_lsize(wp);
2481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002483 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002484 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (enc_utf8)
2486 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002487 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002488 for (int i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002489 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2490 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002493 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2494 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
dundargocc57b5bc2022-11-02 13:30:51 +00002495 // Clear ScreenCols to avoid a warning for uninitialized memory in
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002496 // jump_to_mouse().
2497 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002498 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2499 new_LineWraps = LALLOC_MULT(char_u, Rows);
2500 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002501#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002502 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002503 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002504 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002507 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 if (win_alloc_lines(wp) == FAIL)
2510 {
2511 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002512 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00002515 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002516 if (aucmd_win[i].auc_win != NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002517 && aucmd_win[i].auc_win->w_lines == NULL
2518 && win_alloc_lines(aucmd_win[i].auc_win) == FAIL)
2519 {
2520 outofmem = TRUE;
2521 break;
2522 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002523#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002524 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002525 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002526 if (win_alloc_lines(wp) == FAIL)
2527 {
2528 outofmem = TRUE;
2529 goto give_up;
2530 }
2531 // tab-local popup windows
2532 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002533 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002534 if (win_alloc_lines(wp) == FAIL)
2535 {
2536 outofmem = TRUE;
2537 goto give_up;
2538 }
2539#endif
2540
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002541give_up:
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002542 found_null = FALSE;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002543 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002544 if (new_ScreenLinesC[i] == NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002545 {
2546 found_null = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002547 break;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (new_ScreenLines == NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002550 || (enc_utf8 && (new_ScreenLinesUC == NULL || found_null))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002553 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 || new_LineOffset == NULL
2555 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002556 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002557#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002558 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002559 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002560 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 || outofmem)
2563 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002564 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002565 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002566 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002567 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2568
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002569 // Remember we did this to avoid getting outofmem messages over
2570 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002571 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002572 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002573 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002574 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002575 for (int i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002576 VIM_CLEAR(new_ScreenLinesC[i]);
2577 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002578 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002579 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002580 VIM_CLEAR(new_LineOffset);
2581 VIM_CLEAR(new_LineWraps);
2582 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002583#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002584 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002585 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002586 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589 else
2590 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002591 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 for (new_row = 0; new_row < Rows; ++new_row)
2594 {
2595 new_LineOffset[new_row] = new_row * Columns;
2596 new_LineWraps[new_row] = FALSE;
2597
Olaf Seibertfd472652024-02-01 21:11:16 +01002598 (void)vim_memset(new_ScreenLines + new_row * Columns,
2599 ' ', (size_t)Columns * sizeof(schar_T));
2600 if (enc_utf8)
2601 {
2602 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2603 0, (size_t)Columns * sizeof(u8char_T));
2604 for (int i = 0; i < p_mco; ++i)
2605 (void)vim_memset(new_ScreenLinesC[i]
2606 + new_row * Columns,
2607 0, (size_t)Columns * sizeof(u8char_T));
2608 }
2609 if (enc_dbcs == DBCS_JPNU)
2610 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2611 0, (size_t)Columns * sizeof(schar_T));
2612 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2613 0, (size_t)Columns * sizeof(sattr_T));
2614 (void)vim_memset(new_ScreenCols + new_row * Columns,
2615 0, (size_t)Columns * sizeof(colnr_T));
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 /*
2618 * If the screen is not going to be cleared, copy as much as
2619 * possible from the old screen to the new one and clear the rest
2620 * (used when resizing the window at the "--more--" prompt or when
2621 * executing an external command, for the GUI).
2622 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002623 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002626 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
2628 if (screen_Columns < Columns)
2629 len = screen_Columns;
2630 else
2631 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002632 // When switching to utf-8 don't copy characters, they
2633 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002634 if (!(enc_utf8 && ScreenLinesUC == NULL)
2635 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002636 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2637 ScreenLines + LineOffset[old_row],
2638 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002639 if (enc_utf8 && ScreenLinesUC != NULL
2640 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
2642 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2643 ScreenLinesUC + LineOffset[old_row],
2644 (size_t)len * sizeof(u8char_T));
Bram Moolenaare76062c2022-11-28 18:51:43 +00002645 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002646 mch_memmove(new_ScreenLinesC[i]
2647 + new_LineOffset[new_row],
2648 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 (size_t)len * sizeof(u8char_T));
2650 }
2651 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2652 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2653 ScreenLines2 + LineOffset[old_row],
2654 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2656 ScreenAttrs + LineOffset[old_row],
2657 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002658 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2659 ScreenAttrs + LineOffset[old_row],
2660 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
2662 }
2663 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002664 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002666
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002667#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002668 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2669 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
2672
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002673 free_screenlines();
2674
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002675 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002678 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002679 ScreenLinesC[i] = new_ScreenLinesC[i];
2680 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002683 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 LineOffset = new_LineOffset;
2685 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002686 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002687#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002688 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002689 popup_mask_next = new_popup_mask_next;
2690 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002691 popup_mask_refresh = TRUE;
2692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002694 // It's important that screen_Rows and screen_Columns reflect the actual
2695 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696#ifdef FEAT_GUI
2697 old_Rows = screen_Rows;
2698#endif
2699 screen_Rows = Rows;
2700 screen_Columns = Columns;
2701
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002702 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002703 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002704 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705#ifdef FEAT_GUI
2706 else if (gui.in_use
2707 && !gui.starting
2708 && ScreenLines != NULL
2709 && old_Rows != Rows)
2710 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002711 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2712
2713 // Adjust the position of the cursor, for when executing an external
2714 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002715 if (msg_row >= Rows) // Rows got smaller
2716 msg_row = Rows - 1; // put cursor at last row
2717 else if (Rows > old_Rows) // Rows got bigger
2718 msg_row += Rows - old_Rows; // put cursor in same place
2719 if (msg_col >= Columns) // Columns got smaller
2720 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002723 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002725#ifdef FEAT_GUI_HAIKU
2726 vim_unlock_screen();
2727#endif
2728
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 entered = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002730 if (RedrawingDisabled > 0)
2731 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002732
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002733 /*
2734 * Do not apply autocommands more than 3 times to avoid an endless loop
2735 * in case applying autocommands always changes Rows or Columns.
2736 */
2737 if (starting == 0 && ++retry_count <= 3)
2738 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002739 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002740 // In rare cases, autocommands may have altered Rows or Columns,
2741 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002742 goto retry;
2743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744}
2745
2746 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002747free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002748{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002749 int i;
2750
Bram Moolenaar33796b32019-06-08 16:01:13 +02002751 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002752 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002753 VIM_CLEAR(ScreenLinesC[i]);
2754 VIM_CLEAR(ScreenLines2);
2755 VIM_CLEAR(ScreenLines);
2756 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002757 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002758 VIM_CLEAR(LineOffset);
2759 VIM_CLEAR(LineWraps);
2760 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002761#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002762 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002763 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002764 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002765#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002766}
2767
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002768/*
2769 * Clear the screen.
2770 * May delay if there is something the user should read.
2771 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002772 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002773 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002774 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01002775 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002776screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002779 screenalloc(FALSE); // allocate screen buffers if size changed
2780 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002781}
2782
2783/*
2784 * Do not clear the screen but mark everything for redraw.
2785 */
2786 void
2787redraw_as_cleared(void)
2788{
2789 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790}
2791
Bram Moolenaar838b7462022-09-26 15:19:56 +01002792 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002793screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002796 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 if (starting == NO_SCREEN || ScreenLines == NULL
2799#ifdef FEAT_GUI
2800 || (gui.in_use && gui.starting)
2801#endif
2802 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01002803 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805#ifdef FEAT_GUI
2806 if (!gui.in_use)
2807#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002808 screen_attr = -1; // force setting the Normal colors
2809 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002812 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 clip_scroll_selection(9999);
2814#endif
2815
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002816 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 for (i = 0; i < Rows; ++i)
2818 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002819 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 LineWraps[i] = FALSE;
2821 }
2822
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002823 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002825 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002826 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002828 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
2830 else
2831 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002832 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 for (i = 0; i < Rows; ++i)
2834 lineinvalid(LineOffset[i], (int)Columns);
2835 clear_cmdline = TRUE;
2836 }
2837
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002838 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002840 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002842 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002843 if (must_redraw == UPD_CLEAR) // no need to clear again
2844 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002845 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002847#ifdef FEAT_PROP_POPUP
2848 popup_redraw_all(); // redraw all popup windows
2849#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002850 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002852 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 msg_didany = FALSE;
2854 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002855
2856 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857}
2858
2859/*
2860 * Clear one line in ScreenLines.
2861 */
2862 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002863lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
2865 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (enc_utf8)
2867 (void)vim_memset(ScreenLinesUC + off, 0,
2868 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002869 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002870 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871}
2872
2873/*
2874 * Mark one line in ScreenLines invalid by setting the attributes to an
2875 * invalid value.
2876 */
2877 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002878lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002881 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882}
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02002885 * To be called when characters were sent to the terminal directly, outputting
2886 * test on "screen_lnum".
2887 */
2888 void
2889line_was_clobbered(int screen_lnum)
2890{
2891 lineinvalid(LineOffset[screen_lnum], (int)Columns);
2892}
2893
2894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 * Copy part of a Screenline for vertically split window "wp".
2896 */
2897 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002898linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
2900 unsigned off_to = LineOffset[to] + wp->w_wincol;
2901 unsigned off_from = LineOffset[from] + wp->w_wincol;
2902
2903 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
2904 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 if (enc_utf8)
2906 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002907 int i;
2908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
2910 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002911 for (i = 0; i < p_mco; ++i)
2912 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
2913 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 if (enc_dbcs == DBCS_JPNU)
2916 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
2917 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
2919 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002920 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
2921 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
2924/*
2925 * Return TRUE if clearing with term string "p" would work.
2926 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02002927 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 */
2929 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002930can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 return (*p != NUL && (t_colors <= 1
2933#ifdef FEAT_GUI
2934 || gui.in_use
2935#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002936#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002937 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02002938 || (!p_tgc && cterm_normal_bg_color == 0)
2939#else
2940 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002941#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002942 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002943#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002944 && !(p == T_CE && popup_visible)
2945#endif
2946 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947}
2948
2949/*
2950 * Reset cursor position. Use whenever cursor was moved because of outputting
2951 * something directly to the screen (shell commands) or a terminal control
2952 * code.
2953 */
2954 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002955screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 screen_cur_row = screen_cur_col = 9999;
2958}
2959
2960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 * Move the cursor to position "row","col" in the screen.
2962 * This tries to find the most efficient way to move, minimizing the number of
2963 * characters sent to the terminal.
2964 */
2965 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002966windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002968 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 int i;
2970 int plan;
2971 int cost;
2972 int wouldbe_col;
2973 int noinvcurs;
2974 char_u *bs;
2975 int goto_cost;
2976 int attr;
2977
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002978#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
2979#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
2981#define PLAN_LE 1
2982#define PLAN_CR 2
2983#define PLAN_NL 3
2984#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002985 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 if (ScreenLines == NULL)
2987 return;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002988 if (col == screen_cur_col && row == screen_cur_row)
2989 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002991 // Check for valid position.
2992 if (row < 0) // window without text lines?
2993 row = 0;
2994 if (row >= screen_Rows)
2995 row = screen_Rows - 1;
2996 if (col >= screen_Columns)
2997 col = screen_Columns - 1;
2998
2999 // check if no cursor movement is allowed in highlight mode
3000 if (screen_attr && *T_MS == NUL)
3001 noinvcurs = HIGHL_COST;
3002 else
3003 noinvcurs = 0;
3004 goto_cost = GOTO_COST + noinvcurs;
3005
3006 /*
3007 * Plan how to do the positioning:
3008 * 1. Use CR to move it to column 0, same row.
3009 * 2. Use T_LE to move it a few columns to the left.
3010 * 3. Use NL to move a few lines down, column 0.
3011 * 4. Move a few columns to the right with T_ND or by writing chars.
3012 *
3013 * Don't do this if the cursor went beyond the last column, the cursor
3014 * position is unknown then (some terminals wrap, some don't )
3015 *
3016 * First check if the highlighting attributes allow us to write
3017 * characters to move the cursor to the right.
3018 */
3019 if (row >= screen_cur_row && screen_cur_col < Columns)
3020 {
3021 /*
3022 * If the cursor is in the same row, bigger col, we can use CR
3023 * or T_LE.
3024 */
3025 bs = NULL; // init for GCC
3026 attr = screen_attr;
3027 if (row == screen_cur_row && col < screen_cur_col)
3028 {
3029 // "le" is preferred over "bc", because "bc" is obsolete
3030 if (*T_LE)
3031 bs = T_LE; // "cursor left"
3032 else
3033 bs = T_BC; // "backspace character (old)
3034 if (*bs)
3035 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3036 else
3037 cost = 999;
3038 if (col + 1 < cost) // using CR is less characters
3039 {
3040 plan = PLAN_CR;
3041 wouldbe_col = 0;
3042 cost = 1; // CR is just one character
3043 }
3044 else
3045 {
3046 plan = PLAN_LE;
3047 wouldbe_col = col;
3048 }
3049 if (noinvcurs) // will stop highlighting
3050 {
3051 cost += noinvcurs;
3052 attr = 0;
3053 }
3054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
3056 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003057 * If the cursor is above where we want to be, we can use CR LF.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003059 else if (row > screen_cur_row)
3060 {
3061 plan = PLAN_NL;
3062 wouldbe_col = 0;
3063 cost = (row - screen_cur_row) * 2; // CR LF
3064 if (noinvcurs) // will stop highlighting
3065 {
3066 cost += noinvcurs;
3067 attr = 0;
3068 }
3069 }
3070
3071 /*
3072 * If the cursor is in the same row, smaller col, just use write.
3073 */
3074 else
3075 {
3076 plan = PLAN_WRITE;
3077 wouldbe_col = screen_cur_col;
3078 cost = 0;
3079 }
3080
3081 /*
3082 * Check if any characters that need to be written have the
3083 * correct attributes. Also avoid UTF-8 characters.
3084 */
3085 i = col - wouldbe_col;
3086 if (i > 0)
3087 cost += i;
3088 if (cost < goto_cost && i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
3090 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003091 * Check if the attributes are correct without additionally
3092 * stopping highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003094 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3095 while (i && *p++ == attr)
3096 --i;
3097 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 {
3099 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003100 * Try if it works when highlighting is stopped here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003102 if (*--p == 0)
3103 {
3104 cost += noinvcurs;
3105 while (i && *p++ == 0)
3106 --i;
3107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (i != 0)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003109 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003111 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003113 // Don't use an UTF-8 char for positioning, it's slow.
3114 for (i = wouldbe_col; i < col; ++i)
3115 if (ScreenLinesUC[LineOffset[row] + i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003117 cost = 999;
3118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003120 }
3121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003123 /*
3124 * We can do it without term_windgoto()!
3125 */
3126 if (cost < goto_cost)
3127 {
3128 if (plan == PLAN_LE)
3129 {
3130 if (noinvcurs)
3131 screen_stop_highlight();
3132 while (screen_cur_col > col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003134 out_str(bs);
3135 --screen_cur_col;
3136 }
3137 }
3138 else if (plan == PLAN_CR)
3139 {
3140 if (noinvcurs)
3141 screen_stop_highlight();
3142 out_char('\r');
3143 screen_cur_col = 0;
3144 }
3145 else if (plan == PLAN_NL)
3146 {
3147 if (noinvcurs)
3148 screen_stop_highlight();
3149 while (screen_cur_row < row)
3150 {
3151 out_char('\n');
3152 ++screen_cur_row;
3153 }
3154 screen_cur_col = 0;
3155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003157 i = col - screen_cur_col;
3158 if (i > 0)
3159 {
3160 /*
3161 * Use cursor-right if it's one character only. Avoids
3162 * removing a line of pixels from the last bold char, when
3163 * using the bold trick in the GUI.
3164 */
3165 if (T_ND[0] != NUL && T_ND[1] == NUL)
3166 {
3167 while (i-- > 0)
3168 out_char(*T_ND);
3169 }
3170 else
3171 {
3172 int off;
3173
3174 off = LineOffset[row] + screen_cur_col;
3175 while (i-- > 0)
3176 {
3177 if (ScreenAttrs[off] != screen_attr)
3178 screen_stop_highlight();
3179 out_flush_check();
3180 out_char(ScreenLines[off]);
3181 if (enc_dbcs == DBCS_JPNU
3182 && ScreenLines[off] == 0x8e)
3183 out_char(ScreenLines2[off]);
3184 ++off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186 }
3187 }
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003190 else
3191 cost = 999;
3192
3193 if (cost >= goto_cost)
3194 {
3195 if (noinvcurs)
3196 screen_stop_highlight();
3197 if (row == screen_cur_row && (col > screen_cur_col)
3198 && *T_CRI != NUL)
3199 term_cursor_right(col - screen_cur_col);
3200 else
3201 term_windgoto(row, col);
3202 }
3203 screen_cur_row = row;
3204 screen_cur_col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205}
3206
3207/*
3208 * Set cursor to its position in the current window.
3209 */
3210 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003211setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003213 setcursor_mayforce(FALSE);
3214}
3215
3216/*
3217 * Set cursor to its position in the current window.
3218 * When "force" is TRUE also when not redrawing.
3219 */
3220 void
3221setcursor_mayforce(int force)
3222{
3223 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 {
3225 validate_cursor();
3226 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003227 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003229 // With 'rightleft' set and the cursor on a double-wide
3230 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003231 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3232 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003233 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003234 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235#endif
3236 curwin->w_wcol));
3237 }
3238}
3239
3240
3241/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003242 * Insert 'line_count' lines at 'row' in window 'wp'.
3243 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3244 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 * scrolling.
3246 * Returns FAIL if the lines are not inserted, OK for success.
3247 */
3248 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003249win_ins_lines(
3250 win_T *wp,
3251 int row,
3252 int line_count,
3253 int invalid,
3254 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 int did_delete;
3257 int nextrow;
3258 int lastrow;
3259 int retval;
3260
3261 if (invalid)
3262 wp->w_lines_valid = 0;
3263
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003264 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (wp->w_height < 5)
3266 return FAIL;
3267
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003268 // with the popup menu visible this might not work correctly
3269 if (pum_visible())
3270 return FAIL;
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 if (line_count > wp->w_height - row)
3273 line_count = wp->w_height - row;
3274
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003275 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (retval != MAYBE)
3277 return retval;
3278
3279 /*
3280 * If there is a next window or a status line, we first try to delete the
3281 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003282 * If this fails and there are following windows, don't do anything to
3283 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 */
3285 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (wp->w_next != NULL || wp->w_status_height)
3287 {
3288 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003289 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 did_delete = TRUE;
3291 else if (wp->w_next)
3292 return FAIL;
3293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 /*
3295 * if no lines deleted, blank the lines that will end up below the window
3296 */
3297 if (!did_delete)
3298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003301 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 lastrow = nextrow + line_count;
3303 if (lastrow > Rows)
3304 lastrow = Rows;
3305 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003306 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 ' ', ' ', 0);
3308 }
3309
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003310 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 == FAIL)
3312 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003313 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 if (did_delete)
3315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 win_rest_invalid(W_NEXT(wp));
3318 }
3319 return FAIL;
3320 }
3321
3322 return OK;
3323}
3324
3325/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003326 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3328 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3329 * scrolling
3330 * Return OK for success, FAIL if the lines are not deleted.
3331 */
3332 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003333win_del_lines(
3334 win_T *wp,
3335 int row,
3336 int line_count,
3337 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003338 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003339 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 int retval;
3342
3343 if (invalid)
3344 wp->w_lines_valid = 0;
3345
3346 if (line_count > wp->w_height - row)
3347 line_count = wp->w_height - row;
3348
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003349 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 if (retval != MAYBE)
3351 return retval;
3352
3353 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003354 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 return FAIL;
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 /*
3358 * If there are windows or status lines below, try to put them at the
3359 * correct place. If we can't do that, they have to be redrawn.
3360 */
3361 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3362 {
3363 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003364 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
3366 wp->w_redr_status = TRUE;
3367 win_rest_invalid(wp->w_next);
3368 }
3369 }
3370 /*
3371 * If this is the last window and there is no status line, redraw the
3372 * command line later.
3373 */
3374 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 redraw_cmdline = TRUE;
3376 return OK;
3377}
3378
3379/*
3380 * Common code for win_ins_lines() and win_del_lines().
3381 * Returns OK or FAIL when the work has been done.
3382 * Returns MAYBE when not finished yet.
3383 */
3384 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003385win_do_lines(
3386 win_T *wp,
3387 int row,
3388 int line_count,
3389 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003390 int del,
3391 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 int retval;
3394
3395 if (!redrawing() || line_count <= 0)
3396 return FAIL;
3397
Bram Moolenaar33796b32019-06-08 16:01:13 +02003398 // When inserting lines would result in loss of command output, just redraw
3399 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003400 if (no_win_do_lines_ins && !del)
3401 return FAIL;
3402
Bram Moolenaar33796b32019-06-08 16:01:13 +02003403 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003404 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003406 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003407 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 return FAIL;
3409 }
3410
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003411#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003412 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003413 if (popup_visible)
3414 return FAIL;
3415#endif
3416
3417 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (row + line_count >= wp->w_height)
3419 {
3420 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003421 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 ' ', ' ', 0);
3423 return OK;
3424 }
3425
3426 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003427 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003429 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003431 if (!no_win_do_lines_ins)
3432 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 /*
3435 * If the terminal can set a scroll region, use that.
3436 * Always do this in a vertically split window. This will redraw from
3437 * ScreenLines[] when t_CV isn't defined. That's faster than using
3438 * win_line().
3439 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003440 * a character in the lower right corner of the scroll region may cause a
3441 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003443 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 scroll_region_set(wp, row);
3447 if (del)
3448 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003449 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 else
3451 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003452 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 scroll_region_reset();
3455 return retval;
3456 }
3457
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003458 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
3461 return MAYBE;
3462}
3463
3464/*
3465 * window 'wp' and everything after it is messed up, mark it for redraw
3466 */
3467 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003468win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003472 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 wp->w_redr_status = TRUE;
3474 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
3476 redraw_cmdline = TRUE;
3477}
3478
3479/*
3480 * The rest of the routines in this file perform screen manipulations. The
3481 * given operation is performed physically on the screen. The corresponding
3482 * change is also made to the internal screen image. In this way, the editor
3483 * anticipates the effect of editing changes on the appearance of the screen.
3484 * That way, when we call screenupdate a complete redraw isn't usually
3485 * necessary. Another advantage is that we can keep adding code to anticipate
3486 * screen changes, and in the meantime, everything still works.
3487 */
3488
3489/*
3490 * types for inserting or deleting lines
3491 */
3492#define USE_T_CAL 1
3493#define USE_T_CDL 2
3494#define USE_T_AL 3
3495#define USE_T_CE 4
3496#define USE_T_DL 5
3497#define USE_T_SR 6
3498#define USE_NL 7
3499#define USE_T_CD 8
3500#define USE_REDRAW 9
3501
3502/*
3503 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003504 * "end" is the line after the scrolled part. Normally it is Rows.
3505 * When scrolling region used "off" is the offset from the top for the region.
3506 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 *
3508 * return FAIL for failure, OK for success.
3509 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003510 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003511screen_ins_lines(
3512 int off,
3513 int row,
3514 int line_count,
3515 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003516 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003517 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
3519 int i;
3520 int j;
3521 unsigned temp;
3522 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003523 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 int type;
3525 int result_empty;
3526 int can_ce = can_clear(T_CE);
3527
3528 /*
3529 * FAIL if
3530 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 * - the line count is less than one
3532 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003533 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003534 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003535 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003537 if (!screen_valid(TRUE)
3538 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003539 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003540#ifdef FEAT_CLIPBOARD
3541 || (clip_star.state != SELECT_CLEARED
3542 && redrawing_for_callback > 0)
3543#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003544#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003545 || popup_visible
3546#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003547 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 return FAIL;
3549
3550 /*
3551 * There are seven ways to insert lines:
3552 * 0. When in a vertically split window and t_CV isn't set, redraw the
3553 * characters from ScreenLines[].
3554 * 1. Use T_CD (clear to end of display) if it exists and the result of
3555 * the insert is just empty lines
3556 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3557 * present or line_count > 1. It looks better if we do all the inserts
3558 * at once.
3559 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3560 * insert is just empty lines and T_CE is not present or line_count >
3561 * 1.
3562 * 4. Use T_AL (insert line) if it exists.
3563 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3564 * just empty lines.
3565 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3566 * just empty lines.
3567 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3568 * the 'da' flag is not set or we have clear line capability.
3569 * 8. redraw the characters from ScreenLines[].
3570 *
3571 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3572 * the scrollbar for the window. It does have insert line, use that if it
3573 * exists.
3574 */
3575 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003577 {
3578 // Avoid that lines are first cleared here and then redrawn, which
3579 // results in many characters updated twice. This happens with CTRL-F
3580 // in a vertically split window. With line-by-line scrolling
3581 // USE_REDRAW should be faster.
3582 if (line_count > 3)
3583 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003585 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003586 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 type = USE_T_CD;
3588 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3589 type = USE_T_CAL;
3590 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3591 type = USE_T_CDL;
3592 else if (*T_AL != NUL)
3593 type = USE_T_AL;
3594 else if (can_ce && result_empty)
3595 type = USE_T_CE;
3596 else if (*T_DL != NUL && result_empty)
3597 type = USE_T_DL;
3598 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3599 type = USE_T_SR;
3600 else
3601 return FAIL;
3602
3603 /*
3604 * For clearing the lines screen_del_lines() is used. This will also take
3605 * care of t_db if necessary.
3606 */
3607 if (type == USE_T_CD || type == USE_T_CDL ||
3608 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003609 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
3611 /*
3612 * If text is retained below the screen, first clear or delete as many
3613 * lines at the bottom of the window as are about to be inserted so that
3614 * the deleted lines won't later surface during a screen_del_lines.
3615 */
3616 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003617 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
3619#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003620 // Remove a modeless selection when inserting lines halfway the screen
3621 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003622 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003623 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 else
3625 clip_scroll_selection(-line_count);
3626#endif
3627
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003628#ifdef FEAT_GUI_HAIKU
3629 vim_lock_screen();
3630#endif
3631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003633 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3634 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003635 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636#endif
3637
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003638 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3639 cursor_col = wp->w_wincol;
3640
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003641 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 cursor_row = row;
3643 else
3644 cursor_row = row + off;
3645
3646 /*
3647 * Shift LineOffset[] line_count down to reflect the inserted lines.
3648 * Clear the inserted lines in ScreenLines[].
3649 */
3650 row += off;
3651 end += off;
3652 for (i = 0; i < line_count; ++i)
3653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (wp != NULL && wp->w_width != Columns)
3655 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003656 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 j = end - 1 - i;
3658 while ((j -= line_count) >= row)
3659 linecopy(j + line_count, j, wp);
3660 j += line_count;
3661 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003662 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3663 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 else
3665 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3666 LineWraps[j] = FALSE;
3667 }
3668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
3670 j = end - 1 - i;
3671 temp = LineOffset[j];
3672 while ((j -= line_count) >= row)
3673 {
3674 LineOffset[j + line_count] = LineOffset[j];
3675 LineWraps[j + line_count] = LineWraps[j];
3676 }
3677 LineOffset[j + line_count] = temp;
3678 LineWraps[j + line_count] = FALSE;
3679 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003680 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 else
3682 lineinvalid(temp, (int)Columns);
3683 }
3684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003686#ifdef FEAT_GUI_HAIKU
3687 vim_unlock_screen();
3688#endif
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003691 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003692 if (clear_attr != 0)
3693 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003695 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (type == USE_REDRAW)
3697 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003698 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
3700 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003701 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 else
3704 {
3705 for (i = 0; i < line_count; i++)
3706 {
3707 if (type == USE_T_AL)
3708 {
3709 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003710 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 out_str(T_AL);
3712 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003713 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003715 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 }
3718
3719 /*
3720 * With scroll-reverse and 'da' flag set we need to clear the lines that
3721 * have been scrolled down into the region.
3722 */
3723 if (type == USE_T_SR && *T_DA)
3724 {
3725 for (i = 0; i < line_count; ++i)
3726 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003727 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003729 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731 }
3732
3733#ifdef FEAT_GUI
3734 gui_can_update_cursor();
3735 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003736 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737#endif
3738 return OK;
3739}
3740
3741/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003742 * Delete lines on the screen and update ScreenLines[].
3743 * "end" is the line after the scrolled part. Normally it is Rows.
3744 * When scrolling region used "off" is the offset from the top for the region.
3745 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 *
3747 * Return OK for success, FAIL if the lines are not deleted.
3748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003750screen_del_lines(
3751 int off,
3752 int row,
3753 int line_count,
3754 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003755 int force, // even when line_count > p_ttyscroll
3756 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003757 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
3759 int j;
3760 int i;
3761 unsigned temp;
3762 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003763 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003765 int result_empty; // result is empty until end of region
3766 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 int type;
3768
3769 /*
3770 * FAIL if
3771 * - there is no valid screen
3772 * - the screen has to be redrawn completely
3773 * - the line count is less than one
3774 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003775 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003776 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003778 if (!screen_valid(TRUE)
3779 || line_count <= 0
3780 || (!force && line_count > p_ttyscroll)
3781 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003782#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003783 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003784#endif
3785 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787
3788 /*
3789 * Check if the rest of the current region will become empty.
3790 */
3791 result_empty = row + line_count >= end;
3792
3793 /*
3794 * We can delete lines only when 'db' flag not set or when 'ce' option
3795 * available.
3796 */
3797 can_delete = (*T_DB == NUL || can_clear(T_CE));
3798
3799 /*
3800 * There are six ways to delete lines:
3801 * 0. When in a vertically split window and t_CV isn't set, redraw the
3802 * characters from ScreenLines[].
3803 * 1. Use T_CD if it exists and the result is empty.
3804 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3805 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3806 * none of the other ways work.
3807 * 4. Use T_CE (erase line) if the result is empty.
3808 * 5. Use T_DL (delete line) if it exists.
3809 * 6. redraw the characters from ScreenLines[].
3810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003812 {
3813 // Avoid that lines are first cleared here and then redrawn, which
3814 // results in many characters updated twice. This happens with CTRL-F
3815 // in a vertically split window. With line-by-line scrolling
3816 // USE_REDRAW should be faster.
3817 if (line_count > 3)
3818 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003820 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003821 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 else if (row == 0 && (
3824#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003825 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3826 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 line_count == 1 ||
3828#endif
3829 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 type = USE_NL;
3831 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3832 type = USE_T_CDL;
3833 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003834 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 type = USE_T_CE;
3836 else if (*T_DL != NUL && can_delete)
3837 type = USE_T_DL;
3838 else if (*T_CDL != NUL && can_delete)
3839 type = USE_T_CDL;
3840 else
3841 return FAIL;
3842
3843#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003844 // Remove a modeless selection when deleting lines halfway the screen or
3845 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003846 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003847 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 else
3849 clip_scroll_selection(line_count);
3850#endif
3851
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003852#ifdef FEAT_GUI_HAIKU
3853 vim_lock_screen();
3854#endif
3855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003857 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3858 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003859 gui_dont_update_cursor(gui.cursor_row >= row + off
3860 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861#endif
3862
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003863 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3864 cursor_col = wp->w_wincol;
3865
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003866 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
3868 cursor_row = row;
3869 cursor_end = end;
3870 }
3871 else
3872 {
3873 cursor_row = row + off;
3874 cursor_end = end + off;
3875 }
3876
3877 /*
3878 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3879 * Clear the inserted lines in ScreenLines[].
3880 */
3881 row += off;
3882 end += off;
3883 for (i = 0; i < line_count; ++i)
3884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 if (wp != NULL && wp->w_width != Columns)
3886 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003887 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 j = row + i;
3889 while ((j += line_count) <= end - 1)
3890 linecopy(j - line_count, j, wp);
3891 j -= line_count;
3892 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003893 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3894 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 else
3896 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3897 LineWraps[j] = FALSE;
3898 }
3899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003901 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 j = row + i;
3903 temp = LineOffset[j];
3904 while ((j += line_count) <= end - 1)
3905 {
3906 LineOffset[j - line_count] = LineOffset[j];
3907 LineWraps[j - line_count] = LineWraps[j];
3908 }
3909 LineOffset[j - line_count] = temp;
3910 LineWraps[j - line_count] = FALSE;
3911 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003912 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 else
3914 lineinvalid(temp, (int)Columns);
3915 }
3916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003918#ifdef FEAT_GUI_HAIKU
3919 vim_unlock_screen();
3920#endif
3921
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003922 if (screen_attr != clear_attr)
3923 screen_stop_highlight();
3924 if (clear_attr != 0)
3925 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003927 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 if (type == USE_REDRAW)
3929 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003930 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003932 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003934 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936 else if (type == USE_T_CDL)
3937 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003938 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003940 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942 /*
3943 * Deleting lines at top of the screen or scroll region: Just scroll
3944 * the whole screen (scroll region) up by outputting newlines on the
3945 * last line.
3946 */
3947 else if (type == USE_NL)
3948 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003949 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003951 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953 else
3954 {
3955 for (i = line_count; --i >= 0; )
3956 {
3957 if (type == USE_T_DL)
3958 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003959 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003960 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003962 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003964 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003965 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003967 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
3970
3971 /*
3972 * If the 'db' flag is set, we need to clear the lines that have been
3973 * scrolled up at the bottom of the region.
3974 */
3975 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
3976 {
3977 for (i = line_count; i > 0; --i)
3978 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003979 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003980 out_str(T_CE); // erase a line
3981 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 }
3983 }
3984
3985#ifdef FEAT_GUI
3986 gui_can_update_cursor();
3987 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003988 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989#endif
3990
3991 return OK;
3992}
3993
3994/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003995 * Return TRUE when postponing displaying the mode message: when not redrawing
3996 * or inside a mapping.
3997 */
3998 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003999skip_showmode(void)
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004000{
4001 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004002 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004003 if (global_busy
4004 || msg_silent != 0
4005 || !redrawing()
4006 || (char_avail() && !KeyTyped))
4007 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004008 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004009 return TRUE;
4010 }
4011 return FALSE;
4012}
4013
4014/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004015 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 *
4017 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4018 * If clear_cmdline is FALSE there may be a message there that needs to be
4019 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004020 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * Return the length of the message (0 if no message).
4022 */
4023 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004024showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 int need_clear;
4027 int length = 0;
4028 int do_mode;
4029 int attr;
4030 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaara2a89732022-08-31 14:46:18 +01004033 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004034 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004035 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004036 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004037 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004039 if (skip_showmode())
4040 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
4042 nwr_save = need_wait_return;
4043
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004044 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 check_for_delay(FALSE);
4046
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004047 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 need_clear = clear_cmdline;
4049 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004050 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004052 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 msg_pos_mode();
4054 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004055 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (do_mode)
4057 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004058 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004060 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004061# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004062 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004063# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004064 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004065# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004066 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004067# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004068 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004070 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071# endif
4072#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004073 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004074 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004076 // These messages can get long, avoid a wrap in a narrow
4077 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 length = (Rows - msg_row) * Columns - 3;
4079 if (edit_submode_extra != NULL)
4080 length -= vim_strsize(edit_submode_extra);
4081 if (length > 0)
4082 {
4083 if (edit_submode_pre != NULL)
4084 length -= vim_strsize(edit_submode_pre);
4085 if (length - vim_strsize(edit_submode) > 0)
4086 {
4087 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004088 msg_puts_attr((char *)edit_submode_pre, attr);
4089 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 if (edit_submode_extra != NULL)
4092 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004093 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004095 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 else
4097 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004098 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004105 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004106 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004107 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004108 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
4110#ifdef FEAT_RIGHTLEFT
4111 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004112 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004114 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004116 else if (restart_edit == 'I' || restart_edit == 'i' ||
4117 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004118 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004120 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004122 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123#ifdef FEAT_RIGHTLEFT
4124 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004125 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126#endif
4127#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004128 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130# ifdef FEAT_ARABIC
4131 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004132 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 else
4134# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004135 if (get_keymap_str(curwin, (char_u *)" (%s)",
4136 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004137 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004140 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004141 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (VIsual_active)
4144 {
4145 char *p;
4146
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004147 // Don't concatenate separate words to avoid translation
4148 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 switch ((VIsual_select ? 4 : 0)
4150 + (VIsual_mode == Ctrl_V) * 2
4151 + (VIsual_mode == 'V'))
4152 {
4153 case 0: p = N_(" VISUAL"); break;
4154 case 1: p = N_(" VISUAL LINE"); break;
4155 case 2: p = N_(" VISUAL BLOCK"); break;
4156 case 4: p = N_(" SELECT"); break;
4157 case 5: p = N_(" SELECT LINE"); break;
4158 default: p = N_(" SELECT BLOCK"); break;
4159 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004160 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004162 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 need_clear = TRUE;
4166 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004167 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004168 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004170 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 need_clear = TRUE;
4172 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004173
4174 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004175 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004177 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 length = msg_col;
4179 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004180 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004183 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004185 else if (redraw_mode)
4186 {
4187 msg_pos_mode();
4188 msg_clr_eos();
4189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004191 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (VIsual_active)
4193 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004195 // If the last window has no status line, the ruler is after the mode
4196 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004197 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004198 win_redr_ruler(lastwin, TRUE, FALSE);
Martin Tournoijba43e762022-10-13 22:12:15 +01004199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004201 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 clear_cmdline = FALSE;
4203
4204 return length;
4205}
4206
4207/*
4208 * Position for a mode message.
4209 */
4210 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004211msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
4213 msg_col = 0;
4214 msg_row = Rows - 1;
4215}
4216
4217/*
4218 * Delete mode message. Used when ESC is typed which is expected to end
4219 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004220 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 */
4222 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004223unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
4225 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004226 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 */
4228 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004229 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004231 clearmode();
4232}
4233
4234/*
4235 * Clear the mode message.
4236 */
4237 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004238clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004239{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004240 int save_msg_row = msg_row;
4241 int save_msg_col = msg_col;
4242
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004243 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004244 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004245 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004246 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004247
4248 msg_col = save_msg_col;
4249 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250}
4251
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004252 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004253recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004254{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004255 msg_puts_attr(_("recording"), attr);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004256 if (shortmess(SHM_RECORDING))
4257 return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004258
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004259 char s[4];
4260
4261 sprintf(s, " @%c", reg_recording);
4262 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004263}
4264
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004265/*
4266 * Draw the tab pages line at the top of the Vim window.
4267 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004268 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004269draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004270{
4271 int tabcount = 0;
4272 tabpage_T *tp;
4273 int tabwidth;
4274 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004275 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004276 int attr;
4277 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004278 win_T *cwp;
4279 int wincount;
4280 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004281 int c;
4282 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004283 int attr_sel = HL_ATTR(HLF_TPS);
4284 int attr_nosel = HL_ATTR(HLF_TP);
4285 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004286 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004287 int room;
4288 int use_sep_chars = (t_colors < 8
4289#ifdef FEAT_GUI
4290 && !gui.in_use
4291#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004292#ifdef FEAT_TERMGUICOLORS
4293 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004294#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004295 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004296
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004297 if (ScreenLines == NULL)
4298 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004299 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004300
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004301#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004302 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004303 if (gui_use_tabline())
4304 {
4305 gui_update_tabline();
4306 return;
4307 }
4308#endif
4309
4310 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004311 return;
4312
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004313#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004314 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004315
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004316 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004317 if (*p_tal != NUL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004318 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004319 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004320#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004321 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004322 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004323 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004324
Bram Moolenaar238a5642006-02-21 22:12:05 +00004325 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4326 if (tabwidth < 6)
4327 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004328
Bram Moolenaar238a5642006-02-21 22:12:05 +00004329 attr = attr_nosel;
4330 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004331 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4332 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004333 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004334 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004335
Bram Moolenaar238a5642006-02-21 22:12:05 +00004336 if (tp->tp_topframe == topframe)
4337 attr = attr_sel;
4338 if (use_sep_chars && col > 0)
4339 screen_putchar('|', 0, col++, attr);
4340
4341 if (tp->tp_topframe != topframe)
4342 attr = attr_nosel;
4343
4344 screen_putchar(' ', 0, col++, attr);
4345
4346 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004347 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004348 cwp = curwin;
4349 wp = firstwin;
4350 }
4351 else
4352 {
4353 cwp = tp->tp_curwin;
4354 wp = tp->tp_firstwin;
4355 }
4356
4357 modified = FALSE;
4358 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4359 if (bufIsChanged(wp->w_buffer))
4360 modified = TRUE;
4361 if (modified || wincount > 1)
4362 {
4363 if (wincount > 1)
4364 {
4365 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004366 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004367 if (col + len >= Columns - 3)
4368 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004369 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004370#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004371 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004372#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004373 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004374#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004375 );
4376 col += len;
4377 }
4378 if (modified)
4379 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4380 screen_putchar(' ', 0, col++, attr);
4381 }
4382
4383 room = scol - col + tabwidth - 1;
4384 if (room > 0)
4385 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004386 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004387 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004388 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004389 len = vim_strsize(NameBuff);
4390 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004391 if (has_mbyte)
4392 while (len > room)
4393 {
4394 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004395 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004396 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004397 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004398 {
4399 p += len - room;
4400 len = room;
4401 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004402 if (len > Columns - col - 1)
4403 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004404
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004405 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004406 col += len;
4407 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004408 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004409
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004410 // Store the tab page number in TabPageIdxs[], so that
4411 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004412 ++tabcount;
4413 while (scol < col)
4414 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004415 }
4416
Bram Moolenaar238a5642006-02-21 22:12:05 +00004417 if (use_sep_chars)
4418 c = '_';
4419 else
4420 c = ' ';
4421 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004422
Luuk van Baalba936f62022-12-15 13:15:39 +00004423 // Draw the 'showcmd' information if 'showcmdloc' == "tabline".
4424 if (p_sc && *p_sloc == 't')
4425 {
4426 int width = MIN(10, (int)Columns - col - (tabcount > 1) * 3);
4427
4428 if (width > 0)
4429 screen_puts_len(showcmd_buf, width, 0, (int)Columns
4430 - width - (tabcount > 1) * 2, attr_nosel);
4431 }
4432
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004433 // Put an "X" for closing the current tab if there are several.
Luuk van Baalba936f62022-12-15 13:15:39 +00004434 if (tabcount > 1)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004435 {
4436 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4437 TabPageIdxs[Columns - 1] = -999;
4438 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004439 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004440
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004441 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4442 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004443 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004444}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004445
4446/*
4447 * Get buffer name for "buf" into NameBuff[].
4448 * Takes care of special buffer names and translates special characters.
4449 */
4450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004451get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004452{
4453 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004454 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004455 else
4456 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4457 trans_characters(NameBuff, MAXPATHL);
4458}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004459
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460/*
4461 * Get the character to use in a status line. Get its attributes in "*attr".
4462 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004463 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004464fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465{
4466 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004467
4468#ifdef FEAT_TERMINAL
4469 if (bt_terminal(wp->w_buffer))
4470 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004471 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004472 {
4473 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004474 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004475 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004476 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004477 {
4478 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004479 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004480 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004481 }
4482 else
4483#endif
4484 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004486 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004487 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 else
4490 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004491 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004492 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
Christian Brabandt6a650bf2023-11-08 21:23:29 +01004494 return fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497/*
4498 * Get the character to use in a separator between vertically split windows.
4499 * Get its attributes in "*attr".
4500 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004501 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004502fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004504 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004505 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 return '|';
4507 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004508 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511/*
4512 * Return TRUE if redrawing should currently be done.
4513 */
4514 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004515redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004517#ifdef FEAT_EVAL
4518 if (disable_redraw_for_testing)
4519 return 0;
4520 else
4521#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004522 return ((RedrawingDisabled == 0
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004523#ifdef FEAT_EVAL
4524 || ignore_redraw_flag_for_testing
4525#endif
4526 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527}
4528
4529/*
4530 * Return TRUE if printing messages should currently be done.
4531 */
4532 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004533messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004535 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536}
4537
Bram Moolenaare677df82019-09-02 22:31:11 +02004538/*
4539 * Compute columns for ruler and shown command. 'sc_col' is also used to
4540 * decide what the maximum length of a message on the status line can be.
4541 * If there is a status line for the last window, 'sc_col' is independent
4542 * of 'ru_col'.
4543 */
4544
4545#define COL_RULER 17 // columns needed by standard ruler
4546
4547 void
4548comp_col(void)
4549{
Sean Dewar876f5fb2023-08-17 22:40:05 +02004550 int last_has_status = last_stl_height(FALSE) > 0;
Bram Moolenaare677df82019-09-02 22:31:11 +02004551
4552 sc_col = 0;
4553 ru_col = 0;
4554 if (p_ru)
4555 {
Martin Tournoijba43e762022-10-13 22:12:15 +01004556#ifdef FEAT_STL_OPT
Bram Moolenaare677df82019-09-02 22:31:11 +02004557 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004558#else
Bram Moolenaare677df82019-09-02 22:31:11 +02004559 ru_col = COL_RULER + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004560#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004561 // no last status line, adjust sc_col
4562 if (!last_has_status)
4563 sc_col = ru_col;
4564 }
Sam-programs062141b2024-02-29 17:40:29 +01004565 if (p_sc && *p_sloc == 'l')
Bram Moolenaare677df82019-09-02 22:31:11 +02004566 {
4567 sc_col += SHOWCMD_COLS;
4568 if (!p_ru || last_has_status) // no need for separating space
4569 ++sc_col;
4570 }
4571 sc_col = Columns - sc_col;
4572 ru_col = Columns - ru_col;
4573 if (sc_col <= 0) // screen too narrow, will become a mess
4574 sc_col = 1;
4575 if (ru_col <= 0)
4576 ru_col = 1;
Bram Moolenaare677df82019-09-02 22:31:11 +02004577#ifdef FEAT_EVAL
4578 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4579#endif
4580}
4581
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004582#if defined(FEAT_LINEBREAK) || defined(PROTO)
4583/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004584 * Return the width of the 'number' and 'relativenumber' column.
4585 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004586 * Otherwise it depends on 'numberwidth' and the line count.
4587 */
4588 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004589number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004590{
4591 int n;
4592 linenr_T lnum;
4593
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004594 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004595 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004596 lnum = wp->w_height;
4597 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004598 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004599 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004600
Bram Moolenaar6b314672015-03-20 15:42:10 +01004601 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004602 return wp->w_nrwidth_width;
4603 wp->w_nrwidth_line_count = lnum;
4604
4605 n = 0;
4606 do
4607 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004608 lnum /= 10;
4609 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004610 } while (lnum > 0);
4611
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004612 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004613 if (n < wp->w_p_nuw - 1)
4614 n = wp->w_p_nuw - 1;
4615
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004616# ifdef FEAT_SIGNS
4617 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4618 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004619 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004620 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4621 n = 2;
4622# endif
4623
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004624 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004625 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004626 return n;
4627}
4628#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004629
Bram Moolenaar113e1072019-01-20 15:30:40 +01004630#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004631/*
4632 * Return the current cursor column. This is the actual position on the
4633 * screen. First column is 0.
4634 */
4635 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004636screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004637{
4638 return screen_cur_col;
4639}
4640
4641/*
4642 * Return the current cursor row. This is the actual position on the screen.
4643 * First row is 0.
4644 */
4645 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004646screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004647{
4648 return screen_cur_row;
4649}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004650#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004651
4652/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004653 * Calls mb_ptr2char_adv(p) and returns the character.
4654 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4655 */
4656 static int
4657get_encoded_char_adv(char_u **p)
4658{
4659 char_u *s = *p;
4660
4661 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4662 {
4663 varnumber_T num = 0;
4664 int bytes;
4665 int n;
4666
4667 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4668 {
4669 *p += 2;
4670 n = hexhex2nr(*p);
4671 if (n < 0)
4672 return 0;
4673 num = num * 256 + n;
4674 }
4675 *p += 2;
4676 return num;
4677 }
4678 return mb_ptr2char_adv(p);
4679}
4680
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004681struct charstab
4682{
4683 int *cp;
4684 char *name;
4685};
4686static fill_chars_T fill_chars;
4687static struct charstab filltab[] =
4688{
4689 {&fill_chars.stl, "stl"},
4690 {&fill_chars.stlnc, "stlnc"},
4691 {&fill_chars.vert, "vert"},
4692 {&fill_chars.fold, "fold"},
4693 {&fill_chars.foldopen, "foldopen"},
4694 {&fill_chars.foldclosed, "foldclose"},
4695 {&fill_chars.foldsep, "foldsep"},
4696 {&fill_chars.diff, "diff"},
4697 {&fill_chars.eob, "eob"},
4698 {&fill_chars.lastline, "lastline"},
4699};
4700static lcs_chars_T lcs_chars;
4701static struct charstab lcstab[] =
4702{
4703 {&lcs_chars.eol, "eol"},
4704 {&lcs_chars.ext, "extends"},
4705 {&lcs_chars.nbsp, "nbsp"},
4706 {&lcs_chars.prec, "precedes"},
4707 {&lcs_chars.space, "space"},
4708 {&lcs_chars.tab2, "tab"},
4709 {&lcs_chars.trail, "trail"},
4710 {&lcs_chars.lead, "lead"},
4711#ifdef FEAT_CONCEAL
4712 {&lcs_chars.conceal, "conceal"},
4713#else
4714 {NULL, "conceal"},
4715#endif
zeertzjq1f025b02023-09-30 12:43:07 +02004716 {NULL, "multispace"},
4717 {NULL, "leadmultispace"},
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004718};
4719
zeertzjq6a8d2e12024-01-17 20:54:49 +01004720 static char *
4721field_value_err(char *errbuf, size_t errbuflen, char *fmt, char *field)
4722{
4723 if (errbuf == NULL)
4724 return "";
4725 vim_snprintf(errbuf, errbuflen, _(fmt), field);
4726 return errbuf;
4727}
4728
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004729/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004730 * Handle setting 'listchars' or 'fillchars'.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004731 * "value" points to either the global or the window-local value.
zeertzjqd619d6a2023-05-08 15:56:21 +01004732 * "is_listchars" is TRUE for "listchars" and FALSE for "fillchars".
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004733 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004734 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004735 * Returns error message, NULL if it's OK.
4736 */
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004737 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004738set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
4739 char *errbuf, size_t errbuflen)
Bram Moolenaare677df82019-09-02 22:31:11 +02004740{
zeertzjq1f025b02023-09-30 12:43:07 +02004741 int round, i, len, entries;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004742 char_u *p, *s;
4743 int c1 = 0, c2 = 0, c3 = 0;
4744 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4745 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4746 int multispace_len = 0; // Length of lcs-multispace string
4747 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004748
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004749 struct charstab *tab;
4750
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004751 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004752 {
4753 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004754 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004755 entries = ARRAY_LENGTH(lcstab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004756 if (wp->w_p_lcs[0] == NUL)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004757 value = p_lcs; // local value is empty, use the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004758 }
4759 else
4760 {
4761 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004762 entries = ARRAY_LENGTH(filltab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004763 if (wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004764 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004765 }
4766
4767 // first round: check for valid value, second round: assign values
zeertzjq00624a22023-11-23 20:47:16 +01004768 for (round = 0; round <= (apply ? 1 : 0); ++round)
Bram Moolenaare677df82019-09-02 22:31:11 +02004769 {
4770 if (round > 0)
4771 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004772 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004773 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004774 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004775 for (i = 0; i < entries; ++i)
4776 if (tab[i].cp != NULL)
4777 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004778 lcs_chars.tab1 = NUL;
4779 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004780
Mike Williamsf5785cf2021-09-13 22:17:38 +02004781 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004782 {
4783 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004784 if (lcs_chars.multispace != NULL)
4785 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004786 }
4787 else
4788 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004789
4790 if (lead_multispace_len > 0)
4791 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004792 lcs_chars.leadmultispace =
4793 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004794 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
4795 }
4796 else
4797 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004798 }
4799 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004800 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004801 fill_chars.stl = ' ';
4802 fill_chars.stlnc = ' ';
4803 fill_chars.vert = ' ';
4804 fill_chars.fold = '-';
4805 fill_chars.foldopen = '-';
4806 fill_chars.foldclosed = '+';
4807 fill_chars.foldsep = '|';
4808 fill_chars.diff = '-';
4809 fill_chars.eob = '~';
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01004810 fill_chars.lastline = '@';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004811 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004812 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004813 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02004814 while (*p)
4815 {
4816 for (i = 0; i < entries; ++i)
4817 {
4818 len = (int)STRLEN(tab[i].name);
zeertzjq6a8d2e12024-01-17 20:54:49 +01004819 if (!(STRNCMP(p, tab[i].name, len) == 0 && p[len] == ':'))
zeertzjq1f025b02023-09-30 12:43:07 +02004820 continue;
Bram Moolenaare677df82019-09-02 22:31:11 +02004821
zeertzjq1f025b02023-09-30 12:43:07 +02004822 if (is_listchars && strcmp(tab[i].name, "multispace") == 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004823 {
4824 s = p + len + 1;
4825 if (round == 0)
4826 {
4827 // Get length of lcs-multispace string in first round
4828 last_multispace = p;
4829 multispace_len = 0;
4830 while (*s != NUL && *s != ',')
4831 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004832 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004833 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004834 return field_value_err(errbuf, errbuflen,
4835 e_wrong_character_width_for_field_str,
4836 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004837 ++multispace_len;
4838 }
4839 if (multispace_len == 0)
4840 // lcs-multispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004841 return field_value_err(errbuf, errbuflen,
4842 e_wrong_number_of_characters_for_field_str,
4843 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004844 p = s;
4845 }
4846 else
4847 {
4848 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02004849
zeertzjqf14b8ba2021-09-10 16:58:30 +02004850 while (*s != NUL && *s != ',')
4851 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004852 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004853 if (p == last_multispace)
4854 lcs_chars.multispace[multispace_pos++] = c1;
4855 }
4856 p = s;
4857 }
zeertzjq1f025b02023-09-30 12:43:07 +02004858 break;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004859 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004860
zeertzjq1f025b02023-09-30 12:43:07 +02004861 if (is_listchars && strcmp(tab[i].name, "leadmultispace") == 0)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004862 {
zeertzjq1f025b02023-09-30 12:43:07 +02004863 s = p + len + 1;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004864 if (round == 0)
4865 {
zeertzjqb5f08012022-06-09 13:55:28 +01004866 // get length of lcs-leadmultispace string in first
4867 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004868 last_lmultispace = p;
4869 lead_multispace_len = 0;
4870 while (*s != NUL && *s != ',')
4871 {
4872 c1 = get_encoded_char_adv(&s);
4873 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004874 return field_value_err(errbuf, errbuflen,
4875 e_wrong_character_width_for_field_str,
4876 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004877 ++lead_multispace_len;
4878 }
4879 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01004880 // lcs-leadmultispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004881 return field_value_err(errbuf, errbuflen,
4882 e_wrong_number_of_characters_for_field_str,
4883 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004884 p = s;
4885 }
4886 else
4887 {
4888 int multispace_pos = 0;
4889
4890 while (*s != NUL && *s != ',')
4891 {
4892 c1 = get_encoded_char_adv(&s);
4893 if (p == last_lmultispace)
4894 lcs_chars.leadmultispace[multispace_pos++] = c1;
4895 }
4896 p = s;
4897 }
zeertzjq1f025b02023-09-30 12:43:07 +02004898 break;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004899 }
zeertzjq1f025b02023-09-30 12:43:07 +02004900
4901 c2 = c3 = 0;
4902 s = p + len + 1;
zeertzjq6a8d2e12024-01-17 20:54:49 +01004903 if (*s == NUL)
4904 return field_value_err(errbuf, errbuflen,
4905 e_wrong_number_of_characters_for_field_str,
4906 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004907 c1 = get_encoded_char_adv(&s);
4908 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004909 return field_value_err(errbuf, errbuflen,
4910 e_wrong_character_width_for_field_str,
4911 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004912 if (tab[i].cp == &lcs_chars.tab2)
4913 {
4914 if (*s == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004915 return field_value_err(errbuf, errbuflen,
4916 e_wrong_number_of_characters_for_field_str,
4917 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004918 c2 = get_encoded_char_adv(&s);
4919 if (char2cells(c2) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004920 return field_value_err(errbuf, errbuflen,
4921 e_wrong_character_width_for_field_str,
4922 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004923 if (!(*s == ',' || *s == NUL))
4924 {
4925 c3 = get_encoded_char_adv(&s);
4926 if (char2cells(c3) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004927 return field_value_err(errbuf, errbuflen,
4928 e_wrong_character_width_for_field_str,
4929 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004930 }
4931 }
4932
4933 if (*s == ',' || *s == NUL)
4934 {
4935 if (round > 0)
4936 {
4937 if (tab[i].cp == &lcs_chars.tab2)
4938 {
4939 lcs_chars.tab1 = c1;
4940 lcs_chars.tab2 = c2;
4941 lcs_chars.tab3 = c3;
4942 }
4943 else if (tab[i].cp != NULL)
4944 *(tab[i].cp) = c1;
4945
4946 }
4947 p = s;
4948 break;
4949 }
zeertzjq6a8d2e12024-01-17 20:54:49 +01004950 else
4951 return field_value_err(errbuf, errbuflen,
4952 e_wrong_number_of_characters_for_field_str,
4953 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004954 }
4955
zeertzjq1f025b02023-09-30 12:43:07 +02004956 if (i == entries)
4957 return e_invalid_argument;
4958
Bram Moolenaare677df82019-09-02 22:31:11 +02004959 if (*p == ',')
4960 ++p;
4961 }
4962 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004963
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004964 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004965 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004966 if (is_listchars)
4967 {
4968 vim_free(wp->w_lcs_chars.multispace);
4969 vim_free(wp->w_lcs_chars.leadmultispace);
4970 wp->w_lcs_chars = lcs_chars;
4971 }
4972 else
4973 {
4974 wp->w_fill_chars = fill_chars;
4975 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02004976 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004977
4978 return NULL; // no error
4979}
zeertzjq8ca29b62022-08-09 12:53:14 +01004980
4981/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004982 * Handle the new value of 'fillchars'.
4983 */
4984 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004985set_fillchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4986 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004987{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004988 return set_chars_option(wp, val, FALSE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004989}
4990
4991/*
4992 * Handle the new value of 'listchars'.
4993 */
4994 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004995set_listchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4996 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004997{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004998 return set_chars_option(wp, val, TRUE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004999}
5000
5001/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02005002 * Function given to ExpandGeneric() to obtain possible arguments of the
5003 * 'fillchars' option.
5004 */
5005 char_u *
5006get_fillchars_name(expand_T *xp UNUSED, int idx)
5007{
5008 if (idx >= (int)(sizeof(filltab) / sizeof(filltab[0])))
5009 return NULL;
5010
5011 return (char_u*)filltab[idx].name;
5012}
5013
5014/*
5015 * Function given to ExpandGeneric() to obtain possible arguments of the
5016 * 'listchars' option.
5017 */
5018 char_u *
5019get_listchars_name(expand_T *xp UNUSED, int idx)
5020{
5021 if (idx >= (int)(sizeof(lcstab) / sizeof(lcstab[0])))
5022 return NULL;
5023
5024 return (char_u*)lcstab[idx].name;
5025}
5026
5027/*
zeertzjq8ca29b62022-08-09 12:53:14 +01005028 * Check all global and local values of 'listchars' and 'fillchars'.
5029 * Return an untranslated error messages if any of them is invalid, NULL
5030 * otherwise.
5031 */
5032 char *
5033check_chars_options(void)
5034{
5035 tabpage_T *tp;
5036 win_T *wp;
5037
zeertzjq6a8d2e12024-01-17 20:54:49 +01005038 if (set_listchars_option(curwin, p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005039 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005040 if (set_fillchars_option(curwin, p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005041 return e_conflicts_with_value_of_fillchars;
5042 FOR_ALL_TAB_WINDOWS(tp, wp)
5043 {
zeertzjq6a8d2e12024-01-17 20:54:49 +01005044 if (set_listchars_option(wp, wp->w_p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005045 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005046 if (set_fillchars_option(wp, wp->w_p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005047 return e_conflicts_with_value_of_fillchars;
5048 }
5049 return NULL;
5050}