blob: 208b1825411337af18bf315e6f0d04454dab4b81 [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"
455 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200456 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100457screen_line(
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100458 win_T *wp,
459 int row,
460 int coloff,
461 int endcol,
462 int clear_width,
463 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464{
465 unsigned off_from;
466 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000467 unsigned max_off_from;
468 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100471 int force = FALSE; // force update rest of the line
472 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100474 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475#endif
476 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100477 int redraw_next; // redraw_this for next character
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100478#ifdef FEAT_GUI_MSWIN
479 int changed_this; // TRUE if character changed
480 int changed_next; // TRUE if next character changed
481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100483 int char_cells; // 1: normal char
484 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100486 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100487 if (row >= Rows)
488 row = Rows - 1;
489 if (endcol > Columns)
490 endcol = Columns;
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492# ifdef FEAT_CLIPBOARD
493 clip_may_clear_selection(row, row);
494# endif
495
496 off_from = (unsigned)(current_ScreenLine - ScreenLines);
497 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000498 max_off_from = off_from + screen_Columns;
499 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
501#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200502 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100504 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 if (clear_width > 0)
506 {
507 while (col <= endcol && ScreenLines[off_to] == ' '
508 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100509 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 {
511 ++off_to;
512 ++col;
513 }
514 if (col <= endcol)
515 screen_fill(row, row + 1, col + coloff,
516 endcol + coloff + 1, ' ', ' ', 0);
517 }
518 col = endcol + 1;
519 off_to = LineOffset[row] + col + coloff;
520 off_from += col;
521 endcol = (clear_width > 0 ? clear_width : -clear_width);
522 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100523#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100525#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100526 // First char of a popup window may go on top of the right half of a
527 // double-wide character. Clear the left half to avoid it getting the popup
528 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100529 if (coloff > 0 && enc_utf8
530 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200531 && ScreenLinesUC[off_to - 1] != 0
532 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100533 {
534 ScreenLines[off_to - 1] = ' ';
535 ScreenLinesUC[off_to - 1] = 0;
536 screen_char(off_to - 1, row, col + coloff - 1);
537 }
538#endif
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100541#ifdef FEAT_GUI_MSWIN
542 changed_next = redraw_next;
543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
545 while (col < endcol)
546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000548 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 else
550 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552 redraw_this = redraw_next;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100553 redraw_next = force || char_needs_redraw(off_from + char_cells,
554 off_to + char_cells, endcol - col - char_cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556#ifdef FEAT_GUI
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100557# ifdef FEAT_GUI_MSWIN
558 changed_this = changed_next;
559 changed_next = redraw_next;
560# endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100561 // If the next character was bold, then redraw the current character to
562 // remove any pixels that might have spilt over into us. This only
563 // happens in the GUI.
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100564 // With MS-Windows antialiasing may also cause pixels to spill over
565 // from a previous character, no matter attributes, always redraw if a
566 // character changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 if (redraw_next && gui.in_use)
568 {
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100569# ifndef FEAT_GUI_MSWIN
Bram Moolenaarb9081882022-07-09 04:56:24 +0100570 hl = ScreenAttrs[off_to + char_cells];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000571 if (hl > HL_ALL)
572 hl = syn_attr2attr(hl);
573 if (hl & HL_BOLD)
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 redraw_this = TRUE;
576 }
577#endif
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100578 // Do not redraw if under the popup menu.
579 if (redraw_this && skip_for_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200580 redraw_this = FALSE;
Bram Moolenaar393f8d62022-10-02 14:28:30 +0100581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 if (redraw_this)
583 {
584 /*
585 * Special handling when 'xs' termcap flag set (hpterm):
586 * Attributes for characters are stored at the position where the
587 * cursor is when writing the highlighting code. The
588 * start-highlighting code must be written with the cursor on the
589 * first highlighted character. The stop-highlighting code must
590 * be written with the cursor just after the last highlighted
591 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100592 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 * to clear the rest of the line, and force redrawing it
594 * completely.
595 */
596 if ( p_wiv
597 && !force
598#ifdef FEAT_GUI
599 && !gui.in_use
600#endif
601 && ScreenAttrs[off_to] != 0
602 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
603 {
604 /*
605 * Need to remove highlighting attributes here.
606 */
607 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100608 out_str(T_CE); // clear rest of this screen line
609 screen_start(); // don't know where cursor is now
610 force = TRUE; // force redraw of rest of the line
611 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613 /*
614 * If the previous character was highlighted, need to stop
615 * highlighting at this character.
616 */
617 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
618 {
619 screen_attr = ScreenAttrs[off_to - 1];
620 term_windgoto(row, col + coloff);
621 screen_stop_highlight();
622 }
623 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100624 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (enc_dbcs != 0)
627 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100628 // Check if overwriting a double-byte with a single-byte or
629 // the other way around requires another character to be
630 // redrawn. For UTF-8 this isn't needed, because comparing
631 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (char_cells == 1
633 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000634 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100636 // Writing a single-cell character over a double-cell
637 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ScreenLines[off_to + 1] = 0;
639 redraw_next = TRUE;
640 }
641 else if (char_cells == 2
642 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000643 && (*mb_off2cells)(off_to, max_off_to) == 1
644 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100646 // Writing the second half of a double-cell character over
647 // a double-cell character: need to redraw the second
648 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 ScreenLines[off_to + 2] = 0;
650 redraw_next = TRUE;
651 }
652
653 if (enc_dbcs == DBCS_JPNU)
654 ScreenLines2[off_to] = ScreenLines2[off_from];
655 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100656 // When writing a single-width character over a double-width
657 // character and at the end of the redrawn text, need to clear out
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000658 // the right half of the old character.
659 // Also required when writing the right half of a double-width
660 // char over the left half of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (has_mbyte && col + char_cells == endcol
662 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000663 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000665 && (*mb_off2cells)(off_to, max_off_to) == 1
666 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
669 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (enc_utf8)
671 {
672 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
673 if (ScreenLinesUC[off_from] != 0)
674 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000675 int i;
676
677 for (i = 0; i < Screen_mco; ++i)
678 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 }
681 if (char_cells == 2)
682 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
684#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100685 // The bold trick makes a single column of pixels appear in the
686 // next character. When a bold character is removed, the next
687 // character should be redrawn too. This happens for our own GUI
688 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (
690# ifdef FEAT_GUI
691 gui.in_use
692# endif
693# if defined(FEAT_GUI) && defined(UNIX)
694 ||
695# endif
696# ifdef UNIX
697 term_is_xterm
698# endif
699 )
700 {
701 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000702 if (hl > HL_ALL)
703 hl = syn_attr2attr(hl);
704 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 redraw_next = TRUE;
706 }
707#endif
Bram Moolenaar0c502d22022-10-11 12:48:44 +0100708#ifdef FEAT_GUI_MSWIN
709 // MS-Windows antialiasing may spill over to the next character,
710 // redraw that one if this one changed, no matter attributes.
711 if (gui.in_use && changed_this)
712 redraw_next = TRUE;
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100715
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100716 // For simplicity set the attributes of second half of a
717 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000718 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000720
721 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 screen_char(off_to, row, col + coloff);
725 }
726 else if ( p_wiv
727#ifdef FEAT_GUI
728 && !gui.in_use
729#endif
730 && col + coloff > 0)
731 {
732 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
733 {
734 /*
735 * Don't output stop-highlight when moving the cursor, it will
736 * stop the highlighting when it should continue.
737 */
738 screen_attr = 0;
739 }
740 else if (screen_attr != 0)
741 screen_stop_highlight();
742 }
743
Bram Moolenaarb9081882022-07-09 04:56:24 +0100744 ScreenCols[off_to] = ScreenCols[off_from];
745 if (char_cells == 2)
zeertzjq99941602023-08-19 13:08:50 +0200746 ScreenCols[off_to + 1] = ScreenCols[off_from + 1];
Bram Moolenaarb9081882022-07-09 04:56:24 +0100747
748 off_to += char_cells;
749 off_from += char_cells;
750 col += char_cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100753 if (clear_next && !skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100755 // Clear the second half of a double-wide character of which the left
756 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 ScreenLines[off_to] = ' ';
758 if (enc_utf8)
759 ScreenLinesUC[off_to] = 0;
760 screen_char(off_to, row, col + coloff);
761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763 if (clear_width > 0
764#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200765 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766#endif
767 )
768 {
769#ifdef FEAT_GUI
770 int startCol = col;
771#endif
772
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100773 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 while (col < clear_width && ScreenLines[off_to] == ' '
775 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100776 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
Bram Moolenaarb9081882022-07-09 04:56:24 +0100778 ScreenCols[off_to] = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 ++off_to;
780 ++col;
781 }
782 if (col < clear_width)
783 {
784#ifdef FEAT_GUI
785 /*
786 * In the GUI, clearing the rest of the line may leave pixels
787 * behind if the first character cleared was bold. Some bold
788 * fonts spill over the left. In this case we redraw the previous
789 * character too. If we didn't skip any blanks above, then we
790 * only redraw if the character wasn't already redrawn anyway.
791 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000792 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
794 hl = ScreenAttrs[off_to];
795 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000796 {
797 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100798
Bram Moolenaar9c697322006-10-09 20:11:17 +0000799 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100800 // for utf-8, ScreenLines[char_offset + 1] == 0 means
801 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000802 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
803 else if (enc_dbcs != 0)
804 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100805 // find previous character by counting from first
806 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000807 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000808 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000809
810 while (off < off_to)
811 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000812 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000813 off += prev_cells;
814 }
815 }
816
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100817 if (!skip_for_popup(row, col + coloff - prev_cells))
818 {
819 if (enc_dbcs != 0 && prev_cells > 1)
820 screen_char_2(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000821 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100822 else
823 screen_char(off_to - prev_cells, row,
Bram Moolenaar9c697322006-10-09 20:11:17 +0000824 col + coloff - prev_cells);
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100825 }
Bram Moolenaar9c697322006-10-09 20:11:17 +0000826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
828#endif
829 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
830 ' ', ' ', 0);
Bram Moolenaarb9081882022-07-09 04:56:24 +0100831 while (col < clear_width)
832 {
833 ScreenCols[off_to++] = MAXCOL;
834 ++col;
835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
837 }
838
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200839 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100840#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200841 && !(flags & SLF_POPUP) // no separator for popup window
842#endif
843 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200845 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200846 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200847 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +0100849 if (!skip_for_popup(row, col + coloff))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200851 int c;
852
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100853 c = fillchar_vsep(&hl, wp);
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200854 if (ScreenLines[off_to] != (schar_T)c
855 || (enc_utf8 && (int)ScreenLinesUC[off_to]
856 != (c >= 0x80 ? c : 0))
857 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200859 ScreenLines[off_to] = c;
860 ScreenAttrs[off_to] = hl;
861 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200863 if (c >= 0x80)
864 {
865 ScreenLinesUC[off_to] = c;
866 ScreenLinesC[0][off_to] = 0;
867 }
868 else
869 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200871 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874 }
875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 LineWraps[row] = FALSE;
877 }
878}
879
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000880#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000882 * Mirror text "str" for right-left displaying.
883 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000885 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100886rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
888 char_u *p1, *p2;
889 int t;
890
891 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
892 {
893 t = *p1;
894 *p1 = *p2;
895 *p2 = t;
896 }
897}
898#endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * Draw the verticap separator right of window "wp" starting with line "row".
902 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200903 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100904draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
906 int hl;
907 int c;
908
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000909 if (!wp->w_vsep_width)
910 return;
911
912 // draw the vertical separator right of this window
913 c = fillchar_vsep(&hl, wp);
914 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
915 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
916 c, ' ', hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 * Return TRUE if the status line of window "wp" is connected to the status
921 * line of the window right of it. If not, then it's a vertical separator.
922 * Only call if (wp->w_vsep_width != 0).
923 */
924 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100925stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
927 frame_T *fr;
928
929 fr = wp->w_frame;
930 while (fr->fr_parent != NULL)
931 {
932 if (fr->fr_parent->fr_layout == FR_COL)
933 {
934 if (fr->fr_next != NULL)
935 break;
936 }
937 else
938 {
939 if (fr->fr_next != NULL)
940 return TRUE;
941 }
942 fr = fr->fr_parent;
943 }
944 return FALSE;
945}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948/*
949 * Get the value to show for the language mappings, active 'keymap'.
950 */
951 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100952get_keymap_str(
953 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100954 char_u *fmt, // format string containing one %s item
955 char_u *buf, // buffer for the result
956 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
958 char_u *p;
959
960 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
961 return FALSE;
962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#ifdef FEAT_EVAL
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000964 buf_T *old_curbuf = curbuf;
965 win_T *old_curwin = curwin;
966 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000968 curbuf = wp->w_buffer;
969 curwin = wp;
970 STRCPY(buf, "b:keymap_name"); // must be writable
971 ++emsg_skip;
972 s = p = eval_to_string(buf, FALSE, FALSE);
973 --emsg_skip;
974 curbuf = old_curbuf;
975 curwin = old_curwin;
976 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000978 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979#ifdef FEAT_KEYMAP
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000980 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
981 p = wp->w_buffer->b_p_keymap;
982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000984 p = (char_u *)"lang";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000986 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
987 buf[0] = NUL;
988#ifdef FEAT_EVAL
989 vim_free(s);
990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 return buf[0] != NUL;
992}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994#if defined(FEAT_STL_OPT) || defined(PROTO)
995/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000996 * Redraw the status line or ruler of window "wp".
997 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200999 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001000win_redr_custom(
1001 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001002 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001004 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 int attr;
1006 int curattr;
1007 int row;
1008 int col = 0;
1009 int maxwidth;
1010 int width;
1011 int n;
1012 int len;
1013 int fillchar;
1014 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001015 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 char_u *p;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001017 char_u *opt_name;
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001018 int opt_scope = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001019 stl_hlrec_T *hltab;
1020 stl_hlrec_T *tabtab;
Bram Moolenaar61452852011-02-01 18:01:11 +01001021 win_T *ewp;
1022 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001024 // There is a tiny chance that this gets called recursively: When
1025 // redrawing a status line triggers redrawing the ruler or tabline.
1026 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001027 if (entered)
1028 return;
1029 entered = TRUE;
1030
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001031 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001032 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001034 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001035 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001036 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001037 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001038 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001039 maxwidth = Columns;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001040 opt_name = (char_u *)"tabline";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001042 else
1043 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001044 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001045 fillchar = fillchar_status(&attr, wp);
Sean Dewarfc8a6012023-04-17 16:41:20 +01001046 int in_status_line = wp->w_status_height != 0;
1047 maxwidth = in_status_line ? wp->w_width : Columns;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001048
1049 if (draw_ruler)
1050 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001051 stl = p_ruf;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001052 opt_name = (char_u *)"rulerformat";
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001053 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001054 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001055 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001056 if (*++stl == '-')
1057 stl++;
1058 if (atoi((char *)stl))
1059 while (VIM_ISDIGIT(*stl))
1060 stl++;
1061 if (*stl++ != '(')
1062 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001063 }
Sean Dewarfc8a6012023-04-17 16:41:20 +01001064 col = ru_col - (Columns - maxwidth);
1065 if (col < (maxwidth + 1) / 2)
1066 col = (maxwidth + 1) / 2;
1067 maxwidth -= col;
1068 if (!in_status_line)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001069 {
1070 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001071 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001072 fillchar = ' ';
1073 attr = 0;
1074 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001075 }
1076 else
1077 {
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001078 opt_name = (char_u *)"statusline";
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001079 if (*wp->w_p_stl != NUL)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001080 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001081 stl = wp->w_p_stl;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001082 opt_scope = OPT_LOCAL;
1083 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001084 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001085 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001086 }
1087
Sean Dewarfc8a6012023-04-17 16:41:20 +01001088 if (in_status_line)
1089 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001090 }
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001093 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001095 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1096 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001097 ewp = wp == NULL ? curwin : wp;
1098 p_crb_save = ewp->w_p_crb;
1099 ewp->w_p_crb = FALSE;
1100
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001101 // Make a copy, because the statusline may include a function call that
1102 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001103 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001104 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Luuk van Baal7b224fd2022-11-07 12:16:51 +00001105 stl, opt_name, opt_scope,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001106 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001107 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001108 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001110 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001111 p = transstr(buf);
1112 if (p != NULL)
1113 {
1114 vim_strncpy(buf, p, sizeof(buf) - 1);
1115 vim_free(p);
1116 }
1117
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001118 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001119 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001120 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 ++width;
1124 }
1125 buf[len] = NUL;
1126
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001127 /*
1128 * Draw each snippet with the specified highlighting.
1129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 curattr = attr;
1131 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001132 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001134 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 screen_puts_len(p, len, row, col, curattr);
1136 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001137 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001139 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001141 else if (hltab[n].userhl < 0)
1142 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001143#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001144 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1145 && wp->w_status_height != 0)
1146 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001147 else if (wp != NULL && bt_terminal(wp->w_buffer)
1148 && wp->w_status_height != 0)
1149 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001150#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001151 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001152 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001154 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001157
1158 if (wp == NULL)
1159 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001160 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001161 col = 0;
1162 len = 0;
1163 p = buf;
1164 fillchar = 0;
1165 for (n = 0; tabtab[n].start != NULL; n++)
1166 {
1167 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1168 while (col < len)
1169 TabPageIdxs[col++] = fillchar;
1170 p = tabtab[n].start;
1171 fillchar = tabtab[n].userhl;
1172 }
1173 while (col < Columns)
1174 TabPageIdxs[col++] = fillchar;
1175 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001176
1177theend:
1178 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179}
1180
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001181#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
1183/*
1184 * Output a single character directly to the screen and update ScreenLines.
1185 */
1186 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001187screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 char_u buf[MB_MAXBYTES + 1];
1190
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001191 if (has_mbyte)
1192 buf[(*mb_char2bytes)(c, buf)] = NUL;
1193 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001194 {
1195 buf[0] = c;
1196 buf[1] = NUL;
1197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 screen_puts(buf, row, col, attr);
1199}
1200
1201/*
zeertzjq47eec672023-06-01 20:26:55 +01001202 * Get a single character directly from ScreenLines into "bytes", which must
1203 * have a size of "MB_MAXBYTES + 1".
1204 * If "attrp" is not NULL, return the character's attribute in "*attrp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 */
1206 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001207screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208{
1209 unsigned off;
1210
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001211 // safety check
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001212 if (ScreenLines == NULL || row >= screen_Rows || col >= screen_Columns)
1213 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001215 off = LineOffset[row] + col;
zeertzjq47eec672023-06-01 20:26:55 +01001216 if (attrp != NULL)
1217 *attrp = ScreenAttrs[off];
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001218 bytes[0] = ScreenLines[off];
1219 bytes[1] = NUL;
1220
1221 if (enc_utf8 && ScreenLinesUC[off] != 0)
1222 bytes[utfc_char2bytes(off, bytes)] = NUL;
1223 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1224 {
1225 bytes[0] = ScreenLines[off];
1226 bytes[1] = ScreenLines2[off];
1227 bytes[2] = NUL;
1228 }
1229 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1230 {
1231 bytes[1] = ScreenLines[off + 1];
1232 bytes[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
1234}
1235
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001236/*
1237 * Return TRUE if composing characters for screen posn "off" differs from
1238 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001239 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001240 */
1241 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001242screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001243{
1244 int i;
1245
1246 for (i = 0; i < Screen_mco; ++i)
1247 {
1248 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1249 return TRUE;
1250 if (u8cc[i] == 0)
1251 break;
1252 }
1253 return FALSE;
1254}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256/*
1257 * Put string '*text' on the screen at position 'row' and 'col', with
1258 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1259 * Note: only outputs within one row, message is truncated at screen boundary!
1260 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1261 */
1262 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001263screen_puts(
1264 char_u *text,
1265 int row,
1266 int col,
1267 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 screen_puts_len(text, -1, row, col, attr);
1270}
1271
1272/*
1273 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1274 * a NUL.
1275 */
1276 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001277screen_puts_len(
1278 char_u *text,
1279 int textlen,
1280 int row,
1281 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001282 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001284 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 unsigned off;
1286 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001287 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001289 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 int mbyte_blen = 1;
1291 int mbyte_cells = 1;
1292 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001293 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001295#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001296 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001297 int pc, nc, nc1;
1298 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001300 int force_redraw_this;
1301 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001302 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001304 // Safety check. The check for negative row and column is to fix issue
1305 // #4102. TODO: find out why row/col could be negative.
1306 if (ScreenLines == NULL
1307 || row >= screen_Rows || row < 0
1308 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001310 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001312 // When drawing over the right half of a double-wide char clear out the
1313 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001314 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001315#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001316 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001317#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001318 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001319 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001320 if (!skip_for_popup(row, col - 1))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001321 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001322 ScreenLines[off - 1] = ' ';
1323 ScreenAttrs[off - 1] = 0;
1324 if (enc_utf8)
1325 {
1326 ScreenLinesUC[off - 1] = 0;
1327 ScreenLinesC[0][off - 1] = 0;
1328 }
1329 // redraw the previous cell, make it empty
1330 screen_char(off - 1, row, col - 1);
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001331 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001332 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001333 force_redraw_next = TRUE;
1334 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001335
Bram Moolenaar367329b2007-08-30 11:53:22 +00001336 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001337 while (col < screen_Columns
1338 && (len < 0 || (int)(ptr - text) < len)
1339 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 {
1341 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001342 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (has_mbyte)
1344 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001345 mbyte_blen = enc_utf8 && len > 0
1346 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1347 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1349 mbyte_cells = 1;
1350 else if (enc_dbcs != 0)
1351 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001352 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001354 u8c = len >= 0
1355 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1356 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001358#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1360 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001361 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1363 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001364 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 nc = NUL;
1366 nc1 = NUL;
1367 }
1368 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001369 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001370 nc = len >= 0
1371 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1372 (int)((text + len) - ptr - mbyte_blen))
1373 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 nc1 = pcc[0];
1375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 pc = prev_c;
1377 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001378 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380 else
1381 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001382#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001383 if (col + mbyte_cells > screen_Columns)
1384 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001385 // Only 1 cell left, but character requires 2 cells:
1386 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001387 c = '>';
1388 mbyte_cells = 1;
1389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001393 force_redraw_this = force_redraw_next;
1394 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001395
1396 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 || (mbyte_cells == 2
1398 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1399 || (enc_dbcs == DBCS_JPNU
1400 && c == 0x8e
1401 && ScreenLines2[off] != ptr[1])
1402 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001403 && (ScreenLinesUC[off] !=
1404 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1405 || (ScreenLinesUC[off] != 0
1406 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001408 || exmode_active;
1409
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001410 if ((need_redraw || force_redraw_this) && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
1412#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001413 // The bold trick makes a single row of pixels appear in the next
1414 // character. When a bold character is removed, the next
1415 // character should be redrawn too. This happens for our own GUI
1416 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001417 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418# ifdef FEAT_GUI
1419 gui.in_use
1420# endif
1421# if defined(FEAT_GUI) && defined(UNIX)
1422 ||
1423# endif
1424# ifdef UNIX
1425 term_is_xterm
1426# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001427 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001429 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001431 if (n > HL_ALL)
1432 n = syn_attr2attr(n);
1433 if (n & HL_BOLD)
1434 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
1436#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001437 // When at the end of the text and overwriting a two-cell
1438 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001439 // cell. Also when overwriting the left half of a two-cell char
1440 // with the right half of a two-cell char. Do this only once
1441 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (clear_next_cell)
1443 clear_next_cell = FALSE;
1444 else if (has_mbyte
1445 && (len < 0 ? ptr[mbyte_blen] == NUL
1446 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001447 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001449 && (*mb_off2cells)(off, max_off) == 1
1450 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 clear_next_cell = TRUE;
1452
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001453 // Make sure we never leave a second byte of a double-byte behind,
1454 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001456 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001458 && (*mb_off2cells)(off, max_off) == 1
1459 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 ScreenLines[off] = c;
1462 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001463 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (enc_utf8)
1465 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001466 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 ScreenLinesUC[off] = 0;
1468 else
1469 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 int i;
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001473 for (i = 0; i < Screen_mco; ++i)
1474 {
1475 ScreenLinesC[i][off] = u8cc[i];
1476 if (u8cc[i] == 0)
1477 break;
1478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480 if (mbyte_cells == 2)
1481 {
1482 ScreenLines[off + 1] = 0;
1483 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001484 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
1486 screen_char(off, row, col);
1487 }
1488 else if (mbyte_cells == 2)
1489 {
1490 ScreenLines[off + 1] = ptr[1];
1491 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001492 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 screen_char_2(off, row, col);
1494 }
1495 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1496 {
1497 ScreenLines2[off] = ptr[1];
1498 screen_char(off, row, col);
1499 }
1500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 screen_char(off, row, col);
1502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (has_mbyte)
1504 {
1505 off += mbyte_cells;
1506 col += mbyte_cells;
1507 ptr += mbyte_blen;
1508 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001509 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001510 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001511 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001513 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001514 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
1519 ++off;
1520 ++col;
1521 ++ptr;
1522 }
1523 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001524
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001525 // If we detected the next character needs to be redrawn, but the text
1526 // doesn't extend up to there, update the character here.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01001527 if (force_redraw_next && col < screen_Columns && !skip_for_popup(row, col))
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001528 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001529 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1530 screen_char_2(off, row, col);
1531 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001532 screen_char(off, row, col);
1533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534}
1535
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001536#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001538 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001540 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001541start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001543 if (!p_hls || no_hlsearch)
1544 return;
1545
1546 end_search_hl(); // just in case it wasn't called before
1547 last_pat_prog(&screen_search_hl.rm);
1548 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549}
1550
1551/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001552 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001554 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001555end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001557 if (screen_search_hl.rm.regprog == NULL)
1558 return;
1559
1560 vim_regfree(screen_search_hl.rm.regprog);
1561 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001563#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001564
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001566screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
1568 attrentry_T *aep = NULL;
1569
1570 screen_attr = attr;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001571 if (!full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001572#ifdef MSWIN
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001573 || !termcap_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001575 )
1576 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001578#ifdef FEAT_GUI
1579 if (gui.in_use)
1580 {
1581 char buf[20];
1582
1583 // The GUI handles this internally.
1584 sprintf(buf, "\033|%dh", attr);
1585 OUT_STR(buf);
1586 return;
1587 }
1588#endif
1589
1590 if (attr > HL_ALL) // special HL attr.
1591 {
1592 if (IS_CTERM)
1593 aep = syn_cterm_attr2entry(attr);
1594 else
1595 aep = syn_term_attr2entry(attr);
1596 if (aep == NULL) // did ":syntax clear"
1597 attr = 0;
1598 else
1599 attr = aep->ae_attr;
1600 }
1601#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1602 if (use_vtp())
1603 {
1604 guicolor_T defguifg, defguibg;
1605 int defctermfg, defctermbg;
1606
1607 // If FG and BG are unset, the color is undefined when
1608 // BOLD+INVERSE. Use Normal as the default value.
1609 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1610 &defguibg);
1611
1612 if (p_tgc)
1613 {
1614 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1615 term_fg_rgb_color(defguifg);
1616 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1617 term_bg_rgb_color(defguibg);
1618 }
1619 else if (t_colors >= 256)
1620 {
1621 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1622 term_fg_color(defctermfg);
1623 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1624 term_bg_color(defctermbg);
1625 }
1626 }
1627#endif
1628 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
1629 out_str(T_MD);
1630 else if (aep != NULL && cterm_normal_fg_bold && (
1631#ifdef FEAT_TERMGUICOLORS
1632 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1633 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1634 :
1635#endif
1636 t_colors > 1 && aep->ae_u.cterm.fg_color))
1637 // If the Normal FG color has BOLD attribute and the new HL
1638 // has a FG color defined, clear BOLD.
1639 out_str(T_ME);
1640 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
1641 out_str(T_SO);
1642 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
1643 out_str(T_UCS);
1644 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1645 out_str(T_USS);
1646 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1647 out_str(T_DS);
1648 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1649 out_str(T_CDS);
1650 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1651 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1652 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1653 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1654 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
1655 && *T_US != NUL)
1656 out_str(T_US);
1657 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
1658 out_str(T_CZH);
1659 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
1660 out_str(T_MR);
1661 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
1662 out_str(T_STS);
1663
1664 /*
1665 * Output the color or start string after bold etc., in case the
1666 * bold etc. override the color setting.
1667 */
1668 if (aep != NULL)
1669 {
PMuncha606f3a2023-11-15 15:35:49 +01001670 if (aep->ae_u.cterm.font > 0 && aep->ae_u.cterm.font < 12)
1671 term_font(aep->ae_u.cterm.font);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001672#ifdef FEAT_TERMGUICOLORS
1673 // When 'termguicolors' is set but fg or bg is unset,
1674 // fall back to the cterm colors. This helps for SpellBad,
1675 // where the GUI uses a red undercurl.
1676 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
1677 {
1678 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
1679 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681 else
1682#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001683 if (t_colors > 1)
1684 {
1685 if (aep->ae_u.cterm.fg_color)
1686 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1687 }
1688#ifdef FEAT_TERMGUICOLORS
1689 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001691 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
1692 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
1693 }
1694 else
1695#endif
1696 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001698 if (aep->ae_u.cterm.bg_color)
1699 term_bg_color(aep->ae_u.cterm.bg_color - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001701#ifdef FEAT_TERMGUICOLORS
1702 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1703 {
1704 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1705 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1706 }
1707 else
1708#endif
1709 if (t_colors > 1)
Bram Moolenaara050b942019-12-02 21:35:31 +01001710 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001711 if (aep->ae_u.cterm.ul_color)
1712 term_ul_color(aep->ae_u.cterm.ul_color - 1);
Bram Moolenaara050b942019-12-02 21:35:31 +01001713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001715 if (!IS_CTERM)
1716 {
1717 if (aep->ae_u.term.start != NULL)
1718 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 }
1721}
1722
1723 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001724screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001726 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001727#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001728 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001732#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 && termcap_active
1734#endif
1735 )
1736 {
1737#ifdef FEAT_GUI
1738 if (gui.in_use)
1739 {
1740 char buf[20];
1741
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001742 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001743 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 OUT_STR(buf);
1745 }
1746 else
1747#endif
1748 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001749 int is_under;
1750
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001751 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
1753 attrentry_T *aep;
1754
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001755 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 {
1757 /*
1758 * Assume that t_me restores the original colors!
1759 */
1760 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001761 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001762#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001763 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1764 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001765# ifdef FEAT_VTP
1766 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1767# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001768 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001769#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001770 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001771#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001772 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
1773 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001774# ifdef FEAT_VTP
1775 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
1776# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001777 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001778#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001779 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001781#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1782 if (use_vtp())
1783 {
1784 if (do_ME_fg && do_ME_bg)
1785 do_ME = TRUE;
1786
1787 // FG and BG cannot be separated in T_ME, which is not
1788 // efficient.
1789 if (!do_ME && do_ME_fg)
1790 out_str((char_u *)"\033|39m"); // restore FG
1791 if (!do_ME && do_ME_bg)
1792 out_str((char_u *)"\033|49m"); // restore BG
1793 }
1794 else
1795 {
1796 // Process FG and BG at once.
1797 if (!do_ME)
1798 do_ME = do_ME_fg | do_ME_bg;
1799 }
1800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
1802 else
1803 {
1804 aep = syn_term_attr2entry(screen_attr);
1805 if (aep != NULL && aep->ae_u.term.stop != NULL)
1806 {
1807 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
1808 do_ME = TRUE;
1809 else
1810 out_str(aep->ae_u.term.stop);
1811 }
1812 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001813 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 screen_attr = 0;
1815 else
1816 screen_attr = aep->ae_attr;
1817 }
1818
1819 /*
1820 * Often all ending-codes are equal to T_ME. Avoid outputting the
1821 * same sequence several times.
1822 */
1823 if (screen_attr & HL_STANDOUT)
1824 {
1825 if (STRCMP(T_SE, T_ME) == 0)
1826 do_ME = TRUE;
1827 else
1828 out_str(T_SE);
1829 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001830 is_under = (screen_attr & (HL_UNDERCURL
1831 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
1832 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001833 {
1834 if (STRCMP(T_UCE, T_ME) == 0)
1835 do_ME = TRUE;
1836 else
1837 out_str(T_UCE);
1838 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01001839 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
1841 if (STRCMP(T_UE, T_ME) == 0)
1842 do_ME = TRUE;
1843 else
1844 out_str(T_UE);
1845 }
1846 if (screen_attr & HL_ITALIC)
1847 {
1848 if (STRCMP(T_CZR, T_ME) == 0)
1849 do_ME = TRUE;
1850 else
1851 out_str(T_CZR);
1852 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001853 if (screen_attr & HL_STRIKETHROUGH)
1854 {
1855 if (STRCMP(T_STE, T_ME) == 0)
1856 do_ME = TRUE;
1857 else
1858 out_str(T_STE);
1859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
1861 out_str(T_ME);
1862
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001863#ifdef FEAT_TERMGUICOLORS
1864 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001866 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001867 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001868 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001869 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02001870 if (cterm_normal_ul_gui_color != INVALCOLOR)
1871 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001872 }
1873 else
1874#endif
1875 {
1876 if (t_colors > 1)
1877 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001878 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001879 if (cterm_normal_fg_color != 0)
1880 term_fg_color(cterm_normal_fg_color - 1);
1881 if (cterm_normal_bg_color != 0)
1882 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02001883 if (cterm_normal_ul_color != 0)
1884 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001885 if (cterm_normal_fg_bold)
1886 out_str(T_MD);
1887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
1889 }
1890 }
1891 screen_attr = 0;
1892}
1893
1894/*
1895 * Reset the colors for a cterm. Used when leaving Vim.
1896 * The machine specific code may override this again.
1897 */
1898 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001899reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001901 if (!IS_CTERM)
1902 return;
1903
1904 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001905#ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001906 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
1907 || cterm_normal_bg_gui_color != INVALCOLOR)
1908 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001909#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
1913 out_str(T_OP);
1914 screen_attr = -1;
1915 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00001916 if (cterm_normal_fg_bold)
1917 {
1918 out_str(T_ME);
1919 screen_attr = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921}
1922
1923/*
1924 * Put character ScreenLines["off"] on the screen at position "row" and "col",
1925 * using the attributes from ScreenAttrs["off"].
1926 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001927 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001928screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
1930 int attr;
1931
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001932 // Check for illegal values, just in case (could happen just after
1933 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (row >= screen_Rows || col >= screen_Columns)
1935 return;
1936
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001937 // Outputting a character in the last cell on the screen may scroll the
1938 // screen up. Only do it when the "xn" termcap property is set, otherwise
1939 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01001940 if (*T_XN == NUL
1941 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001943 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 && !cmdmsg_rl
1945#endif
1946 )
1947 {
1948 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001949 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 return;
1951 }
1952
1953 /*
1954 * Stop highlighting first, so it's easier to move the cursor.
1955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (screen_char_attr != 0)
1957 attr = screen_char_attr;
1958 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 attr = ScreenAttrs[off];
1960 if (screen_attr != attr)
1961 screen_stop_highlight();
1962
1963 windgoto(row, col);
1964
1965 if (screen_attr != attr)
1966 screen_start_highlight(attr);
1967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (enc_utf8 && ScreenLinesUC[off] != 0)
1969 {
1970 char_u buf[MB_MAXBYTES + 1];
1971
Bram Moolenaarcb070082016-04-02 22:14:51 +02001972 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001973 {
1974 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01001975#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001976 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001977#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001978 )
1979 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001980 // Clear the two screen cells. If the character is actually
1981 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001982 out_str((char_u *)" ");
1983 term_windgoto(row, col);
1984 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001985 // not sure where the cursor is after drawing the ambiguous width
1986 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02001987 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001988 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02001989 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001991
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001992 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01001993 buf[utfc_char2bytes(off, buf)] = NUL;
1994 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
1996 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002000 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2002 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004
2005 screen_cur_col++;
2006}
2007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008/*
2009 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2010 * on the screen at position 'row' and 'col'.
2011 * The attributes of the first byte is used for all. This is required to
2012 * output the two bytes of a double-byte character with nothing in between.
2013 */
2014 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002015screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002017 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2019 return;
2020
dundargocc57b5bc2022-11-02 13:30:51 +00002021 // Outputting the last character on the screen may scroll the screen up.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002022 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2024 {
2025 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002026 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 return;
2028 }
2029
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002030 // Output the first byte normally (positions the cursor), then write the
2031 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 screen_char(off, row, col);
2033 out_char(ScreenLines[off + 1]);
2034 ++screen_cur_col;
2035}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037/*
2038 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2039 * This uses the contents of ScreenLines[] and doesn't change it.
2040 */
2041 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002042screen_draw_rectangle(
2043 int row,
2044 int col,
2045 int height,
2046 int width,
2047 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
2049 int r, c;
2050 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002051 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002053 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002054 if (ScreenLines == NULL)
2055 return;
2056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (invert)
2058 screen_char_attr = HL_INVERSE;
2059 for (r = row; r < row + height; ++r)
2060 {
2061 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002062 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 for (c = col; c < col + width; ++c)
2064 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002065 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002067 if (!skip_for_popup(r, c))
2068 screen_char_2(off + c, r, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 ++c;
2070 }
2071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002073 if (!skip_for_popup(r, c))
2074 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002075 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
2078 }
2079 }
2080 screen_char_attr = 0;
2081}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083/*
2084 * Redraw the characters for a vertically split window.
2085 */
2086 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002087redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
2089 int col;
2090 int width;
2091
2092# ifdef FEAT_CLIPBOARD
2093 clip_may_clear_selection(row, end - 1);
2094# endif
2095
2096 if (wp == NULL)
2097 {
2098 col = 0;
2099 width = Columns;
2100 }
2101 else
2102 {
2103 col = wp->w_wincol;
2104 width = wp->w_width;
2105 }
2106 screen_draw_rectangle(row, col, end - row, width, FALSE);
2107}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002109 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002110space_to_screenline(int off, int attr)
2111{
2112 ScreenLines[off] = ' ';
2113 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002114 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002115 if (enc_utf8)
2116 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002117}
2118
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002120 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2121 * to "end_col" (exclusive) with character "c1" in first column followed by
2122 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 */
2124 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002125screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002126 int start_row,
2127 int end_row,
2128 int start_col,
2129 int end_col,
2130 int c1,
2131 int c2,
2132 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002134 int row;
2135 int col;
2136 int off;
2137 int end_off;
2138 int did_delete;
2139 int c;
2140 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002142 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143#endif
2144
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002145 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002147 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 end_col = screen_Columns;
2149 if (ScreenLines == NULL
2150 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002151 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 return;
2153
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002154 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 norm_term = (
2156#ifdef FEAT_GUI
2157 !gui.in_use &&
2158#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002159 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 for (row = start_row; row < end_row; ++row)
2161 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002162 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002163#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002164 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002165#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002166 )
2167 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002168 // When drawing over the right half of a double-wide char clear
2169 // out the left half. When drawing over the left half of a
2170 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002171 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002172 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002173 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002174 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002175 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 /*
2178 * Try to use delete-line termcap code, when no attributes or in a
2179 * "normal" terminal, where a bold/italic space is just a
2180 * space.
2181 */
2182 did_delete = FALSE;
2183 if (c2 == ' '
2184 && end_col == Columns
2185 && can_clear(T_CE)
2186 && (attr == 0
2187 || (norm_term
2188 && attr <= HL_ALL
2189 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2190 {
2191 /*
2192 * check if we really need to clear something
2193 */
2194 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002195 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 ++col;
2197
2198 off = LineOffset[row] + col;
2199 end_off = LineOffset[row] + end_col;
2200
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002201 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (enc_utf8)
2203 while (off < end_off && ScreenLines[off] == ' '
2204 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2205 ++off;
2206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 while (off < end_off && ScreenLines[off] == ' '
2208 && ScreenAttrs[off] == 0)
2209 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002210 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
2212 col = off - LineOffset[row];
2213 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002214 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002216 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002218 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002220 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 ++off;
2222 }
2223 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002224 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 }
2226
2227 off = LineOffset[row] + start_col;
2228 c = c1;
2229 for (col = start_col; col < end_col; ++col)
2230 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002231 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002232 || (enc_utf8 && (int)ScreenLinesUC[off]
2233 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 || ScreenAttrs[off] != attr
Bram Moolenaar838b7462022-09-26 15:19:56 +01002235 || must_redraw == UPD_CLEAR // screen clear pending
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236#if defined(FEAT_GUI) || defined(UNIX)
2237 || force_next
2238#endif
2239 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02002240 // Skip if under a(nother) popup.
Bram Moolenaarff85d4a2022-10-02 15:21:04 +01002241 && !skip_for_popup(row, col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002244 // The bold trick may make a single row of pixels appear in
2245 // the next character. When a bold character is removed, the
2246 // next character should be redrawn too. This happens for our
2247 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if (
2249# ifdef FEAT_GUI
2250 gui.in_use
2251# endif
2252# if defined(FEAT_GUI) && defined(UNIX)
2253 ||
2254# endif
2255# ifdef UNIX
2256 term_is_xterm
2257# endif
2258 )
2259 {
2260 if (ScreenLines[off] != ' '
2261 && (ScreenAttrs[off] > HL_ALL
2262 || ScreenAttrs[off] & HL_BOLD))
2263 force_next = TRUE;
2264 else
2265 force_next = FALSE;
2266 }
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002267#endif // FEAT_GUI || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (enc_utf8)
2270 {
2271 if (c >= 0x80)
2272 {
2273 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002274 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276 else
2277 ScreenLinesUC[off] = 0;
2278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 ScreenAttrs[off] = attr;
2280 if (!did_delete || c != ' ')
2281 screen_char(off, row, col);
2282 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002283 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 ++off;
2285 if (col == start_col)
2286 {
2287 if (did_delete)
2288 break;
2289 c = c2;
2290 }
2291 }
2292 if (end_col == Columns)
2293 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002294 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002297 if (start_col == 0 && end_col == Columns
2298 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002299 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002300 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002301 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 }
2304}
2305
2306/*
2307 * Check if there should be a delay. Used before clearing or redrawing the
2308 * screen or the command line.
2309 */
2310 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002311check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
2313 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2314 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002315 && emsg_silent == 0
2316 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
2318 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002319 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 emsg_on_display = FALSE;
2321 if (check_msg_scroll)
2322 msg_scroll = FALSE;
2323 }
2324}
2325
2326/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002327 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2328 */
2329 static void
2330clear_TabPageIdxs(void)
2331{
2332 int scol;
2333
2334 for (scol = 0; scol < Columns; ++scol)
2335 TabPageIdxs[scol] = 0;
2336}
2337
2338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002340 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 * Returns TRUE if there is a valid screen to write to.
2342 * Returns FALSE when starting up and screen not initialized yet.
2343 */
2344 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002345screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002347 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 return (ScreenLines != NULL);
2349}
2350
2351/*
2352 * Resize the shell to Rows and Columns.
2353 * Allocate ScreenLines[] and associated items.
2354 *
2355 * There may be some time between setting Rows and Columns and (re)allocating
2356 * ScreenLines[]. This happens when starting up and when (manually) changing
2357 * the shell size. Always use screen_Rows and screen_Columns to access items
2358 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2359 * final size of the shell is needed.
2360 */
2361 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002362screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 int new_row, old_row;
2365#ifdef FEAT_GUI
2366 int old_Rows;
2367#endif
2368 win_T *wp;
2369 int outofmem = FALSE;
2370 int len;
2371 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002373 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002376 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 unsigned *new_LineOffset;
2378 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002379 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002380#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002381 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002382 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002383 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002384#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002385 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002386 static int entered = FALSE; // avoid recursiveness
2387 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002388 int retry_count = 0;
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002389 int found_null;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002391retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 /*
2393 * Allocation of the screen buffers is done only when the size changes and
2394 * when Rows and Columns have been set and we have started doing full
2395 * screen stuff.
2396 */
2397 if ((ScreenLines != NULL
2398 && Rows == screen_Rows
2399 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 && enc_utf8 == (ScreenLinesUC != NULL)
2401 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002402 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 || Rows == 0
2404 || Columns == 0
2405 || (!full_screen && ScreenLines == NULL))
2406 return;
2407
2408 /*
2409 * It's possible that we produce an out-of-memory message below, which
2410 * will cause this function to be called again. To break the loop, just
2411 * return here.
2412 */
2413 if (entered)
2414 return;
2415 entered = TRUE;
2416
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002417 /*
2418 * Note that the window sizes are updated before reallocating the arrays,
2419 * thus we must not redraw here!
2420 */
2421 ++RedrawingDisabled;
2422
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002423 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002425#ifdef FEAT_GUI_HAIKU
2426 vim_lock_screen(); // be safe, put it here
2427#endif
2428
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002429 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431 /*
2432 * We're changing the size of the screen.
2433 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2434 * - Move lines from the old arrays into the new arrays, clear extra
2435 * lines (unless the screen is going to be cleared).
2436 * - Free the old arrays.
2437 *
2438 * If anything fails, make ScreenLines NULL, so we don't do anything!
2439 * Continuing with the old ScreenLines may result in a crash, because the
2440 * size is wrong.
2441 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002442 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 win_free_lsize(wp);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002444 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002445 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002446 win_free_lsize(aucmd_win[i].auc_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002447#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002448 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002449 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002450 win_free_lsize(wp);
2451 // tab-local popup windows
2452 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002453 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002454 win_free_lsize(wp);
2455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002457 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002458 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 if (enc_utf8)
2460 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002461 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002462 for (int i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002463 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2464 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002467 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2468 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
dundargocc57b5bc2022-11-02 13:30:51 +00002469 // Clear ScreenCols to avoid a warning for uninitialized memory in
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002470 // jump_to_mouse().
2471 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002472 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2473 new_LineWraps = LALLOC_MULT(char_u, Rows);
2474 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002475#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002476 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002477 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002478 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002481 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
2483 if (win_alloc_lines(wp) == FAIL)
2484 {
2485 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002486 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
2488 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00002489 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00002490 if (aucmd_win[i].auc_win != NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002491 && aucmd_win[i].auc_win->w_lines == NULL
2492 && win_alloc_lines(aucmd_win[i].auc_win) == FAIL)
2493 {
2494 outofmem = TRUE;
2495 break;
2496 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002497#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002498 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002499 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002500 if (win_alloc_lines(wp) == FAIL)
2501 {
2502 outofmem = TRUE;
2503 goto give_up;
2504 }
2505 // tab-local popup windows
2506 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002507 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002508 if (win_alloc_lines(wp) == FAIL)
2509 {
2510 outofmem = TRUE;
2511 goto give_up;
2512 }
2513#endif
2514
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002515give_up:
Bram Moolenaarf86490e2022-11-28 19:11:02 +00002516 found_null = FALSE;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002517 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002518 if (new_ScreenLinesC[i] == NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00002519 {
2520 found_null = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002521 break;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (new_ScreenLines == NULL
Bram Moolenaare76062c2022-11-28 18:51:43 +00002524 || (enc_utf8 && (new_ScreenLinesUC == NULL || found_null))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002527 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 || new_LineOffset == NULL
2529 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002530 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002531#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002532 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002533 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002534 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 || outofmem)
2537 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002538 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002539 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002540 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002541 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2542
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002543 // Remember we did this to avoid getting outofmem messages over
2544 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002546 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002547 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002548 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002549 for (int i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002550 VIM_CLEAR(new_ScreenLinesC[i]);
2551 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002552 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002553 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002554 VIM_CLEAR(new_LineOffset);
2555 VIM_CLEAR(new_LineWraps);
2556 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002557#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002558 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002559 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002560 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563 else
2564 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002565 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 for (new_row = 0; new_row < Rows; ++new_row)
2568 {
2569 new_LineOffset[new_row] = new_row * Columns;
2570 new_LineWraps[new_row] = FALSE;
2571
Olaf Seibertfd472652024-02-01 21:11:16 +01002572 (void)vim_memset(new_ScreenLines + new_row * Columns,
2573 ' ', (size_t)Columns * sizeof(schar_T));
2574 if (enc_utf8)
2575 {
2576 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2577 0, (size_t)Columns * sizeof(u8char_T));
2578 for (int i = 0; i < p_mco; ++i)
2579 (void)vim_memset(new_ScreenLinesC[i]
2580 + new_row * Columns,
2581 0, (size_t)Columns * sizeof(u8char_T));
2582 }
2583 if (enc_dbcs == DBCS_JPNU)
2584 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2585 0, (size_t)Columns * sizeof(schar_T));
2586 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2587 0, (size_t)Columns * sizeof(sattr_T));
2588 (void)vim_memset(new_ScreenCols + new_row * Columns,
2589 0, (size_t)Columns * sizeof(colnr_T));
2590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 /*
2592 * If the screen is not going to be cleared, copy as much as
2593 * possible from the old screen to the new one and clear the rest
2594 * (used when resizing the window at the "--more--" prompt or when
2595 * executing an external command, for the GUI).
2596 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002597 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002600 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
2602 if (screen_Columns < Columns)
2603 len = screen_Columns;
2604 else
2605 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002606 // When switching to utf-8 don't copy characters, they
2607 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002608 if (!(enc_utf8 && ScreenLinesUC == NULL)
2609 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002610 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2611 ScreenLines + LineOffset[old_row],
2612 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002613 if (enc_utf8 && ScreenLinesUC != NULL
2614 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2617 ScreenLinesUC + LineOffset[old_row],
2618 (size_t)len * sizeof(u8char_T));
Bram Moolenaare76062c2022-11-28 18:51:43 +00002619 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002620 mch_memmove(new_ScreenLinesC[i]
2621 + new_LineOffset[new_row],
2622 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 (size_t)len * sizeof(u8char_T));
2624 }
2625 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2626 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2627 ScreenLines2 + LineOffset[old_row],
2628 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2630 ScreenAttrs + LineOffset[old_row],
2631 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002632 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2633 ScreenAttrs + LineOffset[old_row],
2634 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636 }
2637 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002638 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002640
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002641#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002642 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2643 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002647 free_screenlines();
2648
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002649 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaare76062c2022-11-28 18:51:43 +00002652 for (int i = 0; i < p_mco; ++i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002653 ScreenLinesC[i] = new_ScreenLinesC[i];
2654 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002657 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 LineOffset = new_LineOffset;
2659 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002660 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002661#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002662 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002663 popup_mask_next = new_popup_mask_next;
2664 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002665 popup_mask_refresh = TRUE;
2666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002668 // It's important that screen_Rows and screen_Columns reflect the actual
2669 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670#ifdef FEAT_GUI
2671 old_Rows = screen_Rows;
2672#endif
2673 screen_Rows = Rows;
2674 screen_Columns = Columns;
2675
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002676 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002677 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002678 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679#ifdef FEAT_GUI
2680 else if (gui.in_use
2681 && !gui.starting
2682 && ScreenLines != NULL
2683 && old_Rows != Rows)
2684 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002685 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2686
2687 // Adjust the position of the cursor, for when executing an external
2688 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002689 if (msg_row >= Rows) // Rows got smaller
2690 msg_row = Rows - 1; // put cursor at last row
2691 else if (Rows > old_Rows) // Rows got bigger
2692 msg_row += Rows - old_Rows; // put cursor in same place
2693 if (msg_col >= Columns) // Columns got smaller
2694 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002697 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002699#ifdef FEAT_GUI_HAIKU
2700 vim_unlock_screen();
2701#endif
2702
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 entered = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002704 if (RedrawingDisabled > 0)
2705 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002706
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002707 /*
2708 * Do not apply autocommands more than 3 times to avoid an endless loop
2709 * in case applying autocommands always changes Rows or Columns.
2710 */
2711 if (starting == 0 && ++retry_count <= 3)
2712 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002713 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002714 // In rare cases, autocommands may have altered Rows or Columns,
2715 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002716 goto retry;
2717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718}
2719
2720 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002721free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002722{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002723 int i;
2724
Bram Moolenaar33796b32019-06-08 16:01:13 +02002725 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002726 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002727 VIM_CLEAR(ScreenLinesC[i]);
2728 VIM_CLEAR(ScreenLines2);
2729 VIM_CLEAR(ScreenLines);
2730 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002731 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002732 VIM_CLEAR(LineOffset);
2733 VIM_CLEAR(LineWraps);
2734 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002735#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002736 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002737 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002738 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002739#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002740}
2741
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002742/*
2743 * Clear the screen.
2744 * May delay if there is something the user should read.
2745 * Allocated the screen for resizing if needed.
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002746 * Returns TRUE when the screen was actually cleared, FALSE if all display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002747 * cells were marked for updating.
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002748 */
Bram Moolenaar838b7462022-09-26 15:19:56 +01002749 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002750screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 check_for_delay(FALSE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002753 screenalloc(FALSE); // allocate screen buffers if size changed
2754 return screenclear2(TRUE); // clear the screen
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002755}
2756
2757/*
2758 * Do not clear the screen but mark everything for redraw.
2759 */
2760 void
2761redraw_as_cleared(void)
2762{
2763 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764}
2765
Bram Moolenaar838b7462022-09-26 15:19:56 +01002766 static int
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002767screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
2769 int i;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002770 int did_clear = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
2772 if (starting == NO_SCREEN || ScreenLines == NULL
2773#ifdef FEAT_GUI
2774 || (gui.in_use && gui.starting)
2775#endif
2776 )
Bram Moolenaar838b7462022-09-26 15:19:56 +01002777 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779#ifdef FEAT_GUI
2780 if (!gui.in_use)
2781#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002782 screen_attr = -1; // force setting the Normal colors
2783 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002786 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 clip_scroll_selection(9999);
2788#endif
2789
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002790 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 for (i = 0; i < Rows; ++i)
2792 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002793 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 LineWraps[i] = FALSE;
2795 }
2796
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002797 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002799 out_str(T_CL); // clear the display
Bram Moolenaar838b7462022-09-26 15:19:56 +01002800 did_clear = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002802 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 else
2805 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002806 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 for (i = 0; i < Rows; ++i)
2808 lineinvalid(LineOffset[i], (int)Columns);
2809 clear_cmdline = TRUE;
2810 }
2811
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002812 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002814 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002816 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002817 if (must_redraw == UPD_CLEAR) // no need to clear again
2818 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002819 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01002821#ifdef FEAT_PROP_POPUP
2822 popup_redraw_all(); // redraw all popup windows
2823#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002824 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002826 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 msg_didany = FALSE;
2828 msg_didout = FALSE;
Bram Moolenaar838b7462022-09-26 15:19:56 +01002829
2830 return did_clear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831}
2832
2833/*
2834 * Clear one line in ScreenLines.
2835 */
2836 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002837lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
2839 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (enc_utf8)
2841 (void)vim_memset(ScreenLinesUC + off, 0,
2842 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002843 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002844 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845}
2846
2847/*
2848 * Mark one line in ScreenLines invalid by setting the attributes to an
2849 * invalid value.
2850 */
2851 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002852lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002855 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856}
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02002859 * To be called when characters were sent to the terminal directly, outputting
2860 * test on "screen_lnum".
2861 */
2862 void
2863line_was_clobbered(int screen_lnum)
2864{
2865 lineinvalid(LineOffset[screen_lnum], (int)Columns);
2866}
2867
2868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 * Copy part of a Screenline for vertically split window "wp".
2870 */
2871 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002872linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
2874 unsigned off_to = LineOffset[to] + wp->w_wincol;
2875 unsigned off_from = LineOffset[from] + wp->w_wincol;
2876
2877 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
2878 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (enc_utf8)
2880 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002881 int i;
2882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
2884 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002885 for (i = 0; i < p_mco; ++i)
2886 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
2887 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
2889 if (enc_dbcs == DBCS_JPNU)
2890 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
2891 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
2893 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002894 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
2895 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898/*
2899 * Return TRUE if clearing with term string "p" would work.
2900 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02002901 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 */
2903 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002904can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 return (*p != NUL && (t_colors <= 1
2907#ifdef FEAT_GUI
2908 || gui.in_use
2909#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002910#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002911 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02002912 || (!p_tgc && cterm_normal_bg_color == 0)
2913#else
2914 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002915#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002916 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002917#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002918 && !(p == T_CE && popup_visible)
2919#endif
2920 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921}
2922
2923/*
2924 * Reset cursor position. Use whenever cursor was moved because of outputting
2925 * something directly to the screen (shell commands) or a terminal control
2926 * code.
2927 */
2928 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002929screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
2931 screen_cur_row = screen_cur_col = 9999;
2932}
2933
2934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 * Move the cursor to position "row","col" in the screen.
2936 * This tries to find the most efficient way to move, minimizing the number of
2937 * characters sent to the terminal.
2938 */
2939 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002940windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002942 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 int i;
2944 int plan;
2945 int cost;
2946 int wouldbe_col;
2947 int noinvcurs;
2948 char_u *bs;
2949 int goto_cost;
2950 int attr;
2951
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002952#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
2953#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955#define PLAN_LE 1
2956#define PLAN_CR 2
2957#define PLAN_NL 3
2958#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002959 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 if (ScreenLines == NULL)
2961 return;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002962 if (col == screen_cur_col && row == screen_cur_row)
2963 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002965 // Check for valid position.
2966 if (row < 0) // window without text lines?
2967 row = 0;
2968 if (row >= screen_Rows)
2969 row = screen_Rows - 1;
2970 if (col >= screen_Columns)
2971 col = screen_Columns - 1;
2972
2973 // check if no cursor movement is allowed in highlight mode
2974 if (screen_attr && *T_MS == NUL)
2975 noinvcurs = HIGHL_COST;
2976 else
2977 noinvcurs = 0;
2978 goto_cost = GOTO_COST + noinvcurs;
2979
2980 /*
2981 * Plan how to do the positioning:
2982 * 1. Use CR to move it to column 0, same row.
2983 * 2. Use T_LE to move it a few columns to the left.
2984 * 3. Use NL to move a few lines down, column 0.
2985 * 4. Move a few columns to the right with T_ND or by writing chars.
2986 *
2987 * Don't do this if the cursor went beyond the last column, the cursor
2988 * position is unknown then (some terminals wrap, some don't )
2989 *
2990 * First check if the highlighting attributes allow us to write
2991 * characters to move the cursor to the right.
2992 */
2993 if (row >= screen_cur_row && screen_cur_col < Columns)
2994 {
2995 /*
2996 * If the cursor is in the same row, bigger col, we can use CR
2997 * or T_LE.
2998 */
2999 bs = NULL; // init for GCC
3000 attr = screen_attr;
3001 if (row == screen_cur_row && col < screen_cur_col)
3002 {
3003 // "le" is preferred over "bc", because "bc" is obsolete
3004 if (*T_LE)
3005 bs = T_LE; // "cursor left"
3006 else
3007 bs = T_BC; // "backspace character (old)
3008 if (*bs)
3009 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3010 else
3011 cost = 999;
3012 if (col + 1 < cost) // using CR is less characters
3013 {
3014 plan = PLAN_CR;
3015 wouldbe_col = 0;
3016 cost = 1; // CR is just one character
3017 }
3018 else
3019 {
3020 plan = PLAN_LE;
3021 wouldbe_col = col;
3022 }
3023 if (noinvcurs) // will stop highlighting
3024 {
3025 cost += noinvcurs;
3026 attr = 0;
3027 }
3028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003031 * If the cursor is above where we want to be, we can use CR LF.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003033 else if (row > screen_cur_row)
3034 {
3035 plan = PLAN_NL;
3036 wouldbe_col = 0;
3037 cost = (row - screen_cur_row) * 2; // CR LF
3038 if (noinvcurs) // will stop highlighting
3039 {
3040 cost += noinvcurs;
3041 attr = 0;
3042 }
3043 }
3044
3045 /*
3046 * If the cursor is in the same row, smaller col, just use write.
3047 */
3048 else
3049 {
3050 plan = PLAN_WRITE;
3051 wouldbe_col = screen_cur_col;
3052 cost = 0;
3053 }
3054
3055 /*
3056 * Check if any characters that need to be written have the
3057 * correct attributes. Also avoid UTF-8 characters.
3058 */
3059 i = col - wouldbe_col;
3060 if (i > 0)
3061 cost += i;
3062 if (cost < goto_cost && i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
3064 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003065 * Check if the attributes are correct without additionally
3066 * stopping highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003068 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3069 while (i && *p++ == attr)
3070 --i;
3071 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
3073 /*
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003074 * Try if it works when highlighting is stopped here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 */
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003076 if (*--p == 0)
3077 {
3078 cost += noinvcurs;
3079 while (i && *p++ == 0)
3080 --i;
3081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (i != 0)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003083 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003085 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003087 // Don't use an UTF-8 char for positioning, it's slow.
3088 for (i = wouldbe_col; i < col; ++i)
3089 if (ScreenLinesUC[LineOffset[row] + i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003091 cost = 999;
3092 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003094 }
3095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003097 /*
3098 * We can do it without term_windgoto()!
3099 */
3100 if (cost < goto_cost)
3101 {
3102 if (plan == PLAN_LE)
3103 {
3104 if (noinvcurs)
3105 screen_stop_highlight();
3106 while (screen_cur_col > col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003108 out_str(bs);
3109 --screen_cur_col;
3110 }
3111 }
3112 else if (plan == PLAN_CR)
3113 {
3114 if (noinvcurs)
3115 screen_stop_highlight();
3116 out_char('\r');
3117 screen_cur_col = 0;
3118 }
3119 else if (plan == PLAN_NL)
3120 {
3121 if (noinvcurs)
3122 screen_stop_highlight();
3123 while (screen_cur_row < row)
3124 {
3125 out_char('\n');
3126 ++screen_cur_row;
3127 }
3128 screen_cur_col = 0;
3129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003131 i = col - screen_cur_col;
3132 if (i > 0)
3133 {
3134 /*
3135 * Use cursor-right if it's one character only. Avoids
3136 * removing a line of pixels from the last bold char, when
3137 * using the bold trick in the GUI.
3138 */
3139 if (T_ND[0] != NUL && T_ND[1] == NUL)
3140 {
3141 while (i-- > 0)
3142 out_char(*T_ND);
3143 }
3144 else
3145 {
3146 int off;
3147
3148 off = LineOffset[row] + screen_cur_col;
3149 while (i-- > 0)
3150 {
3151 if (ScreenAttrs[off] != screen_attr)
3152 screen_stop_highlight();
3153 out_flush_check();
3154 out_char(ScreenLines[off]);
3155 if (enc_dbcs == DBCS_JPNU
3156 && ScreenLines[off] == 0x8e)
3157 out_char(ScreenLines2[off]);
3158 ++off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
3160 }
3161 }
3162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003164 else
3165 cost = 999;
3166
3167 if (cost >= goto_cost)
3168 {
3169 if (noinvcurs)
3170 screen_stop_highlight();
3171 if (row == screen_cur_row && (col > screen_cur_col)
3172 && *T_CRI != NUL)
3173 term_cursor_right(col - screen_cur_col);
3174 else
3175 term_windgoto(row, col);
3176 }
3177 screen_cur_row = row;
3178 screen_cur_col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179}
3180
3181/*
3182 * Set cursor to its position in the current window.
3183 */
3184 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003185setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003187 setcursor_mayforce(FALSE);
3188}
3189
3190/*
3191 * Set cursor to its position in the current window.
3192 * When "force" is TRUE also when not redrawing.
3193 */
3194 void
3195setcursor_mayforce(int force)
3196{
3197 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
3199 validate_cursor();
3200 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003201 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003203 // With 'rightleft' set and the cursor on a double-wide
3204 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003205 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3206 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003207 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003208 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#endif
3210 curwin->w_wcol));
3211 }
3212}
3213
3214
3215/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003216 * Insert 'line_count' lines at 'row' in window 'wp'.
3217 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3218 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 * scrolling.
3220 * Returns FAIL if the lines are not inserted, OK for success.
3221 */
3222 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003223win_ins_lines(
3224 win_T *wp,
3225 int row,
3226 int line_count,
3227 int invalid,
3228 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230 int did_delete;
3231 int nextrow;
3232 int lastrow;
3233 int retval;
3234
3235 if (invalid)
3236 wp->w_lines_valid = 0;
3237
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003238 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (wp->w_height < 5)
3240 return FAIL;
3241
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003242 // with the popup menu visible this might not work correctly
3243 if (pum_visible())
3244 return FAIL;
3245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (line_count > wp->w_height - row)
3247 line_count = wp->w_height - row;
3248
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003249 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (retval != MAYBE)
3251 return retval;
3252
3253 /*
3254 * If there is a next window or a status line, we first try to delete the
3255 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003256 * If this fails and there are following windows, don't do anything to
3257 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 */
3259 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (wp->w_next != NULL || wp->w_status_height)
3261 {
3262 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003263 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 did_delete = TRUE;
3265 else if (wp->w_next)
3266 return FAIL;
3267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 /*
3269 * if no lines deleted, blank the lines that will end up below the window
3270 */
3271 if (!did_delete)
3272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003275 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 lastrow = nextrow + line_count;
3277 if (lastrow > Rows)
3278 lastrow = Rows;
3279 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003280 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 ' ', ' ', 0);
3282 }
3283
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003284 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 == FAIL)
3286 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003287 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (did_delete)
3289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 win_rest_invalid(W_NEXT(wp));
3292 }
3293 return FAIL;
3294 }
3295
3296 return OK;
3297}
3298
3299/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003300 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3302 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3303 * scrolling
3304 * Return OK for success, FAIL if the lines are not deleted.
3305 */
3306 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003307win_del_lines(
3308 win_T *wp,
3309 int row,
3310 int line_count,
3311 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003312 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003313 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 int retval;
3316
3317 if (invalid)
3318 wp->w_lines_valid = 0;
3319
3320 if (line_count > wp->w_height - row)
3321 line_count = wp->w_height - row;
3322
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003323 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (retval != MAYBE)
3325 return retval;
3326
3327 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003328 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 return FAIL;
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 /*
3332 * If there are windows or status lines below, try to put them at the
3333 * correct place. If we can't do that, they have to be redrawn.
3334 */
3335 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3336 {
3337 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003338 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 {
3340 wp->w_redr_status = TRUE;
3341 win_rest_invalid(wp->w_next);
3342 }
3343 }
3344 /*
3345 * If this is the last window and there is no status line, redraw the
3346 * command line later.
3347 */
3348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 redraw_cmdline = TRUE;
3350 return OK;
3351}
3352
3353/*
3354 * Common code for win_ins_lines() and win_del_lines().
3355 * Returns OK or FAIL when the work has been done.
3356 * Returns MAYBE when not finished yet.
3357 */
3358 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003359win_do_lines(
3360 win_T *wp,
3361 int row,
3362 int line_count,
3363 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003364 int del,
3365 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 int retval;
3368
3369 if (!redrawing() || line_count <= 0)
3370 return FAIL;
3371
Bram Moolenaar33796b32019-06-08 16:01:13 +02003372 // When inserting lines would result in loss of command output, just redraw
3373 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003374 if (no_win_do_lines_ins && !del)
3375 return FAIL;
3376
Bram Moolenaar33796b32019-06-08 16:01:13 +02003377 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003378 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003380 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003381 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 return FAIL;
3383 }
3384
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003385#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003386 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003387 if (popup_visible)
3388 return FAIL;
3389#endif
3390
3391 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 if (row + line_count >= wp->w_height)
3393 {
3394 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003395 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 ' ', ' ', 0);
3397 return OK;
3398 }
3399
3400 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003401 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003403 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003405 if (!no_win_do_lines_ins)
3406 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 /*
3409 * If the terminal can set a scroll region, use that.
3410 * Always do this in a vertically split window. This will redraw from
3411 * ScreenLines[] when t_CV isn't defined. That's faster than using
3412 * win_line().
3413 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003414 * a character in the lower right corner of the scroll region may cause a
3415 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003417 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 scroll_region_set(wp, row);
3421 if (del)
3422 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003423 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 else
3425 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003426 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 scroll_region_reset();
3429 return retval;
3430 }
3431
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003432 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 return MAYBE;
3436}
3437
3438/*
3439 * window 'wp' and everything after it is messed up, mark it for redraw
3440 */
3441 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003442win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003446 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 wp->w_redr_status = TRUE;
3448 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
3450 redraw_cmdline = TRUE;
3451}
3452
3453/*
3454 * The rest of the routines in this file perform screen manipulations. The
3455 * given operation is performed physically on the screen. The corresponding
3456 * change is also made to the internal screen image. In this way, the editor
3457 * anticipates the effect of editing changes on the appearance of the screen.
3458 * That way, when we call screenupdate a complete redraw isn't usually
3459 * necessary. Another advantage is that we can keep adding code to anticipate
3460 * screen changes, and in the meantime, everything still works.
3461 */
3462
3463/*
3464 * types for inserting or deleting lines
3465 */
3466#define USE_T_CAL 1
3467#define USE_T_CDL 2
3468#define USE_T_AL 3
3469#define USE_T_CE 4
3470#define USE_T_DL 5
3471#define USE_T_SR 6
3472#define USE_NL 7
3473#define USE_T_CD 8
3474#define USE_REDRAW 9
3475
3476/*
3477 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003478 * "end" is the line after the scrolled part. Normally it is Rows.
3479 * When scrolling region used "off" is the offset from the top for the region.
3480 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 *
3482 * return FAIL for failure, OK for success.
3483 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003484 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003485screen_ins_lines(
3486 int off,
3487 int row,
3488 int line_count,
3489 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003490 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003491 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
3493 int i;
3494 int j;
3495 unsigned temp;
3496 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003497 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 int type;
3499 int result_empty;
3500 int can_ce = can_clear(T_CE);
3501
3502 /*
3503 * FAIL if
3504 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 * - the line count is less than one
3506 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003507 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003508 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003509 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003511 if (!screen_valid(TRUE)
3512 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003513 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003514#ifdef FEAT_CLIPBOARD
3515 || (clip_star.state != SELECT_CLEARED
3516 && redrawing_for_callback > 0)
3517#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003518#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003519 || popup_visible
3520#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003521 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 return FAIL;
3523
3524 /*
3525 * There are seven ways to insert lines:
3526 * 0. When in a vertically split window and t_CV isn't set, redraw the
3527 * characters from ScreenLines[].
3528 * 1. Use T_CD (clear to end of display) if it exists and the result of
3529 * the insert is just empty lines
3530 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3531 * present or line_count > 1. It looks better if we do all the inserts
3532 * at once.
3533 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3534 * insert is just empty lines and T_CE is not present or line_count >
3535 * 1.
3536 * 4. Use T_AL (insert line) if it exists.
3537 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3538 * just empty lines.
3539 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3540 * just empty lines.
3541 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3542 * the 'da' flag is not set or we have clear line capability.
3543 * 8. redraw the characters from ScreenLines[].
3544 *
3545 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3546 * the scrollbar for the window. It does have insert line, use that if it
3547 * exists.
3548 */
3549 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003551 {
3552 // Avoid that lines are first cleared here and then redrawn, which
3553 // results in many characters updated twice. This happens with CTRL-F
3554 // in a vertically split window. With line-by-line scrolling
3555 // USE_REDRAW should be faster.
3556 if (line_count > 3)
3557 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003559 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003560 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 type = USE_T_CD;
3562 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3563 type = USE_T_CAL;
3564 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3565 type = USE_T_CDL;
3566 else if (*T_AL != NUL)
3567 type = USE_T_AL;
3568 else if (can_ce && result_empty)
3569 type = USE_T_CE;
3570 else if (*T_DL != NUL && result_empty)
3571 type = USE_T_DL;
3572 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3573 type = USE_T_SR;
3574 else
3575 return FAIL;
3576
3577 /*
3578 * For clearing the lines screen_del_lines() is used. This will also take
3579 * care of t_db if necessary.
3580 */
3581 if (type == USE_T_CD || type == USE_T_CDL ||
3582 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003583 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
3585 /*
3586 * If text is retained below the screen, first clear or delete as many
3587 * lines at the bottom of the window as are about to be inserted so that
3588 * the deleted lines won't later surface during a screen_del_lines.
3589 */
3590 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003591 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
3593#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003594 // Remove a modeless selection when inserting lines halfway the screen
3595 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003596 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003597 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 else
3599 clip_scroll_selection(-line_count);
3600#endif
3601
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003602#ifdef FEAT_GUI_HAIKU
3603 vim_lock_screen();
3604#endif
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003607 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3608 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003609 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610#endif
3611
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003612 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3613 cursor_col = wp->w_wincol;
3614
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003615 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 cursor_row = row;
3617 else
3618 cursor_row = row + off;
3619
3620 /*
3621 * Shift LineOffset[] line_count down to reflect the inserted lines.
3622 * Clear the inserted lines in ScreenLines[].
3623 */
3624 row += off;
3625 end += off;
3626 for (i = 0; i < line_count; ++i)
3627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (wp != NULL && wp->w_width != Columns)
3629 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003630 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 j = end - 1 - i;
3632 while ((j -= line_count) >= row)
3633 linecopy(j + line_count, j, wp);
3634 j += line_count;
3635 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003636 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3637 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 else
3639 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3640 LineWraps[j] = FALSE;
3641 }
3642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 j = end - 1 - i;
3645 temp = LineOffset[j];
3646 while ((j -= line_count) >= row)
3647 {
3648 LineOffset[j + line_count] = LineOffset[j];
3649 LineWraps[j + line_count] = LineWraps[j];
3650 }
3651 LineOffset[j + line_count] = temp;
3652 LineWraps[j + line_count] = FALSE;
3653 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003654 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 else
3656 lineinvalid(temp, (int)Columns);
3657 }
3658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003660#ifdef FEAT_GUI_HAIKU
3661 vim_unlock_screen();
3662#endif
3663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003665 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003666 if (clear_attr != 0)
3667 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003669 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 if (type == USE_REDRAW)
3671 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003672 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
3674 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003675 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677 else
3678 {
3679 for (i = 0; i < line_count; i++)
3680 {
3681 if (type == USE_T_AL)
3682 {
3683 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003684 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 out_str(T_AL);
3686 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003687 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003689 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 }
3692
3693 /*
3694 * With scroll-reverse and 'da' flag set we need to clear the lines that
3695 * have been scrolled down into the region.
3696 */
3697 if (type == USE_T_SR && *T_DA)
3698 {
3699 for (i = 0; i < line_count; ++i)
3700 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003701 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003703 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705 }
3706
3707#ifdef FEAT_GUI
3708 gui_can_update_cursor();
3709 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003710 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711#endif
3712 return OK;
3713}
3714
3715/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003716 * Delete lines on the screen and update ScreenLines[].
3717 * "end" is the line after the scrolled part. Normally it is Rows.
3718 * When scrolling region used "off" is the offset from the top for the region.
3719 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 *
3721 * Return OK for success, FAIL if the lines are not deleted.
3722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003724screen_del_lines(
3725 int off,
3726 int row,
3727 int line_count,
3728 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003729 int force, // even when line_count > p_ttyscroll
3730 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003731 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
3733 int j;
3734 int i;
3735 unsigned temp;
3736 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003737 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003739 int result_empty; // result is empty until end of region
3740 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 int type;
3742
3743 /*
3744 * FAIL if
3745 * - there is no valid screen
3746 * - the screen has to be redrawn completely
3747 * - the line count is less than one
3748 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003749 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003750 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003752 if (!screen_valid(TRUE)
3753 || line_count <= 0
3754 || (!force && line_count > p_ttyscroll)
3755 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003756#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003757 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003758#endif
3759 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 return FAIL;
3761
3762 /*
3763 * Check if the rest of the current region will become empty.
3764 */
3765 result_empty = row + line_count >= end;
3766
3767 /*
3768 * We can delete lines only when 'db' flag not set or when 'ce' option
3769 * available.
3770 */
3771 can_delete = (*T_DB == NUL || can_clear(T_CE));
3772
3773 /*
3774 * There are six ways to delete lines:
3775 * 0. When in a vertically split window and t_CV isn't set, redraw the
3776 * characters from ScreenLines[].
3777 * 1. Use T_CD if it exists and the result is empty.
3778 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
3779 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
3780 * none of the other ways work.
3781 * 4. Use T_CE (erase line) if the result is empty.
3782 * 5. Use T_DL (delete line) if it exists.
3783 * 6. redraw the characters from ScreenLines[].
3784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003786 {
3787 // Avoid that lines are first cleared here and then redrawn, which
3788 // results in many characters updated twice. This happens with CTRL-F
3789 // in a vertically split window. With line-by-line scrolling
3790 // USE_REDRAW should be faster.
3791 if (line_count > 3)
3792 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003794 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003795 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 else if (row == 0 && (
3798#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003799 // On the Amiga, somehow '\n' on the last line doesn't always scroll
3800 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 line_count == 1 ||
3802#endif
3803 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 type = USE_NL;
3805 else if (*T_CDL != NUL && line_count > 1 && can_delete)
3806 type = USE_T_CDL;
3807 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02003808 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 type = USE_T_CE;
3810 else if (*T_DL != NUL && can_delete)
3811 type = USE_T_DL;
3812 else if (*T_CDL != NUL && can_delete)
3813 type = USE_T_CDL;
3814 else
3815 return FAIL;
3816
3817#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003818 // Remove a modeless selection when deleting lines halfway the screen or
3819 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003820 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003821 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 else
3823 clip_scroll_selection(line_count);
3824#endif
3825
Bram Moolenaar92c461e2020-04-24 22:19:00 +02003826#ifdef FEAT_GUI_HAIKU
3827 vim_lock_screen();
3828#endif
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003831 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3832 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003833 gui_dont_update_cursor(gui.cursor_row >= row + off
3834 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#endif
3836
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003837 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3838 cursor_col = wp->w_wincol;
3839
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003840 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
3842 cursor_row = row;
3843 cursor_end = end;
3844 }
3845 else
3846 {
3847 cursor_row = row + off;
3848 cursor_end = end + off;
3849 }
3850
3851 /*
3852 * Now shift LineOffset[] line_count up to reflect the deleted lines.
3853 * Clear the inserted lines in ScreenLines[].
3854 */
3855 row += off;
3856 end += off;
3857 for (i = 0; i < line_count; ++i)
3858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (wp != NULL && wp->w_width != Columns)
3860 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003861 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 j = row + i;
3863 while ((j += line_count) <= end - 1)
3864 linecopy(j - line_count, j, wp);
3865 j -= line_count;
3866 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003867 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3868 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 else
3870 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3871 LineWraps[j] = FALSE;
3872 }
3873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003875 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 j = row + i;
3877 temp = LineOffset[j];
3878 while ((j += line_count) <= end - 1)
3879 {
3880 LineOffset[j - line_count] = LineOffset[j];
3881 LineWraps[j - line_count] = LineWraps[j];
3882 }
3883 LineOffset[j - line_count] = temp;
3884 LineWraps[j - line_count] = FALSE;
3885 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003886 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 else
3888 lineinvalid(temp, (int)Columns);
3889 }
3890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003892#ifdef FEAT_GUI_HAIKU
3893 vim_unlock_screen();
3894#endif
3895
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003896 if (screen_attr != clear_attr)
3897 screen_stop_highlight();
3898 if (clear_attr != 0)
3899 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003901 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 if (type == USE_REDRAW)
3903 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003904 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003906 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003908 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910 else if (type == USE_T_CDL)
3911 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003912 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003914 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 /*
3917 * Deleting lines at top of the screen or scroll region: Just scroll
3918 * the whole screen (scroll region) up by outputting newlines on the
3919 * last line.
3920 */
3921 else if (type == USE_NL)
3922 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003923 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003925 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 else
3928 {
3929 for (i = line_count; --i >= 0; )
3930 {
3931 if (type == USE_T_DL)
3932 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003933 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003934 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003936 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003938 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003939 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003941 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 }
3944
3945 /*
3946 * If the 'db' flag is set, we need to clear the lines that have been
3947 * scrolled up at the bottom of the region.
3948 */
3949 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
3950 {
3951 for (i = line_count; i > 0; --i)
3952 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003953 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003954 out_str(T_CE); // erase a line
3955 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
3957 }
3958
3959#ifdef FEAT_GUI
3960 gui_can_update_cursor();
3961 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003962 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963#endif
3964
3965 return OK;
3966}
3967
3968/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003969 * Return TRUE when postponing displaying the mode message: when not redrawing
3970 * or inside a mapping.
3971 */
3972 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003973skip_showmode(void)
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003974{
3975 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01003976 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003977 if (global_busy
3978 || msg_silent != 0
3979 || !redrawing()
3980 || (char_avail() && !KeyTyped))
3981 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02003982 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003983 return TRUE;
3984 }
3985 return FALSE;
3986}
3987
3988/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01003989 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 *
3991 * If clear_cmdline is TRUE, clear the rest of the cmdline.
3992 * If clear_cmdline is FALSE there may be a message there that needs to be
3993 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02003994 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 * Return the length of the message (0 if no message).
3996 */
3997 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003998showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999{
4000 int need_clear;
4001 int length = 0;
4002 int do_mode;
4003 int attr;
4004 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
Bram Moolenaara2a89732022-08-31 14:46:18 +01004007 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004008 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004009 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004010 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004011 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004013 if (skip_showmode())
4014 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 nwr_save = need_wait_return;
4017
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004018 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 check_for_delay(FALSE);
4020
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004021 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 need_clear = clear_cmdline;
4023 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004024 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004026 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 msg_pos_mode();
4028 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004029 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 if (do_mode)
4031 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004032 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004034 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004035# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004036 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004037# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004038 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004039# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004040 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004041# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004042 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004044 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045# endif
4046#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004047 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004048 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004050 // These messages can get long, avoid a wrap in a narrow
4051 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 length = (Rows - msg_row) * Columns - 3;
4053 if (edit_submode_extra != NULL)
4054 length -= vim_strsize(edit_submode_extra);
4055 if (length > 0)
4056 {
4057 if (edit_submode_pre != NULL)
4058 length -= vim_strsize(edit_submode_pre);
4059 if (length - vim_strsize(edit_submode) > 0)
4060 {
4061 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004062 msg_puts_attr((char *)edit_submode_pre, attr);
4063 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 if (edit_submode_extra != NULL)
4066 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004067 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004069 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 else
4071 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004072 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004079 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004080 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004081 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004082 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
4084#ifdef FEAT_RIGHTLEFT
4085 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004086 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004088 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004090 else if (restart_edit == 'I' || restart_edit == 'i' ||
4091 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004092 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004094 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004096 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097#ifdef FEAT_RIGHTLEFT
4098 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004099 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100#endif
4101#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004102 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
4104# ifdef FEAT_ARABIC
4105 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004106 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 else
4108# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004109 if (get_keymap_str(curwin, (char_u *)" (%s)",
4110 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004111 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004114 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004115 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (VIsual_active)
4118 {
4119 char *p;
4120
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004121 // Don't concatenate separate words to avoid translation
4122 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 switch ((VIsual_select ? 4 : 0)
4124 + (VIsual_mode == Ctrl_V) * 2
4125 + (VIsual_mode == 'V'))
4126 {
4127 case 0: p = N_(" VISUAL"); break;
4128 case 1: p = N_(" VISUAL LINE"); break;
4129 case 2: p = N_(" VISUAL BLOCK"); break;
4130 case 4: p = N_(" SELECT"); break;
4131 case 5: p = N_(" SELECT LINE"); break;
4132 default: p = N_(" SELECT BLOCK"); break;
4133 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004134 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004136 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 need_clear = TRUE;
4140 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004141 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004142 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004144 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 need_clear = TRUE;
4146 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004147
4148 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004149 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004151 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 length = msg_col;
4153 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004154 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004157 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004159 else if (redraw_mode)
4160 {
4161 msg_pos_mode();
4162 msg_clr_eos();
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004165 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (VIsual_active)
4167 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004169 // If the last window has no status line, the ruler is after the mode
4170 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004171 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004172 win_redr_ruler(lastwin, TRUE, FALSE);
Martin Tournoijba43e762022-10-13 22:12:15 +01004173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004175 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 clear_cmdline = FALSE;
4177
4178 return length;
4179}
4180
4181/*
4182 * Position for a mode message.
4183 */
4184 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004185msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186{
4187 msg_col = 0;
4188 msg_row = Rows - 1;
4189}
4190
4191/*
4192 * Delete mode message. Used when ESC is typed which is expected to end
4193 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004194 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 */
4196 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004197unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
4199 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004200 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 */
4202 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004203 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004205 clearmode();
4206}
4207
4208/*
4209 * Clear the mode message.
4210 */
4211 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004212clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004213{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004214 int save_msg_row = msg_row;
4215 int save_msg_col = msg_col;
4216
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004217 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004218 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004219 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004220 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004221
4222 msg_col = save_msg_col;
4223 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224}
4225
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004226 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004227recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004228{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004229 msg_puts_attr(_("recording"), attr);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004230 if (shortmess(SHM_RECORDING))
4231 return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004232
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00004233 char s[4];
4234
4235 sprintf(s, " @%c", reg_recording);
4236 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004237}
4238
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004239/*
4240 * Draw the tab pages line at the top of the Vim window.
4241 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004242 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004243draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004244{
4245 int tabcount = 0;
4246 tabpage_T *tp;
4247 int tabwidth;
4248 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004249 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004250 int attr;
4251 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004252 win_T *cwp;
4253 int wincount;
4254 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004255 int c;
4256 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004257 int attr_sel = HL_ATTR(HLF_TPS);
4258 int attr_nosel = HL_ATTR(HLF_TP);
4259 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004260 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004261 int room;
4262 int use_sep_chars = (t_colors < 8
4263#ifdef FEAT_GUI
4264 && !gui.in_use
4265#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004266#ifdef FEAT_TERMGUICOLORS
4267 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004268#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004269 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004270
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004271 if (ScreenLines == NULL)
4272 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004273 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004274
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004275#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004276 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004277 if (gui_use_tabline())
4278 {
4279 gui_update_tabline();
4280 return;
4281 }
4282#endif
4283
4284 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004285 return;
4286
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004287#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004288 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004289
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004290 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004291 if (*p_tal != NUL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004292 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004293 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004294#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004295 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004296 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004297 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004298
Bram Moolenaar238a5642006-02-21 22:12:05 +00004299 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4300 if (tabwidth < 6)
4301 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004302
Bram Moolenaar238a5642006-02-21 22:12:05 +00004303 attr = attr_nosel;
4304 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004305 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4306 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004307 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004308 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004309
Bram Moolenaar238a5642006-02-21 22:12:05 +00004310 if (tp->tp_topframe == topframe)
4311 attr = attr_sel;
4312 if (use_sep_chars && col > 0)
4313 screen_putchar('|', 0, col++, attr);
4314
4315 if (tp->tp_topframe != topframe)
4316 attr = attr_nosel;
4317
4318 screen_putchar(' ', 0, col++, attr);
4319
4320 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004321 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004322 cwp = curwin;
4323 wp = firstwin;
4324 }
4325 else
4326 {
4327 cwp = tp->tp_curwin;
4328 wp = tp->tp_firstwin;
4329 }
4330
4331 modified = FALSE;
4332 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4333 if (bufIsChanged(wp->w_buffer))
4334 modified = TRUE;
4335 if (modified || wincount > 1)
4336 {
4337 if (wincount > 1)
4338 {
4339 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004340 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004341 if (col + len >= Columns - 3)
4342 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004343 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004344#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004345 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004346#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004347 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004348#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004349 );
4350 col += len;
4351 }
4352 if (modified)
4353 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4354 screen_putchar(' ', 0, col++, attr);
4355 }
4356
4357 room = scol - col + tabwidth - 1;
4358 if (room > 0)
4359 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004360 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004361 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004362 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004363 len = vim_strsize(NameBuff);
4364 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004365 if (has_mbyte)
4366 while (len > room)
4367 {
4368 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004369 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004370 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004371 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004372 {
4373 p += len - room;
4374 len = room;
4375 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004376 if (len > Columns - col - 1)
4377 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004378
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004379 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004380 col += len;
4381 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004382 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004383
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004384 // Store the tab page number in TabPageIdxs[], so that
4385 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004386 ++tabcount;
4387 while (scol < col)
4388 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004389 }
4390
Bram Moolenaar238a5642006-02-21 22:12:05 +00004391 if (use_sep_chars)
4392 c = '_';
4393 else
4394 c = ' ';
4395 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004396
Luuk van Baalba936f62022-12-15 13:15:39 +00004397 // Draw the 'showcmd' information if 'showcmdloc' == "tabline".
4398 if (p_sc && *p_sloc == 't')
4399 {
4400 int width = MIN(10, (int)Columns - col - (tabcount > 1) * 3);
4401
4402 if (width > 0)
4403 screen_puts_len(showcmd_buf, width, 0, (int)Columns
4404 - width - (tabcount > 1) * 2, attr_nosel);
4405 }
4406
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004407 // Put an "X" for closing the current tab if there are several.
Luuk van Baalba936f62022-12-15 13:15:39 +00004408 if (tabcount > 1)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004409 {
4410 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4411 TabPageIdxs[Columns - 1] = -999;
4412 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004413 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004414
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004415 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4416 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004417 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004418}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004419
4420/*
4421 * Get buffer name for "buf" into NameBuff[].
4422 * Takes care of special buffer names and translates special characters.
4423 */
4424 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004425get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004426{
4427 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004428 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004429 else
4430 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4431 trans_characters(NameBuff, MAXPATHL);
4432}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004433
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434/*
4435 * Get the character to use in a status line. Get its attributes in "*attr".
4436 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004437 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004438fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439{
4440 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004441
4442#ifdef FEAT_TERMINAL
4443 if (bt_terminal(wp->w_buffer))
4444 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004445 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004446 {
4447 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004448 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004449 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004450 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004451 {
4452 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004453 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004454 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004455 }
4456 else
4457#endif
4458 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004460 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004461 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463 else
4464 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004465 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004466 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
Christian Brabandt6a650bf2023-11-08 21:23:29 +01004468 return fill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471/*
4472 * Get the character to use in a separator between vertically split windows.
4473 * Get its attributes in "*attr".
4474 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004475 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004476fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004478 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004479 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 return '|';
4481 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004482 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485/*
4486 * Return TRUE if redrawing should currently be done.
4487 */
4488 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004489redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004491#ifdef FEAT_EVAL
4492 if (disable_redraw_for_testing)
4493 return 0;
4494 else
4495#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004496 return ((RedrawingDisabled == 0
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004497#ifdef FEAT_EVAL
4498 || ignore_redraw_flag_for_testing
4499#endif
4500 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501}
4502
4503/*
4504 * Return TRUE if printing messages should currently be done.
4505 */
4506 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004507messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004509 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510}
4511
Bram Moolenaare677df82019-09-02 22:31:11 +02004512/*
4513 * Compute columns for ruler and shown command. 'sc_col' is also used to
4514 * decide what the maximum length of a message on the status line can be.
4515 * If there is a status line for the last window, 'sc_col' is independent
4516 * of 'ru_col'.
4517 */
4518
4519#define COL_RULER 17 // columns needed by standard ruler
4520
4521 void
4522comp_col(void)
4523{
Sean Dewar876f5fb2023-08-17 22:40:05 +02004524 int last_has_status = last_stl_height(FALSE) > 0;
Bram Moolenaare677df82019-09-02 22:31:11 +02004525
4526 sc_col = 0;
4527 ru_col = 0;
4528 if (p_ru)
4529 {
Martin Tournoijba43e762022-10-13 22:12:15 +01004530#ifdef FEAT_STL_OPT
Bram Moolenaare677df82019-09-02 22:31:11 +02004531 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004532#else
Bram Moolenaare677df82019-09-02 22:31:11 +02004533 ru_col = COL_RULER + 1;
Martin Tournoijba43e762022-10-13 22:12:15 +01004534#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004535 // no last status line, adjust sc_col
4536 if (!last_has_status)
4537 sc_col = ru_col;
4538 }
Sam-programs062141b2024-02-29 17:40:29 +01004539 if (p_sc && *p_sloc == 'l')
Bram Moolenaare677df82019-09-02 22:31:11 +02004540 {
4541 sc_col += SHOWCMD_COLS;
4542 if (!p_ru || last_has_status) // no need for separating space
4543 ++sc_col;
4544 }
4545 sc_col = Columns - sc_col;
4546 ru_col = Columns - ru_col;
4547 if (sc_col <= 0) // screen too narrow, will become a mess
4548 sc_col = 1;
4549 if (ru_col <= 0)
4550 ru_col = 1;
Bram Moolenaare677df82019-09-02 22:31:11 +02004551#ifdef FEAT_EVAL
4552 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4553#endif
4554}
4555
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004556#if defined(FEAT_LINEBREAK) || defined(PROTO)
4557/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004558 * Return the width of the 'number' and 'relativenumber' column.
4559 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004560 * Otherwise it depends on 'numberwidth' and the line count.
4561 */
4562 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004563number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004564{
4565 int n;
4566 linenr_T lnum;
4567
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004568 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004569 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004570 lnum = wp->w_height;
4571 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004572 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004573 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004574
Bram Moolenaar6b314672015-03-20 15:42:10 +01004575 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004576 return wp->w_nrwidth_width;
4577 wp->w_nrwidth_line_count = lnum;
4578
4579 n = 0;
4580 do
4581 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004582 lnum /= 10;
4583 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004584 } while (lnum > 0);
4585
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004586 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004587 if (n < wp->w_p_nuw - 1)
4588 n = wp->w_p_nuw - 1;
4589
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004590# ifdef FEAT_SIGNS
4591 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4592 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004593 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004594 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4595 n = 2;
4596# endif
4597
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004598 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004599 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004600 return n;
4601}
4602#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004603
Bram Moolenaar113e1072019-01-20 15:30:40 +01004604#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004605/*
4606 * Return the current cursor column. This is the actual position on the
4607 * screen. First column is 0.
4608 */
4609 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004610screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004611{
4612 return screen_cur_col;
4613}
4614
4615/*
4616 * Return the current cursor row. This is the actual position on the screen.
4617 * First row is 0.
4618 */
4619 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004620screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004621{
4622 return screen_cur_row;
4623}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004624#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004625
4626/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004627 * Calls mb_ptr2char_adv(p) and returns the character.
4628 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4629 */
4630 static int
4631get_encoded_char_adv(char_u **p)
4632{
4633 char_u *s = *p;
4634
4635 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4636 {
4637 varnumber_T num = 0;
4638 int bytes;
4639 int n;
4640
4641 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4642 {
4643 *p += 2;
4644 n = hexhex2nr(*p);
4645 if (n < 0)
4646 return 0;
4647 num = num * 256 + n;
4648 }
4649 *p += 2;
4650 return num;
4651 }
4652 return mb_ptr2char_adv(p);
4653}
4654
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004655struct charstab
4656{
4657 int *cp;
4658 char *name;
4659};
4660static fill_chars_T fill_chars;
4661static struct charstab filltab[] =
4662{
4663 {&fill_chars.stl, "stl"},
4664 {&fill_chars.stlnc, "stlnc"},
4665 {&fill_chars.vert, "vert"},
4666 {&fill_chars.fold, "fold"},
4667 {&fill_chars.foldopen, "foldopen"},
4668 {&fill_chars.foldclosed, "foldclose"},
4669 {&fill_chars.foldsep, "foldsep"},
4670 {&fill_chars.diff, "diff"},
4671 {&fill_chars.eob, "eob"},
4672 {&fill_chars.lastline, "lastline"},
4673};
4674static lcs_chars_T lcs_chars;
4675static struct charstab lcstab[] =
4676{
4677 {&lcs_chars.eol, "eol"},
4678 {&lcs_chars.ext, "extends"},
4679 {&lcs_chars.nbsp, "nbsp"},
4680 {&lcs_chars.prec, "precedes"},
4681 {&lcs_chars.space, "space"},
4682 {&lcs_chars.tab2, "tab"},
4683 {&lcs_chars.trail, "trail"},
4684 {&lcs_chars.lead, "lead"},
4685#ifdef FEAT_CONCEAL
4686 {&lcs_chars.conceal, "conceal"},
4687#else
4688 {NULL, "conceal"},
4689#endif
zeertzjq1f025b02023-09-30 12:43:07 +02004690 {NULL, "multispace"},
4691 {NULL, "leadmultispace"},
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004692};
4693
zeertzjq6a8d2e12024-01-17 20:54:49 +01004694 static char *
4695field_value_err(char *errbuf, size_t errbuflen, char *fmt, char *field)
4696{
4697 if (errbuf == NULL)
4698 return "";
4699 vim_snprintf(errbuf, errbuflen, _(fmt), field);
4700 return errbuf;
4701}
4702
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004703/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004704 * Handle setting 'listchars' or 'fillchars'.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004705 * "value" points to either the global or the window-local value.
zeertzjqd619d6a2023-05-08 15:56:21 +01004706 * "is_listchars" is TRUE for "listchars" and FALSE for "fillchars".
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004707 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004708 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004709 * Returns error message, NULL if it's OK.
4710 */
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004711 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004712set_chars_option(win_T *wp, char_u *value, int is_listchars, int apply,
4713 char *errbuf, size_t errbuflen)
Bram Moolenaare677df82019-09-02 22:31:11 +02004714{
zeertzjq1f025b02023-09-30 12:43:07 +02004715 int round, i, len, entries;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004716 char_u *p, *s;
4717 int c1 = 0, c2 = 0, c3 = 0;
4718 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4719 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4720 int multispace_len = 0; // Length of lcs-multispace string
4721 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004722
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004723 struct charstab *tab;
4724
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004725 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004726 {
4727 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004728 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004729 entries = ARRAY_LENGTH(lcstab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004730 if (wp->w_p_lcs[0] == NUL)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004731 value = p_lcs; // local value is empty, use the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004732 }
4733 else
4734 {
4735 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004736 entries = ARRAY_LENGTH(filltab);
zeertzjqd619d6a2023-05-08 15:56:21 +01004737 if (wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004738 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004739 }
4740
4741 // first round: check for valid value, second round: assign values
zeertzjq00624a22023-11-23 20:47:16 +01004742 for (round = 0; round <= (apply ? 1 : 0); ++round)
Bram Moolenaare677df82019-09-02 22:31:11 +02004743 {
4744 if (round > 0)
4745 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004746 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004747 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004748 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004749 for (i = 0; i < entries; ++i)
4750 if (tab[i].cp != NULL)
4751 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004752 lcs_chars.tab1 = NUL;
4753 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004754
Mike Williamsf5785cf2021-09-13 22:17:38 +02004755 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004756 {
4757 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004758 if (lcs_chars.multispace != NULL)
4759 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004760 }
4761 else
4762 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004763
4764 if (lead_multispace_len > 0)
4765 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004766 lcs_chars.leadmultispace =
4767 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004768 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
4769 }
4770 else
4771 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02004772 }
4773 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004774 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004775 fill_chars.stl = ' ';
4776 fill_chars.stlnc = ' ';
4777 fill_chars.vert = ' ';
4778 fill_chars.fold = '-';
4779 fill_chars.foldopen = '-';
4780 fill_chars.foldclosed = '+';
4781 fill_chars.foldsep = '|';
4782 fill_chars.diff = '-';
4783 fill_chars.eob = '~';
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01004784 fill_chars.lastline = '@';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01004785 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004786 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004787 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02004788 while (*p)
4789 {
4790 for (i = 0; i < entries; ++i)
4791 {
4792 len = (int)STRLEN(tab[i].name);
zeertzjq6a8d2e12024-01-17 20:54:49 +01004793 if (!(STRNCMP(p, tab[i].name, len) == 0 && p[len] == ':'))
zeertzjq1f025b02023-09-30 12:43:07 +02004794 continue;
Bram Moolenaare677df82019-09-02 22:31:11 +02004795
zeertzjq1f025b02023-09-30 12:43:07 +02004796 if (is_listchars && strcmp(tab[i].name, "multispace") == 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004797 {
4798 s = p + len + 1;
4799 if (round == 0)
4800 {
4801 // Get length of lcs-multispace string in first round
4802 last_multispace = p;
4803 multispace_len = 0;
4804 while (*s != NUL && *s != ',')
4805 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004806 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00004807 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004808 return field_value_err(errbuf, errbuflen,
4809 e_wrong_character_width_for_field_str,
4810 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004811 ++multispace_len;
4812 }
4813 if (multispace_len == 0)
4814 // lcs-multispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004815 return field_value_err(errbuf, errbuflen,
4816 e_wrong_number_of_characters_for_field_str,
4817 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004818 p = s;
4819 }
4820 else
4821 {
4822 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02004823
zeertzjqf14b8ba2021-09-10 16:58:30 +02004824 while (*s != NUL && *s != ',')
4825 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004826 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004827 if (p == last_multispace)
4828 lcs_chars.multispace[multispace_pos++] = c1;
4829 }
4830 p = s;
4831 }
zeertzjq1f025b02023-09-30 12:43:07 +02004832 break;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004833 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004834
zeertzjq1f025b02023-09-30 12:43:07 +02004835 if (is_listchars && strcmp(tab[i].name, "leadmultispace") == 0)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004836 {
zeertzjq1f025b02023-09-30 12:43:07 +02004837 s = p + len + 1;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004838 if (round == 0)
4839 {
zeertzjqb5f08012022-06-09 13:55:28 +01004840 // get length of lcs-leadmultispace string in first
4841 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004842 last_lmultispace = p;
4843 lead_multispace_len = 0;
4844 while (*s != NUL && *s != ',')
4845 {
4846 c1 = get_encoded_char_adv(&s);
4847 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004848 return field_value_err(errbuf, errbuflen,
4849 e_wrong_character_width_for_field_str,
4850 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004851 ++lead_multispace_len;
4852 }
4853 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01004854 // lcs-leadmultispace cannot be an empty string
zeertzjq6a8d2e12024-01-17 20:54:49 +01004855 return field_value_err(errbuf, errbuflen,
4856 e_wrong_number_of_characters_for_field_str,
4857 tab[i].name);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004858 p = s;
4859 }
4860 else
4861 {
4862 int multispace_pos = 0;
4863
4864 while (*s != NUL && *s != ',')
4865 {
4866 c1 = get_encoded_char_adv(&s);
4867 if (p == last_lmultispace)
4868 lcs_chars.leadmultispace[multispace_pos++] = c1;
4869 }
4870 p = s;
4871 }
zeertzjq1f025b02023-09-30 12:43:07 +02004872 break;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004873 }
zeertzjq1f025b02023-09-30 12:43:07 +02004874
4875 c2 = c3 = 0;
4876 s = p + len + 1;
zeertzjq6a8d2e12024-01-17 20:54:49 +01004877 if (*s == NUL)
4878 return field_value_err(errbuf, errbuflen,
4879 e_wrong_number_of_characters_for_field_str,
4880 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004881 c1 = get_encoded_char_adv(&s);
4882 if (char2cells(c1) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004883 return field_value_err(errbuf, errbuflen,
4884 e_wrong_character_width_for_field_str,
4885 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004886 if (tab[i].cp == &lcs_chars.tab2)
4887 {
4888 if (*s == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004889 return field_value_err(errbuf, errbuflen,
4890 e_wrong_number_of_characters_for_field_str,
4891 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004892 c2 = get_encoded_char_adv(&s);
4893 if (char2cells(c2) > 1)
zeertzjq6a8d2e12024-01-17 20:54:49 +01004894 return field_value_err(errbuf, errbuflen,
4895 e_wrong_character_width_for_field_str,
4896 tab[i].name);
zeertzjq1f025b02023-09-30 12:43:07 +02004897 if (!(*s == ',' || *s == NUL))
4898 {
4899 c3 = get_encoded_char_adv(&s);
4900 if (char2cells(c3) > 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 }
4905 }
4906
4907 if (*s == ',' || *s == NUL)
4908 {
4909 if (round > 0)
4910 {
4911 if (tab[i].cp == &lcs_chars.tab2)
4912 {
4913 lcs_chars.tab1 = c1;
4914 lcs_chars.tab2 = c2;
4915 lcs_chars.tab3 = c3;
4916 }
4917 else if (tab[i].cp != NULL)
4918 *(tab[i].cp) = c1;
4919
4920 }
4921 p = s;
4922 break;
4923 }
zeertzjq6a8d2e12024-01-17 20:54:49 +01004924 else
4925 return field_value_err(errbuf, errbuflen,
4926 e_wrong_number_of_characters_for_field_str,
4927 tab[i].name);
zeertzjqf14b8ba2021-09-10 16:58:30 +02004928 }
4929
zeertzjq1f025b02023-09-30 12:43:07 +02004930 if (i == entries)
4931 return e_invalid_argument;
4932
Bram Moolenaare677df82019-09-02 22:31:11 +02004933 if (*p == ',')
4934 ++p;
4935 }
4936 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004937
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004938 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004939 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004940 if (is_listchars)
4941 {
4942 vim_free(wp->w_lcs_chars.multispace);
4943 vim_free(wp->w_lcs_chars.leadmultispace);
4944 wp->w_lcs_chars = lcs_chars;
4945 }
4946 else
4947 {
4948 wp->w_fill_chars = fill_chars;
4949 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02004950 }
Bram Moolenaare677df82019-09-02 22:31:11 +02004951
4952 return NULL; // no error
4953}
zeertzjq8ca29b62022-08-09 12:53:14 +01004954
4955/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004956 * Handle the new value of 'fillchars'.
4957 */
4958 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004959set_fillchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4960 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004961{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004962 return set_chars_option(wp, val, FALSE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004963}
4964
4965/*
4966 * Handle the new value of 'listchars'.
4967 */
4968 char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01004969set_listchars_option(win_T *wp, char_u *val, int apply, char *errbuf,
4970 size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004971{
zeertzjq6a8d2e12024-01-17 20:54:49 +01004972 return set_chars_option(wp, val, TRUE, apply, errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004973}
4974
4975/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004976 * Function given to ExpandGeneric() to obtain possible arguments of the
4977 * 'fillchars' option.
4978 */
4979 char_u *
4980get_fillchars_name(expand_T *xp UNUSED, int idx)
4981{
4982 if (idx >= (int)(sizeof(filltab) / sizeof(filltab[0])))
4983 return NULL;
4984
4985 return (char_u*)filltab[idx].name;
4986}
4987
4988/*
4989 * Function given to ExpandGeneric() to obtain possible arguments of the
4990 * 'listchars' option.
4991 */
4992 char_u *
4993get_listchars_name(expand_T *xp UNUSED, int idx)
4994{
4995 if (idx >= (int)(sizeof(lcstab) / sizeof(lcstab[0])))
4996 return NULL;
4997
4998 return (char_u*)lcstab[idx].name;
4999}
5000
5001/*
zeertzjq8ca29b62022-08-09 12:53:14 +01005002 * Check all global and local values of 'listchars' and 'fillchars'.
5003 * Return an untranslated error messages if any of them is invalid, NULL
5004 * otherwise.
5005 */
5006 char *
5007check_chars_options(void)
5008{
5009 tabpage_T *tp;
5010 win_T *wp;
5011
zeertzjq6a8d2e12024-01-17 20:54:49 +01005012 if (set_listchars_option(curwin, p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005013 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005014 if (set_fillchars_option(curwin, p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005015 return e_conflicts_with_value_of_fillchars;
5016 FOR_ALL_TAB_WINDOWS(tp, wp)
5017 {
zeertzjq6a8d2e12024-01-17 20:54:49 +01005018 if (set_listchars_option(wp, wp->w_p_lcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005019 return e_conflicts_with_value_of_listchars;
zeertzjq6a8d2e12024-01-17 20:54:49 +01005020 if (set_fillchars_option(wp, wp->w_p_fcs, FALSE, NULL, 0) != NULL)
zeertzjq8ca29b62022-08-09 12:53:14 +01005021 return e_conflicts_with_value_of_fillchars;
5022 }
5023 return NULL;
5024}