blob: 06bda0d63a83e62469e25ae7158e1fd0e589fa9a [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.
Bram Moolenaarb9081882022-07-09 04:56:24 +010020 * ScreenCols[off] Contains the byte offset in the line. -1 means not
21 * available (below last line), MAXCOL means after the end
22 * of the line.
23 *
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 Moolenaarb13d3402022-08-29 13:44:28 +010052static void 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{
Bram Moolenaarea042672021-06-29 20:22:32 +020097 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin) != was_concealed)
Bram Moolenaarf5963f72010-07-23 22:10:27 +020098 {
Bram Moolenaarea042672021-06-29 20:22:32 +020099 int wcol = curwin->w_wcol;
100
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200101 need_cursor_line_redraw = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100102 // Need to recompute cursor column, e.g., when starting Visual mode
103 // without concealing.
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200104 curs_columns(TRUE);
Bram Moolenaarea042672021-06-29 20:22:32 +0200105
106 // When concealing now w_wcol will be computed wrong, keep the previous
107 // value, it will be updated in win_line().
108 if (!was_concealed)
109 curwin->w_wcol = wcol;
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200110 }
111}
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
382screen_get_current_line_off()
383{
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 Moolenaar071d4272004-06-13 20:20:40 +0000423 * Move one "cooked" screen line to the screen, but only the characters that
424 * have actually changed. Handle insert/delete character.
425 * "coloff" gives the first column on the screen for this line.
426 * "endcol" gives the columns where valid characters are.
427 * "clear_width" is the width of the window. It's > 0 if the rest of the line
428 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200429 * "flags" can have bits:
430 * SLF_POPUP popup window
431 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
433 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
434 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200435 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100436screen_line(
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100437 win_T *wp,
438 int row,
439 int coloff,
440 int endcol,
441 int clear_width,
442 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443{
444 unsigned off_from;
445 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000446 unsigned max_off_from;
447 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 int hl;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100450 int force = FALSE; // force update rest of the line
451 int redraw_this // bool: does character need redraw?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100453 = TRUE // For GUI when while-loop empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454#endif
455 ;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100456 int redraw_next; // redraw_this for next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 int clear_next = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100458 int char_cells; // 1: normal char
459 // 2: occupies two display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100461 // Check for illegal row and col, just in case.
Bram Moolenaar5ad15df2012-03-16 19:07:58 +0100462 if (row >= Rows)
463 row = Rows - 1;
464 if (endcol > Columns)
465 endcol = Columns;
466
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467# ifdef FEAT_CLIPBOARD
468 clip_may_clear_selection(row, row);
469# endif
470
471 off_from = (unsigned)(current_ScreenLine - ScreenLines);
472 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +0000473 max_off_from = off_from + screen_Columns;
474 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475
476#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200477 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100479 // Clear rest first, because it's left of the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 if (clear_width > 0)
481 {
482 while (col <= endcol && ScreenLines[off_to] == ' '
483 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100484 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 {
486 ++off_to;
487 ++col;
488 }
489 if (col <= endcol)
490 screen_fill(row, row + 1, col + coloff,
491 endcol + coloff + 1, ' ', ' ', 0);
492 }
493 col = endcol + 1;
494 off_to = LineOffset[row] + col + coloff;
495 off_from += col;
496 endcol = (clear_width > 0 ? clear_width : -clear_width);
497 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100498#endif // FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100500#ifdef FEAT_PROP_POPUP
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100501 // First char of a popup window may go on top of the right half of a
502 // double-wide character. Clear the left half to avoid it getting the popup
503 // window background color.
Bram Moolenaar927495b2020-11-06 17:58:35 +0100504 if (coloff > 0 && enc_utf8
505 && ScreenLines[off_to] == 0
Bram Moolenaardc0cf1d2020-08-23 15:09:36 +0200506 && ScreenLinesUC[off_to - 1] != 0
507 && (*mb_char2cells)(ScreenLinesUC[off_to - 1]) > 1)
Bram Moolenaar92e25ab2019-11-26 22:39:10 +0100508 {
509 ScreenLines[off_to - 1] = ' ';
510 ScreenLinesUC[off_to - 1] = 0;
511 screen_char(off_to - 1, row, col + coloff - 1);
512 }
513#endif
514
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
516
517 while (col < endcol)
518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +0000520 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 else
522 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523
524 redraw_this = redraw_next;
Bram Moolenaarb9081882022-07-09 04:56:24 +0100525 redraw_next = force || char_needs_redraw(off_from + char_cells,
526 off_to + char_cells, endcol - col - char_cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
528#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100529 // If the next character was bold, then redraw the current character to
530 // remove any pixels that might have spilt over into us. This only
531 // happens in the GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (redraw_next && gui.in_use)
533 {
Bram Moolenaarb9081882022-07-09 04:56:24 +0100534 hl = ScreenAttrs[off_to + char_cells];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000535 if (hl > HL_ALL)
536 hl = syn_attr2attr(hl);
537 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 redraw_this = TRUE;
539 }
540#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100541#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200542 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +0200543 redraw_this = FALSE;
544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 if (redraw_this)
546 {
547 /*
548 * Special handling when 'xs' termcap flag set (hpterm):
549 * Attributes for characters are stored at the position where the
550 * cursor is when writing the highlighting code. The
551 * start-highlighting code must be written with the cursor on the
552 * first highlighted character. The stop-highlighting code must
553 * be written with the cursor just after the last highlighted
554 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100555 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 * to clear the rest of the line, and force redrawing it
557 * completely.
558 */
559 if ( p_wiv
560 && !force
561#ifdef FEAT_GUI
562 && !gui.in_use
563#endif
564 && ScreenAttrs[off_to] != 0
565 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
566 {
567 /*
568 * Need to remove highlighting attributes here.
569 */
570 windgoto(row, col + coloff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100571 out_str(T_CE); // clear rest of this screen line
572 screen_start(); // don't know where cursor is now
573 force = TRUE; // force redraw of rest of the line
574 redraw_next = TRUE; // or else next char would miss out
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576 /*
577 * If the previous character was highlighted, need to stop
578 * highlighting at this character.
579 */
580 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
581 {
582 screen_attr = ScreenAttrs[off_to - 1];
583 term_windgoto(row, col + coloff);
584 screen_stop_highlight();
585 }
586 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100587 screen_attr = 0; // highlighting has stopped
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 if (enc_dbcs != 0)
590 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100591 // Check if overwriting a double-byte with a single-byte or
592 // the other way around requires another character to be
593 // redrawn. For UTF-8 this isn't needed, because comparing
594 // ScreenLinesUC[] is sufficient.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 if (char_cells == 1
596 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000597 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100599 // Writing a single-cell character over a double-cell
600 // character: need to redraw the next cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 ScreenLines[off_to + 1] = 0;
602 redraw_next = TRUE;
603 }
604 else if (char_cells == 2
605 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +0000606 && (*mb_off2cells)(off_to, max_off_to) == 1
607 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100609 // Writing the second half of a double-cell character over
610 // a double-cell character: need to redraw the second
611 // cell.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 ScreenLines[off_to + 2] = 0;
613 redraw_next = TRUE;
614 }
615
616 if (enc_dbcs == DBCS_JPNU)
617 ScreenLines2[off_to] = ScreenLines2[off_from];
618 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100619 // When writing a single-width character over a double-width
620 // character and at the end of the redrawn text, need to clear out
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000621 // the right half of the old character.
622 // Also required when writing the right half of a double-width
623 // char over the left half of an existing one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (has_mbyte && col + char_cells == endcol
625 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +0000626 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +0000628 && (*mb_off2cells)(off_to, max_off_to) == 1
629 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (enc_utf8)
634 {
635 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
636 if (ScreenLinesUC[off_from] != 0)
637 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000638 int i;
639
640 for (i = 0; i < Screen_mco; ++i)
641 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643 }
644 if (char_cells == 2)
645 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
647#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100648 // The bold trick makes a single column of pixels appear in the
649 // next character. When a bold character is removed, the next
650 // character should be redrawn too. This happens for our own GUI
651 // and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 if (
653# ifdef FEAT_GUI
654 gui.in_use
655# endif
656# if defined(FEAT_GUI) && defined(UNIX)
657 ||
658# endif
659# ifdef UNIX
660 term_is_xterm
661# endif
662 )
663 {
664 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000665 if (hl > HL_ALL)
666 hl = syn_attr2attr(hl);
667 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 redraw_next = TRUE;
669 }
670#endif
671 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaarb9081882022-07-09 04:56:24 +0100672 ScreenCols[off_to] = ScreenCols[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +0100673
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100674 // For simplicity set the attributes of second half of a
675 // double-wide character equal to the first half.
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000676 if (char_cells == 2)
Bram Moolenaarb9081882022-07-09 04:56:24 +0100677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaarb9081882022-07-09 04:56:24 +0100679 ScreenCols[off_to + 1] = ScreenCols[off_from + 1];
680 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000681
682 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 screen_char(off_to, row, col + coloff);
686 }
687 else if ( p_wiv
688#ifdef FEAT_GUI
689 && !gui.in_use
690#endif
691 && col + coloff > 0)
692 {
693 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
694 {
695 /*
696 * Don't output stop-highlight when moving the cursor, it will
697 * stop the highlighting when it should continue.
698 */
699 screen_attr = 0;
700 }
701 else if (screen_attr != 0)
702 screen_stop_highlight();
703 }
704
Bram Moolenaarb9081882022-07-09 04:56:24 +0100705 ScreenCols[off_to] = ScreenCols[off_from];
706 if (char_cells == 2)
707 ScreenCols[off_to + 1] = ScreenCols[off_from];
708
709 off_to += char_cells;
710 off_from += char_cells;
711 col += char_cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 }
713
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (clear_next)
715 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100716 // Clear the second half of a double-wide character of which the left
717 // half was overwritten with a single-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 ScreenLines[off_to] = ' ';
719 if (enc_utf8)
720 ScreenLinesUC[off_to] = 0;
721 screen_char(off_to, row, col + coloff);
722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
724 if (clear_width > 0
725#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200726 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#endif
728 )
729 {
730#ifdef FEAT_GUI
731 int startCol = col;
732#endif
733
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100734 // blank out the rest of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 while (col < clear_width && ScreenLines[off_to] == ' '
736 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +0100737 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 {
Bram Moolenaarb9081882022-07-09 04:56:24 +0100739 ScreenCols[off_to] = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 ++off_to;
741 ++col;
742 }
743 if (col < clear_width)
744 {
745#ifdef FEAT_GUI
746 /*
747 * In the GUI, clearing the rest of the line may leave pixels
748 * behind if the first character cleared was bold. Some bold
749 * fonts spill over the left. In this case we redraw the previous
750 * character too. If we didn't skip any blanks above, then we
751 * only redraw if the character wasn't already redrawn anyway.
752 */
Bram Moolenaar9c697322006-10-09 20:11:17 +0000753 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
755 hl = ScreenAttrs[off_to];
756 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +0000757 {
758 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +0100759
Bram Moolenaar9c697322006-10-09 20:11:17 +0000760 if (enc_utf8)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100761 // for utf-8, ScreenLines[char_offset + 1] == 0 means
762 // that its width is 2.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000763 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
764 else if (enc_dbcs != 0)
765 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100766 // find previous character by counting from first
767 // column and get its width.
Bram Moolenaar9c697322006-10-09 20:11:17 +0000768 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +0000769 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +0000770
771 while (off < off_to)
772 {
Bram Moolenaar367329b2007-08-30 11:53:22 +0000773 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +0000774 off += prev_cells;
775 }
776 }
777
778 if (enc_dbcs != 0 && prev_cells > 1)
779 screen_char_2(off_to - prev_cells, row,
780 col + coloff - prev_cells);
781 else
Bram Moolenaar9c697322006-10-09 20:11:17 +0000782 screen_char(off_to - prev_cells, row,
783 col + coloff - prev_cells);
784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786#endif
787 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
788 ' ', ' ', 0);
Bram Moolenaarb9081882022-07-09 04:56:24 +0100789 while (col < clear_width)
790 {
791 ScreenCols[off_to++] = MAXCOL;
792 ++col;
793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795 }
796
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200797 if (clear_width > 0
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100798#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200799 && !(flags & SLF_POPUP) // no separator for popup window
800#endif
801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200803 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200804 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200805 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100807#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +0200808 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200811 int c;
812
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100813 c = fillchar_vsep(&hl, wp);
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200814 if (ScreenLines[off_to] != (schar_T)c
815 || (enc_utf8 && (int)ScreenLinesUC[off_to]
816 != (c >= 0x80 ? c : 0))
817 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200819 ScreenLines[off_to] = c;
820 ScreenAttrs[off_to] = hl;
821 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200823 if (c >= 0x80)
824 {
825 ScreenLinesUC[off_to] = c;
826 ScreenLinesC[0][off_to] = 0;
827 }
828 else
829 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +0200831 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834 }
835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 LineWraps[row] = FALSE;
837 }
838}
839
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000840#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000842 * Mirror text "str" for right-left displaying.
843 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000845 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100846rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
848 char_u *p1, *p2;
849 int t;
850
851 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
852 {
853 t = *p1;
854 *p1 = *p2;
855 *p2 = t;
856 }
857}
858#endif
859
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 * Draw the verticap separator right of window "wp" starting with line "row".
862 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200863 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100864draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865{
866 int hl;
867 int c;
868
869 if (wp->w_vsep_width)
870 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100871 // draw the vertical separator right of this window
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100872 c = fillchar_vsep(&hl, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
874 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
875 c, ' ', hl);
876 }
877}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100879static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880
881/*
Bram Moolenaar367329b2007-08-30 11:53:22 +0000882 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 */
884 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100885status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886{
887 int len = 0;
888
889#ifdef FEAT_MENU
890 int emenu = (xp->xp_context == EXPAND_MENUS
891 || xp->xp_context == EXPAND_MENUNAMES);
892
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100893 // Check for menu separators - replace with '|'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (emenu && menu_is_separator(s))
895 return 1;
896#endif
897
898 while (*s != NUL)
899 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000900 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +0000901 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100902 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904
905 return len;
906}
907
908/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000909 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000910 * These are backslashes used for escaping. Do show backslashes in help tags.
911 */
912 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100913skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000914{
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000915 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000916#ifdef FEAT_MENU
917 || ((xp->xp_context == EXPAND_MENUS
918 || xp->xp_context == EXPAND_MENUNAMES)
919 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
920#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +0000921 )
922 {
923#ifndef BACKSLASH_IN_FILENAME
924 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
925 return 2;
926#endif
927 return 1;
928 }
929 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +0000930}
931
932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 * Show wildchar matches in the status line.
934 * Show at least the "match" item.
935 * We start at item 'first_match' in the list and show all matches that fit.
936 *
937 * If inversion is possible we use it. Else '=' characters are used.
938 */
939 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100940win_redr_status_matches(
941 expand_T *xp,
942 int num_matches,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100943 char_u **matches, // list of matches
Bram Moolenaar05540972016-01-30 20:31:25 +0100944 int match,
945 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
948 int row;
949 char_u *buf;
950 int len;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100951 int clen; // length in screen cells
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 int fillchar;
953 int attr;
954 int i;
955 int highlight = TRUE;
956 char_u *selstart = NULL;
957 int selstart_col = 0;
958 char_u *selend = NULL;
959 static int first_match = 0;
960 int add_left = FALSE;
961 char_u *s;
962#ifdef FEAT_MENU
963 int emenu;
964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100967 if (matches == NULL) // interrupted completion?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 return;
969
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000970 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200971 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000972 else
Bram Moolenaar964b3742019-05-24 18:54:09 +0200973 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (buf == NULL)
975 return;
976
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100977 if (match == -1) // don't show match but original text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 {
979 match = 0;
980 highlight = FALSE;
981 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100982 // count 1 for the ending ">"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 clen = status_match_len(xp, L_MATCH(match)) + 3;
984 if (match == 0)
985 first_match = 0;
986 else if (match < first_match)
987 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100988 // jumping left, as far as we can go
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 first_match = match;
990 add_left = TRUE;
991 }
992 else
993 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100994 // check if match fits on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 for (i = first_match; i < match; ++i)
996 clen += status_match_len(xp, L_MATCH(i)) + 2;
997 if (first_match > 0)
998 clen += 2;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100999 // jumping right, put match at the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if ((long)clen > Columns)
1001 {
1002 first_match = match;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001003 // if showing the last match, we can add some on the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 clen = 2;
1005 for (i = match; i < num_matches; ++i)
1006 {
1007 clen += status_match_len(xp, L_MATCH(i)) + 2;
1008 if ((long)clen >= Columns)
1009 break;
1010 }
1011 if (i == num_matches)
1012 add_left = TRUE;
1013 }
1014 }
1015 if (add_left)
1016 while (first_match > 0)
1017 {
1018 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
1019 if ((long)clen >= Columns)
1020 break;
1021 --first_match;
1022 }
1023
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001024 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
1026 if (first_match == 0)
1027 {
1028 *buf = NUL;
1029 len = 0;
1030 }
1031 else
1032 {
1033 STRCPY(buf, "< ");
1034 len = 2;
1035 }
1036 clen = len;
1037
1038 i = first_match;
1039 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
1040 {
1041 if (i == match)
1042 {
1043 selstart = buf + len;
1044 selstart_col = clen;
1045 }
1046
1047 s = L_MATCH(i);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001048 // Check for menu separators - replace with '|'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#ifdef FEAT_MENU
1050 emenu = (xp->xp_context == EXPAND_MENUS
1051 || xp->xp_context == EXPAND_MENUNAMES);
1052 if (emenu && menu_is_separator(s))
1053 {
1054 STRCPY(buf + len, transchar('|'));
1055 l = (int)STRLEN(buf + len);
1056 len += l;
1057 clen += l;
1058 }
1059 else
1060#endif
1061 for ( ; *s != NUL; ++s)
1062 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001063 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001065 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
1067 STRNCPY(buf + len, s, l);
1068 s += l - 1;
1069 len += l;
1070 }
1071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 STRCPY(buf + len, transchar_byte(*s));
1074 len += (int)STRLEN(buf + len);
1075 }
1076 }
1077 if (i == match)
1078 selend = buf + len;
1079
1080 *(buf + len++) = ' ';
1081 *(buf + len++) = ' ';
1082 clen += 2;
1083 if (++i == num_matches)
1084 break;
1085 }
1086
1087 if (i != num_matches)
1088 {
1089 *(buf + len++) = '>';
1090 ++clen;
1091 }
1092
1093 buf[len] = NUL;
1094
1095 row = cmdline_row - 1;
1096 if (row >= 0)
1097 {
1098 if (wild_menu_showing == 0)
1099 {
1100 if (msg_scrolled > 0)
1101 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001102 // Put the wildmenu just above the command line. If there is
1103 // no room, scroll the screen one line up.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (cmdline_row == Rows - 1)
1105 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001106 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 ++msg_scrolled;
1108 }
1109 else
1110 {
1111 ++cmdline_row;
1112 ++row;
1113 }
1114 wild_menu_showing = WM_SCROLLED;
1115 }
1116 else
1117 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001118 // Create status line if needed by setting 'laststatus' to 2.
1119 // Set 'winminheight' to zero to avoid that the window is
1120 // resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (lastwin->w_status_height == 0)
1122 {
1123 save_p_ls = p_ls;
1124 save_p_wmh = p_wmh;
1125 p_ls = 2;
1126 p_wmh = 0;
1127 last_status(FALSE);
1128 }
1129 wild_menu_showing = WM_SHOWN;
1130 }
1131 }
1132
1133 screen_puts(buf, row, 0, attr);
1134 if (selstart != NULL && highlight)
1135 {
1136 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001137 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139
1140 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
1141 }
1142
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 vim_free(buf);
1145}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 * Return TRUE if the status line of window "wp" is connected to the status
1149 * line of the window right of it. If not, then it's a vertical separator.
1150 * Only call if (wp->w_vsep_width != 0).
1151 */
1152 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001153stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
1155 frame_T *fr;
1156
1157 fr = wp->w_frame;
1158 while (fr->fr_parent != NULL)
1159 {
1160 if (fr->fr_parent->fr_layout == FR_COL)
1161 {
1162 if (fr->fr_next != NULL)
1163 break;
1164 }
1165 else
1166 {
1167 if (fr->fr_next != NULL)
1168 return TRUE;
1169 }
1170 fr = fr->fr_parent;
1171 }
1172 return FALSE;
1173}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176/*
1177 * Get the value to show for the language mappings, active 'keymap'.
1178 */
1179 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001180get_keymap_str(
1181 win_T *wp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001182 char_u *fmt, // format string containing one %s item
1183 char_u *buf, // buffer for the result
1184 int len) // length of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
1186 char_u *p;
1187
1188 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
1189 return FALSE;
1190
1191 {
1192#ifdef FEAT_EVAL
1193 buf_T *old_curbuf = curbuf;
1194 win_T *old_curwin = curwin;
1195 char_u *s;
1196
1197 curbuf = wp->w_buffer;
1198 curwin = wp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001199 STRCPY(buf, "b:keymap_name"); // must be writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001201 s = p = eval_to_string(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 --emsg_skip;
1203 curbuf = old_curbuf;
1204 curwin = old_curwin;
1205 if (p == NULL || *p == NUL)
1206#endif
1207 {
1208#ifdef FEAT_KEYMAP
1209 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
1210 p = wp->w_buffer->b_p_keymap;
1211 else
1212#endif
1213 p = (char_u *)"lang";
1214 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02001215 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 buf[0] = NUL;
1217#ifdef FEAT_EVAL
1218 vim_free(s);
1219#endif
1220 }
1221 return buf[0] != NUL;
1222}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
1224#if defined(FEAT_STL_OPT) || defined(PROTO)
1225/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001226 * Redraw the status line or ruler of window "wp".
1227 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001229 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001230win_redr_custom(
1231 win_T *wp,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001232 int draw_ruler) // TRUE or FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233{
Bram Moolenaar1d633412013-12-11 15:52:01 +01001234 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 int attr;
1236 int curattr;
1237 int row;
1238 int col = 0;
1239 int maxwidth;
1240 int width;
1241 int n;
1242 int len;
1243 int fillchar;
1244 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00001245 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 char_u *p;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001247 stl_hlrec_T *hltab;
1248 stl_hlrec_T *tabtab;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001249 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01001250 win_T *ewp;
1251 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001253 // There is a tiny chance that this gets called recursively: When
1254 // redrawing a status line triggers redrawing the ruler or tabline.
1255 // Avoid trouble by not allowing recursion.
Bram Moolenaar1d633412013-12-11 15:52:01 +01001256 if (entered)
1257 return;
1258 entered = TRUE;
1259
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001260 // setup environment for the task at hand
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001261 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001263 // Use 'tabline'. Always at the first line of the screen.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001264 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001265 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00001266 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01001267 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001268 maxwidth = Columns;
1269# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001270 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001273 else
1274 {
Bram Moolenaar49c51b82021-04-01 16:16:18 +02001275 row = statusline_row(wp);
Bram Moolenaar3633cf52017-07-31 22:29:35 +02001276 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02001277 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001278
1279 if (draw_ruler)
1280 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001281 stl = p_ruf;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001282 // advance past any leading group spec - implicit in ru_col
Bram Moolenaar362f3562009-11-03 16:20:34 +00001283 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001284 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00001285 if (*++stl == '-')
1286 stl++;
1287 if (atoi((char *)stl))
1288 while (VIM_ISDIGIT(*stl))
1289 stl++;
1290 if (*stl++ != '(')
1291 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001292 }
Bram Moolenaar02631462017-09-22 15:20:32 +02001293 col = ru_col - (Columns - wp->w_width);
1294 if (col < (wp->w_width + 1) / 2)
1295 col = (wp->w_width + 1) / 2;
1296 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001297 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001298 {
1299 row = Rows - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001300 --maxwidth; // writing in last column may cause scrolling
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001301 fillchar = ' ';
1302 attr = 0;
1303 }
1304
1305# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001306 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001307# endif
1308 }
1309 else
1310 {
1311 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00001312 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001313 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00001314 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001315# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001316 use_sandbox = was_set_insecurely((char_u *)"statusline",
1317 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001318# endif
1319 }
1320
Bram Moolenaar53f81742017-09-22 14:35:51 +02001321 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001322 }
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01001325 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001327 // Temporarily reset 'cursorbind', we don't want a side effect from moving
1328 // the cursor away and back.
Bram Moolenaar61452852011-02-01 18:01:11 +01001329 ewp = wp == NULL ? curwin : wp;
1330 p_crb_save = ewp->w_p_crb;
1331 ewp->w_p_crb = FALSE;
1332
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001333 // Make a copy, because the statusline may include a function call that
1334 // might change the option value and free the memory.
Bram Moolenaar362f3562009-11-03 16:20:34 +00001335 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001336 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00001337 stl, use_sandbox,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01001338 fillchar, maxwidth, &hltab, &tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00001339 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01001340 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001342 // Make all characters printable.
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001343 p = transstr(buf);
1344 if (p != NULL)
1345 {
1346 vim_strncpy(buf, p, sizeof(buf) - 1);
1347 vim_free(p);
1348 }
1349
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001350 // fill up with "fillchar"
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01001351 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001352 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 ++width;
1356 }
1357 buf[len] = NUL;
1358
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001359 /*
1360 * Draw each snippet with the specified highlighting.
1361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 curattr = attr;
1363 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001364 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001366 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 screen_puts_len(p, len, row, col, curattr);
1368 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001369 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001371 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001373 else if (hltab[n].userhl < 0)
1374 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001375#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02001376 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
1377 && wp->w_status_height != 0)
1378 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02001379 else if (wp != NULL && bt_terminal(wp->w_buffer)
1380 && wp->w_status_height != 0)
1381 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02001382#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00001383 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001384 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001386 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001389
1390 if (wp == NULL)
1391 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001392 // Fill the TabPageIdxs[] array for clicking in the tab pagesline.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001393 col = 0;
1394 len = 0;
1395 p = buf;
1396 fillchar = 0;
1397 for (n = 0; tabtab[n].start != NULL; n++)
1398 {
1399 len += vim_strnsize(p, (int)(tabtab[n].start - p));
1400 while (col < len)
1401 TabPageIdxs[col++] = fillchar;
1402 p = tabtab[n].start;
1403 fillchar = tabtab[n].userhl;
1404 }
1405 while (col < Columns)
1406 TabPageIdxs[col++] = fillchar;
1407 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01001408
1409theend:
1410 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411}
1412
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001413#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
1415/*
1416 * Output a single character directly to the screen and update ScreenLines.
1417 */
1418 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001419screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 char_u buf[MB_MAXBYTES + 1];
1422
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001423 if (has_mbyte)
1424 buf[(*mb_char2bytes)(c, buf)] = NUL;
1425 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001426 {
1427 buf[0] = c;
1428 buf[1] = NUL;
1429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 screen_puts(buf, row, col, attr);
1431}
1432
1433/*
1434 * Get a single character directly from ScreenLines into "bytes[]".
1435 * Also return its attribute in *attrp;
1436 */
1437 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001438screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
1440 unsigned off;
1441
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001442 // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
1444 {
1445 off = LineOffset[row] + col;
1446 *attrp = ScreenAttrs[off];
1447 bytes[0] = ScreenLines[off];
1448 bytes[1] = NUL;
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 if (enc_utf8 && ScreenLinesUC[off] != 0)
1451 bytes[utfc_char2bytes(off, bytes)] = NUL;
1452 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
1453 {
1454 bytes[0] = ScreenLines[off];
1455 bytes[1] = ScreenLines2[off];
1456 bytes[2] = NUL;
1457 }
1458 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
1459 {
1460 bytes[1] = ScreenLines[off + 1];
1461 bytes[2] = NUL;
1462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
1464}
1465
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001466/*
1467 * Return TRUE if composing characters for screen posn "off" differs from
1468 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001469 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 */
1471 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001472screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001473{
1474 int i;
1475
1476 for (i = 0; i < Screen_mco; ++i)
1477 {
1478 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
1479 return TRUE;
1480 if (u8cc[i] == 0)
1481 break;
1482 }
1483 return FALSE;
1484}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486/*
1487 * Put string '*text' on the screen at position 'row' and 'col', with
1488 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
1489 * Note: only outputs within one row, message is truncated at screen boundary!
1490 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
1491 */
1492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001493screen_puts(
1494 char_u *text,
1495 int row,
1496 int col,
1497 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 screen_puts_len(text, -1, row, col, attr);
1500}
1501
1502/*
1503 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
1504 * a NUL.
1505 */
1506 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001507screen_puts_len(
1508 char_u *text,
1509 int textlen,
1510 int row,
1511 int col,
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001512 int attr_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001514 int attr = attr_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 unsigned off;
1516 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001517 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00001519 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 int mbyte_blen = 1;
1521 int mbyte_cells = 1;
1522 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001523 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001525#ifdef FEAT_ARABIC
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001526 int prev_c = 0; // previous Arabic character
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001527 int pc, nc, nc1;
1528 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001530 int force_redraw_this;
1531 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001532 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02001534 // Safety check. The check for negative row and column is to fix issue
1535 // #4102. TODO: find out why row/col could be negative.
1536 if (ScreenLines == NULL
1537 || row >= screen_Rows || row < 0
1538 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001540 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001542 // When drawing over the right half of a double-wide char clear out the
1543 // left half. Only needed in a terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00001544 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01001545#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00001546 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01001547#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00001548 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001549 {
1550 ScreenLines[off - 1] = ' ';
1551 ScreenAttrs[off - 1] = 0;
1552 if (enc_utf8)
1553 {
1554 ScreenLinesUC[off - 1] = 0;
1555 ScreenLinesC[0][off - 1] = 0;
1556 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001557 // redraw the previous cell, make it empty
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001558 screen_char(off - 1, row, col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001559 // force the cell at "col" to be redrawn
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001560 force_redraw_next = TRUE;
1561 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00001562
Bram Moolenaar367329b2007-08-30 11:53:22 +00001563 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00001564 while (col < screen_Columns
1565 && (len < 0 || (int)(ptr - text) < len)
1566 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
1568 c = *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001569 // check if this is the first byte of a multibyte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (has_mbyte)
1571 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001572 mbyte_blen = enc_utf8 && len > 0
1573 ? utfc_ptr2len_len(ptr, (int)((text + len) - ptr))
1574 : (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1576 mbyte_cells = 1;
1577 else if (enc_dbcs != 0)
1578 mbyte_cells = mbyte_blen;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001579 else // enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001581 u8c = len >= 0
1582 ? utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr))
1583 : utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01001585#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
1587 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001588 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
1590 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001591 // Past end of string to be displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 nc = NUL;
1593 nc1 = NUL;
1594 }
1595 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001596 {
zeertzjq4dc513a2022-07-25 19:42:02 +01001597 nc = len >= 0
1598 ? utfc_ptr2char_len(ptr + mbyte_blen, pcc,
1599 (int)((text + len) - ptr - mbyte_blen))
1600 : utfc_ptr2char(ptr + mbyte_blen, pcc);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001601 nc1 = pcc[0];
1602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 pc = prev_c;
1604 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001605 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 else
1608 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01001609#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001610 if (col + mbyte_cells > screen_Columns)
1611 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001612 // Only 1 cell left, but character requires 2 cells:
1613 // display a '>' in the last column to avoid wrapping.
Bram Moolenaare4ebd292010-01-19 17:40:46 +01001614 c = '>';
1615 mbyte_cells = 1;
1616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 }
1618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001620 force_redraw_this = force_redraw_next;
1621 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001622
1623 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 || (mbyte_cells == 2
1625 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
1626 || (enc_dbcs == DBCS_JPNU
1627 && c == 0x8e
1628 && ScreenLines2[off] != ptr[1])
1629 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01001630 && (ScreenLinesUC[off] !=
1631 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
1632 || (ScreenLinesUC[off] != 0
1633 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001635 || exmode_active;
1636
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001637 if ((need_redraw || force_redraw_this)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001638#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001639 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02001640#endif
1641 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 {
1643#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001644 // The bold trick makes a single row of pixels appear in the next
1645 // character. When a bold character is removed, the next
1646 // character should be redrawn too. This happens for our own GUI
1647 // and for some xterms.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001648 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649# ifdef FEAT_GUI
1650 gui.in_use
1651# endif
1652# if defined(FEAT_GUI) && defined(UNIX)
1653 ||
1654# endif
1655# ifdef UNIX
1656 term_is_xterm
1657# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001658 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001660 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001662 if (n > HL_ALL)
1663 n = syn_attr2attr(n);
1664 if (n & HL_BOLD)
1665 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
1667#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001668 // When at the end of the text and overwriting a two-cell
1669 // character with a one-cell character, need to clear the next
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001670 // cell. Also when overwriting the left half of a two-cell char
1671 // with the right half of a two-cell char. Do this only once
1672 // (mb_off2cells() may return 2 on the right half).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (clear_next_cell)
1674 clear_next_cell = FALSE;
1675 else if (has_mbyte
1676 && (len < 0 ? ptr[mbyte_blen] == NUL
1677 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00001678 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001680 && (*mb_off2cells)(off, max_off) == 1
1681 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 clear_next_cell = TRUE;
1683
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001684 // Make sure we never leave a second byte of a double-byte behind,
1685 // it confuses mb_off2cells().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00001687 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00001689 && (*mb_off2cells)(off, max_off) == 1
1690 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 ScreenLines[off] = c;
1693 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001694 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (enc_utf8)
1696 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001697 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 ScreenLinesUC[off] = 0;
1699 else
1700 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001701 int i;
1702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001704 for (i = 0; i < Screen_mco; ++i)
1705 {
1706 ScreenLinesC[i][off] = u8cc[i];
1707 if (u8cc[i] == 0)
1708 break;
1709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711 if (mbyte_cells == 2)
1712 {
1713 ScreenLines[off + 1] = 0;
1714 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001715 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
1717 screen_char(off, row, col);
1718 }
1719 else if (mbyte_cells == 2)
1720 {
1721 ScreenLines[off + 1] = ptr[1];
1722 ScreenAttrs[off + 1] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01001723 ScreenCols[off + 1] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 screen_char_2(off, row, col);
1725 }
1726 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
1727 {
1728 ScreenLines2[off] = ptr[1];
1729 screen_char(off, row, col);
1730 }
1731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 screen_char(off, row, col);
1733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (has_mbyte)
1735 {
1736 off += mbyte_cells;
1737 col += mbyte_cells;
1738 ptr += mbyte_blen;
1739 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001740 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001741 // This only happens at the end, display one space next.
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001742 // Keep the attribute from before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001744 len = -1;
Bram Moolenaar35d8c202022-03-03 11:46:00 +00001745 attr = ScreenAttrs[off];
Bram Moolenaare4c21e62014-05-22 16:05:19 +02001746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
1748 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 {
1750 ++off;
1751 ++col;
1752 ++ptr;
1753 }
1754 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001755
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001756 // If we detected the next character needs to be redrawn, but the text
1757 // doesn't extend up to there, update the character here.
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001758 if (force_redraw_next && col < screen_Columns)
1759 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001760 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
1761 screen_char_2(off, row, col);
1762 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00001763 screen_char(off, row, col);
1764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765}
1766
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001767#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001769 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001771 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001772start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 if (p_hls && !no_hlsearch)
1775 {
Bram Moolenaar0b6849e2020-05-02 18:33:25 +02001776 end_search_hl(); // just in case it wasn't called before
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001777 last_pat_prog(&screen_search_hl.rm);
1778 screen_search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 }
1780}
1781
1782/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001783 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001785 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001786end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001788 if (screen_search_hl.rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001790 vim_regfree(screen_search_hl.rm.regprog);
1791 screen_search_hl.rm.regprog = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
1793}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02001794#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001797screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 attrentry_T *aep = NULL;
1800
1801 screen_attr = attr;
1802 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01001803#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 && termcap_active
1805#endif
1806 )
1807 {
1808#ifdef FEAT_GUI
1809 if (gui.in_use)
1810 {
1811 char buf[20];
1812
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001813 // The GUI handles this internally.
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001814 sprintf(buf, "\033|%dh", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 OUT_STR(buf);
1816 }
1817 else
1818#endif
1819 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001820 if (attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001822 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 aep = syn_cterm_attr2entry(attr);
1824 else
1825 aep = syn_term_attr2entry(attr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001826 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 attr = 0;
1828 else
1829 attr = aep->ae_attr;
1830 }
Bram Moolenaara050b942019-12-02 21:35:31 +01001831#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1832 if (use_vtp())
1833 {
1834 guicolor_T defguifg, defguibg;
1835 int defctermfg, defctermbg;
1836
1837 // If FG and BG are unset, the color is undefined when
1838 // BOLD+INVERSE. Use Normal as the default value.
1839 get_default_console_color(&defctermfg, &defctermbg, &defguifg,
1840 &defguibg);
1841
1842 if (p_tgc)
1843 {
1844 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.fg_rgb))
1845 term_fg_rgb_color(defguifg);
1846 if (aep == NULL || COLOR_INVALID(aep->ae_u.cterm.bg_rgb))
1847 term_bg_rgb_color(defguibg);
1848 }
1849 else if (t_colors >= 256)
1850 {
1851 if (aep == NULL || aep->ae_u.cterm.fg_color == 0)
1852 term_fg_color(defctermfg);
1853 if (aep == NULL || aep->ae_u.cterm.bg_color == 0)
1854 term_bg_color(defctermbg);
1855 }
1856 }
1857#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001858 if ((attr & HL_BOLD) && *T_MD != NUL) // bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001860 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001861#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001862 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1863 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
1864 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001865#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001866 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001867 // If the Normal FG color has BOLD attribute and the new HL
1868 // has a FG color defined, clear BOLD.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001869 out_str(T_ME);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001870 if ((attr & HL_STANDOUT) && *T_SO != NUL) // standout
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 out_str(T_SO);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001872 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) // undercurl
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01001873 out_str(T_UCS);
Bram Moolenaar84f54632022-06-29 18:39:11 +01001874 if ((attr & HL_UNDERDOUBLE) && *T_USS != NUL) // double underline
1875 out_str(T_USS);
1876 if ((attr & HL_UNDERDOTTED) && *T_DS != NUL) // dotted underline
1877 out_str(T_DS);
1878 if ((attr & HL_UNDERDASHED) && *T_CDS != NUL) // dashed underline
1879 out_str(T_CDS);
1880 if (((attr & HL_UNDERLINE) // underline or undercurl, etc.
1881 || ((attr & HL_UNDERCURL) && *T_UCS == NUL)
1882 || ((attr & HL_UNDERDOUBLE) && *T_USS == NUL)
1883 || ((attr & HL_UNDERDOTTED) && *T_DS == NUL)
1884 || ((attr & HL_UNDERDASHED) && *T_CDS == NUL))
Bram Moolenaar45a00002017-12-22 21:12:34 +01001885 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 out_str(T_US);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001887 if ((attr & HL_ITALIC) && *T_CZH != NUL) // italic
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 out_str(T_CZH);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001889 if ((attr & HL_INVERSE) && *T_MR != NUL) // inverse (reverse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 out_str(T_MR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001891 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) // strike
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001892 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
1894 /*
1895 * Output the color or start string after bold etc., in case the
1896 * bold etc. override the color setting.
1897 */
1898 if (aep != NULL)
1899 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001900#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001901 // When 'termguicolors' is set but fg or bg is unset,
1902 // fall back to the cterm colors. This helps for SpellBad,
1903 // where the GUI uses a red undercurl.
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001904 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001906 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001907 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001908 }
1909 else
1910#endif
1911 if (t_colors > 1)
1912 {
1913 if (aep->ae_u.cterm.fg_color)
1914 term_fg_color(aep->ae_u.cterm.fg_color - 1);
1915 }
1916#ifdef FEAT_TERMGUICOLORS
1917 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
1918 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001919 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001920 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
1922 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001923#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001924 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001926 if (aep->ae_u.cterm.bg_color)
1927 term_bg_color(aep->ae_u.cterm.bg_color - 1);
1928 }
Bram Moolenaare023e882020-05-31 16:42:30 +02001929#ifdef FEAT_TERMGUICOLORS
1930 if (p_tgc && aep->ae_u.cterm.ul_rgb != CTERMCOLOR)
1931 {
1932 if (aep->ae_u.cterm.ul_rgb != INVALCOLOR)
1933 term_ul_rgb_color(aep->ae_u.cterm.ul_rgb);
1934 }
1935 else
1936#endif
1937 if (t_colors > 1)
1938 {
1939 if (aep->ae_u.cterm.ul_color)
1940 term_ul_color(aep->ae_u.cterm.ul_color - 1);
1941 }
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001942
Bram Moolenaarf708ac52018-03-12 21:48:32 +01001943 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001944 {
1945 if (aep->ae_u.term.start != NULL)
1946 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 }
1949 }
1950 }
1951}
1952
1953 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001954screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001956 int do_ME = FALSE; // output T_ME code
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001957#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar09307e32020-05-29 21:42:55 +02001958 int do_ME_fg = FALSE, do_ME_bg = FALSE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
1961 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001962#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 && termcap_active
1964#endif
1965 )
1966 {
1967#ifdef FEAT_GUI
1968 if (gui.in_use)
1969 {
1970 char buf[20];
1971
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001972 // use internal GUI code
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001973 sprintf(buf, "\033|%dH", screen_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 OUT_STR(buf);
1975 }
1976 else
1977#endif
1978 {
Bram Moolenaar84f54632022-06-29 18:39:11 +01001979 int is_under;
1980
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001981 if (screen_attr > HL_ALL) // special HL attr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 attrentry_T *aep;
1984
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001985 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 /*
1988 * Assume that t_me restores the original colors!
1989 */
1990 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001991 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001992#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001993 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
1994 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02001995# ifdef FEAT_VTP
1996 ? !(do_ME_fg = TRUE) : (do_ME_fg = FALSE)
1997# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01001998 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001999#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002000 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002001#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002002 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
2003 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002004# ifdef FEAT_VTP
2005 ? !(do_ME_bg = TRUE) : (do_ME_bg = FALSE)
2006# endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002007 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002008#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01002009 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 do_ME = TRUE;
Bram Moolenaar4e5534f2020-04-30 20:59:57 +02002011#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2012 if (use_vtp())
2013 {
2014 if (do_ME_fg && do_ME_bg)
2015 do_ME = TRUE;
2016
2017 // FG and BG cannot be separated in T_ME, which is not
2018 // efficient.
2019 if (!do_ME && do_ME_fg)
2020 out_str((char_u *)"\033|39m"); // restore FG
2021 if (!do_ME && do_ME_bg)
2022 out_str((char_u *)"\033|49m"); // restore BG
2023 }
2024 else
2025 {
2026 // Process FG and BG at once.
2027 if (!do_ME)
2028 do_ME = do_ME_fg | do_ME_bg;
2029 }
2030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032 else
2033 {
2034 aep = syn_term_attr2entry(screen_attr);
2035 if (aep != NULL && aep->ae_u.term.stop != NULL)
2036 {
2037 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
2038 do_ME = TRUE;
2039 else
2040 out_str(aep->ae_u.term.stop);
2041 }
2042 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002043 if (aep == NULL) // did ":syntax clear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 screen_attr = 0;
2045 else
2046 screen_attr = aep->ae_attr;
2047 }
2048
2049 /*
2050 * Often all ending-codes are equal to T_ME. Avoid outputting the
2051 * same sequence several times.
2052 */
2053 if (screen_attr & HL_STANDOUT)
2054 {
2055 if (STRCMP(T_SE, T_ME) == 0)
2056 do_ME = TRUE;
2057 else
2058 out_str(T_SE);
2059 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002060 is_under = (screen_attr & (HL_UNDERCURL
2061 | HL_UNDERDOUBLE | HL_UNDERDOTTED | HL_UNDERDASHED));
2062 if (is_under && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01002063 {
2064 if (STRCMP(T_UCE, T_ME) == 0)
2065 do_ME = TRUE;
2066 else
2067 out_str(T_UCE);
2068 }
Bram Moolenaar84f54632022-06-29 18:39:11 +01002069 if ((screen_attr & HL_UNDERLINE) || (is_under && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
2071 if (STRCMP(T_UE, T_ME) == 0)
2072 do_ME = TRUE;
2073 else
2074 out_str(T_UE);
2075 }
2076 if (screen_attr & HL_ITALIC)
2077 {
2078 if (STRCMP(T_CZR, T_ME) == 0)
2079 do_ME = TRUE;
2080 else
2081 out_str(T_CZR);
2082 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002083 if (screen_attr & HL_STRIKETHROUGH)
2084 {
2085 if (STRCMP(T_STE, T_ME) == 0)
2086 do_ME = TRUE;
2087 else
2088 out_str(T_STE);
2089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
2091 out_str(T_ME);
2092
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002093#ifdef FEAT_TERMGUICOLORS
2094 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002096 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002097 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002098 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002099 term_bg_rgb_color(cterm_normal_bg_gui_color);
Bram Moolenaare023e882020-05-31 16:42:30 +02002100 if (cterm_normal_ul_gui_color != INVALCOLOR)
2101 term_ul_rgb_color(cterm_normal_ul_gui_color);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002102 }
2103 else
2104#endif
2105 {
2106 if (t_colors > 1)
2107 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002108 // set Normal cterm colors
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002109 if (cterm_normal_fg_color != 0)
2110 term_fg_color(cterm_normal_fg_color - 1);
2111 if (cterm_normal_bg_color != 0)
2112 term_bg_color(cterm_normal_bg_color - 1);
Bram Moolenaare023e882020-05-31 16:42:30 +02002113 if (cterm_normal_ul_color != 0)
2114 term_ul_color(cterm_normal_ul_color - 1);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002115 if (cterm_normal_fg_bold)
2116 out_str(T_MD);
2117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
2119 }
2120 }
2121 screen_attr = 0;
2122}
2123
2124/*
2125 * Reset the colors for a cterm. Used when leaving Vim.
2126 * The machine specific code may override this again.
2127 */
2128 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002129reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002131 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002133 // set Normal cterm colors
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002134#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002135 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
2136 || cterm_normal_bg_gui_color != INVALCOLOR)
2137 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002138#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
2142 out_str(T_OP);
2143 screen_attr = -1;
2144 }
2145 if (cterm_normal_fg_bold)
2146 {
2147 out_str(T_ME);
2148 screen_attr = -1;
2149 }
2150 }
2151}
2152
2153/*
2154 * Put character ScreenLines["off"] on the screen at position "row" and "col",
2155 * using the attributes from ScreenAttrs["off"].
2156 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002157 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002158screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159{
2160 int attr;
2161
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002162 // Check for illegal values, just in case (could happen just after
2163 // resizing).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (row >= screen_Rows || col >= screen_Columns)
2165 return;
2166
Bram Moolenaar33796b32019-06-08 16:01:13 +02002167 // Skip if under the popup menu.
2168 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
Bakudankun65555002021-11-17 20:40:16 +00002169 if (pum_under_menu(row, col, TRUE)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002170#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002171 && screen_zindex <= POPUPMENU_ZINDEX
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002172#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02002173 )
Bram Moolenaarae654382019-01-17 21:09:05 +01002174 return;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002175#ifdef FEAT_PROP_POPUP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002176 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02002177 return;
2178#endif
2179
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002180 // Outputting a character in the last cell on the screen may scroll the
2181 // screen up. Only do it when the "xn" termcap property is set, otherwise
2182 // mark the character invalid (update it when scrolled up).
Bram Moolenaar494838a2015-02-10 19:20:37 +01002183 if (*T_XN == NUL
2184 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002186 // account for first command-line character in rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 && !cmdmsg_rl
2188#endif
2189 )
2190 {
2191 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002192 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return;
2194 }
2195
2196 /*
2197 * Stop highlighting first, so it's easier to move the cursor.
2198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (screen_char_attr != 0)
2200 attr = screen_char_attr;
2201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 attr = ScreenAttrs[off];
2203 if (screen_attr != attr)
2204 screen_stop_highlight();
2205
2206 windgoto(row, col);
2207
2208 if (screen_attr != attr)
2209 screen_start_highlight(attr);
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (enc_utf8 && ScreenLinesUC[off] != 0)
2212 {
2213 char_u buf[MB_MAXBYTES + 1];
2214
Bram Moolenaarcb070082016-04-02 22:14:51 +02002215 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002216 {
2217 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01002218#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002219 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002220#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002221 )
2222 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002223 // Clear the two screen cells. If the character is actually
2224 // single width it won't change the second cell.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002225 out_str((char_u *)" ");
2226 term_windgoto(row, col);
2227 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002228 // not sure where the cursor is after drawing the ambiguous width
2229 // character
Bram Moolenaarcb070082016-04-02 22:14:51 +02002230 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002231 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02002232 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002234
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002235 // Convert the UTF-8 character to bytes and write it.
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01002236 buf[utfc_char2bytes(off, buf)] = NUL;
2237 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 }
2239 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 out_char(ScreenLines[off]);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002243 // double-byte character in single-width cell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2245 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247
2248 screen_cur_col++;
2249}
2250
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251/*
2252 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
2253 * on the screen at position 'row' and 'col'.
2254 * The attributes of the first byte is used for all. This is required to
2255 * output the two bytes of a double-byte character with nothing in between.
2256 */
2257 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002258screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002260 // Check for illegal values (could be wrong when screen was resized).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
2262 return;
2263
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002264 // Outputting the last character on the screen may scrollup the screen.
2265 // Don't to it! Mark the character invalid (update it when scrolled up)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
2267 {
2268 ScreenAttrs[off] = (sattr_T)-1;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002269 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 return;
2271 }
2272
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002273 // Output the first byte normally (positions the cursor), then write the
2274 // second byte directly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 screen_char(off, row, col);
2276 out_char(ScreenLines[off + 1]);
2277 ++screen_cur_col;
2278}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280/*
2281 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
2282 * This uses the contents of ScreenLines[] and doesn't change it.
2283 */
2284 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002285screen_draw_rectangle(
2286 int row,
2287 int col,
2288 int height,
2289 int width,
2290 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 int r, c;
2293 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00002294 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002296 // Can't use ScreenLines unless initialized
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002297 if (ScreenLines == NULL)
2298 return;
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (invert)
2301 screen_char_attr = HL_INVERSE;
2302 for (r = row; r < row + height; ++r)
2303 {
2304 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00002305 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 for (c = col; c < col + width; ++c)
2307 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00002308 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 screen_char_2(off + c, r, c);
2311 ++c;
2312 }
2313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00002316 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319 }
2320 }
2321 screen_char_attr = 0;
2322}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324/*
2325 * Redraw the characters for a vertically split window.
2326 */
2327 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002328redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 int col;
2331 int width;
2332
2333# ifdef FEAT_CLIPBOARD
2334 clip_may_clear_selection(row, end - 1);
2335# endif
2336
2337 if (wp == NULL)
2338 {
2339 col = 0;
2340 width = Columns;
2341 }
2342 else
2343 {
2344 col = wp->w_wincol;
2345 width = wp->w_width;
2346 }
2347 screen_draw_rectangle(row, col, end - row, width, FALSE);
2348}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002350 void
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002351space_to_screenline(int off, int attr)
2352{
2353 ScreenLines[off] = ' ';
2354 ScreenAttrs[off] = attr;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002355 ScreenCols[off] = -1;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002356 if (enc_utf8)
2357 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002358}
2359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360/*
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002361 * Fill the screen from "start_row" to "end_row" (exclusive), from "start_col"
2362 * to "end_col" (exclusive) with character "c1" in first column followed by
2363 * "c2" in the other columns. Use attributes "attr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 */
2365 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002366screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002367 int start_row,
2368 int end_row,
2369 int start_col,
2370 int end_col,
2371 int c1,
2372 int c2,
2373 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002375 int row;
2376 int col;
2377 int off;
2378 int end_off;
2379 int did_delete;
2380 int c;
2381 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002383 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#endif
2385
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002386 if (end_row > screen_Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 end_row = screen_Rows;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002388 if (end_col > screen_Columns) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 end_col = screen_Columns;
2390 if (ScreenLines == NULL
2391 || start_row >= end_row
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002392 || start_col >= end_col) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 return;
2394
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002395 // it's a "normal" terminal when not in a GUI or cterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 norm_term = (
2397#ifdef FEAT_GUI
2398 !gui.in_use &&
2399#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002400 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 for (row = start_row; row < end_row; ++row)
2402 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00002403 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01002404#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00002405 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01002406#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00002407 )
2408 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002409 // When drawing over the right half of a double-wide char clear
2410 // out the left half. When drawing over the left half of a
2411 // double wide-char clear out the right half. Only needed in a
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002412 // terminal.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00002413 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002414 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00002415 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00002416 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00002417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 /*
2419 * Try to use delete-line termcap code, when no attributes or in a
2420 * "normal" terminal, where a bold/italic space is just a
2421 * space.
2422 */
2423 did_delete = FALSE;
2424 if (c2 == ' '
2425 && end_col == Columns
2426 && can_clear(T_CE)
2427 && (attr == 0
2428 || (norm_term
2429 && attr <= HL_ALL
2430 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
2431 {
2432 /*
2433 * check if we really need to clear something
2434 */
2435 col = start_col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002436 if (c1 != ' ') // don't clear first char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 ++col;
2438
2439 off = LineOffset[row] + col;
2440 end_off = LineOffset[row] + end_col;
2441
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002442 // skip blanks (used often, keep it fast!)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if (enc_utf8)
2444 while (off < end_off && ScreenLines[off] == ' '
2445 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
2446 ++off;
2447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 while (off < end_off && ScreenLines[off] == ' '
2449 && ScreenAttrs[off] == 0)
2450 ++off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002451 if (off < end_off) // something to be cleared
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
2453 col = off - LineOffset[row];
2454 screen_stop_highlight();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002455 term_windgoto(row, col);// clear rest of this screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002457 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 col = end_col - col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002459 while (col--) // clear chars in ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002461 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 ++off;
2463 }
2464 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002465 did_delete = TRUE; // the chars are cleared now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
2467
2468 off = LineOffset[row] + start_col;
2469 c = c1;
2470 for (col = start_col; col < end_col; ++col)
2471 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02002472 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002473 || (enc_utf8 && (int)ScreenLinesUC[off]
2474 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 || ScreenAttrs[off] != attr
2476#if defined(FEAT_GUI) || defined(UNIX)
2477 || force_next
2478#endif
2479 )
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002480#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002481 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002482 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002483#endif
2484 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002487 // The bold trick may make a single row of pixels appear in
2488 // the next character. When a bold character is removed, the
2489 // next character should be redrawn too. This happens for our
2490 // own GUI and for some xterms.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (
2492# ifdef FEAT_GUI
2493 gui.in_use
2494# endif
2495# if defined(FEAT_GUI) && defined(UNIX)
2496 ||
2497# endif
2498# ifdef UNIX
2499 term_is_xterm
2500# endif
2501 )
2502 {
2503 if (ScreenLines[off] != ' '
2504 && (ScreenAttrs[off] > HL_ALL
2505 || ScreenAttrs[off] & HL_BOLD))
2506 force_next = TRUE;
2507 else
2508 force_next = FALSE;
2509 }
2510#endif
2511 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (enc_utf8)
2513 {
2514 if (c >= 0x80)
2515 {
2516 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002517 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519 else
2520 ScreenLinesUC[off] = 0;
2521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 ScreenAttrs[off] = attr;
2523 if (!did_delete || c != ' ')
2524 screen_char(off, row, col);
2525 }
Bram Moolenaarb9081882022-07-09 04:56:24 +01002526 ScreenCols[off] = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 ++off;
2528 if (col == start_col)
2529 {
2530 if (did_delete)
2531 break;
2532 c = c2;
2533 }
2534 }
2535 if (end_col == Columns)
2536 LineWraps[row] = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002537 if (row == Rows - 1) // overwritten the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02002540 if (start_col == 0 && end_col == Columns
2541 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002542 clear_cmdline = FALSE; // command line has been cleared
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002543 if (start_col == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002544 mode_displayed = FALSE; // mode cleared or overwritten
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546 }
2547}
2548
2549/*
2550 * Check if there should be a delay. Used before clearing or redrawing the
2551 * screen or the command line.
2552 */
2553 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002554check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
2557 && !did_wait_return
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002558 && emsg_silent == 0
2559 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002562 ui_delay(1006L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 emsg_on_display = FALSE;
2564 if (check_msg_scroll)
2565 msg_scroll = FALSE;
2566 }
2567}
2568
2569/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002570 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
2571 */
2572 static void
2573clear_TabPageIdxs(void)
2574{
2575 int scol;
2576
2577 for (scol = 0; scol < Columns; ++scol)
2578 TabPageIdxs[scol] = 0;
2579}
2580
2581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002583 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 * Returns TRUE if there is a valid screen to write to.
2585 * Returns FALSE when starting up and screen not initialized yet.
2586 */
2587 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002588screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002590 screenalloc(doclear); // allocate screen buffers if size changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return (ScreenLines != NULL);
2592}
2593
2594/*
2595 * Resize the shell to Rows and Columns.
2596 * Allocate ScreenLines[] and associated items.
2597 *
2598 * There may be some time between setting Rows and Columns and (re)allocating
2599 * ScreenLines[]. This happens when starting up and when (manually) changing
2600 * the shell size. Always use screen_Rows and screen_Columns to access items
2601 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
2602 * final size of the shell is needed.
2603 */
2604 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002605screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 int new_row, old_row;
2608#ifdef FEAT_GUI
2609 int old_Rows;
2610#endif
2611 win_T *wp;
2612 int outofmem = FALSE;
2613 int len;
2614 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002616 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002618 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 sattr_T *new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002620 colnr_T *new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 unsigned *new_LineOffset;
2622 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002623 short *new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002624#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002625 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002626 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002627 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002628#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00002629 tabpage_T *tp;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002630 static int entered = FALSE; // avoid recursiveness
2631 static int done_outofmem_msg = FALSE; // did outofmem message
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002632 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002634retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 /*
2636 * Allocation of the screen buffers is done only when the size changes and
2637 * when Rows and Columns have been set and we have started doing full
2638 * screen stuff.
2639 */
2640 if ((ScreenLines != NULL
2641 && Rows == screen_Rows
2642 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 && enc_utf8 == (ScreenLinesUC != NULL)
2644 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01002645 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 || Rows == 0
2647 || Columns == 0
2648 || (!full_screen && ScreenLines == NULL))
2649 return;
2650
2651 /*
2652 * It's possible that we produce an out-of-memory message below, which
2653 * will cause this function to be called again. To break the loop, just
2654 * return here.
2655 */
2656 if (entered)
2657 return;
2658 entered = TRUE;
2659
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002660 /*
2661 * Note that the window sizes are updated before reallocating the arrays,
2662 * thus we must not redraw here!
2663 */
2664 ++RedrawingDisabled;
2665
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002666 win_new_shellsize(); // fit the windows in the new sized shell
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002668#ifdef FEAT_GUI_HAIKU
2669 vim_lock_screen(); // be safe, put it here
2670#endif
2671
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002672 comp_col(); // recompute columns for shown command and ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674 /*
2675 * We're changing the size of the screen.
2676 * - Allocate new arrays for ScreenLines and ScreenAttrs.
2677 * - Move lines from the old arrays into the new arrays, clear extra
2678 * lines (unless the screen is going to be cleared).
2679 * - Free the old arrays.
2680 *
2681 * If anything fails, make ScreenLines NULL, so we don't do anything!
2682 * Continuing with the old ScreenLines may result in a crash, because the
2683 * size is wrong.
2684 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002685 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002687 if (aucmd_win != NULL)
2688 win_free_lsize(aucmd_win);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002689#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002690 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002691 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002692 win_free_lsize(wp);
2693 // tab-local popup windows
2694 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002695 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002696 win_free_lsize(wp);
2697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002699 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01002700 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (enc_utf8)
2702 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002703 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002704 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002705 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
2706 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002709 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
2710 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
Bram Moolenaar18ee0fe2022-09-19 11:44:11 +01002711 // Clear ScreenCols to avoid a warning for unitialized memory in
2712 // jump_to_mouse().
2713 new_ScreenCols = LALLOC_CLEAR_MULT(colnr_T, (Rows + 1) * Columns);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002714 new_LineOffset = LALLOC_MULT(unsigned, Rows);
2715 new_LineWraps = LALLOC_MULT(char_u, Rows);
2716 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002717#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002718 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002719 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002720 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002723 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
2725 if (win_alloc_lines(wp) == FAIL)
2726 {
2727 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002728 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00002731 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
2732 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002733 outofmem = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002734#ifdef FEAT_PROP_POPUP
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002735 // global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002736 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002737 if (win_alloc_lines(wp) == FAIL)
2738 {
2739 outofmem = TRUE;
2740 goto give_up;
2741 }
2742 // tab-local popup windows
2743 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002744 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar8caaf822019-06-01 18:11:22 +02002745 if (win_alloc_lines(wp) == FAIL)
2746 {
2747 outofmem = TRUE;
2748 goto give_up;
2749 }
2750#endif
2751
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00002752give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002754 for (i = 0; i < p_mco; ++i)
2755 if (new_ScreenLinesC[i] == NULL)
2756 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002758 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 || new_ScreenAttrs == NULL
Bram Moolenaarb9081882022-07-09 04:56:24 +01002761 || new_ScreenCols == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 || new_LineOffset == NULL
2763 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00002764 || new_TabPageIdxs == NULL
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002765#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002766 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002767 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002768 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02002769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 || outofmem)
2771 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002772 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002773 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002774 // guess the size
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002775 do_outofmem_msg((long_u)((Rows + 1) * Columns));
2776
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002777 // Remember we did this to avoid getting outofmem messages over
2778 // and over again.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002779 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002780 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002781 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002782 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002783 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002784 VIM_CLEAR(new_ScreenLinesC[i]);
2785 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002786 VIM_CLEAR(new_ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002787 VIM_CLEAR(new_ScreenCols);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002788 VIM_CLEAR(new_LineOffset);
2789 VIM_CLEAR(new_LineWraps);
2790 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002791#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002792 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002793 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002794 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797 else
2798 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002799 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002800
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 for (new_row = 0; new_row < Rows; ++new_row)
2802 {
2803 new_LineOffset[new_row] = new_row * Columns;
2804 new_LineWraps[new_row] = FALSE;
2805
2806 /*
2807 * If the screen is not going to be cleared, copy as much as
2808 * possible from the old screen to the new one and clear the rest
2809 * (used when resizing the window at the "--more--" prompt or when
2810 * executing an external command, for the GUI).
2811 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002812 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
2814 (void)vim_memset(new_ScreenLines + new_row * Columns,
2815 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 if (enc_utf8)
2817 {
2818 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
2819 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002820 for (i = 0; i < p_mco; ++i)
2821 (void)vim_memset(new_ScreenLinesC[i]
2822 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 0, (size_t)Columns * sizeof(u8char_T));
2824 }
2825 if (enc_dbcs == DBCS_JPNU)
2826 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
2827 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
2829 0, (size_t)Columns * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002830 (void)vim_memset(new_ScreenCols + new_row * Columns,
2831 0, (size_t)Columns * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002833 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
2835 if (screen_Columns < Columns)
2836 len = screen_Columns;
2837 else
2838 len = Columns;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002839 // When switching to utf-8 don't copy characters, they
2840 // may be invalid now. Also when p_mco changes.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002841 if (!(enc_utf8 && ScreenLinesUC == NULL)
2842 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002843 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
2844 ScreenLines + LineOffset[old_row],
2845 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002846 if (enc_utf8 && ScreenLinesUC != NULL
2847 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
2849 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
2850 ScreenLinesUC + LineOffset[old_row],
2851 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002852 for (i = 0; i < p_mco; ++i)
2853 mch_memmove(new_ScreenLinesC[i]
2854 + new_LineOffset[new_row],
2855 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 (size_t)len * sizeof(u8char_T));
2857 }
2858 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
2859 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
2860 ScreenLines2 + LineOffset[old_row],
2861 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
2863 ScreenAttrs + LineOffset[old_row],
2864 (size_t)len * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01002865 mch_memmove(new_ScreenCols + new_LineOffset[new_row],
2866 ScreenAttrs + LineOffset[old_row],
2867 (size_t)len * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 }
2870 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002871 // Use the last line of the screen for the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 current_ScreenLine = new_ScreenLines + Rows * Columns;
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002873
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002874#ifdef FEAT_PROP_POPUP
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002875 vim_memset(new_popup_mask, 0, Rows * Columns * sizeof(short));
2876 vim_memset(new_popup_transparent, 0, Rows * Columns * sizeof(char));
2877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 }
2879
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002880 free_screenlines();
2881
Bram Moolenaar6ace95e2019-08-13 23:09:49 +02002882 // NOTE: this may result in all pointers to become NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002885 for (i = 0; i < p_mco; ++i)
2886 ScreenLinesC[i] = new_ScreenLinesC[i];
2887 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 ScreenAttrs = new_ScreenAttrs;
Bram Moolenaarb9081882022-07-09 04:56:24 +01002890 ScreenCols = new_ScreenCols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 LineOffset = new_LineOffset;
2892 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002893 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002894#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002895 popup_mask = new_popup_mask;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002896 popup_mask_next = new_popup_mask_next;
2897 popup_transparent = new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02002898 popup_mask_refresh = TRUE;
2899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002901 // It's important that screen_Rows and screen_Columns reflect the actual
2902 // size of ScreenLines[]. Set them before calling anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903#ifdef FEAT_GUI
2904 old_Rows = screen_Rows;
2905#endif
2906 screen_Rows = Rows;
2907 screen_Columns = Columns;
2908
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002909 set_must_redraw(UPD_CLEAR); // need to clear the screen later
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002910 if (doclear)
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002911 screenclear2(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#ifdef FEAT_GUI
2913 else if (gui.in_use
2914 && !gui.starting
2915 && ScreenLines != NULL
2916 && old_Rows != Rows)
2917 {
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002918 gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
2919
2920 // Adjust the position of the cursor, for when executing an external
2921 // command.
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002922 if (msg_row >= Rows) // Rows got smaller
2923 msg_row = Rows - 1; // put cursor at last row
2924 else if (Rows > old_Rows) // Rows got bigger
2925 msg_row += Rows - old_Rows; // put cursor in same place
2926 if (msg_col >= Columns) // Columns got smaller
2927 msg_col = Columns - 1; // put cursor at last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 }
2929#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02002930 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaarb3f74062020-02-26 16:16:53 +01002932#ifdef FEAT_GUI_HAIKU
2933 vim_unlock_screen();
2934#endif
2935
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00002937 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002938
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002939 /*
2940 * Do not apply autocommands more than 3 times to avoid an endless loop
2941 * in case applying autocommands always changes Rows or Columns.
2942 */
2943 if (starting == 0 && ++retry_count <= 3)
2944 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002945 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002946 // In rare cases, autocommands may have altered Rows or Columns,
2947 // jump back to check if we need to allocate the screen again.
Bram Moolenaar87e817c2009-02-22 20:13:39 +00002948 goto retry;
2949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002953free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002954{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002955 int i;
2956
Bram Moolenaar33796b32019-06-08 16:01:13 +02002957 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002958 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02002959 VIM_CLEAR(ScreenLinesC[i]);
2960 VIM_CLEAR(ScreenLines2);
2961 VIM_CLEAR(ScreenLines);
2962 VIM_CLEAR(ScreenAttrs);
Bram Moolenaarb9081882022-07-09 04:56:24 +01002963 VIM_CLEAR(ScreenCols);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002964 VIM_CLEAR(LineOffset);
2965 VIM_CLEAR(LineWraps);
2966 VIM_CLEAR(TabPageIdxs);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002967#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02002968 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02002969 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02002970 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02002971#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002972}
2973
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002974/*
2975 * Clear the screen.
2976 * May delay if there is something the user should read.
2977 * Allocated the screen for resizing if needed.
2978 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002979 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002980screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 check_for_delay(FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002983 screenalloc(FALSE); // allocate screen buffers if size changed
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002984 screenclear2(TRUE); // clear the screen
2985}
2986
2987/*
2988 * Do not clear the screen but mark everything for redraw.
2989 */
2990 void
2991redraw_as_cleared(void)
2992{
2993 screenclear2(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994}
2995
2996 static void
Bram Moolenaarb13d3402022-08-29 13:44:28 +01002997screenclear2(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 int i;
3000
3001 if (starting == NO_SCREEN || ScreenLines == NULL
3002#ifdef FEAT_GUI
3003 || (gui.in_use && gui.starting)
3004#endif
3005 )
3006 return;
3007
3008#ifdef FEAT_GUI
3009 if (!gui.in_use)
3010#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003011 screen_attr = -1; // force setting the Normal colors
3012 screen_stop_highlight(); // don't want highlighting here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
3014#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003015 // disable selection without redrawing it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 clip_scroll_selection(9999);
3017#endif
3018
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003019 // blank out ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 for (i = 0; i < Rows; ++i)
3021 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003022 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 LineWraps[i] = FALSE;
3024 }
3025
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003026 if (doclear && can_clear(T_CL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003028 out_str(T_CL); // clear the display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003030 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
3032 else
3033 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003034 // can't clear the screen, mark all chars with invalid attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 for (i = 0; i < Rows; ++i)
3036 lineinvalid(LineOffset[i], (int)Columns);
3037 clear_cmdline = TRUE;
3038 }
3039
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003040 screen_cleared = TRUE; // can use contents of ScreenLines now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
Bram Moolenaarb13d3402022-08-29 13:44:28 +01003042 win_rest_invalid(firstwin); // redraw all regular windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003044 redraw_tabline = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003045 if (must_redraw == UPD_CLEAR) // no need to clear again
3046 must_redraw = UPD_NOT_VALID;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01003047 msg_scrolled = 0; // compute_cmdrow() uses this
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 compute_cmdrow();
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01003049#ifdef FEAT_PROP_POPUP
3050 popup_redraw_all(); // redraw all popup windows
3051#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003052 msg_row = cmdline_row; // put cursor on last line for messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003054 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 msg_didany = FALSE;
3056 msg_didout = FALSE;
3057}
3058
3059/*
3060 * Clear one line in ScreenLines.
3061 */
3062 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003063lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 if (enc_utf8)
3067 (void)vim_memset(ScreenLinesUC + off, 0,
3068 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003069 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003070 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071}
3072
3073/*
3074 * Mark one line in ScreenLines invalid by setting the attributes to an
3075 * invalid value.
3076 */
3077 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003078lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003081 (void)vim_memset(ScreenCols + off, -1, (size_t)width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082}
3083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084/*
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003085 * To be called when characters were sent to the terminal directly, outputting
3086 * test on "screen_lnum".
3087 */
3088 void
3089line_was_clobbered(int screen_lnum)
3090{
3091 lineinvalid(LineOffset[screen_lnum], (int)Columns);
3092}
3093
3094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 * Copy part of a Screenline for vertically split window "wp".
3096 */
3097 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003098linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 unsigned off_to = LineOffset[to] + wp->w_wincol;
3101 unsigned off_from = LineOffset[from] + wp->w_wincol;
3102
3103 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
3104 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 if (enc_utf8)
3106 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003107 int i;
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
3110 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003111 for (i = 0; i < p_mco; ++i)
3112 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
3113 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
3115 if (enc_dbcs == DBCS_JPNU)
3116 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
3117 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
3119 wp->w_width * sizeof(sattr_T));
Bram Moolenaarb9081882022-07-09 04:56:24 +01003120 mch_memmove(ScreenCols + off_to, ScreenCols + off_from,
3121 wp->w_width * sizeof(colnr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124/*
3125 * Return TRUE if clearing with term string "p" would work.
3126 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02003127 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 */
3129 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003130can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 return (*p != NUL && (t_colors <= 1
3133#ifdef FEAT_GUI
3134 || gui.in_use
3135#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003136#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003137 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02003138 || (!p_tgc && cterm_normal_bg_color == 0)
3139#else
3140 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003141#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02003142 || *T_UT != NUL)
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003143#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003144 && !(p == T_CE && popup_visible)
3145#endif
3146 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147}
3148
3149/*
3150 * Reset cursor position. Use whenever cursor was moved because of outputting
3151 * something directly to the screen (shell commands) or a terminal control
3152 * code.
3153 */
3154 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003155screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
3157 screen_cur_row = screen_cur_col = 9999;
3158}
3159
3160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * Move the cursor to position "row","col" in the screen.
3162 * This tries to find the most efficient way to move, minimizing the number of
3163 * characters sent to the terminal.
3164 */
3165 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003166windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003168 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 int i;
3170 int plan;
3171 int cost;
3172 int wouldbe_col;
3173 int noinvcurs;
3174 char_u *bs;
3175 int goto_cost;
3176 int attr;
3177
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003178#define GOTO_COST 7 // assume a term_windgoto() takes about 7 chars
3179#define HIGHL_COST 5 // assume unhighlight takes 5 chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181#define PLAN_LE 1
3182#define PLAN_CR 2
3183#define PLAN_NL 3
3184#define PLAN_WRITE 4
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003185 // Can't use ScreenLines unless initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (ScreenLines == NULL)
3187 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (col != screen_cur_col || row != screen_cur_row)
3189 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003190 // Check for valid position.
3191 if (row < 0) // window without text lines?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 row = 0;
3193 if (row >= screen_Rows)
3194 row = screen_Rows - 1;
3195 if (col >= screen_Columns)
3196 col = screen_Columns - 1;
3197
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003198 // check if no cursor movement is allowed in highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (screen_attr && *T_MS == NUL)
3200 noinvcurs = HIGHL_COST;
3201 else
3202 noinvcurs = 0;
3203 goto_cost = GOTO_COST + noinvcurs;
3204
3205 /*
3206 * Plan how to do the positioning:
3207 * 1. Use CR to move it to column 0, same row.
3208 * 2. Use T_LE to move it a few columns to the left.
3209 * 3. Use NL to move a few lines down, column 0.
3210 * 4. Move a few columns to the right with T_ND or by writing chars.
3211 *
3212 * Don't do this if the cursor went beyond the last column, the cursor
3213 * position is unknown then (some terminals wrap, some don't )
3214 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00003215 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * characters to move the cursor to the right.
3217 */
3218 if (row >= screen_cur_row && screen_cur_col < Columns)
3219 {
3220 /*
3221 * If the cursor is in the same row, bigger col, we can use CR
3222 * or T_LE.
3223 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003224 bs = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 attr = screen_attr;
3226 if (row == screen_cur_row && col < screen_cur_col)
3227 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003228 // "le" is preferred over "bc", because "bc" is obsolete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (*T_LE)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003230 bs = T_LE; // "cursor left"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003232 bs = T_BC; // "backspace character (old)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (*bs)
3234 cost = (screen_cur_col - col) * (int)STRLEN(bs);
3235 else
3236 cost = 999;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003237 if (col + 1 < cost) // using CR is less characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 plan = PLAN_CR;
3240 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003241 cost = 1; // CR is just one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
3243 else
3244 {
3245 plan = PLAN_LE;
3246 wouldbe_col = col;
3247 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003248 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
3250 cost += noinvcurs;
3251 attr = 0;
3252 }
3253 }
3254
3255 /*
3256 * If the cursor is above where we want to be, we can use CR LF.
3257 */
3258 else if (row > screen_cur_row)
3259 {
3260 plan = PLAN_NL;
3261 wouldbe_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003262 cost = (row - screen_cur_row) * 2; // CR LF
3263 if (noinvcurs) // will stop highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 cost += noinvcurs;
3266 attr = 0;
3267 }
3268 }
3269
3270 /*
3271 * If the cursor is in the same row, smaller col, just use write.
3272 */
3273 else
3274 {
3275 plan = PLAN_WRITE;
3276 wouldbe_col = screen_cur_col;
3277 cost = 0;
3278 }
3279
3280 /*
3281 * Check if any characters that need to be written have the
3282 * correct attributes. Also avoid UTF-8 characters.
3283 */
3284 i = col - wouldbe_col;
3285 if (i > 0)
3286 cost += i;
3287 if (cost < goto_cost && i > 0)
3288 {
3289 /*
3290 * Check if the attributes are correct without additionally
3291 * stopping highlighting.
3292 */
3293 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
3294 while (i && *p++ == attr)
3295 --i;
3296 if (i != 0)
3297 {
3298 /*
3299 * Try if it works when highlighting is stopped here.
3300 */
3301 if (*--p == 0)
3302 {
3303 cost += noinvcurs;
3304 while (i && *p++ == 0)
3305 --i;
3306 }
3307 if (i != 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003308 cost = 999; // different attributes, don't do it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (enc_utf8)
3311 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003312 // Don't use an UTF-8 char for positioning, it's slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 for (i = wouldbe_col; i < col; ++i)
3314 if (ScreenLinesUC[LineOffset[row] + i] != 0)
3315 {
3316 cost = 999;
3317 break;
3318 }
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
3321
3322 /*
3323 * We can do it without term_windgoto()!
3324 */
3325 if (cost < goto_cost)
3326 {
3327 if (plan == PLAN_LE)
3328 {
3329 if (noinvcurs)
3330 screen_stop_highlight();
3331 while (screen_cur_col > col)
3332 {
3333 out_str(bs);
3334 --screen_cur_col;
3335 }
3336 }
3337 else if (plan == PLAN_CR)
3338 {
3339 if (noinvcurs)
3340 screen_stop_highlight();
3341 out_char('\r');
3342 screen_cur_col = 0;
3343 }
3344 else if (plan == PLAN_NL)
3345 {
3346 if (noinvcurs)
3347 screen_stop_highlight();
3348 while (screen_cur_row < row)
3349 {
3350 out_char('\n');
3351 ++screen_cur_row;
3352 }
3353 screen_cur_col = 0;
3354 }
3355
3356 i = col - screen_cur_col;
3357 if (i > 0)
3358 {
3359 /*
3360 * Use cursor-right if it's one character only. Avoids
3361 * removing a line of pixels from the last bold char, when
3362 * using the bold trick in the GUI.
3363 */
3364 if (T_ND[0] != NUL && T_ND[1] == NUL)
3365 {
3366 while (i-- > 0)
3367 out_char(*T_ND);
3368 }
3369 else
3370 {
3371 int off;
3372
3373 off = LineOffset[row] + screen_cur_col;
3374 while (i-- > 0)
3375 {
3376 if (ScreenAttrs[off] != screen_attr)
3377 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 if (enc_dbcs == DBCS_JPNU
3381 && ScreenLines[off] == 0x8e)
3382 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 ++off;
3384 }
3385 }
3386 }
3387 }
3388 }
3389 else
3390 cost = 999;
3391
3392 if (cost >= goto_cost)
3393 {
3394 if (noinvcurs)
3395 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02003396 if (row == screen_cur_row && (col > screen_cur_col)
3397 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 term_cursor_right(col - screen_cur_col);
3399 else
3400 term_windgoto(row, col);
3401 }
3402 screen_cur_row = row;
3403 screen_cur_col = col;
3404 }
3405}
3406
3407/*
3408 * Set cursor to its position in the current window.
3409 */
3410 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003411setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
Bram Moolenaar987723e2018-03-06 11:43:04 +01003413 setcursor_mayforce(FALSE);
3414}
3415
3416/*
3417 * Set cursor to its position in the current window.
3418 * When "force" is TRUE also when not redrawing.
3419 */
3420 void
3421setcursor_mayforce(int force)
3422{
3423 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 validate_cursor();
3426 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003427 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003429 // With 'rightleft' set and the cursor on a double-wide
3430 // character, position it on the leftmost column.
Bram Moolenaara12a1612019-01-24 16:39:02 +01003431 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
3432 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00003433 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01003434 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435#endif
3436 curwin->w_wcol));
3437 }
3438}
3439
3440
3441/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003442 * Insert 'line_count' lines at 'row' in window 'wp'.
3443 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
3444 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 * scrolling.
3446 * Returns FAIL if the lines are not inserted, OK for success.
3447 */
3448 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003449win_ins_lines(
3450 win_T *wp,
3451 int row,
3452 int line_count,
3453 int invalid,
3454 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 int did_delete;
3457 int nextrow;
3458 int lastrow;
3459 int retval;
3460
3461 if (invalid)
3462 wp->w_lines_valid = 0;
3463
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003464 // with only a few lines it's not worth the effort
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (wp->w_height < 5)
3466 return FAIL;
3467
Bram Moolenaarc856ceb2022-06-21 18:10:39 +01003468 // with the popup menu visible this might not work correctly
3469 if (pum_visible())
3470 return FAIL;
3471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (line_count > wp->w_height - row)
3473 line_count = wp->w_height - row;
3474
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003475 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (retval != MAYBE)
3477 return retval;
3478
3479 /*
3480 * If there is a next window or a status line, we first try to delete the
3481 * lines at the bottom to avoid messing what is after the window.
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003482 * If this fails and there are following windows, don't do anything to
3483 * avoid messing up those windows, better just redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 */
3485 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (wp->w_next != NULL || wp->w_status_height)
3487 {
3488 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003489 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 did_delete = TRUE;
3491 else if (wp->w_next)
3492 return FAIL;
3493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 /*
3495 * if no lines deleted, blank the lines that will end up below the window
3496 */
3497 if (!did_delete)
3498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003501 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 lastrow = nextrow + line_count;
3503 if (lastrow > Rows)
3504 lastrow = Rows;
3505 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003506 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 ' ', ' ', 0);
3508 }
3509
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003510 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 == FAIL)
3512 {
Bram Moolenaarc363fe12019-08-04 18:13:46 +02003513 // deletion will have messed up other windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (did_delete)
3515 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 win_rest_invalid(W_NEXT(wp));
3518 }
3519 return FAIL;
3520 }
3521
3522 return OK;
3523}
3524
3525/*
Bram Moolenaar86033562017-07-12 20:24:41 +02003526 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
3528 * If "mayclear" is TRUE the screen will be cleared if it is faster than
3529 * scrolling
3530 * Return OK for success, FAIL if the lines are not deleted.
3531 */
3532 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003533win_del_lines(
3534 win_T *wp,
3535 int row,
3536 int line_count,
3537 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003538 int mayclear,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003539 int clear_attr) // for clearing lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540{
3541 int retval;
3542
3543 if (invalid)
3544 wp->w_lines_valid = 0;
3545
3546 if (line_count > wp->w_height - row)
3547 line_count = wp->w_height - row;
3548
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003549 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (retval != MAYBE)
3551 return retval;
3552
3553 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003554 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 return FAIL;
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /*
3558 * If there are windows or status lines below, try to put them at the
3559 * correct place. If we can't do that, they have to be redrawn.
3560 */
3561 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
3562 {
3563 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003564 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
3566 wp->w_redr_status = TRUE;
3567 win_rest_invalid(wp->w_next);
3568 }
3569 }
3570 /*
3571 * If this is the last window and there is no status line, redraw the
3572 * command line later.
3573 */
3574 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 redraw_cmdline = TRUE;
3576 return OK;
3577}
3578
3579/*
3580 * Common code for win_ins_lines() and win_del_lines().
3581 * Returns OK or FAIL when the work has been done.
3582 * Returns MAYBE when not finished yet.
3583 */
3584 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003585win_do_lines(
3586 win_T *wp,
3587 int row,
3588 int line_count,
3589 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003590 int del,
3591 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
3593 int retval;
3594
3595 if (!redrawing() || line_count <= 0)
3596 return FAIL;
3597
Bram Moolenaar33796b32019-06-08 16:01:13 +02003598 // When inserting lines would result in loss of command output, just redraw
3599 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003600 if (no_win_do_lines_ins && !del)
3601 return FAIL;
3602
Bram Moolenaar33796b32019-06-08 16:01:13 +02003603 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02003604 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003606 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02003607 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 return FAIL;
3609 }
3610
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003611#ifdef FEAT_PROP_POPUP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003612 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02003613 if (popup_visible)
3614 return FAIL;
3615#endif
3616
3617 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (row + line_count >= wp->w_height)
3619 {
3620 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02003621 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 ' ', ' ', 0);
3623 return OK;
3624 }
3625
3626 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003627 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003629 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003631 if (!no_win_do_lines_ins)
3632 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 /*
3635 * If the terminal can set a scroll region, use that.
3636 * Always do this in a vertically split window. This will redraw from
3637 * ScreenLines[] when t_CV isn't defined. That's faster than using
3638 * win_line().
3639 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003640 * a character in the lower right corner of the scroll region may cause a
3641 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
Bram Moolenaar02631462017-09-22 15:20:32 +02003643 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 scroll_region_set(wp, row);
3647 if (del)
3648 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003649 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 else
3651 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003652 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 scroll_region_reset();
3655 return retval;
3656 }
3657
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003658 if (wp->w_next != NULL && p_tf) // don't delete/insert on fast terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 return MAYBE;
3662}
3663
3664/*
3665 * window 'wp' and everything after it is messed up, mark it for redraw
3666 */
3667 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003668win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003672 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 wp->w_redr_status = TRUE;
3674 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 redraw_cmdline = TRUE;
3677}
3678
3679/*
3680 * The rest of the routines in this file perform screen manipulations. The
3681 * given operation is performed physically on the screen. The corresponding
3682 * change is also made to the internal screen image. In this way, the editor
3683 * anticipates the effect of editing changes on the appearance of the screen.
3684 * That way, when we call screenupdate a complete redraw isn't usually
3685 * necessary. Another advantage is that we can keep adding code to anticipate
3686 * screen changes, and in the meantime, everything still works.
3687 */
3688
3689/*
3690 * types for inserting or deleting lines
3691 */
3692#define USE_T_CAL 1
3693#define USE_T_CDL 2
3694#define USE_T_AL 3
3695#define USE_T_CE 4
3696#define USE_T_DL 5
3697#define USE_T_SR 6
3698#define USE_NL 7
3699#define USE_T_CD 8
3700#define USE_REDRAW 9
3701
3702/*
3703 * insert lines on the screen and update ScreenLines[]
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003704 * "end" is the line after the scrolled part. Normally it is Rows.
3705 * When scrolling region used "off" is the offset from the top for the region.
3706 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 *
3708 * return FAIL for failure, OK for success.
3709 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003710 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003711screen_ins_lines(
3712 int off,
3713 int row,
3714 int line_count,
3715 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003716 int clear_attr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003717 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
3719 int i;
3720 int j;
3721 unsigned temp;
3722 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003723 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 int type;
3725 int result_empty;
3726 int can_ce = can_clear(T_CE);
3727
3728 /*
3729 * FAIL if
3730 * - there is no valid screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 * - the line count is less than one
3732 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003733 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003734 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02003735 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02003737 if (!screen_valid(TRUE)
3738 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003739 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003740#ifdef FEAT_CLIPBOARD
3741 || (clip_star.state != SELECT_CLEARED
3742 && redrawing_for_callback > 0)
3743#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003744#ifdef FEAT_PROP_POPUP
Bram Moolenaar33796b32019-06-08 16:01:13 +02003745 || popup_visible
3746#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003747 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 return FAIL;
3749
3750 /*
3751 * There are seven ways to insert lines:
3752 * 0. When in a vertically split window and t_CV isn't set, redraw the
3753 * characters from ScreenLines[].
3754 * 1. Use T_CD (clear to end of display) if it exists and the result of
3755 * the insert is just empty lines
3756 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
3757 * present or line_count > 1. It looks better if we do all the inserts
3758 * at once.
3759 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
3760 * insert is just empty lines and T_CE is not present or line_count >
3761 * 1.
3762 * 4. Use T_AL (insert line) if it exists.
3763 * 5. Use T_CE (erase line) if it exists and the result of the insert is
3764 * just empty lines.
3765 * 6. Use T_DL (delete line) if it exists and the result of the insert is
3766 * just empty lines.
3767 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
3768 * the 'da' flag is not set or we have clear line capability.
3769 * 8. redraw the characters from ScreenLines[].
3770 *
3771 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
3772 * the scrollbar for the window. It does have insert line, use that if it
3773 * exists.
3774 */
3775 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003777 {
3778 // Avoid that lines are first cleared here and then redrawn, which
3779 // results in many characters updated twice. This happens with CTRL-F
3780 // in a vertically split window. With line-by-line scrolling
3781 // USE_REDRAW should be faster.
3782 if (line_count > 3)
3783 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003785 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003786 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 type = USE_T_CD;
3788 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
3789 type = USE_T_CAL;
3790 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
3791 type = USE_T_CDL;
3792 else if (*T_AL != NUL)
3793 type = USE_T_AL;
3794 else if (can_ce && result_empty)
3795 type = USE_T_CE;
3796 else if (*T_DL != NUL && result_empty)
3797 type = USE_T_DL;
3798 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
3799 type = USE_T_SR;
3800 else
3801 return FAIL;
3802
3803 /*
3804 * For clearing the lines screen_del_lines() is used. This will also take
3805 * care of t_db if necessary.
3806 */
3807 if (type == USE_T_CD || type == USE_T_CDL ||
3808 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003809 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811 /*
3812 * If text is retained below the screen, first clear or delete as many
3813 * lines at the bottom of the window as are about to be inserted so that
3814 * the deleted lines won't later surface during a screen_del_lines.
3815 */
3816 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003817 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
3819#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003820 // Remove a modeless selection when inserting lines halfway the screen
3821 // or not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003822 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003823 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 else
3825 clip_scroll_selection(-line_count);
3826#endif
3827
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003828#ifdef FEAT_GUI_HAIKU
3829 vim_lock_screen();
3830#endif
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003833 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
3834 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02003835 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#endif
3837
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003838 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
3839 cursor_col = wp->w_wincol;
3840
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003841 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 cursor_row = row;
3843 else
3844 cursor_row = row + off;
3845
3846 /*
3847 * Shift LineOffset[] line_count down to reflect the inserted lines.
3848 * Clear the inserted lines in ScreenLines[].
3849 */
3850 row += off;
3851 end += off;
3852 for (i = 0; i < line_count; ++i)
3853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (wp != NULL && wp->w_width != Columns)
3855 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003856 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 j = end - 1 - i;
3858 while ((j -= line_count) >= row)
3859 linecopy(j + line_count, j, wp);
3860 j += line_count;
3861 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003862 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
3863 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 else
3865 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
3866 LineWraps[j] = FALSE;
3867 }
3868 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 j = end - 1 - i;
3871 temp = LineOffset[j];
3872 while ((j -= line_count) >= row)
3873 {
3874 LineOffset[j + line_count] = LineOffset[j];
3875 LineWraps[j + line_count] = LineWraps[j];
3876 }
3877 LineOffset[j + line_count] = temp;
3878 LineWraps[j + line_count] = FALSE;
3879 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003880 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 else
3882 lineinvalid(temp, (int)Columns);
3883 }
3884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885
Bram Moolenaarb3f74062020-02-26 16:16:53 +01003886#ifdef FEAT_GUI_HAIKU
3887 vim_unlock_screen();
3888#endif
3889
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003891 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003892 if (clear_attr != 0)
3893 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003895 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (type == USE_REDRAW)
3897 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02003898 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 {
3900 term_append_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003901 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 else
3904 {
3905 for (i = 0; i < line_count; i++)
3906 {
3907 if (type == USE_T_AL)
3908 {
3909 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003910 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 out_str(T_AL);
3912 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003913 else // type == USE_T_SR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 out_str(T_SR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003915 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917 }
3918
3919 /*
3920 * With scroll-reverse and 'da' flag set we need to clear the lines that
3921 * have been scrolled down into the region.
3922 */
3923 if (type == USE_T_SR && *T_DA)
3924 {
3925 for (i = 0; i < line_count; ++i)
3926 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003927 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 out_str(T_CE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003929 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 }
3932
3933#ifdef FEAT_GUI
3934 gui_can_update_cursor();
3935 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003936 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937#endif
3938 return OK;
3939}
3940
3941/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02003942 * Delete lines on the screen and update ScreenLines[].
3943 * "end" is the line after the scrolled part. Normally it is Rows.
3944 * When scrolling region used "off" is the offset from the top for the region.
3945 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 *
3947 * Return OK for success, FAIL if the lines are not deleted.
3948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003950screen_del_lines(
3951 int off,
3952 int row,
3953 int line_count,
3954 int end,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003955 int force, // even when line_count > p_ttyscroll
3956 int clear_attr, // used for clearing lines
Bram Moolenaar8ef69972022-04-03 13:23:22 +01003957 win_T *wp) // NULL or window to use width from
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
3959 int j;
3960 int i;
3961 unsigned temp;
3962 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02003963 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 int cursor_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003965 int result_empty; // result is empty until end of region
3966 int can_delete; // deleting line codes can be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 int type;
3968
3969 /*
3970 * FAIL if
3971 * - there is no valid screen
3972 * - the screen has to be redrawn completely
3973 * - the line count is less than one
3974 * - the line count is more than 'ttyscroll'
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003975 * - "end" is more than "Rows" (safety check, should not happen)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003976 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 */
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003978 if (!screen_valid(TRUE)
3979 || line_count <= 0
3980 || (!force && line_count > p_ttyscroll)
3981 || end > Rows
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003982#ifdef FEAT_CLIPBOARD
Bram Moolenaar17fa2332022-04-01 19:44:47 +01003983 || (clip_star.state != SELECT_CLEARED && redrawing_for_callback > 0)
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02003984#endif
3985 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 return FAIL;
3987
3988 /*
3989 * Check if the rest of the current region will become empty.
3990 */
3991 result_empty = row + line_count >= end;
3992
3993 /*
3994 * We can delete lines only when 'db' flag not set or when 'ce' option
3995 * available.
3996 */
3997 can_delete = (*T_DB == NUL || can_clear(T_CE));
3998
3999 /*
4000 * There are six ways to delete lines:
4001 * 0. When in a vertically split window and t_CV isn't set, redraw the
4002 * characters from ScreenLines[].
4003 * 1. Use T_CD if it exists and the result is empty.
4004 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
4005 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
4006 * none of the other ways work.
4007 * 4. Use T_CE (erase line) if the result is empty.
4008 * 5. Use T_DL (delete line) if it exists.
4009 * 6. redraw the characters from ScreenLines[].
4010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
Bram Moolenaar8ef69972022-04-03 13:23:22 +01004012 {
4013 // Avoid that lines are first cleared here and then redrawn, which
4014 // results in many characters updated twice. This happens with CTRL-F
4015 // in a vertically split window. With line-by-line scrolling
4016 // USE_REDRAW should be faster.
4017 if (line_count > 3)
4018 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 type = USE_REDRAW;
Bram Moolenaar8ef69972022-04-03 13:23:22 +01004020 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004021 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 type = USE_T_CD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 else if (row == 0 && (
4024#ifndef AMIGA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004025 // On the Amiga, somehow '\n' on the last line doesn't always scroll
4026 // up, so use delete-line command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 line_count == 1 ||
4028#endif
4029 *T_CDL == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 type = USE_NL;
4031 else if (*T_CDL != NUL && line_count > 1 && can_delete)
4032 type = USE_T_CDL;
4033 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02004034 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 type = USE_T_CE;
4036 else if (*T_DL != NUL && can_delete)
4037 type = USE_T_DL;
4038 else if (*T_CDL != NUL && can_delete)
4039 type = USE_T_CDL;
4040 else
4041 return FAIL;
4042
4043#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004044 // Remove a modeless selection when deleting lines halfway the screen or
4045 // not the full width of the screen.
Bram Moolenaar4033c552017-09-16 20:54:51 +02004046 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02004047 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 else
4049 clip_scroll_selection(line_count);
4050#endif
4051
Bram Moolenaar92c461e2020-04-24 22:19:00 +02004052#ifdef FEAT_GUI_HAIKU
4053 vim_lock_screen();
4054#endif
4055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#ifdef FEAT_GUI
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004057 // Don't update the GUI cursor here, ScreenLines[] is invalid until the
4058 // scrolling is actually carried out.
Bram Moolenaar107abd22016-08-12 14:08:25 +02004059 gui_dont_update_cursor(gui.cursor_row >= row + off
4060 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#endif
4062
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004063 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
4064 cursor_col = wp->w_wincol;
4065
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004066 if (*T_CCS != NUL) // cursor relative to region
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
4068 cursor_row = row;
4069 cursor_end = end;
4070 }
4071 else
4072 {
4073 cursor_row = row + off;
4074 cursor_end = end + off;
4075 }
4076
4077 /*
4078 * Now shift LineOffset[] line_count up to reflect the deleted lines.
4079 * Clear the inserted lines in ScreenLines[].
4080 */
4081 row += off;
4082 end += off;
4083 for (i = 0; i < line_count; ++i)
4084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (wp != NULL && wp->w_width != Columns)
4086 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004087 // need to copy part of a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 j = row + i;
4089 while ((j += line_count) <= end - 1)
4090 linecopy(j - line_count, j, wp);
4091 j -= line_count;
4092 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004093 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
4094 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 else
4096 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
4097 LineWraps[j] = FALSE;
4098 }
4099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004101 // whole width, moving the line pointers is faster
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 j = row + i;
4103 temp = LineOffset[j];
4104 while ((j += line_count) <= end - 1)
4105 {
4106 LineOffset[j - line_count] = LineOffset[j];
4107 LineWraps[j - line_count] = LineWraps[j];
4108 }
4109 LineOffset[j - line_count] = temp;
4110 LineWraps[j - line_count] = FALSE;
4111 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004112 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 else
4114 lineinvalid(temp, (int)Columns);
4115 }
4116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004118#ifdef FEAT_GUI_HAIKU
4119 vim_unlock_screen();
4120#endif
4121
Bram Moolenaarcfce7172017-08-17 20:31:48 +02004122 if (screen_attr != clear_attr)
4123 screen_stop_highlight();
4124 if (clear_attr != 0)
4125 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004127 // redraw the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if (type == USE_REDRAW)
4129 redraw_block(row, end, wp);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004130 else if (type == USE_T_CD) // delete the lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004132 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 out_str(T_CD);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004134 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 else if (type == USE_T_CDL)
4137 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004138 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 term_delete_lines(line_count);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004140 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142 /*
4143 * Deleting lines at top of the screen or scroll region: Just scroll
4144 * the whole screen (scroll region) up by outputting newlines on the
4145 * last line.
4146 */
4147 else if (type == USE_NL)
4148 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004149 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 for (i = line_count; --i >= 0; )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004151 out_char('\n'); // cursor will remain on same line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153 else
4154 {
4155 for (i = line_count; --i >= 0; )
4156 {
4157 if (type == USE_T_DL)
4158 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004159 windgoto(cursor_row, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004160 out_str(T_DL); // delete a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004162 else // type == USE_T_CE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004164 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004165 out_str(T_CE); // erase a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004167 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 }
4169 }
4170
4171 /*
4172 * If the 'db' flag is set, we need to clear the lines that have been
4173 * scrolled up at the bottom of the region.
4174 */
4175 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
4176 {
4177 for (i = line_count; i > 0; --i)
4178 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02004179 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004180 out_str(T_CE); // erase a line
4181 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183 }
4184
4185#ifdef FEAT_GUI
4186 gui_can_update_cursor();
4187 if (gui.in_use)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004188 out_flush(); // always flush after a scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#endif
4190
4191 return OK;
4192}
4193
4194/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004195 * Return TRUE when postponing displaying the mode message: when not redrawing
4196 * or inside a mapping.
4197 */
4198 int
4199skip_showmode()
4200{
4201 // Call char_avail() only when we are going to show something, because it
Bram Moolenaar944cc9c2022-06-27 22:17:37 +01004202 // takes a bit of time. redrawing() may also call char_avail().
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004203 if (global_busy
4204 || msg_silent != 0
4205 || !redrawing()
4206 || (char_avail() && !KeyTyped))
4207 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004208 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004209 return TRUE;
4210 }
4211 return FALSE;
4212}
4213
4214/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01004215 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 *
4217 * If clear_cmdline is TRUE, clear the rest of the cmdline.
4218 * If clear_cmdline is FALSE there may be a message there that needs to be
4219 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004220 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 * Return the length of the message (0 if no message).
4222 */
4223 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004224showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 int need_clear;
4227 int length = 0;
4228 int do_mode;
4229 int attr;
4230 int nwr_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 int sub_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
Bram Moolenaara2a89732022-08-31 14:46:18 +01004233 do_mode = p_smd && msg_silent == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01004234 && ((State & MODE_INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02004235 || restart_edit != NUL
Bram Moolenaar9198de32022-08-27 21:30:03 +01004236 || VIsual_active);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004237 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01004239 if (skip_showmode())
4240 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 nwr_save = need_wait_return;
4243
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004244 // wait a bit before overwriting an important message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 check_for_delay(FALSE);
4246
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004247 // if the cmdline is more than one line high, erase top lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 need_clear = clear_cmdline;
4249 if (clear_cmdline && cmdline_row < Rows - 1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004250 msg_clr_cmdline(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004252 // Position on the last line in the window, column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 msg_pos_mode();
4254 cursor_off();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004255 attr = HL_ATTR(HLF_CM); // Highlight mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 if (do_mode)
4257 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004258 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00004260 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02004261# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00004262 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02004263# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00004264 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00004265# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02004266 )
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004267# ifdef FEAT_GUI_GTK // most of the time, it's not XIM being used
Bram Moolenaar32526b32019-01-19 17:43:09 +01004268 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004270 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271# endif
4272#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004273 // CTRL-X in Insert mode
Bram Moolenaarea389e92014-05-28 21:40:52 +02004274 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004276 // These messages can get long, avoid a wrap in a narrow
4277 // window. Prefer showing edit_submode_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 length = (Rows - msg_row) * Columns - 3;
4279 if (edit_submode_extra != NULL)
4280 length -= vim_strsize(edit_submode_extra);
4281 if (length > 0)
4282 {
4283 if (edit_submode_pre != NULL)
4284 length -= vim_strsize(edit_submode_pre);
4285 if (length - vim_strsize(edit_submode) > 0)
4286 {
4287 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004288 msg_puts_attr((char *)edit_submode_pre, attr);
4289 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 if (edit_submode_extra != NULL)
4292 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004293 msg_puts_attr(" ", attr); // add a space in between
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004295 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 else
4297 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004298 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004305 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02004306 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004307 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar24959102022-05-07 20:01:16 +01004308 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
4310#ifdef FEAT_RIGHTLEFT
4311 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004312 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01004314 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004316 else if (restart_edit == 'I' || restart_edit == 'i' ||
4317 restart_edit == 'a' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004318 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004320 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004322 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323#ifdef FEAT_RIGHTLEFT
4324 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004325 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326#endif
4327#ifdef FEAT_KEYMAP
Bram Moolenaar24959102022-05-07 20:01:16 +01004328 if (State & MODE_LANGMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330# ifdef FEAT_ARABIC
4331 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004332 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 else
4334# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004335 if (get_keymap_str(curwin, (char_u *)" (%s)",
4336 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004337 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01004340 if ((State & MODE_INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004341 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 if (VIsual_active)
4344 {
4345 char *p;
4346
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004347 // Don't concatenate separate words to avoid translation
4348 // problems.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 switch ((VIsual_select ? 4 : 0)
4350 + (VIsual_mode == Ctrl_V) * 2
4351 + (VIsual_mode == 'V'))
4352 {
4353 case 0: p = N_(" VISUAL"); break;
4354 case 1: p = N_(" VISUAL LINE"); break;
4355 case 2: p = N_(" VISUAL BLOCK"); break;
4356 case 4: p = N_(" SELECT"); break;
4357 case 5: p = N_(" SELECT LINE"); break;
4358 default: p = N_(" SELECT BLOCK"); break;
4359 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004360 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004362 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004364
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 need_clear = TRUE;
4366 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004367 if (reg_recording != 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004368 && edit_submode == NULL) // otherwise it gets too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004370 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 need_clear = TRUE;
4372 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004373
4374 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004375 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 msg_clr_eos();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004377 msg_didout = FALSE; // overwrite this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 length = msg_col;
4379 msg_col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004380 need_wait_return = nwr_save; // never ask for hit-return for this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382 else if (clear_cmdline && msg_silent == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004383 // Clear the whole command line. Will reset "clear_cmdline".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004385 else if (redraw_mode)
4386 {
4387 msg_pos_mode();
4388 msg_clr_eos();
4389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391#ifdef FEAT_CMDL_INFO
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004392 // In Visual mode the size of the selected area must be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (VIsual_active)
4394 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004396 // If the last window has no status line, the ruler is after the mode
4397 // message and must be redrawn
Bram Moolenaar4033c552017-09-16 20:54:51 +02004398 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +02004399 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400#endif
4401 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02004402 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 clear_cmdline = FALSE;
4404
4405 return length;
4406}
4407
4408/*
4409 * Position for a mode message.
4410 */
4411 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004412msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
4414 msg_col = 0;
4415 msg_row = Rows - 1;
4416}
4417
4418/*
4419 * Delete mode message. Used when ESC is typed which is expected to end
4420 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004421 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 */
4423 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004424unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
4426 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01004427 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 */
4429 if (!redrawing() || (!force && char_avail() && !KeyTyped))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004430 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004432 clearmode();
4433}
4434
4435/*
4436 * Clear the mode message.
4437 */
4438 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004439clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004440{
Bram Moolenaar2abad542018-05-19 14:43:45 +02004441 int save_msg_row = msg_row;
4442 int save_msg_col = msg_col;
4443
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004444 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02004445 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004446 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +02004447 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +02004448
4449 msg_col = save_msg_col;
4450 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451}
4452
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004453 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004454recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004455{
Bram Moolenaar32526b32019-01-19 17:43:09 +01004456 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004457 if (!shortmess(SHM_RECORDING))
4458 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004459 char s[4];
4460
4461 sprintf(s, " @%c", reg_recording);
4462 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01004463 }
4464}
4465
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004466/*
4467 * Draw the tab pages line at the top of the Vim window.
4468 */
Bram Moolenaare12bab32019-01-08 22:02:56 +01004469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004470draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004471{
4472 int tabcount = 0;
4473 tabpage_T *tp;
4474 int tabwidth;
4475 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004476 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004477 int attr;
4478 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004479 win_T *cwp;
4480 int wincount;
4481 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004482 int c;
4483 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004484 int attr_sel = HL_ATTR(HLF_TPS);
4485 int attr_nosel = HL_ATTR(HLF_TP);
4486 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004487 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004488 int room;
4489 int use_sep_chars = (t_colors < 8
4490#ifdef FEAT_GUI
4491 && !gui.in_use
4492#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02004493#ifdef FEAT_TERMGUICOLORS
4494 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +02004495#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004496 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004497
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004498 if (ScreenLines == NULL)
4499 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004500 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004501
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004502#ifdef FEAT_GUI_TABLINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004503 // Take care of a GUI tabline.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004504 if (gui_use_tabline())
4505 {
4506 gui_update_tabline();
4507 return;
4508 }
4509#endif
4510
4511 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004512 return;
4513
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004514#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +02004515 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004516
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004517 // Use the 'tabline' option if it's set.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004518 if (*p_tal != NUL)
4519 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004520 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004521
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004522 // Check for an error. If there is one we would loop in redrawing the
4523 // screen. Avoid that by making 'tabline' empty.
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004524 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004525 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004526 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004527 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004528 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02004529 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004530 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00004531 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004532#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004533 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004534 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004535 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004536
Bram Moolenaar238a5642006-02-21 22:12:05 +00004537 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
4538 if (tabwidth < 6)
4539 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004540
Bram Moolenaar238a5642006-02-21 22:12:05 +00004541 attr = attr_nosel;
4542 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004543 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
4544 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004545 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004546 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004547
Bram Moolenaar238a5642006-02-21 22:12:05 +00004548 if (tp->tp_topframe == topframe)
4549 attr = attr_sel;
4550 if (use_sep_chars && col > 0)
4551 screen_putchar('|', 0, col++, attr);
4552
4553 if (tp->tp_topframe != topframe)
4554 attr = attr_nosel;
4555
4556 screen_putchar(' ', 0, col++, attr);
4557
4558 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004559 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00004560 cwp = curwin;
4561 wp = firstwin;
4562 }
4563 else
4564 {
4565 cwp = tp->tp_curwin;
4566 wp = tp->tp_firstwin;
4567 }
4568
4569 modified = FALSE;
4570 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
4571 if (bufIsChanged(wp->w_buffer))
4572 modified = TRUE;
4573 if (modified || wincount > 1)
4574 {
4575 if (wincount > 1)
4576 {
4577 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004578 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004579 if (col + len >= Columns - 3)
4580 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004581 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004582#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004583 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004584#else
Bram Moolenaare0f14822014-08-06 13:20:56 +02004585 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004586#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00004587 );
4588 col += len;
4589 }
4590 if (modified)
4591 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
4592 screen_putchar(' ', 0, col++, attr);
4593 }
4594
4595 room = scol - col + tabwidth - 1;
4596 if (room > 0)
4597 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004598 // Get buffer name in NameBuff[]
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004599 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004600 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004601 len = vim_strsize(NameBuff);
4602 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004603 if (has_mbyte)
4604 while (len > room)
4605 {
4606 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004607 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004608 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01004609 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +00004610 {
4611 p += len - room;
4612 len = room;
4613 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004614 if (len > Columns - col - 1)
4615 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004616
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004617 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004618 col += len;
4619 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00004620 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00004621
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004622 // Store the tab page number in TabPageIdxs[], so that
4623 // jump_to_mouse() knows where each one is.
Bram Moolenaar238a5642006-02-21 22:12:05 +00004624 ++tabcount;
4625 while (scol < col)
4626 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004627 }
4628
Bram Moolenaar238a5642006-02-21 22:12:05 +00004629 if (use_sep_chars)
4630 c = '_';
4631 else
4632 c = ' ';
4633 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004634
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004635 // Put an "X" for closing the current tab if there are several.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004636 if (first_tabpage->tp_next != NULL)
4637 {
4638 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
4639 TabPageIdxs[Columns - 1] = -999;
4640 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004641 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004642
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004643 // Reset the flag here again, in case evaluating 'tabline' causes it to be
4644 // set.
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004645 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004646}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004647
4648/*
4649 * Get buffer name for "buf" into NameBuff[].
4650 * Takes care of special buffer names and translates special characters.
4651 */
4652 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004653get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004654{
4655 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004656 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004657 else
4658 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
4659 trans_characters(NameBuff, MAXPATHL);
4660}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004661
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662/*
4663 * Get the character to use in a status line. Get its attributes in "*attr".
4664 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004665 int
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004666fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
4668 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004669
4670#ifdef FEAT_TERMINAL
4671 if (bt_terminal(wp->w_buffer))
4672 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004673 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004674 {
4675 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004676 fill = wp->w_fill_chars.stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004677 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004678 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004679 {
4680 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004681 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02004682 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004683 }
4684 else
4685#endif
4686 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004688 *attr = HL_ATTR(HLF_S);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004689 fill = wp->w_fill_chars.stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691 else
4692 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004693 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004694 fill = wp->w_fill_chars.stlnc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004696 // Use fill when there is highlighting, and highlighting of current
4697 // window differs, or the fillchars differ, or this is not the
4698 // current window
Bram Moolenaar8820b482017-03-16 17:23:31 +01004699 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004700 || wp != curwin || ONE_WINDOW)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004701 || (wp->w_fill_chars.stl != wp->w_fill_chars.stlnc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02004703 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return '^';
4705 return '=';
4706}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708/*
4709 * Get the character to use in a separator between vertically split windows.
4710 * Get its attributes in "*attr".
4711 */
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02004712 int
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004713fillchar_vsep(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
Bram Moolenaar8820b482017-03-16 17:23:31 +01004715 *attr = HL_ATTR(HLF_C);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004716 if (*attr == 0 && wp->w_fill_chars.vert == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 return '|';
4718 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004719 return wp->w_fill_chars.vert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
4722/*
4723 * Return TRUE if redrawing should currently be done.
4724 */
4725 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004726redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
Bram Moolenaareb992cb2017-03-09 18:20:16 +01004728#ifdef FEAT_EVAL
4729 if (disable_redraw_for_testing)
4730 return 0;
4731 else
4732#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +02004733 return ((!RedrawingDisabled
4734#ifdef FEAT_EVAL
4735 || ignore_redraw_flag_for_testing
4736#endif
4737 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738}
4739
4740/*
4741 * Return TRUE if printing messages should currently be done.
4742 */
4743 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004744messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
Bram Moolenaara2a89732022-08-31 14:46:18 +01004746 return (!(p_lz && char_avail() && !KeyTyped));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747}
4748
Bram Moolenaare677df82019-09-02 22:31:11 +02004749/*
4750 * Compute columns for ruler and shown command. 'sc_col' is also used to
4751 * decide what the maximum length of a message on the status line can be.
4752 * If there is a status line for the last window, 'sc_col' is independent
4753 * of 'ru_col'.
4754 */
4755
4756#define COL_RULER 17 // columns needed by standard ruler
4757
4758 void
4759comp_col(void)
4760{
4761#if defined(FEAT_CMDL_INFO)
4762 int last_has_status = (p_ls == 2 || (p_ls == 1 && !ONE_WINDOW));
4763
4764 sc_col = 0;
4765 ru_col = 0;
4766 if (p_ru)
4767 {
4768# ifdef FEAT_STL_OPT
4769 ru_col = (ru_wid ? ru_wid : COL_RULER) + 1;
4770# else
4771 ru_col = COL_RULER + 1;
4772# endif
4773 // no last status line, adjust sc_col
4774 if (!last_has_status)
4775 sc_col = ru_col;
4776 }
4777 if (p_sc)
4778 {
4779 sc_col += SHOWCMD_COLS;
4780 if (!p_ru || last_has_status) // no need for separating space
4781 ++sc_col;
4782 }
4783 sc_col = Columns - sc_col;
4784 ru_col = Columns - ru_col;
4785 if (sc_col <= 0) // screen too narrow, will become a mess
4786 sc_col = 1;
4787 if (ru_col <= 0)
4788 ru_col = 1;
4789#else
4790 sc_col = Columns;
4791 ru_col = Columns;
4792#endif
4793#ifdef FEAT_EVAL
4794 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
4795#endif
4796}
4797
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004798#if defined(FEAT_LINEBREAK) || defined(PROTO)
4799/*
Bram Moolenaar64486672010-05-16 15:46:46 +02004800 * Return the width of the 'number' and 'relativenumber' column.
4801 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004802 * Otherwise it depends on 'numberwidth' and the line count.
4803 */
4804 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004805number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004806{
4807 int n;
4808 linenr_T lnum;
4809
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004810 if (wp->w_p_rnu && !wp->w_p_nu)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004811 // cursor line shows "0"
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004812 lnum = wp->w_height;
4813 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004814 // cursor line shows absolute line number
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004815 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +02004816
Bram Moolenaar6b314672015-03-20 15:42:10 +01004817 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004818 return wp->w_nrwidth_width;
4819 wp->w_nrwidth_line_count = lnum;
4820
4821 n = 0;
4822 do
4823 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004824 lnum /= 10;
4825 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004826 } while (lnum > 0);
4827
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004828 // 'numberwidth' gives the minimal width plus one
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004829 if (n < wp->w_p_nuw - 1)
4830 n = wp->w_p_nuw - 1;
4831
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004832# ifdef FEAT_SIGNS
4833 // If 'signcolumn' is set to 'number' and there is a sign to display, then
4834 // the minimal width for the number column is 2.
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01004835 if (n < 2 && get_first_valid_sign(wp) != NULL
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004836 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
4837 n = 2;
4838# endif
4839
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004840 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +01004841 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004842 return n;
4843}
4844#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004845
Bram Moolenaar113e1072019-01-20 15:30:40 +01004846#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004847/*
4848 * Return the current cursor column. This is the actual position on the
4849 * screen. First column is 0.
4850 */
4851 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004852screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004853{
4854 return screen_cur_col;
4855}
4856
4857/*
4858 * Return the current cursor row. This is the actual position on the screen.
4859 * First row is 0.
4860 */
4861 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004862screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +01004863{
4864 return screen_cur_row;
4865}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004866#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004867
4868/*
Bram Moolenaar93ff6722021-10-16 17:51:40 +01004869 * Calls mb_ptr2char_adv(p) and returns the character.
4870 * If "p" starts with "\x", "\u" or "\U" the hex or unicode value is used.
4871 */
4872 static int
4873get_encoded_char_adv(char_u **p)
4874{
4875 char_u *s = *p;
4876
4877 if (s[0] == '\\' && (s[1] == 'x' || s[1] == 'u' || s[1] == 'U'))
4878 {
4879 varnumber_T num = 0;
4880 int bytes;
4881 int n;
4882
4883 for (bytes = s[1] == 'x' ? 1 : s[1] == 'u' ? 2 : 4; bytes > 0; --bytes)
4884 {
4885 *p += 2;
4886 n = hexhex2nr(*p);
4887 if (n < 0)
4888 return 0;
4889 num = num * 256 + n;
4890 }
4891 *p += 2;
4892 return num;
4893 }
4894 return mb_ptr2char_adv(p);
4895}
4896
4897/*
Bram Moolenaare677df82019-09-02 22:31:11 +02004898 * Handle setting 'listchars' or 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004899 * "varp" points to either the global or the window-local value.
4900 * When "apply" is FALSE do not store the flags, only check for errors.
Bram Moolenaareed9d462021-02-15 20:38:25 +01004901 * Assume monocell characters.
Bram Moolenaare677df82019-09-02 22:31:11 +02004902 * Returns error message, NULL if it's OK.
4903 */
4904 char *
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004905set_chars_option(win_T *wp, char_u **varp, int apply)
Bram Moolenaare677df82019-09-02 22:31:11 +02004906{
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004907 int round, i, len, len2, entries;
4908 char_u *p, *s;
4909 int c1 = 0, c2 = 0, c3 = 0;
4910 char_u *last_multispace = NULL; // Last occurrence of "multispace:"
4911 char_u *last_lmultispace = NULL; // Last occurrence of "leadmultispace:"
4912 int multispace_len = 0; // Length of lcs-multispace string
4913 int lead_multispace_len = 0; // Length of lcs-leadmultispace string
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004914 int is_listchars = (varp == &p_lcs || varp == &wp->w_p_lcs);
4915 char_u *value = *varp;
4916
Bram Moolenaare677df82019-09-02 22:31:11 +02004917 struct charstab
4918 {
4919 int *cp;
4920 char *name;
4921 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004922 struct charstab *tab;
4923
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004924 static fill_chars_T fill_chars;
Bram Moolenaare677df82019-09-02 22:31:11 +02004925 static struct charstab filltab[] =
4926 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004927 {&fill_chars.stl, "stl"},
4928 {&fill_chars.stlnc, "stlnc"},
4929 {&fill_chars.vert, "vert"},
4930 {&fill_chars.fold, "fold"},
4931 {&fill_chars.foldopen, "foldopen"},
4932 {&fill_chars.foldclosed, "foldclose"},
4933 {&fill_chars.foldsep, "foldsep"},
4934 {&fill_chars.diff, "diff"},
4935 {&fill_chars.eob, "eob"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004936 };
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004937
Bram Moolenaar333bd562021-02-16 22:22:13 +01004938 static lcs_chars_T lcs_chars;
Bram Moolenaareed9d462021-02-15 20:38:25 +01004939 struct charstab lcstab[] =
Bram Moolenaare677df82019-09-02 22:31:11 +02004940 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01004941 {&lcs_chars.eol, "eol"},
4942 {&lcs_chars.ext, "extends"},
4943 {&lcs_chars.nbsp, "nbsp"},
4944 {&lcs_chars.prec, "precedes"},
4945 {&lcs_chars.space, "space"},
4946 {&lcs_chars.tab2, "tab"},
4947 {&lcs_chars.trail, "trail"},
4948 {&lcs_chars.lead, "lead"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004949#ifdef FEAT_CONCEAL
Bram Moolenaar333bd562021-02-16 22:22:13 +01004950 {&lcs_chars.conceal, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004951#else
Bram Moolenaar333bd562021-02-16 22:22:13 +01004952 {NULL, "conceal"},
Bram Moolenaare677df82019-09-02 22:31:11 +02004953#endif
4954 };
Bram Moolenaare677df82019-09-02 22:31:11 +02004955
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004956 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004957 {
4958 tab = lcstab;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004959 CLEAR_FIELD(lcs_chars);
K.Takataeeec2542021-06-02 13:28:16 +02004960 entries = ARRAY_LENGTH(lcstab);
Bram Moolenaareed9d462021-02-15 20:38:25 +01004961 if (varp == &wp->w_p_lcs && wp->w_p_lcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004962 value = p_lcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004963 }
4964 else
4965 {
4966 tab = filltab;
K.Takataeeec2542021-06-02 13:28:16 +02004967 entries = ARRAY_LENGTH(filltab);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004968 if (varp == &wp->w_p_fcs && wp->w_p_fcs[0] == NUL)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004969 value = p_fcs; // local value is empty, us the global value
Bram Moolenaare677df82019-09-02 22:31:11 +02004970 }
4971
4972 // first round: check for valid value, second round: assign values
4973 for (round = 0; round <= 1; ++round)
4974 {
4975 if (round > 0)
4976 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004977 // After checking that the value is valid: set defaults.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004978 if (is_listchars)
Bram Moolenaare677df82019-09-02 22:31:11 +02004979 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004980 for (i = 0; i < entries; ++i)
4981 if (tab[i].cp != NULL)
4982 *(tab[i].cp) = NUL;
Bram Moolenaar333bd562021-02-16 22:22:13 +01004983 lcs_chars.tab1 = NUL;
4984 lcs_chars.tab3 = NUL;
zeertzjqb5f08012022-06-09 13:55:28 +01004985
Mike Williamsf5785cf2021-09-13 22:17:38 +02004986 if (multispace_len > 0)
zeertzjqf14b8ba2021-09-10 16:58:30 +02004987 {
4988 lcs_chars.multispace = ALLOC_MULT(int, multispace_len + 1);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01004989 if (lcs_chars.multispace != NULL)
4990 lcs_chars.multispace[multispace_len] = NUL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02004991 }
4992 else
4993 lcs_chars.multispace = NULL;
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004994
4995 if (lead_multispace_len > 0)
4996 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01004997 lcs_chars.leadmultispace =
4998 ALLOC_MULT(int, lead_multispace_len + 1);
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01004999 lcs_chars.leadmultispace[lead_multispace_len] = NUL;
5000 }
5001 else
5002 lcs_chars.leadmultispace = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02005003 }
5004 else
Bram Moolenaara98f8a22021-02-13 18:24:23 +01005005 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005006 fill_chars.stl = ' ';
5007 fill_chars.stlnc = ' ';
5008 fill_chars.vert = ' ';
5009 fill_chars.fold = '-';
5010 fill_chars.foldopen = '-';
5011 fill_chars.foldclosed = '+';
5012 fill_chars.foldsep = '|';
5013 fill_chars.diff = '-';
5014 fill_chars.eob = '~';
Bram Moolenaara98f8a22021-02-13 18:24:23 +01005015 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005016 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005017 p = value;
Bram Moolenaare677df82019-09-02 22:31:11 +02005018 while (*p)
5019 {
5020 for (i = 0; i < entries; ++i)
5021 {
5022 len = (int)STRLEN(tab[i].name);
5023 if (STRNCMP(p, tab[i].name, len) == 0
5024 && p[len] == ':'
5025 && p[len + 1] != NUL)
5026 {
5027 c2 = c3 = 0;
5028 s = p + len + 1;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005029 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005030 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005031 return e_invalid_argument;
Bram Moolenaar333bd562021-02-16 22:22:13 +01005032 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005033 {
5034 if (*s == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005035 return e_invalid_argument;
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005036 c2 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005037 if (char2cells(c2) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005038 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005039 if (!(*s == ',' || *s == NUL))
5040 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005041 c3 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005042 if (char2cells(c3) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005043 return e_invalid_argument;
Bram Moolenaare677df82019-09-02 22:31:11 +02005044 }
5045 }
5046
5047 if (*s == ',' || *s == NUL)
5048 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005049 if (round > 0)
Bram Moolenaare677df82019-09-02 22:31:11 +02005050 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005051 if (tab[i].cp == &lcs_chars.tab2)
Bram Moolenaare677df82019-09-02 22:31:11 +02005052 {
Bram Moolenaar333bd562021-02-16 22:22:13 +01005053 lcs_chars.tab1 = c1;
5054 lcs_chars.tab2 = c2;
5055 lcs_chars.tab3 = c3;
Bram Moolenaare677df82019-09-02 22:31:11 +02005056 }
5057 else if (tab[i].cp != NULL)
5058 *(tab[i].cp) = c1;
5059
5060 }
5061 p = s;
5062 break;
5063 }
5064 }
5065 }
5066
5067 if (i == entries)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005068 {
Mike Williamsf5785cf2021-09-13 22:17:38 +02005069 len = (int)STRLEN("multispace");
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005070 len2 = (int)STRLEN("leadmultispace");
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005071 if (is_listchars
zeertzjqf14b8ba2021-09-10 16:58:30 +02005072 && STRNCMP(p, "multispace", len) == 0
5073 && p[len] == ':'
5074 && p[len + 1] != NUL)
5075 {
5076 s = p + len + 1;
5077 if (round == 0)
5078 {
5079 // Get length of lcs-multispace string in first round
5080 last_multispace = p;
5081 multispace_len = 0;
5082 while (*s != NUL && *s != ',')
5083 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005084 c1 = get_encoded_char_adv(&s);
zeertzjq60618c82021-12-18 15:32:46 +00005085 if (char2cells(c1) > 1)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005086 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005087 ++multispace_len;
5088 }
5089 if (multispace_len == 0)
5090 // lcs-multispace cannot be an empty string
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005091 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005092 p = s;
5093 }
5094 else
5095 {
5096 int multispace_pos = 0;
Mike Williamsf5785cf2021-09-13 22:17:38 +02005097
zeertzjqf14b8ba2021-09-10 16:58:30 +02005098 while (*s != NUL && *s != ',')
5099 {
Bram Moolenaar93ff6722021-10-16 17:51:40 +01005100 c1 = get_encoded_char_adv(&s);
zeertzjqf14b8ba2021-09-10 16:58:30 +02005101 if (p == last_multispace)
5102 lcs_chars.multispace[multispace_pos++] = c1;
5103 }
5104 p = s;
5105 }
5106 }
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005107
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005108 else if (is_listchars
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005109 && STRNCMP(p, "leadmultispace", len2) == 0
5110 && p[len2] == ':'
5111 && p[len2 + 1] != NUL)
5112 {
5113 s = p + len2 + 1;
5114 if (round == 0)
5115 {
zeertzjqb5f08012022-06-09 13:55:28 +01005116 // get length of lcs-leadmultispace string in first
5117 // round
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005118 last_lmultispace = p;
5119 lead_multispace_len = 0;
5120 while (*s != NUL && *s != ',')
5121 {
5122 c1 = get_encoded_char_adv(&s);
5123 if (char2cells(c1) > 1)
5124 return e_invalid_argument;
5125 ++lead_multispace_len;
5126 }
5127 if (lead_multispace_len == 0)
zeertzjqb5f08012022-06-09 13:55:28 +01005128 // lcs-leadmultispace cannot be an empty string
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01005129 return e_invalid_argument;
5130 p = s;
5131 }
5132 else
5133 {
5134 int multispace_pos = 0;
5135
5136 while (*s != NUL && *s != ',')
5137 {
5138 c1 = get_encoded_char_adv(&s);
5139 if (p == last_lmultispace)
5140 lcs_chars.leadmultispace[multispace_pos++] = c1;
5141 }
5142 p = s;
5143 }
5144 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005145 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005146 return e_invalid_argument;
zeertzjqf14b8ba2021-09-10 16:58:30 +02005147 }
5148
Bram Moolenaare677df82019-09-02 22:31:11 +02005149 if (*p == ',')
5150 ++p;
5151 }
5152 }
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005153
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005154 if (apply)
zeertzjqf14b8ba2021-09-10 16:58:30 +02005155 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005156 if (is_listchars)
5157 {
5158 vim_free(wp->w_lcs_chars.multispace);
5159 vim_free(wp->w_lcs_chars.leadmultispace);
5160 wp->w_lcs_chars = lcs_chars;
5161 }
5162 else
5163 {
5164 wp->w_fill_chars = fill_chars;
5165 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02005166 }
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005167 else if (is_listchars)
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005168 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01005169 vim_free(lcs_chars.multispace);
5170 vim_free(lcs_chars.leadmultispace);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01005171 }
Bram Moolenaare677df82019-09-02 22:31:11 +02005172
5173 return NULL; // no error
5174}
zeertzjq8ca29b62022-08-09 12:53:14 +01005175
5176/*
5177 * Check all global and local values of 'listchars' and 'fillchars'.
5178 * Return an untranslated error messages if any of them is invalid, NULL
5179 * otherwise.
5180 */
5181 char *
5182check_chars_options(void)
5183{
5184 tabpage_T *tp;
5185 win_T *wp;
5186
5187 if (set_chars_option(curwin, &p_lcs, FALSE) != NULL)
5188 return e_conflicts_with_value_of_listchars;
5189 if (set_chars_option(curwin, &p_fcs, FALSE) != NULL)
5190 return e_conflicts_with_value_of_fillchars;
5191 FOR_ALL_TAB_WINDOWS(tp, wp)
5192 {
5193 if (set_chars_option(wp, &wp->w_p_lcs, FALSE) != NULL)
5194 return e_conflicts_with_value_of_listchars;
5195 if (set_chars_option(wp, &wp->w_p_fcs, FALSE) != NULL)
5196 return e_conflicts_with_value_of_fillchars;
5197 }
5198 return NULL;
5199}