blob: 8fb17ae87a6360fccc0c6bdab35af08b5f89bc6b [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;
John Marriotta21240b2025-01-08 20:10:59 +0100971 int plen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
John Marriotta21240b2025-01-08 20:10:59 +0100974 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#ifdef FEAT_EVAL
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000977 buf_T *old_curbuf = curbuf;
978 win_T *old_curwin = curwin;
John Marriotta21240b2025-01-08 20:10:59 +0100979 char_u to_evaluate[] = "b:keymap_name";
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000980 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000982 curbuf = wp->w_buffer;
983 curwin = wp;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000984 ++emsg_skip;
John Marriotta21240b2025-01-08 20:10:59 +0100985 s = p = eval_to_string(to_evaluate, FALSE, FALSE);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000986 --emsg_skip;
987 curbuf = old_curbuf;
988 curwin = old_curwin;
989 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#ifdef FEAT_KEYMAP
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000993 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
994 p = wp->w_buffer->b_p_keymap;
995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000997 p = (char_u *)"lang";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 }
John Marriotta21240b2025-01-08 20:10:59 +0100999 plen = vim_snprintf((char *)buf, len, (char *)fmt, p);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001000#ifdef FEAT_EVAL
1001 vim_free(s);
1002#endif
John Marriotta21240b2025-01-08 20:10:59 +01001003 if (plen < 0 || plen > len - 1)
1004 {
1005 buf[0] = NUL;
1006 plen = 0;
1007 }
1008
1009 return plen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012#if defined(FEAT_STL_OPT) || defined(PROTO)
1013/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001014 * Redraw the status line or ruler of window "wp".
1015 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001018win_redr_custom(
1019 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001020 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001022 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 int attr;
1024 int curattr;
1025 int row;
1026 int col = 0;
1027 int maxwidth;
1028 int width;
1029 int n;
1030 int len;
1031 int fillchar;
1032 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001033 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 char_u *p;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001035 char_u *opt_name;
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001036 int opt_scope = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001037 stl_hlrec_T *hltab;
1038 stl_hlrec_T *tabtab;
Bram Moolenaar61452852011-02-01 18:01:11 +01001039 win_T *ewp;
1040 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001042 // There is a tiny chance that this gets called recursively: When
1043 // redrawing a status line triggers redrawing the ruler or tabline.
1044 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001045 if (entered)
1046 return;
1047 entered = TRUE;
1048
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001049 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001050 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001052 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001053 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001054 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001055 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001056 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001057 maxwidth = Columns;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001058 opt_name = (char_u *)"tabline";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001060 else
1061 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001062 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001063 fillchar = fillchar_status(&attr, wp);
Sean Dewarfc8a6012023-04-17 16:41:20 +01001064 int in_status_line = wp->w_status_height != 0;
1065 maxwidth = in_status_line ? wp->w_width : Columns;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001066
1067 if (draw_ruler)
1068 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001069 stl = p_ruf;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001070 opt_name = (char_u *)"rulerformat";
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001071 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001072 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001073 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001074 if (*++stl == '-')
1075 stl++;
1076 if (atoi((char *)stl))
1077 while (VIM_ISDIGIT(*stl))
1078 stl++;
1079 if (*stl++ != '(')
1080 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001081 }
Sean Dewarfc8a6012023-04-17 16:41:20 +01001082 col = ru_col - (Columns - maxwidth);
1083 if (col < (maxwidth + 1) / 2)
1084 col = (maxwidth + 1) / 2;
1085 maxwidth -= col;
1086 if (!in_status_line)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001087 {
1088 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001089 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001090 fillchar = ' ';
1091 attr = 0;
1092 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001093 }
1094 else
1095 {
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001096 opt_name = (char_u *)"statusline";
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001097 if (*wp->w_p_stl != NUL)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001098 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001099 stl = wp->w_p_stl;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001100 opt_scope = OPT_LOCAL;
1101 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001102 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001103 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001104 }
1105
Sean Dewarfc8a6012023-04-17 16:41:20 +01001106 if (in_status_line)
1107 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001108 }
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001111 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001113 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1114 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001115 ewp = wp == NULL ? curwin : wp;
1116 p_crb_save = ewp->w_p_crb;
1117 ewp->w_p_crb = FALSE;
1118
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001119 // Make a copy, because the statusline may include a function call that
1120 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001121 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001122 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001123 stl, opt_name, opt_scope,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001124 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001125 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001126 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001128 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001129 p = transstr(buf);
1130 if (p != NULL)
1131 {
1132 vim_strncpy(buf, p, sizeof(buf) - 1);
1133 vim_free(p);
1134 }
1135
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001136 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001137 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001138 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 ++width;
1142 }
1143 buf[len] = NUL;
1144
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001145 /*
1146 * Draw each snippet with the specified highlighting.
1147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 curattr = attr;
1149 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001150 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001152 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 screen_puts_len(p, len, row, col, curattr);
1154 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001155 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001157 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001159 else if (hltab[n].userhl < 0)
1160 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001161#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001162 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1163 && wp->w_status_height != 0)
1164 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001165 else if (wp != NULL && bt_terminal(wp->w_buffer)
1166 && wp->w_status_height != 0)
1167 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001168#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001169 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001170 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001172 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001175
1176 if (wp == NULL)
1177 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001178 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001179 col = 0;
1180 len = 0;
1181 p = buf;
1182 fillchar = 0;
1183 for (n = 0; tabtab[n].start != NULL; n++)
1184 {
1185 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1186 while (col < len)
1187 TabPageIdxs[col++] = fillchar;
1188 p = tabtab[n].start;
1189 fillchar = tabtab[n].userhl;
1190 }
1191 while (col < Columns)
1192 TabPageIdxs[col++] = fillchar;
1193 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001194
1195theend:
1196 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197}
1198
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001199#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201/*
1202 * Output a single character directly to the screen and update ScreenLines.
1203 */
1204 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001205screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 char_u buf[MB_MAXBYTES + 1];
1208
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001209 if (has_mbyte)
1210 buf[(*mb_char2bytes)(c, buf)] = NUL;
1211 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001212 {
1213 buf[0] = c;
1214 buf[1] = NUL;
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 screen_puts(buf, row, col, attr);
1217}
1218
1219/*
zeertzjq47eec672023-06-01 20:26:55 +01001220 * Get a single character directly from ScreenLines into "bytes", which must
1221 * have a size of "MB_MAXBYTES + 1".
1222 * If "attrp" is not NULL, return the character's attribute in "*attrp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
1224 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001225screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226{
1227 unsigned off;
1228
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001229 // safety check
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001230 if (ScreenLines == NULL || row >= screen_Rows || col >= screen_Columns)
1231 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001233 off = LineOffset[row] + col;
zeertzjq47eec672023-06-01 20:26:55 +01001234 if (attrp != NULL)
1235 *attrp = ScreenAttrs[off];
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001236 bytes[0] = ScreenLines[off];
1237 bytes[1] = NUL;
1238
1239 if (enc_utf8 && ScreenLinesUC[off] != 0)
1240 bytes[utfc_char2bytes(off, bytes)] = NUL;
1241 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1242 {
1243 bytes[0] = ScreenLines[off];
1244 bytes[1] = ScreenLines2[off];
1245 bytes[2] = NUL;
1246 }
1247 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1248 {
1249 bytes[1] = ScreenLines[off + 1];
1250 bytes[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
1252}
1253
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001254/*
1255 * Return TRUE if composing characters for screen posn "off" differs from
1256 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001257 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001258 */
1259 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001260screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001261{
1262 int i;
1263
1264 for (i = 0; i < Screen_mco; ++i)
1265 {
1266 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1267 return TRUE;
1268 if (u8cc[i] == 0)
1269 break;
1270 }
1271 return FALSE;
1272}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274/*
1275 * Put string '*text' on the screen at position 'row' and 'col', with
1276 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1277 * Note: only outputs within one row, message is truncated at screen boundary!
1278 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1279 */
1280 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001281screen_puts(
1282 char_u *text,
1283 int row,
1284 int col,
1285 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 screen_puts_len(text, -1, row, col, attr);
1288}
1289
1290/*
1291 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1292 * a NUL.
1293 */
1294 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001295screen_puts_len(
1296 char_u *text,
1297 int textlen,
1298 int row,
1299 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001300 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001302 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 unsigned off;
1304 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001305 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001307 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 int mbyte_blen = 1;
1309 int mbyte_cells = 1;
1310 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001311 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001313#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001314 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001315 int pc, nc, nc1;
1316 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001318 int force_redraw_this;
1319 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001320 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001322 // Safety check. The check for negative row and column is to fix issue
1323 // #4102. TODO: find out why row/col could be negative.
1324 if (ScreenLines == NULL
1325 || row >= screen_Rows || row < 0
1326 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001328 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001330 // When drawing over the right half of a double-wide char clear out the
1331 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001332 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001333#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001334 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001335#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001336 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001337 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001338 if (!skip_for_popup(row, col - 1))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001339 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001340 ScreenLines[off - 1] = ' ';
1341 ScreenAttrs[off - 1] = 0;
1342 if (enc_utf8)
1343 {
1344 ScreenLinesUC[off - 1] = 0;
1345 ScreenLinesC[0][off - 1] = 0;
1346 }
1347 // redraw the previous cell, make it empty
1348 screen_char(off - 1, row, col - 1);
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001349 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001350 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001351 force_redraw_next = TRUE;
1352 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001353
Bram Moolenaar367329b2007-08-30 11:53:22 +00001354 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001355 while (col < screen_Columns
1356 && (len < 0 || (int)(ptr - text) < len)
1357 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001360 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 if (has_mbyte)
1362 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001363 mbyte_blen = enc_utf8 && len > 0
1364 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1365 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1367 mbyte_cells = 1;
1368 else if (enc_dbcs != 0)
1369 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001370 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001372 u8c = len >= 0
1373 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1374 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001376#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1378 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001379 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1381 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001382 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 nc = NUL;
1384 nc1 = NUL;
1385 }
1386 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001387 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001388 nc = len >= 0
1389 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1390 (int)((text + len) - ptr - mbyte_blen))
1391 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001392 nc1 = pcc[0];
1393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 pc = prev_c;
1395 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 else
1399 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001400#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001401 if (col + mbyte_cells > screen_Columns)
1402 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001403 // Only 1 cell left, but character requires 2 cells:
1404 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001405 c = '>';
1406 mbyte_cells = 1;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001411 force_redraw_this = force_redraw_next;
1412 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001413
1414 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 || (mbyte_cells == 2
1416 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1417 || (enc_dbcs == DBCS_JPNU
1418 && c == 0x8e
1419 && ScreenLines2[off] != ptr[1])
1420 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001421 && (ScreenLinesUC[off] !=
1422 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1423 || (ScreenLinesUC[off] != 0
1424 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001426 || exmode_active;
1427
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001428 if ((need_redraw || force_redraw_this) && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
1430#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001431 // The bold trick makes a single row of pixels appear in the next
1432 // character. When a bold character is removed, the next
1433 // character should be redrawn too. This happens for our own GUI
1434 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001435 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436# ifdef FEAT_GUI
1437 gui.in_use
1438# endif
1439# if defined(FEAT_GUI) && defined(UNIX)
1440 ||
1441# endif
1442# ifdef UNIX
1443 term_is_xterm
1444# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001445 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001447 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001449 if (n > HL_ALL)
1450 n = syn_attr2attr(n);
1451 if (n & HL_BOLD)
1452 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001455 // When at the end of the text and overwriting a two-cell
1456 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001457 // cell. Also when overwriting the left half of a two-cell char
1458 // with the right half of a two-cell char. Do this only once
1459 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (clear_next_cell)
1461 clear_next_cell = FALSE;
1462 else if (has_mbyte
1463 && (len < 0 ? ptr[mbyte_blen] == NUL
1464 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001465 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001467 && (*mb_off2cells)(off, max_off) == 1
1468 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 clear_next_cell = TRUE;
1470
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001471 // Make sure we never leave a second byte of a double-byte behind,
1472 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001474 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001476 && (*mb_off2cells)(off, max_off) == 1
1477 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 ScreenLines[off] = c;
1480 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001481 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (enc_utf8)
1483 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001484 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 ScreenLinesUC[off] = 0;
1486 else
1487 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001488 int i;
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001491 for (i = 0; i < Screen_mco; ++i)
1492 {
1493 ScreenLinesC[i][off] = u8cc[i];
1494 if (u8cc[i] == 0)
1495 break;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 if (mbyte_cells == 2)
1499 {
1500 ScreenLines[off + 1] = 0;
1501 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001502 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 screen_char(off, row, col);
1505 }
1506 else if (mbyte_cells == 2)
1507 {
1508 ScreenLines[off + 1] = ptr[1];
1509 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001510 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 screen_char_2(off, row, col);
1512 }
1513 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1514 {
1515 ScreenLines2[off] = ptr[1];
1516 screen_char(off, row, col);
1517 }
1518 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 screen_char(off, row, col);
1520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (has_mbyte)
1522 {
1523 off += mbyte_cells;
1524 col += mbyte_cells;
1525 ptr += mbyte_blen;
1526 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001527 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001528 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001529 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001531 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001532 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 ++off;
1538 ++col;
1539 ++ptr;
1540 }
1541 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001542
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001543 // If we detected the next character needs to be redrawn, but the text
1544 // doesn't extend up to there, update the character here.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001545 if (force_redraw_next && col < screen_Columns && !skip_for_popup(row, col))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001546 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001547 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1548 screen_char_2(off, row, col);
1549 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001550 screen_char(off, row, col);
1551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552}
1553
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001554#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001556 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001558 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001559start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001561 if (!p_hls || no_hlsearch)
1562 return;
1563
1564 end_search_hl(); // just in case it wasn't called before
1565 last_pat_prog(&screen_search_hl.rm);
1566 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567}
1568
1569/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001570 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001572 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001573end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001575 if (screen_search_hl.rm.regprog == NULL)
1576 return;
1577
1578 vim_regfree(screen_search_hl.rm.regprog);
1579 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001581#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001582
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001584screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 attrentry_T *aep = NULL;
1587
1588 screen_attr = attr;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001589 if (!full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001590#ifdef MSWIN
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001591 || !termcap_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001593 )
1594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001596#ifdef FEAT_GUI
1597 if (gui.in_use)
1598 {
1599 char buf[20];
1600
1601 // The GUI handles this internally.
1602 sprintf(buf, "\033|%dh", attr);
1603 OUT_STR(buf);
1604 return;
1605 }
1606#endif
1607
1608 if (attr > HL_ALL) // special HL attr.
1609 {
1610 if (IS_CTERM)
1611 aep = syn_cterm_attr2entry(attr);
1612 else
1613 aep = syn_term_attr2entry(attr);
1614 if (aep == NULL) // did ":syntax clear"
1615 attr = 0;
1616 else
1617 attr = aep->ae_attr;
1618 }
1619#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1620 if (use_vtp())
1621 {
1622 guicolor_T defguifg, defguibg;
1623 int defctermfg, defctermbg;
1624
1625 // If FG and BG are unset, the color is undefined when
1626 // BOLD+INVERSE. Use Normal as the default value.
1627 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1628 &defguibg);
1629
1630 if (p_tgc)
1631 {
1632 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1633 term_fg_rgb_color(defguifg);
1634 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1635 term_bg_rgb_color(defguibg);
1636 }
1637 else if (t_colors >= 256)
1638 {
1639 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1640 term_fg_color(defctermfg);
1641 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1642 term_bg_color(defctermbg);
1643 }
1644 }
1645#endif
1646 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
1647 out_str(T_MD);
1648 else if (aep != NULL && cterm_normal_fg_bold && (
1649#ifdef FEAT_TERMGUICOLORS
1650 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1651 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1652 :
1653#endif
1654 t_colors > 1 && aep->ae_u.cterm.fg_color))
1655 // If the Normal FG color has BOLD attribute and the new HL
1656 // has a FG color defined, clear BOLD.
1657 out_str(T_ME);
1658 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
1659 out_str(T_SO);
1660 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
1661 out_str(T_UCS);
1662 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1663 out_str(T_USS);
1664 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1665 out_str(T_DS);
1666 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1667 out_str(T_CDS);
1668 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1669 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1670 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1671 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1672 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
1673 && *T_US != NUL)
1674 out_str(T_US);
1675 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
1676 out_str(T_CZH);
1677 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
1678 out_str(T_MR);
1679 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
1680 out_str(T_STS);
1681
1682 /*
1683 * Output the color or start string after bold etc., in case the
1684 * bold etc. override the color setting.
1685 */
1686 if (aep != NULL)
1687 {
PMuncha606f3a2023-11-15 15:35:49 +01001688 if (aep->ae_u.cterm.font > 0 && aep->ae_u.cterm.font < 12)
1689 term_font(aep->ae_u.cterm.font);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001690#ifdef FEAT_TERMGUICOLORS
1691 // When 'termguicolors' is set but fg or bg is unset,
1692 // fall back to the cterm colors. This helps for SpellBad,
1693 // where the GUI uses a red undercurl.
1694 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
1695 {
1696 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
1697 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699 else
1700#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001701 if (t_colors > 1)
1702 {
1703 if (aep->ae_u.cterm.fg_color)
1704 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1705 }
1706#ifdef FEAT_TERMGUICOLORS
1707 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001709 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
1710 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
1711 }
1712 else
1713#endif
1714 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001716 if (aep->ae_u.cterm.bg_color)
1717 term_bg_color(aep->ae_u.cterm.bg_color - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001719#ifdef FEAT_TERMGUICOLORS
1720 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1721 {
1722 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1723 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1724 }
1725 else
1726#endif
1727 if (t_colors > 1)
Bram Moolenaara050b942019-12-02 21:35:31 +01001728 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001729 if (aep->ae_u.cterm.ul_color)
1730 term_ul_color(aep->ae_u.cterm.ul_color - 1);
Bram Moolenaara050b942019-12-02 21:35:31 +01001731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001733 if (!IS_CTERM)
1734 {
1735 if (aep->ae_u.term.start != NULL)
1736 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
1738 }
1739}
1740
1741 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001742screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001744 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001745#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001746 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001750#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 && termcap_active
1752#endif
1753 )
1754 {
1755#ifdef FEAT_GUI
1756 if (gui.in_use)
1757 {
1758 char buf[20];
1759
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001760 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001761 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 OUT_STR(buf);
1763 }
1764 else
1765#endif
1766 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001767 int is_under;
1768
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001769 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
1771 attrentry_T *aep;
1772
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001773 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 {
1775 /*
1776 * Assume that t_me restores the original colors!
1777 */
1778 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001779 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001780#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001781 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1782 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001783# ifdef FEAT_VTP
1784 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1785# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001786 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001787#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001788 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001789#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001790 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1791 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001792# ifdef FEAT_VTP
1793 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1794# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001795 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001796#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001797 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001799#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1800 if (use_vtp())
1801 {
1802 if (do_ME_fg && do_ME_bg)
1803 do_ME = TRUE;
1804
1805 // FG and BG cannot be separated in T_ME, which is not
1806 // efficient.
1807 if (!do_ME && do_ME_fg)
1808 out_str((char_u *)"\033|39m"); // restore FG
1809 if (!do_ME && do_ME_bg)
1810 out_str((char_u *)"\033|49m"); // restore BG
1811 }
1812 else
1813 {
1814 // Process FG and BG at once.
1815 if (!do_ME)
1816 do_ME = do_ME_fg | do_ME_bg;
1817 }
1818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820 else
1821 {
1822 aep = syn_term_attr2entry(screen_attr);
1823 if (aep != NULL && aep->ae_u.term.stop != NULL)
1824 {
1825 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1826 do_ME = TRUE;
1827 else
1828 out_str(aep->ae_u.term.stop);
1829 }
1830 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001831 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 screen_attr = 0;
1833 else
1834 screen_attr = aep->ae_attr;
1835 }
1836
1837 /*
1838 * Often all ending-codes are equal to T_ME. Avoid outputting the
1839 * same sequence several times.
1840 */
1841 if (screen_attr & HL_STANDOUT)
1842 {
1843 if (STRCMP(T_SE, T_ME) == 0)
1844 do_ME = TRUE;
1845 else
1846 out_str(T_SE);
1847 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001848 is_under = (screen_attr & (HL_UNDERCURL
1849 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
1850 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001851 {
1852 if (STRCMP(T_UCE, T_ME) == 0)
1853 do_ME = TRUE;
1854 else
1855 out_str(T_UCE);
1856 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001857 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
1859 if (STRCMP(T_UE, T_ME) == 0)
1860 do_ME = TRUE;
1861 else
1862 out_str(T_UE);
1863 }
1864 if (screen_attr & HL_ITALIC)
1865 {
1866 if (STRCMP(T_CZR, T_ME) == 0)
1867 do_ME = TRUE;
1868 else
1869 out_str(T_CZR);
1870 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001871 if (screen_attr & HL_STRIKETHROUGH)
1872 {
1873 if (STRCMP(T_STE, T_ME) == 0)
1874 do_ME = TRUE;
1875 else
1876 out_str(T_STE);
1877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
1879 out_str(T_ME);
1880
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001881#ifdef FEAT_TERMGUICOLORS
1882 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001884 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001885 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001886 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001887 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02001888 if (cterm_normal_ul_gui_color != INVALCOLOR)
1889 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001890 }
1891 else
1892#endif
1893 {
1894 if (t_colors > 1)
1895 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001896 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001897 if (cterm_normal_fg_color != 0)
1898 term_fg_color(cterm_normal_fg_color - 1);
1899 if (cterm_normal_bg_color != 0)
1900 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02001901 if (cterm_normal_ul_color != 0)
1902 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001903 if (cterm_normal_fg_bold)
1904 out_str(T_MD);
1905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 }
1908 }
1909 screen_attr = 0;
1910}
1911
1912/*
1913 * Reset the colors for a cterm. Used when leaving Vim.
1914 * The machine specific code may override this again.
1915 */
1916 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001917reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001919 if (!IS_CTERM)
1920 return;
1921
1922 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001923#ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001924 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
1925 || cterm_normal_bg_gui_color != INVALCOLOR)
1926 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001927#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
1931 out_str(T_OP);
1932 screen_attr = -1;
1933 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001934 if (cterm_normal_fg_bold)
1935 {
1936 out_str(T_ME);
1937 screen_attr = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939}
1940
1941/*
1942 * Put character ScreenLines["off"] on the screen at position "row" and "col",
1943 * using the attributes from ScreenAttrs["off"].
1944 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001945 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001946screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
1948 int attr;
1949
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001950 // Check for illegal values, just in case (could happen just after
1951 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (row >= screen_Rows || col >= screen_Columns)
1953 return;
1954
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001955 // Outputting a character in the last cell on the screen may scroll the
1956 // screen up. Only do it when the "xn" termcap property is set, otherwise
1957 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01001958 if (*T_XN == NUL
1959 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001961 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 && !cmdmsg_rl
1963#endif
1964 )
1965 {
1966 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001967 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 return;
1969 }
1970
1971 /*
1972 * Stop highlighting first, so it's easier to move the cursor.
1973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (screen_char_attr != 0)
1975 attr = screen_char_attr;
1976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 attr = ScreenAttrs[off];
1978 if (screen_attr != attr)
1979 screen_stop_highlight();
1980
1981 windgoto(row, col);
1982
1983 if (screen_attr != attr)
1984 screen_start_highlight(attr);
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (enc_utf8 && ScreenLinesUC[off] != 0)
1987 {
1988 char_u buf[MB_MAXBYTES + 1];
1989
h-east8927c9b2024-04-20 17:57:19 +02001990 if (
1991#ifdef FEAT_GUI
1992 !gui.in_use &&
1993#endif
1994 get_cellwidth(ScreenLinesUC[off]) > 1
1995 )
mikoto2000e20fa592024-04-17 22:06:54 +02001996 {
h-east8927c9b2024-04-20 17:57:19 +02001997 // If the width is set to 2 with setcellwidths()
1998 // clear the two screen cells. If the character is actually
1999 // single width it won't change the second cell.
2000 out_str((char_u *)" ");
2001 term_windgoto(row, col);
2002 screen_cur_col = 9999;
mikoto2000e20fa592024-04-17 22:06:54 +02002003 }
2004 else if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002005 {
2006 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002007#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002008 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002009#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002010 )
2011 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002012 // Clear the two screen cells. If the character is actually
2013 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002014 out_str((char_u *)" ");
2015 term_windgoto(row, col);
2016 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002017 // not sure where the cursor is after drawing the ambiguous width
2018 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002019 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002020 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002021 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002023
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002024 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002025 buf[utfc_char2bytes(off, buf)] = NUL;
2026 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002032 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2034 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036
2037 screen_cur_col++;
2038}
2039
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040/*
2041 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2042 * on the screen at position 'row' and 'col'.
2043 * The attributes of the first byte is used for all. This is required to
2044 * output the two bytes of a double-byte character with nothing in between.
2045 */
2046 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002047screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002049 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2051 return;
2052
dundargocc57b5bc2022-11-02 13:30:51 +00002053 // Outputting the last character on the screen may scroll the screen up.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002054 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2056 {
2057 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002058 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 return;
2060 }
2061
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002062 // Output the first byte normally (positions the cursor), then write the
2063 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 screen_char(off, row, col);
2065 out_char(ScreenLines[off + 1]);
2066 ++screen_cur_col;
2067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069/*
2070 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2071 * This uses the contents of ScreenLines[] and doesn't change it.
2072 */
2073 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002074screen_draw_rectangle(
2075 int row,
2076 int col,
2077 int height,
2078 int width,
2079 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 int r, c;
2082 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002083 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002085 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002086 if (ScreenLines == NULL)
2087 return;
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 if (invert)
2090 screen_char_attr = HL_INVERSE;
2091 for (r = row; r < row + height; ++r)
2092 {
2093 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002094 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 for (c = col; c < col + width; ++c)
2096 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002097 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002099 if (!skip_for_popup(r, c))
2100 screen_char_2(off + c, r, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 ++c;
2102 }
2103 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002105 if (!skip_for_popup(r, c))
2106 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002107 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110 }
2111 }
2112 screen_char_attr = 0;
2113}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115/*
2116 * Redraw the characters for a vertically split window.
2117 */
2118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002119redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121 int col;
2122 int width;
2123
2124# ifdef FEAT_CLIPBOARD
2125 clip_may_clear_selection(row, end - 1);
2126# endif
2127
2128 if (wp == NULL)
2129 {
2130 col = 0;
2131 width = Columns;
2132 }
2133 else
2134 {
2135 col = wp->w_wincol;
2136 width = wp->w_width;
2137 }
2138 screen_draw_rectangle(row, col, end - row, width, FALSE);
2139}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002141 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002142space_to_screenline(int off, int attr)
2143{
2144 ScreenLines[off] = ' ';
2145 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002146 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002147 if (enc_utf8)
2148 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002149}
2150
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002152 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2153 * to "end_col" (exclusive) with character "c1" in first column followed by
2154 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 */
2156 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002157screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002158 int start_row,
2159 int end_row,
2160 int start_col,
2161 int end_col,
2162 int c1,
2163 int c2,
2164 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002166 int row;
2167 int col;
2168 int off;
2169 int end_off;
2170 int did_delete;
2171 int c;
2172 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002174 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#endif
2176
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002177 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002179 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 end_col = screen_Columns;
2181 if (ScreenLines == NULL
2182 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002183 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 return;
2185
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002186 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 norm_term = (
2188#ifdef FEAT_GUI
2189 !gui.in_use &&
2190#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002191 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 for (row = start_row; row < end_row; ++row)
2193 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002194 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002195#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002196 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002197#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002198 )
2199 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002200 // When drawing over the right half of a double-wide char clear
2201 // out the left half. When drawing over the left half of a
2202 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002203 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002204 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002205 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002206 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002207 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 /*
2210 * Try to use delete-line termcap code, when no attributes or in a
2211 * "normal" terminal, where a bold/italic space is just a
2212 * space.
2213 */
2214 did_delete = FALSE;
2215 if (c2 == ' '
2216 && end_col == Columns
2217 && can_clear(T_CE)
2218 && (attr == 0
2219 || (norm_term
2220 && attr <= HL_ALL
2221 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2222 {
2223 /*
2224 * check if we really need to clear something
2225 */
2226 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002227 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 ++col;
2229
2230 off = LineOffset[row] + col;
2231 end_off = LineOffset[row] + end_col;
2232
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002233 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (enc_utf8)
2235 while (off < end_off && ScreenLines[off] == ' '
2236 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2237 ++off;
2238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 while (off < end_off && ScreenLines[off] == ' '
2240 && ScreenAttrs[off] == 0)
2241 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002242 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 col = off - LineOffset[row];
2245 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002246 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002248 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002250 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002252 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 ++off;
2254 }
2255 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002256 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258
2259 off = LineOffset[row] + start_col;
2260 c = c1;
2261 for (col = start_col; col < end_col; ++col)
2262 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002263 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002264 || (enc_utf8 && (int)ScreenLinesUC[off]
2265 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002267 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#if defined(FEAT_GUI) || defined(UNIX)
2269 || force_next
2270#endif
2271 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002272 // Skip if under a(nother) popup.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002273 && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002276 // The bold trick may make a single row of pixels appear in
2277 // the next character. When a bold character is removed, the
2278 // next character should be redrawn too. This happens for our
2279 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (
2281# ifdef FEAT_GUI
2282 gui.in_use
2283# endif
2284# if defined(FEAT_GUI) && defined(UNIX)
2285 ||
2286# endif
2287# ifdef UNIX
2288 term_is_xterm
2289# endif
2290 )
2291 {
2292 if (ScreenLines[off] != ' '
2293 && (ScreenAttrs[off] > HL_ALL
2294 || ScreenAttrs[off] & HL_BOLD))
2295 force_next = TRUE;
2296 else
2297 force_next = FALSE;
2298 }
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002299#endif // FEAT_GUI || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (enc_utf8)
2302 {
2303 if (c >= 0x80)
2304 {
2305 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002306 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308 else
2309 ScreenLinesUC[off] = 0;
2310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 ScreenAttrs[off] = attr;
2312 if (!did_delete || c != ' ')
2313 screen_char(off, row, col);
2314 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002315 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 ++off;
2317 if (col == start_col)
2318 {
2319 if (did_delete)
2320 break;
2321 c = c2;
2322 }
2323 }
2324 if (end_col == Columns)
2325 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002326 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002329 if (start_col == 0 && end_col == Columns
2330 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002331 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002332 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002333 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335 }
2336}
2337
2338/*
2339 * Check if there should be a delay. Used before clearing or redrawing the
2340 * screen or the command line.
2341 */
2342 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002343check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344{
2345 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2346 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002347 && emsg_silent == 0
2348 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002351 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 emsg_on_display = FALSE;
2353 if (check_msg_scroll)
2354 msg_scroll = FALSE;
2355 }
2356}
2357
2358/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002359 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2360 */
2361 static void
2362clear_TabPageIdxs(void)
2363{
2364 int scol;
2365
2366 for (scol = 0; scol < Columns; ++scol)
2367 TabPageIdxs[scol] = 0;
2368}
2369
2370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002372 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * Returns TRUE if there is a valid screen to write to.
2374 * Returns FALSE when starting up and screen not initialized yet.
2375 */
2376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002377screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002379 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 return (ScreenLines != NULL);
2381}
2382
2383/*
2384 * Resize the shell to Rows and Columns.
2385 * Allocate ScreenLines[] and associated items.
2386 *
2387 * There may be some time between setting Rows and Columns and (re)allocating
2388 * ScreenLines[]. This happens when starting up and when (manually) changing
2389 * the shell size. Always use screen_Rows and screen_Columns to access items
2390 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2391 * final size of the shell is needed.
2392 */
2393 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002394screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396 int new_row, old_row;
2397#ifdef FEAT_GUI
2398 int old_Rows;
2399#endif
2400 win_T *wp;
2401 int outofmem = FALSE;
2402 int len;
2403 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002405 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002408 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 unsigned *new_LineOffset;
2410 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002411 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002412#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002413 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002414 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002415 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002416#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002417 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002418 static int entered = FALSE; // avoid recursiveness
2419 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002420 int retry_count = 0;
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002421 int found_null;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002423retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 /*
2425 * Allocation of the screen buffers is done only when the size changes and
2426 * when Rows and Columns have been set and we have started doing full
2427 * screen stuff.
2428 */
2429 if ((ScreenLines != NULL
2430 && Rows == screen_Rows
2431 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 && enc_utf8 == (ScreenLinesUC != NULL)
2433 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002434 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 || Rows == 0
2436 || Columns == 0
2437 || (!full_screen && ScreenLines == NULL))
2438 return;
2439
2440 /*
2441 * It's possible that we produce an out-of-memory message below, which
2442 * will cause this function to be called again. To break the loop, just
2443 * return here.
2444 */
2445 if (entered)
2446 return;
2447 entered = TRUE;
2448
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002449 /*
2450 * Note that the window sizes are updated before reallocating the arrays,
2451 * thus we must not redraw here!
2452 */
2453 ++RedrawingDisabled;
2454
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002455 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002457#ifdef FEAT_GUI_HAIKU
2458 vim_lock_screen(); // be safe, put it here
2459#endif
2460
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002461 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
2463 /*
2464 * We're changing the size of the screen.
2465 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2466 * - Move lines from the old arrays into the new arrays, clear extra
2467 * lines (unless the screen is going to be cleared).
2468 * - Free the old arrays.
2469 *
2470 * If anything fails, make ScreenLines NULL, so we don't do anything!
2471 * Continuing with the old ScreenLines may result in a crash, because the
2472 * size is wrong.
2473 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002474 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 win_free_lsize(wp);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002476 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002477 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002478 win_free_lsize(aucmd_win[i].auc_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002479#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002480 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002481 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002482 win_free_lsize(wp);
2483 // tab-local popup windows
2484 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002485 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002486 win_free_lsize(wp);
2487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002489 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002490 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (enc_utf8)
2492 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002493 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002494 for (int i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002495 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2496 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
2498 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002499 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2500 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
dundargocc57b5bc2022-11-02 13:30:51 +00002501 // Clear ScreenCols to avoid a warning for uninitialized memory in
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002502 // jump_to_mouse().
2503 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002504 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2505 new_LineWraps = LALLOC_MULT(char_u, Rows);
2506 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002507#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002508 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002509 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002510 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002513 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
2515 if (win_alloc_lines(wp) == FAIL)
2516 {
2517 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002518 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00002521 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002522 if (aucmd_win[i].auc_win != NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002523 && aucmd_win[i].auc_win->w_lines == NULL
2524 && win_alloc_lines(aucmd_win[i].auc_win) == FAIL)
2525 {
2526 outofmem = TRUE;
2527 break;
2528 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002529#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002530 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002531 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002532 if (win_alloc_lines(wp) == FAIL)
2533 {
2534 outofmem = TRUE;
2535 goto give_up;
2536 }
2537 // tab-local popup windows
2538 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002539 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002540 if (win_alloc_lines(wp) == FAIL)
2541 {
2542 outofmem = TRUE;
2543 goto give_up;
2544 }
2545#endif
2546
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002547give_up:
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002548 found_null = FALSE;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002549 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002550 if (new_ScreenLinesC[i] == NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002551 {
2552 found_null = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002553 break;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if (new_ScreenLines == NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002556 || (enc_utf8 && (new_ScreenLinesUC == NULL || found_null))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002559 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 || new_LineOffset == NULL
2561 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002562 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002563#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002564 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002565 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002566 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 || outofmem)
2569 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002570 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002571 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002572 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002573 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2574
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002575 // Remember we did this to avoid getting outofmem messages over
2576 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002577 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002578 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002579 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002580 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002581 for (int i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002582 VIM_CLEAR(new_ScreenLinesC[i]);
2583 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002584 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002585 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002586 VIM_CLEAR(new_LineOffset);
2587 VIM_CLEAR(new_LineWraps);
2588 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002589#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002590 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002591 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002592 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595 else
2596 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002597 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 for (new_row = 0; new_row < Rows; ++new_row)
2600 {
2601 new_LineOffset[new_row] = new_row * Columns;
2602 new_LineWraps[new_row] = FALSE;
2603
Olaf Seibertfd472652024-02-01 21:11:16 +01002604 (void)vim_memset(new_ScreenLines + new_row * Columns,
2605 ' ', (size_t)Columns * sizeof(schar_T));
2606 if (enc_utf8)
2607 {
2608 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2609 0, (size_t)Columns * sizeof(u8char_T));
2610 for (int i = 0; i < p_mco; ++i)
2611 (void)vim_memset(new_ScreenLinesC[i]
2612 + new_row * Columns,
2613 0, (size_t)Columns * sizeof(u8char_T));
2614 }
2615 if (enc_dbcs == DBCS_JPNU)
2616 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2617 0, (size_t)Columns * sizeof(schar_T));
2618 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2619 0, (size_t)Columns * sizeof(sattr_T));
2620 (void)vim_memset(new_ScreenCols + new_row * Columns,
2621 0, (size_t)Columns * sizeof(colnr_T));
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 /*
2624 * If the screen is not going to be cleared, copy as much as
2625 * possible from the old screen to the new one and clear the rest
2626 * (used when resizing the window at the "--more--" prompt or when
2627 * executing an external command, for the GUI).
2628 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002629 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002632 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {
2634 if (screen_Columns < Columns)
2635 len = screen_Columns;
2636 else
2637 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002638 // When switching to utf-8 don't copy characters, they
2639 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002640 if (!(enc_utf8 && ScreenLinesUC == NULL)
2641 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002642 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2643 ScreenLines + LineOffset[old_row],
2644 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002645 if (enc_utf8 && ScreenLinesUC != NULL
2646 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
2648 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2649 ScreenLinesUC + LineOffset[old_row],
2650 (size_t)len * sizeof(u8char_T));
Bram Moolenaare76062c2022-11-28 18:51:43 +00002651 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002652 mch_memmove(new_ScreenLinesC[i]
2653 + new_LineOffset[new_row],
2654 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 (size_t)len * sizeof(u8char_T));
2656 }
2657 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2658 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2659 ScreenLines2 + LineOffset[old_row],
2660 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2662 ScreenAttrs + LineOffset[old_row],
2663 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002664 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2665 ScreenAttrs + LineOffset[old_row],
2666 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668 }
2669 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002670 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002672
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002673#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002674 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2675 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002679 free_screenlines();
2680
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002681 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002684 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002685 ScreenLinesC[i] = new_ScreenLinesC[i];
2686 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002689 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 LineOffset = new_LineOffset;
2691 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002692 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002693#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002694 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002695 popup_mask_next = new_popup_mask_next;
2696 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002697 popup_mask_refresh = TRUE;
2698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002700 // It's important that screen_Rows and screen_Columns reflect the actual
2701 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#ifdef FEAT_GUI
2703 old_Rows = screen_Rows;
2704#endif
2705 screen_Rows = Rows;
2706 screen_Columns = Columns;
2707
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002708 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002709 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002710 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711#ifdef FEAT_GUI
2712 else if (gui.in_use
2713 && !gui.starting
2714 && ScreenLines != NULL
2715 && old_Rows != Rows)
2716 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002717 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2718
2719 // Adjust the position of the cursor, for when executing an external
2720 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002721 if (msg_row >= Rows) // Rows got smaller
2722 msg_row = Rows - 1; // put cursor at last row
2723 else if (Rows > old_Rows) // Rows got bigger
2724 msg_row += Rows - old_Rows; // put cursor in same place
2725 if (msg_col >= Columns) // Columns got smaller
2726 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002729 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002731#ifdef FEAT_GUI_HAIKU
2732 vim_unlock_screen();
2733#endif
2734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 entered = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002736 if (RedrawingDisabled > 0)
2737 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002738
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002739 /*
2740 * Do not apply autocommands more than 3 times to avoid an endless loop
2741 * in case applying autocommands always changes Rows or Columns.
2742 */
2743 if (starting == 0 && ++retry_count <= 3)
2744 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002745 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002746 // In rare cases, autocommands may have altered Rows or Columns,
2747 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002748 goto retry;
2749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750}
2751
2752 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002753free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002754{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002755 int i;
2756
Bram Moolenaar33796b32019-06-08 16:01:13 +02002757 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002758 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002759 VIM_CLEAR(ScreenLinesC[i]);
2760 VIM_CLEAR(ScreenLines2);
2761 VIM_CLEAR(ScreenLines);
2762 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002763 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002764 VIM_CLEAR(LineOffset);
2765 VIM_CLEAR(LineWraps);
2766 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002767#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002768 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002769 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002770 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002771#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002772}
2773
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002774/*
2775 * Clear the screen.
2776 * May delay if there is something the user should read.
2777 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002778 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002779 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002780 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01002781 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002782screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002785 screenalloc(FALSE); // allocate screen buffers if size changed
2786 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002787}
2788
2789/*
2790 * Do not clear the screen but mark everything for redraw.
2791 */
2792 void
2793redraw_as_cleared(void)
2794{
2795 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796}
2797
Bram Moolenaar838b7462022-09-26 15:19:56 +01002798 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002799screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800{
2801 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002802 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
2804 if (starting == NO_SCREEN || ScreenLines == NULL
2805#ifdef FEAT_GUI
2806 || (gui.in_use && gui.starting)
2807#endif
2808 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01002809 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811#ifdef FEAT_GUI
2812 if (!gui.in_use)
2813#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002814 screen_attr = -1; // force setting the Normal colors
2815 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
2817#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002818 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 clip_scroll_selection(9999);
2820#endif
2821
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002822 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 for (i = 0; i < Rows; ++i)
2824 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002825 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 LineWraps[i] = FALSE;
2827 }
2828
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002829 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002831 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002832 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002834 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
2836 else
2837 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002838 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 for (i = 0; i < Rows; ++i)
2840 lineinvalid(LineOffset[i], (int)Columns);
2841 clear_cmdline = TRUE;
2842 }
2843
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002844 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002846 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002848 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002849 if (must_redraw == UPD_CLEAR) // no need to clear again
2850 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002851 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002853#ifdef FEAT_PROP_POPUP
2854 popup_redraw_all(); // redraw all popup windows
2855#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002856 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002858 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 msg_didany = FALSE;
2860 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002861
2862 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863}
2864
2865/*
2866 * Clear one line in ScreenLines.
2867 */
2868 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002869lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
2871 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (enc_utf8)
2873 (void)vim_memset(ScreenLinesUC + off, 0,
2874 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002875 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002876 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877}
2878
2879/*
2880 * Mark one line in ScreenLines invalid by setting the attributes to an
2881 * invalid value.
2882 */
2883 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002884lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002887 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888}
2889
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02002891 * To be called when characters were sent to the terminal directly, outputting
2892 * test on "screen_lnum".
2893 */
2894 void
2895line_was_clobbered(int screen_lnum)
2896{
2897 lineinvalid(LineOffset[screen_lnum], (int)Columns);
2898}
2899
2900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 * Copy part of a Screenline for vertically split window "wp".
2902 */
2903 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002904linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 unsigned off_to = LineOffset[to] + wp->w_wincol;
2907 unsigned off_from = LineOffset[from] + wp->w_wincol;
2908
2909 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
2910 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 if (enc_utf8)
2912 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002913 int i;
2914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
2916 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002917 for (i = 0; i < p_mco; ++i)
2918 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
2919 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
2921 if (enc_dbcs == DBCS_JPNU)
2922 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
2923 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
2925 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002926 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
2927 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930/*
2931 * Return TRUE if clearing with term string "p" would work.
2932 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02002933 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 */
2935 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002936can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937{
2938 return (*p != NUL && (t_colors <= 1
2939#ifdef FEAT_GUI
2940 || gui.in_use
2941#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002942#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002943 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02002944 || (!p_tgc && cterm_normal_bg_color == 0)
2945#else
2946 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002947#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002948 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002949#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002950 && !(p == T_CE && popup_visible)
2951#endif
2952 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953}
2954
2955/*
2956 * Reset cursor position. Use whenever cursor was moved because of outputting
2957 * something directly to the screen (shell commands) or a terminal control
2958 * code.
2959 */
2960 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002961screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 screen_cur_row = screen_cur_col = 9999;
2964}
2965
2966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 * Move the cursor to position "row","col" in the screen.
2968 * This tries to find the most efficient way to move, minimizing the number of
2969 * characters sent to the terminal.
2970 */
2971 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002972windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002974 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 int i;
2976 int plan;
2977 int cost;
2978 int wouldbe_col;
2979 int noinvcurs;
2980 char_u *bs;
2981 int goto_cost;
2982 int attr;
2983
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002984#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
2985#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987#define PLAN_LE 1
2988#define PLAN_CR 2
2989#define PLAN_NL 3
2990#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002991 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (ScreenLines == NULL)
2993 return;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002994 if (col == screen_cur_col && row == screen_cur_row)
2995 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002997 // Check for valid position.
2998 if (row < 0) // window without text lines?
2999 row = 0;
3000 if (row >= screen_Rows)
3001 row = screen_Rows - 1;
3002 if (col >= screen_Columns)
3003 col = screen_Columns - 1;
3004
3005 // check if no cursor movement is allowed in highlight mode
3006 if (screen_attr && *T_MS == NUL)
3007 noinvcurs = HIGHL_COST;
3008 else
3009 noinvcurs = 0;
3010 goto_cost = GOTO_COST + noinvcurs;
3011
3012 /*
3013 * Plan how to do the positioning:
3014 * 1. Use CR to move it to column 0, same row.
3015 * 2. Use T_LE to move it a few columns to the left.
3016 * 3. Use NL to move a few lines down, column 0.
3017 * 4. Move a few columns to the right with T_ND or by writing chars.
3018 *
3019 * Don't do this if the cursor went beyond the last column, the cursor
3020 * position is unknown then (some terminals wrap, some don't )
3021 *
3022 * First check if the highlighting attributes allow us to write
3023 * characters to move the cursor to the right.
3024 */
3025 if (row >= screen_cur_row && screen_cur_col < Columns)
3026 {
3027 /*
3028 * If the cursor is in the same row, bigger col, we can use CR
3029 * or T_LE.
3030 */
3031 bs = NULL; // init for GCC
3032 attr = screen_attr;
3033 if (row == screen_cur_row && col < screen_cur_col)
3034 {
3035 // "le" is preferred over "bc", because "bc" is obsolete
3036 if (*T_LE)
3037 bs = T_LE; // "cursor left"
3038 else
3039 bs = T_BC; // "backspace character (old)
3040 if (*bs)
3041 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3042 else
3043 cost = 999;
3044 if (col + 1 < cost) // using CR is less characters
3045 {
3046 plan = PLAN_CR;
3047 wouldbe_col = 0;
3048 cost = 1; // CR is just one character
3049 }
3050 else
3051 {
3052 plan = PLAN_LE;
3053 wouldbe_col = col;
3054 }
3055 if (noinvcurs) // will stop highlighting
3056 {
3057 cost += noinvcurs;
3058 attr = 0;
3059 }
3060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003063 * If the cursor is above where we want to be, we can use CR LF.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003065 else if (row > screen_cur_row)
3066 {
3067 plan = PLAN_NL;
3068 wouldbe_col = 0;
3069 cost = (row - screen_cur_row) * 2; // CR LF
3070 if (noinvcurs) // will stop highlighting
3071 {
3072 cost += noinvcurs;
3073 attr = 0;
3074 }
3075 }
3076
3077 /*
3078 * If the cursor is in the same row, smaller col, just use write.
3079 */
3080 else
3081 {
3082 plan = PLAN_WRITE;
3083 wouldbe_col = screen_cur_col;
3084 cost = 0;
3085 }
3086
3087 /*
3088 * Check if any characters that need to be written have the
3089 * correct attributes. Also avoid UTF-8 characters.
3090 */
3091 i = col - wouldbe_col;
3092 if (i > 0)
3093 cost += i;
3094 if (cost < goto_cost && i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 {
3096 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003097 * Check if the attributes are correct without additionally
3098 * stopping highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003100 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3101 while (i && *p++ == attr)
3102 --i;
3103 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
3105 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003106 * Try if it works when highlighting is stopped here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003108 if (*--p == 0)
3109 {
3110 cost += noinvcurs;
3111 while (i && *p++ == 0)
3112 --i;
3113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 if (i != 0)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003115 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003117 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003119 // Don't use an UTF-8 char for positioning, it's slow.
3120 for (i = wouldbe_col; i < col; ++i)
3121 if (ScreenLinesUC[LineOffset[row] + i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003123 cost = 999;
3124 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003126 }
3127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003129 /*
3130 * We can do it without term_windgoto()!
3131 */
3132 if (cost < goto_cost)
3133 {
3134 if (plan == PLAN_LE)
3135 {
3136 if (noinvcurs)
3137 screen_stop_highlight();
3138 while (screen_cur_col > col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003140 out_str(bs);
3141 --screen_cur_col;
3142 }
3143 }
3144 else if (plan == PLAN_CR)
3145 {
3146 if (noinvcurs)
3147 screen_stop_highlight();
3148 out_char('\r');
3149 screen_cur_col = 0;
3150 }
3151 else if (plan == PLAN_NL)
3152 {
3153 if (noinvcurs)
3154 screen_stop_highlight();
3155 while (screen_cur_row < row)
3156 {
3157 out_char('\n');
3158 ++screen_cur_row;
3159 }
3160 screen_cur_col = 0;
3161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003163 i = col - screen_cur_col;
3164 if (i > 0)
3165 {
3166 /*
3167 * Use cursor-right if it's one character only. Avoids
3168 * removing a line of pixels from the last bold char, when
3169 * using the bold trick in the GUI.
3170 */
3171 if (T_ND[0] != NUL && T_ND[1] == NUL)
3172 {
3173 while (i-- > 0)
3174 out_char(*T_ND);
3175 }
3176 else
3177 {
3178 int off;
3179
3180 off = LineOffset[row] + screen_cur_col;
3181 while (i-- > 0)
3182 {
3183 if (ScreenAttrs[off] != screen_attr)
3184 screen_stop_highlight();
3185 out_flush_check();
3186 out_char(ScreenLines[off]);
3187 if (enc_dbcs == DBCS_JPNU
3188 && ScreenLines[off] == 0x8e)
3189 out_char(ScreenLines2[off]);
3190 ++off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192 }
3193 }
3194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003196 else
3197 cost = 999;
3198
3199 if (cost >= goto_cost)
3200 {
3201 if (noinvcurs)
3202 screen_stop_highlight();
3203 if (row == screen_cur_row && (col > screen_cur_col)
3204 && *T_CRI != NUL)
3205 term_cursor_right(col - screen_cur_col);
3206 else
3207 term_windgoto(row, col);
3208 }
3209 screen_cur_row = row;
3210 screen_cur_col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211}
3212
3213/*
3214 * Set cursor to its position in the current window.
3215 */
3216 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003217setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003219 setcursor_mayforce(FALSE);
3220}
3221
3222/*
3223 * Set cursor to its position in the current window.
3224 * When "force" is TRUE also when not redrawing.
3225 */
3226 void
3227setcursor_mayforce(int force)
3228{
3229 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 validate_cursor();
3232 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003233 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003235 // With 'rightleft' set and the cursor on a double-wide
3236 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003237 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3238 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003239 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003240 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#endif
3242 curwin->w_wcol));
3243 }
3244}
3245
3246
3247/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003248 * Insert 'line_count' lines at 'row' in window 'wp'.
3249 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3250 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 * scrolling.
3252 * Returns FAIL if the lines are not inserted, OK for success.
3253 */
3254 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003255win_ins_lines(
3256 win_T *wp,
3257 int row,
3258 int line_count,
3259 int invalid,
3260 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262 int did_delete;
3263 int nextrow;
3264 int lastrow;
3265 int retval;
3266
3267 if (invalid)
3268 wp->w_lines_valid = 0;
3269
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003270 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (wp->w_height < 5)
3272 return FAIL;
3273
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003274 // with the popup menu visible this might not work correctly
3275 if (pum_visible())
3276 return FAIL;
3277
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (line_count > wp->w_height - row)
3279 line_count = wp->w_height - row;
3280
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003281 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (retval != MAYBE)
3283 return retval;
3284
3285 /*
3286 * If there is a next window or a status line, we first try to delete the
3287 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003288 * If this fails and there are following windows, don't do anything to
3289 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 */
3291 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (wp->w_next != NULL || wp->w_status_height)
3293 {
3294 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003295 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 did_delete = TRUE;
3297 else if (wp->w_next)
3298 return FAIL;
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 /*
3301 * if no lines deleted, blank the lines that will end up below the window
3302 */
3303 if (!did_delete)
3304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003307 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 lastrow = nextrow + line_count;
3309 if (lastrow > Rows)
3310 lastrow = Rows;
3311 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003312 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 ' ', ' ', 0);
3314 }
3315
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003316 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 == FAIL)
3318 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003319 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 if (did_delete)
3321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 win_rest_invalid(W_NEXT(wp));
3324 }
3325 return FAIL;
3326 }
3327
3328 return OK;
3329}
3330
3331/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003332 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3334 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3335 * scrolling
3336 * Return OK for success, FAIL if the lines are not deleted.
3337 */
3338 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003339win_del_lines(
3340 win_T *wp,
3341 int row,
3342 int line_count,
3343 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003344 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003345 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 int retval;
3348
3349 if (invalid)
3350 wp->w_lines_valid = 0;
3351
3352 if (line_count > wp->w_height - row)
3353 line_count = wp->w_height - row;
3354
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003355 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (retval != MAYBE)
3357 return retval;
3358
3359 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003360 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 return FAIL;
3362
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 /*
3364 * If there are windows or status lines below, try to put them at the
3365 * correct place. If we can't do that, they have to be redrawn.
3366 */
3367 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3368 {
3369 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003370 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
3372 wp->w_redr_status = TRUE;
3373 win_rest_invalid(wp->w_next);
3374 }
3375 }
3376 /*
3377 * If this is the last window and there is no status line, redraw the
3378 * command line later.
3379 */
3380 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 redraw_cmdline = TRUE;
3382 return OK;
3383}
3384
3385/*
3386 * Common code for win_ins_lines() and win_del_lines().
3387 * Returns OK or FAIL when the work has been done.
3388 * Returns MAYBE when not finished yet.
3389 */
3390 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003391win_do_lines(
3392 win_T *wp,
3393 int row,
3394 int line_count,
3395 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003396 int del,
3397 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 int retval;
3400
3401 if (!redrawing() || line_count <= 0)
3402 return FAIL;
3403
Bram Moolenaar33796b32019-06-08 16:01:13 +02003404 // When inserting lines would result in loss of command output, just redraw
3405 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003406 if (no_win_do_lines_ins && !del)
3407 return FAIL;
3408
Bram Moolenaar33796b32019-06-08 16:01:13 +02003409 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003410 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003412 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003413 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 return FAIL;
3415 }
3416
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003417#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003418 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003419 if (popup_visible)
3420 return FAIL;
3421#endif
3422
3423 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 if (row + line_count >= wp->w_height)
3425 {
3426 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003427 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 ' ', ' ', 0);
3429 return OK;
3430 }
3431
3432 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003433 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003435 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003437 if (!no_win_do_lines_ins)
3438 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 /*
3441 * If the terminal can set a scroll region, use that.
3442 * Always do this in a vertically split window. This will redraw from
3443 * ScreenLines[] when t_CV isn't defined. That's faster than using
3444 * win_line().
3445 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003446 * a character in the lower right corner of the scroll region may cause a
3447 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003449 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 scroll_region_set(wp, row);
3453 if (del)
3454 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003455 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 else
3457 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003458 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 scroll_region_reset();
3461 return retval;
3462 }
3463
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003464 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 return MAYBE;
3468}
3469
3470/*
3471 * window 'wp' and everything after it is messed up, mark it for redraw
3472 */
3473 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003474win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003478 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 wp->w_redr_status = TRUE;
3480 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
3482 redraw_cmdline = TRUE;
3483}
3484
3485/*
3486 * The rest of the routines in this file perform screen manipulations. The
3487 * given operation is performed physically on the screen. The corresponding
3488 * change is also made to the internal screen image. In this way, the editor
3489 * anticipates the effect of editing changes on the appearance of the screen.
3490 * That way, when we call screenupdate a complete redraw isn't usually
3491 * necessary. Another advantage is that we can keep adding code to anticipate
3492 * screen changes, and in the meantime, everything still works.
3493 */
3494
3495/*
3496 * types for inserting or deleting lines
3497 */
3498#define USE_T_CAL 1
3499#define USE_T_CDL 2
3500#define USE_T_AL 3
3501#define USE_T_CE 4
3502#define USE_T_DL 5
3503#define USE_T_SR 6
3504#define USE_NL 7
3505#define USE_T_CD 8
3506#define USE_REDRAW 9
3507
3508/*
3509 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003510 * "end" is the line after the scrolled part. Normally it is Rows.
3511 * When scrolling region used "off" is the offset from the top for the region.
3512 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 *
3514 * return FAIL for failure, OK for success.
3515 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003516 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003517screen_ins_lines(
3518 int off,
3519 int row,
3520 int line_count,
3521 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003522 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003523 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
3525 int i;
3526 int j;
3527 unsigned temp;
3528 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003529 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 int type;
3531 int result_empty;
3532 int can_ce = can_clear(T_CE);
3533
3534 /*
3535 * FAIL if
3536 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 * - the line count is less than one
3538 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003539 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003540 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003541 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003543 if (!screen_valid(TRUE)
3544 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003545 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003546#ifdef FEAT_CLIPBOARD
3547 || (clip_star.state != SELECT_CLEARED
3548 && redrawing_for_callback > 0)
3549#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003550#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003551 || popup_visible
3552#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003553 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 return FAIL;
3555
3556 /*
3557 * There are seven ways to insert lines:
3558 * 0. When in a vertically split window and t_CV isn't set, redraw the
3559 * characters from ScreenLines[].
3560 * 1. Use T_CD (clear to end of display) if it exists and the result of
3561 * the insert is just empty lines
3562 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3563 * present or line_count > 1. It looks better if we do all the inserts
3564 * at once.
3565 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3566 * insert is just empty lines and T_CE is not present or line_count >
3567 * 1.
3568 * 4. Use T_AL (insert line) if it exists.
3569 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3570 * just empty lines.
3571 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3572 * just empty lines.
3573 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3574 * the 'da' flag is not set or we have clear line capability.
3575 * 8. redraw the characters from ScreenLines[].
3576 *
3577 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3578 * the scrollbar for the window. It does have insert line, use that if it
3579 * exists.
3580 */
3581 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003583 {
3584 // Avoid that lines are first cleared here and then redrawn, which
3585 // results in many characters updated twice. This happens with CTRL-F
3586 // in a vertically split window. With line-by-line scrolling
3587 // USE_REDRAW should be faster.
3588 if (line_count > 3)
3589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003591 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003592 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 type = USE_T_CD;
3594 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3595 type = USE_T_CAL;
3596 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3597 type = USE_T_CDL;
3598 else if (*T_AL != NUL)
3599 type = USE_T_AL;
3600 else if (can_ce && result_empty)
3601 type = USE_T_CE;
3602 else if (*T_DL != NUL && result_empty)
3603 type = USE_T_DL;
3604 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3605 type = USE_T_SR;
3606 else
3607 return FAIL;
3608
3609 /*
3610 * For clearing the lines screen_del_lines() is used. This will also take
3611 * care of t_db if necessary.
3612 */
3613 if (type == USE_T_CD || type == USE_T_CDL ||
3614 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003615 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 /*
3618 * If text is retained below the screen, first clear or delete as many
3619 * lines at the bottom of the window as are about to be inserted so that
3620 * the deleted lines won't later surface during a screen_del_lines.
3621 */
3622 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003623 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
3625#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003626 // Remove a modeless selection when inserting lines halfway the screen
3627 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003628 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003629 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 else
3631 clip_scroll_selection(-line_count);
3632#endif
3633
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003634#ifdef FEAT_GUI_HAIKU
3635 vim_lock_screen();
3636#endif
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003639 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3640 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003641 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642#endif
3643
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003644 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3645 cursor_col = wp->w_wincol;
3646
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003647 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 cursor_row = row;
3649 else
3650 cursor_row = row + off;
3651
3652 /*
3653 * Shift LineOffset[] line_count down to reflect the inserted lines.
3654 * Clear the inserted lines in ScreenLines[].
3655 */
3656 row += off;
3657 end += off;
3658 for (i = 0; i < line_count; ++i)
3659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 if (wp != NULL && wp->w_width != Columns)
3661 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003662 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 j = end - 1 - i;
3664 while ((j -= line_count) >= row)
3665 linecopy(j + line_count, j, wp);
3666 j += line_count;
3667 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003668 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3669 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 else
3671 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3672 LineWraps[j] = FALSE;
3673 }
3674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
3676 j = end - 1 - i;
3677 temp = LineOffset[j];
3678 while ((j -= line_count) >= row)
3679 {
3680 LineOffset[j + line_count] = LineOffset[j];
3681 LineWraps[j + line_count] = LineWraps[j];
3682 }
3683 LineOffset[j + line_count] = temp;
3684 LineWraps[j + line_count] = FALSE;
3685 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003686 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 else
3688 lineinvalid(temp, (int)Columns);
3689 }
3690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003692#ifdef FEAT_GUI_HAIKU
3693 vim_unlock_screen();
3694#endif
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003697 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003698 if (clear_attr != 0)
3699 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003701 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (type == USE_REDRAW)
3703 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003704 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
3706 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003707 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709 else
3710 {
3711 for (i = 0; i < line_count; i++)
3712 {
3713 if (type == USE_T_AL)
3714 {
3715 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003716 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 out_str(T_AL);
3718 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003719 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003721 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723 }
3724
3725 /*
3726 * With scroll-reverse and 'da' flag set we need to clear the lines that
3727 * have been scrolled down into the region.
3728 */
3729 if (type == USE_T_SR && *T_DA)
3730 {
3731 for (i = 0; i < line_count; ++i)
3732 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003733 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003735 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 }
3738
3739#ifdef FEAT_GUI
3740 gui_can_update_cursor();
3741 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003742 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743#endif
3744 return OK;
3745}
3746
3747/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003748 * Delete lines on the screen and update ScreenLines[].
3749 * "end" is the line after the scrolled part. Normally it is Rows.
3750 * When scrolling region used "off" is the offset from the top for the region.
3751 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 *
3753 * Return OK for success, FAIL if the lines are not deleted.
3754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003756screen_del_lines(
3757 int off,
3758 int row,
3759 int line_count,
3760 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003761 int force, // even when line_count > p_ttyscroll
3762 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003763 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
3765 int j;
3766 int i;
3767 unsigned temp;
3768 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003769 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003771 int result_empty; // result is empty until end of region
3772 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 int type;
3774
3775 /*
3776 * FAIL if
3777 * - there is no valid screen
3778 * - the screen has to be redrawn completely
3779 * - the line count is less than one
3780 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003781 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003782 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003784 if (!screen_valid(TRUE)
3785 || line_count <= 0
3786 || (!force && line_count > p_ttyscroll)
3787 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003788#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003789 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003790#endif
3791 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 return FAIL;
3793
3794 /*
3795 * Check if the rest of the current region will become empty.
3796 */
3797 result_empty = row + line_count >= end;
3798
3799 /*
3800 * We can delete lines only when 'db' flag not set or when 'ce' option
3801 * available.
3802 */
3803 can_delete = (*T_DB == NUL || can_clear(T_CE));
3804
3805 /*
3806 * There are six ways to delete lines:
3807 * 0. When in a vertically split window and t_CV isn't set, redraw the
3808 * characters from ScreenLines[].
3809 * 1. Use T_CD if it exists and the result is empty.
3810 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3811 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3812 * none of the other ways work.
3813 * 4. Use T_CE (erase line) if the result is empty.
3814 * 5. Use T_DL (delete line) if it exists.
3815 * 6. redraw the characters from ScreenLines[].
3816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003818 {
3819 // Avoid that lines are first cleared here and then redrawn, which
3820 // results in many characters updated twice. This happens with CTRL-F
3821 // in a vertically split window. With line-by-line scrolling
3822 // USE_REDRAW should be faster.
3823 if (line_count > 3)
3824 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003826 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003827 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 else if (row == 0 && (
3830#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003831 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3832 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 line_count == 1 ||
3834#endif
3835 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 type = USE_NL;
3837 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3838 type = USE_T_CDL;
3839 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003840 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 type = USE_T_CE;
3842 else if (*T_DL != NUL && can_delete)
3843 type = USE_T_DL;
3844 else if (*T_CDL != NUL && can_delete)
3845 type = USE_T_CDL;
3846 else
3847 return FAIL;
3848
3849#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003850 // Remove a modeless selection when deleting lines halfway the screen or
3851 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003852 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003853 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 else
3855 clip_scroll_selection(line_count);
3856#endif
3857
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003858#ifdef FEAT_GUI_HAIKU
3859 vim_lock_screen();
3860#endif
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003863 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3864 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003865 gui_dont_update_cursor(gui.cursor_row >= row + off
3866 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867#endif
3868
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003869 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3870 cursor_col = wp->w_wincol;
3871
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003872 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
3874 cursor_row = row;
3875 cursor_end = end;
3876 }
3877 else
3878 {
3879 cursor_row = row + off;
3880 cursor_end = end + off;
3881 }
3882
3883 /*
3884 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3885 * Clear the inserted lines in ScreenLines[].
3886 */
3887 row += off;
3888 end += off;
3889 for (i = 0; i < line_count; ++i)
3890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if (wp != NULL && wp->w_width != Columns)
3892 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003893 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 j = row + i;
3895 while ((j += line_count) <= end - 1)
3896 linecopy(j - line_count, j, wp);
3897 j -= line_count;
3898 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003899 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3900 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 else
3902 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3903 LineWraps[j] = FALSE;
3904 }
3905 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003907 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 j = row + i;
3909 temp = LineOffset[j];
3910 while ((j += line_count) <= end - 1)
3911 {
3912 LineOffset[j - line_count] = LineOffset[j];
3913 LineWraps[j - line_count] = LineWraps[j];
3914 }
3915 LineOffset[j - line_count] = temp;
3916 LineWraps[j - line_count] = FALSE;
3917 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003918 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else
3920 lineinvalid(temp, (int)Columns);
3921 }
3922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003924#ifdef FEAT_GUI_HAIKU
3925 vim_unlock_screen();
3926#endif
3927
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003928 if (screen_attr != clear_attr)
3929 screen_stop_highlight();
3930 if (clear_attr != 0)
3931 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003933 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if (type == USE_REDRAW)
3935 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003936 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003938 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 out_str(T_CD);
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 else if (type == USE_T_CDL)
3943 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003944 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003946 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948 /*
3949 * Deleting lines at top of the screen or scroll region: Just scroll
3950 * the whole screen (scroll region) up by outputting newlines on the
3951 * last line.
3952 */
3953 else if (type == USE_NL)
3954 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003955 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003957 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959 else
3960 {
3961 for (i = line_count; --i >= 0; )
3962 {
3963 if (type == USE_T_DL)
3964 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003965 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003966 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003968 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003970 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003971 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003973 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 }
3976
3977 /*
3978 * If the 'db' flag is set, we need to clear the lines that have been
3979 * scrolled up at the bottom of the region.
3980 */
3981 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
3982 {
3983 for (i = line_count; i > 0; --i)
3984 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003985 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003986 out_str(T_CE); // erase a line
3987 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 }
3990
3991#ifdef FEAT_GUI
3992 gui_can_update_cursor();
3993 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003994 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995#endif
3996
3997 return OK;
3998}
3999
4000/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004001 * Return TRUE when postponing displaying the mode message: when not redrawing
4002 * or inside a mapping.
4003 */
4004 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004005skip_showmode(void)
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004006{
4007 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004008 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004009 if (global_busy
4010 || msg_silent != 0
4011 || !redrawing()
4012 || (char_avail() && !KeyTyped))
4013 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004014 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004015 return TRUE;
4016 }
4017 return FALSE;
4018}
4019
4020/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004021 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 *
4023 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4024 * If clear_cmdline is FALSE there may be a message there that needs to be
4025 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004026 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 * Return the length of the message (0 if no message).
4028 */
4029 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004030showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031{
4032 int need_clear;
4033 int length = 0;
4034 int do_mode;
4035 int attr;
4036 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
Bram Moolenaara2a89732022-08-31 14:46:18 +01004039 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004040 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004041 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004042 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004043 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004045 if (skip_showmode())
4046 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 nwr_save = need_wait_return;
4049
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004050 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 check_for_delay(FALSE);
4052
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004053 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 need_clear = clear_cmdline;
4055 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004056 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004058 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 msg_pos_mode();
4060 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004061 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (do_mode)
4063 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004064 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004066 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004067# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004068 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004069# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004070 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004071# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004072 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004073# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004074 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004076 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077# endif
4078#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004079 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004080 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004082 // These messages can get long, avoid a wrap in a narrow
4083 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 length = (Rows - msg_row) * Columns - 3;
4085 if (edit_submode_extra != NULL)
4086 length -= vim_strsize(edit_submode_extra);
4087 if (length > 0)
4088 {
4089 if (edit_submode_pre != NULL)
4090 length -= vim_strsize(edit_submode_pre);
4091 if (length - vim_strsize(edit_submode) > 0)
4092 {
4093 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004094 msg_puts_attr((char *)edit_submode_pre, attr);
4095 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 if (edit_submode_extra != NULL)
4098 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004099 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004101 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 else
4103 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004104 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004111 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004112 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004113 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004114 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
4116#ifdef FEAT_RIGHTLEFT
4117 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004118 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004120 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004122 else if (restart_edit == 'I' || restart_edit == 'i' ||
4123 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004124 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004126 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004128 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129#ifdef FEAT_RIGHTLEFT
4130 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004131 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#endif
4133#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004134 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
4136# ifdef FEAT_ARABIC
4137 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004138 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 else
4140# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004141 if (get_keymap_str(curwin, (char_u *)" (%s)",
John Marriotta21240b2025-01-08 20:10:59 +01004142 NameBuff, MAXPATHL) > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004143 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004146 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004147 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if (VIsual_active)
4150 {
4151 char *p;
4152
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004153 // Don't concatenate separate words to avoid translation
4154 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 switch ((VIsual_select ? 4 : 0)
4156 + (VIsual_mode == Ctrl_V) * 2
4157 + (VIsual_mode == 'V'))
4158 {
4159 case 0: p = N_(" VISUAL"); break;
4160 case 1: p = N_(" VISUAL LINE"); break;
4161 case 2: p = N_(" VISUAL BLOCK"); break;
4162 case 4: p = N_(" SELECT"); break;
4163 case 5: p = N_(" SELECT LINE"); break;
4164 default: p = N_(" SELECT BLOCK"); break;
4165 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004166 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004168 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004170
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 need_clear = TRUE;
4172 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004173 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004174 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004176 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 need_clear = TRUE;
4178 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004179
4180 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004181 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004183 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 length = msg_col;
4185 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004186 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004189 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004191 else if (redraw_mode)
4192 {
4193 msg_pos_mode();
4194 msg_clr_eos();
4195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004197 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 if (VIsual_active)
4199 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004201 // If the last window has no status line, the ruler is after the mode
4202 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004203 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004204 win_redr_ruler(lastwin, TRUE, FALSE);
Martin Tournoijba43e762022-10-13 22:12:15 +01004205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004207 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 clear_cmdline = FALSE;
4209
4210 return length;
4211}
4212
4213/*
4214 * Position for a mode message.
4215 */
4216 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004217msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
4219 msg_col = 0;
4220 msg_row = Rows - 1;
4221}
4222
4223/*
4224 * Delete mode message. Used when ESC is typed which is expected to end
4225 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004226 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 */
4228 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004229unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004232 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 */
4234 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004235 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004237 clearmode();
4238}
4239
4240/*
4241 * Clear the mode message.
4242 */
4243 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004244clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004245{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004246 int save_msg_row = msg_row;
4247 int save_msg_col = msg_col;
4248
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004249 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004250 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004251 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004252 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004253
4254 msg_col = save_msg_col;
4255 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256}
4257
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004258 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004259recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004260{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004261 msg_puts_attr(_("recording"), attr);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004262 if (shortmess(SHM_RECORDING))
4263 return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004264
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004265 char s[4];
4266
4267 sprintf(s, " @%c", reg_recording);
4268 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004269}
4270
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004271/*
4272 * Draw the tab pages line at the top of the Vim window.
4273 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004275draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004276{
4277 int tabcount = 0;
4278 tabpage_T *tp;
4279 int tabwidth;
4280 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004281 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004282 int attr;
4283 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004284 win_T *cwp;
4285 int wincount;
4286 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004287 int c;
4288 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004289 int attr_sel = HL_ATTR(HLF_TPS);
4290 int attr_nosel = HL_ATTR(HLF_TP);
4291 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004292 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004293 int room;
4294 int use_sep_chars = (t_colors < 8
4295#ifdef FEAT_GUI
4296 && !gui.in_use
4297#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004298#ifdef FEAT_TERMGUICOLORS
4299 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004300#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004301 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004302
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004303 if (ScreenLines == NULL)
4304 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004305 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004306
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004307#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004308 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004309 if (gui_use_tabline())
4310 {
4311 gui_update_tabline();
4312 return;
4313 }
4314#endif
4315
4316 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004317 return;
4318
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004319#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004320 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004321
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004322 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004323 if (*p_tal != NUL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004324 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004325 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004326#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004327 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004328 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004329 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004330
Bram Moolenaar238a5642006-02-21 22:12:05 +00004331 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4332 if (tabwidth < 6)
4333 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004334
Bram Moolenaar238a5642006-02-21 22:12:05 +00004335 attr = attr_nosel;
4336 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004337 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4338 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004339 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004340 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004341
Bram Moolenaar238a5642006-02-21 22:12:05 +00004342 if (tp->tp_topframe == topframe)
4343 attr = attr_sel;
4344 if (use_sep_chars && col > 0)
4345 screen_putchar('|', 0, col++, attr);
4346
4347 if (tp->tp_topframe != topframe)
4348 attr = attr_nosel;
4349
4350 screen_putchar(' ', 0, col++, attr);
4351
4352 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004353 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004354 cwp = curwin;
4355 wp = firstwin;
4356 }
4357 else
4358 {
4359 cwp = tp->tp_curwin;
4360 wp = tp->tp_firstwin;
4361 }
4362
4363 modified = FALSE;
4364 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4365 if (bufIsChanged(wp->w_buffer))
4366 modified = TRUE;
4367 if (modified || wincount > 1)
4368 {
4369 if (wincount > 1)
4370 {
4371 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004372 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004373 if (col + len >= Columns - 3)
4374 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004375 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004376#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004377 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004378#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004379 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004380#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004381 );
4382 col += len;
4383 }
4384 if (modified)
4385 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4386 screen_putchar(' ', 0, col++, attr);
4387 }
4388
4389 room = scol - col + tabwidth - 1;
4390 if (room > 0)
4391 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004392 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004393 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004394 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004395 len = vim_strsize(NameBuff);
4396 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004397 if (has_mbyte)
4398 while (len > room)
4399 {
4400 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004401 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004402 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004403 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004404 {
4405 p += len - room;
4406 len = room;
4407 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004408 if (len > Columns - col - 1)
4409 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004410
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004411 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004412 col += len;
4413 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004414 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004415
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004416 // Store the tab page number in TabPageIdxs[], so that
4417 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004418 ++tabcount;
4419 while (scol < col)
4420 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004421 }
4422
Bram Moolenaar238a5642006-02-21 22:12:05 +00004423 if (use_sep_chars)
4424 c = '_';
4425 else
4426 c = ' ';
4427 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004428
Luuk van Baalba936f62022-12-15 13:15:39 +00004429 // Draw the 'showcmd' information if 'showcmdloc' == "tabline".
4430 if (p_sc && *p_sloc == 't')
4431 {
4432 int width = MIN(10, (int)Columns - col - (tabcount > 1) * 3);
4433
4434 if (width > 0)
4435 screen_puts_len(showcmd_buf, width, 0, (int)Columns
4436 - width - (tabcount > 1) * 2, attr_nosel);
4437 }
4438
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004439 // Put an "X" for closing the current tab if there are several.
Luuk van Baalba936f62022-12-15 13:15:39 +00004440 if (tabcount > 1)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004441 {
4442 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4443 TabPageIdxs[Columns - 1] = -999;
4444 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004445 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004446
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004447 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4448 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004449 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004450}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004451
4452/*
4453 * Get buffer name for "buf" into NameBuff[].
4454 * Takes care of special buffer names and translates special characters.
4455 */
4456 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004457get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004458{
4459 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004460 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004461 else
4462 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4463 trans_characters(NameBuff, MAXPATHL);
4464}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466/*
4467 * Get the character to use in a status line. Get its attributes in "*attr".
4468 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004469 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004470fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471{
4472 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004473
4474#ifdef FEAT_TERMINAL
4475 if (bt_terminal(wp->w_buffer))
4476 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004477 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004478 {
4479 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004480 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004481 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004482 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004483 {
4484 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004485 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004486 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004487 }
4488 else
4489#endif
4490 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004492 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004493 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495 else
4496 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004497 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004498 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
Christian Brabandt6a650bf2023-11-08 21:23:29 +01004500 return fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503/*
4504 * Get the character to use in a separator between vertically split windows.
4505 * Get its attributes in "*attr".
4506 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004507 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004508fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004510 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004511 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 return '|';
4513 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004514 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
4517/*
4518 * Return TRUE if redrawing should currently be done.
4519 */
4520 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004521redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004523#ifdef FEAT_EVAL
4524 if (disable_redraw_for_testing)
4525 return 0;
4526 else
4527#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004528 return ((RedrawingDisabled == 0
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004529#ifdef FEAT_EVAL
4530 || ignore_redraw_flag_for_testing
4531#endif
4532 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533}
4534
4535/*
4536 * Return TRUE if printing messages should currently be done.
4537 */
4538 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004539messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004541 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542}
4543
Bram Moolenaare677df82019-09-02 22:31:11 +02004544/*
4545 * Compute columns for ruler and shown command. 'sc_col' is also used to
4546 * decide what the maximum length of a message on the status line can be.
4547 * If there is a status line for the last window, 'sc_col' is independent
4548 * of 'ru_col'.
4549 */
4550
4551#define COL_RULER 17 // columns needed by standard ruler
4552
4553 void
4554comp_col(void)
4555{
Sean Dewar876f5fb2023-08-17 22:40:05 +02004556 int last_has_status = last_stl_height(FALSE) > 0;
Bram Moolenaare677df82019-09-02 22:31:11 +02004557
4558 sc_col = 0;
4559 ru_col = 0;
4560 if (p_ru)
4561 {
Martin Tournoijba43e762022-10-13 22:12:15 +01004562#ifdef FEAT_STL_OPT
Bram Moolenaare677df82019-09-02 22:31:11 +02004563 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004564#else
Bram Moolenaare677df82019-09-02 22:31:11 +02004565 ru_col = COL_RULER + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004566#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004567 // no last status line, adjust sc_col
4568 if (!last_has_status)
4569 sc_col = ru_col;
4570 }
Sam-programs062141b2024-02-29 17:40:29 +01004571 if (p_sc && *p_sloc == 'l')
Bram Moolenaare677df82019-09-02 22:31:11 +02004572 {
4573 sc_col += SHOWCMD_COLS;
4574 if (!p_ru || last_has_status) // no need for separating space
4575 ++sc_col;
4576 }
4577 sc_col = Columns - sc_col;
4578 ru_col = Columns - ru_col;
4579 if (sc_col <= 0) // screen too narrow, will become a mess
4580 sc_col = 1;
4581 if (ru_col <= 0)
4582 ru_col = 1;
Bram Moolenaare677df82019-09-02 22:31:11 +02004583#ifdef FEAT_EVAL
4584 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4585#endif
4586}
4587
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004588#if defined(FEAT_LINEBREAK) || defined(PROTO)
4589/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004590 * Return the width of the 'number' and 'relativenumber' column.
4591 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004592 * Otherwise it depends on 'numberwidth' and the line count.
4593 */
4594 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004595number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004596{
4597 int n;
4598 linenr_T lnum;
4599
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004600 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004601 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004602 lnum = wp->w_height;
4603 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004604 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004605 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004606
Bram Moolenaar6b314672015-03-20 15:42:10 +01004607 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004608 return wp->w_nrwidth_width;
4609 wp->w_nrwidth_line_count = lnum;
4610
4611 n = 0;
4612 do
4613 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004614 lnum /= 10;
4615 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004616 } while (lnum > 0);
4617
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004618 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004619 if (n < wp->w_p_nuw - 1)
4620 n = wp->w_p_nuw - 1;
4621
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004622# ifdef FEAT_SIGNS
4623 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4624 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004625 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004626 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4627 n = 2;
4628# endif
4629
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004630 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004631 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004632 return n;
4633}
4634#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004635
Bram Moolenaar113e1072019-01-20 15:30:40 +01004636#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004637/*
4638 * Return the current cursor column. This is the actual position on the
4639 * screen. First column is 0.
4640 */
4641 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004642screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004643{
4644 return screen_cur_col;
4645}
4646
4647/*
4648 * Return the current cursor row. This is the actual position on the screen.
4649 * First row is 0.
4650 */
4651 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004652screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004653{
4654 return screen_cur_row;
4655}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004656#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004657
4658/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004659 * Calls mb_ptr2char_adv(p) and returns the character.
4660 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4661 */
4662 static int
4663get_encoded_char_adv(char_u **p)
4664{
4665 char_u *s = *p;
4666
4667 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4668 {
4669 varnumber_T num = 0;
4670 int bytes;
4671 int n;
4672
4673 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4674 {
4675 *p += 2;
4676 n = hexhex2nr(*p);
4677 if (n < 0)
4678 return 0;
4679 num = num * 256 + n;
4680 }
4681 *p += 2;
4682 return num;
4683 }
4684 return mb_ptr2char_adv(p);
4685}
4686
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004687struct charstab
4688{
4689 int *cp;
4690 char *name;
4691};
4692static fill_chars_T fill_chars;
4693static struct charstab filltab[] =
4694{
4695 {&fill_chars.stl, "stl"},
4696 {&fill_chars.stlnc, "stlnc"},
4697 {&fill_chars.vert, "vert"},
4698 {&fill_chars.fold, "fold"},
4699 {&fill_chars.foldopen, "foldopen"},
4700 {&fill_chars.foldclosed, "foldclose"},
4701 {&fill_chars.foldsep, "foldsep"},
4702 {&fill_chars.diff, "diff"},
4703 {&fill_chars.eob, "eob"},
4704 {&fill_chars.lastline, "lastline"},
4705};
4706static lcs_chars_T lcs_chars;
4707static struct charstab lcstab[] =
4708{
4709 {&lcs_chars.eol, "eol"},
4710 {&lcs_chars.ext, "extends"},
4711 {&lcs_chars.nbsp, "nbsp"},
4712 {&lcs_chars.prec, "precedes"},
4713 {&lcs_chars.space, "space"},
4714 {&lcs_chars.tab2, "tab"},
4715 {&lcs_chars.trail, "trail"},
4716 {&lcs_chars.lead, "lead"},
4717#ifdef FEAT_CONCEAL
4718 {&lcs_chars.conceal, "conceal"},
4719#else
4720 {NULL, "conceal"},
4721#endif
zeertzjq1f025b02023-09-30 12:43:07 +02004722 {NULL, "multispace"},
4723 {NULL, "leadmultispace"},
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004724};
4725
zeertzjq6a8d2e12024-01-17 20:54:49 +01004726 static char *
4727field_value_err(char *errbuf, size_t errbuflen, char *fmt, char *field)
4728{
4729 if (errbuf == NULL)
4730 return "";
4731 vim_snprintf(errbuf, errbuflen, _(fmt), field);
4732 return errbuf;
4733}
4734
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004735/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004736 * Handle setting 'listchars' or 'fillchars'.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004737 * "value" points to either the global or the window-local value.
zeertzjqd619d6a2023-05-08 15:56:21 +01004738 * "is_listchars" is TRUE for "listchars" and FALSE for "fillchars".
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004739 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004740 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004741 * Returns error message, NULL if it's OK.
4742 */
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004743 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004744set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
4745 char *errbuf, size_t errbuflen)
Bram Moolenaare677df82019-09-02 22:31:11 +02004746{
zeertzjq1f025b02023-09-30 12:43:07 +02004747 int round, i, len, entries;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004748 char_u *p, *s;
4749 int c1 = 0, c2 = 0, c3 = 0;
4750 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4751 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4752 int multispace_len = 0; // Length of lcs-multispace string
4753 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004754
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004755 struct charstab *tab;
4756
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004757 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004758 {
4759 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004760 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004761 entries = ARRAY_LENGTH(lcstab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004762 if (wp->w_p_lcs[0] == NUL)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004763 value = p_lcs; // local value is empty, use the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004764 }
4765 else
4766 {
4767 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004768 entries = ARRAY_LENGTH(filltab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004769 if (wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004770 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004771 }
4772
4773 // first round: check for valid value, second round: assign values
zeertzjq00624a22023-11-23 20:47:16 +01004774 for (round = 0; round <= (apply ? 1 : 0); ++round)
Bram Moolenaare677df82019-09-02 22:31:11 +02004775 {
4776 if (round > 0)
4777 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004778 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004779 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004780 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004781 for (i = 0; i < entries; ++i)
4782 if (tab[i].cp != NULL)
4783 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004784 lcs_chars.tab1 = NUL;
4785 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004786
Mike Williamsf5785cf2021-09-13 22:17:38 +02004787 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004788 {
4789 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004790 if (lcs_chars.multispace != NULL)
4791 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004792 }
4793 else
4794 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004795
4796 if (lead_multispace_len > 0)
4797 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004798 lcs_chars.leadmultispace =
4799 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004800 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
4801 }
4802 else
4803 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004804 }
4805 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004806 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004807 fill_chars.stl = ' ';
4808 fill_chars.stlnc = ' ';
4809 fill_chars.vert = ' ';
4810 fill_chars.fold = '-';
4811 fill_chars.foldopen = '-';
4812 fill_chars.foldclosed = '+';
4813 fill_chars.foldsep = '|';
4814 fill_chars.diff = '-';
4815 fill_chars.eob = '~';
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01004816 fill_chars.lastline = '@';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004817 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004818 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004819 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02004820 while (*p)
4821 {
4822 for (i = 0; i < entries; ++i)
4823 {
4824 len = (int)STRLEN(tab[i].name);
zeertzjq6a8d2e12024-01-17 20:54:49 +01004825 if (!(STRNCMP(p, tab[i].name, len) == 0 && p[len] == ':'))
zeertzjq1f025b02023-09-30 12:43:07 +02004826 continue;
Bram Moolenaare677df82019-09-02 22:31:11 +02004827
zeertzjq1f025b02023-09-30 12:43:07 +02004828 if (is_listchars && strcmp(tab[i].name, "multispace") == 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004829 {
4830 s = p + len + 1;
4831 if (round == 0)
4832 {
4833 // Get length of lcs-multispace string in first round
4834 last_multispace = p;
4835 multispace_len = 0;
4836 while (*s != NUL && *s != ',')
4837 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004838 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004839 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004840 return field_value_err(errbuf, errbuflen,
4841 e_wrong_character_width_for_field_str,
4842 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004843 ++multispace_len;
4844 }
4845 if (multispace_len == 0)
4846 // lcs-multispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004847 return field_value_err(errbuf, errbuflen,
4848 e_wrong_number_of_characters_for_field_str,
4849 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004850 p = s;
4851 }
4852 else
4853 {
4854 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02004855
zeertzjqf14b8ba2021-09-10 16:58:30 +02004856 while (*s != NUL && *s != ',')
4857 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004858 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004859 if (p == last_multispace)
4860 lcs_chars.multispace[multispace_pos++] = c1;
4861 }
4862 p = s;
4863 }
zeertzjq1f025b02023-09-30 12:43:07 +02004864 break;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004865 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004866
zeertzjq1f025b02023-09-30 12:43:07 +02004867 if (is_listchars && strcmp(tab[i].name, "leadmultispace") == 0)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004868 {
zeertzjq1f025b02023-09-30 12:43:07 +02004869 s = p + len + 1;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004870 if (round == 0)
4871 {
zeertzjqb5f08012022-06-09 13:55:28 +01004872 // get length of lcs-leadmultispace string in first
4873 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004874 last_lmultispace = p;
4875 lead_multispace_len = 0;
4876 while (*s != NUL && *s != ',')
4877 {
4878 c1 = get_encoded_char_adv(&s);
4879 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004880 return field_value_err(errbuf, errbuflen,
4881 e_wrong_character_width_for_field_str,
4882 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004883 ++lead_multispace_len;
4884 }
4885 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01004886 // lcs-leadmultispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004887 return field_value_err(errbuf, errbuflen,
4888 e_wrong_number_of_characters_for_field_str,
4889 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004890 p = s;
4891 }
4892 else
4893 {
4894 int multispace_pos = 0;
4895
4896 while (*s != NUL && *s != ',')
4897 {
4898 c1 = get_encoded_char_adv(&s);
4899 if (p == last_lmultispace)
4900 lcs_chars.leadmultispace[multispace_pos++] = c1;
4901 }
4902 p = s;
4903 }
zeertzjq1f025b02023-09-30 12:43:07 +02004904 break;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004905 }
zeertzjq1f025b02023-09-30 12:43:07 +02004906
4907 c2 = c3 = 0;
4908 s = p + len + 1;
zeertzjq6a8d2e12024-01-17 20:54:49 +01004909 if (*s == NUL)
4910 return field_value_err(errbuf, errbuflen,
4911 e_wrong_number_of_characters_for_field_str,
4912 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004913 c1 = get_encoded_char_adv(&s);
4914 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004915 return field_value_err(errbuf, errbuflen,
4916 e_wrong_character_width_for_field_str,
4917 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004918 if (tab[i].cp == &lcs_chars.tab2)
4919 {
4920 if (*s == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004921 return field_value_err(errbuf, errbuflen,
4922 e_wrong_number_of_characters_for_field_str,
4923 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004924 c2 = get_encoded_char_adv(&s);
4925 if (char2cells(c2) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004926 return field_value_err(errbuf, errbuflen,
4927 e_wrong_character_width_for_field_str,
4928 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004929 if (!(*s == ',' || *s == NUL))
4930 {
4931 c3 = get_encoded_char_adv(&s);
4932 if (char2cells(c3) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004933 return field_value_err(errbuf, errbuflen,
4934 e_wrong_character_width_for_field_str,
4935 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004936 }
4937 }
4938
4939 if (*s == ',' || *s == NUL)
4940 {
4941 if (round > 0)
4942 {
4943 if (tab[i].cp == &lcs_chars.tab2)
4944 {
4945 lcs_chars.tab1 = c1;
4946 lcs_chars.tab2 = c2;
4947 lcs_chars.tab3 = c3;
4948 }
4949 else if (tab[i].cp != NULL)
4950 *(tab[i].cp) = c1;
4951
4952 }
4953 p = s;
4954 break;
4955 }
zeertzjq6a8d2e12024-01-17 20:54:49 +01004956 else
4957 return field_value_err(errbuf, errbuflen,
4958 e_wrong_number_of_characters_for_field_str,
4959 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004960 }
4961
zeertzjq1f025b02023-09-30 12:43:07 +02004962 if (i == entries)
4963 return e_invalid_argument;
4964
Bram Moolenaare677df82019-09-02 22:31:11 +02004965 if (*p == ',')
4966 ++p;
4967 }
4968 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004969
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004970 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004971 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004972 if (is_listchars)
4973 {
4974 vim_free(wp->w_lcs_chars.multispace);
4975 vim_free(wp->w_lcs_chars.leadmultispace);
4976 wp->w_lcs_chars = lcs_chars;
4977 }
4978 else
4979 {
4980 wp->w_fill_chars = fill_chars;
4981 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02004982 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004983
4984 return NULL; // no error
4985}
zeertzjq8ca29b62022-08-09 12:53:14 +01004986
4987/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004988 * Handle the new value of 'fillchars'.
4989 */
4990 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004991set_fillchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4992 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004993{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004994 return set_chars_option(wp, val, FALSE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004995}
4996
4997/*
4998 * Handle the new value of 'listchars'.
4999 */
5000 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01005001set_listchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
5002 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005003{
zeertzjq6a8d2e12024-01-17 20:54:49 +01005004 return set_chars_option(wp, val, TRUE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005005}
5006
5007/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02005008 * Function given to ExpandGeneric() to obtain possible arguments of the
5009 * 'fillchars' option.
5010 */
5011 char_u *
5012get_fillchars_name(expand_T *xp UNUSED, int idx)
5013{
5014 if (idx >= (int)(sizeof(filltab) / sizeof(filltab[0])))
5015 return NULL;
5016
5017 return (char_u*)filltab[idx].name;
5018}
5019
5020/*
5021 * Function given to ExpandGeneric() to obtain possible arguments of the
5022 * 'listchars' option.
5023 */
5024 char_u *
5025get_listchars_name(expand_T *xp UNUSED, int idx)
5026{
5027 if (idx >= (int)(sizeof(lcstab) / sizeof(lcstab[0])))
5028 return NULL;
5029
5030 return (char_u*)lcstab[idx].name;
5031}
5032
5033/*
zeertzjq8ca29b62022-08-09 12:53:14 +01005034 * Check all global and local values of 'listchars' and 'fillchars'.
5035 * Return an untranslated error messages if any of them is invalid, NULL
5036 * otherwise.
5037 */
5038 char *
5039check_chars_options(void)
5040{
5041 tabpage_T *tp;
5042 win_T *wp;
5043
zeertzjq6a8d2e12024-01-17 20:54:49 +01005044 if (set_listchars_option(curwin, 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(curwin, p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005047 return e_conflicts_with_value_of_fillchars;
5048 FOR_ALL_TAB_WINDOWS(tp, wp)
5049 {
zeertzjq6a8d2e12024-01-17 20:54:49 +01005050 if (set_listchars_option(wp, wp->w_p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005051 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005052 if (set_fillchars_option(wp, wp->w_p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005053 return e_conflicts_with_value_of_fillchars;
5054 }
5055 return NULL;
5056}