blob: 71ddbcaab220c0b77c38fbdd87af829f11596dba [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020011 * screen.c: Lower level code for displaying on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
zeertzjq99941602023-08-19 13:08:50 +020020 * ScreenCols[off] Contains the virtual columns in the line. -1 means not
zeertzjqdeb22042024-03-17 19:44:30 +010021 * available or before buffer text.
Bram Moolenaarb9081882022-07-09 04:56:24 +010022 *
23 * LineOffset[row] Contains the offset into ScreenLines*[], ScreenAttrs[]
24 * and ScreenCols[] for each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025 * LineWraps[row] Flag for each line whether it wraps to the next line.
26 *
27 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
28 * one character which occupies two display cells.
29 * For UTF-8 a multi-byte character is converted to Unicode and stored in
30 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010031 * character without composing chars ScreenLinesUC[] will be 0 and
32 * ScreenLinesC[][] is not used. When the character occupies two display
33 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000034 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010035 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000036 * ScreenLines2[] is only used for euc-jp to store the second byte if the
37 * first byte is 0x8e (single-width character).
38 *
39 * The screen_*() functions write to the screen and handle updating
40 * ScreenLines[].
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 */
42
43#include "vim.h"
44
45/*
46 * The attributes that are actually active for writing to the screen.
47 */
48static int screen_attr = 0;
49
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar838b7462022-09-26 15:19:56 +010051static int screenclear2(int doclear);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020052static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010053static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020054static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void win_rest_invalid(win_T *wp);
56static void msg_pos_mode(void);
57static void recording_mode(int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaar63d9e732019-12-05 21:10:38 +010059// Ugly global: overrule attribute used by screen_char()
Bram Moolenaar071d4272004-06-13 20:20:40 +000060static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaar860cae12010-06-05 23:22:07 +020062#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020063/*
64 * Return TRUE if the cursor line in window "wp" may be concealed, according
65 * to the 'concealcursor' option.
66 */
67 int
Bram Moolenaar05540972016-01-30 20:31:25 +010068conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020069{
70 int c;
71
72 if (*wp->w_p_cocu == NUL)
73 return FALSE;
Bram Moolenaar24959102022-05-07 20:01:16 +010074 if (get_real_state() & MODE_VISUAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020075 c = 'v';
Bram Moolenaar24959102022-05-07 20:01:16 +010076 else if (State & MODE_INSERT)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020077 c = 'i';
Bram Moolenaar24959102022-05-07 20:01:16 +010078 else if (State & MODE_NORMAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020079 c = 'n';
Bram Moolenaar24959102022-05-07 20:01:16 +010080 else if (State & MODE_CMDLINE)
Bram Moolenaarca8c9862010-07-24 15:00:38 +020081 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +020082 else
83 return FALSE;
84 return vim_strchr(wp->w_p_cocu, c) != NULL;
85}
86
87/*
88 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
Bram Moolenaarea042672021-06-29 20:22:32 +020089 * To be called after changing the state, "was_concealed" is the value of
90 * "conceal_cursor_line()" before the change.
91 * "
Bram Moolenaarf5963f72010-07-23 22:10:27 +020092 */
93 void
Bram Moolenaarea042672021-06-29 20:22:32 +020094conceal_check_cursor_line(int was_concealed)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020095{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000096 if (curwin->w_p_cole <= 0 || conceal_cursor_line(curwin) == was_concealed)
97 return;
Bram Moolenaarea042672021-06-29 20:22:32 +020098
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000099 int wcol = curwin->w_wcol;
Bram Moolenaarea042672021-06-29 20:22:32 +0200100
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000101 need_cursor_line_redraw = TRUE;
102 // Need to recompute cursor column, e.g., when starting Visual mode
103 // without concealing.
104 curs_columns(TRUE);
105
106 // When concealing now w_wcol will be computed wrong, keep the previous
107 // value, it will be updated in win_line().
108 if (!was_concealed)
109 curwin->w_wcol = wcol;
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200110}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#endif
112
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200113/*
114 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
115 * window then get the "Pmenu" highlight attribute.
116 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200117 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200118get_wcr_attr(win_T *wp)
119{
120 int wcr_attr = 0;
121
122 if (*wp->w_p_wcr != NUL)
123 wcr_attr = syn_name2attr(wp->w_p_wcr);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100124#ifdef FEAT_PROP_POPUP
Bram Moolenaarc363fe12019-08-04 18:13:46 +0200125 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200126 {
127 if (wp->w_popup_flags & POPF_INFO)
128 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
129 else
130 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
131 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200132#endif
133 return wcr_attr;
134}
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100137 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
138 * Return the new offset.
139 */
140 static int
141screen_fill_end(
142 win_T *wp,
143 int c1,
144 int c2,
145 int off,
146 int width,
147 int row,
148 int endrow,
149 int attr)
150{
151 int nn = off + width;
152
153 if (nn > wp->w_width)
154 nn = wp->w_width;
155#ifdef FEAT_RIGHTLEFT
156 if (wp->w_p_rl)
157 {
158 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
159 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
160 c1, c2, attr);
161 }
162 else
163#endif
164 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
165 wp->w_wincol + off, (int)wp->w_wincol + nn,
166 c1, c2, attr);
167 return nn;
168}
169
170/*
171 * Clear lines near the end the window and mark the unused lines with "c1".
172 * use "c2" as the filler character.
173 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200175 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100176win_draw_end(
177 win_T *wp,
178 int c1,
179 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100180 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +0100181 int row,
182 int endrow,
183 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200186 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200187 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200188
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200189 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100190
191 if (draw_margin)
192 {
Bram Moolenaar1c934292015-01-27 16:39:29 +0100193#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100194 int fdc = compute_foldcolumn(wp, 0);
195
196 if (fdc > 0)
197 // draw the fold column
198 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200199 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +0100200#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100201#ifdef FEAT_SIGNS
202 if (signcolumn_on(wp))
203 // draw the sign column
204 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200205 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100206#endif
207 if ((wp->w_p_nu || wp->w_p_rnu)
208 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
209 // draw the number column
210 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200211 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214#ifdef FEAT_RIGHTLEFT
215 if (wp->w_p_rl)
216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100218 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200219 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100221 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200222 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224 else
225#endif
226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100228 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200229 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 set_empty_rows(wp, row);
233}
234
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200235#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236/*
Bram Moolenaar1c934292015-01-27 16:39:29 +0100237 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
238 * space is available for window "wp", minus "col".
239 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200240 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100241compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +0100242{
243 int fdc = wp->w_p_fdc;
244 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +0200245 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100246
247 if (fdc > wwidth - (col + wmw))
248 fdc = wwidth - (col + wmw);
249 return fdc;
250}
251
252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000254 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100255 * Returns the number of bytes stored in 'p'. When non-multibyte characters are
256 * used for the fold column markers, this is equal to 'fdc' setting. Otherwise,
257 * this will be greater than 'fdc'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 */
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100259 size_t
Bram Moolenaar05540972016-01-30 20:31:25 +0100260fill_foldcolumn(
261 char_u *p,
262 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100263 int closed, // TRUE of FALSE
264 linenr_T lnum) // current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265{
266 int i = 0;
267 int level;
268 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000269 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100270 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100271 size_t byte_counter = 0;
272 int symbol = 0;
273 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100275 // Init to all spaces.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100276 vim_memset(p, ' ', MAX_MCO * fdc + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278 level = win_foldinfo.fi_level;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100279 empty = (fdc == 1) ? 0 : 1;
280
281 // If the column is too narrow, we start at the lowest level that
Bram Moolenaar008bff92021-03-04 21:55:58 +0100282 // fits and use numbers to indicate the depth.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100283 first_level = level - fdc - closed + 1 + empty;
284 if (first_level < 1)
285 first_level = 1;
286
287 for (i = 0; i < MIN(fdc, level); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100289 if (win_foldinfo.fi_lnum == lnum
290 && first_level + i >= win_foldinfo.fi_low_level)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100291 symbol = wp->w_fill_chars.foldopen;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100292 else if (first_level == 1)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100293 symbol = wp->w_fill_chars.foldsep;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100294 else if (first_level + i <= 9)
295 symbol = '0' + first_level + i;
296 else
297 symbol = '>';
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000298
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100299 len = utf_char2bytes(symbol, &p[byte_counter]);
300 byte_counter += len;
301 if (first_level + i >= level)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100303 i++;
304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 }
306 }
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 if (closed)
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100309 {
310 if (symbol != 0)
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100311 {
312 // rollback length and the character
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100313 byte_counter -= len;
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100314 if (len > 1)
315 // for a multibyte character, erase all the bytes
316 vim_memset(p + byte_counter, ' ', len);
317 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100318 symbol = wp->w_fill_chars.foldclosed;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100319 len = utf_char2bytes(symbol, &p[byte_counter]);
320 byte_counter += len;
321 }
322
323 return MAX(byte_counter + (fdc - i), (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324}
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100325#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000327/*
328 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +0100329 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000330 */
331 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100332comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000333{
334 int i;
335
336 for (i = 0; i < Screen_mco; ++i)
337 {
338 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
339 return TRUE;
340 if (ScreenLinesC[i][off_from] == 0)
341 break;
342 }
343 return FALSE;
344}
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000345
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346/*
347 * Check whether the given character needs redrawing:
348 * - the (first byte of the) character is different
349 * - the attributes are different
350 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000351 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 */
353 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100354char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355{
356 if (cols > 0
357 && ((ScreenLines[off_from] != ScreenLines[off_to]
358 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 || (enc_dbcs != 0
360 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
361 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
362 ? ScreenLines2[off_from] != ScreenLines2[off_to]
363 : (cols > 1 && ScreenLines[off_from + 1]
364 != ScreenLines[off_to + 1])))
365 || (enc_utf8
366 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
367 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000368 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +0200369 || ((*mb_off2cells)(off_from, off_from + cols) > 1
370 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +0100371 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 return TRUE;
373 return FALSE;
374}
375
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200376#if defined(FEAT_TERMINAL) || defined(PROTO)
377/*
378 * Return the index in ScreenLines[] for the current screen line.
379 */
380 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000381screen_get_current_line_off(void)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200382{
383 return (int)(current_ScreenLine - ScreenLines);
384}
385#endif
386
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100387#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200388/*
389 * Return TRUE if this position has a higher level popup or this cell is
390 * transparent in the current popup.
391 */
392 static int
393blocked_by_popup(int row, int col)
394{
395 int off;
396
397 if (!popup_visible)
398 return FALSE;
399 off = row * screen_Columns + col;
400 return popup_mask[off] > screen_zindex || popup_transparent[off];
401}
402#endif
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200405 * Reset the highlighting. Used before clearing the screen.
406 */
407 void
408reset_screen_attr(void)
409{
410#ifdef FEAT_GUI
411 if (gui.in_use)
412 // Use a code that will reset gui.highlight_mask in
413 // gui_stop_highlight().
414 screen_attr = HL_ALL + 1;
415 else
416#endif
417 // Use attributes that is very unlikely to appear in text.
418 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
419}
420
421/*
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100422 * Return TRUE if the character at "row" / "col" is under the popup menu and it
423 * will be redrawn soon or it is under another popup.
424 */
425 static int
426skip_for_popup(int row, int col)
427{
428 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
429 if (pum_under_menu(row, col, TRUE)
430#ifdef FEAT_PROP_POPUP
431 && screen_zindex <= POPUPMENU_ZINDEX
432#endif
433 )
434 return TRUE;
435#ifdef FEAT_PROP_POPUP
436 if (blocked_by_popup(row, col))
437 return TRUE;
438#endif
439 return FALSE;
440}
441
442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 * Move one "cooked" screen line to the screen, but only the characters that
444 * have actually changed. Handle insert/delete character.
445 * "coloff" gives the first column on the screen for this line.
446 * "endcol" gives the columns where valid characters are.
447 * "clear_width" is the width of the window. It's > 0 if the rest of the line
448 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200449 * "flags" can have bits:
450 * SLF_POPUP popup window
451 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
453 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
zeertzjqd0c1b772024-03-16 15:03:33 +0100454 * SLF_INC_VCOL:
455 * When FALSE, use "last_vcol" for ScreenCols[] of the columns to clear.
456 * When TRUE, use an increasing sequence starting from "last_vcol + 1" for
457 * ScreenCols[] of the columns to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200459 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100460screen_line(
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100461 win_T *wp,
462 int row,
463 int coloff,
464 int endcol,
465 int clear_width,
zeertzjqd0c1b772024-03-16 15:03:33 +0100466 colnr_T last_vcol,
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100467 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
469 unsigned off_from;
470 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000471 unsigned max_off_from;
472 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100475 int force = FALSE; // force update rest of the line
476 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100478 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479#endif
480 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100481 int redraw_next; // redraw_this for next character
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100482#ifdef FEAT_GUI_MSWIN
483 int changed_this; // TRUE if character changed
484 int changed_next; // TRUE if next character changed
485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100487 int char_cells; // 1: normal char
488 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100490 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100491 if (row >= Rows)
492 row = Rows - 1;
493 if (endcol > Columns)
494 endcol = Columns;
495
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496# ifdef FEAT_CLIPBOARD
497 clip_may_clear_selection(row, row);
498# endif
499
500 off_from = (unsigned)(current_ScreenLine - ScreenLines);
501 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000502 max_off_from = off_from + screen_Columns;
503 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
505#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200506 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100508 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (clear_width > 0)
510 {
zeertzjqdeb22042024-03-17 19:44:30 +0100511 int clear_start = col;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 while (col <= endcol && ScreenLines[off_to] == ' '
514 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100515 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 {
517 ++off_to;
518 ++col;
519 }
520 if (col <= endcol)
521 screen_fill(row, row + 1, col + coloff,
522 endcol + coloff + 1, ' ', ' ', 0);
zeertzjqdeb22042024-03-17 19:44:30 +0100523
524 for (int i = endcol; i >= clear_start; i--)
525 ScreenCols[off_to + (i - col)] =
526 (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
528 col = endcol + 1;
529 off_to = LineOffset[row] + col + coloff;
530 off_from += col;
531 endcol = (clear_width > 0 ? clear_width : -clear_width);
532 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100533#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100535#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100536 // First char of a popup window may go on top of the right half of a
537 // double-wide character. Clear the left half to avoid it getting the popup
538 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100539 if (coloff > 0 && enc_utf8
540 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200541 && ScreenLinesUC[off_to - 1] != 0
542 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100543 {
544 ScreenLines[off_to - 1] = ' ';
545 ScreenLinesUC[off_to - 1] = 0;
546 screen_char(off_to - 1, row, col + coloff - 1);
547 }
548#endif
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100551#ifdef FEAT_GUI_MSWIN
552 changed_next = redraw_next;
553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 while (col < endcol)
556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000558 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 else
560 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
562 redraw_this = redraw_next;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100563 redraw_next = force || char_needs_redraw(off_from + char_cells,
564 off_to + char_cells, endcol - col - char_cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
566#ifdef FEAT_GUI
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100567# ifdef FEAT_GUI_MSWIN
568 changed_this = changed_next;
569 changed_next = redraw_next;
570# endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100571 // If the next character was bold, then redraw the current character to
572 // remove any pixels that might have spilt over into us. This only
573 // happens in the GUI.
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100574 // With MS-Windows antialiasing may also cause pixels to spill over
575 // from a previous character, no matter attributes, always redraw if a
576 // character changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (redraw_next && gui.in_use)
578 {
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100579# ifndef FEAT_GUI_MSWIN
Bram Moolenaarb9081882022-07-09 04:56:24 +0100580 hl = ScreenAttrs[off_to + char_cells];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000581 if (hl > HL_ALL)
582 hl = syn_attr2attr(hl);
583 if (hl & HL_BOLD)
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 redraw_this = TRUE;
586 }
587#endif
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100588 // Do not redraw if under the popup menu.
589 if (redraw_this && skip_for_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200590 redraw_this = FALSE;
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100591
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 if (redraw_this)
593 {
594 /*
595 * Special handling when 'xs' termcap flag set (hpterm):
596 * Attributes for characters are stored at the position where the
597 * cursor is when writing the highlighting code. The
598 * start-highlighting code must be written with the cursor on the
599 * first highlighted character. The stop-highlighting code must
600 * be written with the cursor just after the last highlighted
601 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100602 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * to clear the rest of the line, and force redrawing it
604 * completely.
605 */
606 if ( p_wiv
607 && !force
608#ifdef FEAT_GUI
609 && !gui.in_use
610#endif
611 && ScreenAttrs[off_to] != 0
612 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
613 {
614 /*
615 * Need to remove highlighting attributes here.
616 */
617 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100618 out_str(T_CE); // clear rest of this screen line
619 screen_start(); // don't know where cursor is now
620 force = TRUE; // force redraw of rest of the line
621 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623 /*
624 * If the previous character was highlighted, need to stop
625 * highlighting at this character.
626 */
627 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
628 {
629 screen_attr = ScreenAttrs[off_to - 1];
630 term_windgoto(row, col + coloff);
631 screen_stop_highlight();
632 }
633 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100634 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (enc_dbcs != 0)
637 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100638 // Check if overwriting a double-byte with a single-byte or
639 // the other way around requires another character to be
640 // redrawn. For UTF-8 this isn't needed, because comparing
641 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (char_cells == 1
643 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000644 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100646 // Writing a single-cell character over a double-cell
647 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 ScreenLines[off_to + 1] = 0;
649 redraw_next = TRUE;
650 }
651 else if (char_cells == 2
652 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000653 && (*mb_off2cells)(off_to, max_off_to) == 1
654 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100656 // Writing the second half of a double-cell character over
657 // a double-cell character: need to redraw the second
658 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 ScreenLines[off_to + 2] = 0;
660 redraw_next = TRUE;
661 }
662
663 if (enc_dbcs == DBCS_JPNU)
664 ScreenLines2[off_to] = ScreenLines2[off_from];
665 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100666 // When writing a single-width character over a double-width
667 // character and at the end of the redrawn text, need to clear out
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000668 // the right half of the old character.
669 // Also required when writing the right half of a double-width
670 // char over the left half of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if (has_mbyte && col + char_cells == endcol
672 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000673 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000675 && (*mb_off2cells)(off_to, max_off_to) == 1
676 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (enc_utf8)
681 {
682 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
683 if (ScreenLinesUC[off_from] != 0)
684 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000685 int i;
686
687 for (i = 0; i < Screen_mco; ++i)
688 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690 }
691 if (char_cells == 2)
692 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100695 // The bold trick makes a single column of pixels appear in the
696 // next character. When a bold character is removed, the next
697 // character should be redrawn too. This happens for our own GUI
698 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (
700# ifdef FEAT_GUI
701 gui.in_use
702# endif
703# if defined(FEAT_GUI) && defined(UNIX)
704 ||
705# endif
706# ifdef UNIX
707 term_is_xterm
708# endif
709 )
710 {
711 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000712 if (hl > HL_ALL)
713 hl = syn_attr2attr(hl);
714 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 redraw_next = TRUE;
716 }
717#endif
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100718#ifdef FEAT_GUI_MSWIN
719 // MS-Windows antialiasing may spill over to the next character,
720 // redraw that one if this one changed, no matter attributes.
721 if (gui.in_use && changed_this)
722 redraw_next = TRUE;
723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100725
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100726 // For simplicity set the attributes of second half of a
727 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000728 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000730
731 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 screen_char(off_to, row, col + coloff);
735 }
736 else if ( p_wiv
737#ifdef FEAT_GUI
738 && !gui.in_use
739#endif
740 && col + coloff > 0)
741 {
742 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
743 {
744 /*
745 * Don't output stop-highlight when moving the cursor, it will
746 * stop the highlighting when it should continue.
747 */
748 screen_attr = 0;
749 }
750 else if (screen_attr != 0)
751 screen_stop_highlight();
752 }
753
Bram Moolenaarb9081882022-07-09 04:56:24 +0100754 ScreenCols[off_to] = ScreenCols[off_from];
755 if (char_cells == 2)
zeertzjq99941602023-08-19 13:08:50 +0200756 ScreenCols[off_to + 1] = ScreenCols[off_from + 1];
Bram Moolenaarb9081882022-07-09 04:56:24 +0100757
758 off_to += char_cells;
759 off_from += char_cells;
760 col += char_cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100763 if (clear_next && !skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100765 // Clear the second half of a double-wide character of which the left
766 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 ScreenLines[off_to] = ' ';
768 if (enc_utf8)
769 ScreenLinesUC[off_to] = 0;
770 screen_char(off_to, row, col + coloff);
771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
773 if (clear_width > 0
774#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200775 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#endif
777 )
778 {
779#ifdef FEAT_GUI
780 int startCol = col;
781#endif
782
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100783 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 while (col < clear_width && ScreenLines[off_to] == ' '
785 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100786 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
zeertzjqd0c1b772024-03-16 15:03:33 +0100788 ScreenCols[off_to] =
789 (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 ++off_to;
791 ++col;
792 }
793 if (col < clear_width)
794 {
795#ifdef FEAT_GUI
796 /*
797 * In the GUI, clearing the rest of the line may leave pixels
798 * behind if the first character cleared was bold. Some bold
799 * fonts spill over the left. In this case we redraw the previous
800 * character too. If we didn't skip any blanks above, then we
801 * only redraw if the character wasn't already redrawn anyway.
802 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000803 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
805 hl = ScreenAttrs[off_to];
806 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000807 {
808 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100809
Bram Moolenaar9c697322006-10-09 20:11:17 +0000810 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100811 // for utf-8, ScreenLines[char_offset + 1] == 0 means
812 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000813 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
814 else if (enc_dbcs != 0)
815 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100816 // find previous character by counting from first
817 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000818 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000819 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000820
821 while (off < off_to)
822 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000823 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000824 off += prev_cells;
825 }
826 }
827
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100828 if (!skip_for_popup(row, col + coloff - prev_cells))
829 {
830 if (enc_dbcs != 0 && prev_cells > 1)
831 screen_char_2(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000832 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100833 else
834 screen_char(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000835 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100836 }
Bram Moolenaar9c697322006-10-09 20:11:17 +0000837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 }
839#endif
840 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
841 ' ', ' ', 0);
Bram Moolenaarb9081882022-07-09 04:56:24 +0100842 while (col < clear_width)
843 {
zeertzjqd0c1b772024-03-16 15:03:33 +0100844 ScreenCols[off_to++]
845 = (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100846 ++col;
847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
849 }
850
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200851 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100852#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200853 && !(flags & SLF_POPUP) // no separator for popup window
854#endif
855 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200857 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200858 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200859 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100861 if (!skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200863 int c;
864
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100865 c = fillchar_vsep(&hl, wp);
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200866 if (ScreenLines[off_to] != (schar_T)c
867 || (enc_utf8 && (int)ScreenLinesUC[off_to]
868 != (c >= 0x80 ? c : 0))
869 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200871 ScreenLines[off_to] = c;
872 ScreenAttrs[off_to] = hl;
873 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200875 if (c >= 0x80)
876 {
877 ScreenLinesUC[off_to] = c;
878 ScreenLinesC[0][off_to] = 0;
879 }
880 else
881 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200883 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886 }
887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 LineWraps[row] = FALSE;
889 }
890}
891
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000892#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000894 * Mirror text "str" for right-left displaying.
895 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000897 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100898rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900 char_u *p1, *p2;
901 int t;
902
903 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
904 {
905 t = *p1;
906 *p1 = *p2;
907 *p2 = t;
908 }
909}
910#endif
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * Draw the verticap separator right of window "wp" starting with line "row".
914 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200915 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100916draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
918 int hl;
919 int c;
920
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000921 if (!wp->w_vsep_width)
922 return;
923
924 // draw the vertical separator right of this window
925 c = fillchar_vsep(&hl, wp);
926 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
927 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
928 c, ' ', hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 * Return TRUE if the status line of window "wp" is connected to the status
933 * line of the window right of it. If not, then it's a vertical separator.
934 * Only call if (wp->w_vsep_width != 0).
935 */
936 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100937stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
939 frame_T *fr;
940
941 fr = wp->w_frame;
942 while (fr->fr_parent != NULL)
943 {
944 if (fr->fr_parent->fr_layout == FR_COL)
945 {
946 if (fr->fr_next != NULL)
947 break;
948 }
949 else
950 {
951 if (fr->fr_next != NULL)
952 return TRUE;
953 }
954 fr = fr->fr_parent;
955 }
956 return FALSE;
957}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960/*
961 * Get the value to show for the language mappings, active 'keymap'.
962 */
963 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100964get_keymap_str(
965 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100966 char_u *fmt, // format string containing one %s item
967 char_u *buf, // buffer for the result
968 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
970 char_u *p;
971
972 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
973 return FALSE;
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975#ifdef FEAT_EVAL
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000976 buf_T *old_curbuf = curbuf;
977 win_T *old_curwin = curwin;
978 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000980 curbuf = wp->w_buffer;
981 curwin = wp;
982 STRCPY(buf, "b:keymap_name"); // must be writable
983 ++emsg_skip;
984 s = p = eval_to_string(buf, FALSE, FALSE);
985 --emsg_skip;
986 curbuf = old_curbuf;
987 curwin = old_curwin;
988 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_KEYMAP
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000992 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
993 p = wp->w_buffer->b_p_keymap;
994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000996 p = (char_u *)"lang";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000998 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
999 buf[0] = NUL;
1000#ifdef FEAT_EVAL
1001 vim_free(s);
1002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 return buf[0] != NUL;
1004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
1006#if defined(FEAT_STL_OPT) || defined(PROTO)
1007/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001008 * Redraw the status line or ruler of window "wp".
1009 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001011 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001012win_redr_custom(
1013 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001014 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001016 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 int attr;
1018 int curattr;
1019 int row;
1020 int col = 0;
1021 int maxwidth;
1022 int width;
1023 int n;
1024 int len;
1025 int fillchar;
1026 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001027 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 char_u *p;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001029 char_u *opt_name;
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001030 int opt_scope = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001031 stl_hlrec_T *hltab;
1032 stl_hlrec_T *tabtab;
Bram Moolenaar61452852011-02-01 18:01:11 +01001033 win_T *ewp;
1034 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001036 // There is a tiny chance that this gets called recursively: When
1037 // redrawing a status line triggers redrawing the ruler or tabline.
1038 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001039 if (entered)
1040 return;
1041 entered = TRUE;
1042
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001043 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001044 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001046 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001047 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001048 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001049 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001050 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001051 maxwidth = Columns;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001052 opt_name = (char_u *)"tabline";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001054 else
1055 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001056 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001057 fillchar = fillchar_status(&attr, wp);
Sean Dewarfc8a6012023-04-17 16:41:20 +01001058 int in_status_line = wp->w_status_height != 0;
1059 maxwidth = in_status_line ? wp->w_width : Columns;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001060
1061 if (draw_ruler)
1062 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001063 stl = p_ruf;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001064 opt_name = (char_u *)"rulerformat";
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001065 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001066 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001067 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001068 if (*++stl == '-')
1069 stl++;
1070 if (atoi((char *)stl))
1071 while (VIM_ISDIGIT(*stl))
1072 stl++;
1073 if (*stl++ != '(')
1074 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001075 }
Sean Dewarfc8a6012023-04-17 16:41:20 +01001076 col = ru_col - (Columns - maxwidth);
1077 if (col < (maxwidth + 1) / 2)
1078 col = (maxwidth + 1) / 2;
1079 maxwidth -= col;
1080 if (!in_status_line)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001081 {
1082 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001083 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001084 fillchar = ' ';
1085 attr = 0;
1086 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001087 }
1088 else
1089 {
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001090 opt_name = (char_u *)"statusline";
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001091 if (*wp->w_p_stl != NUL)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001092 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001093 stl = wp->w_p_stl;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001094 opt_scope = OPT_LOCAL;
1095 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001096 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001097 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001098 }
1099
Sean Dewarfc8a6012023-04-17 16:41:20 +01001100 if (in_status_line)
1101 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001102 }
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001105 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001107 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1108 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001109 ewp = wp == NULL ? curwin : wp;
1110 p_crb_save = ewp->w_p_crb;
1111 ewp->w_p_crb = FALSE;
1112
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001113 // Make a copy, because the statusline may include a function call that
1114 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001115 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001116 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001117 stl, opt_name, opt_scope,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001118 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001119 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001120 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001122 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001123 p = transstr(buf);
1124 if (p != NULL)
1125 {
1126 vim_strncpy(buf, p, sizeof(buf) - 1);
1127 vim_free(p);
1128 }
1129
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001130 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001131 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001132 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 ++width;
1136 }
1137 buf[len] = NUL;
1138
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001139 /*
1140 * Draw each snippet with the specified highlighting.
1141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 curattr = attr;
1143 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001144 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001146 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 screen_puts_len(p, len, row, col, curattr);
1148 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001149 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001151 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001153 else if (hltab[n].userhl < 0)
1154 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001155#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001156 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1157 && wp->w_status_height != 0)
1158 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001159 else if (wp != NULL && bt_terminal(wp->w_buffer)
1160 && wp->w_status_height != 0)
1161 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001162#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001163 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001164 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001166 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 }
1168 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001169
1170 if (wp == NULL)
1171 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001172 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001173 col = 0;
1174 len = 0;
1175 p = buf;
1176 fillchar = 0;
1177 for (n = 0; tabtab[n].start != NULL; n++)
1178 {
1179 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1180 while (col < len)
1181 TabPageIdxs[col++] = fillchar;
1182 p = tabtab[n].start;
1183 fillchar = tabtab[n].userhl;
1184 }
1185 while (col < Columns)
1186 TabPageIdxs[col++] = fillchar;
1187 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001188
1189theend:
1190 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191}
1192
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001193#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195/*
1196 * Output a single character directly to the screen and update ScreenLines.
1197 */
1198 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001199screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 char_u buf[MB_MAXBYTES + 1];
1202
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001203 if (has_mbyte)
1204 buf[(*mb_char2bytes)(c, buf)] = NUL;
1205 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001206 {
1207 buf[0] = c;
1208 buf[1] = NUL;
1209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 screen_puts(buf, row, col, attr);
1211}
1212
1213/*
zeertzjq47eec672023-06-01 20:26:55 +01001214 * Get a single character directly from ScreenLines into "bytes", which must
1215 * have a size of "MB_MAXBYTES + 1".
1216 * If "attrp" is not NULL, return the character's attribute in "*attrp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
1218 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001219screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
1221 unsigned off;
1222
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001223 // safety check
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001224 if (ScreenLines == NULL || row >= screen_Rows || col >= screen_Columns)
1225 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001227 off = LineOffset[row] + col;
zeertzjq47eec672023-06-01 20:26:55 +01001228 if (attrp != NULL)
1229 *attrp = ScreenAttrs[off];
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001230 bytes[0] = ScreenLines[off];
1231 bytes[1] = NUL;
1232
1233 if (enc_utf8 && ScreenLinesUC[off] != 0)
1234 bytes[utfc_char2bytes(off, bytes)] = NUL;
1235 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1236 {
1237 bytes[0] = ScreenLines[off];
1238 bytes[1] = ScreenLines2[off];
1239 bytes[2] = NUL;
1240 }
1241 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1242 {
1243 bytes[1] = ScreenLines[off + 1];
1244 bytes[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246}
1247
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001248/*
1249 * Return TRUE if composing characters for screen posn "off" differs from
1250 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001251 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001252 */
1253 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001254screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001255{
1256 int i;
1257
1258 for (i = 0; i < Screen_mco; ++i)
1259 {
1260 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1261 return TRUE;
1262 if (u8cc[i] == 0)
1263 break;
1264 }
1265 return FALSE;
1266}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268/*
1269 * Put string '*text' on the screen at position 'row' and 'col', with
1270 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1271 * Note: only outputs within one row, message is truncated at screen boundary!
1272 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1273 */
1274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001275screen_puts(
1276 char_u *text,
1277 int row,
1278 int col,
1279 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280{
1281 screen_puts_len(text, -1, row, col, attr);
1282}
1283
1284/*
1285 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1286 * a NUL.
1287 */
1288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001289screen_puts_len(
1290 char_u *text,
1291 int textlen,
1292 int row,
1293 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001294 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001296 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 unsigned off;
1298 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001299 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001301 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 int mbyte_blen = 1;
1303 int mbyte_cells = 1;
1304 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001305 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001307#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001308 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001309 int pc, nc, nc1;
1310 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001312 int force_redraw_this;
1313 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001314 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001316 // Safety check. The check for negative row and column is to fix issue
1317 // #4102. TODO: find out why row/col could be negative.
1318 if (ScreenLines == NULL
1319 || row >= screen_Rows || row < 0
1320 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001322 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001324 // When drawing over the right half of a double-wide char clear out the
1325 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001326 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001327#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001328 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001329#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001330 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001331 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001332 if (!skip_for_popup(row, col - 1))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001333 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001334 ScreenLines[off - 1] = ' ';
1335 ScreenAttrs[off - 1] = 0;
1336 if (enc_utf8)
1337 {
1338 ScreenLinesUC[off - 1] = 0;
1339 ScreenLinesC[0][off - 1] = 0;
1340 }
1341 // redraw the previous cell, make it empty
1342 screen_char(off - 1, row, col - 1);
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001343 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001344 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001345 force_redraw_next = TRUE;
1346 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001347
Bram Moolenaar367329b2007-08-30 11:53:22 +00001348 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001349 while (col < screen_Columns
1350 && (len < 0 || (int)(ptr - text) < len)
1351 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 {
1353 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001354 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (has_mbyte)
1356 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001357 mbyte_blen = enc_utf8 && len > 0
1358 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1359 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1361 mbyte_cells = 1;
1362 else if (enc_dbcs != 0)
1363 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001364 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001366 u8c = len >= 0
1367 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1368 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001370#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1372 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001373 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1375 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001376 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 nc = NUL;
1378 nc1 = NUL;
1379 }
1380 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001382 nc = len >= 0
1383 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1384 (int)((text + len) - ptr - mbyte_blen))
1385 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001386 nc1 = pcc[0];
1387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 pc = prev_c;
1389 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001390 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 else
1393 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001394#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001395 if (col + mbyte_cells > screen_Columns)
1396 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001397 // Only 1 cell left, but character requires 2 cells:
1398 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001399 c = '>';
1400 mbyte_cells = 1;
1401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001405 force_redraw_this = force_redraw_next;
1406 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001407
1408 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 || (mbyte_cells == 2
1410 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1411 || (enc_dbcs == DBCS_JPNU
1412 && c == 0x8e
1413 && ScreenLines2[off] != ptr[1])
1414 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001415 && (ScreenLinesUC[off] !=
1416 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1417 || (ScreenLinesUC[off] != 0
1418 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001420 || exmode_active;
1421
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001422 if ((need_redraw || force_redraw_this) && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
1424#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001425 // The bold trick makes a single row of pixels appear in the next
1426 // character. When a bold character is removed, the next
1427 // character should be redrawn too. This happens for our own GUI
1428 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001429 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430# ifdef FEAT_GUI
1431 gui.in_use
1432# endif
1433# if defined(FEAT_GUI) && defined(UNIX)
1434 ||
1435# endif
1436# ifdef UNIX
1437 term_is_xterm
1438# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001439 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001441 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001443 if (n > HL_ALL)
1444 n = syn_attr2attr(n);
1445 if (n & HL_BOLD)
1446 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001449 // When at the end of the text and overwriting a two-cell
1450 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001451 // cell. Also when overwriting the left half of a two-cell char
1452 // with the right half of a two-cell char. Do this only once
1453 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if (clear_next_cell)
1455 clear_next_cell = FALSE;
1456 else if (has_mbyte
1457 && (len < 0 ? ptr[mbyte_blen] == NUL
1458 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001459 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001461 && (*mb_off2cells)(off, max_off) == 1
1462 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 clear_next_cell = TRUE;
1464
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001465 // Make sure we never leave a second byte of a double-byte behind,
1466 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001468 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001470 && (*mb_off2cells)(off, max_off) == 1
1471 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ScreenLines[off] = c;
1474 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001475 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (enc_utf8)
1477 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001478 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 ScreenLinesUC[off] = 0;
1480 else
1481 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001482 int i;
1483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001485 for (i = 0; i < Screen_mco; ++i)
1486 {
1487 ScreenLinesC[i][off] = u8cc[i];
1488 if (u8cc[i] == 0)
1489 break;
1490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492 if (mbyte_cells == 2)
1493 {
1494 ScreenLines[off + 1] = 0;
1495 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001496 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 screen_char(off, row, col);
1499 }
1500 else if (mbyte_cells == 2)
1501 {
1502 ScreenLines[off + 1] = ptr[1];
1503 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001504 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 screen_char_2(off, row, col);
1506 }
1507 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1508 {
1509 ScreenLines2[off] = ptr[1];
1510 screen_char(off, row, col);
1511 }
1512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 screen_char(off, row, col);
1514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 if (has_mbyte)
1516 {
1517 off += mbyte_cells;
1518 col += mbyte_cells;
1519 ptr += mbyte_blen;
1520 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001521 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001522 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001523 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001525 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001526 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
1529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
1531 ++off;
1532 ++col;
1533 ++ptr;
1534 }
1535 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001536
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001537 // If we detected the next character needs to be redrawn, but the text
1538 // doesn't extend up to there, update the character here.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001539 if (force_redraw_next && col < screen_Columns && !skip_for_popup(row, col))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001540 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001541 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1542 screen_char_2(off, row, col);
1543 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001544 screen_char(off, row, col);
1545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546}
1547
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001548#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001550 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001552 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001553start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001555 if (!p_hls || no_hlsearch)
1556 return;
1557
1558 end_search_hl(); // just in case it wasn't called before
1559 last_pat_prog(&screen_search_hl.rm);
1560 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561}
1562
1563/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001564 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001566 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001567end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001569 if (screen_search_hl.rm.regprog == NULL)
1570 return;
1571
1572 vim_regfree(screen_search_hl.rm.regprog);
1573 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001575#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001578screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 attrentry_T *aep = NULL;
1581
1582 screen_attr = attr;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001583 if (!full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001584#ifdef MSWIN
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001585 || !termcap_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001587 )
1588 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001590#ifdef FEAT_GUI
1591 if (gui.in_use)
1592 {
1593 char buf[20];
1594
1595 // The GUI handles this internally.
1596 sprintf(buf, "\033|%dh", attr);
1597 OUT_STR(buf);
1598 return;
1599 }
1600#endif
1601
1602 if (attr > HL_ALL) // special HL attr.
1603 {
1604 if (IS_CTERM)
1605 aep = syn_cterm_attr2entry(attr);
1606 else
1607 aep = syn_term_attr2entry(attr);
1608 if (aep == NULL) // did ":syntax clear"
1609 attr = 0;
1610 else
1611 attr = aep->ae_attr;
1612 }
1613#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1614 if (use_vtp())
1615 {
1616 guicolor_T defguifg, defguibg;
1617 int defctermfg, defctermbg;
1618
1619 // If FG and BG are unset, the color is undefined when
1620 // BOLD+INVERSE. Use Normal as the default value.
1621 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1622 &defguibg);
1623
1624 if (p_tgc)
1625 {
1626 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1627 term_fg_rgb_color(defguifg);
1628 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1629 term_bg_rgb_color(defguibg);
1630 }
1631 else if (t_colors >= 256)
1632 {
1633 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1634 term_fg_color(defctermfg);
1635 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1636 term_bg_color(defctermbg);
1637 }
1638 }
1639#endif
1640 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
1641 out_str(T_MD);
1642 else if (aep != NULL && cterm_normal_fg_bold && (
1643#ifdef FEAT_TERMGUICOLORS
1644 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1645 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1646 :
1647#endif
1648 t_colors > 1 && aep->ae_u.cterm.fg_color))
1649 // If the Normal FG color has BOLD attribute and the new HL
1650 // has a FG color defined, clear BOLD.
1651 out_str(T_ME);
1652 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
1653 out_str(T_SO);
1654 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
1655 out_str(T_UCS);
1656 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1657 out_str(T_USS);
1658 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1659 out_str(T_DS);
1660 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1661 out_str(T_CDS);
1662 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1663 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1664 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1665 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1666 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
1667 && *T_US != NUL)
1668 out_str(T_US);
1669 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
1670 out_str(T_CZH);
1671 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
1672 out_str(T_MR);
1673 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
1674 out_str(T_STS);
1675
1676 /*
1677 * Output the color or start string after bold etc., in case the
1678 * bold etc. override the color setting.
1679 */
1680 if (aep != NULL)
1681 {
PMuncha606f3a2023-11-15 15:35:49 +01001682 if (aep->ae_u.cterm.font > 0 && aep->ae_u.cterm.font < 12)
1683 term_font(aep->ae_u.cterm.font);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001684#ifdef FEAT_TERMGUICOLORS
1685 // When 'termguicolors' is set but fg or bg is unset,
1686 // fall back to the cterm colors. This helps for SpellBad,
1687 // where the GUI uses a red undercurl.
1688 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
1689 {
1690 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
1691 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
1693 else
1694#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001695 if (t_colors > 1)
1696 {
1697 if (aep->ae_u.cterm.fg_color)
1698 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1699 }
1700#ifdef FEAT_TERMGUICOLORS
1701 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001703 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
1704 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
1705 }
1706 else
1707#endif
1708 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001710 if (aep->ae_u.cterm.bg_color)
1711 term_bg_color(aep->ae_u.cterm.bg_color - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001713#ifdef FEAT_TERMGUICOLORS
1714 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1715 {
1716 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1717 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1718 }
1719 else
1720#endif
1721 if (t_colors > 1)
Bram Moolenaara050b942019-12-02 21:35:31 +01001722 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001723 if (aep->ae_u.cterm.ul_color)
1724 term_ul_color(aep->ae_u.cterm.ul_color - 1);
Bram Moolenaara050b942019-12-02 21:35:31 +01001725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001727 if (!IS_CTERM)
1728 {
1729 if (aep->ae_u.term.start != NULL)
1730 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
1732 }
1733}
1734
1735 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001736screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001738 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001739#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001740 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
1743 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001744#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 && termcap_active
1746#endif
1747 )
1748 {
1749#ifdef FEAT_GUI
1750 if (gui.in_use)
1751 {
1752 char buf[20];
1753
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001754 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001755 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 OUT_STR(buf);
1757 }
1758 else
1759#endif
1760 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001761 int is_under;
1762
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001763 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
1765 attrentry_T *aep;
1766
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001767 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 {
1769 /*
1770 * Assume that t_me restores the original colors!
1771 */
1772 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001773 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001774#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001775 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1776 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001777# ifdef FEAT_VTP
1778 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1779# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001780 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001781#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001782 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001783#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001784 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1785 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001786# ifdef FEAT_VTP
1787 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1788# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001789 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001790#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001791 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001793#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1794 if (use_vtp())
1795 {
1796 if (do_ME_fg && do_ME_bg)
1797 do_ME = TRUE;
1798
1799 // FG and BG cannot be separated in T_ME, which is not
1800 // efficient.
1801 if (!do_ME && do_ME_fg)
1802 out_str((char_u *)"\033|39m"); // restore FG
1803 if (!do_ME && do_ME_bg)
1804 out_str((char_u *)"\033|49m"); // restore BG
1805 }
1806 else
1807 {
1808 // Process FG and BG at once.
1809 if (!do_ME)
1810 do_ME = do_ME_fg | do_ME_bg;
1811 }
1812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 else
1815 {
1816 aep = syn_term_attr2entry(screen_attr);
1817 if (aep != NULL && aep->ae_u.term.stop != NULL)
1818 {
1819 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1820 do_ME = TRUE;
1821 else
1822 out_str(aep->ae_u.term.stop);
1823 }
1824 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001825 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 screen_attr = 0;
1827 else
1828 screen_attr = aep->ae_attr;
1829 }
1830
1831 /*
1832 * Often all ending-codes are equal to T_ME. Avoid outputting the
1833 * same sequence several times.
1834 */
1835 if (screen_attr & HL_STANDOUT)
1836 {
1837 if (STRCMP(T_SE, T_ME) == 0)
1838 do_ME = TRUE;
1839 else
1840 out_str(T_SE);
1841 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001842 is_under = (screen_attr & (HL_UNDERCURL
1843 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
1844 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001845 {
1846 if (STRCMP(T_UCE, T_ME) == 0)
1847 do_ME = TRUE;
1848 else
1849 out_str(T_UCE);
1850 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001851 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
1853 if (STRCMP(T_UE, T_ME) == 0)
1854 do_ME = TRUE;
1855 else
1856 out_str(T_UE);
1857 }
1858 if (screen_attr & HL_ITALIC)
1859 {
1860 if (STRCMP(T_CZR, T_ME) == 0)
1861 do_ME = TRUE;
1862 else
1863 out_str(T_CZR);
1864 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001865 if (screen_attr & HL_STRIKETHROUGH)
1866 {
1867 if (STRCMP(T_STE, T_ME) == 0)
1868 do_ME = TRUE;
1869 else
1870 out_str(T_STE);
1871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
1873 out_str(T_ME);
1874
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001875#ifdef FEAT_TERMGUICOLORS
1876 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001878 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001879 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001880 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001881 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02001882 if (cterm_normal_ul_gui_color != INVALCOLOR)
1883 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001884 }
1885 else
1886#endif
1887 {
1888 if (t_colors > 1)
1889 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001890 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001891 if (cterm_normal_fg_color != 0)
1892 term_fg_color(cterm_normal_fg_color - 1);
1893 if (cterm_normal_bg_color != 0)
1894 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02001895 if (cterm_normal_ul_color != 0)
1896 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001897 if (cterm_normal_fg_bold)
1898 out_str(T_MD);
1899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901 }
1902 }
1903 screen_attr = 0;
1904}
1905
1906/*
1907 * Reset the colors for a cterm. Used when leaving Vim.
1908 * The machine specific code may override this again.
1909 */
1910 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001911reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001913 if (!IS_CTERM)
1914 return;
1915
1916 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001917#ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001918 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
1919 || cterm_normal_bg_gui_color != INVALCOLOR)
1920 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001921#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 out_str(T_OP);
1926 screen_attr = -1;
1927 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001928 if (cterm_normal_fg_bold)
1929 {
1930 out_str(T_ME);
1931 screen_attr = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
1933}
1934
1935/*
1936 * Put character ScreenLines["off"] on the screen at position "row" and "col",
1937 * using the attributes from ScreenAttrs["off"].
1938 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001939 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001940screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
1942 int attr;
1943
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001944 // Check for illegal values, just in case (could happen just after
1945 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (row >= screen_Rows || col >= screen_Columns)
1947 return;
1948
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001949 // Outputting a character in the last cell on the screen may scroll the
1950 // screen up. Only do it when the "xn" termcap property is set, otherwise
1951 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01001952 if (*T_XN == NUL
1953 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001955 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 && !cmdmsg_rl
1957#endif
1958 )
1959 {
1960 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001961 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 return;
1963 }
1964
1965 /*
1966 * Stop highlighting first, so it's easier to move the cursor.
1967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (screen_char_attr != 0)
1969 attr = screen_char_attr;
1970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 attr = ScreenAttrs[off];
1972 if (screen_attr != attr)
1973 screen_stop_highlight();
1974
1975 windgoto(row, col);
1976
1977 if (screen_attr != attr)
1978 screen_start_highlight(attr);
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (enc_utf8 && ScreenLinesUC[off] != 0)
1981 {
1982 char_u buf[MB_MAXBYTES + 1];
1983
Bram Moolenaarcb070082016-04-02 22:14:51 +02001984 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001985 {
1986 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01001987#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001988 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001989#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001990 )
1991 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001992 // Clear the two screen cells. If the character is actually
1993 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001994 out_str((char_u *)" ");
1995 term_windgoto(row, col);
1996 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001997 // not sure where the cursor is after drawing the ambiguous width
1998 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02001999 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002000 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002001 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002003
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002004 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002005 buf[utfc_char2bytes(off, buf)] = NUL;
2006 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
2008 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002012 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2014 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016
2017 screen_cur_col++;
2018}
2019
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020/*
2021 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2022 * on the screen at position 'row' and 'col'.
2023 * The attributes of the first byte is used for all. This is required to
2024 * output the two bytes of a double-byte character with nothing in between.
2025 */
2026 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002027screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002029 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2031 return;
2032
dundargocc57b5bc2022-11-02 13:30:51 +00002033 // Outputting the last character on the screen may scroll the screen up.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002034 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2036 {
2037 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002038 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 return;
2040 }
2041
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002042 // Output the first byte normally (positions the cursor), then write the
2043 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 screen_char(off, row, col);
2045 out_char(ScreenLines[off + 1]);
2046 ++screen_cur_col;
2047}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049/*
2050 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2051 * This uses the contents of ScreenLines[] and doesn't change it.
2052 */
2053 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002054screen_draw_rectangle(
2055 int row,
2056 int col,
2057 int height,
2058 int width,
2059 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
2061 int r, c;
2062 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002063 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002065 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002066 if (ScreenLines == NULL)
2067 return;
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 if (invert)
2070 screen_char_attr = HL_INVERSE;
2071 for (r = row; r < row + height; ++r)
2072 {
2073 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002074 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 for (c = col; c < col + width; ++c)
2076 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002077 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002079 if (!skip_for_popup(r, c))
2080 screen_char_2(off + c, r, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 ++c;
2082 }
2083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002085 if (!skip_for_popup(r, c))
2086 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002087 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 }
2091 }
2092 screen_char_attr = 0;
2093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095/*
2096 * Redraw the characters for a vertically split window.
2097 */
2098 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002099redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100{
2101 int col;
2102 int width;
2103
2104# ifdef FEAT_CLIPBOARD
2105 clip_may_clear_selection(row, end - 1);
2106# endif
2107
2108 if (wp == NULL)
2109 {
2110 col = 0;
2111 width = Columns;
2112 }
2113 else
2114 {
2115 col = wp->w_wincol;
2116 width = wp->w_width;
2117 }
2118 screen_draw_rectangle(row, col, end - row, width, FALSE);
2119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002121 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002122space_to_screenline(int off, int attr)
2123{
2124 ScreenLines[off] = ' ';
2125 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002126 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002127 if (enc_utf8)
2128 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002129}
2130
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002132 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2133 * to "end_col" (exclusive) with character "c1" in first column followed by
2134 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 */
2136 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002137screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002138 int start_row,
2139 int end_row,
2140 int start_col,
2141 int end_col,
2142 int c1,
2143 int c2,
2144 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002146 int row;
2147 int col;
2148 int off;
2149 int end_off;
2150 int did_delete;
2151 int c;
2152 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002154 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#endif
2156
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002157 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002159 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 end_col = screen_Columns;
2161 if (ScreenLines == NULL
2162 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002163 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 return;
2165
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002166 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 norm_term = (
2168#ifdef FEAT_GUI
2169 !gui.in_use &&
2170#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002171 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 for (row = start_row; row < end_row; ++row)
2173 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002174 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002175#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002176 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002177#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002178 )
2179 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002180 // When drawing over the right half of a double-wide char clear
2181 // out the left half. When drawing over the left half of a
2182 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002183 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002184 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002185 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002186 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002187 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 /*
2190 * Try to use delete-line termcap code, when no attributes or in a
2191 * "normal" terminal, where a bold/italic space is just a
2192 * space.
2193 */
2194 did_delete = FALSE;
2195 if (c2 == ' '
2196 && end_col == Columns
2197 && can_clear(T_CE)
2198 && (attr == 0
2199 || (norm_term
2200 && attr <= HL_ALL
2201 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2202 {
2203 /*
2204 * check if we really need to clear something
2205 */
2206 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002207 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 ++col;
2209
2210 off = LineOffset[row] + col;
2211 end_off = LineOffset[row] + end_col;
2212
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002213 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (enc_utf8)
2215 while (off < end_off && ScreenLines[off] == ' '
2216 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2217 ++off;
2218 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 while (off < end_off && ScreenLines[off] == ' '
2220 && ScreenAttrs[off] == 0)
2221 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002222 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224 col = off - LineOffset[row];
2225 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002226 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002228 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002230 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002232 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 ++off;
2234 }
2235 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002236 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
2238
2239 off = LineOffset[row] + start_col;
2240 c = c1;
2241 for (col = start_col; col < end_col; ++col)
2242 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002243 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002244 || (enc_utf8 && (int)ScreenLinesUC[off]
2245 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002247 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248#if defined(FEAT_GUI) || defined(UNIX)
2249 || force_next
2250#endif
2251 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002252 // Skip if under a(nother) popup.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002253 && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
2255#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002256 // The bold trick may make a single row of pixels appear in
2257 // the next character. When a bold character is removed, the
2258 // next character should be redrawn too. This happens for our
2259 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 if (
2261# ifdef FEAT_GUI
2262 gui.in_use
2263# endif
2264# if defined(FEAT_GUI) && defined(UNIX)
2265 ||
2266# endif
2267# ifdef UNIX
2268 term_is_xterm
2269# endif
2270 )
2271 {
2272 if (ScreenLines[off] != ' '
2273 && (ScreenAttrs[off] > HL_ALL
2274 || ScreenAttrs[off] & HL_BOLD))
2275 force_next = TRUE;
2276 else
2277 force_next = FALSE;
2278 }
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002279#endif // FEAT_GUI || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (enc_utf8)
2282 {
2283 if (c >= 0x80)
2284 {
2285 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002286 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288 else
2289 ScreenLinesUC[off] = 0;
2290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 ScreenAttrs[off] = attr;
2292 if (!did_delete || c != ' ')
2293 screen_char(off, row, col);
2294 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002295 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 ++off;
2297 if (col == start_col)
2298 {
2299 if (did_delete)
2300 break;
2301 c = c2;
2302 }
2303 }
2304 if (end_col == Columns)
2305 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002306 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002309 if (start_col == 0 && end_col == Columns
2310 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002311 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002312 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002313 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315 }
2316}
2317
2318/*
2319 * Check if there should be a delay. Used before clearing or redrawing the
2320 * screen or the command line.
2321 */
2322 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002323check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324{
2325 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2326 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002327 && emsg_silent == 0
2328 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002331 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 emsg_on_display = FALSE;
2333 if (check_msg_scroll)
2334 msg_scroll = FALSE;
2335 }
2336}
2337
2338/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002339 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2340 */
2341 static void
2342clear_TabPageIdxs(void)
2343{
2344 int scol;
2345
2346 for (scol = 0; scol < Columns; ++scol)
2347 TabPageIdxs[scol] = 0;
2348}
2349
2350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002352 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 * Returns TRUE if there is a valid screen to write to.
2354 * Returns FALSE when starting up and screen not initialized yet.
2355 */
2356 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002357screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002359 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 return (ScreenLines != NULL);
2361}
2362
2363/*
2364 * Resize the shell to Rows and Columns.
2365 * Allocate ScreenLines[] and associated items.
2366 *
2367 * There may be some time between setting Rows and Columns and (re)allocating
2368 * ScreenLines[]. This happens when starting up and when (manually) changing
2369 * the shell size. Always use screen_Rows and screen_Columns to access items
2370 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2371 * final size of the shell is needed.
2372 */
2373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002374screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
2376 int new_row, old_row;
2377#ifdef FEAT_GUI
2378 int old_Rows;
2379#endif
2380 win_T *wp;
2381 int outofmem = FALSE;
2382 int len;
2383 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002385 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002388 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 unsigned *new_LineOffset;
2390 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002391 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002392#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002393 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002394 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002395 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002396#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002397 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002398 static int entered = FALSE; // avoid recursiveness
2399 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002400 int retry_count = 0;
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002401 int found_null;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002403retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 /*
2405 * Allocation of the screen buffers is done only when the size changes and
2406 * when Rows and Columns have been set and we have started doing full
2407 * screen stuff.
2408 */
2409 if ((ScreenLines != NULL
2410 && Rows == screen_Rows
2411 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 && enc_utf8 == (ScreenLinesUC != NULL)
2413 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002414 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 || Rows == 0
2416 || Columns == 0
2417 || (!full_screen && ScreenLines == NULL))
2418 return;
2419
2420 /*
2421 * It's possible that we produce an out-of-memory message below, which
2422 * will cause this function to be called again. To break the loop, just
2423 * return here.
2424 */
2425 if (entered)
2426 return;
2427 entered = TRUE;
2428
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002429 /*
2430 * Note that the window sizes are updated before reallocating the arrays,
2431 * thus we must not redraw here!
2432 */
2433 ++RedrawingDisabled;
2434
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002435 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002437#ifdef FEAT_GUI_HAIKU
2438 vim_lock_screen(); // be safe, put it here
2439#endif
2440
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002441 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
2443 /*
2444 * We're changing the size of the screen.
2445 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2446 * - Move lines from the old arrays into the new arrays, clear extra
2447 * lines (unless the screen is going to be cleared).
2448 * - Free the old arrays.
2449 *
2450 * If anything fails, make ScreenLines NULL, so we don't do anything!
2451 * Continuing with the old ScreenLines may result in a crash, because the
2452 * size is wrong.
2453 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002454 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 win_free_lsize(wp);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002456 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002457 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002458 win_free_lsize(aucmd_win[i].auc_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002459#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002460 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002461 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002462 win_free_lsize(wp);
2463 // tab-local popup windows
2464 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002465 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002466 win_free_lsize(wp);
2467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002469 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002470 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 if (enc_utf8)
2472 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002473 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002474 for (int i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002475 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2476 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
2478 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002479 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2480 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
dundargocc57b5bc2022-11-02 13:30:51 +00002481 // Clear ScreenCols to avoid a warning for uninitialized memory in
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002482 // jump_to_mouse().
2483 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002484 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2485 new_LineWraps = LALLOC_MULT(char_u, Rows);
2486 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002487#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002488 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002489 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002490 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002493 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 if (win_alloc_lines(wp) == FAIL)
2496 {
2497 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002498 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00002501 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002502 if (aucmd_win[i].auc_win != NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002503 && aucmd_win[i].auc_win->w_lines == NULL
2504 && win_alloc_lines(aucmd_win[i].auc_win) == FAIL)
2505 {
2506 outofmem = TRUE;
2507 break;
2508 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002509#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002510 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002511 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002512 if (win_alloc_lines(wp) == FAIL)
2513 {
2514 outofmem = TRUE;
2515 goto give_up;
2516 }
2517 // tab-local popup windows
2518 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002519 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002520 if (win_alloc_lines(wp) == FAIL)
2521 {
2522 outofmem = TRUE;
2523 goto give_up;
2524 }
2525#endif
2526
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002527give_up:
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002528 found_null = FALSE;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002529 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002530 if (new_ScreenLinesC[i] == NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002531 {
2532 found_null = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002533 break;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (new_ScreenLines == NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002536 || (enc_utf8 && (new_ScreenLinesUC == NULL || found_null))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002539 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 || new_LineOffset == NULL
2541 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002542 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002543#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002544 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002545 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002546 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 || outofmem)
2549 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002550 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002551 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002552 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002553 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2554
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002555 // Remember we did this to avoid getting outofmem messages over
2556 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002557 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002558 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002559 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002560 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002561 for (int i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002562 VIM_CLEAR(new_ScreenLinesC[i]);
2563 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002564 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002565 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002566 VIM_CLEAR(new_LineOffset);
2567 VIM_CLEAR(new_LineWraps);
2568 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002569#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002570 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002571 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002572 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575 else
2576 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002577 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002578
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 for (new_row = 0; new_row < Rows; ++new_row)
2580 {
2581 new_LineOffset[new_row] = new_row * Columns;
2582 new_LineWraps[new_row] = FALSE;
2583
Olaf Seibertfd472652024-02-01 21:11:16 +01002584 (void)vim_memset(new_ScreenLines + new_row * Columns,
2585 ' ', (size_t)Columns * sizeof(schar_T));
2586 if (enc_utf8)
2587 {
2588 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2589 0, (size_t)Columns * sizeof(u8char_T));
2590 for (int i = 0; i < p_mco; ++i)
2591 (void)vim_memset(new_ScreenLinesC[i]
2592 + new_row * Columns,
2593 0, (size_t)Columns * sizeof(u8char_T));
2594 }
2595 if (enc_dbcs == DBCS_JPNU)
2596 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2597 0, (size_t)Columns * sizeof(schar_T));
2598 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2599 0, (size_t)Columns * sizeof(sattr_T));
2600 (void)vim_memset(new_ScreenCols + new_row * Columns,
2601 0, (size_t)Columns * sizeof(colnr_T));
2602
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 /*
2604 * If the screen is not going to be cleared, copy as much as
2605 * possible from the old screen to the new one and clear the rest
2606 * (used when resizing the window at the "--more--" prompt or when
2607 * executing an external command, for the GUI).
2608 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002609 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002612 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
2614 if (screen_Columns < Columns)
2615 len = screen_Columns;
2616 else
2617 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002618 // When switching to utf-8 don't copy characters, they
2619 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002620 if (!(enc_utf8 && ScreenLinesUC == NULL)
2621 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002622 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2623 ScreenLines + LineOffset[old_row],
2624 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002625 if (enc_utf8 && ScreenLinesUC != NULL
2626 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
2628 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2629 ScreenLinesUC + LineOffset[old_row],
2630 (size_t)len * sizeof(u8char_T));
Bram Moolenaare76062c2022-11-28 18:51:43 +00002631 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002632 mch_memmove(new_ScreenLinesC[i]
2633 + new_LineOffset[new_row],
2634 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 (size_t)len * sizeof(u8char_T));
2636 }
2637 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2638 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2639 ScreenLines2 + LineOffset[old_row],
2640 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2642 ScreenAttrs + LineOffset[old_row],
2643 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002644 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2645 ScreenAttrs + LineOffset[old_row],
2646 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648 }
2649 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002650 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002652
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002653#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002654 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2655 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002659 free_screenlines();
2660
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002661 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002664 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002665 ScreenLinesC[i] = new_ScreenLinesC[i];
2666 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002669 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 LineOffset = new_LineOffset;
2671 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002672 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002673#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002674 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002675 popup_mask_next = new_popup_mask_next;
2676 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002677 popup_mask_refresh = TRUE;
2678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002680 // It's important that screen_Rows and screen_Columns reflect the actual
2681 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682#ifdef FEAT_GUI
2683 old_Rows = screen_Rows;
2684#endif
2685 screen_Rows = Rows;
2686 screen_Columns = Columns;
2687
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002688 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002689 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002690 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691#ifdef FEAT_GUI
2692 else if (gui.in_use
2693 && !gui.starting
2694 && ScreenLines != NULL
2695 && old_Rows != Rows)
2696 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002697 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2698
2699 // Adjust the position of the cursor, for when executing an external
2700 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002701 if (msg_row >= Rows) // Rows got smaller
2702 msg_row = Rows - 1; // put cursor at last row
2703 else if (Rows > old_Rows) // Rows got bigger
2704 msg_row += Rows - old_Rows; // put cursor in same place
2705 if (msg_col >= Columns) // Columns got smaller
2706 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002709 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002711#ifdef FEAT_GUI_HAIKU
2712 vim_unlock_screen();
2713#endif
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 entered = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002716 if (RedrawingDisabled > 0)
2717 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002718
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002719 /*
2720 * Do not apply autocommands more than 3 times to avoid an endless loop
2721 * in case applying autocommands always changes Rows or Columns.
2722 */
2723 if (starting == 0 && ++retry_count <= 3)
2724 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002725 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002726 // In rare cases, autocommands may have altered Rows or Columns,
2727 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002728 goto retry;
2729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730}
2731
2732 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002733free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002734{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002735 int i;
2736
Bram Moolenaar33796b32019-06-08 16:01:13 +02002737 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002738 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002739 VIM_CLEAR(ScreenLinesC[i]);
2740 VIM_CLEAR(ScreenLines2);
2741 VIM_CLEAR(ScreenLines);
2742 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002743 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002744 VIM_CLEAR(LineOffset);
2745 VIM_CLEAR(LineWraps);
2746 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002747#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002748 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002749 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002750 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002751#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002752}
2753
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002754/*
2755 * Clear the screen.
2756 * May delay if there is something the user should read.
2757 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002758 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002759 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002760 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01002761 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002762screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
2764 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002765 screenalloc(FALSE); // allocate screen buffers if size changed
2766 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002767}
2768
2769/*
2770 * Do not clear the screen but mark everything for redraw.
2771 */
2772 void
2773redraw_as_cleared(void)
2774{
2775 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776}
2777
Bram Moolenaar838b7462022-09-26 15:19:56 +01002778 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002779screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002782 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
2784 if (starting == NO_SCREEN || ScreenLines == NULL
2785#ifdef FEAT_GUI
2786 || (gui.in_use && gui.starting)
2787#endif
2788 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01002789 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791#ifdef FEAT_GUI
2792 if (!gui.in_use)
2793#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002794 screen_attr = -1; // force setting the Normal colors
2795 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
2797#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002798 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 clip_scroll_selection(9999);
2800#endif
2801
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002802 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 for (i = 0; i < Rows; ++i)
2804 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002805 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 LineWraps[i] = FALSE;
2807 }
2808
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002809 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002811 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002812 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002814 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
2816 else
2817 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002818 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 for (i = 0; i < Rows; ++i)
2820 lineinvalid(LineOffset[i], (int)Columns);
2821 clear_cmdline = TRUE;
2822 }
2823
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002824 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002826 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002828 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002829 if (must_redraw == UPD_CLEAR) // no need to clear again
2830 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002831 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002833#ifdef FEAT_PROP_POPUP
2834 popup_redraw_all(); // redraw all popup windows
2835#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002836 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002838 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 msg_didany = FALSE;
2840 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002841
2842 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843}
2844
2845/*
2846 * Clear one line in ScreenLines.
2847 */
2848 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002849lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
2851 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (enc_utf8)
2853 (void)vim_memset(ScreenLinesUC + off, 0,
2854 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002855 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002856 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857}
2858
2859/*
2860 * Mark one line in ScreenLines invalid by setting the attributes to an
2861 * invalid value.
2862 */
2863 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002864lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865{
2866 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002867 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868}
2869
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02002871 * To be called when characters were sent to the terminal directly, outputting
2872 * test on "screen_lnum".
2873 */
2874 void
2875line_was_clobbered(int screen_lnum)
2876{
2877 lineinvalid(LineOffset[screen_lnum], (int)Columns);
2878}
2879
2880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 * Copy part of a Screenline for vertically split window "wp".
2882 */
2883 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002884linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 unsigned off_to = LineOffset[to] + wp->w_wincol;
2887 unsigned off_from = LineOffset[from] + wp->w_wincol;
2888
2889 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
2890 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 if (enc_utf8)
2892 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002893 int i;
2894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
2896 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002897 for (i = 0; i < p_mco; ++i)
2898 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
2899 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 }
2901 if (enc_dbcs == DBCS_JPNU)
2902 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
2903 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
2905 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002906 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
2907 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910/*
2911 * Return TRUE if clearing with term string "p" would work.
2912 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02002913 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 */
2915 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002916can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 return (*p != NUL && (t_colors <= 1
2919#ifdef FEAT_GUI
2920 || gui.in_use
2921#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002922#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002923 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02002924 || (!p_tgc && cterm_normal_bg_color == 0)
2925#else
2926 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002927#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002928 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002929#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002930 && !(p == T_CE && popup_visible)
2931#endif
2932 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933}
2934
2935/*
2936 * Reset cursor position. Use whenever cursor was moved because of outputting
2937 * something directly to the screen (shell commands) or a terminal control
2938 * code.
2939 */
2940 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002941screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
2943 screen_cur_row = screen_cur_col = 9999;
2944}
2945
2946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 * Move the cursor to position "row","col" in the screen.
2948 * This tries to find the most efficient way to move, minimizing the number of
2949 * characters sent to the terminal.
2950 */
2951 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002952windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002954 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 int i;
2956 int plan;
2957 int cost;
2958 int wouldbe_col;
2959 int noinvcurs;
2960 char_u *bs;
2961 int goto_cost;
2962 int attr;
2963
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002964#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
2965#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967#define PLAN_LE 1
2968#define PLAN_CR 2
2969#define PLAN_NL 3
2970#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002971 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if (ScreenLines == NULL)
2973 return;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002974 if (col == screen_cur_col && row == screen_cur_row)
2975 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002977 // Check for valid position.
2978 if (row < 0) // window without text lines?
2979 row = 0;
2980 if (row >= screen_Rows)
2981 row = screen_Rows - 1;
2982 if (col >= screen_Columns)
2983 col = screen_Columns - 1;
2984
2985 // check if no cursor movement is allowed in highlight mode
2986 if (screen_attr && *T_MS == NUL)
2987 noinvcurs = HIGHL_COST;
2988 else
2989 noinvcurs = 0;
2990 goto_cost = GOTO_COST + noinvcurs;
2991
2992 /*
2993 * Plan how to do the positioning:
2994 * 1. Use CR to move it to column 0, same row.
2995 * 2. Use T_LE to move it a few columns to the left.
2996 * 3. Use NL to move a few lines down, column 0.
2997 * 4. Move a few columns to the right with T_ND or by writing chars.
2998 *
2999 * Don't do this if the cursor went beyond the last column, the cursor
3000 * position is unknown then (some terminals wrap, some don't )
3001 *
3002 * First check if the highlighting attributes allow us to write
3003 * characters to move the cursor to the right.
3004 */
3005 if (row >= screen_cur_row && screen_cur_col < Columns)
3006 {
3007 /*
3008 * If the cursor is in the same row, bigger col, we can use CR
3009 * or T_LE.
3010 */
3011 bs = NULL; // init for GCC
3012 attr = screen_attr;
3013 if (row == screen_cur_row && col < screen_cur_col)
3014 {
3015 // "le" is preferred over "bc", because "bc" is obsolete
3016 if (*T_LE)
3017 bs = T_LE; // "cursor left"
3018 else
3019 bs = T_BC; // "backspace character (old)
3020 if (*bs)
3021 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3022 else
3023 cost = 999;
3024 if (col + 1 < cost) // using CR is less characters
3025 {
3026 plan = PLAN_CR;
3027 wouldbe_col = 0;
3028 cost = 1; // CR is just one character
3029 }
3030 else
3031 {
3032 plan = PLAN_LE;
3033 wouldbe_col = col;
3034 }
3035 if (noinvcurs) // will stop highlighting
3036 {
3037 cost += noinvcurs;
3038 attr = 0;
3039 }
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003043 * If the cursor is above where we want to be, we can use CR LF.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003045 else if (row > screen_cur_row)
3046 {
3047 plan = PLAN_NL;
3048 wouldbe_col = 0;
3049 cost = (row - screen_cur_row) * 2; // CR LF
3050 if (noinvcurs) // will stop highlighting
3051 {
3052 cost += noinvcurs;
3053 attr = 0;
3054 }
3055 }
3056
3057 /*
3058 * If the cursor is in the same row, smaller col, just use write.
3059 */
3060 else
3061 {
3062 plan = PLAN_WRITE;
3063 wouldbe_col = screen_cur_col;
3064 cost = 0;
3065 }
3066
3067 /*
3068 * Check if any characters that need to be written have the
3069 * correct attributes. Also avoid UTF-8 characters.
3070 */
3071 i = col - wouldbe_col;
3072 if (i > 0)
3073 cost += i;
3074 if (cost < goto_cost && i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
3076 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003077 * Check if the attributes are correct without additionally
3078 * stopping highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003080 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3081 while (i && *p++ == attr)
3082 --i;
3083 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 {
3085 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003086 * Try if it works when highlighting is stopped here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003088 if (*--p == 0)
3089 {
3090 cost += noinvcurs;
3091 while (i && *p++ == 0)
3092 --i;
3093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (i != 0)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003095 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003097 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003099 // Don't use an UTF-8 char for positioning, it's slow.
3100 for (i = wouldbe_col; i < col; ++i)
3101 if (ScreenLinesUC[LineOffset[row] + i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003103 cost = 999;
3104 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003106 }
3107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003109 /*
3110 * We can do it without term_windgoto()!
3111 */
3112 if (cost < goto_cost)
3113 {
3114 if (plan == PLAN_LE)
3115 {
3116 if (noinvcurs)
3117 screen_stop_highlight();
3118 while (screen_cur_col > col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003120 out_str(bs);
3121 --screen_cur_col;
3122 }
3123 }
3124 else if (plan == PLAN_CR)
3125 {
3126 if (noinvcurs)
3127 screen_stop_highlight();
3128 out_char('\r');
3129 screen_cur_col = 0;
3130 }
3131 else if (plan == PLAN_NL)
3132 {
3133 if (noinvcurs)
3134 screen_stop_highlight();
3135 while (screen_cur_row < row)
3136 {
3137 out_char('\n');
3138 ++screen_cur_row;
3139 }
3140 screen_cur_col = 0;
3141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003143 i = col - screen_cur_col;
3144 if (i > 0)
3145 {
3146 /*
3147 * Use cursor-right if it's one character only. Avoids
3148 * removing a line of pixels from the last bold char, when
3149 * using the bold trick in the GUI.
3150 */
3151 if (T_ND[0] != NUL && T_ND[1] == NUL)
3152 {
3153 while (i-- > 0)
3154 out_char(*T_ND);
3155 }
3156 else
3157 {
3158 int off;
3159
3160 off = LineOffset[row] + screen_cur_col;
3161 while (i-- > 0)
3162 {
3163 if (ScreenAttrs[off] != screen_attr)
3164 screen_stop_highlight();
3165 out_flush_check();
3166 out_char(ScreenLines[off]);
3167 if (enc_dbcs == DBCS_JPNU
3168 && ScreenLines[off] == 0x8e)
3169 out_char(ScreenLines2[off]);
3170 ++off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172 }
3173 }
3174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003176 else
3177 cost = 999;
3178
3179 if (cost >= goto_cost)
3180 {
3181 if (noinvcurs)
3182 screen_stop_highlight();
3183 if (row == screen_cur_row && (col > screen_cur_col)
3184 && *T_CRI != NUL)
3185 term_cursor_right(col - screen_cur_col);
3186 else
3187 term_windgoto(row, col);
3188 }
3189 screen_cur_row = row;
3190 screen_cur_col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192
3193/*
3194 * Set cursor to its position in the current window.
3195 */
3196 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003197setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003199 setcursor_mayforce(FALSE);
3200}
3201
3202/*
3203 * Set cursor to its position in the current window.
3204 * When "force" is TRUE also when not redrawing.
3205 */
3206 void
3207setcursor_mayforce(int force)
3208{
3209 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
3211 validate_cursor();
3212 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003213 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003215 // With 'rightleft' set and the cursor on a double-wide
3216 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003217 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3218 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003219 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003220 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221#endif
3222 curwin->w_wcol));
3223 }
3224}
3225
3226
3227/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003228 * Insert 'line_count' lines at 'row' in window 'wp'.
3229 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3230 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 * scrolling.
3232 * Returns FAIL if the lines are not inserted, OK for success.
3233 */
3234 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003235win_ins_lines(
3236 win_T *wp,
3237 int row,
3238 int line_count,
3239 int invalid,
3240 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 int did_delete;
3243 int nextrow;
3244 int lastrow;
3245 int retval;
3246
3247 if (invalid)
3248 wp->w_lines_valid = 0;
3249
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003250 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (wp->w_height < 5)
3252 return FAIL;
3253
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003254 // with the popup menu visible this might not work correctly
3255 if (pum_visible())
3256 return FAIL;
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (line_count > wp->w_height - row)
3259 line_count = wp->w_height - row;
3260
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003261 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (retval != MAYBE)
3263 return retval;
3264
3265 /*
3266 * If there is a next window or a status line, we first try to delete the
3267 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003268 * If this fails and there are following windows, don't do anything to
3269 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 */
3271 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 if (wp->w_next != NULL || wp->w_status_height)
3273 {
3274 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003275 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 did_delete = TRUE;
3277 else if (wp->w_next)
3278 return FAIL;
3279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 /*
3281 * if no lines deleted, blank the lines that will end up below the window
3282 */
3283 if (!did_delete)
3284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003287 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 lastrow = nextrow + line_count;
3289 if (lastrow > Rows)
3290 lastrow = Rows;
3291 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003292 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 ' ', ' ', 0);
3294 }
3295
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003296 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 == FAIL)
3298 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003299 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (did_delete)
3301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 win_rest_invalid(W_NEXT(wp));
3304 }
3305 return FAIL;
3306 }
3307
3308 return OK;
3309}
3310
3311/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003312 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3314 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3315 * scrolling
3316 * Return OK for success, FAIL if the lines are not deleted.
3317 */
3318 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003319win_del_lines(
3320 win_T *wp,
3321 int row,
3322 int line_count,
3323 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003324 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003325 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327 int retval;
3328
3329 if (invalid)
3330 wp->w_lines_valid = 0;
3331
3332 if (line_count > wp->w_height - row)
3333 line_count = wp->w_height - row;
3334
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003335 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 if (retval != MAYBE)
3337 return retval;
3338
3339 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003340 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 return FAIL;
3342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 /*
3344 * If there are windows or status lines below, try to put them at the
3345 * correct place. If we can't do that, they have to be redrawn.
3346 */
3347 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3348 {
3349 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003350 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
3352 wp->w_redr_status = TRUE;
3353 win_rest_invalid(wp->w_next);
3354 }
3355 }
3356 /*
3357 * If this is the last window and there is no status line, redraw the
3358 * command line later.
3359 */
3360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 redraw_cmdline = TRUE;
3362 return OK;
3363}
3364
3365/*
3366 * Common code for win_ins_lines() and win_del_lines().
3367 * Returns OK or FAIL when the work has been done.
3368 * Returns MAYBE when not finished yet.
3369 */
3370 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003371win_do_lines(
3372 win_T *wp,
3373 int row,
3374 int line_count,
3375 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003376 int del,
3377 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 int retval;
3380
3381 if (!redrawing() || line_count <= 0)
3382 return FAIL;
3383
Bram Moolenaar33796b32019-06-08 16:01:13 +02003384 // When inserting lines would result in loss of command output, just redraw
3385 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003386 if (no_win_do_lines_ins && !del)
3387 return FAIL;
3388
Bram Moolenaar33796b32019-06-08 16:01:13 +02003389 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003390 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003392 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003393 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return FAIL;
3395 }
3396
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003397#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003398 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003399 if (popup_visible)
3400 return FAIL;
3401#endif
3402
3403 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (row + line_count >= wp->w_height)
3405 {
3406 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003407 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 ' ', ' ', 0);
3409 return OK;
3410 }
3411
3412 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003413 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003415 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003417 if (!no_win_do_lines_ins)
3418 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 /*
3421 * If the terminal can set a scroll region, use that.
3422 * Always do this in a vertically split window. This will redraw from
3423 * ScreenLines[] when t_CV isn't defined. That's faster than using
3424 * win_line().
3425 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003426 * a character in the lower right corner of the scroll region may cause a
3427 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003429 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 scroll_region_set(wp, row);
3433 if (del)
3434 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003435 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 else
3437 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003438 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 scroll_region_reset();
3441 return retval;
3442 }
3443
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003444 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 return MAYBE;
3448}
3449
3450/*
3451 * window 'wp' and everything after it is messed up, mark it for redraw
3452 */
3453 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003454win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003458 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 wp->w_redr_status = TRUE;
3460 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462 redraw_cmdline = TRUE;
3463}
3464
3465/*
3466 * The rest of the routines in this file perform screen manipulations. The
3467 * given operation is performed physically on the screen. The corresponding
3468 * change is also made to the internal screen image. In this way, the editor
3469 * anticipates the effect of editing changes on the appearance of the screen.
3470 * That way, when we call screenupdate a complete redraw isn't usually
3471 * necessary. Another advantage is that we can keep adding code to anticipate
3472 * screen changes, and in the meantime, everything still works.
3473 */
3474
3475/*
3476 * types for inserting or deleting lines
3477 */
3478#define USE_T_CAL 1
3479#define USE_T_CDL 2
3480#define USE_T_AL 3
3481#define USE_T_CE 4
3482#define USE_T_DL 5
3483#define USE_T_SR 6
3484#define USE_NL 7
3485#define USE_T_CD 8
3486#define USE_REDRAW 9
3487
3488/*
3489 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003490 * "end" is the line after the scrolled part. Normally it is Rows.
3491 * When scrolling region used "off" is the offset from the top for the region.
3492 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 *
3494 * return FAIL for failure, OK for success.
3495 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003496 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003497screen_ins_lines(
3498 int off,
3499 int row,
3500 int line_count,
3501 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003502 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003503 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 int i;
3506 int j;
3507 unsigned temp;
3508 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003509 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 int type;
3511 int result_empty;
3512 int can_ce = can_clear(T_CE);
3513
3514 /*
3515 * FAIL if
3516 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 * - the line count is less than one
3518 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003519 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003520 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003521 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003523 if (!screen_valid(TRUE)
3524 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003525 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003526#ifdef FEAT_CLIPBOARD
3527 || (clip_star.state != SELECT_CLEARED
3528 && redrawing_for_callback > 0)
3529#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003530#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003531 || popup_visible
3532#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003533 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 return FAIL;
3535
3536 /*
3537 * There are seven ways to insert lines:
3538 * 0. When in a vertically split window and t_CV isn't set, redraw the
3539 * characters from ScreenLines[].
3540 * 1. Use T_CD (clear to end of display) if it exists and the result of
3541 * the insert is just empty lines
3542 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3543 * present or line_count > 1. It looks better if we do all the inserts
3544 * at once.
3545 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3546 * insert is just empty lines and T_CE is not present or line_count >
3547 * 1.
3548 * 4. Use T_AL (insert line) if it exists.
3549 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3550 * just empty lines.
3551 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3552 * just empty lines.
3553 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3554 * the 'da' flag is not set or we have clear line capability.
3555 * 8. redraw the characters from ScreenLines[].
3556 *
3557 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3558 * the scrollbar for the window. It does have insert line, use that if it
3559 * exists.
3560 */
3561 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003563 {
3564 // Avoid that lines are first cleared here and then redrawn, which
3565 // results in many characters updated twice. This happens with CTRL-F
3566 // in a vertically split window. With line-by-line scrolling
3567 // USE_REDRAW should be faster.
3568 if (line_count > 3)
3569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003571 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003572 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 type = USE_T_CD;
3574 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3575 type = USE_T_CAL;
3576 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3577 type = USE_T_CDL;
3578 else if (*T_AL != NUL)
3579 type = USE_T_AL;
3580 else if (can_ce && result_empty)
3581 type = USE_T_CE;
3582 else if (*T_DL != NUL && result_empty)
3583 type = USE_T_DL;
3584 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3585 type = USE_T_SR;
3586 else
3587 return FAIL;
3588
3589 /*
3590 * For clearing the lines screen_del_lines() is used. This will also take
3591 * care of t_db if necessary.
3592 */
3593 if (type == USE_T_CD || type == USE_T_CDL ||
3594 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003595 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597 /*
3598 * If text is retained below the screen, first clear or delete as many
3599 * lines at the bottom of the window as are about to be inserted so that
3600 * the deleted lines won't later surface during a screen_del_lines.
3601 */
3602 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003603 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003606 // Remove a modeless selection when inserting lines halfway the screen
3607 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003608 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003609 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 else
3611 clip_scroll_selection(-line_count);
3612#endif
3613
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003614#ifdef FEAT_GUI_HAIKU
3615 vim_lock_screen();
3616#endif
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003619 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3620 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003621 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#endif
3623
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003624 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3625 cursor_col = wp->w_wincol;
3626
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003627 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 cursor_row = row;
3629 else
3630 cursor_row = row + off;
3631
3632 /*
3633 * Shift LineOffset[] line_count down to reflect the inserted lines.
3634 * Clear the inserted lines in ScreenLines[].
3635 */
3636 row += off;
3637 end += off;
3638 for (i = 0; i < line_count; ++i)
3639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (wp != NULL && wp->w_width != Columns)
3641 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003642 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 j = end - 1 - i;
3644 while ((j -= line_count) >= row)
3645 linecopy(j + line_count, j, wp);
3646 j += line_count;
3647 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003648 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3649 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 else
3651 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3652 LineWraps[j] = FALSE;
3653 }
3654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
3656 j = end - 1 - i;
3657 temp = LineOffset[j];
3658 while ((j -= line_count) >= row)
3659 {
3660 LineOffset[j + line_count] = LineOffset[j];
3661 LineWraps[j + line_count] = LineWraps[j];
3662 }
3663 LineOffset[j + line_count] = temp;
3664 LineWraps[j + line_count] = FALSE;
3665 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003666 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 else
3668 lineinvalid(temp, (int)Columns);
3669 }
3670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003672#ifdef FEAT_GUI_HAIKU
3673 vim_unlock_screen();
3674#endif
3675
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003677 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003678 if (clear_attr != 0)
3679 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003681 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 if (type == USE_REDRAW)
3683 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003684 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
3686 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003687 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689 else
3690 {
3691 for (i = 0; i < line_count; i++)
3692 {
3693 if (type == USE_T_AL)
3694 {
3695 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003696 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 out_str(T_AL);
3698 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003699 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003701 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 }
3704
3705 /*
3706 * With scroll-reverse and 'da' flag set we need to clear the lines that
3707 * have been scrolled down into the region.
3708 */
3709 if (type == USE_T_SR && *T_DA)
3710 {
3711 for (i = 0; i < line_count; ++i)
3712 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003713 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003715 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 }
3718
3719#ifdef FEAT_GUI
3720 gui_can_update_cursor();
3721 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003722 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723#endif
3724 return OK;
3725}
3726
3727/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003728 * Delete lines on the screen and update ScreenLines[].
3729 * "end" is the line after the scrolled part. Normally it is Rows.
3730 * When scrolling region used "off" is the offset from the top for the region.
3731 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 *
3733 * Return OK for success, FAIL if the lines are not deleted.
3734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003736screen_del_lines(
3737 int off,
3738 int row,
3739 int line_count,
3740 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003741 int force, // even when line_count > p_ttyscroll
3742 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003743 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 int j;
3746 int i;
3747 unsigned temp;
3748 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003749 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003751 int result_empty; // result is empty until end of region
3752 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 int type;
3754
3755 /*
3756 * FAIL if
3757 * - there is no valid screen
3758 * - the screen has to be redrawn completely
3759 * - the line count is less than one
3760 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003761 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003762 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003764 if (!screen_valid(TRUE)
3765 || line_count <= 0
3766 || (!force && line_count > p_ttyscroll)
3767 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003768#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003769 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003770#endif
3771 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return FAIL;
3773
3774 /*
3775 * Check if the rest of the current region will become empty.
3776 */
3777 result_empty = row + line_count >= end;
3778
3779 /*
3780 * We can delete lines only when 'db' flag not set or when 'ce' option
3781 * available.
3782 */
3783 can_delete = (*T_DB == NUL || can_clear(T_CE));
3784
3785 /*
3786 * There are six ways to delete lines:
3787 * 0. When in a vertically split window and t_CV isn't set, redraw the
3788 * characters from ScreenLines[].
3789 * 1. Use T_CD if it exists and the result is empty.
3790 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3791 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3792 * none of the other ways work.
3793 * 4. Use T_CE (erase line) if the result is empty.
3794 * 5. Use T_DL (delete line) if it exists.
3795 * 6. redraw the characters from ScreenLines[].
3796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003798 {
3799 // Avoid that lines are first cleared here and then redrawn, which
3800 // results in many characters updated twice. This happens with CTRL-F
3801 // in a vertically split window. With line-by-line scrolling
3802 // USE_REDRAW should be faster.
3803 if (line_count > 3)
3804 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003806 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003807 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 else if (row == 0 && (
3810#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003811 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3812 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 line_count == 1 ||
3814#endif
3815 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 type = USE_NL;
3817 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3818 type = USE_T_CDL;
3819 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003820 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 type = USE_T_CE;
3822 else if (*T_DL != NUL && can_delete)
3823 type = USE_T_DL;
3824 else if (*T_CDL != NUL && can_delete)
3825 type = USE_T_CDL;
3826 else
3827 return FAIL;
3828
3829#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003830 // Remove a modeless selection when deleting lines halfway the screen or
3831 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003832 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003833 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 else
3835 clip_scroll_selection(line_count);
3836#endif
3837
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003838#ifdef FEAT_GUI_HAIKU
3839 vim_lock_screen();
3840#endif
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003843 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3844 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003845 gui_dont_update_cursor(gui.cursor_row >= row + off
3846 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847#endif
3848
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003849 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3850 cursor_col = wp->w_wincol;
3851
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003852 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
3854 cursor_row = row;
3855 cursor_end = end;
3856 }
3857 else
3858 {
3859 cursor_row = row + off;
3860 cursor_end = end + off;
3861 }
3862
3863 /*
3864 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3865 * Clear the inserted lines in ScreenLines[].
3866 */
3867 row += off;
3868 end += off;
3869 for (i = 0; i < line_count; ++i)
3870 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 if (wp != NULL && wp->w_width != Columns)
3872 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003873 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 j = row + i;
3875 while ((j += line_count) <= end - 1)
3876 linecopy(j - line_count, j, wp);
3877 j -= line_count;
3878 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003879 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3880 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 else
3882 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3883 LineWraps[j] = FALSE;
3884 }
3885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003887 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 j = row + i;
3889 temp = LineOffset[j];
3890 while ((j += line_count) <= end - 1)
3891 {
3892 LineOffset[j - line_count] = LineOffset[j];
3893 LineWraps[j - line_count] = LineWraps[j];
3894 }
3895 LineOffset[j - line_count] = temp;
3896 LineWraps[j - line_count] = FALSE;
3897 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003898 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 else
3900 lineinvalid(temp, (int)Columns);
3901 }
3902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003904#ifdef FEAT_GUI_HAIKU
3905 vim_unlock_screen();
3906#endif
3907
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003908 if (screen_attr != clear_attr)
3909 screen_stop_highlight();
3910 if (clear_attr != 0)
3911 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003913 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (type == USE_REDRAW)
3915 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003916 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003918 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003920 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 else if (type == USE_T_CDL)
3923 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003924 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003926 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928 /*
3929 * Deleting lines at top of the screen or scroll region: Just scroll
3930 * the whole screen (scroll region) up by outputting newlines on the
3931 * last line.
3932 */
3933 else if (type == USE_NL)
3934 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003935 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003937 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
3939 else
3940 {
3941 for (i = line_count; --i >= 0; )
3942 {
3943 if (type == USE_T_DL)
3944 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003945 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003946 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003948 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003950 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003951 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003953 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955 }
3956
3957 /*
3958 * If the 'db' flag is set, we need to clear the lines that have been
3959 * scrolled up at the bottom of the region.
3960 */
3961 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
3962 {
3963 for (i = line_count; i > 0; --i)
3964 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003965 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003966 out_str(T_CE); // erase a line
3967 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
3970
3971#ifdef FEAT_GUI
3972 gui_can_update_cursor();
3973 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003974 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975#endif
3976
3977 return OK;
3978}
3979
3980/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003981 * Return TRUE when postponing displaying the mode message: when not redrawing
3982 * or inside a mapping.
3983 */
3984 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003985skip_showmode(void)
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003986{
3987 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01003988 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003989 if (global_busy
3990 || msg_silent != 0
3991 || !redrawing()
3992 || (char_avail() && !KeyTyped))
3993 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02003994 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003995 return TRUE;
3996 }
3997 return FALSE;
3998}
3999
4000/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004001 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 *
4003 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4004 * If clear_cmdline is FALSE there may be a message there that needs to be
4005 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004006 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 * Return the length of the message (0 if no message).
4008 */
4009 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004010showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
4012 int need_clear;
4013 int length = 0;
4014 int do_mode;
4015 int attr;
4016 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
Bram Moolenaara2a89732022-08-31 14:46:18 +01004019 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004020 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004021 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004022 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004023 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004025 if (skip_showmode())
4026 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
4028 nwr_save = need_wait_return;
4029
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004030 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 check_for_delay(FALSE);
4032
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004033 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 need_clear = clear_cmdline;
4035 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004036 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004038 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 msg_pos_mode();
4040 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004041 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (do_mode)
4043 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004044 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004046 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004047# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004048 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004049# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004050 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004051# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004052 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004053# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004054 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004056 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057# endif
4058#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004059 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004060 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004062 // These messages can get long, avoid a wrap in a narrow
4063 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 length = (Rows - msg_row) * Columns - 3;
4065 if (edit_submode_extra != NULL)
4066 length -= vim_strsize(edit_submode_extra);
4067 if (length > 0)
4068 {
4069 if (edit_submode_pre != NULL)
4070 length -= vim_strsize(edit_submode_pre);
4071 if (length - vim_strsize(edit_submode) > 0)
4072 {
4073 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004074 msg_puts_attr((char *)edit_submode_pre, attr);
4075 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 if (edit_submode_extra != NULL)
4078 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004079 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004081 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 else
4083 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004084 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004091 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004092 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004093 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004094 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
4096#ifdef FEAT_RIGHTLEFT
4097 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004098 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004100 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004102 else if (restart_edit == 'I' || restart_edit == 'i' ||
4103 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004104 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004106 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004108 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109#ifdef FEAT_RIGHTLEFT
4110 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004111 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112#endif
4113#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004114 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
4116# ifdef FEAT_ARABIC
4117 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004118 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 else
4120# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004121 if (get_keymap_str(curwin, (char_u *)" (%s)",
4122 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004123 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004126 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004127 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 if (VIsual_active)
4130 {
4131 char *p;
4132
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004133 // Don't concatenate separate words to avoid translation
4134 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 switch ((VIsual_select ? 4 : 0)
4136 + (VIsual_mode == Ctrl_V) * 2
4137 + (VIsual_mode == 'V'))
4138 {
4139 case 0: p = N_(" VISUAL"); break;
4140 case 1: p = N_(" VISUAL LINE"); break;
4141 case 2: p = N_(" VISUAL BLOCK"); break;
4142 case 4: p = N_(" SELECT"); break;
4143 case 5: p = N_(" SELECT LINE"); break;
4144 default: p = N_(" SELECT BLOCK"); break;
4145 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004146 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004148 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 need_clear = TRUE;
4152 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004153 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004154 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004156 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 need_clear = TRUE;
4158 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004159
4160 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004161 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004163 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 length = msg_col;
4165 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004166 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004169 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004171 else if (redraw_mode)
4172 {
4173 msg_pos_mode();
4174 msg_clr_eos();
4175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004177 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 if (VIsual_active)
4179 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004181 // If the last window has no status line, the ruler is after the mode
4182 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004183 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004184 win_redr_ruler(lastwin, TRUE, FALSE);
Martin Tournoijba43e762022-10-13 22:12:15 +01004185
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004187 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 clear_cmdline = FALSE;
4189
4190 return length;
4191}
4192
4193/*
4194 * Position for a mode message.
4195 */
4196 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004197msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
4199 msg_col = 0;
4200 msg_row = Rows - 1;
4201}
4202
4203/*
4204 * Delete mode message. Used when ESC is typed which is expected to end
4205 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004206 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 */
4208 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004209unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004212 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
4214 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004215 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004217 clearmode();
4218}
4219
4220/*
4221 * Clear the mode message.
4222 */
4223 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004224clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004225{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004226 int save_msg_row = msg_row;
4227 int save_msg_col = msg_col;
4228
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004229 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004230 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004231 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004232 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004233
4234 msg_col = save_msg_col;
4235 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236}
4237
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004238 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004239recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004240{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004241 msg_puts_attr(_("recording"), attr);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004242 if (shortmess(SHM_RECORDING))
4243 return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004244
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004245 char s[4];
4246
4247 sprintf(s, " @%c", reg_recording);
4248 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004249}
4250
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004251/*
4252 * Draw the tab pages line at the top of the Vim window.
4253 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004254 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004255draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004256{
4257 int tabcount = 0;
4258 tabpage_T *tp;
4259 int tabwidth;
4260 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004261 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004262 int attr;
4263 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004264 win_T *cwp;
4265 int wincount;
4266 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004267 int c;
4268 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004269 int attr_sel = HL_ATTR(HLF_TPS);
4270 int attr_nosel = HL_ATTR(HLF_TP);
4271 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004272 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004273 int room;
4274 int use_sep_chars = (t_colors < 8
4275#ifdef FEAT_GUI
4276 && !gui.in_use
4277#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004278#ifdef FEAT_TERMGUICOLORS
4279 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004280#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004281 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004282
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004283 if (ScreenLines == NULL)
4284 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004285 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004286
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004287#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004288 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004289 if (gui_use_tabline())
4290 {
4291 gui_update_tabline();
4292 return;
4293 }
4294#endif
4295
4296 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004297 return;
4298
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004299#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004300 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004301
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004302 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004303 if (*p_tal != NUL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004304 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004305 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004306#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004307 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004308 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004309 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004310
Bram Moolenaar238a5642006-02-21 22:12:05 +00004311 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4312 if (tabwidth < 6)
4313 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004314
Bram Moolenaar238a5642006-02-21 22:12:05 +00004315 attr = attr_nosel;
4316 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004317 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4318 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004319 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004320 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004321
Bram Moolenaar238a5642006-02-21 22:12:05 +00004322 if (tp->tp_topframe == topframe)
4323 attr = attr_sel;
4324 if (use_sep_chars && col > 0)
4325 screen_putchar('|', 0, col++, attr);
4326
4327 if (tp->tp_topframe != topframe)
4328 attr = attr_nosel;
4329
4330 screen_putchar(' ', 0, col++, attr);
4331
4332 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004333 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004334 cwp = curwin;
4335 wp = firstwin;
4336 }
4337 else
4338 {
4339 cwp = tp->tp_curwin;
4340 wp = tp->tp_firstwin;
4341 }
4342
4343 modified = FALSE;
4344 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4345 if (bufIsChanged(wp->w_buffer))
4346 modified = TRUE;
4347 if (modified || wincount > 1)
4348 {
4349 if (wincount > 1)
4350 {
4351 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004352 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004353 if (col + len >= Columns - 3)
4354 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004355 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004356#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004357 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004358#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004359 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004360#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004361 );
4362 col += len;
4363 }
4364 if (modified)
4365 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4366 screen_putchar(' ', 0, col++, attr);
4367 }
4368
4369 room = scol - col + tabwidth - 1;
4370 if (room > 0)
4371 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004372 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004373 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004374 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004375 len = vim_strsize(NameBuff);
4376 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004377 if (has_mbyte)
4378 while (len > room)
4379 {
4380 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004381 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004382 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004383 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004384 {
4385 p += len - room;
4386 len = room;
4387 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004388 if (len > Columns - col - 1)
4389 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004390
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004391 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004392 col += len;
4393 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004394 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004395
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004396 // Store the tab page number in TabPageIdxs[], so that
4397 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004398 ++tabcount;
4399 while (scol < col)
4400 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004401 }
4402
Bram Moolenaar238a5642006-02-21 22:12:05 +00004403 if (use_sep_chars)
4404 c = '_';
4405 else
4406 c = ' ';
4407 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004408
Luuk van Baalba936f62022-12-15 13:15:39 +00004409 // Draw the 'showcmd' information if 'showcmdloc' == "tabline".
4410 if (p_sc && *p_sloc == 't')
4411 {
4412 int width = MIN(10, (int)Columns - col - (tabcount > 1) * 3);
4413
4414 if (width > 0)
4415 screen_puts_len(showcmd_buf, width, 0, (int)Columns
4416 - width - (tabcount > 1) * 2, attr_nosel);
4417 }
4418
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004419 // Put an "X" for closing the current tab if there are several.
Luuk van Baalba936f62022-12-15 13:15:39 +00004420 if (tabcount > 1)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004421 {
4422 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4423 TabPageIdxs[Columns - 1] = -999;
4424 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004425 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004426
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004427 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4428 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004429 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004430}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004431
4432/*
4433 * Get buffer name for "buf" into NameBuff[].
4434 * Takes care of special buffer names and translates special characters.
4435 */
4436 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004437get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004438{
4439 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004440 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004441 else
4442 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4443 trans_characters(NameBuff, MAXPATHL);
4444}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004445
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446/*
4447 * Get the character to use in a status line. Get its attributes in "*attr".
4448 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004449 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004450fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004453
4454#ifdef FEAT_TERMINAL
4455 if (bt_terminal(wp->w_buffer))
4456 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004457 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004458 {
4459 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004460 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004461 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004462 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004463 {
4464 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004465 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004466 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004467 }
4468 else
4469#endif
4470 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004472 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004473 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 else
4476 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004477 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004478 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
Christian Brabandt6a650bf2023-11-08 21:23:29 +01004480 return fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483/*
4484 * Get the character to use in a separator between vertically split windows.
4485 * Get its attributes in "*attr".
4486 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004487 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004488fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004490 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004491 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 return '|';
4493 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004494 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497/*
4498 * Return TRUE if redrawing should currently be done.
4499 */
4500 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004501redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004503#ifdef FEAT_EVAL
4504 if (disable_redraw_for_testing)
4505 return 0;
4506 else
4507#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004508 return ((RedrawingDisabled == 0
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004509#ifdef FEAT_EVAL
4510 || ignore_redraw_flag_for_testing
4511#endif
4512 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513}
4514
4515/*
4516 * Return TRUE if printing messages should currently be done.
4517 */
4518 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004519messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004521 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522}
4523
Bram Moolenaare677df82019-09-02 22:31:11 +02004524/*
4525 * Compute columns for ruler and shown command. 'sc_col' is also used to
4526 * decide what the maximum length of a message on the status line can be.
4527 * If there is a status line for the last window, 'sc_col' is independent
4528 * of 'ru_col'.
4529 */
4530
4531#define COL_RULER 17 // columns needed by standard ruler
4532
4533 void
4534comp_col(void)
4535{
Sean Dewar876f5fb2023-08-17 22:40:05 +02004536 int last_has_status = last_stl_height(FALSE) > 0;
Bram Moolenaare677df82019-09-02 22:31:11 +02004537
4538 sc_col = 0;
4539 ru_col = 0;
4540 if (p_ru)
4541 {
Martin Tournoijba43e762022-10-13 22:12:15 +01004542#ifdef FEAT_STL_OPT
Bram Moolenaare677df82019-09-02 22:31:11 +02004543 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004544#else
Bram Moolenaare677df82019-09-02 22:31:11 +02004545 ru_col = COL_RULER + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004546#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004547 // no last status line, adjust sc_col
4548 if (!last_has_status)
4549 sc_col = ru_col;
4550 }
Sam-programs062141b2024-02-29 17:40:29 +01004551 if (p_sc && *p_sloc == 'l')
Bram Moolenaare677df82019-09-02 22:31:11 +02004552 {
4553 sc_col += SHOWCMD_COLS;
4554 if (!p_ru || last_has_status) // no need for separating space
4555 ++sc_col;
4556 }
4557 sc_col = Columns - sc_col;
4558 ru_col = Columns - ru_col;
4559 if (sc_col <= 0) // screen too narrow, will become a mess
4560 sc_col = 1;
4561 if (ru_col <= 0)
4562 ru_col = 1;
Bram Moolenaare677df82019-09-02 22:31:11 +02004563#ifdef FEAT_EVAL
4564 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4565#endif
4566}
4567
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004568#if defined(FEAT_LINEBREAK) || defined(PROTO)
4569/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004570 * Return the width of the 'number' and 'relativenumber' column.
4571 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004572 * Otherwise it depends on 'numberwidth' and the line count.
4573 */
4574 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004575number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004576{
4577 int n;
4578 linenr_T lnum;
4579
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004580 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004581 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004582 lnum = wp->w_height;
4583 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004584 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004585 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004586
Bram Moolenaar6b314672015-03-20 15:42:10 +01004587 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004588 return wp->w_nrwidth_width;
4589 wp->w_nrwidth_line_count = lnum;
4590
4591 n = 0;
4592 do
4593 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004594 lnum /= 10;
4595 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004596 } while (lnum > 0);
4597
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004598 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004599 if (n < wp->w_p_nuw - 1)
4600 n = wp->w_p_nuw - 1;
4601
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004602# ifdef FEAT_SIGNS
4603 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4604 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004605 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004606 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4607 n = 2;
4608# endif
4609
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004610 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004611 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004612 return n;
4613}
4614#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004615
Bram Moolenaar113e1072019-01-20 15:30:40 +01004616#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004617/*
4618 * Return the current cursor column. This is the actual position on the
4619 * screen. First column is 0.
4620 */
4621 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004622screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004623{
4624 return screen_cur_col;
4625}
4626
4627/*
4628 * Return the current cursor row. This is the actual position on the screen.
4629 * First row is 0.
4630 */
4631 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004632screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004633{
4634 return screen_cur_row;
4635}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004636#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004637
4638/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004639 * Calls mb_ptr2char_adv(p) and returns the character.
4640 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4641 */
4642 static int
4643get_encoded_char_adv(char_u **p)
4644{
4645 char_u *s = *p;
4646
4647 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4648 {
4649 varnumber_T num = 0;
4650 int bytes;
4651 int n;
4652
4653 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4654 {
4655 *p += 2;
4656 n = hexhex2nr(*p);
4657 if (n < 0)
4658 return 0;
4659 num = num * 256 + n;
4660 }
4661 *p += 2;
4662 return num;
4663 }
4664 return mb_ptr2char_adv(p);
4665}
4666
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004667struct charstab
4668{
4669 int *cp;
4670 char *name;
4671};
4672static fill_chars_T fill_chars;
4673static struct charstab filltab[] =
4674{
4675 {&fill_chars.stl, "stl"},
4676 {&fill_chars.stlnc, "stlnc"},
4677 {&fill_chars.vert, "vert"},
4678 {&fill_chars.fold, "fold"},
4679 {&fill_chars.foldopen, "foldopen"},
4680 {&fill_chars.foldclosed, "foldclose"},
4681 {&fill_chars.foldsep, "foldsep"},
4682 {&fill_chars.diff, "diff"},
4683 {&fill_chars.eob, "eob"},
4684 {&fill_chars.lastline, "lastline"},
4685};
4686static lcs_chars_T lcs_chars;
4687static struct charstab lcstab[] =
4688{
4689 {&lcs_chars.eol, "eol"},
4690 {&lcs_chars.ext, "extends"},
4691 {&lcs_chars.nbsp, "nbsp"},
4692 {&lcs_chars.prec, "precedes"},
4693 {&lcs_chars.space, "space"},
4694 {&lcs_chars.tab2, "tab"},
4695 {&lcs_chars.trail, "trail"},
4696 {&lcs_chars.lead, "lead"},
4697#ifdef FEAT_CONCEAL
4698 {&lcs_chars.conceal, "conceal"},
4699#else
4700 {NULL, "conceal"},
4701#endif
zeertzjq1f025b02023-09-30 12:43:07 +02004702 {NULL, "multispace"},
4703 {NULL, "leadmultispace"},
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004704};
4705
zeertzjq6a8d2e12024-01-17 20:54:49 +01004706 static char *
4707field_value_err(char *errbuf, size_t errbuflen, char *fmt, char *field)
4708{
4709 if (errbuf == NULL)
4710 return "";
4711 vim_snprintf(errbuf, errbuflen, _(fmt), field);
4712 return errbuf;
4713}
4714
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004715/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004716 * Handle setting 'listchars' or 'fillchars'.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004717 * "value" points to either the global or the window-local value.
zeertzjqd619d6a2023-05-08 15:56:21 +01004718 * "is_listchars" is TRUE for "listchars" and FALSE for "fillchars".
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004719 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004720 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004721 * Returns error message, NULL if it's OK.
4722 */
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004723 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004724set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
4725 char *errbuf, size_t errbuflen)
Bram Moolenaare677df82019-09-02 22:31:11 +02004726{
zeertzjq1f025b02023-09-30 12:43:07 +02004727 int round, i, len, entries;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004728 char_u *p, *s;
4729 int c1 = 0, c2 = 0, c3 = 0;
4730 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4731 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4732 int multispace_len = 0; // Length of lcs-multispace string
4733 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004734
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004735 struct charstab *tab;
4736
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004737 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004738 {
4739 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004740 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004741 entries = ARRAY_LENGTH(lcstab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004742 if (wp->w_p_lcs[0] == NUL)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004743 value = p_lcs; // local value is empty, use the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004744 }
4745 else
4746 {
4747 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004748 entries = ARRAY_LENGTH(filltab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004749 if (wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004750 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004751 }
4752
4753 // first round: check for valid value, second round: assign values
zeertzjq00624a22023-11-23 20:47:16 +01004754 for (round = 0; round <= (apply ? 1 : 0); ++round)
Bram Moolenaare677df82019-09-02 22:31:11 +02004755 {
4756 if (round > 0)
4757 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004758 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004759 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004760 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004761 for (i = 0; i < entries; ++i)
4762 if (tab[i].cp != NULL)
4763 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004764 lcs_chars.tab1 = NUL;
4765 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004766
Mike Williamsf5785cf2021-09-13 22:17:38 +02004767 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004768 {
4769 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004770 if (lcs_chars.multispace != NULL)
4771 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004772 }
4773 else
4774 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004775
4776 if (lead_multispace_len > 0)
4777 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004778 lcs_chars.leadmultispace =
4779 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004780 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
4781 }
4782 else
4783 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004784 }
4785 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004786 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004787 fill_chars.stl = ' ';
4788 fill_chars.stlnc = ' ';
4789 fill_chars.vert = ' ';
4790 fill_chars.fold = '-';
4791 fill_chars.foldopen = '-';
4792 fill_chars.foldclosed = '+';
4793 fill_chars.foldsep = '|';
4794 fill_chars.diff = '-';
4795 fill_chars.eob = '~';
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01004796 fill_chars.lastline = '@';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004797 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004798 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004799 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02004800 while (*p)
4801 {
4802 for (i = 0; i < entries; ++i)
4803 {
4804 len = (int)STRLEN(tab[i].name);
zeertzjq6a8d2e12024-01-17 20:54:49 +01004805 if (!(STRNCMP(p, tab[i].name, len) == 0 && p[len] == ':'))
zeertzjq1f025b02023-09-30 12:43:07 +02004806 continue;
Bram Moolenaare677df82019-09-02 22:31:11 +02004807
zeertzjq1f025b02023-09-30 12:43:07 +02004808 if (is_listchars && strcmp(tab[i].name, "multispace") == 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004809 {
4810 s = p + len + 1;
4811 if (round == 0)
4812 {
4813 // Get length of lcs-multispace string in first round
4814 last_multispace = p;
4815 multispace_len = 0;
4816 while (*s != NUL && *s != ',')
4817 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004818 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004819 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004820 return field_value_err(errbuf, errbuflen,
4821 e_wrong_character_width_for_field_str,
4822 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004823 ++multispace_len;
4824 }
4825 if (multispace_len == 0)
4826 // lcs-multispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004827 return field_value_err(errbuf, errbuflen,
4828 e_wrong_number_of_characters_for_field_str,
4829 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004830 p = s;
4831 }
4832 else
4833 {
4834 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02004835
zeertzjqf14b8ba2021-09-10 16:58:30 +02004836 while (*s != NUL && *s != ',')
4837 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004838 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004839 if (p == last_multispace)
4840 lcs_chars.multispace[multispace_pos++] = c1;
4841 }
4842 p = s;
4843 }
zeertzjq1f025b02023-09-30 12:43:07 +02004844 break;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004845 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004846
zeertzjq1f025b02023-09-30 12:43:07 +02004847 if (is_listchars && strcmp(tab[i].name, "leadmultispace") == 0)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004848 {
zeertzjq1f025b02023-09-30 12:43:07 +02004849 s = p + len + 1;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004850 if (round == 0)
4851 {
zeertzjqb5f08012022-06-09 13:55:28 +01004852 // get length of lcs-leadmultispace string in first
4853 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004854 last_lmultispace = p;
4855 lead_multispace_len = 0;
4856 while (*s != NUL && *s != ',')
4857 {
4858 c1 = get_encoded_char_adv(&s);
4859 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004860 return field_value_err(errbuf, errbuflen,
4861 e_wrong_character_width_for_field_str,
4862 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004863 ++lead_multispace_len;
4864 }
4865 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01004866 // lcs-leadmultispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004867 return field_value_err(errbuf, errbuflen,
4868 e_wrong_number_of_characters_for_field_str,
4869 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004870 p = s;
4871 }
4872 else
4873 {
4874 int multispace_pos = 0;
4875
4876 while (*s != NUL && *s != ',')
4877 {
4878 c1 = get_encoded_char_adv(&s);
4879 if (p == last_lmultispace)
4880 lcs_chars.leadmultispace[multispace_pos++] = c1;
4881 }
4882 p = s;
4883 }
zeertzjq1f025b02023-09-30 12:43:07 +02004884 break;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004885 }
zeertzjq1f025b02023-09-30 12:43:07 +02004886
4887 c2 = c3 = 0;
4888 s = p + len + 1;
zeertzjq6a8d2e12024-01-17 20:54:49 +01004889 if (*s == NUL)
4890 return field_value_err(errbuf, errbuflen,
4891 e_wrong_number_of_characters_for_field_str,
4892 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004893 c1 = get_encoded_char_adv(&s);
4894 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004895 return field_value_err(errbuf, errbuflen,
4896 e_wrong_character_width_for_field_str,
4897 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004898 if (tab[i].cp == &lcs_chars.tab2)
4899 {
4900 if (*s == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004901 return field_value_err(errbuf, errbuflen,
4902 e_wrong_number_of_characters_for_field_str,
4903 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004904 c2 = get_encoded_char_adv(&s);
4905 if (char2cells(c2) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004906 return field_value_err(errbuf, errbuflen,
4907 e_wrong_character_width_for_field_str,
4908 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004909 if (!(*s == ',' || *s == NUL))
4910 {
4911 c3 = get_encoded_char_adv(&s);
4912 if (char2cells(c3) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004913 return field_value_err(errbuf, errbuflen,
4914 e_wrong_character_width_for_field_str,
4915 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004916 }
4917 }
4918
4919 if (*s == ',' || *s == NUL)
4920 {
4921 if (round > 0)
4922 {
4923 if (tab[i].cp == &lcs_chars.tab2)
4924 {
4925 lcs_chars.tab1 = c1;
4926 lcs_chars.tab2 = c2;
4927 lcs_chars.tab3 = c3;
4928 }
4929 else if (tab[i].cp != NULL)
4930 *(tab[i].cp) = c1;
4931
4932 }
4933 p = s;
4934 break;
4935 }
zeertzjq6a8d2e12024-01-17 20:54:49 +01004936 else
4937 return field_value_err(errbuf, errbuflen,
4938 e_wrong_number_of_characters_for_field_str,
4939 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004940 }
4941
zeertzjq1f025b02023-09-30 12:43:07 +02004942 if (i == entries)
4943 return e_invalid_argument;
4944
Bram Moolenaare677df82019-09-02 22:31:11 +02004945 if (*p == ',')
4946 ++p;
4947 }
4948 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004949
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004950 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004951 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004952 if (is_listchars)
4953 {
4954 vim_free(wp->w_lcs_chars.multispace);
4955 vim_free(wp->w_lcs_chars.leadmultispace);
4956 wp->w_lcs_chars = lcs_chars;
4957 }
4958 else
4959 {
4960 wp->w_fill_chars = fill_chars;
4961 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02004962 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004963
4964 return NULL; // no error
4965}
zeertzjq8ca29b62022-08-09 12:53:14 +01004966
4967/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004968 * Handle the new value of 'fillchars'.
4969 */
4970 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004971set_fillchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4972 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004973{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004974 return set_chars_option(wp, val, FALSE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004975}
4976
4977/*
4978 * Handle the new value of 'listchars'.
4979 */
4980 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004981set_listchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4982 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004983{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004984 return set_chars_option(wp, val, TRUE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004985}
4986
4987/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004988 * Function given to ExpandGeneric() to obtain possible arguments of the
4989 * 'fillchars' option.
4990 */
4991 char_u *
4992get_fillchars_name(expand_T *xp UNUSED, int idx)
4993{
4994 if (idx >= (int)(sizeof(filltab) / sizeof(filltab[0])))
4995 return NULL;
4996
4997 return (char_u*)filltab[idx].name;
4998}
4999
5000/*
5001 * Function given to ExpandGeneric() to obtain possible arguments of the
5002 * 'listchars' option.
5003 */
5004 char_u *
5005get_listchars_name(expand_T *xp UNUSED, int idx)
5006{
5007 if (idx >= (int)(sizeof(lcstab) / sizeof(lcstab[0])))
5008 return NULL;
5009
5010 return (char_u*)lcstab[idx].name;
5011}
5012
5013/*
zeertzjq8ca29b62022-08-09 12:53:14 +01005014 * Check all global and local values of 'listchars' and 'fillchars'.
5015 * Return an untranslated error messages if any of them is invalid, NULL
5016 * otherwise.
5017 */
5018 char *
5019check_chars_options(void)
5020{
5021 tabpage_T *tp;
5022 win_T *wp;
5023
zeertzjq6a8d2e12024-01-17 20:54:49 +01005024 if (set_listchars_option(curwin, p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005025 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005026 if (set_fillchars_option(curwin, p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005027 return e_conflicts_with_value_of_fillchars;
5028 FOR_ALL_TAB_WINDOWS(tp, wp)
5029 {
zeertzjq6a8d2e12024-01-17 20:54:49 +01005030 if (set_listchars_option(wp, wp->w_p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005031 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005032 if (set_fillchars_option(wp, wp->w_p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005033 return e_conflicts_with_value_of_fillchars;
5034 }
5035 return NULL;
5036}