blob: 08b1c018f219d2610b80e4981b6678e8cfb0039c [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
zeertzjqdb54e982023-09-21 16:33:09 +020021 * available or before buffer text, MAXCOL means after the
22 * end of the line.
Bram Moolenaarb9081882022-07-09 04:56:24 +010023 *
24 * LineOffset[row] Contains the offset into ScreenLines*[], ScreenAttrs[]
25 * and ScreenCols[] for each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * LineWraps[row] Flag for each line whether it wraps to the next line.
27 *
28 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
29 * one character which occupies two display cells.
30 * For UTF-8 a multi-byte character is converted to Unicode and stored in
31 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * character without composing chars ScreenLinesUC[] will be 0 and
33 * ScreenLinesC[][] is not used. When the character occupies two display
34 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000035 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010036 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000037 * ScreenLines2[] is only used for euc-jp to store the second byte if the
38 * first byte is 0x8e (single-width character).
39 *
40 * The screen_*() functions write to the screen and handle updating
41 * ScreenLines[].
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 */
43
44#include "vim.h"
45
46/*
47 * The attributes that are actually active for writing to the screen.
48 */
49static int screen_attr = 0;
50
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010051static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar838b7462022-09-26 15:19:56 +010052static int screenclear2(int doclear);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020053static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020055static 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 +010056static void win_rest_invalid(win_T *wp);
57static void msg_pos_mode(void);
58static void recording_mode(int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaar63d9e732019-12-05 21:10:38 +010060// Ugly global: overrule attribute used by screen_char()
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaar860cae12010-06-05 23:22:07 +020063#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020064/*
65 * Return TRUE if the cursor line in window "wp" may be concealed, according
66 * to the 'concealcursor' option.
67 */
68 int
Bram Moolenaar05540972016-01-30 20:31:25 +010069conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020070{
71 int c;
72
73 if (*wp->w_p_cocu == NUL)
74 return FALSE;
Bram Moolenaar24959102022-05-07 20:01:16 +010075 if (get_real_state() & MODE_VISUAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020076 c = 'v';
Bram Moolenaar24959102022-05-07 20:01:16 +010077 else if (State & MODE_INSERT)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020078 c = 'i';
Bram Moolenaar24959102022-05-07 20:01:16 +010079 else if (State & MODE_NORMAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020080 c = 'n';
Bram Moolenaar24959102022-05-07 20:01:16 +010081 else if (State & MODE_CMDLINE)
Bram Moolenaarca8c9862010-07-24 15:00:38 +020082 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +020083 else
84 return FALSE;
85 return vim_strchr(wp->w_p_cocu, c) != NULL;
86}
87
88/*
89 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
Bram Moolenaarea042672021-06-29 20:22:32 +020090 * To be called after changing the state, "was_concealed" is the value of
91 * "conceal_cursor_line()" before the change.
92 * "
Bram Moolenaarf5963f72010-07-23 22:10:27 +020093 */
94 void
Bram Moolenaarea042672021-06-29 20:22:32 +020095conceal_check_cursor_line(int was_concealed)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020096{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +000097 if (curwin->w_p_cole <= 0 || conceal_cursor_line(curwin) == was_concealed)
98 return;
Bram Moolenaarea042672021-06-29 20:22:32 +020099
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000100 int wcol = curwin->w_wcol;
Bram Moolenaarea042672021-06-29 20:22:32 +0200101
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000102 need_cursor_line_redraw = TRUE;
103 // Need to recompute cursor column, e.g., when starting Visual mode
104 // without concealing.
105 curs_columns(TRUE);
106
107 // When concealing now w_wcol will be computed wrong, keep the previous
108 // value, it will be updated in win_line().
109 if (!was_concealed)
110 curwin->w_wcol = wcol;
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200111}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#endif
113
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200114/*
115 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
116 * window then get the "Pmenu" highlight attribute.
117 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200118 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200119get_wcr_attr(win_T *wp)
120{
121 int wcr_attr = 0;
122
123 if (*wp->w_p_wcr != NUL)
124 wcr_attr = syn_name2attr(wp->w_p_wcr);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100125#ifdef FEAT_PROP_POPUP
Bram Moolenaarc363fe12019-08-04 18:13:46 +0200126 else if (WIN_IS_POPUP(wp))
Bram Moolenaar62a0cb42019-08-18 16:35:23 +0200127 {
128 if (wp->w_popup_flags & POPF_INFO)
129 wcr_attr = HL_ATTR(HLF_PSI); // PmenuSel
130 else
131 wcr_attr = HL_ATTR(HLF_PNI); // Pmenu
132 }
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200133#endif
134 return wcr_attr;
135}
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100138 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
139 * Return the new offset.
140 */
141 static int
142screen_fill_end(
143 win_T *wp,
144 int c1,
145 int c2,
146 int off,
147 int width,
148 int row,
149 int endrow,
150 int attr)
151{
152 int nn = off + width;
153
154 if (nn > wp->w_width)
155 nn = wp->w_width;
156#ifdef FEAT_RIGHTLEFT
157 if (wp->w_p_rl)
158 {
159 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
160 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
161 c1, c2, attr);
162 }
163 else
164#endif
165 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
166 wp->w_wincol + off, (int)wp->w_wincol + nn,
167 c1, c2, attr);
168 return nn;
169}
170
171/*
172 * Clear lines near the end the window and mark the unused lines with "c1".
173 * use "c2" as the filler character.
174 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200176 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100177win_draw_end(
178 win_T *wp,
179 int c1,
180 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100181 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +0100182 int row,
183 int endrow,
184 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200187 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200188 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200189
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200190 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100191
192 if (draw_margin)
193 {
Bram Moolenaar1c934292015-01-27 16:39:29 +0100194#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100195 int fdc = compute_foldcolumn(wp, 0);
196
197 if (fdc > 0)
198 // draw the fold column
199 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200200 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +0100201#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100202#ifdef FEAT_SIGNS
203 if (signcolumn_on(wp))
204 // draw the sign column
205 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200206 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100207#endif
208 if ((wp->w_p_nu || wp->w_p_rnu)
209 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
210 // draw the number column
211 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200212 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215#ifdef FEAT_RIGHTLEFT
216 if (wp->w_p_rl)
217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100219 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200220 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100222 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200223 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 }
225 else
226#endif
227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100229 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +0200230 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 set_empty_rows(wp, row);
234}
235
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200236#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237/*
Bram Moolenaar1c934292015-01-27 16:39:29 +0100238 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
239 * space is available for window "wp", minus "col".
240 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200241 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100242compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +0100243{
244 int fdc = wp->w_p_fdc;
245 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +0200246 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100247
248 if (fdc > wwidth - (col + wmw))
249 fdc = wwidth - (col + wmw);
250 return fdc;
251}
252
253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +0000255 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100256 * Returns the number of bytes stored in 'p'. When non-multibyte characters are
257 * used for the fold column markers, this is equal to 'fdc' setting. Otherwise,
258 * this will be greater than 'fdc'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 */
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100260 size_t
Bram Moolenaar05540972016-01-30 20:31:25 +0100261fill_foldcolumn(
262 char_u *p,
263 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100264 int closed, // TRUE of FALSE
265 linenr_T lnum) // current line number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
267 int i = 0;
268 int level;
269 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000270 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +0100271 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100272 size_t byte_counter = 0;
273 int symbol = 0;
274 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100276 // Init to all spaces.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100277 vim_memset(p, ' ', MAX_MCO * fdc + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
279 level = win_foldinfo.fi_level;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100280 empty = (fdc == 1) ? 0 : 1;
281
282 // If the column is too narrow, we start at the lowest level that
Bram Moolenaar008bff92021-03-04 21:55:58 +0100283 // fits and use numbers to indicate the depth.
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100284 first_level = level - fdc - closed + 1 + empty;
285 if (first_level < 1)
286 first_level = 1;
287
288 for (i = 0; i < MIN(fdc, level); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100290 if (win_foldinfo.fi_lnum == lnum
291 && first_level + i >= win_foldinfo.fi_low_level)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100292 symbol = wp->w_fill_chars.foldopen;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100293 else if (first_level == 1)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100294 symbol = wp->w_fill_chars.foldsep;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100295 else if (first_level + i <= 9)
296 symbol = '0' + first_level + i;
297 else
298 symbol = '>';
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000299
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100300 len = utf_char2bytes(symbol, &p[byte_counter]);
301 byte_counter += len;
302 if (first_level + i >= level)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 {
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100304 i++;
305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307 }
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100308
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 if (closed)
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100310 {
311 if (symbol != 0)
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100312 {
313 // rollback length and the character
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100314 byte_counter -= len;
Bram Moolenaar196a1f72021-03-21 14:39:19 +0100315 if (len > 1)
316 // for a multibyte character, erase all the bytes
317 vim_memset(p + byte_counter, ' ', len);
318 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100319 symbol = wp->w_fill_chars.foldclosed;
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100320 len = utf_char2bytes(symbol, &p[byte_counter]);
321 byte_counter += len;
322 }
323
324 return MAX(byte_counter + (fdc - i), (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325}
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100326#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000328/*
329 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +0100330 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000331 */
332 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100333comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000334{
335 int i;
336
337 for (i = 0; i < Screen_mco; ++i)
338 {
339 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
340 return TRUE;
341 if (ScreenLinesC[i][off_from] == 0)
342 break;
343 }
344 return FALSE;
345}
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Check whether the given character needs redrawing:
349 * - the (first byte of the) character is different
350 * - the attributes are different
351 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000352 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 */
354 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100355char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356{
357 if (cols > 0
358 && ((ScreenLines[off_from] != ScreenLines[off_to]
359 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 || (enc_dbcs != 0
361 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
362 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
363 ? ScreenLines2[off_from] != ScreenLines2[off_to]
364 : (cols > 1 && ScreenLines[off_from + 1]
365 != ScreenLines[off_to + 1])))
366 || (enc_utf8
367 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
368 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +0000369 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +0200370 || ((*mb_off2cells)(off_from, off_from + cols) > 1
371 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +0100372 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 return TRUE;
374 return FALSE;
375}
376
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200377#if defined(FEAT_TERMINAL) || defined(PROTO)
378/*
379 * Return the index in ScreenLines[] for the current screen line.
380 */
381 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000382screen_get_current_line_off(void)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200383{
384 return (int)(current_ScreenLine - ScreenLines);
385}
386#endif
387
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100388#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200389/*
390 * Return TRUE if this position has a higher level popup or this cell is
391 * transparent in the current popup.
392 */
393 static int
394blocked_by_popup(int row, int col)
395{
396 int off;
397
398 if (!popup_visible)
399 return FALSE;
400 off = row * screen_Columns + col;
401 return popup_mask[off] > screen_zindex || popup_transparent[off];
402}
403#endif
404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200406 * Reset the highlighting. Used before clearing the screen.
407 */
408 void
409reset_screen_attr(void)
410{
411#ifdef FEAT_GUI
412 if (gui.in_use)
413 // Use a code that will reset gui.highlight_mask in
414 // gui_stop_highlight().
415 screen_attr = HL_ALL + 1;
416 else
417#endif
418 // Use attributes that is very unlikely to appear in text.
419 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
420}
421
422/*
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100423 * Return TRUE if the character at "row" / "col" is under the popup menu and it
424 * will be redrawn soon or it is under another popup.
425 */
426 static int
427skip_for_popup(int row, int col)
428{
429 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
430 if (pum_under_menu(row, col, TRUE)
431#ifdef FEAT_PROP_POPUP
432 && screen_zindex <= POPUPMENU_ZINDEX
433#endif
434 )
435 return TRUE;
436#ifdef FEAT_PROP_POPUP
437 if (blocked_by_popup(row, col))
438 return TRUE;
439#endif
440 return FALSE;
441}
442
443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 * Move one "cooked" screen line to the screen, but only the characters that
445 * have actually changed. Handle insert/delete character.
446 * "coloff" gives the first column on the screen for this line.
447 * "endcol" gives the columns where valid characters are.
448 * "clear_width" is the width of the window. It's > 0 if the rest of the line
449 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200450 * "flags" can have bits:
451 * SLF_POPUP popup window
452 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
454 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
zeertzjqd0c1b772024-03-16 15:03:33 +0100455 * SLF_INC_VCOL:
456 * When FALSE, use "last_vcol" for ScreenCols[] of the columns to clear.
457 * When TRUE, use an increasing sequence starting from "last_vcol + 1" for
458 * ScreenCols[] of the columns to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200460 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100461screen_line(
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100462 win_T *wp,
463 int row,
464 int coloff,
465 int endcol,
466 int clear_width,
zeertzjqd0c1b772024-03-16 15:03:33 +0100467 colnr_T last_vcol,
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100468 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469{
470 unsigned off_from;
471 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000472 unsigned max_off_from;
473 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100476 int force = FALSE; // force update rest of the line
477 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100479 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480#endif
481 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100482 int redraw_next; // redraw_this for next character
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100483#ifdef FEAT_GUI_MSWIN
484 int changed_this; // TRUE if character changed
485 int changed_next; // TRUE if next character changed
486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100488 int char_cells; // 1: normal char
489 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100491 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100492 if (row >= Rows)
493 row = Rows - 1;
494 if (endcol > Columns)
495 endcol = Columns;
496
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497# ifdef FEAT_CLIPBOARD
498 clip_may_clear_selection(row, row);
499# endif
500
501 off_from = (unsigned)(current_ScreenLine - ScreenLines);
502 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000503 max_off_from = off_from + screen_Columns;
504 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
506#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200507 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100509 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 if (clear_width > 0)
511 {
512 while (col <= endcol && ScreenLines[off_to] == ' '
513 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100514 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
516 ++off_to;
517 ++col;
518 }
519 if (col <= endcol)
520 screen_fill(row, row + 1, col + coloff,
521 endcol + coloff + 1, ' ', ' ', 0);
522 }
523 col = endcol + 1;
524 off_to = LineOffset[row] + col + coloff;
525 off_from += col;
526 endcol = (clear_width > 0 ? clear_width : -clear_width);
527 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100528#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100530#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100531 // First char of a popup window may go on top of the right half of a
532 // double-wide character. Clear the left half to avoid it getting the popup
533 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100534 if (coloff > 0 && enc_utf8
535 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200536 && ScreenLinesUC[off_to - 1] != 0
537 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100538 {
539 ScreenLines[off_to - 1] = ' ';
540 ScreenLinesUC[off_to - 1] = 0;
541 screen_char(off_to - 1, row, col + coloff - 1);
542 }
543#endif
544
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100546#ifdef FEAT_GUI_MSWIN
547 changed_next = redraw_next;
548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550 while (col < endcol)
551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000553 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 else
555 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 redraw_this = redraw_next;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100558 redraw_next = force || char_needs_redraw(off_from + char_cells,
559 off_to + char_cells, endcol - col - char_cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561#ifdef FEAT_GUI
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100562# ifdef FEAT_GUI_MSWIN
563 changed_this = changed_next;
564 changed_next = redraw_next;
565# endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100566 // If the next character was bold, then redraw the current character to
567 // remove any pixels that might have spilt over into us. This only
568 // happens in the GUI.
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100569 // With MS-Windows antialiasing may also cause pixels to spill over
570 // from a previous character, no matter attributes, always redraw if a
571 // character changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (redraw_next && gui.in_use)
573 {
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100574# ifndef FEAT_GUI_MSWIN
Bram Moolenaarb9081882022-07-09 04:56:24 +0100575 hl = ScreenAttrs[off_to + char_cells];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000576 if (hl > HL_ALL)
577 hl = syn_attr2attr(hl);
578 if (hl & HL_BOLD)
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 redraw_this = TRUE;
581 }
582#endif
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100583 // Do not redraw if under the popup menu.
584 if (redraw_this && skip_for_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200585 redraw_this = FALSE;
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (redraw_this)
588 {
589 /*
590 * Special handling when 'xs' termcap flag set (hpterm):
591 * Attributes for characters are stored at the position where the
592 * cursor is when writing the highlighting code. The
593 * start-highlighting code must be written with the cursor on the
594 * first highlighted character. The stop-highlighting code must
595 * be written with the cursor just after the last highlighted
596 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100597 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * to clear the rest of the line, and force redrawing it
599 * completely.
600 */
601 if ( p_wiv
602 && !force
603#ifdef FEAT_GUI
604 && !gui.in_use
605#endif
606 && ScreenAttrs[off_to] != 0
607 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
608 {
609 /*
610 * Need to remove highlighting attributes here.
611 */
612 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100613 out_str(T_CE); // clear rest of this screen line
614 screen_start(); // don't know where cursor is now
615 force = TRUE; // force redraw of rest of the line
616 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
618 /*
619 * If the previous character was highlighted, need to stop
620 * highlighting at this character.
621 */
622 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
623 {
624 screen_attr = ScreenAttrs[off_to - 1];
625 term_windgoto(row, col + coloff);
626 screen_stop_highlight();
627 }
628 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100629 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 if (enc_dbcs != 0)
632 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100633 // Check if overwriting a double-byte with a single-byte or
634 // the other way around requires another character to be
635 // redrawn. For UTF-8 this isn't needed, because comparing
636 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 if (char_cells == 1
638 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000639 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100641 // Writing a single-cell character over a double-cell
642 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 ScreenLines[off_to + 1] = 0;
644 redraw_next = TRUE;
645 }
646 else if (char_cells == 2
647 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000648 && (*mb_off2cells)(off_to, max_off_to) == 1
649 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100651 // Writing the second half of a double-cell character over
652 // a double-cell character: need to redraw the second
653 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 ScreenLines[off_to + 2] = 0;
655 redraw_next = TRUE;
656 }
657
658 if (enc_dbcs == DBCS_JPNU)
659 ScreenLines2[off_to] = ScreenLines2[off_from];
660 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100661 // When writing a single-width character over a double-width
662 // character and at the end of the redrawn text, need to clear out
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000663 // the right half of the old character.
664 // Also required when writing the right half of a double-width
665 // char over the left half of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (has_mbyte && col + char_cells == endcol
667 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000668 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000670 && (*mb_off2cells)(off_to, max_off_to) == 1
671 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
674 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 if (enc_utf8)
676 {
677 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
678 if (ScreenLinesUC[off_from] != 0)
679 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000680 int i;
681
682 for (i = 0; i < Screen_mco; ++i)
683 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
685 }
686 if (char_cells == 2)
687 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100690 // The bold trick makes a single column of pixels appear in the
691 // next character. When a bold character is removed, the next
692 // character should be redrawn too. This happens for our own GUI
693 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (
695# ifdef FEAT_GUI
696 gui.in_use
697# endif
698# if defined(FEAT_GUI) && defined(UNIX)
699 ||
700# endif
701# ifdef UNIX
702 term_is_xterm
703# endif
704 )
705 {
706 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000707 if (hl > HL_ALL)
708 hl = syn_attr2attr(hl);
709 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 redraw_next = TRUE;
711 }
712#endif
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100713#ifdef FEAT_GUI_MSWIN
714 // MS-Windows antialiasing may spill over to the next character,
715 // redraw that one if this one changed, no matter attributes.
716 if (gui.in_use && changed_this)
717 redraw_next = TRUE;
718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100720
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100721 // For simplicity set the attributes of second half of a
722 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000723 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000725
726 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 screen_char(off_to, row, col + coloff);
730 }
731 else if ( p_wiv
732#ifdef FEAT_GUI
733 && !gui.in_use
734#endif
735 && col + coloff > 0)
736 {
737 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
738 {
739 /*
740 * Don't output stop-highlight when moving the cursor, it will
741 * stop the highlighting when it should continue.
742 */
743 screen_attr = 0;
744 }
745 else if (screen_attr != 0)
746 screen_stop_highlight();
747 }
748
Bram Moolenaarb9081882022-07-09 04:56:24 +0100749 ScreenCols[off_to] = ScreenCols[off_from];
750 if (char_cells == 2)
zeertzjq99941602023-08-19 13:08:50 +0200751 ScreenCols[off_to + 1] = ScreenCols[off_from + 1];
Bram Moolenaarb9081882022-07-09 04:56:24 +0100752
753 off_to += char_cells;
754 off_from += char_cells;
755 col += char_cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 }
757
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100758 if (clear_next && !skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100760 // Clear the second half of a double-wide character of which the left
761 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 ScreenLines[off_to] = ' ';
763 if (enc_utf8)
764 ScreenLinesUC[off_to] = 0;
765 screen_char(off_to, row, col + coloff);
766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 if (clear_width > 0
769#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200770 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
772 )
773 {
774#ifdef FEAT_GUI
775 int startCol = col;
776#endif
777
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100778 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 while (col < clear_width && ScreenLines[off_to] == ' '
780 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100781 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {
zeertzjqd0c1b772024-03-16 15:03:33 +0100783 ScreenCols[off_to] =
784 (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 ++off_to;
786 ++col;
787 }
788 if (col < clear_width)
789 {
790#ifdef FEAT_GUI
791 /*
792 * In the GUI, clearing the rest of the line may leave pixels
793 * behind if the first character cleared was bold. Some bold
794 * fonts spill over the left. In this case we redraw the previous
795 * character too. If we didn't skip any blanks above, then we
796 * only redraw if the character wasn't already redrawn anyway.
797 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000798 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
800 hl = ScreenAttrs[off_to];
801 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000802 {
803 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100804
Bram Moolenaar9c697322006-10-09 20:11:17 +0000805 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100806 // for utf-8, ScreenLines[char_offset + 1] == 0 means
807 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000808 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
809 else if (enc_dbcs != 0)
810 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100811 // find previous character by counting from first
812 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000813 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000814 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000815
816 while (off < off_to)
817 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000818 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000819 off += prev_cells;
820 }
821 }
822
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100823 if (!skip_for_popup(row, col + coloff - prev_cells))
824 {
825 if (enc_dbcs != 0 && prev_cells > 1)
826 screen_char_2(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000827 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100828 else
829 screen_char(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000830 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100831 }
Bram Moolenaar9c697322006-10-09 20:11:17 +0000832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834#endif
835 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
836 ' ', ' ', 0);
Bram Moolenaarb9081882022-07-09 04:56:24 +0100837 while (col < clear_width)
838 {
zeertzjqd0c1b772024-03-16 15:03:33 +0100839 ScreenCols[off_to++]
840 = (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100841 ++col;
842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 }
845
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200846 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100847#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200848 && !(flags & SLF_POPUP) // no separator for popup window
849#endif
850 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200852 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200853 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200854 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100856 if (!skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200858 int c;
859
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100860 c = fillchar_vsep(&hl, wp);
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200861 if (ScreenLines[off_to] != (schar_T)c
862 || (enc_utf8 && (int)ScreenLinesUC[off_to]
863 != (c >= 0x80 ? c : 0))
864 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200866 ScreenLines[off_to] = c;
867 ScreenAttrs[off_to] = hl;
868 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200870 if (c >= 0x80)
871 {
872 ScreenLinesUC[off_to] = c;
873 ScreenLinesC[0][off_to] = 0;
874 }
875 else
876 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200878 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 }
882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 LineWraps[row] = FALSE;
884 }
885}
886
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000887#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000889 * Mirror text "str" for right-left displaying.
890 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000892 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100893rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
895 char_u *p1, *p2;
896 int t;
897
898 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
899 {
900 t = *p1;
901 *p1 = *p2;
902 *p2 = t;
903 }
904}
905#endif
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 * Draw the verticap separator right of window "wp" starting with line "row".
909 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200910 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100911draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
913 int hl;
914 int c;
915
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000916 if (!wp->w_vsep_width)
917 return;
918
919 // draw the vertical separator right of this window
920 c = fillchar_vsep(&hl, wp);
921 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
922 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
923 c, ' ', hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 * Return TRUE if the status line of window "wp" is connected to the status
928 * line of the window right of it. If not, then it's a vertical separator.
929 * Only call if (wp->w_vsep_width != 0).
930 */
931 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100932stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 frame_T *fr;
935
936 fr = wp->w_frame;
937 while (fr->fr_parent != NULL)
938 {
939 if (fr->fr_parent->fr_layout == FR_COL)
940 {
941 if (fr->fr_next != NULL)
942 break;
943 }
944 else
945 {
946 if (fr->fr_next != NULL)
947 return TRUE;
948 }
949 fr = fr->fr_parent;
950 }
951 return FALSE;
952}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955/*
956 * Get the value to show for the language mappings, active 'keymap'.
957 */
958 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100959get_keymap_str(
960 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100961 char_u *fmt, // format string containing one %s item
962 char_u *buf, // buffer for the result
963 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
965 char_u *p;
966
967 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
968 return FALSE;
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970#ifdef FEAT_EVAL
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000971 buf_T *old_curbuf = curbuf;
972 win_T *old_curwin = curwin;
973 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000975 curbuf = wp->w_buffer;
976 curwin = wp;
977 STRCPY(buf, "b:keymap_name"); // must be writable
978 ++emsg_skip;
979 s = p = eval_to_string(buf, FALSE, FALSE);
980 --emsg_skip;
981 curbuf = old_curbuf;
982 curwin = old_curwin;
983 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000985 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef FEAT_KEYMAP
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000987 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
988 p = wp->w_buffer->b_p_keymap;
989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000991 p = (char_u *)"lang";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000993 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
994 buf[0] = NUL;
995#ifdef FEAT_EVAL
996 vim_free(s);
997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 return buf[0] != NUL;
999}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001#if defined(FEAT_STL_OPT) || defined(PROTO)
1002/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001003 * Redraw the status line or ruler of window "wp".
1004 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001007win_redr_custom(
1008 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001009 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001011 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 int attr;
1013 int curattr;
1014 int row;
1015 int col = 0;
1016 int maxwidth;
1017 int width;
1018 int n;
1019 int len;
1020 int fillchar;
1021 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001022 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 char_u *p;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001024 char_u *opt_name;
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001025 int opt_scope = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001026 stl_hlrec_T *hltab;
1027 stl_hlrec_T *tabtab;
Bram Moolenaar61452852011-02-01 18:01:11 +01001028 win_T *ewp;
1029 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001031 // There is a tiny chance that this gets called recursively: When
1032 // redrawing a status line triggers redrawing the ruler or tabline.
1033 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001034 if (entered)
1035 return;
1036 entered = TRUE;
1037
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001038 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001039 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001041 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001042 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001043 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001044 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001045 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001046 maxwidth = Columns;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001047 opt_name = (char_u *)"tabline";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001049 else
1050 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001051 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001052 fillchar = fillchar_status(&attr, wp);
Sean Dewarfc8a6012023-04-17 16:41:20 +01001053 int in_status_line = wp->w_status_height != 0;
1054 maxwidth = in_status_line ? wp->w_width : Columns;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001055
1056 if (draw_ruler)
1057 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001058 stl = p_ruf;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001059 opt_name = (char_u *)"rulerformat";
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001060 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001061 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001062 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001063 if (*++stl == '-')
1064 stl++;
1065 if (atoi((char *)stl))
1066 while (VIM_ISDIGIT(*stl))
1067 stl++;
1068 if (*stl++ != '(')
1069 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001070 }
Sean Dewarfc8a6012023-04-17 16:41:20 +01001071 col = ru_col - (Columns - maxwidth);
1072 if (col < (maxwidth + 1) / 2)
1073 col = (maxwidth + 1) / 2;
1074 maxwidth -= col;
1075 if (!in_status_line)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001076 {
1077 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001078 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001079 fillchar = ' ';
1080 attr = 0;
1081 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001082 }
1083 else
1084 {
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001085 opt_name = (char_u *)"statusline";
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001086 if (*wp->w_p_stl != NUL)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001087 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001088 stl = wp->w_p_stl;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001089 opt_scope = OPT_LOCAL;
1090 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001091 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001092 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001093 }
1094
Sean Dewarfc8a6012023-04-17 16:41:20 +01001095 if (in_status_line)
1096 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001097 }
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001100 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001102 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1103 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001104 ewp = wp == NULL ? curwin : wp;
1105 p_crb_save = ewp->w_p_crb;
1106 ewp->w_p_crb = FALSE;
1107
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001108 // Make a copy, because the statusline may include a function call that
1109 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001110 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001111 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001112 stl, opt_name, opt_scope,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001113 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001114 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001115 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001117 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001118 p = transstr(buf);
1119 if (p != NULL)
1120 {
1121 vim_strncpy(buf, p, sizeof(buf) - 1);
1122 vim_free(p);
1123 }
1124
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001125 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001126 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001127 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 ++width;
1131 }
1132 buf[len] = NUL;
1133
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001134 /*
1135 * Draw each snippet with the specified highlighting.
1136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 curattr = attr;
1138 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001139 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001141 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 screen_puts_len(p, len, row, col, curattr);
1143 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001144 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001146 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001148 else if (hltab[n].userhl < 0)
1149 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001150#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001151 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1152 && wp->w_status_height != 0)
1153 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001154 else if (wp != NULL && bt_terminal(wp->w_buffer)
1155 && wp->w_status_height != 0)
1156 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001157#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001158 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001159 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001161 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001164
1165 if (wp == NULL)
1166 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001167 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001168 col = 0;
1169 len = 0;
1170 p = buf;
1171 fillchar = 0;
1172 for (n = 0; tabtab[n].start != NULL; n++)
1173 {
1174 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1175 while (col < len)
1176 TabPageIdxs[col++] = fillchar;
1177 p = tabtab[n].start;
1178 fillchar = tabtab[n].userhl;
1179 }
1180 while (col < Columns)
1181 TabPageIdxs[col++] = fillchar;
1182 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001183
1184theend:
1185 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186}
1187
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001188#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
1190/*
1191 * Output a single character directly to the screen and update ScreenLines.
1192 */
1193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001194screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 char_u buf[MB_MAXBYTES + 1];
1197
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001198 if (has_mbyte)
1199 buf[(*mb_char2bytes)(c, buf)] = NUL;
1200 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001201 {
1202 buf[0] = c;
1203 buf[1] = NUL;
1204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 screen_puts(buf, row, col, attr);
1206}
1207
1208/*
zeertzjq47eec672023-06-01 20:26:55 +01001209 * Get a single character directly from ScreenLines into "bytes", which must
1210 * have a size of "MB_MAXBYTES + 1".
1211 * If "attrp" is not NULL, return the character's attribute in "*attrp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001214screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 unsigned off;
1217
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001218 // safety check
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001219 if (ScreenLines == NULL || row >= screen_Rows || col >= screen_Columns)
1220 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001222 off = LineOffset[row] + col;
zeertzjq47eec672023-06-01 20:26:55 +01001223 if (attrp != NULL)
1224 *attrp = ScreenAttrs[off];
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001225 bytes[0] = ScreenLines[off];
1226 bytes[1] = NUL;
1227
1228 if (enc_utf8 && ScreenLinesUC[off] != 0)
1229 bytes[utfc_char2bytes(off, bytes)] = NUL;
1230 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1231 {
1232 bytes[0] = ScreenLines[off];
1233 bytes[1] = ScreenLines2[off];
1234 bytes[2] = NUL;
1235 }
1236 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1237 {
1238 bytes[1] = ScreenLines[off + 1];
1239 bytes[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241}
1242
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001243/*
1244 * Return TRUE if composing characters for screen posn "off" differs from
1245 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001246 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001247 */
1248 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001249screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001250{
1251 int i;
1252
1253 for (i = 0; i < Screen_mco; ++i)
1254 {
1255 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1256 return TRUE;
1257 if (u8cc[i] == 0)
1258 break;
1259 }
1260 return FALSE;
1261}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263/*
1264 * Put string '*text' on the screen at position 'row' and 'col', with
1265 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1266 * Note: only outputs within one row, message is truncated at screen boundary!
1267 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1268 */
1269 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001270screen_puts(
1271 char_u *text,
1272 int row,
1273 int col,
1274 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276 screen_puts_len(text, -1, row, col, attr);
1277}
1278
1279/*
1280 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1281 * a NUL.
1282 */
1283 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001284screen_puts_len(
1285 char_u *text,
1286 int textlen,
1287 int row,
1288 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001289 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001291 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 unsigned off;
1293 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001294 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001296 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 int mbyte_blen = 1;
1298 int mbyte_cells = 1;
1299 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001300 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001302#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001303 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001304 int pc, nc, nc1;
1305 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001307 int force_redraw_this;
1308 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001309 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001311 // Safety check. The check for negative row and column is to fix issue
1312 // #4102. TODO: find out why row/col could be negative.
1313 if (ScreenLines == NULL
1314 || row >= screen_Rows || row < 0
1315 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001317 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001319 // When drawing over the right half of a double-wide char clear out the
1320 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001321 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001322#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001323 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001324#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001325 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001326 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001327 if (!skip_for_popup(row, col - 1))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001328 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001329 ScreenLines[off - 1] = ' ';
1330 ScreenAttrs[off - 1] = 0;
1331 if (enc_utf8)
1332 {
1333 ScreenLinesUC[off - 1] = 0;
1334 ScreenLinesC[0][off - 1] = 0;
1335 }
1336 // redraw the previous cell, make it empty
1337 screen_char(off - 1, row, col - 1);
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001338 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001339 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001340 force_redraw_next = TRUE;
1341 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001342
Bram Moolenaar367329b2007-08-30 11:53:22 +00001343 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001344 while (col < screen_Columns
1345 && (len < 0 || (int)(ptr - text) < len)
1346 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
1348 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001349 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (has_mbyte)
1351 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001352 mbyte_blen = enc_utf8 && len > 0
1353 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1354 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1356 mbyte_cells = 1;
1357 else if (enc_dbcs != 0)
1358 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001359 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001361 u8c = len >= 0
1362 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1363 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001365#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1367 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001368 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1370 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001371 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 nc = NUL;
1373 nc1 = NUL;
1374 }
1375 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001376 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001377 nc = len >= 0
1378 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1379 (int)((text + len) - ptr - mbyte_blen))
1380 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 nc1 = pcc[0];
1382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 pc = prev_c;
1384 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001385 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 else
1388 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001389#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001390 if (col + mbyte_cells > screen_Columns)
1391 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001392 // Only 1 cell left, but character requires 2 cells:
1393 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001394 c = '>';
1395 mbyte_cells = 1;
1396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001400 force_redraw_this = force_redraw_next;
1401 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001402
1403 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 || (mbyte_cells == 2
1405 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1406 || (enc_dbcs == DBCS_JPNU
1407 && c == 0x8e
1408 && ScreenLines2[off] != ptr[1])
1409 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001410 && (ScreenLinesUC[off] !=
1411 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1412 || (ScreenLinesUC[off] != 0
1413 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001415 || exmode_active;
1416
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001417 if ((need_redraw || force_redraw_this) && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
1419#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001420 // The bold trick makes a single row of pixels appear in the next
1421 // character. When a bold character is removed, the next
1422 // character should be redrawn too. This happens for our own GUI
1423 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001424 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425# ifdef FEAT_GUI
1426 gui.in_use
1427# endif
1428# if defined(FEAT_GUI) && defined(UNIX)
1429 ||
1430# endif
1431# ifdef UNIX
1432 term_is_xterm
1433# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001434 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001436 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001438 if (n > HL_ALL)
1439 n = syn_attr2attr(n);
1440 if (n & HL_BOLD)
1441 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001444 // When at the end of the text and overwriting a two-cell
1445 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001446 // cell. Also when overwriting the left half of a two-cell char
1447 // with the right half of a two-cell char. Do this only once
1448 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (clear_next_cell)
1450 clear_next_cell = FALSE;
1451 else if (has_mbyte
1452 && (len < 0 ? ptr[mbyte_blen] == NUL
1453 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001454 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001456 && (*mb_off2cells)(off, max_off) == 1
1457 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 clear_next_cell = TRUE;
1459
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001460 // Make sure we never leave a second byte of a double-byte behind,
1461 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001463 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001465 && (*mb_off2cells)(off, max_off) == 1
1466 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ScreenLines[off] = c;
1469 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001470 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (enc_utf8)
1472 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001473 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 ScreenLinesUC[off] = 0;
1475 else
1476 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001477 int i;
1478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001480 for (i = 0; i < Screen_mco; ++i)
1481 {
1482 ScreenLinesC[i][off] = u8cc[i];
1483 if (u8cc[i] == 0)
1484 break;
1485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 }
1487 if (mbyte_cells == 2)
1488 {
1489 ScreenLines[off + 1] = 0;
1490 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001491 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493 screen_char(off, row, col);
1494 }
1495 else if (mbyte_cells == 2)
1496 {
1497 ScreenLines[off + 1] = ptr[1];
1498 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001499 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 screen_char_2(off, row, col);
1501 }
1502 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1503 {
1504 ScreenLines2[off] = ptr[1];
1505 screen_char(off, row, col);
1506 }
1507 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 screen_char(off, row, col);
1509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 if (has_mbyte)
1511 {
1512 off += mbyte_cells;
1513 col += mbyte_cells;
1514 ptr += mbyte_blen;
1515 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001516 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001517 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001518 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001520 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001521 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 ++off;
1527 ++col;
1528 ++ptr;
1529 }
1530 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001531
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001532 // If we detected the next character needs to be redrawn, but the text
1533 // doesn't extend up to there, update the character here.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001534 if (force_redraw_next && col < screen_Columns && !skip_for_popup(row, col))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001535 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001536 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1537 screen_char_2(off, row, col);
1538 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001539 screen_char(off, row, col);
1540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541}
1542
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001543#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001545 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001547 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001548start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001550 if (!p_hls || no_hlsearch)
1551 return;
1552
1553 end_search_hl(); // just in case it wasn't called before
1554 last_pat_prog(&screen_search_hl.rm);
1555 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556}
1557
1558/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001559 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001561 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001562end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001564 if (screen_search_hl.rm.regprog == NULL)
1565 return;
1566
1567 vim_regfree(screen_search_hl.rm.regprog);
1568 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001570#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001573screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 attrentry_T *aep = NULL;
1576
1577 screen_attr = attr;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001578 if (!full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001579#ifdef MSWIN
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001580 || !termcap_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001582 )
1583 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001585#ifdef FEAT_GUI
1586 if (gui.in_use)
1587 {
1588 char buf[20];
1589
1590 // The GUI handles this internally.
1591 sprintf(buf, "\033|%dh", attr);
1592 OUT_STR(buf);
1593 return;
1594 }
1595#endif
1596
1597 if (attr > HL_ALL) // special HL attr.
1598 {
1599 if (IS_CTERM)
1600 aep = syn_cterm_attr2entry(attr);
1601 else
1602 aep = syn_term_attr2entry(attr);
1603 if (aep == NULL) // did ":syntax clear"
1604 attr = 0;
1605 else
1606 attr = aep->ae_attr;
1607 }
1608#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1609 if (use_vtp())
1610 {
1611 guicolor_T defguifg, defguibg;
1612 int defctermfg, defctermbg;
1613
1614 // If FG and BG are unset, the color is undefined when
1615 // BOLD+INVERSE. Use Normal as the default value.
1616 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1617 &defguibg);
1618
1619 if (p_tgc)
1620 {
1621 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1622 term_fg_rgb_color(defguifg);
1623 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1624 term_bg_rgb_color(defguibg);
1625 }
1626 else if (t_colors >= 256)
1627 {
1628 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1629 term_fg_color(defctermfg);
1630 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1631 term_bg_color(defctermbg);
1632 }
1633 }
1634#endif
1635 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
1636 out_str(T_MD);
1637 else if (aep != NULL && cterm_normal_fg_bold && (
1638#ifdef FEAT_TERMGUICOLORS
1639 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1640 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1641 :
1642#endif
1643 t_colors > 1 && aep->ae_u.cterm.fg_color))
1644 // If the Normal FG color has BOLD attribute and the new HL
1645 // has a FG color defined, clear BOLD.
1646 out_str(T_ME);
1647 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
1648 out_str(T_SO);
1649 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
1650 out_str(T_UCS);
1651 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1652 out_str(T_USS);
1653 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1654 out_str(T_DS);
1655 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1656 out_str(T_CDS);
1657 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1658 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1659 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1660 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1661 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
1662 && *T_US != NUL)
1663 out_str(T_US);
1664 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
1665 out_str(T_CZH);
1666 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
1667 out_str(T_MR);
1668 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
1669 out_str(T_STS);
1670
1671 /*
1672 * Output the color or start string after bold etc., in case the
1673 * bold etc. override the color setting.
1674 */
1675 if (aep != NULL)
1676 {
PMuncha606f3a2023-11-15 15:35:49 +01001677 if (aep->ae_u.cterm.font > 0 && aep->ae_u.cterm.font < 12)
1678 term_font(aep->ae_u.cterm.font);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001679#ifdef FEAT_TERMGUICOLORS
1680 // When 'termguicolors' is set but fg or bg is unset,
1681 // fall back to the cterm colors. This helps for SpellBad,
1682 // where the GUI uses a red undercurl.
1683 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
1684 {
1685 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
1686 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
1688 else
1689#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001690 if (t_colors > 1)
1691 {
1692 if (aep->ae_u.cterm.fg_color)
1693 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1694 }
1695#ifdef FEAT_TERMGUICOLORS
1696 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001698 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
1699 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
1700 }
1701 else
1702#endif
1703 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001705 if (aep->ae_u.cterm.bg_color)
1706 term_bg_color(aep->ae_u.cterm.bg_color - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001708#ifdef FEAT_TERMGUICOLORS
1709 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1710 {
1711 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1712 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1713 }
1714 else
1715#endif
1716 if (t_colors > 1)
Bram Moolenaara050b942019-12-02 21:35:31 +01001717 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001718 if (aep->ae_u.cterm.ul_color)
1719 term_ul_color(aep->ae_u.cterm.ul_color - 1);
Bram Moolenaara050b942019-12-02 21:35:31 +01001720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001722 if (!IS_CTERM)
1723 {
1724 if (aep->ae_u.term.start != NULL)
1725 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
1727 }
1728}
1729
1730 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001731screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001733 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001734#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001735 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
1738 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001739#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 && termcap_active
1741#endif
1742 )
1743 {
1744#ifdef FEAT_GUI
1745 if (gui.in_use)
1746 {
1747 char buf[20];
1748
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001749 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001750 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 OUT_STR(buf);
1752 }
1753 else
1754#endif
1755 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001756 int is_under;
1757
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001758 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
1760 attrentry_T *aep;
1761
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001762 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 {
1764 /*
1765 * Assume that t_me restores the original colors!
1766 */
1767 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001768 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001769#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001770 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1771 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001772# ifdef FEAT_VTP
1773 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1774# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001775 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001776#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001777 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001778#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001779 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1780 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001781# ifdef FEAT_VTP
1782 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1783# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001784 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001785#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001786 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001788#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1789 if (use_vtp())
1790 {
1791 if (do_ME_fg && do_ME_bg)
1792 do_ME = TRUE;
1793
1794 // FG and BG cannot be separated in T_ME, which is not
1795 // efficient.
1796 if (!do_ME && do_ME_fg)
1797 out_str((char_u *)"\033|39m"); // restore FG
1798 if (!do_ME && do_ME_bg)
1799 out_str((char_u *)"\033|49m"); // restore BG
1800 }
1801 else
1802 {
1803 // Process FG and BG at once.
1804 if (!do_ME)
1805 do_ME = do_ME_fg | do_ME_bg;
1806 }
1807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 else
1810 {
1811 aep = syn_term_attr2entry(screen_attr);
1812 if (aep != NULL && aep->ae_u.term.stop != NULL)
1813 {
1814 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1815 do_ME = TRUE;
1816 else
1817 out_str(aep->ae_u.term.stop);
1818 }
1819 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001820 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 screen_attr = 0;
1822 else
1823 screen_attr = aep->ae_attr;
1824 }
1825
1826 /*
1827 * Often all ending-codes are equal to T_ME. Avoid outputting the
1828 * same sequence several times.
1829 */
1830 if (screen_attr & HL_STANDOUT)
1831 {
1832 if (STRCMP(T_SE, T_ME) == 0)
1833 do_ME = TRUE;
1834 else
1835 out_str(T_SE);
1836 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001837 is_under = (screen_attr & (HL_UNDERCURL
1838 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
1839 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001840 {
1841 if (STRCMP(T_UCE, T_ME) == 0)
1842 do_ME = TRUE;
1843 else
1844 out_str(T_UCE);
1845 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001846 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
1848 if (STRCMP(T_UE, T_ME) == 0)
1849 do_ME = TRUE;
1850 else
1851 out_str(T_UE);
1852 }
1853 if (screen_attr & HL_ITALIC)
1854 {
1855 if (STRCMP(T_CZR, T_ME) == 0)
1856 do_ME = TRUE;
1857 else
1858 out_str(T_CZR);
1859 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001860 if (screen_attr & HL_STRIKETHROUGH)
1861 {
1862 if (STRCMP(T_STE, T_ME) == 0)
1863 do_ME = TRUE;
1864 else
1865 out_str(T_STE);
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
1868 out_str(T_ME);
1869
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001870#ifdef FEAT_TERMGUICOLORS
1871 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001873 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001874 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001875 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001876 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02001877 if (cterm_normal_ul_gui_color != INVALCOLOR)
1878 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001879 }
1880 else
1881#endif
1882 {
1883 if (t_colors > 1)
1884 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001885 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001886 if (cterm_normal_fg_color != 0)
1887 term_fg_color(cterm_normal_fg_color - 1);
1888 if (cterm_normal_bg_color != 0)
1889 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02001890 if (cterm_normal_ul_color != 0)
1891 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001892 if (cterm_normal_fg_bold)
1893 out_str(T_MD);
1894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 }
1897 }
1898 screen_attr = 0;
1899}
1900
1901/*
1902 * Reset the colors for a cterm. Used when leaving Vim.
1903 * The machine specific code may override this again.
1904 */
1905 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001906reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001908 if (!IS_CTERM)
1909 return;
1910
1911 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001912#ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001913 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
1914 || cterm_normal_bg_gui_color != INVALCOLOR)
1915 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001916#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 out_str(T_OP);
1921 screen_attr = -1;
1922 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001923 if (cterm_normal_fg_bold)
1924 {
1925 out_str(T_ME);
1926 screen_attr = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 }
1928}
1929
1930/*
1931 * Put character ScreenLines["off"] on the screen at position "row" and "col",
1932 * using the attributes from ScreenAttrs["off"].
1933 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001934 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001935screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936{
1937 int attr;
1938
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001939 // Check for illegal values, just in case (could happen just after
1940 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (row >= screen_Rows || col >= screen_Columns)
1942 return;
1943
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001944 // Outputting a character in the last cell on the screen may scroll the
1945 // screen up. Only do it when the "xn" termcap property is set, otherwise
1946 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01001947 if (*T_XN == NUL
1948 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001950 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 && !cmdmsg_rl
1952#endif
1953 )
1954 {
1955 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001956 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 return;
1958 }
1959
1960 /*
1961 * Stop highlighting first, so it's easier to move the cursor.
1962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (screen_char_attr != 0)
1964 attr = screen_char_attr;
1965 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 attr = ScreenAttrs[off];
1967 if (screen_attr != attr)
1968 screen_stop_highlight();
1969
1970 windgoto(row, col);
1971
1972 if (screen_attr != attr)
1973 screen_start_highlight(attr);
1974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (enc_utf8 && ScreenLinesUC[off] != 0)
1976 {
1977 char_u buf[MB_MAXBYTES + 1];
1978
Bram Moolenaarcb070082016-04-02 22:14:51 +02001979 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001980 {
1981 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01001982#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001983 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001984#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001985 )
1986 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001987 // Clear the two screen cells. If the character is actually
1988 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001989 out_str((char_u *)" ");
1990 term_windgoto(row, col);
1991 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001992 // not sure where the cursor is after drawing the ambiguous width
1993 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02001994 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001995 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02001996 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001998
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001999 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002000 buf[utfc_char2bytes(off, buf)] = NUL;
2001 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002007 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2009 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011
2012 screen_cur_col++;
2013}
2014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015/*
2016 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2017 * on the screen at position 'row' and 'col'.
2018 * The attributes of the first byte is used for all. This is required to
2019 * output the two bytes of a double-byte character with nothing in between.
2020 */
2021 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002022screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002024 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2026 return;
2027
dundargocc57b5bc2022-11-02 13:30:51 +00002028 // Outputting the last character on the screen may scroll the screen up.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002029 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2031 {
2032 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002033 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 return;
2035 }
2036
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002037 // Output the first byte normally (positions the cursor), then write the
2038 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 screen_char(off, row, col);
2040 out_char(ScreenLines[off + 1]);
2041 ++screen_cur_col;
2042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044/*
2045 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2046 * This uses the contents of ScreenLines[] and doesn't change it.
2047 */
2048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002049screen_draw_rectangle(
2050 int row,
2051 int col,
2052 int height,
2053 int width,
2054 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
2056 int r, c;
2057 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002058 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002060 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002061 if (ScreenLines == NULL)
2062 return;
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (invert)
2065 screen_char_attr = HL_INVERSE;
2066 for (r = row; r < row + height; ++r)
2067 {
2068 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002069 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 for (c = col; c < col + width; ++c)
2071 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002072 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002074 if (!skip_for_popup(r, c))
2075 screen_char_2(off + c, r, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 ++c;
2077 }
2078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002080 if (!skip_for_popup(r, c))
2081 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002082 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 }
2086 }
2087 screen_char_attr = 0;
2088}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090/*
2091 * Redraw the characters for a vertically split window.
2092 */
2093 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002094redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095{
2096 int col;
2097 int width;
2098
2099# ifdef FEAT_CLIPBOARD
2100 clip_may_clear_selection(row, end - 1);
2101# endif
2102
2103 if (wp == NULL)
2104 {
2105 col = 0;
2106 width = Columns;
2107 }
2108 else
2109 {
2110 col = wp->w_wincol;
2111 width = wp->w_width;
2112 }
2113 screen_draw_rectangle(row, col, end - row, width, FALSE);
2114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002116 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002117space_to_screenline(int off, int attr)
2118{
2119 ScreenLines[off] = ' ';
2120 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002121 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002122 if (enc_utf8)
2123 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002124}
2125
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002127 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2128 * to "end_col" (exclusive) with character "c1" in first column followed by
2129 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 */
2131 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002132screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002133 int start_row,
2134 int end_row,
2135 int start_col,
2136 int end_col,
2137 int c1,
2138 int c2,
2139 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002141 int row;
2142 int col;
2143 int off;
2144 int end_off;
2145 int did_delete;
2146 int c;
2147 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002149 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150#endif
2151
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002152 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002154 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 end_col = screen_Columns;
2156 if (ScreenLines == NULL
2157 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002158 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 return;
2160
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002161 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 norm_term = (
2163#ifdef FEAT_GUI
2164 !gui.in_use &&
2165#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002166 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 for (row = start_row; row < end_row; ++row)
2168 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002169 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002170#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002171 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002172#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002173 )
2174 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002175 // When drawing over the right half of a double-wide char clear
2176 // out the left half. When drawing over the left half of a
2177 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002178 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002179 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002180 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002181 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002182 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 /*
2185 * Try to use delete-line termcap code, when no attributes or in a
2186 * "normal" terminal, where a bold/italic space is just a
2187 * space.
2188 */
2189 did_delete = FALSE;
2190 if (c2 == ' '
2191 && end_col == Columns
2192 && can_clear(T_CE)
2193 && (attr == 0
2194 || (norm_term
2195 && attr <= HL_ALL
2196 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2197 {
2198 /*
2199 * check if we really need to clear something
2200 */
2201 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002202 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 ++col;
2204
2205 off = LineOffset[row] + col;
2206 end_off = LineOffset[row] + end_col;
2207
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002208 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 if (enc_utf8)
2210 while (off < end_off && ScreenLines[off] == ' '
2211 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2212 ++off;
2213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 while (off < end_off && ScreenLines[off] == ' '
2215 && ScreenAttrs[off] == 0)
2216 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002217 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 col = off - LineOffset[row];
2220 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002221 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002223 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002225 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002227 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 ++off;
2229 }
2230 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002231 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233
2234 off = LineOffset[row] + start_col;
2235 c = c1;
2236 for (col = start_col; col < end_col; ++col)
2237 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002238 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002239 || (enc_utf8 && (int)ScreenLinesUC[off]
2240 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002242 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#if defined(FEAT_GUI) || defined(UNIX)
2244 || force_next
2245#endif
2246 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002247 // Skip if under a(nother) popup.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002248 && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002251 // The bold trick may make a single row of pixels appear in
2252 // the next character. When a bold character is removed, the
2253 // next character should be redrawn too. This happens for our
2254 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (
2256# ifdef FEAT_GUI
2257 gui.in_use
2258# endif
2259# if defined(FEAT_GUI) && defined(UNIX)
2260 ||
2261# endif
2262# ifdef UNIX
2263 term_is_xterm
2264# endif
2265 )
2266 {
2267 if (ScreenLines[off] != ' '
2268 && (ScreenAttrs[off] > HL_ALL
2269 || ScreenAttrs[off] & HL_BOLD))
2270 force_next = TRUE;
2271 else
2272 force_next = FALSE;
2273 }
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002274#endif // FEAT_GUI || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (enc_utf8)
2277 {
2278 if (c >= 0x80)
2279 {
2280 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002281 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283 else
2284 ScreenLinesUC[off] = 0;
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 ScreenAttrs[off] = attr;
2287 if (!did_delete || c != ' ')
2288 screen_char(off, row, col);
2289 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002290 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 ++off;
2292 if (col == start_col)
2293 {
2294 if (did_delete)
2295 break;
2296 c = c2;
2297 }
2298 }
2299 if (end_col == Columns)
2300 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002301 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002304 if (start_col == 0 && end_col == Columns
2305 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002306 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002307 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002308 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310 }
2311}
2312
2313/*
2314 * Check if there should be a delay. Used before clearing or redrawing the
2315 * screen or the command line.
2316 */
2317 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002318check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2321 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002322 && emsg_silent == 0
2323 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002326 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 emsg_on_display = FALSE;
2328 if (check_msg_scroll)
2329 msg_scroll = FALSE;
2330 }
2331}
2332
2333/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002334 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2335 */
2336 static void
2337clear_TabPageIdxs(void)
2338{
2339 int scol;
2340
2341 for (scol = 0; scol < Columns; ++scol)
2342 TabPageIdxs[scol] = 0;
2343}
2344
2345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002347 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 * Returns TRUE if there is a valid screen to write to.
2349 * Returns FALSE when starting up and screen not initialized yet.
2350 */
2351 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002352screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002354 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 return (ScreenLines != NULL);
2356}
2357
2358/*
2359 * Resize the shell to Rows and Columns.
2360 * Allocate ScreenLines[] and associated items.
2361 *
2362 * There may be some time between setting Rows and Columns and (re)allocating
2363 * ScreenLines[]. This happens when starting up and when (manually) changing
2364 * the shell size. Always use screen_Rows and screen_Columns to access items
2365 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2366 * final size of the shell is needed.
2367 */
2368 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002369screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
2371 int new_row, old_row;
2372#ifdef FEAT_GUI
2373 int old_Rows;
2374#endif
2375 win_T *wp;
2376 int outofmem = FALSE;
2377 int len;
2378 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002380 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002383 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 unsigned *new_LineOffset;
2385 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002386 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002387#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002388 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002389 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002390 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002391#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002392 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002393 static int entered = FALSE; // avoid recursiveness
2394 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002395 int retry_count = 0;
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002396 int found_null;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002398retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 /*
2400 * Allocation of the screen buffers is done only when the size changes and
2401 * when Rows and Columns have been set and we have started doing full
2402 * screen stuff.
2403 */
2404 if ((ScreenLines != NULL
2405 && Rows == screen_Rows
2406 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 && enc_utf8 == (ScreenLinesUC != NULL)
2408 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002409 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 || Rows == 0
2411 || Columns == 0
2412 || (!full_screen && ScreenLines == NULL))
2413 return;
2414
2415 /*
2416 * It's possible that we produce an out-of-memory message below, which
2417 * will cause this function to be called again. To break the loop, just
2418 * return here.
2419 */
2420 if (entered)
2421 return;
2422 entered = TRUE;
2423
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002424 /*
2425 * Note that the window sizes are updated before reallocating the arrays,
2426 * thus we must not redraw here!
2427 */
2428 ++RedrawingDisabled;
2429
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002430 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002432#ifdef FEAT_GUI_HAIKU
2433 vim_lock_screen(); // be safe, put it here
2434#endif
2435
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002436 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 /*
2439 * We're changing the size of the screen.
2440 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2441 * - Move lines from the old arrays into the new arrays, clear extra
2442 * lines (unless the screen is going to be cleared).
2443 * - Free the old arrays.
2444 *
2445 * If anything fails, make ScreenLines NULL, so we don't do anything!
2446 * Continuing with the old ScreenLines may result in a crash, because the
2447 * size is wrong.
2448 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002449 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 win_free_lsize(wp);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002451 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002452 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002453 win_free_lsize(aucmd_win[i].auc_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002454#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002455 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002456 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002457 win_free_lsize(wp);
2458 // tab-local popup windows
2459 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002460 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002461 win_free_lsize(wp);
2462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002464 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002465 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (enc_utf8)
2467 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002468 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002469 for (int i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002470 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2471 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
2473 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002474 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2475 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
dundargocc57b5bc2022-11-02 13:30:51 +00002476 // Clear ScreenCols to avoid a warning for uninitialized memory in
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002477 // jump_to_mouse().
2478 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002479 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2480 new_LineWraps = LALLOC_MULT(char_u, Rows);
2481 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002482#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002483 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002484 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002485 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002488 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 if (win_alloc_lines(wp) == FAIL)
2491 {
2492 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002493 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00002496 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002497 if (aucmd_win[i].auc_win != NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002498 && aucmd_win[i].auc_win->w_lines == NULL
2499 && win_alloc_lines(aucmd_win[i].auc_win) == FAIL)
2500 {
2501 outofmem = TRUE;
2502 break;
2503 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002504#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002505 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002506 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002507 if (win_alloc_lines(wp) == FAIL)
2508 {
2509 outofmem = TRUE;
2510 goto give_up;
2511 }
2512 // tab-local popup windows
2513 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002514 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002515 if (win_alloc_lines(wp) == FAIL)
2516 {
2517 outofmem = TRUE;
2518 goto give_up;
2519 }
2520#endif
2521
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002522give_up:
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002523 found_null = FALSE;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002524 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002525 if (new_ScreenLinesC[i] == NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002526 {
2527 found_null = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002528 break;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 if (new_ScreenLines == NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002531 || (enc_utf8 && (new_ScreenLinesUC == NULL || found_null))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002534 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 || new_LineOffset == NULL
2536 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002537 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002538#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002539 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002540 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002541 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 || outofmem)
2544 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002546 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002547 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002548 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2549
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002550 // Remember we did this to avoid getting outofmem messages over
2551 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002552 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002553 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002554 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002555 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002556 for (int i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002557 VIM_CLEAR(new_ScreenLinesC[i]);
2558 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002559 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002560 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002561 VIM_CLEAR(new_LineOffset);
2562 VIM_CLEAR(new_LineWraps);
2563 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002564#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002565 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002566 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002567 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 else
2571 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002572 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 for (new_row = 0; new_row < Rows; ++new_row)
2575 {
2576 new_LineOffset[new_row] = new_row * Columns;
2577 new_LineWraps[new_row] = FALSE;
2578
Olaf Seibertfd472652024-02-01 21:11:16 +01002579 (void)vim_memset(new_ScreenLines + new_row * Columns,
2580 ' ', (size_t)Columns * sizeof(schar_T));
2581 if (enc_utf8)
2582 {
2583 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2584 0, (size_t)Columns * sizeof(u8char_T));
2585 for (int i = 0; i < p_mco; ++i)
2586 (void)vim_memset(new_ScreenLinesC[i]
2587 + new_row * Columns,
2588 0, (size_t)Columns * sizeof(u8char_T));
2589 }
2590 if (enc_dbcs == DBCS_JPNU)
2591 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2592 0, (size_t)Columns * sizeof(schar_T));
2593 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2594 0, (size_t)Columns * sizeof(sattr_T));
2595 (void)vim_memset(new_ScreenCols + new_row * Columns,
2596 0, (size_t)Columns * sizeof(colnr_T));
2597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 /*
2599 * If the screen is not going to be cleared, copy as much as
2600 * possible from the old screen to the new one and clear the rest
2601 * (used when resizing the window at the "--more--" prompt or when
2602 * executing an external command, for the GUI).
2603 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002604 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002607 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 if (screen_Columns < Columns)
2610 len = screen_Columns;
2611 else
2612 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002613 // When switching to utf-8 don't copy characters, they
2614 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002615 if (!(enc_utf8 && ScreenLinesUC == NULL)
2616 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002617 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2618 ScreenLines + LineOffset[old_row],
2619 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002620 if (enc_utf8 && ScreenLinesUC != NULL
2621 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
2623 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2624 ScreenLinesUC + LineOffset[old_row],
2625 (size_t)len * sizeof(u8char_T));
Bram Moolenaare76062c2022-11-28 18:51:43 +00002626 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002627 mch_memmove(new_ScreenLinesC[i]
2628 + new_LineOffset[new_row],
2629 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 (size_t)len * sizeof(u8char_T));
2631 }
2632 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2633 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2634 ScreenLines2 + LineOffset[old_row],
2635 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2637 ScreenAttrs + LineOffset[old_row],
2638 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002639 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2640 ScreenAttrs + LineOffset[old_row],
2641 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643 }
2644 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002645 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002647
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002648#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002649 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2650 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002654 free_screenlines();
2655
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002656 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002659 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002660 ScreenLinesC[i] = new_ScreenLinesC[i];
2661 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002664 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 LineOffset = new_LineOffset;
2666 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002667 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002668#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002669 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002670 popup_mask_next = new_popup_mask_next;
2671 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002672 popup_mask_refresh = TRUE;
2673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002675 // It's important that screen_Rows and screen_Columns reflect the actual
2676 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677#ifdef FEAT_GUI
2678 old_Rows = screen_Rows;
2679#endif
2680 screen_Rows = Rows;
2681 screen_Columns = Columns;
2682
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002683 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002684 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002685 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686#ifdef FEAT_GUI
2687 else if (gui.in_use
2688 && !gui.starting
2689 && ScreenLines != NULL
2690 && old_Rows != Rows)
2691 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002692 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2693
2694 // Adjust the position of the cursor, for when executing an external
2695 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002696 if (msg_row >= Rows) // Rows got smaller
2697 msg_row = Rows - 1; // put cursor at last row
2698 else if (Rows > old_Rows) // Rows got bigger
2699 msg_row += Rows - old_Rows; // put cursor in same place
2700 if (msg_col >= Columns) // Columns got smaller
2701 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
2703#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002704 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002706#ifdef FEAT_GUI_HAIKU
2707 vim_unlock_screen();
2708#endif
2709
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 entered = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002711 if (RedrawingDisabled > 0)
2712 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002713
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002714 /*
2715 * Do not apply autocommands more than 3 times to avoid an endless loop
2716 * in case applying autocommands always changes Rows or Columns.
2717 */
2718 if (starting == 0 && ++retry_count <= 3)
2719 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002720 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002721 // In rare cases, autocommands may have altered Rows or Columns,
2722 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002723 goto retry;
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725}
2726
2727 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002728free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002729{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730 int i;
2731
Bram Moolenaar33796b32019-06-08 16:01:13 +02002732 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002733 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002734 VIM_CLEAR(ScreenLinesC[i]);
2735 VIM_CLEAR(ScreenLines2);
2736 VIM_CLEAR(ScreenLines);
2737 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002738 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002739 VIM_CLEAR(LineOffset);
2740 VIM_CLEAR(LineWraps);
2741 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002742#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002743 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002744 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002745 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002746#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002747}
2748
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002749/*
2750 * Clear the screen.
2751 * May delay if there is something the user should read.
2752 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002753 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002754 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002755 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01002756 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002757screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
2759 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002760 screenalloc(FALSE); // allocate screen buffers if size changed
2761 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002762}
2763
2764/*
2765 * Do not clear the screen but mark everything for redraw.
2766 */
2767 void
2768redraw_as_cleared(void)
2769{
2770 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
2772
Bram Moolenaar838b7462022-09-26 15:19:56 +01002773 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002774screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002777 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 if (starting == NO_SCREEN || ScreenLines == NULL
2780#ifdef FEAT_GUI
2781 || (gui.in_use && gui.starting)
2782#endif
2783 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01002784 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786#ifdef FEAT_GUI
2787 if (!gui.in_use)
2788#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002789 screen_attr = -1; // force setting the Normal colors
2790 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
2792#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002793 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 clip_scroll_selection(9999);
2795#endif
2796
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002797 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 for (i = 0; i < Rows; ++i)
2799 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002800 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 LineWraps[i] = FALSE;
2802 }
2803
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002804 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002806 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002807 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002809 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811 else
2812 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002813 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 for (i = 0; i < Rows; ++i)
2815 lineinvalid(LineOffset[i], (int)Columns);
2816 clear_cmdline = TRUE;
2817 }
2818
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002819 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002821 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002823 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002824 if (must_redraw == UPD_CLEAR) // no need to clear again
2825 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002826 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002828#ifdef FEAT_PROP_POPUP
2829 popup_redraw_all(); // redraw all popup windows
2830#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002831 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002833 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 msg_didany = FALSE;
2835 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002836
2837 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838}
2839
2840/*
2841 * Clear one line in ScreenLines.
2842 */
2843 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002844lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
2846 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (enc_utf8)
2848 (void)vim_memset(ScreenLinesUC + off, 0,
2849 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002850 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002851 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852}
2853
2854/*
2855 * Mark one line in ScreenLines invalid by setting the attributes to an
2856 * invalid value.
2857 */
2858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002859lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860{
2861 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002862 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863}
2864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02002866 * To be called when characters were sent to the terminal directly, outputting
2867 * test on "screen_lnum".
2868 */
2869 void
2870line_was_clobbered(int screen_lnum)
2871{
2872 lineinvalid(LineOffset[screen_lnum], (int)Columns);
2873}
2874
2875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 * Copy part of a Screenline for vertically split window "wp".
2877 */
2878 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002879linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 unsigned off_to = LineOffset[to] + wp->w_wincol;
2882 unsigned off_from = LineOffset[from] + wp->w_wincol;
2883
2884 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
2885 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 if (enc_utf8)
2887 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002888 int i;
2889
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
2891 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002892 for (i = 0; i < p_mco; ++i)
2893 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
2894 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
2896 if (enc_dbcs == DBCS_JPNU)
2897 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
2898 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
2900 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002901 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
2902 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
2905/*
2906 * Return TRUE if clearing with term string "p" would work.
2907 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02002908 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 */
2910 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002911can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912{
2913 return (*p != NUL && (t_colors <= 1
2914#ifdef FEAT_GUI
2915 || gui.in_use
2916#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002917#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002918 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02002919 || (!p_tgc && cterm_normal_bg_color == 0)
2920#else
2921 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002922#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002923 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002924#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002925 && !(p == T_CE && popup_visible)
2926#endif
2927 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928}
2929
2930/*
2931 * Reset cursor position. Use whenever cursor was moved because of outputting
2932 * something directly to the screen (shell commands) or a terminal control
2933 * code.
2934 */
2935 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002936screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937{
2938 screen_cur_row = screen_cur_col = 9999;
2939}
2940
2941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 * Move the cursor to position "row","col" in the screen.
2943 * This tries to find the most efficient way to move, minimizing the number of
2944 * characters sent to the terminal.
2945 */
2946 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002947windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002949 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 int i;
2951 int plan;
2952 int cost;
2953 int wouldbe_col;
2954 int noinvcurs;
2955 char_u *bs;
2956 int goto_cost;
2957 int attr;
2958
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002959#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
2960#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962#define PLAN_LE 1
2963#define PLAN_CR 2
2964#define PLAN_NL 3
2965#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002966 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 if (ScreenLines == NULL)
2968 return;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002969 if (col == screen_cur_col && row == screen_cur_row)
2970 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002972 // Check for valid position.
2973 if (row < 0) // window without text lines?
2974 row = 0;
2975 if (row >= screen_Rows)
2976 row = screen_Rows - 1;
2977 if (col >= screen_Columns)
2978 col = screen_Columns - 1;
2979
2980 // check if no cursor movement is allowed in highlight mode
2981 if (screen_attr && *T_MS == NUL)
2982 noinvcurs = HIGHL_COST;
2983 else
2984 noinvcurs = 0;
2985 goto_cost = GOTO_COST + noinvcurs;
2986
2987 /*
2988 * Plan how to do the positioning:
2989 * 1. Use CR to move it to column 0, same row.
2990 * 2. Use T_LE to move it a few columns to the left.
2991 * 3. Use NL to move a few lines down, column 0.
2992 * 4. Move a few columns to the right with T_ND or by writing chars.
2993 *
2994 * Don't do this if the cursor went beyond the last column, the cursor
2995 * position is unknown then (some terminals wrap, some don't )
2996 *
2997 * First check if the highlighting attributes allow us to write
2998 * characters to move the cursor to the right.
2999 */
3000 if (row >= screen_cur_row && screen_cur_col < Columns)
3001 {
3002 /*
3003 * If the cursor is in the same row, bigger col, we can use CR
3004 * or T_LE.
3005 */
3006 bs = NULL; // init for GCC
3007 attr = screen_attr;
3008 if (row == screen_cur_row && col < screen_cur_col)
3009 {
3010 // "le" is preferred over "bc", because "bc" is obsolete
3011 if (*T_LE)
3012 bs = T_LE; // "cursor left"
3013 else
3014 bs = T_BC; // "backspace character (old)
3015 if (*bs)
3016 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3017 else
3018 cost = 999;
3019 if (col + 1 < cost) // using CR is less characters
3020 {
3021 plan = PLAN_CR;
3022 wouldbe_col = 0;
3023 cost = 1; // CR is just one character
3024 }
3025 else
3026 {
3027 plan = PLAN_LE;
3028 wouldbe_col = col;
3029 }
3030 if (noinvcurs) // will stop highlighting
3031 {
3032 cost += noinvcurs;
3033 attr = 0;
3034 }
3035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003038 * If the cursor is above where we want to be, we can use CR LF.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003040 else if (row > screen_cur_row)
3041 {
3042 plan = PLAN_NL;
3043 wouldbe_col = 0;
3044 cost = (row - screen_cur_row) * 2; // CR LF
3045 if (noinvcurs) // will stop highlighting
3046 {
3047 cost += noinvcurs;
3048 attr = 0;
3049 }
3050 }
3051
3052 /*
3053 * If the cursor is in the same row, smaller col, just use write.
3054 */
3055 else
3056 {
3057 plan = PLAN_WRITE;
3058 wouldbe_col = screen_cur_col;
3059 cost = 0;
3060 }
3061
3062 /*
3063 * Check if any characters that need to be written have the
3064 * correct attributes. Also avoid UTF-8 characters.
3065 */
3066 i = col - wouldbe_col;
3067 if (i > 0)
3068 cost += i;
3069 if (cost < goto_cost && i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
3071 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003072 * Check if the attributes are correct without additionally
3073 * stopping highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003075 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3076 while (i && *p++ == attr)
3077 --i;
3078 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
3080 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003081 * Try if it works when highlighting is stopped here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003083 if (*--p == 0)
3084 {
3085 cost += noinvcurs;
3086 while (i && *p++ == 0)
3087 --i;
3088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 if (i != 0)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003090 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003092 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003094 // Don't use an UTF-8 char for positioning, it's slow.
3095 for (i = wouldbe_col; i < col; ++i)
3096 if (ScreenLinesUC[LineOffset[row] + i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003098 cost = 999;
3099 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003101 }
3102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003104 /*
3105 * We can do it without term_windgoto()!
3106 */
3107 if (cost < goto_cost)
3108 {
3109 if (plan == PLAN_LE)
3110 {
3111 if (noinvcurs)
3112 screen_stop_highlight();
3113 while (screen_cur_col > col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003115 out_str(bs);
3116 --screen_cur_col;
3117 }
3118 }
3119 else if (plan == PLAN_CR)
3120 {
3121 if (noinvcurs)
3122 screen_stop_highlight();
3123 out_char('\r');
3124 screen_cur_col = 0;
3125 }
3126 else if (plan == PLAN_NL)
3127 {
3128 if (noinvcurs)
3129 screen_stop_highlight();
3130 while (screen_cur_row < row)
3131 {
3132 out_char('\n');
3133 ++screen_cur_row;
3134 }
3135 screen_cur_col = 0;
3136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003138 i = col - screen_cur_col;
3139 if (i > 0)
3140 {
3141 /*
3142 * Use cursor-right if it's one character only. Avoids
3143 * removing a line of pixels from the last bold char, when
3144 * using the bold trick in the GUI.
3145 */
3146 if (T_ND[0] != NUL && T_ND[1] == NUL)
3147 {
3148 while (i-- > 0)
3149 out_char(*T_ND);
3150 }
3151 else
3152 {
3153 int off;
3154
3155 off = LineOffset[row] + screen_cur_col;
3156 while (i-- > 0)
3157 {
3158 if (ScreenAttrs[off] != screen_attr)
3159 screen_stop_highlight();
3160 out_flush_check();
3161 out_char(ScreenLines[off]);
3162 if (enc_dbcs == DBCS_JPNU
3163 && ScreenLines[off] == 0x8e)
3164 out_char(ScreenLines2[off]);
3165 ++off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
3167 }
3168 }
3169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003171 else
3172 cost = 999;
3173
3174 if (cost >= goto_cost)
3175 {
3176 if (noinvcurs)
3177 screen_stop_highlight();
3178 if (row == screen_cur_row && (col > screen_cur_col)
3179 && *T_CRI != NUL)
3180 term_cursor_right(col - screen_cur_col);
3181 else
3182 term_windgoto(row, col);
3183 }
3184 screen_cur_row = row;
3185 screen_cur_col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186}
3187
3188/*
3189 * Set cursor to its position in the current window.
3190 */
3191 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003192setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003194 setcursor_mayforce(FALSE);
3195}
3196
3197/*
3198 * Set cursor to its position in the current window.
3199 * When "force" is TRUE also when not redrawing.
3200 */
3201 void
3202setcursor_mayforce(int force)
3203{
3204 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
3206 validate_cursor();
3207 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003208 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003210 // With 'rightleft' set and the cursor on a double-wide
3211 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003212 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3213 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003214 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003215 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216#endif
3217 curwin->w_wcol));
3218 }
3219}
3220
3221
3222/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003223 * Insert 'line_count' lines at 'row' in window 'wp'.
3224 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3225 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 * scrolling.
3227 * Returns FAIL if the lines are not inserted, OK for success.
3228 */
3229 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003230win_ins_lines(
3231 win_T *wp,
3232 int row,
3233 int line_count,
3234 int invalid,
3235 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 int did_delete;
3238 int nextrow;
3239 int lastrow;
3240 int retval;
3241
3242 if (invalid)
3243 wp->w_lines_valid = 0;
3244
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003245 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (wp->w_height < 5)
3247 return FAIL;
3248
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003249 // with the popup menu visible this might not work correctly
3250 if (pum_visible())
3251 return FAIL;
3252
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (line_count > wp->w_height - row)
3254 line_count = wp->w_height - row;
3255
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003256 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (retval != MAYBE)
3258 return retval;
3259
3260 /*
3261 * If there is a next window or a status line, we first try to delete the
3262 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003263 * If this fails and there are following windows, don't do anything to
3264 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 */
3266 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 if (wp->w_next != NULL || wp->w_status_height)
3268 {
3269 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003270 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 did_delete = TRUE;
3272 else if (wp->w_next)
3273 return FAIL;
3274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 /*
3276 * if no lines deleted, blank the lines that will end up below the window
3277 */
3278 if (!did_delete)
3279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003282 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 lastrow = nextrow + line_count;
3284 if (lastrow > Rows)
3285 lastrow = Rows;
3286 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003287 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 ' ', ' ', 0);
3289 }
3290
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003291 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 == FAIL)
3293 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003294 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (did_delete)
3296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 win_rest_invalid(W_NEXT(wp));
3299 }
3300 return FAIL;
3301 }
3302
3303 return OK;
3304}
3305
3306/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003307 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3309 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3310 * scrolling
3311 * Return OK for success, FAIL if the lines are not deleted.
3312 */
3313 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003314win_del_lines(
3315 win_T *wp,
3316 int row,
3317 int line_count,
3318 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003319 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003320 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 int retval;
3323
3324 if (invalid)
3325 wp->w_lines_valid = 0;
3326
3327 if (line_count > wp->w_height - row)
3328 line_count = wp->w_height - row;
3329
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003330 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (retval != MAYBE)
3332 return retval;
3333
3334 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003335 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 return FAIL;
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 /*
3339 * If there are windows or status lines below, try to put them at the
3340 * correct place. If we can't do that, they have to be redrawn.
3341 */
3342 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3343 {
3344 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003345 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
3347 wp->w_redr_status = TRUE;
3348 win_rest_invalid(wp->w_next);
3349 }
3350 }
3351 /*
3352 * If this is the last window and there is no status line, redraw the
3353 * command line later.
3354 */
3355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 redraw_cmdline = TRUE;
3357 return OK;
3358}
3359
3360/*
3361 * Common code for win_ins_lines() and win_del_lines().
3362 * Returns OK or FAIL when the work has been done.
3363 * Returns MAYBE when not finished yet.
3364 */
3365 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003366win_do_lines(
3367 win_T *wp,
3368 int row,
3369 int line_count,
3370 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003371 int del,
3372 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 int retval;
3375
3376 if (!redrawing() || line_count <= 0)
3377 return FAIL;
3378
Bram Moolenaar33796b32019-06-08 16:01:13 +02003379 // When inserting lines would result in loss of command output, just redraw
3380 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003381 if (no_win_do_lines_ins && !del)
3382 return FAIL;
3383
Bram Moolenaar33796b32019-06-08 16:01:13 +02003384 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003385 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003387 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003388 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 return FAIL;
3390 }
3391
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003392#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003393 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003394 if (popup_visible)
3395 return FAIL;
3396#endif
3397
3398 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (row + line_count >= wp->w_height)
3400 {
3401 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003402 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 ' ', ' ', 0);
3404 return OK;
3405 }
3406
3407 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003408 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003410 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003412 if (!no_win_do_lines_ins)
3413 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
3415 /*
3416 * If the terminal can set a scroll region, use that.
3417 * Always do this in a vertically split window. This will redraw from
3418 * ScreenLines[] when t_CV isn't defined. That's faster than using
3419 * win_line().
3420 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003421 * a character in the lower right corner of the scroll region may cause a
3422 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003424 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 scroll_region_set(wp, row);
3428 if (del)
3429 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003430 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 else
3432 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003433 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 scroll_region_reset();
3436 return retval;
3437 }
3438
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003439 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442 return MAYBE;
3443}
3444
3445/*
3446 * window 'wp' and everything after it is messed up, mark it for redraw
3447 */
3448 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003449win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003453 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 wp->w_redr_status = TRUE;
3455 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
3457 redraw_cmdline = TRUE;
3458}
3459
3460/*
3461 * The rest of the routines in this file perform screen manipulations. The
3462 * given operation is performed physically on the screen. The corresponding
3463 * change is also made to the internal screen image. In this way, the editor
3464 * anticipates the effect of editing changes on the appearance of the screen.
3465 * That way, when we call screenupdate a complete redraw isn't usually
3466 * necessary. Another advantage is that we can keep adding code to anticipate
3467 * screen changes, and in the meantime, everything still works.
3468 */
3469
3470/*
3471 * types for inserting or deleting lines
3472 */
3473#define USE_T_CAL 1
3474#define USE_T_CDL 2
3475#define USE_T_AL 3
3476#define USE_T_CE 4
3477#define USE_T_DL 5
3478#define USE_T_SR 6
3479#define USE_NL 7
3480#define USE_T_CD 8
3481#define USE_REDRAW 9
3482
3483/*
3484 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003485 * "end" is the line after the scrolled part. Normally it is Rows.
3486 * When scrolling region used "off" is the offset from the top for the region.
3487 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 *
3489 * return FAIL for failure, OK for success.
3490 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003491 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003492screen_ins_lines(
3493 int off,
3494 int row,
3495 int line_count,
3496 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003497 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003498 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
3500 int i;
3501 int j;
3502 unsigned temp;
3503 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003504 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 int type;
3506 int result_empty;
3507 int can_ce = can_clear(T_CE);
3508
3509 /*
3510 * FAIL if
3511 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 * - the line count is less than one
3513 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003514 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003515 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003516 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003518 if (!screen_valid(TRUE)
3519 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003520 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003521#ifdef FEAT_CLIPBOARD
3522 || (clip_star.state != SELECT_CLEARED
3523 && redrawing_for_callback > 0)
3524#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003525#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003526 || popup_visible
3527#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003528 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 return FAIL;
3530
3531 /*
3532 * There are seven ways to insert lines:
3533 * 0. When in a vertically split window and t_CV isn't set, redraw the
3534 * characters from ScreenLines[].
3535 * 1. Use T_CD (clear to end of display) if it exists and the result of
3536 * the insert is just empty lines
3537 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3538 * present or line_count > 1. It looks better if we do all the inserts
3539 * at once.
3540 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3541 * insert is just empty lines and T_CE is not present or line_count >
3542 * 1.
3543 * 4. Use T_AL (insert line) if it exists.
3544 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3545 * just empty lines.
3546 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3547 * just empty lines.
3548 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3549 * the 'da' flag is not set or we have clear line capability.
3550 * 8. redraw the characters from ScreenLines[].
3551 *
3552 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3553 * the scrollbar for the window. It does have insert line, use that if it
3554 * exists.
3555 */
3556 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003558 {
3559 // Avoid that lines are first cleared here and then redrawn, which
3560 // results in many characters updated twice. This happens with CTRL-F
3561 // in a vertically split window. With line-by-line scrolling
3562 // USE_REDRAW should be faster.
3563 if (line_count > 3)
3564 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003566 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003567 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 type = USE_T_CD;
3569 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3570 type = USE_T_CAL;
3571 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3572 type = USE_T_CDL;
3573 else if (*T_AL != NUL)
3574 type = USE_T_AL;
3575 else if (can_ce && result_empty)
3576 type = USE_T_CE;
3577 else if (*T_DL != NUL && result_empty)
3578 type = USE_T_DL;
3579 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3580 type = USE_T_SR;
3581 else
3582 return FAIL;
3583
3584 /*
3585 * For clearing the lines screen_del_lines() is used. This will also take
3586 * care of t_db if necessary.
3587 */
3588 if (type == USE_T_CD || type == USE_T_CDL ||
3589 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003590 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 /*
3593 * If text is retained below the screen, first clear or delete as many
3594 * lines at the bottom of the window as are about to be inserted so that
3595 * the deleted lines won't later surface during a screen_del_lines.
3596 */
3597 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003598 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
3600#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003601 // Remove a modeless selection when inserting lines halfway the screen
3602 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003603 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003604 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 else
3606 clip_scroll_selection(-line_count);
3607#endif
3608
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003609#ifdef FEAT_GUI_HAIKU
3610 vim_lock_screen();
3611#endif
3612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003614 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3615 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003616 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#endif
3618
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003619 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3620 cursor_col = wp->w_wincol;
3621
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003622 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 cursor_row = row;
3624 else
3625 cursor_row = row + off;
3626
3627 /*
3628 * Shift LineOffset[] line_count down to reflect the inserted lines.
3629 * Clear the inserted lines in ScreenLines[].
3630 */
3631 row += off;
3632 end += off;
3633 for (i = 0; i < line_count; ++i)
3634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if (wp != NULL && wp->w_width != Columns)
3636 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003637 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 j = end - 1 - i;
3639 while ((j -= line_count) >= row)
3640 linecopy(j + line_count, j, wp);
3641 j += line_count;
3642 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003643 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3644 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 else
3646 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3647 LineWraps[j] = FALSE;
3648 }
3649 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 j = end - 1 - i;
3652 temp = LineOffset[j];
3653 while ((j -= line_count) >= row)
3654 {
3655 LineOffset[j + line_count] = LineOffset[j];
3656 LineWraps[j + line_count] = LineWraps[j];
3657 }
3658 LineOffset[j + line_count] = temp;
3659 LineWraps[j + line_count] = FALSE;
3660 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003661 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 else
3663 lineinvalid(temp, (int)Columns);
3664 }
3665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003667#ifdef FEAT_GUI_HAIKU
3668 vim_unlock_screen();
3669#endif
3670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003672 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003673 if (clear_attr != 0)
3674 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003676 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (type == USE_REDRAW)
3678 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003679 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
3681 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003682 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684 else
3685 {
3686 for (i = 0; i < line_count; i++)
3687 {
3688 if (type == USE_T_AL)
3689 {
3690 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003691 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 out_str(T_AL);
3693 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003694 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003696 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698 }
3699
3700 /*
3701 * With scroll-reverse and 'da' flag set we need to clear the lines that
3702 * have been scrolled down into the region.
3703 */
3704 if (type == USE_T_SR && *T_DA)
3705 {
3706 for (i = 0; i < line_count; ++i)
3707 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003708 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003710 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712 }
3713
3714#ifdef FEAT_GUI
3715 gui_can_update_cursor();
3716 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003717 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718#endif
3719 return OK;
3720}
3721
3722/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003723 * Delete lines on the screen and update ScreenLines[].
3724 * "end" is the line after the scrolled part. Normally it is Rows.
3725 * When scrolling region used "off" is the offset from the top for the region.
3726 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 *
3728 * Return OK for success, FAIL if the lines are not deleted.
3729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003731screen_del_lines(
3732 int off,
3733 int row,
3734 int line_count,
3735 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003736 int force, // even when line_count > p_ttyscroll
3737 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003738 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 int j;
3741 int i;
3742 unsigned temp;
3743 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003744 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003746 int result_empty; // result is empty until end of region
3747 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 int type;
3749
3750 /*
3751 * FAIL if
3752 * - there is no valid screen
3753 * - the screen has to be redrawn completely
3754 * - the line count is less than one
3755 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003756 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003757 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003759 if (!screen_valid(TRUE)
3760 || line_count <= 0
3761 || (!force && line_count > p_ttyscroll)
3762 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003763#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003764 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003765#endif
3766 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 return FAIL;
3768
3769 /*
3770 * Check if the rest of the current region will become empty.
3771 */
3772 result_empty = row + line_count >= end;
3773
3774 /*
3775 * We can delete lines only when 'db' flag not set or when 'ce' option
3776 * available.
3777 */
3778 can_delete = (*T_DB == NUL || can_clear(T_CE));
3779
3780 /*
3781 * There are six ways to delete lines:
3782 * 0. When in a vertically split window and t_CV isn't set, redraw the
3783 * characters from ScreenLines[].
3784 * 1. Use T_CD if it exists and the result is empty.
3785 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3786 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3787 * none of the other ways work.
3788 * 4. Use T_CE (erase line) if the result is empty.
3789 * 5. Use T_DL (delete line) if it exists.
3790 * 6. redraw the characters from ScreenLines[].
3791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003793 {
3794 // Avoid that lines are first cleared here and then redrawn, which
3795 // results in many characters updated twice. This happens with CTRL-F
3796 // in a vertically split window. With line-by-line scrolling
3797 // USE_REDRAW should be faster.
3798 if (line_count > 3)
3799 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003801 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003802 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 else if (row == 0 && (
3805#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003806 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3807 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 line_count == 1 ||
3809#endif
3810 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 type = USE_NL;
3812 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3813 type = USE_T_CDL;
3814 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003815 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 type = USE_T_CE;
3817 else if (*T_DL != NUL && can_delete)
3818 type = USE_T_DL;
3819 else if (*T_CDL != NUL && can_delete)
3820 type = USE_T_CDL;
3821 else
3822 return FAIL;
3823
3824#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003825 // Remove a modeless selection when deleting lines halfway the screen or
3826 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003827 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003828 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 else
3830 clip_scroll_selection(line_count);
3831#endif
3832
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003833#ifdef FEAT_GUI_HAIKU
3834 vim_lock_screen();
3835#endif
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003838 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3839 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003840 gui_dont_update_cursor(gui.cursor_row >= row + off
3841 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#endif
3843
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003844 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3845 cursor_col = wp->w_wincol;
3846
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003847 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
3849 cursor_row = row;
3850 cursor_end = end;
3851 }
3852 else
3853 {
3854 cursor_row = row + off;
3855 cursor_end = end + off;
3856 }
3857
3858 /*
3859 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3860 * Clear the inserted lines in ScreenLines[].
3861 */
3862 row += off;
3863 end += off;
3864 for (i = 0; i < line_count; ++i)
3865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (wp != NULL && wp->w_width != Columns)
3867 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003868 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 j = row + i;
3870 while ((j += line_count) <= end - 1)
3871 linecopy(j - line_count, j, wp);
3872 j -= line_count;
3873 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003874 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3875 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 else
3877 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3878 LineWraps[j] = FALSE;
3879 }
3880 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003882 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 j = row + i;
3884 temp = LineOffset[j];
3885 while ((j += line_count) <= end - 1)
3886 {
3887 LineOffset[j - line_count] = LineOffset[j];
3888 LineWraps[j - line_count] = LineWraps[j];
3889 }
3890 LineOffset[j - line_count] = temp;
3891 LineWraps[j - line_count] = FALSE;
3892 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003893 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 else
3895 lineinvalid(temp, (int)Columns);
3896 }
3897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003899#ifdef FEAT_GUI_HAIKU
3900 vim_unlock_screen();
3901#endif
3902
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003903 if (screen_attr != clear_attr)
3904 screen_stop_highlight();
3905 if (clear_attr != 0)
3906 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003908 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 if (type == USE_REDRAW)
3910 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003911 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003913 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003915 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917 else if (type == USE_T_CDL)
3918 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003919 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003921 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923 /*
3924 * Deleting lines at top of the screen or scroll region: Just scroll
3925 * the whole screen (scroll region) up by outputting newlines on the
3926 * last line.
3927 */
3928 else if (type == USE_NL)
3929 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003930 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003932 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934 else
3935 {
3936 for (i = line_count; --i >= 0; )
3937 {
3938 if (type == USE_T_DL)
3939 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003940 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003941 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003943 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003945 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003946 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003948 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 }
3951
3952 /*
3953 * If the 'db' flag is set, we need to clear the lines that have been
3954 * scrolled up at the bottom of the region.
3955 */
3956 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
3957 {
3958 for (i = line_count; i > 0; --i)
3959 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003960 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003961 out_str(T_CE); // erase a line
3962 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 }
3965
3966#ifdef FEAT_GUI
3967 gui_can_update_cursor();
3968 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003969 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970#endif
3971
3972 return OK;
3973}
3974
3975/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003976 * Return TRUE when postponing displaying the mode message: when not redrawing
3977 * or inside a mapping.
3978 */
3979 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003980skip_showmode(void)
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003981{
3982 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01003983 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003984 if (global_busy
3985 || msg_silent != 0
3986 || !redrawing()
3987 || (char_avail() && !KeyTyped))
3988 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02003989 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003990 return TRUE;
3991 }
3992 return FALSE;
3993}
3994
3995/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01003996 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 *
3998 * If clear_cmdline is TRUE, clear the rest of the cmdline.
3999 * If clear_cmdline is FALSE there may be a message there that needs to be
4000 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004001 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 * Return the length of the message (0 if no message).
4003 */
4004 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004005showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
4007 int need_clear;
4008 int length = 0;
4009 int do_mode;
4010 int attr;
4011 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaara2a89732022-08-31 14:46:18 +01004014 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004015 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004016 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004017 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004018 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004020 if (skip_showmode())
4021 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
4023 nwr_save = need_wait_return;
4024
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004025 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 check_for_delay(FALSE);
4027
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004028 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 need_clear = clear_cmdline;
4030 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004031 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004033 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 msg_pos_mode();
4035 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004036 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 if (do_mode)
4038 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004039 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004041 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004042# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004043 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004044# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004045 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004046# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004047 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004048# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004049 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004051 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052# endif
4053#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004054 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004055 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004057 // These messages can get long, avoid a wrap in a narrow
4058 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 length = (Rows - msg_row) * Columns - 3;
4060 if (edit_submode_extra != NULL)
4061 length -= vim_strsize(edit_submode_extra);
4062 if (length > 0)
4063 {
4064 if (edit_submode_pre != NULL)
4065 length -= vim_strsize(edit_submode_pre);
4066 if (length - vim_strsize(edit_submode) > 0)
4067 {
4068 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004069 msg_puts_attr((char *)edit_submode_pre, attr);
4070 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072 if (edit_submode_extra != NULL)
4073 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004074 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004076 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 else
4078 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004079 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004086 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004087 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004088 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004089 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
4091#ifdef FEAT_RIGHTLEFT
4092 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004093 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004095 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004097 else if (restart_edit == 'I' || restart_edit == 'i' ||
4098 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004099 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004101 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004103 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#ifdef FEAT_RIGHTLEFT
4105 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004106 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107#endif
4108#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004109 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
4111# ifdef FEAT_ARABIC
4112 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004113 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 else
4115# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004116 if (get_keymap_str(curwin, (char_u *)" (%s)",
4117 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004118 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004121 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004122 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (VIsual_active)
4125 {
4126 char *p;
4127
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004128 // Don't concatenate separate words to avoid translation
4129 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 switch ((VIsual_select ? 4 : 0)
4131 + (VIsual_mode == Ctrl_V) * 2
4132 + (VIsual_mode == 'V'))
4133 {
4134 case 0: p = N_(" VISUAL"); break;
4135 case 1: p = N_(" VISUAL LINE"); break;
4136 case 2: p = N_(" VISUAL BLOCK"); break;
4137 case 4: p = N_(" SELECT"); break;
4138 case 5: p = N_(" SELECT LINE"); break;
4139 default: p = N_(" SELECT BLOCK"); break;
4140 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004141 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004143 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004145
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 need_clear = TRUE;
4147 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004148 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004149 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004151 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 need_clear = TRUE;
4153 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004154
4155 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004156 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004158 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 length = msg_col;
4160 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004161 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004164 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004166 else if (redraw_mode)
4167 {
4168 msg_pos_mode();
4169 msg_clr_eos();
4170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004172 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (VIsual_active)
4174 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004176 // If the last window has no status line, the ruler is after the mode
4177 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004178 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004179 win_redr_ruler(lastwin, TRUE, FALSE);
Martin Tournoijba43e762022-10-13 22:12:15 +01004180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004182 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 clear_cmdline = FALSE;
4184
4185 return length;
4186}
4187
4188/*
4189 * Position for a mode message.
4190 */
4191 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004192msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193{
4194 msg_col = 0;
4195 msg_row = Rows - 1;
4196}
4197
4198/*
4199 * Delete mode message. Used when ESC is typed which is expected to end
4200 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004201 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 */
4203 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004204unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
4206 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004207 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 */
4209 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004210 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004212 clearmode();
4213}
4214
4215/*
4216 * Clear the mode message.
4217 */
4218 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004219clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004220{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004221 int save_msg_row = msg_row;
4222 int save_msg_col = msg_col;
4223
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004224 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004225 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004226 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004227 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004228
4229 msg_col = save_msg_col;
4230 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231}
4232
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004233 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004234recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004235{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004236 msg_puts_attr(_("recording"), attr);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004237 if (shortmess(SHM_RECORDING))
4238 return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004239
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004240 char s[4];
4241
4242 sprintf(s, " @%c", reg_recording);
4243 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004244}
4245
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004246/*
4247 * Draw the tab pages line at the top of the Vim window.
4248 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004249 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004250draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004251{
4252 int tabcount = 0;
4253 tabpage_T *tp;
4254 int tabwidth;
4255 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004256 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004257 int attr;
4258 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004259 win_T *cwp;
4260 int wincount;
4261 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004262 int c;
4263 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004264 int attr_sel = HL_ATTR(HLF_TPS);
4265 int attr_nosel = HL_ATTR(HLF_TP);
4266 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004267 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004268 int room;
4269 int use_sep_chars = (t_colors < 8
4270#ifdef FEAT_GUI
4271 && !gui.in_use
4272#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004273#ifdef FEAT_TERMGUICOLORS
4274 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004275#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004276 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004277
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004278 if (ScreenLines == NULL)
4279 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004280 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004281
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004282#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004283 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004284 if (gui_use_tabline())
4285 {
4286 gui_update_tabline();
4287 return;
4288 }
4289#endif
4290
4291 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004292 return;
4293
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004294#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004295 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004296
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004297 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004298 if (*p_tal != NUL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004299 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004300 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004301#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004302 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004303 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004304 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004305
Bram Moolenaar238a5642006-02-21 22:12:05 +00004306 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4307 if (tabwidth < 6)
4308 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004309
Bram Moolenaar238a5642006-02-21 22:12:05 +00004310 attr = attr_nosel;
4311 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004312 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4313 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004314 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004315 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004316
Bram Moolenaar238a5642006-02-21 22:12:05 +00004317 if (tp->tp_topframe == topframe)
4318 attr = attr_sel;
4319 if (use_sep_chars && col > 0)
4320 screen_putchar('|', 0, col++, attr);
4321
4322 if (tp->tp_topframe != topframe)
4323 attr = attr_nosel;
4324
4325 screen_putchar(' ', 0, col++, attr);
4326
4327 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004328 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004329 cwp = curwin;
4330 wp = firstwin;
4331 }
4332 else
4333 {
4334 cwp = tp->tp_curwin;
4335 wp = tp->tp_firstwin;
4336 }
4337
4338 modified = FALSE;
4339 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4340 if (bufIsChanged(wp->w_buffer))
4341 modified = TRUE;
4342 if (modified || wincount > 1)
4343 {
4344 if (wincount > 1)
4345 {
4346 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004347 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004348 if (col + len >= Columns - 3)
4349 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004350 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004351#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004352 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004353#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004354 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004355#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004356 );
4357 col += len;
4358 }
4359 if (modified)
4360 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4361 screen_putchar(' ', 0, col++, attr);
4362 }
4363
4364 room = scol - col + tabwidth - 1;
4365 if (room > 0)
4366 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004367 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004368 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004369 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004370 len = vim_strsize(NameBuff);
4371 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004372 if (has_mbyte)
4373 while (len > room)
4374 {
4375 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004376 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004377 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004378 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004379 {
4380 p += len - room;
4381 len = room;
4382 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004383 if (len > Columns - col - 1)
4384 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004385
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004386 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004387 col += len;
4388 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004389 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004390
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004391 // Store the tab page number in TabPageIdxs[], so that
4392 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004393 ++tabcount;
4394 while (scol < col)
4395 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004396 }
4397
Bram Moolenaar238a5642006-02-21 22:12:05 +00004398 if (use_sep_chars)
4399 c = '_';
4400 else
4401 c = ' ';
4402 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004403
Luuk van Baalba936f62022-12-15 13:15:39 +00004404 // Draw the 'showcmd' information if 'showcmdloc' == "tabline".
4405 if (p_sc && *p_sloc == 't')
4406 {
4407 int width = MIN(10, (int)Columns - col - (tabcount > 1) * 3);
4408
4409 if (width > 0)
4410 screen_puts_len(showcmd_buf, width, 0, (int)Columns
4411 - width - (tabcount > 1) * 2, attr_nosel);
4412 }
4413
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004414 // Put an "X" for closing the current tab if there are several.
Luuk van Baalba936f62022-12-15 13:15:39 +00004415 if (tabcount > 1)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004416 {
4417 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4418 TabPageIdxs[Columns - 1] = -999;
4419 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004420 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004421
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004422 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4423 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004424 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004425}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004426
4427/*
4428 * Get buffer name for "buf" into NameBuff[].
4429 * Takes care of special buffer names and translates special characters.
4430 */
4431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004432get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004433{
4434 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004435 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004436 else
4437 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4438 trans_characters(NameBuff, MAXPATHL);
4439}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004440
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441/*
4442 * Get the character to use in a status line. Get its attributes in "*attr".
4443 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004444 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004445fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446{
4447 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004448
4449#ifdef FEAT_TERMINAL
4450 if (bt_terminal(wp->w_buffer))
4451 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004452 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004453 {
4454 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004455 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004456 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004457 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004458 {
4459 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004460 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004461 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004462 }
4463 else
4464#endif
4465 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004467 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004468 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 else
4471 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004472 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004473 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
Christian Brabandt6a650bf2023-11-08 21:23:29 +01004475 return fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478/*
4479 * Get the character to use in a separator between vertically split windows.
4480 * Get its attributes in "*attr".
4481 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004482 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004483fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004485 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004486 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 return '|';
4488 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004489 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492/*
4493 * Return TRUE if redrawing should currently be done.
4494 */
4495 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004496redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004498#ifdef FEAT_EVAL
4499 if (disable_redraw_for_testing)
4500 return 0;
4501 else
4502#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004503 return ((RedrawingDisabled == 0
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004504#ifdef FEAT_EVAL
4505 || ignore_redraw_flag_for_testing
4506#endif
4507 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508}
4509
4510/*
4511 * Return TRUE if printing messages should currently be done.
4512 */
4513 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004514messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004516 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517}
4518
Bram Moolenaare677df82019-09-02 22:31:11 +02004519/*
4520 * Compute columns for ruler and shown command. 'sc_col' is also used to
4521 * decide what the maximum length of a message on the status line can be.
4522 * If there is a status line for the last window, 'sc_col' is independent
4523 * of 'ru_col'.
4524 */
4525
4526#define COL_RULER 17 // columns needed by standard ruler
4527
4528 void
4529comp_col(void)
4530{
Sean Dewar876f5fb2023-08-17 22:40:05 +02004531 int last_has_status = last_stl_height(FALSE) > 0;
Bram Moolenaare677df82019-09-02 22:31:11 +02004532
4533 sc_col = 0;
4534 ru_col = 0;
4535 if (p_ru)
4536 {
Martin Tournoijba43e762022-10-13 22:12:15 +01004537#ifdef FEAT_STL_OPT
Bram Moolenaare677df82019-09-02 22:31:11 +02004538 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004539#else
Bram Moolenaare677df82019-09-02 22:31:11 +02004540 ru_col = COL_RULER + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004541#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004542 // no last status line, adjust sc_col
4543 if (!last_has_status)
4544 sc_col = ru_col;
4545 }
Sam-programs062141b2024-02-29 17:40:29 +01004546 if (p_sc && *p_sloc == 'l')
Bram Moolenaare677df82019-09-02 22:31:11 +02004547 {
4548 sc_col += SHOWCMD_COLS;
4549 if (!p_ru || last_has_status) // no need for separating space
4550 ++sc_col;
4551 }
4552 sc_col = Columns - sc_col;
4553 ru_col = Columns - ru_col;
4554 if (sc_col <= 0) // screen too narrow, will become a mess
4555 sc_col = 1;
4556 if (ru_col <= 0)
4557 ru_col = 1;
Bram Moolenaare677df82019-09-02 22:31:11 +02004558#ifdef FEAT_EVAL
4559 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4560#endif
4561}
4562
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004563#if defined(FEAT_LINEBREAK) || defined(PROTO)
4564/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004565 * Return the width of the 'number' and 'relativenumber' column.
4566 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004567 * Otherwise it depends on 'numberwidth' and the line count.
4568 */
4569 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004570number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004571{
4572 int n;
4573 linenr_T lnum;
4574
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004575 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004576 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004577 lnum = wp->w_height;
4578 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004579 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004580 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004581
Bram Moolenaar6b314672015-03-20 15:42:10 +01004582 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004583 return wp->w_nrwidth_width;
4584 wp->w_nrwidth_line_count = lnum;
4585
4586 n = 0;
4587 do
4588 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004589 lnum /= 10;
4590 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004591 } while (lnum > 0);
4592
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004593 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004594 if (n < wp->w_p_nuw - 1)
4595 n = wp->w_p_nuw - 1;
4596
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004597# ifdef FEAT_SIGNS
4598 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4599 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004600 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004601 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4602 n = 2;
4603# endif
4604
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004605 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004606 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004607 return n;
4608}
4609#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004610
Bram Moolenaar113e1072019-01-20 15:30:40 +01004611#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004612/*
4613 * Return the current cursor column. This is the actual position on the
4614 * screen. First column is 0.
4615 */
4616 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004617screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004618{
4619 return screen_cur_col;
4620}
4621
4622/*
4623 * Return the current cursor row. This is the actual position on the screen.
4624 * First row is 0.
4625 */
4626 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004627screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004628{
4629 return screen_cur_row;
4630}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004631#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004632
4633/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004634 * Calls mb_ptr2char_adv(p) and returns the character.
4635 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4636 */
4637 static int
4638get_encoded_char_adv(char_u **p)
4639{
4640 char_u *s = *p;
4641
4642 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4643 {
4644 varnumber_T num = 0;
4645 int bytes;
4646 int n;
4647
4648 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4649 {
4650 *p += 2;
4651 n = hexhex2nr(*p);
4652 if (n < 0)
4653 return 0;
4654 num = num * 256 + n;
4655 }
4656 *p += 2;
4657 return num;
4658 }
4659 return mb_ptr2char_adv(p);
4660}
4661
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004662struct charstab
4663{
4664 int *cp;
4665 char *name;
4666};
4667static fill_chars_T fill_chars;
4668static struct charstab filltab[] =
4669{
4670 {&fill_chars.stl, "stl"},
4671 {&fill_chars.stlnc, "stlnc"},
4672 {&fill_chars.vert, "vert"},
4673 {&fill_chars.fold, "fold"},
4674 {&fill_chars.foldopen, "foldopen"},
4675 {&fill_chars.foldclosed, "foldclose"},
4676 {&fill_chars.foldsep, "foldsep"},
4677 {&fill_chars.diff, "diff"},
4678 {&fill_chars.eob, "eob"},
4679 {&fill_chars.lastline, "lastline"},
4680};
4681static lcs_chars_T lcs_chars;
4682static struct charstab lcstab[] =
4683{
4684 {&lcs_chars.eol, "eol"},
4685 {&lcs_chars.ext, "extends"},
4686 {&lcs_chars.nbsp, "nbsp"},
4687 {&lcs_chars.prec, "precedes"},
4688 {&lcs_chars.space, "space"},
4689 {&lcs_chars.tab2, "tab"},
4690 {&lcs_chars.trail, "trail"},
4691 {&lcs_chars.lead, "lead"},
4692#ifdef FEAT_CONCEAL
4693 {&lcs_chars.conceal, "conceal"},
4694#else
4695 {NULL, "conceal"},
4696#endif
zeertzjq1f025b02023-09-30 12:43:07 +02004697 {NULL, "multispace"},
4698 {NULL, "leadmultispace"},
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004699};
4700
zeertzjq6a8d2e12024-01-17 20:54:49 +01004701 static char *
4702field_value_err(char *errbuf, size_t errbuflen, char *fmt, char *field)
4703{
4704 if (errbuf == NULL)
4705 return "";
4706 vim_snprintf(errbuf, errbuflen, _(fmt), field);
4707 return errbuf;
4708}
4709
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004710/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004711 * Handle setting 'listchars' or 'fillchars'.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004712 * "value" points to either the global or the window-local value.
zeertzjqd619d6a2023-05-08 15:56:21 +01004713 * "is_listchars" is TRUE for "listchars" and FALSE for "fillchars".
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004714 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004715 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004716 * Returns error message, NULL if it's OK.
4717 */
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004718 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004719set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
4720 char *errbuf, size_t errbuflen)
Bram Moolenaare677df82019-09-02 22:31:11 +02004721{
zeertzjq1f025b02023-09-30 12:43:07 +02004722 int round, i, len, entries;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004723 char_u *p, *s;
4724 int c1 = 0, c2 = 0, c3 = 0;
4725 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4726 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4727 int multispace_len = 0; // Length of lcs-multispace string
4728 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004729
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004730 struct charstab *tab;
4731
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004732 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004733 {
4734 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004735 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004736 entries = ARRAY_LENGTH(lcstab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004737 if (wp->w_p_lcs[0] == NUL)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004738 value = p_lcs; // local value is empty, use the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004739 }
4740 else
4741 {
4742 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004743 entries = ARRAY_LENGTH(filltab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004744 if (wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004745 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004746 }
4747
4748 // first round: check for valid value, second round: assign values
zeertzjq00624a22023-11-23 20:47:16 +01004749 for (round = 0; round <= (apply ? 1 : 0); ++round)
Bram Moolenaare677df82019-09-02 22:31:11 +02004750 {
4751 if (round > 0)
4752 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004753 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004754 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004755 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004756 for (i = 0; i < entries; ++i)
4757 if (tab[i].cp != NULL)
4758 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004759 lcs_chars.tab1 = NUL;
4760 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004761
Mike Williamsf5785cf2021-09-13 22:17:38 +02004762 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004763 {
4764 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004765 if (lcs_chars.multispace != NULL)
4766 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004767 }
4768 else
4769 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004770
4771 if (lead_multispace_len > 0)
4772 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004773 lcs_chars.leadmultispace =
4774 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004775 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
4776 }
4777 else
4778 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004779 }
4780 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004781 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004782 fill_chars.stl = ' ';
4783 fill_chars.stlnc = ' ';
4784 fill_chars.vert = ' ';
4785 fill_chars.fold = '-';
4786 fill_chars.foldopen = '-';
4787 fill_chars.foldclosed = '+';
4788 fill_chars.foldsep = '|';
4789 fill_chars.diff = '-';
4790 fill_chars.eob = '~';
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01004791 fill_chars.lastline = '@';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004792 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004793 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004794 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02004795 while (*p)
4796 {
4797 for (i = 0; i < entries; ++i)
4798 {
4799 len = (int)STRLEN(tab[i].name);
zeertzjq6a8d2e12024-01-17 20:54:49 +01004800 if (!(STRNCMP(p, tab[i].name, len) == 0 && p[len] == ':'))
zeertzjq1f025b02023-09-30 12:43:07 +02004801 continue;
Bram Moolenaare677df82019-09-02 22:31:11 +02004802
zeertzjq1f025b02023-09-30 12:43:07 +02004803 if (is_listchars && strcmp(tab[i].name, "multispace") == 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004804 {
4805 s = p + len + 1;
4806 if (round == 0)
4807 {
4808 // Get length of lcs-multispace string in first round
4809 last_multispace = p;
4810 multispace_len = 0;
4811 while (*s != NUL && *s != ',')
4812 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004813 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004814 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004815 return field_value_err(errbuf, errbuflen,
4816 e_wrong_character_width_for_field_str,
4817 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004818 ++multispace_len;
4819 }
4820 if (multispace_len == 0)
4821 // lcs-multispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004822 return field_value_err(errbuf, errbuflen,
4823 e_wrong_number_of_characters_for_field_str,
4824 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004825 p = s;
4826 }
4827 else
4828 {
4829 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02004830
zeertzjqf14b8ba2021-09-10 16:58:30 +02004831 while (*s != NUL && *s != ',')
4832 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004833 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004834 if (p == last_multispace)
4835 lcs_chars.multispace[multispace_pos++] = c1;
4836 }
4837 p = s;
4838 }
zeertzjq1f025b02023-09-30 12:43:07 +02004839 break;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004840 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004841
zeertzjq1f025b02023-09-30 12:43:07 +02004842 if (is_listchars && strcmp(tab[i].name, "leadmultispace") == 0)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004843 {
zeertzjq1f025b02023-09-30 12:43:07 +02004844 s = p + len + 1;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004845 if (round == 0)
4846 {
zeertzjqb5f08012022-06-09 13:55:28 +01004847 // get length of lcs-leadmultispace string in first
4848 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004849 last_lmultispace = p;
4850 lead_multispace_len = 0;
4851 while (*s != NUL && *s != ',')
4852 {
4853 c1 = get_encoded_char_adv(&s);
4854 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004855 return field_value_err(errbuf, errbuflen,
4856 e_wrong_character_width_for_field_str,
4857 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004858 ++lead_multispace_len;
4859 }
4860 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01004861 // lcs-leadmultispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004862 return field_value_err(errbuf, errbuflen,
4863 e_wrong_number_of_characters_for_field_str,
4864 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004865 p = s;
4866 }
4867 else
4868 {
4869 int multispace_pos = 0;
4870
4871 while (*s != NUL && *s != ',')
4872 {
4873 c1 = get_encoded_char_adv(&s);
4874 if (p == last_lmultispace)
4875 lcs_chars.leadmultispace[multispace_pos++] = c1;
4876 }
4877 p = s;
4878 }
zeertzjq1f025b02023-09-30 12:43:07 +02004879 break;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004880 }
zeertzjq1f025b02023-09-30 12:43:07 +02004881
4882 c2 = c3 = 0;
4883 s = p + len + 1;
zeertzjq6a8d2e12024-01-17 20:54:49 +01004884 if (*s == NUL)
4885 return field_value_err(errbuf, errbuflen,
4886 e_wrong_number_of_characters_for_field_str,
4887 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004888 c1 = get_encoded_char_adv(&s);
4889 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004890 return field_value_err(errbuf, errbuflen,
4891 e_wrong_character_width_for_field_str,
4892 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004893 if (tab[i].cp == &lcs_chars.tab2)
4894 {
4895 if (*s == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004896 return field_value_err(errbuf, errbuflen,
4897 e_wrong_number_of_characters_for_field_str,
4898 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004899 c2 = get_encoded_char_adv(&s);
4900 if (char2cells(c2) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004901 return field_value_err(errbuf, errbuflen,
4902 e_wrong_character_width_for_field_str,
4903 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004904 if (!(*s == ',' || *s == NUL))
4905 {
4906 c3 = get_encoded_char_adv(&s);
4907 if (char2cells(c3) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004908 return field_value_err(errbuf, errbuflen,
4909 e_wrong_character_width_for_field_str,
4910 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004911 }
4912 }
4913
4914 if (*s == ',' || *s == NUL)
4915 {
4916 if (round > 0)
4917 {
4918 if (tab[i].cp == &lcs_chars.tab2)
4919 {
4920 lcs_chars.tab1 = c1;
4921 lcs_chars.tab2 = c2;
4922 lcs_chars.tab3 = c3;
4923 }
4924 else if (tab[i].cp != NULL)
4925 *(tab[i].cp) = c1;
4926
4927 }
4928 p = s;
4929 break;
4930 }
zeertzjq6a8d2e12024-01-17 20:54:49 +01004931 else
4932 return field_value_err(errbuf, errbuflen,
4933 e_wrong_number_of_characters_for_field_str,
4934 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004935 }
4936
zeertzjq1f025b02023-09-30 12:43:07 +02004937 if (i == entries)
4938 return e_invalid_argument;
4939
Bram Moolenaare677df82019-09-02 22:31:11 +02004940 if (*p == ',')
4941 ++p;
4942 }
4943 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004944
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004945 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004946 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004947 if (is_listchars)
4948 {
4949 vim_free(wp->w_lcs_chars.multispace);
4950 vim_free(wp->w_lcs_chars.leadmultispace);
4951 wp->w_lcs_chars = lcs_chars;
4952 }
4953 else
4954 {
4955 wp->w_fill_chars = fill_chars;
4956 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02004957 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004958
4959 return NULL; // no error
4960}
zeertzjq8ca29b62022-08-09 12:53:14 +01004961
4962/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004963 * Handle the new value of 'fillchars'.
4964 */
4965 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004966set_fillchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4967 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004968{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004969 return set_chars_option(wp, val, FALSE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004970}
4971
4972/*
4973 * Handle the new value of 'listchars'.
4974 */
4975 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004976set_listchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4977 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004978{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004979 return set_chars_option(wp, val, TRUE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004980}
4981
4982/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004983 * Function given to ExpandGeneric() to obtain possible arguments of the
4984 * 'fillchars' option.
4985 */
4986 char_u *
4987get_fillchars_name(expand_T *xp UNUSED, int idx)
4988{
4989 if (idx >= (int)(sizeof(filltab) / sizeof(filltab[0])))
4990 return NULL;
4991
4992 return (char_u*)filltab[idx].name;
4993}
4994
4995/*
4996 * Function given to ExpandGeneric() to obtain possible arguments of the
4997 * 'listchars' option.
4998 */
4999 char_u *
5000get_listchars_name(expand_T *xp UNUSED, int idx)
5001{
5002 if (idx >= (int)(sizeof(lcstab) / sizeof(lcstab[0])))
5003 return NULL;
5004
5005 return (char_u*)lcstab[idx].name;
5006}
5007
5008/*
zeertzjq8ca29b62022-08-09 12:53:14 +01005009 * Check all global and local values of 'listchars' and 'fillchars'.
5010 * Return an untranslated error messages if any of them is invalid, NULL
5011 * otherwise.
5012 */
5013 char *
5014check_chars_options(void)
5015{
5016 tabpage_T *tp;
5017 win_T *wp;
5018
zeertzjq6a8d2e12024-01-17 20:54:49 +01005019 if (set_listchars_option(curwin, p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005020 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005021 if (set_fillchars_option(curwin, p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005022 return e_conflicts_with_value_of_fillchars;
5023 FOR_ALL_TAB_WINDOWS(tp, wp)
5024 {
zeertzjq6a8d2e12024-01-17 20:54:49 +01005025 if (set_listchars_option(wp, wp->w_p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005026 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005027 if (set_fillchars_option(wp, wp->w_p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005028 return e_conflicts_with_value_of_fillchars;
5029 }
5030 return NULL;
5031}